简体   繁体   English

如何防止 TensorFlow eval 梯度

[英]How to prevent TensorFlow eval gradients

Suppose that I have simple TensorFlow model for MNIST data like this假设我有像这样的 MNIST 数据的简单 TensorFlow 模型

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784]) 
y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

When training is done, I want to transform test data into output训练完成后,我想将测试数据转换为输出

y_transformed = sess.run(y, feed_dict={x: mnist.test.images})

But, as I understood, doing this cause TensorFlow to calculate gradients for W and b.但是,据我所知,这样做会导致 TensorFlow 计算 W 和 b 的梯度。 And this is a lot of overhead in more complex cases.在更复杂的情况下,这是很多开销。 So, how can I avoid this gradient calculations?那么,我怎样才能避免这种梯度计算呢?

Tensor.eval is just a shorthand for Session.run for single tensor, which will not improve performance here. Tensor.eval只是Session.run对于单个张量的简写,这里不会提高性能。

In the document of Session.run , it says:Session.run文档中,它说:

This method runs one "step" of TensorFlow computation, by running the necessary graph fragment to execute every Operation and evaluate every Tensor in fetches, substituting the values in feed_dict for the corresponding input values.此方法运行 TensorFlow 计算的一个“步骤”,通过运行必要的图形片段来执行每个操作并评估每个 Tensor 中的提取,将 feed_dict 中的值替换为相应的输入值。

And the gradients of variables y is obviously not a necessary fragment.而变量y的梯度显然不是一个必要的片段。 Thus I don't think it will be run unless you do something like train it with optimizers.因此,我认为它不会运行,除非您使用优化器对其进行训练。

Please correct me if I am wrong.如果我错了,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM