我正在使用tf.TPUEstimator()
实现基于BERT的NLP模型。我想实现逐层训练,我只需要为每个时期选择模型的一层即可训练。为此,我想更改我的model_fn并获取current_epoch的值。
我知道如何使用tf.train.get_or_create_global_step()
BUT中的model_fn
计算current_epoch作为张量的值,我需要评估该张量的值以选择要训练的层并实现返回正确的层train_op
至tf.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时培训会挂起?
答案 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]