#3.2线性回归从零开始
import torch
import numpy as np
import random
num_inputs = 2
num_examples = 1000
true_w = [2,-3.4]
true_b = 4.2
features = torch.randn(num_examples,num_inputs,
dtype=torch.float32)
labels = true_w[0]*features[:,0]+true_w[1]*features[:,1]
labels += torch.tensor(np.random.normal(0,0.01,size=labels.size()),dtype=torch.float32)
#labels加上了一点正态分布的随机量
print(features[0],labels[0])
def data_iter(batch_size,features,labels):
#数据读取
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices)#样本读取是随机的
#打乱顺序编码
for i in range(0,num_examples,batch_size):
j = torch.LongTensor(indices[i:min(i+batch_size,num_examples)])
yield features.index_select(0,j),labels.index_select(0,j)
batch_size = 10
for X,y in data_iter(batch_size,features,labels):
print(X,y)
break
#将权重初始化成均值为0,标准差为0.01的正太随机数,偏差则初始化
#为0
w = torch.tensor(np.random.normal(0,0.01,(num_inputs,1)),dtype=torch.float32)
b = torch.zeros(1,dtype=torch.float32)
#对这些参数求梯度来迭代参数值
w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
#线性回归的矢量计算
def linreg(X,w,b):
return torch.mm(X,w)+b
#定义损失函数
def squared_loss(y_hat,y):
return (y_hat-y.view(y_hat.size()))**2/2
def sgd(params,lr,batch_size):
for param in params:
param.data -= 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,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()))