深度学习剖根问底:caffe常用的优化算法solver实现

常见算法

SGD

x+= -learning_rate*dx

Momentum

Momentum可以使SGD不至于陷入局部鞍点震荡,同时起到一定加速作用。 
Momentum最开始有可能会偏离较远(overshooting the target),但是通常会慢慢矫正回来。

v = mu*v - learning_rate*dx
x+= v

Nesterov momentum


AdaGrad

使用每个变量的历史梯度值累加作为更新的分母,起到平衡不同变量梯度数值差异过大的问题

cache += dx**2
x += -learning_rate*dx/(np.sqrt(cache)+1e-7)

RMSProp

在AdaGrad基础上加入了decay factor,防止历史梯度求和过大

cache = decay_rate*cache + (1-decay_rate)*dx**2
x += -learning_rate*dx/(np.sqrt(cache)+1e-7)

ADAM

初始版本:类似于加入动量的RMSProp

m = beta1*m + (1-beta1)*dx
v = beta2*v + (1-beta2)*(dx**2)
x += -learning_rate*m / (np.sqrt(v)+1e-7)

真实的更新算法如下:

m = beta1*m + (1-beta1)*dx
v = beta2*v + (1-beta2)*(dx**2)
mb = m/(1-beta1**t)   # t is step number
vb = v/(1-beta2**t)
x += -learning_rate*mb / (np.sqrt(vb)+1e-7)

mb和vb起到最开始的时候warm up作用,t很大之后(1-beta1**t) =1

实际经验

  • ADAM通常会取得比较好的结果,同时收敛非常快相比SGD
  • L-BFGS适用于全batch做优化的情况
  • 有时候可以多种优化方法同时使用,比如使用SGD进行warm up,然后ADAM
  • 对于比较奇怪的需求,deepbit两个loss的收敛需要进行控制的情况,比较慢的SGD比较适用

Caffe 不同优化算法参数

caffe的优化需要在solver.prototxt中指定相应的参数

type代表的是优化算法

比较坑的是不同的版本之间type会有变化(ADAM or Adam),需要看具体代码 
* Stochastic Gradient Descent (type: “SGD”), 
* AdaDelta (type: “AdaDelta”), 
* Adaptive Gradient (type: “AdaGrad”), 
* Adam (type: “Adam”), 
* Nesterov’s Accelerated Gradient (type: “Nesterov”) and 
* RMSprop (type: “RMSProp”)

SGD

base_lr: 0.01 
lr_policy: "step"    # 也可以使用指数,多项式等等
gamma: 0.1   
stepsize: 1000  
max_iter: 3500 
momentum: 0.9

AdaDelta

net: "examples/mnist/lenet_train_test.prototxt"
test_iter: 100
test_interval: 500
base_lr: 1.0
lr_policy: "fixed"
momentum: 0.95
weight_decay: 0.0005
display: 100
max_iter: 10000
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet_adadelta"
solver_mode: GPU
type: "AdaDelta"
delta: 1e-6

AdaGrad

net: "examples/mnist/mnist_autoencoder.prototxt"
test_state: { stage: 'test-on-train' }
test_iter: 500
test_state: { stage: 'test-on-test' }
test_iter: 100
test_interval: 500
test_compute_loss: true
base_lr: 0.01
lr_policy: "fixed"
display: 100
max_iter: 65000
weight_decay: 0.0005
snapshot: 10000
snapshot_prefix: "examples/mnist/mnist_autoencoder_adagrad_train"
# solver mode: CPU or GPU
solver_mode: GPU
type: "AdaGrad"

Nesterov

base_lr: 0.01
lr_policy: "step"
gamma: 0.1
weight_decay: 0.0005
momentum: 0.95
type: "Nesterov"

ADAM

train_net: "nin_train_val.prototxt"
base_lr: 0.001
###############
##### step:base_lr * gamma ^ (floor(iter / stepsize))
#lr_policy: "step"
#gamma: 0.1
#stepsize: 25000
##### multi-step:
#lr_policy: "multistep"
#gamma: 0.5
#stepvalue: 1000
#stepvalue: 2000
#stepvalue: 3000
#stepvalue: 4000
#stepvalue: 5000
#stepvalue: 10000
#stepvalue: 20000
###### inv:base_lr * (1 + gamma * iter) ^ (- power)
# lr_policy: "inv"
# gamma: 0.0001
# power: 2
##### exp:base_lr * gamma ^ iter
# lr_policy: "exp"
# gamma: 0.9
##### poly:base_lr (1 - iter/max_iter) ^ (power)
# lr_policy: "poly"
# power: 0.9
##### sigmoid:base_lr ( 1/(1 + exp(-gamma * (iter - stepsize))))
# lr_policy: "sigmoid"
# gamma: 0.9
#momentum: 0.9
solver_type: ADAM
momentum: 0.9
momentum2: 0.999
delta: 1e-8
lr_policy: "fixed"

display: 100
max_iter: 50000
weight_decay: 0.0005
snapshot: 5000
snapshot_prefix: "./stage1/sgd_DeepBit1024_alex_stage1"
solver_mode: GPU

RMSProp

net: "examples/mnist/lenet_train_test.prototxt"
test_iter: 100
test_interval: 500
base_lr: 1.0
lr_policy: "fixed"
momentum: 0.95
weight_decay: 0.0005
display: 100
max_iter: 10000
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet_adadelta"
solver_mode: GPU
type: "RMSProp"
rms_decay: 0.98
阅读更多

没有更多推荐了,返回首页