Skip to content

Commit d1e18af

Browse files
committed
Add automatic upgrade for solver type and update examples and doc
1 parent 3215b3b commit d1e18af

12 files changed

+213
-21
lines changed

docs/tutorial/solver.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ The responsibilities of learning are divided between the Solver for overseeing t
88

99
The Caffe solvers are:
1010

11-
- Stochastic Gradient Descent (`SGD`),
12-
- AdaDelta (`ADADELTA`),
13-
- Adaptive Gradient (`ADAGRAD`),
14-
- Adam (`ADAM`),
15-
- Nesterov's Accelerated Gradient (`NESTEROV`) and
16-
- RMSprop (`RMSPROP`)
11+
- Stochastic Gradient Descent (`type: 'SGD'`),
12+
- AdaDelta (`type: 'AdaDelta'`),
13+
- Adaptive Gradient (`type: 'AdaGrad'`),
14+
- Adam (`type: 'Adam'`),
15+
- Nesterov's Accelerated Gradient (`type: 'Nesterov'`) and
16+
- RMSprop (`type: 'RMSProp'`)
1717

1818
The solver
1919

@@ -51,7 +51,7 @@ The parameter update $$\Delta W$$ is formed by the solver from the error gradien
5151

5252
### SGD
5353

54-
**Stochastic gradient descent** (`solver_type: SGD`) updates the weights $$ W $$ by a linear combination of the negative gradient $$ \nabla L(W) $$ and the previous weight update $$ V_t $$.
54+
**Stochastic gradient descent** (`type: "SGD"`) updates the weights $$ W $$ by a linear combination of the negative gradient $$ \nabla L(W) $$ and the previous weight update $$ V_t $$.
5555
The **learning rate** $$ \alpha $$ is the weight of the negative gradient.
5656
The **momentum** $$ \mu $$ is the weight of the previous update.
5757

