-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)
  • 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. – Karl Knechtel Nov 18 '20 at 09: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.... –  Nov 18 '20 at 09:34
  • I mean, what do you think the error *means*, first of all? – Karl Knechtel Nov 18 '20 at 09:36
  • 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. –  Nov 18 '20 at 09:38
  • 1
    Okay, and what do you suppose it tells you that it is specifically a `'NoneType' object` which has this problem? – Karl Knechtel Nov 18 '20 at 09:41
  • https://stackoverflow.com/a/9320883 – Marko Nov 18 '20 at 09:45
  • 2
    @Marko it doesn't help. –  Nov 18 '20 at 09:47

1 Answers1

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)
Xu Qiushi
  • 1,111
  • 1
  • 5
  • 10
  • I saved the model with `model.save(model_file, overwrite=True)` after doing `model.fit`. Can I still get the history from it? –  Nov 18 '20 at 09:49
  • 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. – Xu Qiushi Nov 18 '20 at 09:52
  • 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 –  Nov 18 '20 at 09:55
  • @Aline The history is not saved in the hdf5 file – Dr. Snoopy Nov 18 '20 at 09:58
  • Thanks, how do I reload it without changing anything to keep the history? –  Nov 18 '20 at 09:59
  • @Aline Reload what? If you did not save the history, it is effectively lost – Dr. Snoopy Nov 18 '20 at 10:01
  • Is the import right `from sklearn.externals.joblib import Memory` ? Which import should I use? –  Nov 18 '20 at 10:01
  • 1
    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) – Xu Qiushi Nov 18 '20 at 10:05
  • `TypeError: can't pickle _thread.RLock objects` How did you deal with this error while saving? –  Nov 18 '20 at 10:14
  • I havn't seen this before. Seem like you are using multi thread. – Xu Qiushi Nov 18 '20 at 10:16
  • You can test your joblib by save a simple object. import joblib joblib.dump({"1": 1}, "tmp/tmp.tmp") – Xu Qiushi Nov 18 '20 at 10:17