ValueError: Layer weight shape (30522, 768) not compatible with provided weight shape ()

ValueError: Layer weight shape (30522, 768) not compatible with provided weight shape ()

我使用 BERT 进行了词嵌入,需要将其作为嵌入层提供给 Keras 模型,我得到的错误是

ValueError: Layer weight shape (30522, 768) not compatible with provided weight shape ()

模型是

embedding = Embedding(30522, 768, mask_zero=True)(sentence)
model.layers[1].set_weights([embedding_matrix])

您正在向 set_weights 传递一个列表列表:

embedding_matrix = [np.random.uniform(0,1, (30522, 768))]

sentence = Input((20,))
embedding = Embedding(30522, 768, mask_zero=True)(sentence)
model = Model(sentence, embedding)

model.layers[1].set_weights([embedding_matrix])

而您应该简单地传递一个数组列表:

embedding_matrix = np.random.uniform(0,1, (30522, 768))

sentence = Input((20,))
embedding = Embedding(30522, 768, mask_zero=True)(sentence)
model = Model(sentence, embedding)

model.layers[1].set_weights([embedding_matrix])