我们首先需要设置一个真实的w和b,用y=x*w+b生成数据集。然后初始化W和B,使用生成的数据集训练求出W和B
def sythetic_data(w,b,num_example):
x=torch.normal(0,1,(num_example,len(w)))
y=torch.matmul(x,w)+b
y+torch.normal(0,0.01,y.shape)
return x,y.reshape(-1,1)
true_w=torch.tensor([2,-3.4])
true_b=4.2
feature,label=sythetic_data(true_w,true_b,1000)
生成数据集true_w和true_b为真实的权重和偏重。x为1000×2张量的标准正态分布,y=x*w+b再加上噪声。
#feature和label为生成的数据集
def data_iter(batch_size,feature,label):
#求出feature的长度
num_example=len(feature)
#转换为list的集合,list为:(0,1,2,3....,999)
indices=list(range(num_example))
#随机打乱list的集合
random.shuffle(indices)
#生成小批量测试样本,每个样本大小为batch_size
for i in range(0,num_example,batch_size):
#min主要是为了防止越界
batch_indices=torch.tensor(indices[i:min(i+batch_size,num_example)])
#返还随机产生的feature和label,feature为10*1,label为10*2
yield feature[batch_indices],label[batch_indices]
batch_size =10
for x, y in data_iter(batch_size, feature, label):
def linreg(x,w,b):
return torch.matmul(x,w)+b
def squared_loss(y_hat,y):
return (y_hat-y.reshape(y_hat.shape))**2/2
w=torch.normal(0,0.01,size=(2,1),requires_grad=True)
b=torch.zeros(1,requires_grad=True)
w和b为需要求出的参数,squared_loss为损失函数
def sgd(parmas,lr,batch_size):
with torch.no_grad():
for parma in parmas:
parma-=lr*parma.grad/batch_size
parma.grad.zero_()
#学习率
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, feature, label):
#w和b为需要推断的数值,net函数计算出y_hat,loss为计算损失函数
l=loss(net(x,w,b),y)
l.sum().backward()
sgd([w,b],lr,batch_size)
with torch.no_grad():
train_l = loss(net(feature, w, b), label)
print(f'epoch {epoch + 1}, loss {float(train_l.mean()):f}')
print(f'w的估计误差: {true_w - w.reshape(true_w.shape)}')
print(f'b的估计误差: {true_b - b}')