Skip to content

Commit 2c24bf1

Browse files
committed
We now reclauclate the learning rate to be safe to continue training.
1 parent af67bcf commit 2c24bf1

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/neuralnetwork/neuralnetworkserializer.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <algorithm>
12
#include <chrono>
3+
#include <cmath>
24
#include <memory>
35

46
#include "./libraries/instrumentor.h"
@@ -46,7 +48,14 @@ NeuralNetwork* NeuralNetworkSerializer::load(const std::string& path)
4648
auto final_learning_rate = json_object->get_or<double>("final-learning-rate", 0.0);
4749
if (final_learning_rate != 0.0)
4850
{
49-
options.with_learning_rate(final_learning_rate);
51+
// 1. Calculate the new target as the geometric mean
52+
const auto updated_target = std::sqrt(final_learning_rate * options.learning_rate());
53+
options.with_learning_rate(updated_target);
54+
55+
// 2. Set warmup start to final_learning_rate to resume smoothly.
56+
// We use std::min to ensure warmup_start <= updated_target, avoiding the panic in build().
57+
const auto safe_warmup_start = std::min(final_learning_rate, updated_target);
58+
options.with_learning_rate_warmup(safe_warmup_start, options.learning_rate_warmup_target());
5059
}
5160
}
5261

@@ -880,8 +889,8 @@ void NeuralNetworkSerializer::save(const NeuralNetwork& nn, const std::string& p
880889
// create the object.
881890
auto tj = new TinyJSON::TJValueObject();
882891
add_basic(*tj);
883-
add_options(nn.options(), *tj);
884892
add_final_learning_rate(nn, *tj);
893+
add_options(nn.options(), *tj);
885894
add_errors(nn, *tj);
886895
add_layers(nn, *tj);
887896

0 commit comments

Comments
 (0)