Solver

The solver orchestrates model optimization by coordinating the network’s forward inference and backward gradients to form parameter updates that attempt to improve the loss.The responsibilities of learning are divided between the Solver for overseeing the optimization and generating parameter updates and the Net for yielding loss and gradients.

The Caffe solvers are:

  • 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")

The solver

  • scaffolds the optimization bookkeeping and creates the training network for learning and test network(s) for evaluation.
  • iteratively optimizes by calling forward / backward and updating parameters
  • (periodically) evaluates the test networks
  • snapshots the model and solver state throughout the optimization
    where each iteration

  • calls network forward to compute the output and loss

  • calls network backward to compute the gradients
  • incorporates the gradients into parameter updates according to the solver method
  • updates the solver state according to learning rate, history, and method
    to take the weights all the way from initialization to learned model.

Like Caffe models, Caffe solvers run in CPU / GPU modes.

Methods

The solver methods address the general optimization problem of loss minimization.For dataset , the optimization objective is the average loss over all data instances throughout the dataset

where is the loss on data instance and is a regularization term with weight . can be very large, so in practice, in each solver iteration we use a stochastic approximation of this objective, drawing a mini-batch of instances:

The model computes in the forward pass and the gradient in the backward pass.

The parameter update is formed by the solver from the error gradient , the regularization gradient , and other particulars to each method.

SGD

Stochastic gradient descent (type: "SGD") updates the weights by a linear combination of the negative gradient and the previous weight update .The learning rate is the weight of the negative gradient.The momentum is the weight of the previous update.

Formally, we have the following formulas to compute the update value and the updated weights at iteration , given the previous weight update and current weights :

The learning “hyperparameters” ( and ) might require a bit of tuning for best results.If you’re not sure where to start, take a look at the “Rules of thumb” below, and for further information you might refer to Leon Bottou’s Stochastic Gradient Descent Tricks [1].

[1] L. Bottou. Stochastic Gradient Descent Tricks. Neural Networks: Tricks of the Trade: Springer, 2012.

Rules of thumb for setting the learning rate \alpha and momentum \mu

A good strategy for deep learning with SGD is to initialize the learning rate to a value around , and dropping it by a constant factor (e.g., 10) throughout training when the loss begins to reach an apparent “plateau”, repeating this several times.Generally, you probably want to use a momentum or similar value.By smoothing the weight updates across iterations, momentum tends to make deep learning with SGD both stabler and faster.

This was the strategy used by Krizhevsky et al. [1] in their famously winning CNN entry to the ILSVRC-2012 competition, and Caffe makes this strategy easy to implement in a SolverParameter, as in our reproduction of [1] at ./examples/imagenet/alexnet_solver.prototxt.

To use a learning rate policy like this, you can put the following lines somewhere in your solver prototxt file:

  1. base_lr: 0.01 # begin training at a learning rate of 0.01 = 1e-2
  2. lr_policy: "step" # learning rate policy: drop the learning rate in "steps"
  3. # by a factor of gamma every stepsize iterations
  4. gamma: 0.1 # drop the learning rate by a factor of 10
  5. # (i.e., multiply it by a factor of gamma = 0.1)
  6. stepsize: 100000 # drop the learning rate every 100K iterations
  7. max_iter: 350000 # train for 350K iterations total
  8. momentum: 0.9

Under the above settings, we’ll always use momentum .We’ll begin training at a base_lr of for the first 100,000 iterations, then multiply the learning rate by gamma () and train at for iterations 100K-200K, then at for iterations 200K-300K, and finally train until iteration 350K (since we have max_iter: 350000) at .

Note that the momentum setting effectively multiplies the size of your updates by a factor of after many iterations of training, so if you increase , it may be a good idea to decrease accordingly (and vice versa).

For example, with , we have an effective update size multiplier of .If we increased the momentum to , we’ve increased our update size multiplier to 100, so we should drop (base_lr) by a factor of 10.

Note also that the above settings are merely guidelines, and they’re definitely not guaranteed to be optimal (or even work at all!) in every situation.If learning diverges (e.g., you start to see very large or NaN or inf loss values or outputs), try dropping the base_lr (e.g., base_lr: 0.001) and re-training, repeating this until you find a base_lr value that works.

[1] A. Krizhevsky, I. Sutskever, and G. Hinton. ImageNet Classification with Deep Convolutional Neural Networks. Advances in Neural Information Processing Systems, 2012.

AdaDelta

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

and

[1] M. Zeiler ADADELTA: AN ADAPTIVE LEARNING RATE METHOD. arXiv preprint, 2012.

AdaGrad

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.Given the update information from all previous iterations for ,the update formulas proposed by [1] are as follows, specified for each component of the weights :

Note that in practice, for weights , AdaGrad implementations (including the one in Caffe) use only extra storage for the historical gradient information (rather than the storage that would be necessary to store each historical gradient individually).

