分类器classifier

之前的回归问题,预测的结果是数值型的,分类器预测出的数据是标称型
一个手写数字识别的分类器

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#number 1 to 10 data
#如果没有这个数据包,会自动从网上帮你下载下来
mnist = input_data.read_data_sets('MNIST_data',one_hot = True)

引入一个mnist的数据包,这里从网上下载已经下载不了了,手动导入数据:

http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_download.html 这个网站上下载以下四个压缩包到项目目录下面的MNIST_data文件夹下面:

tensorflow 分段函数 tensorflow分类器_#define

tensorflow 分段函数 tensorflow分类器_tensorflow 分段函数_02

tensorflow 分段函数 tensorflow分类器_tensorflow 分段函数_03


定义两个placeholder传值变量:

#define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,784])#不规定他的格式,但是规定它的大小28*28
ys = tf.placeholder(tf.float32,[None,10])

添加一层神经网络层:
神经网络的输出是10个数,对应的是该识别结果是0-9十个数字的概率

#add output layer
prediction = add_layer(xs,784,10,activation_function = tf.nn.softmax)
#softmax一般适用于做classify的激励函数
#add一层神经层,输入点784个,输出10个,激励函数用softmax

计算误差

#the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices = [1]))
#loss
#用cross_entropy这种算法

训练

#train
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#使用GradientDescentOptimizer优化器
#学习效率是0.5,使loss最小化

执行过程

sess = tf.Session() #important step

sess.run(tf.initialize_all_variables())

for i in range(1000):
    batch_xs,batch_ys = mnist.train.next_batch(100)
    #batch_xs.shape = (100,784);batch_ys.shape = (100,10)
    #从下载好的MNIST_data中提取出训练数据中的100个
    #不学习整个data,而是每100个学习一下,就效率比较高
    sess.run(train_step,feed_dict = {xs:batch_xs,ys:batch_ys})
    if i % 50 == 0:
        #计算准确度的函数compute_accuracy
        print(compute_accuracy(mnist.test.images,mnist.test.labels))
        #mnist里面包含了test_data和train_data,用test_data的labels与预测值对比,算准确值,测试功能

其中的两个函数add_layer和compute_accuracy:

def add_layer(inputs,in_size,out_size,activation_function = None):
    #add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size,out_size]))
    biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
    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

def compute_accuracy(v_xs,v_ys):
    #将prediction弄成全局变量
    global prediction
    #用xs带入到prediction里面,得到预测值y_pre
    y_pre = sess.run(prediction,feed_dict = {xs:v_xs})
    #检测结果是否正确,将v_xs得出的y_pre与v_ys对比
    #print(y_pre.shape)
    #print(v_ys.shape)
    correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
    #print(correct_prediction)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    #print(accuracy)
    result = sess.run(accuracy,feed_dict = {xs:v_xs,ys:v_ys})
    #得到正确率
    return result

mnist数据集里面一共有训练数据集55000个,测试数据集10000个,用55000算太烧cpu了,就让他一次训练100个,100个100个的学习,这样效率比较高,而且准确度也不一定会比一次学习55000个差

batch_xs,batch_ys = mnist.train.next_batch(100)

tf.equal函数

import tensorflow as tf
import numpy as np
 
A = [[1,3,4,5,6]]
B = [[1,3,4,3,2]]
 
with tf.Session() as sess:
    print(sess.run(tf.equal(A, B)))

结果:[[ True True True False False]]
所以correct_prediction只是得到的一串bool类型的,accuracy才是得到的正确率

我们可以看到执行步骤是这样的:
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

先导入了这四个文件,然后计算每五十次学习的精确度

tensorflow 分段函数 tensorflow分类器_tensorflow 分段函数_04


然后得到一次次的训练成绩,我们可以看到预测的越来越准确

完整程序:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#number 1 to 10 data
#如果没有这个数据包,会自动从网上帮你下载下来
mnist = input_data.read_data_sets('MNIST_data',one_hot = True)

def add_layer(inputs,in_size,out_size,activation_function = None):
    #add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size,out_size]))
    biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
    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

def compute_accuracy(v_xs,v_ys):
    #将prediction弄成全局变量
    global prediction
    #用xs带入到prediction里面,得到预测值y_pre
    y_pre = sess.run(prediction,feed_dict = {xs:v_xs})
    #检测结果是否正确
    #print(y_pre.shape)
    #print(v_ys.shape)
    correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
    #print(correct_prediction)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    #print(accuracy)
    result = sess.run(accuracy,feed_dict = {xs:v_xs,ys:v_ys})
    #得到正确率
    return result

#define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,784])#不规定他的格式,但是规定它的大小28*28
ys = tf.placeholder(tf.float32,[None,10])


#add output layer
prediction = add_layer(xs,784,10,activation_function = tf.nn.softmax)
#add一层神经层,输入点784个,输出10个,激励函数用softmax

#the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices = [1]))
#loss
#用cross_entropy这种算法

#train
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#学习效率是0.5,使loss最小化

sess = tf.Session() #important step

sess.run(tf.initialize_all_variables())

for i in range(1000):
    batch_xs,batch_ys = mnist.train.next_batch(100)
    #batch_xs.shape = (100,784);batch_ys.shape = (100,10)
    #从下载好的MNIST_data中提取出训练数据中的100个
    #不学习整个data,而是每100个学习一下,就效率比较高
    sess.run(train_step,feed_dict = {xs:batch_xs,ys:batch_ys})
    if i % 50 == 0:
        #计算准确度的函数compute_accuracy
        print(compute_accuracy(mnist.test.images,mnist.test.labels))
        #mnist里面包含了test_data和train_data,用test_data的labels与预测值对比,算准确值,测试功能