import torch
from IPython import display
import numpy as np
import random
from matplotlib import pyplot as pltimport syssys.path.append('路径')
from d2lzh_pytorch import *'''
backward()函数:一次小批量执行完在进行反向传播
线性回归模型步骤;1.数据处理2.模型定义:根据矩阵形式运算,模型可以一次计算多个样本,比如X:1000x2, w:2x1 则模型可以一次计算1000个样本3.损失函数:4.优化算法:sgd则是小批量中每个样本loss运行完后,对应参数的梯度进行了累加,得到一个小批量的代表梯度 w1,w2,b然后将每个小批量的参数梯度进行梯度下降5.模型预测
'''
'''
样本X=1000,特征=2,w=2,-3.4;b=4.2 随机噪声ξ y=Xw+b+ξ
噪声服从均值为0,标准差为0.01的正态分布 噪声代表了数据集中无意义的干扰
'''
num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.3
features = torch.randn(num_examples, num_inputs, dtype=torch.float32)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()), dtype=torch.float32)
lr = 0.03
num_epoch = 3
net = linreg
loss = squared_loss
'''
每次返回一个batch-size大小随机样本的特征和标签
'''
batch_size = 10
w = torch.tensor((np.random.normal(0, 0.01, (num_inputs, 1))), dtype=torch.float32)
b = torch.tensor(1, dtype=torch.float32)
w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
for epoch in range(num_epoch):for X, y in data_iter(batch_size, features, labels):l = loss(net(X, w, b), y).sum() l.backward() sgd([w, b], lr, batch_size) w.grad.data.zero_()b.grad.data.zero_()train_l = loss(net(features, w, b), labels) print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))