首页 > 解决方案 > 将 Beholder 插件与 tf.estimator.Estimator 一起使用

问题描述

这是Beholder 插件,它允许可视化所有可训练的变量(对大规模深度网络有合理的限制)。

我的问题是我正在使用该tf.estimator.Estimator课程进行培训,并且 Beholder 插件似乎不能很好地与EstimatorAPI 配合使用。

我的代码如下所示:

# tf.data input pipeline setup
def dataset_input_fn(train=True):
  filenames = ... # training files
  if not train:
    filenames = ... # test files

  dataset = tf.data.TFRecordDataset(filenames), "GZIP")

  # ... and so on until ...
  iterator = batched_dataset.make_one_shot_iterator()
  return iterator.get_next()
  
def train_input_fn():
  return dataset_input_fn(train=True)

def test_input_fn():
  return dataset_input_fn(train=False)

# model function
def cnn(features, labels, mode, params):
  # build model

  # Provide an estimator spec for `ModeKeys.PREDICT`.
  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(
      mode=mode,
      predictions={"sentiment": y_pred_cls})

  eval_metric_ops = {
    "accuracy": accuracy_op,
    "precision": precision_op,
    "recall": recall_op
  }

  normal_summary_hook = tf.train.SummarySaverHook(
    100,
    summary_op=summary_op)

  return tf.estimator.EstimatorSpec(
    mode=mode,
    loss=cost_op,
    train_op=train_op,
    eval_metric_ops=eval_metric_ops,
    training_hooks=[normal_summary_hook]
  )

classifier = tf.estimator.Estimator(model_fn=cnn,
                                    params=...,
                                    model_dir=...) 

classifier.train(input_fn=train_input_fn, steps=1000)
ev = classifier.evaluate(input_fn=test_input_fn, steps=1000)

tf.logging.info("Loss: {}".format(ev["loss"]))
tf.logging.info("Precision: {}".format(ev["precision"]))
tf.logging.info("Recall: {}".format(ev["recall"]))
tf.logging.info("Accuracy: {}".format(ev["accuracy"]))
  

我不知道在这个设置中在哪里添加 beholder 钩子。如果我将它cnn作为训练钩子添加到函数中:

return tf.estimator.EstimatorSpec(
  mode=mode,
  loss=dnn.cost,
  train_op=dnn.train_op,
  eval_metric_ops=eval_metric_ops,
  training_hooks=[normal_summary_hook, beholder_hook]
)

然后我得到一个InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype uint8 and shape [?,?,?].

如果我尝试使用 atf.train.MonitoredTrainingSession进行设置,classifier那么培训将正常进行,但没有任何内容记录到 beholder 插件中。查看标准输出,我看到一个接一个地创建了两个会话,因此当您创建tf.estimator.Estimator分类器时,它会在终止任何现有会话后启动自己的会话。

有没有人有任何想法?

标签: tensorflowpluginstensorboard

解决方案


编辑帖子:

这是旧 tensorflow 版本的问题。幸运的是,这个问题在 tensorflow 1.9 版中得到了修复!下面的代码使用 Beholder 和 tf.estimator.Estimator。它产生了与您在旧版本中提到的相同的错误,但在 1.9 版中一切正常!

from capser_7_model_fn import *
from tensorflow.python import debug as tf_debug
from tensorflow.python.training import basic_session_run_hooks
from tensorboard.plugins.beholder import Beholder
from tensorboard.plugins.beholder import BeholderHook
import logging

# create estimator for model (the model is described in capser_7_model_fn)
capser = tf.estimator.Estimator(model_fn=model_fn, params={'model_batch_size': batch_size}, model_dir=LOGDIR)

# train model
logging.getLogger().setLevel(logging.INFO)  # to show info about training progress in the terminal
beholder = Beholder(LOGDIR)
beholder_hook = BeholderHook(LOGDIR)
capser.train(input_fn=train_input_fn, steps=n_steps, hooks=[beholder_hook])

另一方面是我需要为摘要编写器、张量板命令行调用和 BeholderHook 指定完全相同的 LOGDIR。之前,为了比较我的模型的不同运行,我在 LOGDIR/run_1、LOGDIR/run_2 等中编写了不同运行的摘要,即:

capser = tf.estimator.Estimator(model_fn=model_fn, params={'model_batch_size': batch_size}, model_dir=LOGDIR/run_n)

我用

tensorboard -logdir=LOGDIR

启动张量板,我用

beholder_hook = BeholderHook(LOGDIR)

写入情人数据。在那种情况下,beholder 没有找到它需要的数据。我需要做的是为所有内容指定完全相同的 LOGDIR。即,在代码中:

capser = tf.estimator.Estimator(model_fn=model_fn, params={'model_batch_size': batch_size}, model_dir=LOGDIR+'/run_n')
beholder_hook = BeholderHook(LOGDIR+'/run_n')

并在终端中启动 tensorboard:

tensorboard -logdir=LOGDIR+'/run_n'

希望有帮助。


推荐阅读