我是tensorflow的初学者,我从网上尝试了一些玩具示例:
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])
但是当我运行它时,出现以下错误:
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).
如果您有什么想法要帮助我...
答案 0 :(得分:1)
谢谢,我关注了您的评论和这个stackoverflow(Tensorflow - No gradients provided for any variable),现在它可以工作了:
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)
答案 1 :(得分:0)
您的logits
应该是张量
将呼叫转移到eval()
。
此调用进行前向传递,并返回一个数组,该数组的值具有网络的随机初始化的权重。此时,您不在张量流领域,并且不再可以进行反向传播。