【问题标题】:Keras Embedding index resulted negative value during model trainingKeras Embedding index在模型训练期间导致负值
【发布时间】:2019-04-07 17:49:42
【问题描述】:

我正在尝试使用 glove.6B.50d.txt 文件作为我的模型训练的预训练嵌入矩阵作为基线。出于某种原因,我不断收到一条错误消息: InvalidArgumentError: indices[15,32] = -2147483648 is not in [0, 400001) [[{{node embedding_2/embedding_lookup}}]]

下面是我的代码:

def pretrained_embedding_layer(word_to_vec_map,word_to_index):
    voc_len=len(word_to_index)+1
    embed_dim=word_to_vec_map["the"].shape[0]
    embedding_matrix=np.zeros((voc_len,embed_dim),dtype=np.float32)
    for word,index in word_to_index.items():
        embedding_vector=word_to_vec_map.get(word)
        if embedding_vector is not None:
            embedding_matrix[index,:]=embedding_vector
    embedding_layer=Embedding(input_dim=voc_len,output_dim=embed_dim,weights=[embedding_matrix],trainable=False)
    embedding_layer.build((None,))
    embedding_layer.set_weights([embedding_matrix])
    return embedding_layer

def sentiment_model(input_shape,word_to_vec_map,word_to_index):
    sentence_indices=Input(shape=input_shape,dtype=tf.float32)
    embedding_layer=pretrained_embedding_layer(word_to_vec_map,word_to_index)
    embeddings=embedding_layer(sentence_indices)
    X=LSTM(100,)(embeddings)
    X=Dense(2,activation='softmax')(X)
    model = Model(inputs=sentence_indices,outputs=X)
    return model

【问题讨论】:

    标签: keras nlp embedding


    【解决方案1】:

    问题解决了。原因是我的输入样本比可用的预训练嵌入包含更多的单词

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2023-02-06
      • 2020-03-20
      • 1970-01-01
      相关资源
      最近更新 更多