pytorch入门>线性回归代码实现1
程序员文章站
2022-06-11 23:44:43
...
看李沐老师的视频跟着做的基础线性回归。
线性模型+平方损失函数+随机梯度下降优化
#!/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[ ]:
上一篇: MySQL中批量创建日志表信息脚本
下一篇: Pytorch线性回归-Pytorch