Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow Error : No Variables to optimize

I am trying my hands on implementing a Neural Network in Tensorflow. I am using tf.train.GradientDescentOptimizer to minimize the entropy. However it shows me the error ValueError: No variables to optimize

Below is the code

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})

I am getting the error something like this

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
like image 658
Kavan Avatar asked Mar 16 '17 06:03

Kavan


1 Answers

You dont' have any variables in the graph to be optimized.

w = tf.zeros([748,10])
b = tf.zeros([10])

should be changed to

w = tf.Variable(tf.zeros([748,10]))
b = tf.Variable(tf.zeros([10]))
like image 98
Himaprasoon Avatar answered Nov 06 '22 17:11

Himaprasoon