I have been saving my training history in keras as follows:

history = model.fit(X_train, Y_train, epochs=700, batch_size=128,validation_data=(X_cv, Y_cv))

np.save('./history_sim#', history)

I am then trying to load the training history from the various simulations in order to print figures like loss vs. epoch, etc. as follows:

history = np.load('history_sim#.npy')

When I try to load the training history, I am receiving the following error message:

"ValueError: Unknown metric function:precision"

I am very worried that I have lost all of the training history now. Training takes several days and I am on a bit of a time crunch. Is the data lost or is there some way to get the data from the .npy file where I have saved it?

I already figured out that it works perfectly if I save/load as follows:

np.save('./history_sim#', history.history)

I will do that for the rest of the simulations, but there are already a few that were saved the first way and I do not have time to re-run them.


Additional Info:

import keras_metrics

# Calculate precision for the second label.
precision = keras_metrics.precision(label=1)

# Calculate recall for the first label.
recall = keras_metrics.recall(label=0)

opt = Adam(lr=0.05, beta_1=0.9, beta_2=0.999, decay=0.0)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[precision, recall])
  • $\begingroup$ You shouldn't be saving them that way, use a logger? $\endgroup$
    – Aditya
    Apr 29, 2019 at 16:57
  • $\begingroup$ Thanks for your reply, but it would be very helpful if when you say I should not do something in a certain way to also explain why not. I already figured out how to save the data in a way that I can later load it as shown in my question, I just need help getting the training data from the .npy files in which I have already saved the keras.callbacks.History object instead of the underlying dictionary. $\endgroup$
    – Ben Groene
    Apr 30, 2019 at 8:58
  • $\begingroup$ The history property of this object is a dict with average accuracy and average loss information for each epoch, so *.pkl would be a better choice. I have asked few folks to help if possible recovering the same but I am sorry I don't have expertise on that, maybe you could share a dummy things and people can then experiment on it. $\endgroup$
    – Aditya
    Apr 30, 2019 at 9:04
  • $\begingroup$ Also I hope you didn't pass the metric as a string because then we do see this error iirc.(waiting for folks to help you) $\endgroup$
    – Aditya
    Apr 30, 2019 at 9:16
  • $\begingroup$ Thanks a lot, Aditya! I have also added how I pass the metrics to the question. $\endgroup$
    – Ben Groene
    Apr 30, 2019 at 10:49

2 Answers 2


use allow_pickle=True in np.load

>>>item = np.load("a.npy",allow_pickle=True).item()

Not tested, but I don't have enough reputation to comment:

You might have to covert history to a numpy array first.

import numpy
numpy_history = numpy.array(history)
numpy.save("loss_history", numpy_history)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.