如何使用SessionRunHook使用tf.data.Dataset API打印张量?

时间:2018-01-24 22:18:02

标签: python tensorflow tensor tensorflow-datasets

我正在使用tf.data.Dataset API并将名称分配给闭包中的操作,该操作传递给Dataset.map,如下所示

import tensorflow as tf


def model_fn(features, mode):
    loss = tf.constant(1)
    train_op = tf.no_op()
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)


def input_fn():
    dataset = tf.data.Dataset \
        .from_generator(lambda: (x*x for x in range(10)), tf.int32) \
        .map(lambda x: tf.identity(x, name='tokens_inside'))

    ret = dataset.make_one_shot_iterator().get_next()
    tf.identity(ret, 'tokens_outside')

    return ret


tf.logging.set_verbosity(tf.logging.INFO)

hooks = [
    tf.train.LoggingTensorHook(['tokens_outside'], every_n_iter=1),
    tf.train.LoggingTensorHook(['tokens_inside'], every_n_iter=1),
]

est = tf.estimator.Estimator(model_fn=model_fn, model_dir='mout')
est.train(input_fn=input_fn, hooks=hooks, max_steps=1)

当使用tf.train.LoggingTensorHook转储某些值时,第二个钩子抛出异常:

我收到这样的错误:

KeyError: "The name 'tokens_inside:0' refers to a Tensor which does not exist. The operation, 'tokens_inside', does not exist in the graph."

我想Dataset操作为每个函数创建一个新图形?有没有办法自定义tf.train.LoggingTensorHook,以便它知道搜索指定张量的图形?

0 个答案:

没有答案