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 ...