如何记录从TensorFlow到CSV文件的结果

发布于 2025-01-24 02:14:14 字数 114 浏览 0 评论 0原文

我有一个在TensorFlow上运行的CNN模型,并希望保存准确性,损失,F1,精度和回忆值,因为我还想保存这些图和混淆矩阵(您可以将这些剧情保存到CSV吗?)。 如何将每个模型运行到CSV或文本文件中保存此数据?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2025-01-31 02:14:14

尝试使用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
epochbinary_accuracylossrecallval_binary_accuracyval_lossval_recall
000.440.7917730.4583330.420.7282960.481481
110.540.679280.5416670.420.7093470.259259
220.620.6646610.3750.440.7138290.222222
330.620.6369190.3750.440.7340330.37037
440.680.5869070.5416670.480.7975420.444444

After training,您可以轻松地使用CSV文件进行绘图。

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
epochbinary_accuracylossrecallval_binary_accuracyval_lossval_recall
000.440.7917730.4583330.420.7282960.481481
110.540.679280.5416670.420.7093470.259259
220.620.6646610.3750.440.7138290.222222
330.620.6369190.3750.440.7340330.37037
440.680.5869070.5416670.480.7975420.444444

After training, you can easily use the csv file for plotting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文