softmax_loss层,Inner_Product层,accuracy层,reshape层和dropout层及其他参数配置。
1、softmax-loss
softmax-loss层和softmax层计算大致相同,softmax是一个分类器,计算的是类别概率,是Logistic Regression的一种推广。
softmax与softmax-loss的区别:
softmax计算公式:
而softmax-loss计算公式:
用户可能最终目的就是得到各个类别的概率似然值,这个时候就需要一个Softmax层,而不一定要进行softmax-loss操作;或者用户有通过其他什么方式已得到了某种概率似然值,然后做最大似然估计,此时则只需要后面的softmax-loss操作。因此提供两个不同的Layer结构比只提供一个合在一起的Softmax-loss Layer要灵活许多。
softmax-loss layer:输出loss值
layer{
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip1"
bottom: "label"
top: "loss"
}
softmax layer:输出似然值
layer{
bottom: "cls3_fc"
top: "prob"
name: "prob"
type: "Softmax"
}
2、Inner Product
全连接层,把输入当作成一个向量,输出也是一个简单向量。
输入:n*c0*h*w
输出:n*c1*1*1*1
全连接层实际上也是一种卷积层,只是它的卷积核大小和原数据大小一致。因此它的参数基本和卷积层参数一样。
层类型:InnerProduct
lr_mult:学习率的系数,最终的学习率这个数乘以solver.protxt配置文件中的base_lr。如果有两个lr_mult,则第一个表示权值的学习率,第二个表示偏置项的学习率。一般偏置项的学习率是权值学习率的两倍。
必须设置的参数:
num_output: 过滤器的个数
其他参数:
weight_filler:权值初始化。默认为”constant”值全为0,很多时候我们用”xavier”算法来进行初始化,也可以设置为”gaussian”
bias_filler:偏置项的初始化。一般设置为”constant”,值全为0.
bias_term:是否开启偏置项,默认为true,开启
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"
}
}
}
3、accuracy
输出分类精确度,只有test阶段才有,因此需要加入include参数
层类型: Accuracy
layer{
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include{
phase: TEST
}
}
4、reshape
层类型:Reshape
在不改变数据的情况下,改变输入的维度
层类型:Reshape
layer{
name: "reshape"
type: "Reshape"
bottom: "input"
top: "output"
reshape_param{
shape{
dim: 0 #copy the dimension from below
dim: 2
dim: 3
dim: -1 #infer it from the other dimension
}
}
}
有一个可选的参数组shape,用于指定blob数据的各维的值(blob是一个四维的数据:n*c*w*h)
dim:0 表示维度不变,即输入和输出是相同的维度
dim:2或dim:3 将原来的维度变成2或3
dim: -1表示由系统自动计算维度,数据的总量不变,,系统会根据blob数据的其他三维来自动计算当前维的维度值。
假设原数据为:64*3*28*28,表示64张3通道28*28的彩色图片
经过reshape变换
reshape_param{
shape{
dim: 0
dim: 0
dim: 14
dim: -1
}
}
输出数据为:64*3*14*56
5、Dropout
Dropout是一个防止过拟合的trick。可以随机让网络某些隐含层节点的权重不工作。
layer{
name: "drop7"
type: "Dropout"
bottom: "fc7-conv"
top: "fc7-conv"
dropout_param{
dropout_ratio: 0.5
}
}