Tensorflow
使用1.14.0版本
MNIST
本次笔记要用MNIST数据集训练一个识别手写数字的简单网络。单层,无隐层,无卷积,无脑。
MNIST是一个入门计算机视觉数据集,包含了很多手写数字图片。
数据集中包含了 55000 行的训练数据集(mnist.train)、5000 行验证集(mnist.validation)和 10000 行的测试数据集(mnist.test)
概况



1、图片为灰度图,28*28矩阵(单通道,每个像素范围0-255,0为黑色,255为白色,这一点与MNIST中的正好相反)
2、28*28的矩阵转换成1维矩阵,one-hot读入已经实现了
3、1*10的向量代表标签,也就是这个数字到底是几,举个例子e数字1对应的矩阵就是[0,1,0,0,0,0,0,0,0,0],y_lable就是这样
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
# 在这里程序会首先下载 MNIST 数据集,然后解压并保存到刚刚制定好的 MNIST_data 文件夹中,然后输出数据集对象。
print(mnist)
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Datasets(train=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fcee61de610>, validation=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fcee7813a10>, test=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fcee7826ad0>)
# 准备数据
# 1、提取100个样本用于训练
batch_xs,batch_ys = mnist.train.next_batch(100)
# 100个图片,每个图片已经是一个28*28 = 784的一维向量了
print(batch_xs.shape)
print(batch_ys.shape)
print(batch_xs[0].shape)
# y长这样
print(batch_ys[0])
(100, 784)
(100, 10)
(784,)
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
import tensorflow as tf
import numpy as np
# 可以开始了,先定义添加层
# 一个创建层的函数,可以用来创建隐层,也可以是输出层
def add_layer(inputs,in_size,out_size,activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size,out_size]))
biases = tf.Variable(tf.zeros([1,out_size]))
Wx_plus_b = tf.matmul(inputs,Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# step1 :define placehold for inputs to network
xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])
# step2: add outputlayer
prediction = add_layer(xs,784,10,activation_function=tf.nn.softmax)
# step3: the error between prediction and data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
# step4 : train 梯度下降
train_step= tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# 先定义一个精度函数
def compute_accuracy(v_xs,v_ys):
global prediction
y_pre = sess.run(prediction,feed_dict={xs:v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
accuarcy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result = sess.run(accuarcy,feed_dict={xs:v_xs,ys:v_ys})
return result
with tf.Session() as sess:
init =tf.global_variables_initializer()
sess.run(init)
# 还记得我们使用的输入数据吗,往前看batch_xs,batch_ys
for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100)
sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys})
if i%50==0:
print(compute_accuracy(mnist.test.images,mnist.test.labels))
0.0762
0.6343
0.7389
0.7794
0.8025
0.8263
0.8323
0.8426
0.8448
0.8539
0.8594
0.8595
0.8645
0.8695
0.8715
0.872
0.8749
0.8765
0.8739
0.8805
网友评论