张量流中的值误差

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/temp/data", one_hot=True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

# matrix = height * width
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')


# defining the neural network

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

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

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

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

    l1 = tf.add(tf.matmul(data, hiddenLayer1['weights']), hiddenLayer1['biases'])
    l1 = tf.nn.relu(l1)

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

    l3 = tf.add(tf.matmul(l2, hiddenLayer3['weights']), hiddenLayer3['biases'])
    l3 = tf.nn.relu(l3)
    output = tf.matmul(l3, outputLayer['weights']), outputLayer['biases']
    return output


# training the network
def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(prediction,tf.squeeze(y)))
    #cost = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)
    #cost = tf.reduce_mean(cost) * 100
    optimizer = tf.train.AdamOptimizer(0.003).minimize(cost)

    # cycles feed forward + backprop
    numberOfEpochs = 10

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

         #dealing with training data
        for epoch in range(numberOfEpochs):
            epoch_loss = 0
            for _ in range(int(mnist.train.num_examples / batch_size)):
                epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c
            print('Epoch', epoch, ' completed out of ', numberOfEpochs, ' loss: ', epoch_loss)

            correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
            accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
            print('Accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))


train_neural_network(x)

我是Tensorflow的新手,我试图训练我的模型来读取数据集。 但每次运行代码时,都会出现此错误:

Traceback(最近一次调用的最后一个):train_neural_network(x)中第87行的文件“firstAI.py”,train_neural_network中的第62行文件“firstAI.py”,cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(prediction,tf .squeeze(Y))); 文件“/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py”,第1935行,在sparse_softmax_cross_entropy_with_logits标签,logits)文件“/home/phillipus/.local/ lib / python3.6 / site-packages / tensorflow / python / ops / nn_ops.py“,第1713行,在_ensure_xent_args”named arguments(labels = ...,logits = ...,...)“%name) ValueError:只调用具有命名参数的sparse_softmax_cross_entropy_with_logits (标签= ...,logits = ...,...)

看起来问题出在“ cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(prediction,tf.squeeze(y))) ”和调用“ train_neural_network(x) ”函数。 我是Tensorflow的新手,所以我的故障排除不是最好的,任何人都可以帮助我?


也许你可以在成本计算中尝试使用tf.nn.softmax_cross_entropy_with_logits而不是tf.nn.sparse_softmax_cross_entropy_with_logits。

但是,如果您想继续使用tf.nn.sparse_softmax_cross_entropy_with_logits,则此链接可能有所帮助:Tensorflow ValueError:只能使用命名参数调用`sparse_softmax_cross_entropy_with_logits`。

顺便说一下,您使用的tensorflow和python的版本是什么?

尝试运行这个:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/temp/data", one_hot=True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

# matrix = height * width
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')


# defining the neural network

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

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

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

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

    l1 = tf.add(tf.matmul(data, hiddenLayer1['weights']), hiddenLayer1['biases'])
    l1 = tf.nn.relu(l1)

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

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

    output = tf.add(tf.matmul(l3, outputLayer['weights']),outputLayer['biases'])
    return output


prediction = neural_network_model(x)
cost = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)
optimizer = tf.train.AdamOptimizer(0.003).minimize(cost)

# cycles feed forward + backprop
numberOfEpochs = 10

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

     #dealing with training data
    for epoch in range(numberOfEpochs):
    epoch_loss = 0
    for _ in range(int(mnist.train.num_examples / batch_size)):
        epoch_x, epoch_y = mnist.train.next_batch(batch_size)
        _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
        epoch_loss += c
    print('Epoch', epoch, ' completed out of ', numberOfEpochs, ' loss: ', epoch_loss)

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
    print('Accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
链接地址: http://www.djcxy.com/p/32133.html

上一篇: Value Error in tensorflow

下一篇: what does it mean having a negative cost for my training set?