这是tensorflow库的第一个示例。 它的正确率约为91%。 但是当我认出自己的数字时,我遇到了问题。
#python3
import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
mnist=input_data.read_data_sets("MNIST_data/", one_hot=True)
from PIL import Image,ImageFilter
x=tf.placeholder(tf.float32,[None,784])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y=tf.nn.softmax(tf.matmul(x,w)+b)
y_=tf.placeholder(tf.float32,[None,10])
cross_entropy=-tf.reduce_sum(y_*tf.log(y))
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
sess=tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
batch_xs,batch_ys=mnist.train.next_batch(100)
sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})
print(prediction.eval(feed_dict={x:[l]},session=sess)[0])
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))
img=Image.open("b3.jpg").convert("L")
img=img.resize((28,28),Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
imgArr=img.getdata()
l=[i/255.0 for i in imgArr]
prediction=tf.argmax(y,1)
print(prediction.eval(feed_dict={x:[l]},session=sess)[0])
我的手写号码是3但结果是2?