Caffe-02-网络配置与solver超参数配置详解(二)

简介: Caffe-02-网络配置与solver超参数配置详解

softmax-loss层


例子如下,下面的例子分别输出的是loss值和似然值

##输出loss值
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}
##输出似然值
layer {
  name: "prob"
  type: "Softmax"
  bottom: "cls3_fc"
  top: "prob"
}

reshape层


有时候,我们希望在不改变数据的时候改变输入的维度,所以可以使用reshape进行调整输入数据的参数,例子如下:

layer {
  name: "reshape"
  type: "Reshape"
  top: "data"
  reshape_param { 
  shape: { 
  dim: 0
  dim: 3 
  dim: 32 
  dim: -1 
  } 
}
}
参数解释:在shape中使用参数进行指定输入的数据的大小通道数和批次个数。
dim:0 表示维度不变,和原来相同
dim:3 把原来的维度变成3
dim: -1 表示由系统进行自动计算维度,数据的总量不变。

由上述的例子可以假设输入了10张3通道的64X32的彩色图片),

根据上述的数据设置,批次大小没有改变,然后通道数仍然为32数据的宽为32,高自动计算和原来总量一直,因为原来为64X32,调整宽为32,这里计算后高变为了64。

dropout层


为了防止过拟合,设置dropout层。这里只需要进行配置一个参数dropout_ratio,即可完成dropout的配置,dropout就是随机挑选一些数据进行不使能,模拟神经元遗忘的过程。

layer {
  name: "drop"
  type: "Dropout"
  top: fc1"
  bottom:"fc1"
  dropout_param{
    dropout_ratio: 0.5
  }
}

solver超参数配置详解


因为神经网络的函数往往都是都是非凸的,也就是无法通过数学解析式的方式找到最优解,这时就需要对该网络下的训练参数进行调整设置,已达到更好的训练效果。这里把网络的参数配置文件单独放在了solver.prototxt中,方便对参数进行调整优化。

在例程中给出solver的一个例子,然后对例子进行分析。

# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10
# The train/test net protocol buffer definition
net: "examples/cifar10/cifar10_quick_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.0001
momentum: 0.9
weight_decay: 0.004
# The learning rate policy
lr_policy: "fixed"
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 500
# snapshot intermediate results
snapshot: 500
snapshot_format: HDF5
snapshot_prefix: "examples/cifar10/cifar10_quick"
# solver mode: CPU or GPU
solver_mode: CPU

首先在之前的网络配置文件中,我们配置好了各层的网络的结构,所以这里要首先进行指定网络模型文件:

##网络模型的描述文件
#也可以进行训练和测试分别指定
#train_net="xxxxxxxxxxx"
#test_net ="xxxxxxxxxxx"
net: "examples/cifar10/cifar10_quick_train_test.prototxt"

然后定义测试的间隔和训练次数,训练次数这个参数要和test_layer结合考虑,如果在test_layer的每批次的大小是100而总共的测试数据为10000张,那么参数为10000/100=100。

test_iter: 100
#每训练500次进行一次测试
test_interval: 500

接着定义学习率、动力、权重值的衰减率等参数。

#学习率
base_lr: 0.0001
#动力 
momentum: 0.9

对于优化算法的选择可以忽略,默认为SGD,不同的优化算法差别不是很大。

#在caffe中一共有6种优化算法可以选择

  1. Stochastic Gradient Descent (type: SGD)
  2. AdaDelta (type: AdaDelta)
  3. Adaptive Gradient (type: AdaGrad)
  4. Adam (type: Adam)
  5. Nesterov’s Accelerated Gradient (type: Nesterov)
  6. RMSprop (type: RMSPorp)

权重衰减项,就是正则化项,作用是防止过度拟合。

weight_decay: 0.004

学习率的调整策略:

  • fixed :保持base_lr不变
  • step :如果设置为step,则需要设置一个strpsize,返回值为:base_lr X gamma ^(floor (iter / stepsize)) ,iter 为迭代次数。
  • exp : 返回值为base_lr X gamma ^i ter ,iter 为迭代次数。
  • inv :如果设置为了inv则还需要设置power和gamma项,返回值为base_lr * (1 + gamma * iter ) ^ (- power ),iter 为迭代次数。
  • multistep : 如果设置multistep ,还需要设置stepvalue,这个参数和step相似,step是均匀等间隔变化,而multistep 是根据stepvalue值变化。
  • poly :学习率进行多项式误差。返回:base_lr ( 1 - iter / max_iter )^ (power ) ,iter 为迭代次数。
  • sigmoid : 学习率进行sigmoid 衰减,返回:base_lr (1 / ( 1 + exp ( -gamma X(iter - stepsize))))

每训练100次进行一次屏幕显示,设置为0则不显示。

display: 100

最大迭代次数:

max_iter: 500

#快照,在训练每100次的时候保存一次,如果设置0则为不保存。

snapshot: 100
snapshot_format: HDF5
snapshot_prefix: "examples/cifar10/cifar10_quick"

#选择运行模式

solver_mode: CPU
目录
相关文章
|
1月前
|
监控 Linux Shell
【Shell 命令集合 网络通讯 】Linux 配置和管理网络流量的形状 shapecfg命令 使用指南
【Shell 命令集合 网络通讯 】Linux 配置和管理网络流量的形状 shapecfg命令 使用指南
39 0
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】Linux 设置和配置PPP pppsetup命令 使用教程
【Shell 命令集合 网络通讯 】Linux 设置和配置PPP pppsetup命令 使用教程
44 0
|
1月前
|
缓存 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 配置DNS dnsconf 命令 使用教程
【Shell 命令集合 网络通讯 】Linux 配置DNS dnsconf 命令 使用教程
39 0
|
1月前
|
网络协议 网络虚拟化 数据中心
华为配置VXLAN构建虚拟网络实现相同网段互通示例(静态方式)
配置VXLAN构建虚拟网络实现相同网段互通示例(静态方式
|
1月前
ifconfig 配置网络接口
ifconfig 配置网络接口。
18 1
|
1月前
|
网络协议 Linux Shell
搭建虚拟机的网络布局类型和配置操作
搭建虚拟机的网络布局类型和配置操作
|
1月前
|
域名解析 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
56 1
|
2月前
|
安全 网络协议 Linux
家庭实验室系列文章 - 电脑如何配置网络唤醒 (WOL)?
家庭实验室系列文章 - 电脑如何配置网络唤醒 (WOL)?
|
17天前
|
网络协议 Linux
在Linux中,管理和配置网络接口
在Linux中管理网络接口涉及多个命令,如`ifconfig`(在新版本中被`ip`取代)、`ip`(用于网络设备配置)、`nmcli`(NetworkManager的CLI工具)、`nmtui`(文本界面配置)、`route/ip route`(处理路由表)、`netstat/ss`(显示网络状态)和`hostnamectl/systemctl`(主机名和服务管理)。这些命令帮助用户启动接口、设置IP地址、查看连接和路由信息。不同发行版可能有差异,建议参考相应文档。
19 4
|
1月前
|
网络协议 Linux API
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
89 0