【pytorch_3】简单的线性回归

搭建一个简单的线性回归模型,并将代码转化成pytorch版。
目的:对比普通代码与pytorch代码的区别。

#随机梯度下降法(普通版)
import numpy as np

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

w = 1.0

def forward(x):
    return x*w 
    
def loss(x,y):
    y_pred = forward(x)
    return (y_pred-y)**2  
    
def gradient(x,y):
    return 2*x*(x*w-y)

print('Predict (before training)',4, forward(4))
for epoch in range(100):
    for x,y in zip(x_data,y_data):
        grad = gradient(x,y)
        w = w - 0.01*grad
        print('\tgrad:',x,y,grad)
        l = loss(x,y)
    print('Epoch:',epoch,'w-',w,'loss=',l)
print('Predict (after training)',4,forward(4))
#随机梯度下降法(pytorch)
import torch

x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])

w = torch.Tensor([1.0])
w.requires_grad = True

def forward(x):
    return x*w
    
def loss(x,y):
    y_pred = forward(x)
    return (y_pred-y)**2

print('Predict (before training)',4, forward(4).item())
for epoch in range(100):
    for x,y in zip(x_data,y_data):
        l = loss(x,y)
        l.backward()

        print('\tgrad:',x,y,w.grad.item())
        w.data = w.data - 0.01*w.grad.data
        w.grad.data.zero_()
        
    print('progress:',epoch,'w-',w.item(),'loss=',l.item())
print('Predict (after training)',4,forward(4).item())

你可能感兴趣的:(Pytorch学习专栏,深度学习,python,神经网络,逻辑回归)