@@ -113,7 +113,7 @@ If learning diverges (e.g., you start to see very large or `NaN` or `inf` loss v
113113

114114
### AdaDelta
115115

116-
The **AdaDelta** (`solver_type: ADADELTA`) method (M. Zeiler [1]) is a "robust learning rate method". It is a gradient-based optimization method (like SGD). The update formulas are
116+
The **AdaDelta** (`type: "AdaDelta"`) method (M. Zeiler [1]) is a "robust learning rate method". It is a gradient-based optimization method (like SGD). The update formulas are
117117

118118
$$
119119
\begin{align}
@@ -125,7 +125,7 @@ E[g^2]_t &= \delta{E[g^2]_{t-1} } + (1-\delta)g_{t}^2
125125
\end{align}
126126
$$
127127

128-
and
128+
and
129129

130130
$$
131131
(W_{t+1})_i =
@@ -139,7 +139,7 @@ $$
139139

140140
### AdaGrad
141141

142-
The **adaptive gradient** (`solver_type: ADAGRAD`) method (Duchi et al. [1]) is a gradient-based optimization method (like SGD) that attempts to "find needles in haystacks in the form of very predictive but rarely seen features," in Duchi et al.'s words.
142+
The **adaptive gradient** (`type: "AdaGrad"`) method (Duchi et al. [1]) is a gradient-based optimization method (like SGD) that attempts to "find needles in haystacks in the form of very predictive but rarely seen features," in Duchi et al.'s words.
143143
Given the update information from all previous iterations $$ \left( \nabla L(W) \right)_{t'} $$ for $$ t' \in \{1, 2, ..., t\} $$,
144144
the update formulas proposed by [1] are as follows, specified for each component $$i$$ of the weights $$W$$:
145145

@@ -159,7 +159,7 @@ Note that in practice, for weights $$ W \in \mathcal{R}^d $$, AdaGrad implementa
159159

160160
### Adam
161161

162-
The **Adam** (`solver_type: ADAM`), proposed in Kingma et al. [1], is a gradient-based optimization method (like SGD). This includes an "adaptive moment estimation" ($$m_t, v_t$$) and can be regarded as a generalization of AdaGrad. The update formulas are
162+
The **Adam** (`type: "Adam"`), proposed in Kingma et al. [1], is a gradient-based optimization method (like SGD). This includes an "adaptive moment estimation" ($$m_t, v_t$$) and can be regarded as a generalization of AdaGrad. The update formulas are
163163

164164
$$
165165
(m_t)_i = \beta_1 (m_{t-1})_i + (1-\beta_1)(\nabla L(W_t))_i,\\
@@ -181,7 +181,7 @@ Kingma et al. [1] proposed to use $$\beta_1 = 0.9, \beta_2 = 0.999, \varepsilon
181181

182182
### NAG
183183

184-
**Nesterov's accelerated gradient** (`solver_type: NESTEROV`) was proposed by Nesterov [1] as an "optimal" method of convex optimization, achieving a convergence rate of $$ \mathcal{O}(1/t^2) $$ rather than the $$ \mathcal{O}(1/t) $$.
184+
**Nesterov's accelerated gradient** (`type: "Nesterov"`) was proposed by Nesterov [1] as an "optimal" method of convex optimization, achieving a convergence rate of $$ \mathcal{O}(1/t^2) $$ rather than the $$ \mathcal{O}(1/t) $$.
185185
Though the required assumptions to achieve the $$ \mathcal{O}(1/t^2) $$ convergence typically will not hold for deep networks trained with Caffe (e.g., due to non-smoothness and non-convexity), in practice NAG can be a very effective method for optimizing certain types of deep learning architectures, as demonstrated for deep MNIST autoencoders by Sutskever et al. [2].
186186

187187
The weight update formulas look very similar to the SGD updates given above:
@@ -206,10 +206,10 @@ What distinguishes the method from SGD is the weight setting $$ W $$ on which we
206206

207207
### RMSprop
208208

209-
The **RMSprop** (`solver_type: RMSPROP`), suggested by Tieleman in a Coursera course lecture, is a gradient-based optimization method (like SGD). The update formulas are
209+
The **RMSprop** (`type: "RMSProp"`), suggested by Tieleman in a Coursera course lecture, is a gradient-based optimization method (like SGD). The update formulas are
210210

211211
$$
212-
(v_t)_i =
212+
(v_t)_i =
213213
\begin{cases}
214214
(v_{t-1})_i + \delta, &(\nabla L(W_t))_i(\nabla L(W_{t-1}))_i > 0\\
215215
(v_{t-1})_i \cdot (1-\delta), & \text{else}

examples/mnist/lenet_adadelta_solver.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ snapshot: 5000
2020
snapshot_prefix: "examples/mnist/lenet_adadelta"
2121
# solver mode: CPU or GPU
2222
solver_mode: GPU
23-
solver_type: ADADELTA
23+
type: "AdaDelta"
2424
delta: 1e-6

examples/mnist/lenet_solver_adam.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ max_iter: 10000
2222
snapshot: 5000
2323
snapshot_prefix: "examples/mnist/lenet"
2424
# solver mode: CPU or GPU
25-
solver_type: ADAM
25+
type: "Adam"
2626
solver_mode: GPU

examples/mnist/lenet_solver_rmsprop.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ snapshot: 5000
2323
snapshot_prefix: "examples/mnist/lenet_rmsprop"
2424
# solver mode: CPU or GPU
2525
solver_mode: GPU
26-
solver_type: RMSPROP
26+
type: "RMSProp"
2727
rms_decay: 0.98

examples/mnist/mnist_autoencoder_solver_adadelta.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ snapshot: 10000
1616
snapshot_prefix: "examples/mnist/mnist_autoencoder_adadelta_train"
1717
# solver mode: CPU or GPU
1818
solver_mode: GPU
19-
solver_type: ADADELTA
19+
type: "AdaDelta"

examples/mnist/mnist_autoencoder_solver_adagrad.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ snapshot: 10000
1414
snapshot_prefix: "examples/mnist/mnist_autoencoder_adagrad_train"
1515
# solver mode: CPU or GPU
1616
solver_mode: GPU
17-
solver_type: ADAGRAD
17+
type: "AdaGrad"

examples/mnist/mnist_autoencoder_solver_nesterov.prototxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ snapshot_prefix: "examples/mnist/mnist_autoencoder_nesterov_train"
1717
momentum: 0.95
1818
# solver mode: CPU or GPU
1919
solver_mode: GPU
20-
solver_type: NESTEROV
20+
type: "Nesterov"

include/caffe/util/upgrade_proto.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ void ReadNetParamsFromTextFileOrDie(const string& param_file,
5959
void ReadNetParamsFromBinaryFileOrDie(const string& param_file,
6060
NetParameter* param);
6161

62+
// Return true iff the solver contains any old solver_type specified as enums
63+
bool SolverNeedsTypeUpgrade(const SolverParameter& solver_param);
64+
65+
bool UpgradeSolverType(SolverParameter* solver_param);
66+
67+
// Check for deprecations and upgrade the SolverParameter as needed.
68+
bool UpgradeSolverAsNeeded(const string& param_file, SolverParameter* param);
69+
70+
// Read parameters from a file into a SolverParameter proto message.
71+
void ReadSolverParamsFromTextFileOrDie(const string& param_file,
72+
SolverParameter* param);
73+
6274
} // namespace caffe
6375

6476
#endif // CAFFE_UTIL_UPGRADE_PROTO_H_

src/caffe/solver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Solver<Dtype>::Solver(const string& param_file, const Solver* root_solver)
3636
: net_(), callbacks_(), root_solver_(root_solver),
3737
requested_early_exit_(false) {
3838
SolverParameter param;
39-
ReadProtoFromTextFileOrDie(param_file, &param);
39+
ReadSolverParamsFromTextFileOrDie(param_file, &param);
4040
Init(param);
4141
}
4242

src/caffe/test/test_upgrade_proto.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,4 +2928,65 @@ TEST_F(NetUpgradeTest, TestUpgradeV1LayerType) {
29282928
}
29292929
}
29302930
#endif // USE_OPENCV
2931+
2932+
class SolverTypeUpgradeTest : public ::testing::Test {
2933+
protected:
2934+
void RunSolverTypeUpgradeTest(
2935+
const string& input_param_string, const string& output_param_string) {
2936+
// Test upgrading old solver_type field (enum) to new type field (string)
2937+
SolverParameter input_param;
2938+
CHECK(google::protobuf::TextFormat::ParseFromString(
2939+
input_param_string, &input_param));
2940+
SolverParameter expected_output_param;
2941+
CHECK(google::protobuf::TextFormat::ParseFromString(
2942+
output_param_string, &expected_output_param));
2943+
SolverParameter actual_output_param = input_param;
2944+
UpgradeSolverType(&actual_output_param);
2945+
EXPECT_EQ(expected_output_param.DebugString(),
2946+
actual_output_param.DebugString());
2947+
}
2948+
};
2949+
2950+
TEST_F(SolverTypeUpgradeTest, TestSimple) {
2951+
const char* old_type_vec[6] = { "SGD", "ADAGRAD", "NESTEROV", "RMSPROP",
2952+
"ADADELTA", "ADAM" };
2953+
const char* new_type_vec[6] = { "SGD", "AdaGrad", "Nesterov", "RMSProp",
2954+
"AdaDelta", "Adam" };
2955+
for (int i = 0; i < 6; ++i) {
2956+
const string& input_proto =
2957+
"net: 'examples/mnist/lenet_train_test.prototxt' "
2958+
"test_iter: 100 "
2959+
"test_interval: 500 "
2960+
"base_lr: 0.01 "
2961+
"momentum: 0.0 "
2962+
"weight_decay: 0.0005 "
2963+
"lr_policy: 'inv' "
2964+
"gamma: 0.0001 "
2965+
"power: 0.75 "
2966+
"display: 100 "
2967+
"max_iter: 10000 "
2968+
"snapshot: 5000 "
2969+
"snapshot_prefix: 'examples/mnist/lenet_rmsprop' "
2970+
"solver_mode: GPU "
2971+
"solver_type: " + std::string(old_type_vec[i]) + " ";
2972+
const string& expected_output_proto =
2973+
"net: 'examples/mnist/lenet_train_test.prototxt' "
2974+
"test_iter: 100 "
2975+
"test_interval: 500 "
2976+
"base_lr: 0.01 "
2977+
"momentum: 0.0 "
2978+
"weight_decay: 0.0005 "
2979+
"lr_policy: 'inv' "
2980+
"gamma: 0.0001 "
2981+
"power: 0.75 "
2982+
"display: 100 "
2983+
"max_iter: 10000 "
2984+
"snapshot: 5000 "
2985+
"snapshot_prefix: 'examples/mnist/lenet_rmsprop' "
2986+
"solver_mode: GPU "
2987+
"type: '" + std::string(new_type_vec[i]) + "' ";
2988+
this->RunSolverTypeUpgradeTest(input_proto, expected_output_proto);
2989+
}
2990+
}
2991+
29312992
} // NOLINT(readability/fn_size) // namespace caffe