[1] J. Duchi, E. Hazan, and Y. Singer. Adaptive Subgradient Methods for Online Learning and Stochastic Optimization. The Journal of Machine Learning Research, 2011.

Adam

The Adam (type: "Adam"), proposed in Kingma et al. [1], is a gradient-based optimization method (like SGD). This includes an “adaptive moment estimation” () and can be regarded as a generalization of AdaGrad. The update formulas are

and

Kingma et al. [1] proposed to use as default values. Caffe uses the values of momemtum, momentum2, delta for , respectively.

[1] D. Kingma, J. Ba. Adam: A Method for Stochastic Optimization. International Conference for Learning Representations, 2015.

NAG

Nesterov’s accelerated gradient (type: "Nesterov") was proposed by Nesterov [1] as an “optimal” method of convex optimization, achieving a convergence rate of rather than the .Though the required assumptions to achieve the 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].

The weight update formulas look very similar to the SGD updates given above:

What distinguishes the method from SGD is the weight setting on which we compute the error gradient – in NAG we take the gradient on weights with added momentum ; in SGD we simply take the gradient on the current weights themselves.

[1] Y. Nesterov. A Method of Solving a Convex Programming Problem with Convergence Rate . Soviet Mathematics Doklady, 1983.

[2] I. Sutskever, J. Martens, G. Dahl, and G. Hinton. On the Importance of Initialization and Momentum in Deep Learning. Proceedings of the 30th International Conference on Machine Learning, 2013.

RMSprop

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

The default value of (rms_decay) is set to .

[1] T. Tieleman, and G. Hinton. RMSProp: Divide the gradient by a running average of its recent magnitude. COURSERA: Neural Networks for Machine Learning.Technical report, 2012.

Scaffolding

The solver scaffolding prepares the optimization method and initializes the model to be learned in Solver::Presolve().

  1. > caffe train -solver examples/mnist/lenet_solver.prototxt
  2. I0902 13:35:56.474978 16020 caffe.cpp:90] Starting Optimization
  3. I0902 13:35:56.475190 16020 solver.cpp:32] Initializing solver from parameters:
  4. test_iter: 100
  5. test_interval: 500
  6. base_lr: 0.01
  7. display: 100
  8. max_iter: 10000
  9. lr_policy: "inv"
  10. gamma: 0.0001
  11. power: 0.75
  12. momentum: 0.9
  13. weight_decay: 0.0005
  14. snapshot: 5000
  15. snapshot_prefix: "examples/mnist/lenet"
  16. solver_mode: GPU
  17. net: "examples/mnist/lenet_train_test.prototxt"

Net initialization

  1. I0902 13:35:56.655681 16020 solver.cpp:72] Creating training net from net file: examples/mnist/lenet_train_test.prototxt
  2. [...]
  3. I0902 13:35:56.656740 16020 net.cpp:56] Memory required for data: 0
  4. I0902 13:35:56.656791 16020 net.cpp:67] Creating Layer mnist
  5. I0902 13:35:56.656811 16020 net.cpp:356] mnist -> data
  6. I0902 13:35:56.656846 16020 net.cpp:356] mnist -> label
  7. I0902 13:35:56.656874 16020 net.cpp:96] Setting up mnist
  8. I0902 13:35:56.694052 16020 data_layer.cpp:135] Opening lmdb examples/mnist/mnist_train_lmdb
  9. I0902 13:35:56.701062 16020 data_layer.cpp:195] output data size: 64,1,28,28
  10. I0902 13:35:56.701146 16020 data_layer.cpp:236] Initializing prefetch
  11. I0902 13:35:56.701196 16020 data_layer.cpp:238] Prefetch initialized.
  12. I0902 13:35:56.701212 16020 net.cpp:103] Top shape: 64 1 28 28 (50176)
  13. I0902 13:35:56.701230 16020 net.cpp:103] Top shape: 64 1 1 1 (64)
  14. [...]
  15. I0902 13:35:56.703737 16020 net.cpp:67] Creating Layer ip1
  16. I0902 13:35:56.703753 16020 net.cpp:394] ip1 <- pool2
  17. I0902 13:35:56.703778 16020 net.cpp:356] ip1 -> ip1
  18. I0902 13:35:56.703797 16020 net.cpp:96] Setting up ip1
  19. I0902 13:35:56.728127 16020 net.cpp:103] Top shape: 64 500 1 1 (32000)
  20. I0902 13:35:56.728142 16020 net.cpp:113] Memory required for data: 5039360
  21. I0902 13:35:56.728175 16020 net.cpp:67] Creating Layer relu1
  22. I0902 13:35:56.728194 16020 net.cpp:394] relu1 <- ip1
  23. I0902 13:35:56.728219 16020 net.cpp:345] relu1 -> ip1 (in-place)
  24. I0902 13:35:56.728240 16020 net.cpp:96] Setting up relu1
  25. I0902 13:35:56.728256 16020 net.cpp:103] Top shape: 64 500 1 1 (32000)
  26. I0902 13:35:56.728270 16020 net.cpp:113] Memory required for data: 5167360
  27. I0902 13:35:56.728287 16020 net.cpp:67] Creating Layer ip2
  28. I0902 13:35:56.728304 16020 net.cpp:394] ip2 <- ip1
  29. I0902 13:35:56.728333 16020 net.cpp:356] ip2 -> ip2
  30. I0902 13:35:56.728356 16020 net.cpp:96] Setting up ip2
  31. I0902 13:35:56.728690 16020 net.cpp:103] Top shape: 64 10 1 1 (640)
  32. I0902 13:35:56.728705 16020 net.cpp:113] Memory required for data: 5169920
  33. I0902 13:35:56.728734 16020 net.cpp:67] Creating Layer loss
  34. I0902 13:35:56.728747 16020 net.cpp:394] loss <- ip2
  35. I0902 13:35:56.728767 16020 net.cpp:394] loss <- label
  36. I0902 13:35:56.728786 16020 net.cpp:356] loss -> loss
  37. I0902 13:35:56.728811 16020 net.cpp:96] Setting up loss
  38. I0902 13:35:56.728837 16020 net.cpp:103] Top shape: 1 1 1 1 (1)
  39. I0902 13:35:56.728849 16020 net.cpp:109] with loss weight 1
  40. I0902 13:35:56.728878 16020 net.cpp:113] Memory required for data: 5169924

