如何将结果从Tensorflow记录到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
val_recall00.440.7917730.420.7282960.481481<<10.540.679280.420.7093470.259259<20.620.3750.440.7138290.222222<30.620.6369190.3750.440.7340330.3703740.680.5869070.480.7975420.444444<

相关内容

  • 没有找到相关文章

最新更新