将 Beholder 插件与 tf.estimator.Estimator 一起使用
Using Beholder plugin with tf.estimator.Estimator
这是 Beholder Plugin,它允许所有可训练变量的可视化(对大规模深度网络有合理的限制)。
我的问题 是我 运行 我使用 tf.estimator.Estimator
class 训练,Beholder 插件似乎无法播放与 Estimator
API.
相得益彰
我的代码如下所示:
# 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"]))
我不知道在这个设置中在哪里添加旁观者挂钩。
如果我将它作为训练挂钩添加到 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 [?,?,?]
。
如果我尝试使用 tf.train.MonitoredTrainingSession
来设置 classifier
,那么训练会照常进行,但没有任何内容会记录到 beholder 插件中。查看 stdout,我看到两个会话一个接一个地创建,因此当您创建 tf.estimator.Estimator
classifier 时,它会在终止任何现有会话后启动自己的会话。
有没有人有什么想法?
已编辑 post:
这是旧版 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])
另一个方面是我需要为摘要编写器、tensorboard 命令行调用和 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
启动tensorboard,我用
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'
希望对您有所帮助。
这是 Beholder Plugin,它允许所有可训练变量的可视化(对大规模深度网络有合理的限制)。
我的问题 是我 运行 我使用 tf.estimator.Estimator
class 训练,Beholder 插件似乎无法播放与 Estimator
API.
我的代码如下所示:
# 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"]))
我不知道在这个设置中在哪里添加旁观者挂钩。
如果我将它作为训练挂钩添加到 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 [?,?,?]
。
如果我尝试使用 tf.train.MonitoredTrainingSession
来设置 classifier
,那么训练会照常进行,但没有任何内容会记录到 beholder 插件中。查看 stdout,我看到两个会话一个接一个地创建,因此当您创建 tf.estimator.Estimator
classifier 时,它会在终止任何现有会话后启动自己的会话。
有没有人有什么想法?
已编辑 post:
这是旧版 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])
另一个方面是我需要为摘要编写器、tensorboard 命令行调用和 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
启动tensorboard,我用
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'
希望对您有所帮助。