|
15 | 15 | namespace differentiation { |
16 | 16 | namespace reverse_mode { |
17 | 17 |
|
18 | | - /* autodiff variable of type RealType (numeric types), stores derivatives up to DerivativeOrder*/ |
| 18 | + /* autodiff variable of type RealType (numeric types), stores derivatives up to DerivativeOrder |
| 19 | + * rvar inherits from a generic expression base class |
| 20 | + * expression<RealType, DerivativeOrder, rvar<RealType, DerivativeOrder>> |
| 21 | + * This is a Curiously Recurring Template Pattern(CRTP) |
| 22 | + * The purpose is so that rvar acts as a terminal node in an expression graph, and can be combined |
| 23 | + * with other expression-based types (sums, producs, etc..) to form expression graphs. |
| 24 | + */ |
19 | 25 | template<typename RealType, size_t DerivativeOrder = 1> |
20 | 26 | class rvar : public expression<RealType, DerivativeOrder, rvar<RealType, DerivativeOrder>> { |
21 | 27 | // inner_t of rvar<RealType, N> = var<RealType, N-1> |
|
60 | 66 | } |
61 | 67 |
|
62 | 68 | // gradient tape holds the computational graph |
| 69 | + /* |
| 70 | + * The expression graph is stored on a tape. The tape is closely related to the memory manegement |
| 71 | + * system in the library. BOOST_MATH_BUFFER_SIZE is set to 65536. It controls the block size of the |
| 72 | + * internal memory arena. Its a macro, and can be set at compile time. |
| 73 | + */ |
63 | 74 | template<typename RealType, size_t DerivativeOrder, size_t buffer_size = BOOST_MATH_BUFFER_SIZE> |
64 | 75 | class gradient_tape { |
65 | 76 |
|
@@ -316,7 +327,17 @@ The model and loss functions are called. This is just to initialize y_fit and lo |
316 | 327 | // 5. Gradient Descent Loop |
317 | 328 | double learning_rate = 1e-3; |
318 | 329 |
|
319 | | -The learning rate is a tunable parameter determining the "velocity" with which we descend to a solution. |
| 330 | +The learning rate controls how large a step we take in the direction of the |
| 331 | +negative gradient each iteration. Intuitively, it sets the "velocity" of |
| 332 | +descent toward a minimum. |
| 333 | + |
| 334 | +Too high: the optimization may overshoot minima, oscillate, or even diverge. |
| 335 | +Too low: convergence will be very slow and may stall in shallow regions. |
| 336 | + |
| 337 | +In practice, values in the range [1e-4, 1e-1] are common starting points, |
| 338 | +with 1e-3 being a typical safe default for many problems. The best choice |
| 339 | +depends on the scale of the data, the model, and the curvature of the loss |
| 340 | +landscape. |
320 | 341 |
|
321 | 342 | while (loss_v > 0.005) { |
322 | 343 | tape.zero_grad(); // zero out all the adjoints |
|
0 commit comments