I have a CNN model running on tensorflow and would like to save the accuracy, loss, f1, precision and recall values as , i also have plots and confusion matrix (can you save these plots to csv?)i would like to save. how can i save this data with each model run to a csv or text file?
CodePudding user response:
Try using tf.keras.callbacks.CSVLogger
:
import tensorflow as tf
import pandas as pd
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_dim=40))
model.add(tf.keras.layers.Dense(1, 'sigmoid'))
adam_opt = tf.keras.optimizers.Adam(0.1)
model.compile(loss='bce', optimizer=adam_opt, metrics=[tf.keras.metrics.BinaryAccuracy(name="binary_accuracy", dtype=None),
tf.keras.metrics.Recall()])
train_x = tf.random.normal((50, 40))
train_y = tf.random.uniform((50, 1), maxval=2, dtype=tf.int32)
val_x = tf.random.normal((50, 40))
val_y = tf.random.uniform((50, 1), maxval=2, dtype=tf.int32)
csv_logger = tf.keras.callbacks.CSVLogger('metrics.csv')
history = model.fit(train_x, train_y, epochs=5, validation_data=(val_x, val_y), callbacks=[csv_logger])
df = pd.read_csv('/content/metrics.csv')
print(df.to_markdown())
Epoch 1/5
2/2 [==============================] - 2s 563ms/step - loss: 0.7918 - binary_accuracy: 0.4400 - recall: 0.4583 - val_loss: 0.7283 - val_binary_accuracy: 0.4200 - val_recall: 0.4815
Epoch 2/5
2/2 [==============================] - 0s 62ms/step - loss: 0.6793 - binary_accuracy: 0.5400 - recall: 0.5417 - val_loss: 0.7093 - val_binary_accuracy: 0.4200 - val_recall: 0.2593
Epoch 3/5
2/2 [==============================] - 0s 92ms/step - loss: 0.6647 - binary_accuracy: 0.6200 - recall: 0.3750 - val_loss: 0.7138 - val_binary_accuracy: 0.4400 - val_recall: 0.2222
Epoch 4/5
2/2 [==============================] - 0s 68ms/step - loss: 0.6369 - binary_accuracy: 0.6200 - recall: 0.3750 - val_loss: 0.7340 - val_binary_accuracy: 0.4400 - val_recall: 0.3704
Epoch 5/5
2/2 [==============================] - 0s 69ms/step - loss: 0.5869 - binary_accuracy: 0.6800 - recall: 0.5417 - val_loss: 0.7975 - val_binary_accuracy: 0.4800 - val_recall: 0.4444
epoch | binary_accuracy | loss | recall | val_binary_accuracy | val_loss | val_recall | |
---|---|---|---|---|---|---|---|
0 | 0 | 0.44 | 0.791773 | 0.458333 | 0.42 | 0.728296 | 0.481481 |
1 | 1 | 0.54 | 0.67928 | 0.541667 | 0.42 | 0.709347 | 0.259259 |
2 | 2 | 0.62 | 0.664661 | 0.375 | 0.44 | 0.713829 | 0.222222 |
3 | 3 | 0.62 | 0.636919 | 0.375 | 0.44 | 0.734033 | 0.37037 |
4 | 4 | 0.68 | 0.586907 | 0.541667 | 0.48 | 0.797542 | 0.444444 |
After training, you can easily use the csv file for plotting.