【发布时间】:2017-03-05 19:25:50
【问题描述】:
我是 tensorflow 的新手,我正在构建一个网络,但未能为其计算/应用渐变。我得到了错误:
ValueError: No gradients provided for any variable: ((None, tensorflow.python.ops.variables.Variable object at 0x1025436d0), ... (None, tensorflow.python.ops.variables.Variable object at 0x10800b590))
我尝试使用tensorboard graph 来查看是否有什么东西导致无法追踪图形并获得渐变,但我什么也看不到。
这是部分代码:
sess = tf.Session()
X = tf.placeholder(type, [batch_size,feature_size])
W = tf.Variable(tf.random_normal([feature_size, elements_size * dictionary_size]), name="W")
target_probabilties = tf.placeholder(type, [batch_size * elements_size, dictionary_size])
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_hidden_size)
stacked_lstm = tf.nn.rnn_cell.MultiRNNCell([lstm] * number_of_layers)
initial_state = state = stacked_lstm.zero_state(batch_size, type)
output, state = stacked_lstm(X, state)
pred = tf.matmul(output,W)
pred = tf.reshape(pred, (batch_size * elements_size, dictionary_size))
# instead of calculating this, I will calculate the difference between the target_W and the current W
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(target_probabilties, pred)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess.run(optimizer, feed_dict={X:my_input, target_probabilties:target_prob})
我将不胜感激任何帮助解决这个问题。
【问题讨论】:
-
您的代码中哪里定义了 NanoporeTensor?
-
对不起,我在这里写代码的时候忘了把它拿出来。它实际上不在此代码中,但在原始代码中,这无关紧要。我已经编辑过了。
-
这是真正的代码吗?你确实有那行
sess.run(optimizer, feed_dict={X:my_input, target_probabilties:target_prob})在一个循环中,你实际上将一些东西输入到 my_input- 和 target_prob-placeholders 中,对吧? -
这不是真正的代码。我确实有 sess.run 在我提供所有输入的循环中。我简化了代码,因为我认为这种方式可以更好地理解问题,我没有看到其余部分的重要性。如果确实需要,我可以上传代码。
-
不,一切都很好。只是错过了输入循环。无论如何,我总是使用 tf.nn.softmax_cross_entropy_with_logits() 以便我将 logits 作为第一个参数,将标签作为第二个参数。你可以试试这个吗?
标签: python machine-learning tensorflow