Send Epochs Results during Training to Server in TensorFlow 2

TensorFlow 2 provides the RemoteMonitor callback which allows to send epochs results during training to a server. A server can save results to a file, database table or perform other operations.

We created an instance of RemoteMonitor. We passed the root URL of the target server to constructor. The path parameter defines a root-relative path. A full URL consists of root URL and path.

Requests are sent using HTTP POST method. By default, JSON-encoded data is sent within a form with data key. It can be changed with field parameter.

train.py

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')

rootUrl = 'http://127.0.0.1'
remoteMonitor = keras.callbacks.RemoteMonitor(rootUrl, path='', field='results')

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

To handle requests we use Apache HTTP Server and PHP script. Epochs results are written to a file.

index.php

<?php

if (isset($_POST['results'])) {
    file_put_contents('training.log', $_POST['results'].PHP_EOL, FILE_APPEND);
}

A file contains loss values in JSON format for each epoch.

{"epoch": 0, "loss": 21.40224266052246}
{"epoch": 1, "loss": 17.193668365478516}
{"epoch": 2, "loss": 13.819204330444336}
{"epoch": 3, "loss": 11.113334655761719}
{"epoch": 4, "loss": 8.94339656829834}
..........
{"epoch": 98, "loss": 0.011419638991355896}
{"epoch": 99, "loss": 0.011073360219597816}

Leave a Comment

Your email address will not be published. Required fields are marked *