如何从自定义tf.estimator的模型函数内部评估张量的值

时间:2019-05-26 03:47:44

标签: tensorflow

我正在使用tf.TPUEstimator()实现基于BERT的NLP模型。我想实现逐层训练,我只需要为每个时期选择模型的一层即可训练。为此,我想更改我的model_fn并获取current_epoch的值。

我知道如何使用tf.train.get_or_create_global_step() BUT中的model_fn计算current_epoch作为张量的值,我需要评估该张量的值以选择要训练的层并实现返回正确的层train_optf.estimator train_op属于与根据current_epoch的值选择的单个图层有关的)。

我无法从model_fn内部评估此张量(current_epoch / global_step)。我尝试了以下操作,但培训停在了步骤my_sess.run(my_global_step.initializer

global_step = tf.train.get_or_create_global_step()
graph = tf.get_default_graph()
my_sess = tf.Session(graph=graph)
current_epoch = (global_step * full_bs) // train_size

my_sess.run(my_global_step.initializer)
current_epoch = sess.run(current_epoch)

# My program hangs at the initialising step: my_sess.run(my_global_step.initializer)

有什么方法可以使用tf.Estimators默认会话来评估张量吗?如何获得默认的会话/图表?

最重要的是,我的代码有什么问题,为什么使用tpu和TPUEstimator时培训会挂起

1 个答案:

答案 0 :(得分:0)

这不是OP的第二个问题的直接答案,而是标题的答案。

我设法用get_variable_value打印变量值,但不确定这是否是最佳方法。

使用

estimator = tf.contrib.tpu.TPUEstimator(
# ...
)
out = estimator.get_variable_value('output_bias')
print(type(out))
print(out)

我知道了

<class 'numpy.ndarray'>
[-0.00107745  0.00107744]