python - Tensorflow:没有为任何变量提供梯度

标签 python machine-learning tensorflow

我是 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/

相关文章:

machine-learning - 客户端对tensorflow服务的请求给出错误 "Attempting to use uninitialized value fully_connected/biases"

python - 对 Tensorflow 中的输入应用归一化

python - Tensorflow CNN 'tuple' 对象没有属性 'initializer'

javascript - 在 Python Flask 中传递 Javascript 数组

python - 查找所有希腊文本中带连字符的单词

python - 将公式应用于 pandas 行/回归线

python - Tensorflow js : Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), 但得到形状为 [100,120,3] 的数组

python-3.x - 如何解决在 JupiterLab 中运行 MTCNN 的问题

tensorflow - 如何理解tf.control_dependency?

python - numpy vs Matlab 速度 - arctan 和 power