Is there a way to save an intermediate output in Tensorflow to a file?

≯℡__Kan透↙ 提交于 2019-12-10 23:14:36

问题


I have a conv net that does a bunch of things.

The inference code looks something like this:

conv0_feature_count = 2
with tf.variable_scope('conv0') as scope:
    kernel = _variable_with_weight_decay('weights', shape=[5, 5, 1, conv0_feature_count], stddev=5e-2, wd=0.0)
    conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
    biases = _variable_on_cpu('biases', [conv0_feature_count], tf.constant_initializer(0.0))
    bias = tf.nn.bias_add(conv, biases)
    conv0 = tf.nn.relu(bias, name=scope.name)

And so on and so forth followed by max pooling, etc.

I would like to be able to save conv0 to a file for inspection. I know that tf.Print would print the values in the console, but it is a big tensor. Printing the value makes no sense.


回答1:


TensorFlow doesn't have any public operators that make it possible to write a tensor to a file, but if you look at the implementation of tf.train.Saver (in particular the BaseSaverBuilder.save_op() method), you will see that it includes an implementation of an op that can write one or more tensors to a file, and it is used to write checkpoints.

CAVEAT: The following solution relies on an internal implementation and is subject to change, but works with TensorFlow r0.10rc0.

The following snippet of code writes the tensor t to a file called "/tmp/t.ckpt":

import tensorflow as tf
from tensorflow.python.ops import io_ops

t = tf.matmul(tf.constant([[6, 6]]), tf.constant([[2], [-1]]))

save_op = io_ops._save(filename="/tmp/t.ckpt", tensor_names=["t"],
                       tensors=[t])

sess = tf.Session()
sess.run(save_op)  # Writes `t` to "/tmp/t.ckpt".

To read the value you can use tf.train.NewCheckpointReader() as follows:

reader = tf.train.NewCheckpointReader("/tmp/t.ckpt")
print reader.get_tensor("t")  # ==> "array([[6]], dtype=int32)"


来源:https://stackoverflow.com/questions/39025340/is-there-a-way-to-save-an-intermediate-output-in-tensorflow-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!