Home > Net >  tensorflow keras load models weights
tensorflow keras load models weights

Time:09-23

I have recently saved some models which I have trained in another machine, and didn't save it like I have seen in another models, with the h5 extension. I don't grasp yet how to load the weights. I can load the model, but without the weights means like nothing. Please help :-)

from keras.models import load_model
from keras.models import model_from_json

​
model_LSTM_rendimiento = keras.models
model_LSTM_super = keras.models
model_LSTM_primero = keras.models

model_LSTM_rendimiento.load_model('../model_LSTM_rendimiento')
model_LSTM_super.load_model('../model_LSTM_super')
model_LSTM_primero.load_model('../model_LSTM_primero')

model_LSTM_primero.load_weights('../model_LSTM_primero_weights')

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_186379/3422008780.py in <module>
     12 # model_LSTM_super.load_weights('../model_LSTM_super_weights')
     13 model_LSTM_primero.load_model('../model_LSTM_primero')
---> 14 model_LSTM_primero.load_weights('../model_LSTM_primero_weights')

AttributeError: module 'keras.models' has no attribute 'load_weights'

CodePudding user response:

Since you haven't saved the model in the h5 format, I'll assume you used the SavedModel format like this:

model.save('path/to/location')

If this is what you did, then loading the model like this is enough:

model = keras.models.load_model('path/to/location')

You don't have to load the weights separately; from the SavedModel documentation:

SavedModel is the more comprehensive save format that saves the model architecture, weights, and the traced Tensorflow subgraphs of the call functions. This enables Keras to restore both built-in layers as well as custom objects.

Your code:

from tensorflow import keras

model_LSTM_rendimiento = keras.models.load_model('../model_LSTM_rendimiento')
model_LSTM_super = keras.models.load_model('../model_LSTM_super')
model_LSTM_primero = keras.models.load_model('../model_LSTM_primero')
  • Related