0

I have a problem. I have trained a model and I would now calculate metrics for each epoch. How can I do this ? How can I access each individual epoch and calculate the metrics?

CALLBACKS = [tf.keras.callbacks.ModelCheckpoint(
    filepath=Path(logpath, 'model_checkpoint-{epoch:02d}-{val_loss:.2f}.h5'),
    verbose=1,
    save_weights_only=False,
    save_freq='epoch'), tensorboard]
loss = keras.losses.categorical_crossentropy
optim = keras.optimizers.Adam(learning_rate=0.0009)
metrics = ["accuracy"]

model.compile(loss=loss ,optimizer = optim, metrics=metrics)
history = model.fit(train_X, train_y, batch_size=32, epochs=10, validation_data=(test_X, test_y), callbacks=CALLBACKS)

What I want

          precision    recall    f1-score    support
0              0.45      0.45        0.45       1000  
1              0.47      0.55        0.48       1000  
...             ...       ...         ...        ...
98             0.55      0.65        0.78       1000  
99             0.65      0.75        0.79       1000  

accuarcy                             0.83     100000
marco avg      0.83      0.83        0.83     100000        
weghted avg    0.83      0.83        0.83     100000
Test
  • 571
  • 13
  • 32
  • See [here](https://stackoverflow.com/questions/36895627/python-keras-creating-a-callback-with-one-prediction-for-each-epoch) how to make a prediction for each epoch. Then you can just add the metrics in a similar way to [this](https://stackoverflow.com/questions/50568409/report-keras-model-evaluation-metrics-every-10-epochs). – Ignatius Reilly Jul 10 '22 at 18:34
  • Thank you, however this is during the `model.fit` method. I would like to calculate it at the very end. After the `model.fit` method. – Test Jul 11 '22 at 06:27
  • Then you wouldn't be calculating then for each _epoch_, if I understand what an epoch is.... So, make a prediction against your test set (or validation set) and calculate the metrics, for example with [classification_report](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html)? – Ignatius Reilly Jul 11 '22 at 07:30

0 Answers0