src/caffe/util/upgrade_proto.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,4 +937,73 @@ void ReadNetParamsFromBinaryFileOrDie(const string& param_file,
937937
UpgradeNetAsNeeded(param_file, param);
938938
}
939939

940+
// Return true iff the solver contains any old solver_type specified as enums
941+
bool SolverNeedsTypeUpgrade(const SolverParameter& solver_param) {
942+
if (solver_param.has_solver_type()) {
943+
return true;
944+
}
945+
return false;
946+
}
947+
948+
bool UpgradeSolverType(SolverParameter* solver_param) {
949+
CHECK(!solver_param->has_solver_type() || !solver_param->has_type())
950+
<< "Failed to upgrade solver: old solver_type field (enum) and new type "
951+
<< "field (string) cannot be both specified in solver proto text.";
952+
string type;
953+
switch (solver_param->solver_type()) {
954+
case SolverParameter_SolverType_SGD:
955+
type = "SGD";
956+
break;
957+
case SolverParameter_SolverType_NESTEROV:
958+
type = "Nesterov";
959+
break;
960+
case SolverParameter_SolverType_ADAGRAD:
961+
type = "AdaGrad";
962+
break;
963+
case SolverParameter_SolverType_RMSPROP:
964+
type = "RMSProp";
965+
break;
966+
case SolverParameter_SolverType_ADADELTA:
967+
type = "AdaDelta";
968+
break;
969+
case SolverParameter_SolverType_ADAM:
970+
type = "Adam";
971+
break;
972+
default:
973+
LOG(FATAL) << "Unknown SolverParameter solver_type: " << type;
974+
}
975+
solver_param->set_type(type);
976+
solver_param->clear_solver_type();
977+
return true;
978+
}
979+
980+
// Check for deprecations and upgrade the SolverParameter as needed.
981+
bool UpgradeSolverAsNeeded(const string& param_file, SolverParameter* param) {
982+
bool success = true;
983+
// Try to upgrade old style solver_type enum fields into new string type
984+
if (SolverNeedsTypeUpgrade(*param)) {
985+
LOG(INFO) << "Attempting to upgrade input file specified using deprecated "
986+
<< "'solver_type' field (enum)': " << param_file;
987+
if (!UpgradeSolverType(param)) {
988+
success = false;
989+
LOG(ERROR) << "Warning: had one or more problems upgrading "
990+
<< "SolverType (see above).";
991+
} else {
992+
LOG(INFO) << "Successfully upgraded file specified using deprecated "
993+
<< "'solver_type' field (enum) to 'type' field (string).";
994+
LOG(WARNING) << "Note that future Caffe releases will only support "
995+
<< "'type' field (string) for a solver's type.";
996+
}
997+
}
998+
return success;
999+
}
1000+
1001+
// Read parameters from a file into a SolverParameter proto message.
1002+
void ReadSolverParamsFromTextFileOrDie(const string& param_file,
1003+
SolverParameter* param) {
1004+
CHECK(ReadProtoFromTextFile(param_file, param))
1005+
<< "Failed to parse SolverParameter file: " << param_file;
1006+
UpgradeSolverAsNeeded(param_file, param);
1007+
}
1008+
9401009
} // namespace caffe

