在EVAL模型期间将预测和标签绘制到Tensorboard中

时间:2018-08-08 21:42:23

标签: python matplotlib tensorflow tensorboard

我正在尝试使用tf-matplotlib在训练期间将我的一些数据绘制到Tensorboard中。我目前正在使用时间序列数据,因此很高兴看到在评估期间输入一些数据后我的模型将输出什么。我使用的是TF 1.9,所以我的代码看起来像这样:

estimator = tf.estimator.Estimator(
            model_fn=nilm_model,
            model_dir=self.model_dir,
            params=params,
            config=run_config,
        )

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn,
                                                hooks=[train_input_hook],
                                                max_steps=100000)

eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn,
                                          name='validation_set',
                                          hooks=[eval_hook],
                                          )

tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

这些钩子仅用于使用tf.placeholders从大型numpy数组中获取数据

我希望能够做这样的事情

@tfmpl.figure_tensor
def draw_plots(features, labels, predictions, nplots):
    print(features, labels, predictions)
    figs = tfmpl.create_figures(nplots, figsize=(4,4))
    for idx, f in enumerate(figs):
        ax = f.add_subplot(111)
        ax.plot(features[idx], label='features')
        ax.plot(labels[idx], label='label')
        ax.plot(predictions[idx], label='predictions')
        f.tight_layout()
    return figs

并在评估过程中调用此函数以将我的数据绘制到Tensorboard中

nplots = 3
image_tensor = draw_plots(features=features, labels=labels, predictions=predicted, nplots=nplots)
tf.summary.image('plots', image_tensor)
eval_summary_hook = tf.train.SummarySaverHook(output_dir="{}/images".format(params['model_dir']),
                                                      save_steps=1,
                                                      summary_op=tf.summary.merge_all())
return tf.estimator.EstimatorSpec(
            mode, loss=loss, eval_metric_ops=metrics, evaluation_hooks=[eval_summary_hook]
        )

但是,由于会话此时已关闭,因此我似乎无法从自定义函数中获取功能/目标/预测值,这会导致以下错误:

RuntimeError: Graph is finalized and cannot be modified.

如何实现钩子,以便将张量值传递给函数?

0 个答案:

没有答案
相关问题