0

Hello and Greetings People from around the world,

I was using the original source code from:

https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py

And modified it into:

import tensorflow as tf
import numpy
from PIL import Image

tf.set_random_seed(0)

#Image
filenames = 'gray_kitten.jpg'

img = Image.open(filenames) 

num_maps = 3

img = numpy.asarray(img, dtype='float32') / 256.

img_shape = img.shape

img_reshaped = img.reshape(1, img_shape[0], img_shape[1], num_maps)

#Label

label = numpy.zeros((10))

#Manual label value assignment
label[0] = 1

label = numpy.array(label,dtype = numpy.float32)

label = tf.convert_to_tensor(label,tf.float32)


X = tf.placeholder(tf.float32, [1, None, None, 3])

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


W = tf.Variable(tf.zeros([(img_shape[0] * img_shape[1]), 10]))


b = tf.Variable(tf.zeros([10]))

XX = tf.reshape(X, [-1, (img_shape[0] * img_shape[1])])


# The model
Y = tf.nn.softmax(tf.matmul(XX, W) + b)


cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 10.0

correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)

allweights = tf.reshape(W, [-1])

allbiases = tf.reshape(b, [-1])


# init
init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)


label = sess.run([label])


# You can call this function in a loop to train the model
def training_step(i, update_test_data, update_train_data):



    if update_train_data:

    a, c, w, b = sess.run([accuracy, cross_entropy, allweights, allbiases], feed_dict={X: img_reshaped, Y_: label})
    print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c))


    if update_test_data:

        a, c = sess.run([accuracy, cross_entropy], feed_dict={X: img_reshaped, Y_: label})
    print("test accuracy:" + str(a) + " test loss: " + str(c))

    # the backpropagation training step
    sess.run(train_step, feed_dict={X: img_reshaped, Y_: label})

for i in range(100+1): 
    training_step(i, i % 50 == 0, i % 10 == 0)

When I execute the program, it will produce the following output:

    0: accuracy:1.0 loss: 2.30259
    test accuracy:1.0 test loss: 2.30259
    10: accuracy:1.0 loss: nan
    20: accuracy:1.0 loss: nan
    30: accuracy:1.0 loss: nan
    40: accuracy:1.0 loss: nan
    50: accuracy:1.0 loss: nan
    test accuracy:1.0 test loss: nan
    60: accuracy:1.0 loss: nan
    70: accuracy:1.0 loss: nan
    80: accuracy:1.0 loss: nan
    90: accuracy:1.0 loss: nan
    100: accuracy:1.0 loss: nan
    test accuracy:1.0 test loss: nan

What I am actually looking at is that I am hope for someone who can tell me on whether I have any mistake on image classification using soft-max algorithm with tensorflow.....? Because the the loss section produce "nan" value instead of "0"? and I am not sure my on whether my labeling was correct or wrong.

Thanks

dragon
  • 29
  • 5
  • `nan` may caused by `Y = 0` when doing `log(Y)`, check this [answer](http://stackoverflow.com/a/33713196/6306884) – xxi Mar 11 '17 at 08:54

1 Answers1

0

Thanks to @xxi, I manage to fix the "nan" value that produced by the previous cross entropy equation or formula.

import tensorflow as tf
import numpy
from PIL import Image

tf.set_random_seed(0)

#Image
filenames = 'gray_kitten.jpg'

img = Image.open(filenames) 

num_maps = 3

img = numpy.asarray(img, dtype='float32') / 256.

img_shape = img.shape

img_reshaped = img.reshape(1, img_shape[0], img_shape[1], num_maps)

#Label

label = numpy.zeros((10))

#Manual label value assignment
label[0] = 1

label = numpy.array(label,dtype = numpy.float32)

label = tf.convert_to_tensor(label,tf.float32)


X = tf.placeholder(tf.float32, [1, None, None, 3])

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


W = tf.Variable(tf.zeros([(img_shape[0] * img_shape[1]), 10]))


b = tf.Variable(tf.zeros([10]))

XX = tf.reshape(X, [-1, (img_shape[0] * img_shape[1])])


# The model
Y = tf.nn.softmax(tf.matmul(XX, W) + b)


#cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 10.0
cross_entropy = -tf.reduce_mean(Y_ * tf.log(tf.clip_by_value(Y, 1e-10, 1.0))) * 10.0

correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)

allweights = tf.reshape(W, [-1])

allbiases = tf.reshape(b, [-1])


# init
init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)


label = sess.run([label])


# You can call this function in a loop to train the model
def training_step(i, update_test_data, update_train_data):

    if update_train_data:
        #Train Data
        a, c, w, b = sess.run([accuracy, cross_entropy, allweights, allbiases], feed_dict={X: img_reshaped, Y_: label})
        print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c))

    if update_test_data:
        #Test Data
        a, c = sess.run([accuracy, cross_entropy], feed_dict={X: img_reshaped, Y_: label})
        print(str(i) + ": ********* epoch " +  " ********* test accuracy:" + str(a) + " test loss: " + str(c))

    # the backpropagation training step
    sess.run(train_step, feed_dict={X: img_reshaped, Y_: label})

for i in range(100+1): 
    training_step(i, i % 50 == 0, i % 10 == 0)

Result:

    0: accuracy:1.0 loss: 2.30259
    0: ********* epoch  ********* test accuracy:1.0 test loss: 2.30259
    10: accuracy:1.0 loss: -0.0
    20: accuracy:1.0 loss: -0.0
    30: accuracy:1.0 loss: -0.0
    40: accuracy:1.0 loss: -0.0
    50: accuracy:1.0 loss: -0.0
    50: ********* epoch  ********* test accuracy:1.0 test loss: -0.0
    60: accuracy:1.0 loss: -0.0
    70: accuracy:1.0 loss: -0.0
    80: accuracy:1.0 loss: -0.0
    90: accuracy:1.0 loss: -0.0
    100: accuracy:1.0 loss: -0.0
    100: ********* epoch  ********* test accuracy:1.0 test loss: -0.0

I hope my labeling was correct and implemented in a correct way. On the other hand, if any of you guys have better approach or found any mistake in the program, please feel free to comment about it.

Thanks

dragon
  • 29
  • 5