莫烦python|Tensorflow笔记--Classification 分类学习

Classification 分类问题,定性输出是分类,或者说是离散变量预测。 
Regression 回归问题,定量输出是回归,或者说是连续变量预测。

from __future__ import print_function  # 强制使用python3版本,不管python使用的是什么版本
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data  # 导入mnist库
# number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)   # 如果没有mnist数据就进行下载,使用one_hot编码

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):   #  计算精度函数
    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))    # 函数tf.equal(x,y,name=None)对比x与y矩阵/向量中相等的元素,相等的返回True,不相等返回False,返回的矩阵/向量的维度与x相同;tf.argmax()返回最大值对应的下标(1表示每一列中的,0表示每一行)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  #  tf.cast()类型转换函数,将correct_prediction转换成float32类型,并对correct_prediction求平均值得到arruracy
    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]) # 28x28    #输入为N个图片,每个图片由28x28 个像素点组成
ys = tf.placeholder(tf.float32, [None, 10])    # 输出N个数据 ,每张图片识别一个数字 0-9共十种

# add output layer
prediction = add_layer(xs, 784, 10,  activation_function=tf.nn.softmax)   # 输入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  loss函数(即最优化目标函数)选用交叉熵函数。交叉熵用来衡量预测值和真实值的相似程度,如果完全相同,它们的交叉熵等于零。
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)     # 梯度下降法,学习速率是0.5

sess = tf.Session()
# important step
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
sess.run(init)    # 初始化模型的参数

for i in range(1000):    #训练
    batch_xs, batch_ys = mnist.train.next_batch(100)   # 每次采用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))  # 测试集,images是输入,labels是标签

结果

E:\Anaconda\python.exe D:/code/tutorials-master(莫烦示例)/tensorflowTUT/tf16_classification/full_code.py
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
0.1391
0.6349
0.7283
0.7699
0.797
0.8144
0.8259
0.8345
0.8429
0.8478
0.853
0.8572
0.8584
0.8585
0.8598
0.8652
0.8683
0.8687
0.8724
0.8751

关于MNIST库:

MNIST库是手写数字库,含55000张训练图片,每张图片的分辨率是28×28。

这里写图片描述

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

print(mnist.train.images.shape) #训练
print(mnist.train.labels.shape) 
print(mnist.validation.images.shape) #校验
print(mnist.validation.labels.shape)
print(mnist.test.images.shape) #测试
print(mnist.test.labels.shape)

这里写图片描述

softmax激励函数

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值