Home > Software engineering >  AttributeError: 'History' object has no attribute 'save'
AttributeError: 'History' object has no attribute 'save'

Time:12-23

source: tensorflow tutorials - Save and Load

I trained my model with EfficientNetB0 from tensorflow hub It went well when fit.

# Fit EfficientNet model
efficientnet_history = efficientnet_model.fit(train_data,
                                              epochs=2,
                                              steps_per_epoch=len(train_data),
                                              validation_data=test_data,
                                              validation_steps=len(test_data),
                                              callbacks = [learning_rate_reduction, modelCheckpoint])

results:

2022-12-23 11:05:43.012196: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/2
2022-12-23 11:05:51.883413: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8302
11/11 [==============================] - 20s 819ms/step - loss: 2.1915 - accuracy: 0.2649 - val_loss: 1.9363 - val_accuracy: 0.3286

Epoch 00001: saving model to training_1\cp.ckpt
Epoch 2/2
11/11 [==============================] - 6s 580ms/step - loss: 1.8275 - accuracy: 0.5000 - val_loss: 1.5922 - val_accuracy: 0.6429

Epoch 00002: saving model to training_1\cp.ckpt

But why this model cannot be saved with efficientnet_history.save? my code:

efficientnet_history.save("signlen_efficientnet_model.h5")

error:

Traceback (most recent call last):
  File "c:/Users/OOO/Desktop/Projects/SignLens/test/ASL_for_fun/asl_train.py", line 89, in <module>
    efficientnet_history.save("signlen_efficientnet_model.h5")
AttributeError: 'History' object has no attribute 'save'

How to save model correctly? Or, is there another way to save model?

CodePudding user response:

The reason you are getting this error is because you are calling save on a History object. You should call save on the model itself. For example, change this:

efficientnet_history.save("signlen_efficientnet_model.h5")

To this:

efficientnet_model.save("signlen_efficientnet_model.h5")
  • Related