对于神经网络实现手写数字识别(MNIST)网络结构通过在线可视化工具查看和修改:
(http://ethereon.github.io/netscope/#/editor )
一、卷积层(Convolution)
输入为28*28的图像,经过5*5的卷积之后,得到一个(28-5+1)*(28-5+1) = 24*24的map。
每个map是不同卷积核在前一层每个map上进行卷积,并将每个对应位置上的值相加然后再加上一个偏置项。
二、池化层(Pooling)
输入为卷积层1的输出,大小为24*24,对每个不重叠的2*2的区域进行降采样。对于max-pooling,选出每个区域中的最大值作为输出。而对于mean-pooling,需计算每个区域的平均值作为输出。最终,该层输出一个(24/2)*(24/2)的map
三、激活层(ReLU)
激活函数是用来引入非线性因素的。当激活函数输出值是有限的时候,基于梯度的优化方法会更加稳定,因为特征的表示受有限权值的影响更显著;当激活函数的输出是无限的时候,模型的训练会更加高效,不过在这种情况小,一般需要更小的learning rate.
Type为该层类型,可取值分别为:
1、ReLU:表示我们使用relu激活函数,relu层支持in-place计算,这意味着该层的输入和输出共享一块内存,以避免内存的消耗。
2、Sigmoid:代表使用sigmoid函数;
3、TanH:代表使用tanh函数;
4、AbsVal:计算每个输入的绝对值f(x)=Abs(x)
5、Power对每个输入数据进行幂运算
f(x)= (shift + scale * x) ^ power
层类型:Power
可选参数:
power: 默认为1
scale: 默认为1
shift: 默认为0
四、全连接层(InnerProduct)
50*4*4=800个输入结点和500个输出结点
五、softmax层
Softmax回归模型是logistic回归模型在多分类问题上的推广,在多分类问题中,待分类的类别数量大于2,且类别之间互斥。通常情况下softmax会被用在网络中的最后一层,用来进行最后的分类和归一化。
神经网络实现手写数字识别(MNIST)的 prototxt 文件源码:
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/gh/caffe/examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/gh/caffe/examples/mnist/mnist_test_lmdb"
batch_size: 100
backend: LMDB
}
}
layer {
name: "conv1" #卷积层1
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1 #学习率1,和权值更新相关
}
param {
lr_mult: 2 #学习率2,和权值更新相关
}
convolution_param {
num_output: 20 #20个输出的map
kernel_size: 5 #卷积核大小为5*5
stride: 1 #卷积步长为1
weight_filler { #权值初始化方式
type: "xavier" #默认为“constant",值全为0,很多时候我们也可以用"xavier"或者”gaussian"来进行初始化
}
bias_filler { #偏置值的初始化方式
type: "constant" #该参数的值和weight_filler类似,一般设置为"constant",值全为0
}
}
}
layer {
name: "pool1" #池化层1
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX #Pool为池化方式,默认值为MAX,可以选择的参数有MAX、AVE、STOCHASTIC
kernel_size: 2 #池化区域的大小,也可以用kernel_h和kernel_w分别设置长和宽
stride: 2 #步长,即每次池化区域左右或上下移动的距离,一般和kernel_size相同,即为不重叠池化。也可以也可以小于kernel_size,即为重叠池化,Alexnet中就用到了重叠池化的方法
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU" #relu激活函数层支持in-place计算,这意味着该层的输入和输出共享一块内存,以避免内存的消耗。
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer { #可以计算给出每个样本对应的损失函数值
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}