Search code examples
tensorboard

Graph tab showing past models


After running several models in a row and when I check on the dashboard, I can see the previous models trained in the Graph tab.

I do save in different folders of each model.

Here my code (tensorflow2 on conda 3.7 environment) :

import tensorflow as tf

def create_model(num_model):
    '''
    Choose between two neural networks models
    :param num_model: Model number : 1 for model one and anything else for the second model
    :return: model architecture
    '''
    model = None
    if num_model == 1:
        model = tf.keras.models.Sequential([
            tf.keras.layers.Flatten(input_shape=(28, 28)),
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dropout(0.2),
            tf.keras.layers.Dense(10, activation='softmax')
        ])
    else:
        model = tf.keras.models.Sequential([
            tf.keras.layers.Flatten(input_shape=(28, 28)),
            tf.keras.layers.Dense(256, activation='relu'),
            tf.keras.layers.Dropout(0.5),
            tf.keras.layers.Dense(10, activation='softmax')
        ])
    return model

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

"""
Create and train the first model
"""
model1 = create_model(1)
model1.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="mnist\\first")
model1.fit(x_train, y_train, epochs=5, callbacks=[tensorboard_callback])
model1.evaluate(x_test, y_test, verbose=2)

"""
Create and train the second model
"""
model2 = create_model(2)
tensorboard_callback2 = tf.keras.callbacks.TensorBoard(log_dir="mnist\\second")
model2.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
model2.fit(x_train, y_train, epochs=5, callbacks=[tensorboard_callback2])
model2.evaluate(x_test, y_test, verbose=2)

After training on terminal :

tensorboard --logdir mnist/first

Graph Tab of the first model No problem, in the Graph tab I see the correct model.


But when I check for the second model:

tensorboard --logdir mnist/second

Graph tab of the second model that includes the first one Now in the graph model I can see both models.

How to have only the second model in the tensorboard graph for the second model ?


Solution

  • I guess tensorboard shows the keras default graph which contains all operation nodes created sofar. To have only the second graph in the second tensorboard, try reseting the keras default session/graph with - tf.keras.backend.clear_session().

    If it does not have the required effect, try - tf.compat.v1.reset_default_graph().

    You might have to also call mnist.load_data() a second time.