pytorch入门>线性回归代码实现1

看李沐老师的视频跟着做的基础线性回归。

线性模型+平方损失函数+随机梯度下降优化

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import random
import torch


# In[51]:


def generate_data(w,b,num):
    x = torch.normal(0,1, (num,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.])
true_b = 3.
data,labels = generate_data(true_w,true_b,100)
print(data)
print(labels)


# In[52]:


import matplotlib.pyplot as plt
#print([data[:,(1)]])data第1列


# In[53]:


plt.scatter(data[:,(1)].detach().numpy(),labels.detach().numpy())
plt.show()


# In[54]:


def data_iter(batch_size,data,labels):
    num_examples = len(data)
    indices = list(range(num_examples))
    print(   "indices:",indices)
    random.shuffle(indices)
    for i in range(0,num_examples,batch_size):
        batch_index = torch.tensor(indices[i:min(i+batch_size,num_examples)])
        yield data[batch_index],labels[batch_index]
batch_size =10
# for x, y in data_iter(batch_size,data,labels):
#     print(x)
#     print(y)
#     break


# In[60]:


w = torch.normal(0,1,size =(2,1),requires_grad = True)
b =torch.zeros(1,requires_grad = True)
def model(x, w, b):
    return torch.matmul(x,w)+b
def my_loss(pred,y):
    return ((pred-y.reshape(pred.shape))**2/2)
def sgd(params,lr,batch_size):
    with torch.no_grad():
        for para in params:
            para-=lr*para.grad/batch_size
            para.grad.zero_()


# In[61]:


lr=0.1
epochs = 3
net = model
loss = my_loss
for epo  in range(epochs):
    for x,y in data_iter(batch_size,data, labels):
        l= loss(model(x,w,b),y)
        l.sum().backward()
        sgd([w,b],lr,batch_size)
    with torch.no_grad():
        trainl= loss(net(data,w,b),labels)
        print(epo,trainl.mean())


# In[62]:


print(w,'\n',true_w,b, true_b)


# In[ ]:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值