Loss

  1. I0902 13:35:56.728893 16020 net.cpp:170] loss needs backward computation.
  2. I0902 13:35:56.728909 16020 net.cpp:170] ip2 needs backward computation.
  3. I0902 13:35:56.728924 16020 net.cpp:170] relu1 needs backward computation.
  4. I0902 13:35:56.728938 16020 net.cpp:170] ip1 needs backward computation.
  5. I0902 13:35:56.728953 16020 net.cpp:170] pool2 needs backward computation.
  6. I0902 13:35:56.728970 16020 net.cpp:170] conv2 needs backward computation.
  7. I0902 13:35:56.728984 16020 net.cpp:170] pool1 needs backward computation.
  8. I0902 13:35:56.728998 16020 net.cpp:170] conv1 needs backward computation.
  9. I0902 13:35:56.729014 16020 net.cpp:172] mnist does not need backward computation.
  10. I0902 13:35:56.729027 16020 net.cpp:208] This network produces output loss
  11. I0902 13:35:56.729053 16020 net.cpp:467] Collecting Learning Rate and Weight Decay.
  12. I0902 13:35:56.729071 16020 net.cpp:219] Network initialization done.
  13. I0902 13:35:56.729085 16020 net.cpp:220] Memory required for data: 5169924
  14. I0902 13:35:56.729277 16020 solver.cpp:156] Creating test net (#0) specified by net file: examples/mnist/lenet_train_test.prototxt

Completion

  1. I0902 13:35:56.806970 16020 solver.cpp:46] Solver scaffolding done.
  2. I0902 13:35:56.806984 16020 solver.cpp:165] Solving LeNet

Updating Parameters

The actual weight update is made by the solver then applied to the net parameters in Solver::ComputeUpdateValue().The ComputeUpdateValue method incorporates any weight decay into the weight gradients (which currently just contain the error gradients) to get the final gradient with respect to each network weight.Then these gradients are scaled by the learning rate and the update to subtract is stored in each parameter Blob’s diff field.Finally, the Blob::Update method is called on each parameter blob, which performs the final update (subtracting the Blob’s diff from its data).

Snapshotting and Resuming

The solver snapshots the weights and its own state during training in Solver::Snapshot() and Solver::SnapshotSolverState().The weight snapshots export the learned model while the solver snapshots allow training to be resumed from a given point.Training is resumed by Solver::Restore() and Solver::RestoreSolverState().

Weights are saved without extension while solver states are saved with .solverstate extension.Both files will have an _iter_N suffix for the snapshot iteration number.

Snapshotting is configured by:

  1. # The snapshot interval in iterations.
  2. snapshot: 5000
  3. # File path prefix for snapshotting model weights and solver state.
  4. # Note: this is relative to the invocation of the `caffe` utility, not the
  5. # solver definition file.
  6. snapshot_prefix: "/path/to/model"
  7. # Snapshot the diff along with the weights. This can help debugging training
  8. # but takes more storage.
  9. snapshot_diff: false
  10. # A final snapshot is saved at the end of training unless
  11. # this flag is set to false. The default is true.
  12. snapshot_after_train: true

in the solver definition prototxt.