Hello and Greetings来自世界各地的人们,
我使用的是原始源代码:
https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py
并将其修改为:
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)
当我执行程序时,它将产生以下输出:
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
我实际看到的是,我希望有人能告诉我是否在使用带有张量流的soft-max算法进行图像分类时有任何错误.....? 因为损失部分产生了" nan"值而不是" 0"? 而且我不确定我的标签是正确还是错误。
谢谢
答案 0 :(得分:0)
感谢@xxi,我设法修复了" nan"由先前的交叉熵方程或公式产生的值。
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)
结果:
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
我希望我的标签是正确的,并以正确的方式实施。另一方面,如果你们中的任何人有更好的方法或在程序中发现任何错误,请随时评论。
谢谢