我正在使用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)
然后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...
我真的不明白错误是什么意思...
似乎输入层定义不正确...以前,当我使用顺序模型并将嵌入层定义完全相同时,一切正常。但是当我切换到功能性API时,会出现此错误。
非常感谢任何帮助,在此先感谢
答案 0 :(得分:2)
请尝试以下更新的代码:您必须在嵌入层中使用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)