-1

I would like to load a model and display it in a plot. Unfortunately I get an error.

Why does this error occur at this point (Usually he should know?) and how do I solve it?

I've already investigated this bug and looked for questions like Python Math - TypeError: 'NoneType' object is not subscriptable

Why am I getting this error here history.history['loss']? Usually he should know that!

Error:

training_loss = history.history['loss']
TypeError: 'NoneType' object is not subscriptable

Code:

def load_model():
    history = tf.keras.models.load_model(path)
    return model

def get_loss(history):
    # Get training and test loss histories
    training_loss = history.history['loss'] # here is the error
    test_loss = history.history['val_loss']

    # Create count of the number of epochs
    epoch_count = range(1, len(training_loss) + 1)

    # Visualize loss history
    plt.plot(epoch_count, training_loss, 'r--')
    plt.plot(epoch_count, test_loss, 'b-')
    plt.legend(['Training Loss', 'Test Loss'])
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.show();

get_loss(load_model)

Edit:

history = model.fit(...)
model.save(model_file, overwrite=True)
7
  • 2
    "Why is this and how do I solve it?" If you are asking this question about an error this ordinary, and without being able to do any investigation yourself, then you need to take many steps back and thoroughly study the language fundamentals before trying to figure out machine learning. This is, like, flying before you can crawl. Commented Nov 18, 2020 at 9:32
  • 5
    Of course I did some research. I could rule out that it was before. It must have something to do with the code here.... Commented Nov 18, 2020 at 9:34
  • 4
    In general, the error means that you attempted to index an object that doesn't have that functionality. 'NoneType' object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the method. Commented Nov 18, 2020 at 9:38
  • 1
    Okay, and what do you suppose it tells you that it is specifically a 'NoneType' object which has this problem? Commented Nov 18, 2020 at 9:41
  • 2
    @Marko it doesn't help. Commented Nov 18, 2020 at 9:47

1 Answer 1

0

tf.keras.models.load_model

is a method to load model.

Model is not history.

In tensorflow. After you define a model. You train it with:

model = your defination
history = model.fit(**kwargs)

Then, you should save both your model and history. And load history to draw picture.

Which is :

model.save(your path)
joblib.dump(history, your history path)

Then you should load your history with:

history = joblib.load(your history path)
Sign up to request clarification or add additional context in comments.

11 Comments

I saved the model with model.save(model_file, overwrite=True) after doing model.fit. Can I still get the history from it?
No. At least in my understanding you can get it. You must get your history with history = model.fit(**kwargs). You can't get it from your trained model.
Thanks. Please have a look at my edit. So that's not how I save the history? Do I have to load the model and train again to get the history? I only have the .h5 file and want to get the history from it
@Aline The history is not saved in the hdf5 file
You can just use import joblib if your python version is high enough. Otherwise just use pip install joblib to install it. Then import joblib and use joblib.dump(history, your path)
|