线性回归的从零开始实现

该代码实现了一个简单的线性回归模型训练过程。首先,生成带噪声的数据集,然后定义了数据迭代器、线性回归函数、平方损失函数及随机梯度下降优化器。通过多次迭代,更新权重w和偏置b,最终计算出的w和b的估计误差。
摘要由CSDN通过智能技术生成

我们首先需要设置一个真实的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}')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值