4
我正在嘗試在Tensorflow中實現神經網絡。我正在使用tf.train.GradientDescentOptimizer
來最小化熵。但是它顯示了我的錯誤ValueError: No variables to optimize
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,748])
w = tf.zeros([748,10])
b = tf.zeros([10])
y = tf.matmul(x,w) + b
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSessoin()
tf.global_variables_initializer().run()
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})
我得到的錯誤是這樣的
Traceback (most recent call last):
File "NeuralNetwork.py", line 15, in <module>
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/training/optimizer.py", line 193, in minimize
grad_loss=grad_loss)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/training/optimizer.py", line 244, in compute_gradients
raise ValueError("No variables to optimize")
ValueError: No variables to optimize
Himaprasoon是正確的!除此之外:零重量不會優化!將它們改爲隨機分佈... – rmeertens
@rmeertens,那是不正確的。亞歷山大, –
。它只是意味着變量被初始化爲零。它仍然會優化(值會改變)。 – Himaprasoon