Tensorflow Error:无需优化的变量

时间:2017-03-16 06:58:27

标签: python machine-learning tensorflow

我正在尝试在Tensorflow中实现神经网络。我正在使用tf.train.GradientDescentOptimizer来最小化熵。但它显示错误ValueError: No variables to optimize

以下是代码

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

1 个答案:

答案 0 :(得分:12)

你没有&#39;在图中有任何变量要优化。

.box {
  text-align: center;
  position: relative;
  line-height: 100px;
  background: #fff;
  height: 100px;
  width: 300px;
}

.box:after {
  background: linear-gradient(to right, #bcbcbc 25%, #ffcd02 25%, #ffcd02 50%, #e84f47 50%, #e84f47 75%, #65c1ac 75%);
  position: absolute;
  content: '';
  height: 4px;
  right: 0;
  left: 0;
  top: 0;
}

应改为

<table>
  <td class="box"></td>
</table>