set_weights stopped working on tensorflow 2

Viewed 424

Updating our code base from keras --> tensorflow.keras for unrelated reasons.

keras 2.3.1 tensorflow 2.1.0

This code works on keras, but fails on tf.keras:

weights = applications.VGG16(weights='imagenet', include_top=False).get_weights()
model.set_weights(tempweights)

Error:

You called `set_weights(weights)` on layer "model" with a  weight list of length 26, but the layer was expecting 32 weights. Provided weights: [array([[[[ 4.29470569e-01,  1.17273867e-01,  3.40...

using keras.applications.VGG16().get_weights() does not fix it, results in exact same error.

Have examined these github issues that look similar, but wasn't able to find the cause of my issue:

https://github.com/keras-team/keras/issues/7229 https://github.com/keras-team/keras/issues/4753

The following works:

model.weights[0] = weights[0]

It SEEMS Like keras.model.set_weights() had some behavior for dealing with mismatched weights, but tf.tensorflow.keras does not have this behavior?

Confirmed. Behavior has changed. Include_top=True fixed it in our specific case, because that made our models match up. Keras doesn't throw errors when models don't match, and I'm not sure exactly what it does in that case. Not going to investigate that though. Leaving the solution here.

0 Answers
Related