文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書
注:本文為李沐大神的《動(dòng)手學(xué)深度學(xué)習(xí)》的課程筆記迹淌!
創(chuàng)建數(shù)據(jù)集
# 導(dǎo)入mxnet
import random
import mxnet as mx
# 導(dǎo)入mxnet的gluon, ndarray, autograd
from mxnet import gluon
from mxnet import autograd
from mxnet import ndarray as nd
# 設(shè)置隨機(jī)種子
mx.random.seed(1)
random.seed(1)
# 訓(xùn)練數(shù)據(jù)的維度
num_inputs = 2
# 訓(xùn)練數(shù)據(jù)的樣本數(shù)量
num_examples = 1000
# 實(shí)際的權(quán)重w
true_w = [2, -3.4]
# 實(shí)際的偏置b
true_b = 4.2
# 隨機(jī)生成均值為0, 方差為1, 服從正態(tài)分布的訓(xùn)練數(shù)據(jù)X,
X = nd.random_normal(shape=(num_examples, num_inputs))
# 根據(jù)X, w, b生成對(duì)應(yīng)的輸出y
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
# 給y加上隨機(jī)噪聲
y += 0.01 * nd.random_normal(shape=y.shape)
數(shù)據(jù)展示
%matplotlib inline
import matplotlib.pyplot as plt
# 繪制數(shù)據(jù)的散點(diǎn)圖
plt.scatter(X[:, 1].asnumpy(), y.asnumpy())
plt.show()
數(shù)據(jù)讀取
# 訓(xùn)練時(shí)的批數(shù)據(jù)大小
batch_size = 10
# 創(chuàng)建數(shù)據(jù)集
dataset = gluon.data.ArrayDataset(X, y)
# 讀取數(shù)據(jù)
data_iter = gluon.data.DataLoader(dataset, batch_size, shuffle=True)
# 查看數(shù)據(jù)
for data, label in data_iter:
print data, label
break
[[-2.11255503 0.61242002]
[ 2.18546367 -0.48856559]
[ 0.91085583 0.38985687]
[-0.56097323 1.44421673]
[ 0.31765923 -1.75729597]
[-0.57738042 2.03963804]
[-0.91808975 0.64181799]
[-0.20269176 0.21012937]
[-0.22549874 0.19895147]
[ 1.42844415 0.06982213]]
<NDArray 10x2 @cpu(0)>
[ -2.11691356 10.22533131 4.70613146 -1.82755637 10.82125568
-3.88111711 0.17608714 3.07074499 3.06542921 6.82972908]
<NDArray 10 @cpu(0)>
定義模型
# 定義一個(gè)空的模型
net = gluon.nn.Sequential()
# 加入一個(gè)Dense層
net.add(gluon.nn.Dense(1))
初始化模型參數(shù)
net.initialize()
定義損失函數(shù)
square_loss = gluon.loss.L2Loss()
優(yōu)化
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.01})
訓(xùn)練
# 定義訓(xùn)練的迭代周期
epochs = 5
# 訓(xùn)練
for epoch in xrange(epochs):
# 總的loss
total_loss = 0
for data, label in data_iter:
# 記錄梯度
with autograd.record():
# 計(jì)算預(yù)測(cè)值
output = net(data)
# 計(jì)算loss
loss = square_loss(output, label)
# 根據(jù)loss進(jìn)行反向傳播計(jì)算梯度
loss.backward()
# 更新權(quán)重, batch_size用來(lái)進(jìn)行梯度平均
trainer.step(batch_size)
# 計(jì)算總的loss
total_loss += nd.sum(loss).asscalar()
print "Epoch %d, average loss: %f" % (epoch, total_loss/num_examples)
Epoch 0, average loss: 7.403182
Epoch 1, average loss: 0.854247
Epoch 2, average loss: 0.099864
Epoch 3, average loss: 0.011887
Epoch 4, average loss: 0.001479
代碼地址
https://github.com/SnailTyan/gluon-practice-code
參考資料
ArrayDataset
https://mxnet.incubator.apache.org/api/python/gluon/data.html#mxnet.gluon.data.ArrayDatasetTrainer
https://mxnet.incubator.apache.org/api/python/gluon/gluon.html?highlight=trainer#mxnet.gluon.Trainer