gpt4 book ai didi

python - tensorflow 损失中的logits可以是占位符

转载 作者:行者123 更新时间:2023-11-30 09:44:58 26 4
gpt4 key购买 nike

我使用tensorflow来实现手写数字识别。我希望softmax_cross_entropy_with_logits中的logits先用占位符表示,然后在计算时通过计算值传递给占位符,但是tensorflow会报错ValueError: Nogradationsprovidedforanyvariable,checkYourgraph foropsthatnotsupport梯度。我知道直接将 logits 更改为输出是可以的,但是如果我必须使用 logits,结果首先是占位符。我该如何解决?

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/home/as/downloads/resnet-152_mnist-master/mnist_dataset", one_hot=True)

from tensorflow.contrib.layers import fully_connected

x = tf.placeholder(dtype=tf.float32,shape=[None,784])
y = tf.placeholder(dtype=tf.float32,shape=[None,10])

hidden1 = fully_connected(x,100,activation_fn=tf.nn.elu,
weights_initializer=tf.random_normal_initializer())

hidden2 = fully_connected(hidden1,200,activation_fn=tf.nn.elu,
weights_initializer=tf.random_normal_initializer())
hidden3 = fully_connected(hidden2,200,activation_fn=tf.nn.elu,
weights_initializer=tf.random_normal_initializer())


outputs = fully_connected(hidden3,10,activation_fn=None,
weights_initializer=tf.random_normal_initializer())




a = tf.placeholder(tf.float32,[None,10])


loss = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=a)
reduce_mean_loss = tf.reduce_mean(loss)

equal_result = tf.equal(tf.argmax(outputs,1),tf.argmax(y,1))
cast_result = tf.cast(equal_result,dtype=tf.float32)
accuracy = tf.reduce_mean(cast_result)

train_op = tf.train.AdamOptimizer(0.001).minimize(reduce_mean_loss)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(30000):
xs,ys = mnist.train.next_batch(128)
result = outputs.eval(feed_dict={x:xs})
sess.run(train_op,feed_dict={a:result,y:ys})
print(i)

最佳答案

简单来说,损失中的 logits 不能是占位符,而必须是 tensorflow Operation 。否则,您的优化器无法计算任何变量的梯度(请参阅错误消息)。

操作是“对张量执行计算的图形节点”,而 placeholder是在评估图时需要输入的张量。我真的不明白,为什么你不直接将输出操作分配给 logits,如下所示:

loss = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=outputs)

如果您提供特殊用例,我可以尝试进一步帮助您?

关于python - tensorflow 损失中的logits可以是占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com