Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

cannot load a trained tf.keras model on google colab Pro immediately after it is saved

We are using TF.2.4.0 and have multiple models generated as follows:

model = models [model_name]
    model.compile (optimizer = tf.keras.optimizers.RMSprop (lr=0.01),
                   loss = tf.keras.losses.Huber (),
                   metrics = metrics_list)
    history = model.fit (train_generator, steps_per_epoch = train_steps, epochs = epochs,
                         validation_data = val_generator, validation_steps = val_steps, verbose=1)
    model.save (f'{model_name}_{epochs}.h5')

But when we load them via

models = {}
models ['dense'] = tf.keras.models.load_model('myDense_2.h5’)

We get the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-0da6ff3dd6dd> in <module>()
      1 models = {}
      2 
----> 3 models ['dense'] = tf.keras.models.load_model('myDense_2.h5')



13 frames

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py in build(self, input_shape)
   1180     last_dim = tensor_shape.dimension_value(input_shape[-1])
   1181     if last_dim is None:
-> 1182       raise ValueError('The last dimension of the inputs to `Dense` '
   1183                        'should be defined. Found `None`.')
   1184     self.input_spec = InputSpec(min_ndim=2, axes={-1: last_dim})

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

Any ideas why we are getting this error, please?

Any help would be much appreciated.

question from:https://stackoverflow.com/questions/65838525/cannot-load-a-trained-tf-keras-model-on-google-colab-pro-immediately-after-it-is

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...