在TensorFlow中出错

时间:2017-09-17 04:32:45

标签: python machine-learning tensorflow neural-network deep-learning

我正在关注youtube上的TensorFlow教程,我遇到了一个错误:

这是我的代码:

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

# height x width
x = tf.placeholder("float")
y = tf.placeholder("float")

def neural_network_model(data):
    hidden_1_layer = {"weights":tf.Variable(tf.random_normal([784,n_nodes_hl1])),"biases":tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {"weights":tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),"biases":tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {"weights":tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),"biases":tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer   = {"weights":tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),"biases":tf.Variable(tf.random_normal([n_classes]))}

    # (input_data * weights + biases
    l1 = tf.add(tf.matmul(data,hidden_1_layer["weights"]), hidden_1_layer["biases"])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer["weights"]), hidden_2_layer["biases"])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer["weights"]), hidden_3_layer["biases"])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer["weights"]) + output_layer["biases"]

    return output

def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=prediction,logits=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    # cycles of 
    hm_epochs = 10

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epoch):
            epoch_loss = 0
            for i in range(int(mnist.train.num.examples/batch_size)):
                epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                i, c = sess.run([optimizer,cost],feed_dict = {x:epoch_x,y:epoch_y})
                epoch_loss += c
            print("Epoch", epoch, "completed out of", hm_epochs, "loss:" , epoch_loss)
        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y1))

        accuracy = tf.reduce_mean(tf,cast(correct, "float"))
        print("Accuracy", accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))



train_neural_network(x)

,这是错误:

Traceback (most recent call last):
  File "/home/markus/Documents/NN-Tutorial-04.py", line 65, in <module>
    train_neural_network(x)
  File "/home/markus/Documents/NN-Tutorial-04.py", line 43, in train_neural_network
    optimizer = tf.train.AdamOptimizer().minimize(cost)
  File "/home/markus/.local/lib/python3.5/site-packages/tensorflow/python/training/optimizer.py", line 322, 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 'Variable:0' shape=(784, 500) dtype=float32_ref>", "<tf.Variable 'Variable_1:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_2:0' shape=(500, 500) dtype=float32_ref>", "<tf.Variable 'Variable_3:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_4:0' shape=(500, 500) dtype=float32_ref>", "<tf.Variable 'Variable_5:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_6:0' shape=(500, 10) dtype=float32_ref>", "<tf.Variable 'Variable_7:0' shape=(10,) dtype=float32_ref>"] and loss Tensor("Mean:0", dtype=float32).

任何帮助将不胜感激,这是我关注的视频(https://www.youtube.com/watch?v=PwAGxqrXSCs)。

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

1 个答案:

答案 0 :(得分:1)

您在调用tf.nn.softmax_cross_entropy_with_logits()时混淆了标签和日志。这种方式tensorflow可能会尝试将渐变分配给标签(我不认为它应该是可能的,因为这些是占位符),并且图中的变量不会获得任何渐变。这是不正确的。

而不是

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
       labels=prediction,logits=y)
)    

你应该写

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
       labels=y,logits=prediction)
)

此外,还有一些语法错误,您还会在内部循环中使用返回的优化器覆盖i变量(外部循环的计数器)。现在你不使用i,但如果你愿意,那将导致很难诊断错误。只需将返回的变量重命名为_(未使用的返回值的python约定):

_, c = sess.run([optimizer,cost],feed_dict = {x:epoch_x,y:epoch_y})