mirror of
https://github.com/RustyRaptor/incubator-mxnet.git
synced 2026-12-05 16:55:25 -07:00
[cpp-package] add lr scheduler (#6885)
* add lr scheduler * Update lr_scheduler.h * Update mlp_gpu.cpp * Update test_score.cpp * update optimizer.hpp
This commit is contained in:
1 parent
8519eafd1d
commit
76dee53dc4
15 files changed
+257
-96
No files matched your search
@@ -24,7 +24,7 @@ Symbol mlp(const vector<int> &layers) {
|
||||
weights[i],
|
||||
biases[i],
|
||||
layers[i]);
|
||||
outputs[i] = i == layers.size()-1? fc : Activation(fc, ActivationActType::kRelu);
|
||||
outputs[i] = i == layers.size()-1 ? fc : Activation(fc, ActivationActType::kRelu);
|
||||
}
|
||||
|
||||
return SoftmaxOutput(outputs.back(), label);
|
||||
@@ -70,12 +70,24 @@ int main(int argc, char** argv) {
|
||||
|
||||
// Create sgd optimizer
|
||||
Optimizer* opt = OptimizerRegistry::Find("sgd");
|
||||
opt->SetParam("rescale_grad", 1.0/batch_size);
|
||||
opt->SetParam("rescale_grad", 1.0/batch_size)
|
||||
->SetParam("lr", learning_rate)
|
||||
->SetParam("wd", weight_decay);
|
||||
std::unique_ptr<LRScheduler> lr_sch(new FactorScheduler(5000, 0.1));
|
||||
opt->SetLRScheduler(std::move(lr_sch));
|
||||
|
||||
// Create executor by binding parameters to the model
|
||||
auto *exec = net.SimpleBind(ctx, args);
|
||||
auto arg_names = net.ListArguments();
|
||||
|
||||
// Create metrics
|
||||
Accuracy train_acc, val_acc;
|
||||
|
||||
// Start training
|
||||
for (int iter = 0; iter < max_epoch; ++iter) {
|
||||
int samples = 0;
|
||||
train_iter.Reset();
|
||||
train_acc.Reset();
|
||||
|
||||
auto tic = chrono::system_clock::now();
|
||||
while (train_iter.Next()) {
|
||||
@@ -87,35 +99,40 @@ int main(int argc, char** argv) {
|
||||
// CopyTo is imperative, need to wait for it to complete.
|
||||
NDArray::WaitAll();
|
||||
|
||||
// Create executor by binding parameters to the model
|
||||
auto *exec = net.SimpleBind(ctx, args);
|
||||
// Compute gradients
|
||||
exec->Forward(true);
|
||||
exec->Backward();
|
||||
// Update parameters
|
||||
exec->UpdateAll(opt, learning_rate, weight_decay);
|
||||
// Remember to free the memory
|
||||
delete exec;
|
||||
}
|
||||
auto toc = chrono::system_clock::now();
|
||||
|
||||
Accuracy acc;
|
||||
// Update parameters
|
||||
for (size_t i = 0; i < arg_names.size(); ++i) {
|
||||
if (arg_names[i] == "X" || arg_names[i] == "label") continue;
|
||||
opt->Update(i, exec->arg_arrays[i], exec->grad_arrays[i]);
|
||||
}
|
||||
// Update metric
|
||||
train_acc.Update(data_batch.label, exec->outputs[0]);
|
||||
}
|
||||
// one epoch of training is finished
|
||||
auto toc = chrono::system_clock::now();
|
||||
float duration = chrono::duration_cast<chrono::milliseconds>(toc - tic).count() / 1000.0;
|
||||
LG << "Epoch[" << iter << "] " << samples/duration \
|
||||
<< " samples/sec " << "Train-Accuracy=" << train_acc.Get();;
|
||||
|
||||
val_iter.Reset();
|
||||
val_acc.Reset();
|
||||
while (val_iter.Next()) {
|
||||
auto data_batch = val_iter.GetDataBatch();
|
||||
data_batch.data.CopyTo(&args["X"]);
|
||||
data_batch.label.CopyTo(&args["label"]);
|
||||
NDArray::WaitAll();
|
||||
auto *exec = net.SimpleBind(ctx, args);
|
||||
|
||||
// Only forward pass is enough as no gradient is needed when evaluating
|
||||
exec->Forward(false);
|
||||
acc.Update(data_batch.label, exec->outputs[0]);
|
||||
delete exec;
|
||||
val_acc.Update(data_batch.label, exec->outputs[0]);
|
||||
}
|
||||
float duration = chrono::duration_cast<chrono::milliseconds>(toc - tic).count() / 1000.0;
|
||||
LG << "Epoch: " << iter << " " << samples/duration << " samples/sec Accuracy: " << acc.Get();
|
||||
LG << "Epoch[" << iter << "] Val-Accuracy=" << val_acc.Get();
|
||||
}
|
||||
|
||||
delete exec;
|
||||
MXNotifyShutdown();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user