当前位置:网站首页>Univariate linear regression model

Univariate linear regression model

2022-06-12 06:06:00 Singing under the hedge

One variable linear regression model

One 、 Solving steps

1. Determine the model

Model: y=wx+b

2. Choose the loss function MES Insert picture description here
3. Solve gradient and update w,b

w = w - LR * w.grade

b = b - LR * b.grade

Two 、 Code implementation

# One variable linear regression model 
import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)
lr=0.05 # Learning rate 
# Create training data 
x = torch.rand(20,1)*10
y = 2*x+(5+torch.randn(20,1))# Plus random noise 

# Construct linear regression parameters 
w = torch.randn((1),requires_grad=True)
b = torch.zeros((1),requires_grad=True)

for iteration in range(1000):
    #  Forward propagation 
    wx = torch.mul(w,x)
    y_pred = torch.add(wx,b)

    # Calculation  MSE loss
    loss=(0.5*(y-y_pred)**2).mean()

    #  Back propagation 
    loss.backward()

    #  Update parameters 

    b.data.sub_(lr*b.grad)
    w.data.sub_(lr * w.grad)

    # The gradient of the clearing tensor 
    w.grad.zero_()
    b.grad.zero_()

    # mapping 
    if iteration %20 ==0 :
        plt.scatter(x.data.numpy(),y.data.numpy())
        plt.plot(x.data.numpy(),y_pred.data.numpy(),'r-',lw=5)
        plt.text(2,20,'Loss=%.4f' % loss.data.numpy(),fontdict={
    'size':20,'color':'red'})
        plt.xlim(1.5,10)
        plt.ylim(8,28)
        plt.title("Iteration:{}\nw:{} b:{}".format(iteration,w.data.numpy(),b.data.numpy()))
        plt.pause(0.5)

        if loss.data.numpy() <1:
            break

3、 ... and 、 Realization effect

 Insert picture description here

原网站

版权声明
本文为[Singing under the hedge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010612307233.html