图表选项卡显示过去的模型

Graph tab showing past models

在连续 运行 个模型之后,当我在仪表板上查看时,我可以在图表选项卡中看到之前训练的模型。

我确实保存在每个模型的不同文件夹中。

这里是我的代码(conda 3.7 环境下的 tensorflow2):

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)

终端训练后:

tensorboard --logdir mnist/first

Graph Tab of the first model 没问题,在图表选项卡中我看到了正确的模型。


但是当我检查第二个模型时:

tensorboard --logdir mnist/second

Graph tab of the second model that includes the first one 现在在图形模型中我可以看到两个模型。

如何让第二个模型的张量板图中只有第二个模型?

我猜 tensorboard 显示了 keras 默认图表,其中包含到目前为止创建的所有操作节点。要在第二张量板上只有第二张图,请尝试使用 - tf.keras.backend.clear_session().

重置 keras 默认值 session/graph

如果没有达到要求的效果,试试- tf.compat.v1.reset_default_graph().

您可能还需要再次调用 mnist.load_data()