Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras : history not accessible for loss or accuracy

I am training a CNN in Keras with Tensorflow backend,

mod1=gmodel.fit(images, train_labels,
      batch_size=100,
      epochs=2,
      verbose=1,
      validation_data=(test_images, test_labels))

and at every epoch I can see printed in the output the accuracy and loss (until here everything seems ok).

Epoch 1/10
1203/1203 [==============================] - 190s - loss: 0.7600 - acc: 0.5628 
- val_loss: 0.5592 - val_acc: 0.6933
Epoch 2/10
1203/1203 [==============================] - 187s - loss: 0.5490 - acc: 0.6933 
- val_loss: 0.4589 - val_acc: 0.7930
Epoch 3/10
....

At the end, I want to plot the validation loss so in previous projects I have accessed the validation loss via

mod1.history['val_loss']

but I am getting an error as if .history() was empty.

TypeError                                 Traceback (most recent call last)
<ipython-input-23-ecdd306e9232> in <module>()
----> 1 modl.history()
TypeError: 'History' object is not callable

EDIT (after answer below): When I try to access the loss, for example:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-06fcc6efb374> in <module>()
----> 1 mod1.history['val_loss']

TypeError: 'History' object is not subscriptable

I haven't found anything like this problem before, so I am lost as to what could be happening or how to debug.

Any pointers or ideas are greatly appreciated.

like image 414
user3177938 Avatar asked Nov 25 '25 09:11

user3177938


2 Answers

model.fit(x_train, y_train,batch_size=128,validation_data=(x_test, y_test))

vy = model.history.history['val_loss']
ty = model.history.history['loss']

Please use the validation_data in model.fit statement for test data then the only "model.history.history" will come

Reference: https://keras.io/callbacks/

like image 119
Yatin Arora Avatar answered Nov 28 '25 00:11

Yatin Arora


Although you say you have called mod1.history['val_loss'], your error message tells a different story - most probably, as Daniel Moller has already commented, you have in fact used something like mod1.history() (i.e. with parentheses). Here is what I get (Python 3.5):

mod1.history()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-67bafe3187cc> in <module>()
----> 1 mod1.history()

TypeError: 'dict' object is not callable

mod1.history is not a function to be called with (), but a Python dictionary:

mod1.history
# result:
{'acc': [0.82374999999999998,
  0.94294999999999995,
  0.95861666666666667,
  ...],
 'loss': [0.62551526172161098,
  0.18810810926556587,
  0.13734668906728426,
  ...],
 'val_loss': [12.05395287322998,
  11.584557554626464,
  10.949809835815429,
  ...]}

mod1.history['val_loss']
# result:
[12.05395287322998,
 11.584557554626464,
 10.949809835815429,
 ...]
like image 27
desertnaut Avatar answered Nov 28 '25 00:11

desertnaut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!