Python: Neural Network - TypeError: 'History' object is not subscriptable
The accepted answer is great. However, in case anyone is trying to access history without storing it during fit, try the following:
Since val_loss
is not an attribute on the History
object and not a key that you can index with, the way you wrote it won't work. However, what you can try is to access the attribute history
in the History
object, which is a dict that should contain val_loss
as a key.
so, replace:
plt.plot(model1.history['val_loss'], 'r', model2.history['val_loss'], 'b',
model3.history['val_loss'], 'g')
with
plt.plot(model1.history.history['val_loss'], 'r', model2.history.history['val_loss'], 'b',
model3.history.history['val_loss'], 'g')
Call to model.fit()
returns a History
object that has a member history
, which is of type dict
.
So you can replace :
model2.fit(X, y, validation_split=0.33, epochs=30, callbacks=
[early_stopping_monitor], verbose=False)
with
history2 = model2.fit(X, y, validation_split=0.33, epochs=30, callbacks=
[early_stopping_monitor], verbose=False)
Similarly for other models.
and then you can use :
plt.plot(history1.history['val_loss'], 'r', history2.history['val_loss'], 'b',
history3.history['val_loss'], 'g')