momentum-gluon.md 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
# 动量法 --- 使用Gluon


`Gluon`里,使用动量法很容易。我们无需重新实现它。例如,在随机梯度下降中,我们可以定义`momentum`参数。

```{.python .input  n=1}
import mxnet as mx
from mxnet import autograd
from mxnet import gluon
A
Aston Zhang 已提交
10
from mxnet import nd
11 12 13 14 15 16 17 18 19 20 21
import random

# 生成数据集。
num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
X = nd.random_normal(scale=1, shape=(num_examples, num_inputs))
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
y += .01 * nd.random_normal(scale=1, shape=y.shape)

A
Aston Zhang 已提交
22 23

# 创建模型和定义损失函数。
24 25 26 27 28 29
net = gluon.nn.Sequential()
net.add(gluon.nn.Dense(1))
```

为了使学习率在两个epoch后自我衰减,我们需要访问`gluon.Trainer``learning_rate`属性和`set_learning_rate`函数。

A
Aston Zhang 已提交
30
```{.python .input}
31
%matplotlib inline
A
Aston Zhang 已提交
32 33 34 35 36
%config InlineBackend.figure_format = 'retina'
import numpy as np
import sys
sys.path.append('..')
import utils
37 38 39 40 41
```

使用动量法,最终学到的参数值与真实值较接近。

```{.python .input  n=3}
A
Aston Zhang 已提交
42 43 44 45 46
net.collect_params().initialize(mx.init.Normal(sigma=1), force_reinit=True)
trainer = gluon.Trainer(net.collect_params(), 'sgd',
                        {'learning_rate': 0.2, 'momentum': 0.9})
utils.optimize(batch_size=10, trainer=trainer, num_epochs=3, decay_epoch=2,
               log_interval=10, X=X, y=y, net=net)
47 48
```

A
Aston Zhang 已提交
49
## 小结
50 51 52 53 54 55 56

* 使用`Gluon``Trainer`可以轻松使用动量法。

## 练习

* 如果想用以上代码重现随机梯度下降,应该把动量参数改为多少?

A
Aston Zhang 已提交
57 58 59 60 61
## 讨论

欢迎扫码直达[本节内容讨论区](https://discuss.gluon.ai/t/topic/1880)

![](../img/qr_momentum-gluon.svg)