08 线性回归从零开始实现
temp0723
In [64]:
import torch
from torch import Tensor
import random as r
import matplotlib.pyplot as plt
from tianfy.pytools import batch_data
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
In [70]:
def get_data(w, b, num_samples):
num_features = w.shape[0]
X = torch.normal(0, 1, (num_samples, num_features))
y = X @ w + b
y += torch.normal(0, 0.01, y.shape)
return X, y.reshape(-1, 1)
In [81]:
w = Tensor([[3.5, 4]]).T
b = 2.5
X, y = get_data(w, b, 1000)
n_samples, n_features = X.shape
w = torch.zeros((n_features + 1, 1), requires_grad=True)
X = torch.cat((torch.ones((n_samples, 1)), X), dim=1)
def model(X, w):
return X @ w
def loss(y_hat, y):
return (y_hat - y) ** 2 / 2
def sgd(w, lr, batch_size):
with torch.no_grad():
w -= lr * w.grad / batch_size
w.grad.zero_()
lr = 0.03
epochs = 5
batch_size = 10
for epoch in range(epochs):
for X_, y_ in batch_data(X, y, batch_size):
l = loss(model(X_, w), y_)
l.sum().backward()
sgd(w, lr, batch_size)
with torch.no_grad():
train_l = loss(model(X, w), y)
print(f"epoch{epoch+1}, loss {train_l.mean():2f}, params {(w).view(-1).tolist()}")

浙公网安备 33010602011771号