首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Tensorflow中打印张量`自定义估计器`进行调试?

如何在Tensorflow中打印张量`自定义估计器`进行调试?
EN

Stack Overflow用户
提问于 2018-03-16 17:09:49
回答 3查看 3.6K关注 0票数 1

在低级api中,我们可以使用

代码语言:javascript
复制
print(session.run(xx_tensor_after_xx_operation, feed_dict=feed_dict))

获取用于调试的真实数据。但是在自定义估计器中,如何调试这些张量?

下面是我的一个生动示例的代码片段:

代码语言:javascript
复制
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS


def yichu_dssm_model_fn(
        features,  # This is batch_features from input_fn
        labels,  # This is batch_labels from input_fn
        mode,  # An instance of tf.estimator.ModeKeys
        params):
    # word_id sequence in content
    content_input = tf.feature_column.input_layer(features, params['feature_columns'])
    content_embedding_matrix = tf.get_variable(name='content_embedding_matrix',
                                               shape=[FLAGS.max_vocab_size, FLAGS.word_vec_dim])
    content_embedding = tf.nn.embedding_lookup(content_embedding_matrix, content_input)
    content_embedding = tf.reshape(content_embedding, shape=[-1, FLAGS.max_text_len, FLAGS.word_vec_dim, 1])
    content_conv = tf.layers.Conv2D(filters=100, kernel_size=[3, FLAGS.word_vec_dim])

    content_conv_tensor = content_conv(content_embedding)
    """
      in low-level-api, we can use `print(session.run(content_conv_tensor))` to get the real data to debug.
      But in custom estimator, how to debug these tensors?
    """
EN

回答 3

Stack Overflow用户

发布于 2018-03-16 21:16:34

您可以使用tf.Print。它向图形添加操作,在执行时将张量内容打印到标准错误。

代码语言:javascript
复制
content_conv_tensor = tf.Print(content_conv_tensor, [content_conv_tensor], 'content_conv_tensor: ')
票数 3
EN

Stack Overflow用户

发布于 2018-11-23 01:09:00

tf.Print已弃用,请使用tf.print,但它不容易使用

最好的选择是日志钩子

代码语言:javascript
复制
hook =  \
    tf.train.LoggingTensorHook({"var is:": var_to_print},
                               every_n_iter=10)
return tf.estimator.EstimatorSpec(mode, loss=loss, 
                                  train_op=train_op,
                                  training_hooks=[hook])
票数 2
EN

Stack Overflow用户

发布于 2018-07-02 11:14:33

sess = tf.InteractiveSession() test = sess.run(features) print('features:') print(test)

尽管这会导致错误,但它仍然会打印出张量值。错误发生在打印之后,因此您只能使用它来检查张量值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49316843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档