1

I am a beginner in tensorflow and I tried some toy example from the net:

import io
import tensorflow as tf
import math

def _runModel(data):
        hidden1 = tf.nn.relu(tf.matmul(data, weights) + biases) 
        print(weights.eval())
        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)  
        print(weights.eval())
        logits = tf.matmul(hidden2, weights) + biases   
        print(weights.eval())
        return logits

sess = tf.InteractiveSession()
weights = tf.Variable( tf.truncated_normal([5,5], stddev=1.0 / math.sqrt(float(5))), name='weights')
biases = tf.Variable(tf.zeros([5]), name='biases')
data_placeholder = tf.placeholder(tf.float32, shape= [1,5]) 
labels_placeholder = tf.placeholder(tf.int32, shape=[1])
sess.run(tf.global_variables_initializer())
data_placeholder = [[0.0, 1.0, 2.0, 3.0, 4.0]]
labels_placeholder = [1]

inference=_runModel(data_placeholder)
logits = inference.eval()
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.to_int64(labels_placeholder), logits=logits, name='xentropy')
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)

_, c = sess.run([optimizer, loss])

But when I run it, I have the following error :

Traceback (most recent call last):   File "test_tensorflow.py", line 43, in <module>
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)   File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 421, in minimize
    ([str(v) for _, v in grads_and_vars], loss)) ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'weights:0' shape=(5, 5) dtype=float32_ref>", "<tf.Variable 'biases:0' shape=(5,) dtype=float32_ref>"] and loss Tensor("xentropy_mean:0", shape=(), dtype=float32).

If you have any idea to help me ...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
R.SY
  • 23
  • 5

2 Answers2

1

Your logits should be a tensor

Remove the call to eval().

This call does a forward pass and returns an array with values with the randomly initialized weights of your network. At this point you are out of the tensorflow realm and backpropagation is no longer possible.

f4.
  • 3,814
  • 1
  • 23
  • 30
1

Thanks, I followed your comment and this stackoverflow ( Tensorflow - No gradients provided for any variable ) and now it works :

import io
import tensorflow as tf
import math


def _runModel(data):
        hidden1 = tf.nn.relu(tf.matmul(data, weights) + biases)
        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
        logits = tf.matmul(hidden2, weights) + biases
        return logits

weights = tf.Variable( tf.truncated_normal([5,5], stddev=1.0 / math.sqrt(float(5))), name='weights')

biases = tf.Variable(tf.zeros([5]), name='biases')

data_placeholder = tf.placeholder(tf.float32, shape= [1,5])

labels_placeholder = tf.placeholder(tf.int32, shape=[1])

logits = _runModel(data_placeholder)

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.to_int64(labels_placeholder), logits=logits, name='xentropy')

loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)



sess = tf.InteractiveSession()

with sess.as_default():

    sess.run(tf.global_variables_initializer())

    feed_dict = {
        data_placeholder: [[0.0, 1.0, 2.0, 3.0, 4.0]],
        labels_placeholder: [1],
    }

    _, c = sess.run([optimizer, loss], feed_dict=feed_dict)

    print(c)
R.SY
  • 23
  • 5