如何在Tensorflow“custom estimator”中打印张量以进行调试?

如何在Tensorflow“custom estimator”中打印张量以进行调试?,tensorflow,Tensorflow,在低级api中,我们可以使用 print(session.run(xx_tensor_after_xx_operation, feed_dict=feed_dict)) 以获取用于调试的真实数据。但在自定义估计器中,如何调试这些张量 下面是我的一个生动示例片段: import tensorflow as tf FLAGS = tf.app.flags.FLAGS def yichu_dssm_model_fn( features, # This is batch_fea

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

print(session.run(xx_tensor_after_xx_operation, feed_dict=feed_dict))
以获取用于调试的真实数据。但在自定义估计器中,如何调试这些张量

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

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?
    """
你可以用。它向图形添加操作,在执行时将张量内容打印为标准错误

content-conv-tensor=tf.Print(content-conv-tensor,[content-conv-tensor],'content-conv-tensor:')
您可以使用。它向图形添加操作,在执行时将张量内容打印为标准错误

content-conv-tensor=tf.Print(content-conv-tensor,[content-conv-tensor],'content-conv-tensor:')

sess=tf.InteractiveSession()
测试=sess.run(功能)
打印('功能:')
打印(测试)


虽然这会导致错误,但它仍然会打印出张量值。打印后立即发生错误,因此您只能将其用于检查张量值。

sess=tf.InteractiveSession()
测试=sess.run(功能)
打印('功能:')
打印(测试)


虽然这会导致错误,但它仍然会打印出张量值。打印后立即发生错误,因此您只能使用它检查张量值。

tf.print不推荐使用,请使用tf.print,但它不容易使用

最好的选择是日志挂钩

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])

不推荐使用tf.Print,请使用tf.Print,但它不容易使用

最好的选择是日志挂钩

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])