0
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)

I am new to Tensorflow and I am trying to train my model to read datasets. But every time I run the code, I get this error:

Traceback (most recent call last): File "firstAI.py", line 87, in train_neural_network(x) File "firstAI.py", line 62, in train_neural_network cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(prediction,tf.squeeze(y))); File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1935, in sparse_softmax_cross_entropy_with_logits labels, logits) File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1713, in _ensure_xent_args "named arguments (labels=..., logits=..., ...)" % name) ValueError: Only call sparse_softmax_cross_entropy_with_logits with named arguments (labels=..., logits=..., ...)

Looks like the problem is at the "cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(prediction,tf.squeeze(y)))" and the calling of the "train_neural_network(x)" function. I am new to Tensorflow so my troubleshooting isn't at its best, anyone to help me?

2 Answers2

0

Maybe you could try using tf.nn.softmax_cross_entropy_with_logits rather than tf.nn.sparse_softmax_cross_entropy_with_logits inside the cost calculation.

However, if you want to continue to use tf.nn.sparse_softmax_cross_entropy_with_logits then this link might help: Tensorflow ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments .

By the way, what are the versions of tensorflow and python you're using?

Try running this:

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}))
Alexandre
  • 101
  • 1
  • 6
  • My python version is 3.6.3 and my tensorflow version is 1.5.0. I tried your suggestion and I got the same exact error – Phillipus Nghipangelwa Feb 10 '18 at 17:18
  • Try this line to see if it works: cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=tf.squeeze(y))) – Alexandre Feb 10 '18 at 17:22
  • Well let's try some more combinations then, hopefully at some point you won't get any errors and then you can build from there. Try: cost = (1) tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=y) (2) the commented line below the one you're trying for cost, does it work? (with and without tf.squeeze(y)) – Alexandre Feb 10 '18 at 17:36
  • All failed, I got to say, this is brutal – Phillipus Nghipangelwa Feb 10 '18 at 17:43
  • Yeah! That's usually how it goes. But you get this code to run, not to worry. Did you try the cost **without** using tf.reduce_mean for the option (1)? Just: cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=y) – Alexandre Feb 10 '18 at 17:47
  • Calling the method train_neural_network(x) seems to be failing too, That is how a method is called in Python yes? – Phillipus Nghipangelwa Feb 10 '18 at 17:47
  • Yes I did, again... Fails – Phillipus Nghipangelwa Feb 10 '18 at 17:49
  • It's giving you errors because of the cost calculation. The thing is it looks like you're not adding anything to the function. You have to add the data in there: train_neural_network(your_data_of_interest) rather than train_neural_network(x) because x is just a placeholder variable – Alexandre Feb 10 '18 at 18:01
  • Oh!!!!!!! That makes sense, how does one pass in the data in the function though? – Phillipus Nghipangelwa Feb 10 '18 at 18:06
  • That's what I'm researching right now! Try compiling only up to mnist and seeing what it gives you. I'll try on my end to see what it is. – Alexandre Feb 10 '18 at 18:08
  • Up to mnist?? Like my 3rd line?? – Phillipus Nghipangelwa Feb 10 '18 at 18:11
  • Try mnist.train as your input data for train_neural_network(x). That is: train_neural_network(mnist.train). Or maybe train_neural_network(mnist.train.images) – Alexandre Feb 10 '18 at 18:12
  • If this doesn't work, look at the new edit from the answer! Try running the code I just added there! – Alexandre Feb 10 '18 at 18:22
  • Oh I see, all you did was remove the function, or is there something else that I missed in the edited code? – Phillipus Nghipangelwa Feb 10 '18 at 18:57
  • In the last line of neural_network_model function, the output does not have the tf.add() in it, which is important. I've also added the logits= and labels= in the cost line. Did it work? – Alexandre Feb 10 '18 at 18:59
  • The same line shows the error, but before that it shows the message that I will indicate in my next comment – Phillipus Nghipangelwa Feb 10 '18 at 19:03
  • Okay, with ALL changes made, the following error comes up – Phillipus Nghipangelwa Feb 10 '18 at 19:06
  • Traceback (most recent call last): File "firstAI.py", line 62, in cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction,labels=tf.squeeze(y))); File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1964, in sparse_softmax_cross_entropy_with_logits precise_logits, labels, name=name) File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 4804, in _sparse_softmax_cross_entropy_with_logits labels=labels, name=name) That's the first part – Phillipus Nghipangelwa Feb 10 '18 at 19:07
  • File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 609, in _apply_op_helper param_name=input_name) File "/home/phillipus/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint ", ".join(dtypes.as_dtype(x).name for x in allowed_list))) TypeError: Value passed to parameter 'labels' has DataType float32 not in list of allowed values: int32, int64 the second part – Phillipus Nghipangelwa Feb 10 '18 at 19:09
  • I just updated the code in my answer! See if that works please! – Alexandre Feb 10 '18 at 19:16
  • What parts were changed?? Trying it now – Phillipus Nghipangelwa Feb 10 '18 at 19:20
  • I used the commented cost you had (second line below the used cost variable). That was the only change. By the way, you could have added the function like you did originally...I just took it out for personal preference. All the changes I made you can make on your original code. My comment about train_neural_network(x) can be disregarded. I though data was going in, but it was not. – Alexandre Feb 10 '18 at 19:22
  • THANK YOU THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! YOU WERE A HUGE HELP!!!!! THANK YOU SO MUCH... IT WORKED!!!!!!! – Phillipus Nghipangelwa Feb 10 '18 at 19:24
  • I'm really glad I could help you Phillipus! Do you mind tagging my answer as the correct one? Try implementing the changes I made in your own original code (the way you originally intended). I'm pretty confident it will work! – Alexandre Feb 10 '18 at 19:27
  • Quick question though, what is the reduction for (tf.reduce), I mean, aren't we minimizing the cross_entropy through the optimizer?? – Phillipus Nghipangelwa Feb 10 '18 at 19:29
  • Good question. As I understand it, the intention is to minimize the cost you calculated. You can do it in many ways. The function tf.reduce_mean just returns the mean from whatever you added as input. I think you can do it with other functions as well (eg tf.reduce_max). Try them out in the cost and see how it works. – Alexandre Feb 10 '18 at 19:34
  • Another question, if you don't mind. Uhm, like right now, I am only training my model and it keeps getting between 94 to 96 percent. (1) How do I increase the accuracy, are more hidden layers needed to achieve this? (2) Is it possible to load my own data into the model and it prints out what that data is? For example if I hand write the number 8, can I load it into the model then it prints "This number is 8", according to how accurate it is of course. – Phillipus Nghipangelwa Feb 10 '18 at 19:46
  • (1) That's the hard question! There is no one answer to this. You may find yourself testing many different configurations (number of layers, how wide each layer is). Maybe going as far as changing the type of neural network you're applying. (2) There definitely is a way! Once you have trained a neural network you can save the graph and its parameters. Then, to apply it to new data coming in you load your graph and apply it! I've never done it myself, but I sense it's not as hard as it seems. – Alexandre Feb 10 '18 at 20:43
  • I will find out how its done, Thank you once again, much appreciated – Phillipus Nghipangelwa Feb 11 '18 at 01:06
0

Try this code

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

#height x width

x = tf.placeholder('float',[None, 784])
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]))}

    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_v2(logits=prediction,labels=y))

    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10

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

        for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(mnist.train.num_examples/batch_size)):
                epoch_x,epoch_y  = mnist.train.next_batch(batch_size)
                _,epoch_c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})
                epoch_loss += epoch_c
            print('Epoch', epoch, 'completed out of ', hm_epochs, 'loss: ', epoch_loss)
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:', accuracy.eval({x:mnist.test.images, y: mnist.test.labels}))



train_neural_network(x)