3

I have a saved model in 'h5' format. I am trying to resume training and trying to load the weights of the optimizer after compiling the model using

model.optimizer.set_weights(weights_list)

where weights_list is a list of numpy arrays containing the optimizer weights that I manually extracted from the h5 file.

But I am getting the above error.

When I try model.optimizer.get_weights() I am getting an empty list.

I want to know how to initialize the optimizer weights without any error.

However, if I do

model = tf.keras.model.load_model('~/modelname.h5',custom_objects={},compile=True)

It works, that is I can resume training with the previously saved weights.

However, tf.keras.models.load_model doesn't always work and sometimes it requires one to build the model and use load_weights to initialize the model.

Siladittya
  • 1,156
  • 2
  • 13
  • 41

1 Answers1

0

Just ran into this myself. Running model._make_train_function() as per this question fixed the issue for me.

x = Input((50,))
out = Dense(1, activation='sigmoid')(x)
model = Model(x, out)
model.compile(optimizer='adam', loss='binary_crossentropy')

model.load_weights('weights.h5')
model._make_train_function()
with open('optimizer.pkl', 'rb') as f:
    weight_values = pickle.load(f)
model.optimizer.set_weights(weight_values)
tea_pea
  • 1,482
  • 14
  • 19
  • 3
    Hi, I'm also trying these lines of code but I get this error : AttributeError: 'Model' object has no attribute '_make_train_function', why this? – Ciccios_1518 Jan 20 '21 at 10:04