PyTorch代码之用numpy数组模拟线性回归

102 阅读1分钟
# gradients_numpy
import numpy as np
#1)Design model (input, output size, forward pass)
#2)Construct loss and optimizer
#3)Training loop
#   -forward pass:compute prediction
#   -backward pass:gradients
#   -update weights

# f = w * x

# f = 2 * x
X = np.array([1, 2, 3, 4], dtype=np.float32)
Y = np.array([2, 4, 6, 8], dtype=np.float32)
# import torch
#
# X = torch.tensor([1, 2, 3, 4], dtype=torch.float32)
# Y = torch.tensor([2, 4, 6, 8], dtype=torch.float32)

w = 0.1 #取0.0则会出现梯度消失的问题
# w = torch.tensor(0.0, dtype=torch.float32, requires_grad=True)

#model prediction
def forward(x):
    return w * x
#loss
def loss(y, y_predicted):
    return ((y_predicted - y) ** 2).mean()
#gradient
#MSE = 1/N * (w*x - y) ** 2
#dJ/dw=1/N 2x (w*x-y)
def gradient(x, y, y_predicted):
# 这里写 w * x 而不是 2 * x, 因为线性模型的参数我们是不知道的
    return np.dot(w*x, y_predicted-y).mean()
print(f'Prediction before training:f(5) = {forward(5):.3f}')

#Training
learning_rate = 0.01
n_iters = 10

for epoch in range(n_iters):
    #prediction = forward pass
    y_pred = forward(X)

    #loss
    l = loss(Y, y_pred)

    #gradients = backward pass
    dw = gradient(X, Y, y_pred)
    w -= learning_rate * dw
    # w -= learning_rate * w.grad

    # w.grad.zero_()

    if (epoch+1) % 1 == 0:
        print(f'epoch{epoch+1}:w = {w:.3f}, loss = {l:.8f}')

print(f'Prediction after training:f(5) = {forward(5):.3f}')