【问题标题】:Attribute error: None type has no attribute summary in keras属性错误:Keras 中 None 类型没有属性摘要
【发布时间】:2019-08-13 20:08:00
【问题描述】:

我尝试深入了解 keras 中的词嵌入和 NLP 实现和复制使用功能 API 创建 Keras 模型的部分代码。当我启动 model.summary 时,我收到一个属性错误:None type has no attribute 'summary'。

在多次尝试减少层数之后,不幸的是词嵌入矩阵的维度没有改变。我不知道该怎么办。

def pretrained_embedding_layer(word_to_vec, word_to_index):


    vocab_len = len(word_to_index) + 1                  
    emb_dim = word_to_vec["sole"].shape[0]      
    emb_matrix = np.zeros((vocab_len,emb_dim))

    for word, index in word_to_index.items():
        emb_matrix[index, :] = word_to_vec[word]
    print(emb_matrix.shape)

    embedding_layer = Embedding(vocab_len,emb_dim,trainable =False)
    embedding_layer.build((None,))


    embedding_layer.set_weights([emb_matrix])

    return embedding_layer
def Chatbot_V1(input_shape, word_to_vec, word_to_index):

    # Define sentence_indices as the input of the graph, it should be of shape input_shape and dtype 'int32' (as it contains indices).
    sentence_indices = Input(input_shape, dtype='int32')

    # Create the embedding layer pretrained with GloVe Vectors (≈1 line)
    embedding_layer = pretrained_embedding_layer(word_to_vec, word_to_index)

    embeddings = embedding_layer(sentence_indices)   
    # Propagate the embeddings through an LSTM layer with 128-dimensional hidden state
    X = LSTM(128, return_sequences=True)(embeddings)

    # Add dropout with a probability of 0.5
    X = Dropout(0.5)(X)
    # Propagate X trough another LSTM layer with 128-dimensional hidden state
    # Be careful, the returned output should be a single hidden state, not a batch of sequences.
    X = LSTM(128, return_sequences=True)(X)
    # Add dropout with a probability of 0.5
    X = Dropout(0.5)(X)
    # Propagate X through a Dense layer with softmax activation to get back a batch of vocab_dim dimensional vectors.
    X = Dense(vocab_dim)(X)
    # Add a softmax activation
    preds = Activation('softmax')(X)

    # Create Model instance which converts sentence_indices into X.
    model = Model(sentence_indices, preds)

model = Chatbot_V1((maxLen,), word_to_vec, word_to_index)

model.summary()

启动model.summary: AttributeError: 'NoneType' 对象没有属性 'summary'

为什么?层定义有什么问题?

【问题讨论】:

    标签: object keras attributes nonetype


    【解决方案1】:

    函数Chatbot_V1 不返回任何内容,在python 中,如果将函数的返回值分配给变量,则None 会发出信号。所以只需使用return关键字返回Chatbot_V1末尾的模型

    【讨论】:

    • 你完全正确,我正在寻找一些非常奇怪的东西,而不是复制和粘贴错误。非常感谢您的帮助和时间。
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 2018-12-07
    • 2017-11-12
    • 2020-03-13
    • 2010-12-23
    • 2013-09-30
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多