sách gpt4 ăn đã đi

python - 如何防止 TensorFlow eval 梯度

In lại Tác giả: Walker 123 更新时间:2023-11-30 09:11:13 31 4
mua khóa gpt4 giày nike

假设我有一个针对 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})

训练完成后,我想将测试数据转换为输出

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

但是,据我了解,这样做会导致 TensorFlow 计算 W 和 b 的梯度。在更复杂的情况下,这是很大的开销。那么,如何避免这种梯度计算呢?

câu trả lời hay nhất

Tensor.eval 只是单个张量的 Session.run 的简写,在这里不会提高性能。

hiện hữu document 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.

而变量的梯度显然不是必要片段。因此,我认为除非您使用优化器对其进行训练,否则它不会运行。

如有错误,请指正。

关于python - 如何防止 TensorFlow eval 梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150084/

31 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress