如何将 Tensorflow 的结果记录到 CSV 文件
How to record the results from Tensorflow to CSV file
我在 tensorflow 上有一个 CNN 模型 运行,我想将准确度、损失、f1、精度和召回值保存为 ,我也有图和混淆矩阵(你能把这些图保存到 csv ?)我想保存。
我如何将每个模型的数据保存为 运行 csv 或文本文件?
尝试使用 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
训练完成后,可以轻松使用csv文件进行绘图。
我在 tensorflow 上有一个 CNN 模型 运行,我想将准确度、损失、f1、精度和召回值保存为 ,我也有图和混淆矩阵(你能把这些图保存到 csv ?)我想保存。 我如何将每个模型的数据保存为 运行 csv 或文本文件?
尝试使用 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 |
训练完成后,可以轻松使用csv文件进行绘图。