Tensorboard损失图可以追溯到过去(Keras)

时间:2018-10-10 09:52:27

标签: python tensorflow keras tensorboard

我有一个与上述here类似的问题,只是我在使用带有Tensorflow后端的Keras,而不是Tensorflow。 我已经训练了18种MLP模型,用于使用不同的元参数进行时间序列预测,所有这些参数均使用相同的基本架构创建。我正在扫描的3个元参数是模型用于预测的前瞻性,模型的深度以及我是否正在使用L2正则化。

    model = Sequential()
    # input_shape should be a 3D tensor with shape (batch_size, timesteps ,input_dim)
    model.add(Flatten())
    # hidden layer sizes should drop gradually from 256 to 2*lookahead
    hidden_layer_sizes = [int(256 - i * (256 - 2 * lookahead) / depth) for i in range(depth)]
    for hidden_layer_size in hidden_layer_sizes:
        if regularization:
            model.add(Dense(hidden_layer_size, kernel_initializer="he_normal",
                        kernel_regularizer = regularizers.l2(0.01), activation=activations))
        else:
            model.add(Dense(hidden_layer_size, kernel_initializer="he_normal",
                        activation=activations))
    model.add(Dense(2 * lookahead))
    loss = losses.mean_squared_error
    model.compile(loss=loss, optimizer=self.kwargs["optimizer"], metrics=['mae'])

每个模型的张量板数据通过相关的Keras回调保存在单独的文件夹中

callback_tensorboard = TensorBoard(log_dir=log_dir,
                                   histogram_freq=5,
                                   write_graph=False,
                                   write_grads=True,
                                   write_images=False)  

但是由于某种原因,在18个模型中,有3个保存了两个张量板文件,而不是一个,并且生成的图显示了这种奇怪的现象,随着时间的推移逐步向后发展 enter image description here

为什么会这样?然后删除第二张tensorboard文件,我该怎么做才能防止这种情况?

1 个答案:

答案 0 :(得分:0)

之所以会这样,是因为每次您调用mode.fit()时,都会在keras中重新初始化内部纪元计数器。但是,您可以在编译模型时手动设置它,例如:

model.compile(optimizer=tf.train.AdamOptimizer(),
              loss=tf.losses.mean_squared_error)
init_epoch = 0

然后在ypu调用合适时,您可以将其他参数传递给keras,该参数描述了当前纪元:

epoch_count = 200
init_epoch += epoch_count
history = model.fit(x_train, y_train,
                batch_size=256,
                epochs=init_epoch,
                validation_data=(x_test, y_test),
                verbose=1,
                initial_epoch=init_epoch-epoch_count,
                )