forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learning_rate_op.cc
146 lines (137 loc) · 6.48 KB
/
learning_rate_op.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "caffe2/sgd/learning_rate_op.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(LearningRate, LearningRateOp<float, CPUContext>);
OPERATOR_SCHEMA(LearningRate)
.NumInputs(1)
.NumOutputs(1)
.TensorInferenceFunction([](const OperatorDef&,
const vector<TensorShape>& in) {
vector<TensorShape> out(1);
out[0] = in[0];
return out;
})
.SetDoc(R"DOC(
Learning rate is a decreasing function of time. With low learning rates the
improvements will be linear. With high learning rates they will start to look
more exponential. Learning rate is controlled by the following arguments:
Required:
`iterations`
`base_lr`: base learning rate
`policy`: this controls how the learning rate is applied, options are:
`fixed`
`step`: uses `stepsize`, `gamma`
`exp`: uses `gamma`
`gate`: uses 'multiplier_1', 'multiplier_2', `num_iter``
`inv`: uses `gamma`, `power`
`linearWarmup`: uses `start_multiplier`, `num_iter`
`constantWarmup`: uses `multiplier`, `num_iter`
`alter`: uses `active_first`, `active_period`, `inactive_period`
`hill`: uses those in both `linearWarmup` and `inv`, plus `end_multiplier`
`composite`: uses `sub_policy_num_iters` and additional args with format
`cyclic`: uses `max_lr`, `stepsize`
`cosine`: uses `min_lr`, `max_lr`, `period`, `t_mult`, `lr_shrink`
`constantThenLinearWarmup`: uses `start_warmup_multiplier`, `constant_warmup_num_iter`, `linear_warmup_num_iter`
`compositeCyclical`: uses `start_warmup_multiplier`, `constant_warmup_num_iter`, `linear_warmup_num_iter`, `cyclical_max_lr`, `cyclical_step_size`, `cyclical_decay`
`compositeCosine`: uses `start_warmup_multiplier`, `constant_warmup_num_iter`, `linear_warmup_num_iter`, `cosine_max_lr`, `cosine_period`, `cosine_t_mult`, `cosine_lr_shrink`
sub_policy_{sub_policy_index}_{sub_policy_arg}, for example:
sub_policy_0_policy: "exp", sub_policy_0_gamma: 0.99,
sub_policy_0_lr_scale: 1.2
sub_policy_0_policy: "fixed", sub_policy_0_lr_scale: 1.0
sub_policy_num_iters: [1000, 1000]
Optional:
`stepsize`: defaults to 0
`max_lr`: defaults to 0.005
`gamma`: defaults to 0
`power`: defaults to 0
`num_iter`: defaults to 0
`start_multiplier`: defaults to 0
`multiplier`: defaults to 0.5
`multiplier_1`: defaults to 1
`multiplier_2`: defaults to 1
`m1`: defaults to 0.5, the first piece lr of piece warmup
`n1`: defaults to 0, iter threshold of the first piece lr
`m2`: defaults to 0.5, the second piece lr of piece warmup
`n2`: defaults to 0, iter threshold of the second piece lr
`m3`: defaults to 0.5, the third piece lr of piece warmup
`start_warmup_multiplier`: defaults to 0.1, part of constantThenLinearWarmup
`constant_warmup_num_iter`: defaults to 10000000, part of constantThenLinearWarmup and constantThenLinearWarmup
`linear_warmup_num_iter`: defaults to 10000000, part of constantThenLinearWarmup, CompositeCyclicalLRPolicy, CompositeCosineLRPolicy
`cyclical_max_lr`: defaults to 0.05, part of CompositeCyclicalLRPolicy
`cyclical_step_size`: defaults to 1000000, part of CompositeCyclicalLRPolicy
`cyclical_decay`: defaults to 1.0, part of CompositeCyclicalLRPolicy
`cosine_min_lr`:defaults to 0.01, part of CompositeCosineLRPolicy
`cosine_max_lr`:defaults to 0.05, part of CompositeCosineLRPolicy
`cosine_period`:defaults to 50, part of CompositeCosineLRPolicy
`cosine_t_mult`:defaults to 1.0, part of CompositeCosineLRPolicy
`cosine_lr_shrink`:defaults to 0.99, part of CompositeCosineLRPolicy
Usage:
train_net.LearningRate(*iterations*, "*label*", base_lr=*float*,
policy="policy_name", stepsize=*int*, gamma=*float*)
Example usage:
train_net.LearningRate(200, "LR", base_lr=-0.1,
policy="step", stepsize=20, gamma=0.9)
)DOC")
.Arg("base_lr", "(float, required) base learning rate")
.Arg("policy", "(float, default 1.0) strategy for gamma enforcement")
.Arg("power", "(float, default 1.0) used only for inv policy type")
.Arg("gamma", "(float, default 1.0) momentum of change")
.Arg("stepsize", "(float, default 1.0) sampling rate on iterations")
.Arg("max_lr", "(float, default 0.005) max learning rate")
.Arg("active_first", "(boolean, default True) in alter policy")
.Arg("active_period", "(int64_t, required) in alter policy")
.Arg("inactive_period", "(int64_t, required) in alter policy")
.Arg(
"max_iter",
"(int, default -1) maximum iterations in this training run")
.Arg(
"num_iter",
"(int, default 0) number of iterations over which to warmup lr")
.Arg(
"start_multiplier",
"(float, default 0) starting multiplier for learning rate")
.Arg(
"end_multiplier",
"(float, default 0) end multiplier for learning rate")
.Arg(
"multiplier",
"(float, default 0.5) constant multiplier for learning rate")
.Arg(
"multiplier_1",
"(float, default 1) start multiplier for learning rate")
.Arg("multiplier_2", "(float, default 1) end multiplier for learning rate")
.Arg(
"sub_policy_num_iters",
"(int array, default empty) number of iterations for each sub learning rate policy in composite policy")
.Arg("m1", "")
.Arg("n1", "")
.Arg("m2", "")
.Arg("n2", "")
.Arg("m3", "")
.Arg("start_warmup_multiplier", "defaults to 0.1")
.Arg("constant_warmup_num_iter", "defaults to 10000000")
.Arg("linear_warmup_num_iter", "defaults to 10000000")
.Arg(
"cyclical_max_lr",
"defaults to 0.05, part of CompositeCyclicalLRPolicy")
.Arg(
"cyclical_step_size",
"defaults to 1000000, part of CompositeCyclicalLRPolicy")
.Arg(
"cyclical_decay",
"defaults to 0.999, part of CompositeCyclicalLRPolicy")
.Arg("cosine_min_lr", "defaults to 0.01, part of CompositeCosineLRPolicy")
.Arg("cosine_max_lr", "defaults to 0.05, part of CompositeCosineLRPolicy")
.Arg("cosine_period", "defaults to 50, part of CompositeCosineLRPolicy")
.Arg("cosine_t_mult", "defaults to 1,0, part of CompositeCosineLRPolicy")
.Arg(
"cosine_lr_shrink",
"defaults to 0.99, part of CompositeCosineLRPolicy")
.Input(0, "input", "description needed")
.Output(0, "output", "description needed")
.DeviceInferenceFunction([](const OperatorDef& def) {
return std::make_pair(
std::vector<DeviceOption>{DeviceOption()},
std::vector<DeviceOption>{def.device_option()});
});
NO_GRADIENT(LearningRate);
} // namespace caffe2