一个菜鸡的自强之路-MNIST手写数字分类问题(进阶篇)

版权声明:版权所有 翻版必究 转载请注明来源 谢谢配合!!! https://blog.csdn.net/snail_youth/article/details/89004356

各种模块各种函数一一查询,还是不是很理解MNIST手写数字分类问题(进阶篇)的实现,作为菜鸡先记录下来,留作以后复盘使用。废话不多说,先进入正题

一、实例代码


import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 1, 32])
# 1通道卷积,卷积出32个特征
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 1, 32])
# 1通道卷积,卷积出32个特征
b_conv1 = bias_variable([32])
# 32个偏执数据
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
                                         

二、模块函数小解

1、tensorflow.truncated_normal()

格式:tf.truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None )

意义:从截断的正态分布中输出随机值. 生成的值遵循具有指定平均值和标准偏差的正态分布,如生成值跟均值的差值大于2倍标准差,则舍弃重新生成.

参数解析:shape:一维整数张量或 Python 数组,输出张量的形状.

                  mean:dtype 类型的 0-D 张量或 Python 值,截断正态分布的均值.

                  stddev:dtype 类型的 0-D 张量或 Python 值,截断前正态分布的标准偏差.

                  dtype:输出的类型.

                  seed:一个 Python 整数.用于为分发创建随机种子.查看tf.set_random_seed行为. 整数,确定后返回值不在变化.

                  name:操作的名称(可选,爱写不写).

实例:

2、tensorflow.nn.conv2d()

格式:tf.nn.conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', dilations=[1, 1, 1, 1], name=None )

意义:计算给定的4-D input和filter张量的2-D卷积.

参数解析:

input:一个Tensor,必须是下列类型之一:half,bfloat16,float32,float64;一个4-D张量,维度顺序根据data_format值进行解释.

filter:一个Tensor,必须与input相同,形状为[filter_height, filter_width, in_channels, out_channels]的4-D张量.

strides:ints列表,长度为4的1-D张量,input的每个维度的滑动窗口的步幅;维度顺序由data_format值确定.

padding:string,可以是:"SAME", "VALID",要使用的填充算法的类型. use_cudnn_on_gpu:bool,默认为True.

data_format:string,可以是"NHWC", "NCHW",默认为"NHWC";指定输入和输出数据的数据格式;使用默认格式“NHWC”,数据按以下顺序存储:[batch, height, width, channels];或者,格式可以是“NCHW”,数据存储顺序为:[batch, channels, height, width].

dilations:ints的可选列表,默认为[1, 1, 1, 1],长度为4的1-D张量,input的每个维度的扩张系数;如果设置为k> 1,则该维度上的每个滤镜元素之间将有k-1个跳过的单元格;维度顺序由data_format值确定;批次和深度尺寸的扩张必须为1.

name:操作的名称(可选,爱写不写).

3、tensorflow.nn.max_pool()

格式:tf.nn.max_pool( value, ksize, strides, padding, data_format='NHWC', name=None )

意义:在输入上执行最大池化

参数解析:

value:由data_format指定格式的4-D Tensor.

ksize:具有4个元素的1-D整数Tensor.输入张量的每个维度的窗口大小.

strides:具有4个元素的1-D整数Tensor.输入张量的每个维度的滑动窗口的步幅.

padding:一个字符串,可以是'VALID'或'SAME'.填充算法.

data_format:一个字符串.支持'NHWC','NCHW'和'NCHW_VECT_C'.

name:操作的名称(可选,爱写不写).

实例:

第一步:定义一个张量

得到的结果为:

第二步:重塑张量

第三步:最大池化

valid算法:若有多余,直接丢掉最右边的列(或最底部的行)

嗯?也许看来半天也处在迷糊中,为什么会是这个结果呢?接下来就解析下为什么会是这个结果。

ksize=[1,2,2,1]形状为  [[ ] [ ]

                                     [ ] [ ]] ,

最池化过程:

channel1最大池化过程:将channel第一行的一二列和第二行的一二列进行最大池化得到结果8,将channel第一行的二三列和第二行的二三列进行最大池化得到结果6,遍历循环,直到最大池化完得到右边的图。

channel2的最大池化过程同channel1。

最终池化完成得到的结果为

过程明细图

same算法:如有少,尝试向左和向右均匀填充0,但如果要添加的列数是奇数,它将向右添加额外的列

4、tensorflow.nn.relu()

格式:tf.nn.relu( features, name=None )

意义:计算校正线性:max(features, 0).

参数解析:

features:一个Tensor.必须是下列类型之一:float32,float64,int32,uint8,int16,int8,int64,bfloat16,uint16,half,uint32,uint64.

name:操作的名称(可选,爱写不写).

实例:

5、tensorflow.nn.dropout()

格式:tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None )

意义:使用概率keep_prob,输出按照1/keep_prob的比例放大输入元素,否则输出0.缩放是为了使预期的总和不变.

参数解析:

x:一个浮点型Tensor.

keep_prob:一个标量Tensor,它与x具有相同类型.保留每个元素的概率.

noise_shape:类型为int32的1维Tensor,表示随机产生的保持/丢弃标志的形状.

seed:一个Python整数.用于创建随机种子.

name:此操作的名称(可选,爱写不写).

实例:

6、tensorflow.ones()

格式:ones( shape, dtype=tf.float32, name=None )

意义:创建一个张量,所有元素的值为1.

参数解析:

shape:整数的列表,整数的元组或 int32 类型的一维张量.

dtype:生成的张量中元素的类型.

name:操作的名称(可选,爱写不写).

实例:

上一篇:一个菜鸡的自强之路-MNIST手写数字分类问题(基础篇)

猜你喜欢

转载自blog.csdn.net/snail_youth/article/details/89004356