Keras:函数式 API 对于嵌入层,输入层应该是什么?

Ziq*_*iqi 6 python keras

我正在使用 Keras 函数式 API 创建一个神经网络,该网络将词嵌入层作为句子分类任务的输入。但是我的代码在连接输入层和嵌入层的开始就中断了。按照https://medium.com/tensorflow/predicting-the-price-of-wine-with-the-keras-functional-api-and-tensorflow-a95d1c2c1b03 上的教程,我有如下代码:

max_seq_length=100 #i.e., sentence has a max of 100 words 
word_weight_matrix = ... #this has a shape of 9825, 300, i.e., the vocabulary has 9825 words and each is a 300 dimension vector 
deep_inputs = Input(shape=(max_seq_length,))
embedding = Embedding(9825, 300, input_length=max_seq_length,
                          weights=word_weight_matrix, trainable=False)(deep_inputs) # line A
hidden = Dense(targets, activation="softmax")(embedding)
model = Model(inputs=deep_inputs, outputs=hidden)
Run Code Online (Sandbox Code Playgroud)

然后行 A 导致错误,如下所示:

ValueError: You called `set_weights(weights)` on layer "embedding_1" with a  weight list of length 9825, but the layer was expecting 1 weights. Provided weights: [[-0.04057981  0.05743935  0.0109863  ...,  0.0072...
Run Code Online (Sandbox Code Playgroud)

而且我真的不明白错误意味着什么......

似乎输入层定义不正确......以前,当我使用 Sequential 模型与定义完全相同的嵌入层时,一切正常。但是当我切换到函数式 API 时,出现此错误。

非常感谢任何帮助,提前致谢

Sne*_*hal 5

试试这个更新的代码:你必须len(vocabulary) + 1在嵌入层中使用!和weights=[word_weight_matrix]

max_seq_length=100 #i.e., sentence has a max of 100 words 
word_weight_matrix = ... #this has a shape of 9825, 300, i.e., the vocabulary has 9825 words and each is a 300 dimension vector 
deep_inputs = Input(shape=(max_seq_length,))
embedding = Embedding(9826, 300, input_length=max_seq_length,
                      weights=[word_weight_matrix], trainable=False)(deep_inputs) # line A
hidden = Dense(targets, activation="softmax")(embedding)
model = Model(inputs=deep_inputs, outputs=hidden)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢, weights=[word_weight_matrix] 是修复。len(vocabulary) + 1 会导致另一个问题,尽管形状不兼容。当 len(词汇)起作用时。 (2认同)