Keras embedding layer set_weights() error on google colab

Viewed 268

I am trying to manually set new weights for the Keras embedding layer of an LSTM model I created on google colab (tf version 2.6.0) using this code:

model4Ax.layers[0].set_weights(emb4A)

emb4A is the embedding I created by combining multiple embeddings but still with the same dimension as the original

but I'm getting this error:

ValueError: You called set_weights(weights) on layer "embedding" with a weight list of length 500, but the layer was expecting 1 weights. Provided weights: [[-0.03877353 -0.02181869 -0.06819688 ... 0.02823...

Is this familiar to anyone? I need help!

1 Answers

I eventually found my mistake. Even though my embedding weights value was in the right shape, set_weight() expects a weight as a list of NumPy arrays. All I had to do was wrap my weights in [] as seen below:

model4Ax.layers[0].set_weights([emb4A])
Related