How to save the history epochs and plots in files from keras models

I have a question, that maybe is very simple, but I can't do it: I have 3 neural networks which are trained with 100 epochs, and I need to save all the history's train displayed at the end in a .txt file (i.e: time: 00s 0ms/step loss:... accuracy:... recall:... etc.), maybe it's easy, but also I need to plot each metric and its val_metric from each epoch and save that plots too, as images I guess (I know that maybe is nonsense to plot at epoch 1,2... but my professor ask us to do that). How can I do that? And, it's possible to save each line of the epoch and under it the correspondig plots in a file? I use Keras to build the model. This is the .compile() function that I use:

   import tensorflow_addons as tfa
   residual_network_1d_model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=[tf.keras.metrics.BinaryAccuracy(
        name='accuracy', dtype=None, threshold=0.5),tf.keras.metrics.Recall(name='Recall'),tf.keras.metrics.Precision(name='Precision'),tfa.metrics.F1Score(num_classes=27,average='macro'),
                    tf.keras.metrics.AUC(
        num_thresholds=200,
        curve=ROC,
        summation_method=interpolation,
        name=AUC,
        dtype=None,
        thresholds=None,
        multi_label=True,
        label_weights=None,
    )])

Thanks for your answers.

Topic colab matplotlib keras deep-learning python

Category Data Science


For the data that is printed to the terminal while training you should be able to use the CSVLogger callback which will save the output to a plain text file. For plotting the metrics you can use the metrics stored in the History object and plot them using a plotting library such as matplotlib and save them using the library specific function for saving the plot (matplotlib.pyplot.savefig for matplotlib).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.