Write Epochs Results during Training to CSV File in TensorFlow 2

Write Epochs Results during Training to CSV File in TensorFlow 2

TensorFlow 2 provides the CSVLogger callback which allows to write epochs results during training to a CSV file. After that file can be opened and results can be interpretated by our desired tools.

We create a model that solves a simple linear regression problem. An instance of CSVLogger are passed to the fit method as callbacks argument.

from tensorflow import keras
import numpy as np

xs = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0, 9.0], dtype=float)

model = keras.Sequential([
    keras.layers.Dense(1, input_shape=[1])
])

model.compile(optimizer='sgd', loss='mean_squared_error')

csvLogger = keras.callbacks.CSVLogger('training.log')

model.fit(xs, ys, epochs=100, callbacks=[csvLogger])

CSV file contains loss values for each epoch.

epoch,loss
0,43.32571029663086
1,34.74224853515625
2,27.86192512512207
3,22.346723556518555
4,17.925708770751953
..........
98,0.004555319901555777
99,0.00441718939691782

Leave a Comment

Cancel reply

Your email address will not be published.