我是 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})
如果您能帮助我解决这个问题,我将不胜感激。
最佳答案
我总是使用 tf.nn.softmax_cross_entropy_with_logits() 这样我就可以将 logits 作为第一个参数,将标签作为第二个参数。你能试试这个吗?
关于python - Tensorflow:没有为任何变量提供梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778760/