在低级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?
"""发布于 2018-03-16 21:16:34
您可以使用tf.Print。它向图形添加操作,在执行时将张量内容打印到标准错误。
content_conv_tensor = tf.Print(content_conv_tensor, [content_conv_tensor], 'content_conv_tensor: ')发布于 2018-11-23 01:09:00
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])发布于 2018-07-02 11:14:33
sess = tf.InteractiveSession() test = sess.run(features) print('features:') print(test)
尽管这会导致错误,但它仍然会打印出张量值。错误发生在打印之后,因此您只能使用它来检查张量值。
https://stackoverflow.com/questions/49316843
复制相似问题