tools/upgrade_solver_proto_text.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This is a script to upgrade old solver prototxts to the new format.
2+
// Usage:
3+
// upgrade_solver_proto_text old_solver_proto_file_in solver_proto_file_out
4+
5+
#include <cstring>
6+
#include <fstream> // NOLINT(readability/streams)
7+
#include <iostream> // NOLINT(readability/streams)
8+
#include <string>
9+
10+
#include "caffe/caffe.hpp"
11+
#include "caffe/util/io.hpp"
12+
#include "caffe/util/upgrade_proto.hpp"
13+
14+
using std::ofstream;
15+
16+
using namespace caffe; // NOLINT(build/namespaces)
17+
18+
int main(int argc, char** argv) {
19+
::google::InitGoogleLogging(argv[0]);
20+
if (argc != 3) {
21+
LOG(ERROR) << "Usage: upgrade_solver_proto_text "
22+
<< "old_solver_proto_file_in solver_proto_file_out";
23+
return 1;
24+
}
25+
26+
SolverParameter solver_param;
27+
string input_filename(argv[1]);
28+
if (!ReadProtoFromTextFile(input_filename, &solver_param)) {
29+
LOG(ERROR) << "Failed to parse input text file as SolverParameter: "
30+
<< input_filename;
31+
return 2;
32+
}
33+
bool need_upgrade = SolverNeedsTypeUpgrade(solver_param);
34+
bool success = true;
35+
if (need_upgrade) {
36+
success = UpgradeSolverAsNeeded(input_filename, &solver_param);
37+
if (!success) {
38+
LOG(ERROR) << "Encountered error(s) while upgrading prototxt; "
39+
<< "see details above.";
40+
}
41+
} else {
42+
LOG(ERROR) << "File already in latest proto format: " << input_filename;
43+
}
44+
45+
// Save new format prototxt.
46+
WriteProtoToTextFile(solver_param, argv[2]);
47+
48+
LOG(ERROR) << "Wrote upgraded SolverParameter text proto to " << argv[2];
49+
return !success;
50+
}

0 commit comments

Comments
 (0)