我正在使用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时,会出现此错误。
任何帮助,不胜感激,在此先感谢
试试这个更新的代码:您必须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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句