Usage Example tf.keras

tf.keras - example code [link to example]

Note

This code was tested with Tensorflow v1.10.1

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# define folds used by this run
train_fold = Fold(
    data=(x_train, y_train),
    foldId="fashion-mnist_train",
    dataset_config="fashion-mnist.yml",
)
test_fold = Fold(
    data=(x_test, y_test),
    foldId="fashion-mnist_test",
    dataset_config="fashion-mnist.yml",
)

# create new run object with the folds that should be logged
run = Run(
    runId="example_logs_tf.keras",
    folds=[train_fold, test_fold],
    trainfoldId="fashion-mnist_train",
)

# create a new Keras callback for logging the performance after every epoch
runlogger = run.get_keras_callback(loss="sparse_categorical_crossentropy")

# fit the model and supply the `runlogger` callback.
model.fit(x_train, y_train, epochs=3, batch_size=64, callbacks=[runlogger])

# export current log to logdir
run.export(logdir="logs")

In the example using the Tensorflow Keras API we first wrap the tuple of the numpy arrays of the instances and corresponding labels in a Fold object and provide a unique identifier which references the additional metadata specified in the dataset_config (see Dataset Configuration).

We then create a Run object with a unique identifier for the experiment that is run, the list of folds that should be tracked as well as the fold identifier trainfoldId of the fold that’s used during training.

After that we call the get_keras_callback function with the loss we use in model.compile. After that we can simply pass the callback to the fit function which will automatically evaluate the model performance on the specified folds at every epoch.

In the end we can export the logs to a logdir.