gpt4 book ai didi

python - Keras:功能性 API 嵌入层的输入层应该是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:30 25 4
gpt4 key购买 nike

我正在使用 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)

关于python - Keras:功能性 API 嵌入层的输入层应该是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com