【问题标题】:How to evaluate the value of a tensor, from inside the model function of a custom tf.estimator如何从自定义 tf.estimator 的模型函数内部评估张量的值
【发布时间】:2019-05-26 03:47:43
【问题描述】:

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

我知道如何使用 model_fn 内部的 tf.train.get_or_create_global_step() 将 current_epoch 的值计算为张量,但是我需要评估此张量的值以选择要训练和实现的层,将正确的 train_op 返回到tf.estimatortrain_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 时训练会挂起

【问题讨论】:

  • 我也有类似的问题。你找到解决办法了吗?

标签: tensorflow


【解决方案1】:

这不是对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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2013-04-12
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多