我有一个可以做很多事情的转换网络。
推理代码看起来像这样:
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)
依此类推,然后是最大池化,等等。
我希望能够将 conv0 保存到文件中以供检查。我知道 tf.Print 会打印控制台中的值,但它是一个大张量。打印该值没有任何意义。
TensorFlow 没有任何公共运算符可以将张量写入文件,但是如果您查看以下实现tf.train.Saver https://www.tensorflow.org/versions/r0.10/api_docs/python/state_ops.html#Saver(特别是BaseSaverBuilder.save_op() https://github.com/tensorflow/tensorflow/blob/952a447010cdb4ed507ff29c28f999ba43e959f3/tensorflow/python/training/saver.py#L233方法),您将看到它包含一个可以将一个或多个张量写入文件的操作的实现,并且它用于写入检查点。
CAVEAT:以下解决方案依赖于内部实现,并且可能会发生变化,但适用于 TensorFlow r0.10rc0。
以下代码片段写入张量t
到一个名为"/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()
如下:
reader = tf.train.NewCheckpointReader("/tmp/t.ckpt")
print reader.get_tensor("t") # ==> "array([[6]], dtype=int32)"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)