有没有办法将Tensorflow中的中间输出保存到文件中? [英] Is there a way to save an intermediate output in Tensorflow to a file?

查看:901
本文介绍了有没有办法将Tensorflow中的中间输出保存到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可做很多事情的转换网。

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.

我希望能够将conv0保存到文件中以进行检查。我知道tf.Print会在控制台中打印值,但这是一个很大的张量。打印值没有任何意义。

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.

推荐答案

TensorFlow没有任何公共运算符,可以将张量写入到文件,但如果您查看 tf.train.Saver (尤其是 BaseSaverBuilder.save_op() 方法),您将看到它包含一个可以实现将一个或多个张量写入文件,并用于写入检查点。

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:以下解决方案取决于内部实现和可能会有所更改,但可与TensorFlow r0.10rc0一起使用。

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

以下代码段将张量写为 t 到名为 / tmp / t.ck的文件pt

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".

要读取值,可以使用 tf.train.NewCheckpointReader()如下:

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

这篇关于有没有办法将Tensorflow中的中间输出保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