/ deeplearn  

mxnet实现线性回归模型

mxnet线性回归demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from IPython import display
from matplotlib import pyplot as plt
from mxnet import autograd, nd
import random

# 特征个数
num_inputs = 2
# 样本个数
num_examples = 1000
# 权重
true_w=[2,-3.4]
# 偏差
true_b=4.2
# y=Xw+b+c 简单线性回归 c为噪音

# 生成行长度为2的向量
features = nd.random.normal(scale=1, shape=(num_examples, num_inputs))
lables = true_w[0]*features[:, 0]+true_w[1]*features[:, 1] + true_b
lables += nd.random.normal(scale=0.01, shape=lables.shape)

def use_svg_display():
display.set_matplotlib_formats('svg')

def set_figsize(figsize=(3.5,2.5)):
use_svg_display()
plt.rcParams['figure.figsize'] = figsize

set_figsize((7,5))
# 画图显示点之间的关系
# plt.scatter(features[:,1].asnumpy(), lables.asnumpy(), 1);
# plt.show()

def data_iter(batch_size, features, lables):
num_examples = len(features)
indices = list(range(num_examples))
# 生成features长度的索引列表 并用shuffle打乱
random.shuffle(indices)
for i in range(0, num_examples, batch_size):
j = nd.array(indices[i: min(i+batch_size, num_examples)])
# 获取i到i+batch_size对应长度的索引值列表
# 根据提供的索引值列表使用take获取对应的数组值
yield features.take(j), lables.take(j)


batch_size = 10

# for x,y in data_iter(batch_size, features, lables):
# print(x, y)
# break;

# 初始化权重 标准差0.01 均值0
w = nd.random.normal(scale=0.01, shape=(num_inputs, 1))

# 偏差0
b = nd.zeros(shape=(1,))

# 创建梯度
w.attach_grad()
b.attach_grad()

# 定义线性回归模型
def linreg(X, w, b):
return nd.dot(X, w) + b

# 定义损失函数
def squared_loss(y_hat, y):
return (y_hat - y.reshape(y_hat.shape)) ** 2 /2

# 定义sgd迭代模型进行随机梯度下降
def sgd(params, lr, batch_size):
for param in params:
param[:] = param - lr*param.grad/batch_size #?

# 模型训练
lr = 0.03 #学习率
num_epochs = 3 #迭代周期
net = linreg
loss = squared_loss

for epoch in range(num_epochs):
for X, y in data_iter(batch_size, features, lables):
with autograd.record(): # autograd模块⾃动化计算求导
l = loss(net(X, w, b), y)# 一个批量的损失
l.backward() # 计算梯度 反向传播函数处理
sgd([w,b], lr, batch_size) # 优化w 和 b
# 将回归模型得到的解析解的值与标准值进行比较
train_l = loss(net(features, w, b), lables)
print('epoch %d, loss %f' % (epoch+1, train_l.mean().asnumpy()))

## 比较模型与真实值的 权重 与 偏差
print(true_w, w)
print(true_b, b)