<jupyter_start><jupyter_text>mxnet linear regression  1. 随机生成数据集<jupyter_code>%matplotlib inline
from IPython import display
from matplotlib import pyplot as plt
from mxnet import autograd, nd
import random
# generate random x1,x2 as features
num_inputs = 2 # 2 input: x1, x2
num_examples = 1000 # 1000 points
true_w = [2, -3.4] # 2 weight
true_b = 4.2 # bais
features = nd.random.normal(scale=1, shape=(num_examples, num_inputs))#标准差为1的正态分布
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += nd.random.normal(scale=0.01, shape=labels.shape)#标准差为0.01的正态分布
features[0], labels[0]
# features[0] is [x1,x2], label is y = x1*w1+x2*w2+b
# display data
# x: x1, y:y
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()
plt.scatter(features[:, 1].asnumpy(), labels.asnumpy(), 1)<jupyter_output><empty_output><jupyter_text>2. 读取数据Gluon提供了data包来读取数据<jupyter_code>from mxnet.gluon import data as gdata

batch_size = 10
# 将训练数据的特征和标签组合
dataset = gdata.ArrayDataset(features, labels)
# 随机读取小批量
data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True)
# 测试这个函数
batch_size = 10

for X, y in data_iter:
    print(X, y)
    break<jupyter_output>[[-1.9584894   0.3811466 ]
 [-0.55021375 -1.5918757 ]
 [-0.9457527  -1.8475903 ]
 [ 1.6698817   0.5393773 ]
 [ 1.1348199  -0.4925288 ]
 [-0.3367721  -1.253791  ]
 [ 0.95956105  0.14733684]
 [-0.7230168   0.88875777]
 [ 0.06527644 -0.9506362 ]
 [ 0.50472885 -0.48745614]]
<NDArray 10x2 @cpu(0)> 
[-1.0243565  8.525719   8.591789   5.711088   8.140179   7.782056
  5.623976  -0.2697006  7.5680094  6.874393 ]
<NDArray 10 @cpu(0)><jupyter_text>3. 定义模型<jupyter_code>from mxnet.gluon import nn

net = nn.Sequential()
net.add(nn.Dense(1))<jupyter_output><empty_output><jupyter_text>4. 初始化模型参数<jupyter_code>from mxnet import init

net.initialize(init.Normal(sigma=0.01))<jupyter_output><empty_output><jupyter_text>5. 定义损失函数<jupyter_code>from mxnet.gluon import loss as gloss

loss = gloss.L2Loss()  # 平方损失又称L2范数损失<jupyter_output><empty_output><jupyter_text>6. 定义优化算法<jupyter_code>from mxnet import gluon

trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.03})<jupyter_output><empty_output><jupyter_text>7. 训练模型<jupyter_code>num_epochs = 3
for epoch in range(1, num_epochs + 1):
    for X, y in data_iter:
        with autograd.record():
            l = loss(net(X), y)
        l.backward()
        trainer.step(batch_size)
    l = loss(net(features), labels)
    print('epoch %d, loss: %f' % (epoch, l.mean().asnumpy()))<jupyter_output>epoch 1, loss: 0.034991
epoch 2, loss: 0.000126
epoch 3, loss: 0.000049<jupyter_text>8. 访问模型训练出来的真实值<jupyter_code>dense = net[0]
true_w, dense.weight.data()
true_b, dense.bias.data()<jupyter_output><empty_output><jupyter_text>9. 思考 （1） 如果将l = loss(net(X), y)替换成l = loss(net(X), y).mean()，我们需要将trainer.step(batch_size)相应地改成trainer.step(1)。这是为什么呢？ （2） 查阅MXNet文档，看看gluon.loss和init模块里提供了哪些损失函数和初始化方法。<jupyter_code>from mxnet.gluon import loss as gloss

dir(gloss)<jupyter_output><empty_output><jupyter_text>（3）如何访问dense.weight的梯度？<jupyter_code>dense.weight.grad<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14329454990814453, 'fraction_numerical': 0.1065523576240049, 'mean_word_length': 5.1992409867172675, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '65022', 'license': 'MIT', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1583, 'n_words': 245, 'path': 'mxnet/linear_regression_gluon.ipynb', 'repo_name': 'ybdesire/machinelearning', 'size': '3266'}