tensorflow识别手写数字

数据集下载:

http://yann.lecun.com/exdb/mnist/

文件 内容
train-images-idx3-ubyte.gz 训练集图片 - 55000 张 训练图片, 5000 张 验证图片
train-labels-idx1-ubyte.gz 训练集图片对应的数字标签
t10k-images-idx3-ubyte.gz 测试集图片 - 10000 张 图片
t10k-labels-idx1-ubyte.gz 测试集图片对应的数字标签


推荐资源:

Tensorflow中文社区


完整代码:

import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)

for i in range(20):
    one_hot_label=mnist.train.labels[i,:]
    label=np.argmax(one_hot_label)
    print('mnist_train_%d.jpg label: %d' %(i,label))

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_mean(-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})

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


个人资料
刀神T
等级:6
文章:16篇
访问:1.1w
排名: 22
上一篇: 机器学习算法岗位直接内推二
下一篇:逆序打印单链表--python
标签: tf、mnist、train、ubyte、labels、面试题
隐藏