import numpy as np
x_data = np.array([1, 2, 3])
y_data = np.array([2, 4, 6])
epochs = 10
lr = 0.01
w = 0
cost = []
for epoch in range(epochs):
yhat = x_data * w
loss = np.average((yhat - y_data)**2)
cost.append(loss)
dw = -2*(y_data - yhat) @ x_data.T/(x_data.shape[0])
w = w - lr*dw
print(w)
pytorch实现线性回归
import torch
num_samples = 64 # N
dim_in, dim_hid, dim_out = 1000, 100, 10 # IN H OUT
x = torch.randn(num_samples, dim_in) # N * IN
y = torch.randn(num_samples, dim_out) # N * OUT
w1 = torch.randn(dim_in, dim_hid) # IN * H
w2 = torch.randn(dim_hid, dim_out) # H * OUT
eta = 1e-6
for i in range(1000):
#Forward pass
h = x @ w1 # N * H
h_relu = h.clamp(min = 0) # N * H
y_pred = h_relu @ w2 # N * OUT
#Loss
loss = (y_pred - y).pow(2).sum().item()
print('times is {}, loss is {}'.format(i, loss))
#Backward pass
grad_y_pred = 2.0 * (y_pred - y) # N * OUT
grad_w2 = (h_relu.t()) @ (grad_y_pred) #H * OUT = (H * N) * (N * OUT),其中(H * N) = (N * H).T
grad_h_relu = grad_y_pred @ ((w2.t()))# N * H = (N * OUT) * (OUT * H),其中(OUT * H) = (H * OUT).T
grad_h = grad_h_relu.clone()
grad_h[h < 0] = 0
grad_w1 = (x.t()) @ (grad_h) # IN * H = (IN * N) * (N * H)
w1 = w1 - eta * grad_w1
w2 = w2 - eta
pytorch实现简单的神经网络
import torch
import torch.nn as nn
import torch.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = nn.Linear(1,1,bias=False)
def forward(self,x):
y_pred = self.linear(x)
return y_pred
model = Model()
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])
criterion = nn.MSELoss(size_average= True) # 注意此处改为True#注意这里改为True
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
epoches = 20
cost = []
for epoch in range(epoches):
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print(loss)
cost.append(loss.item()) #由于只有一个值 直接item()
optimizer.zero_grad()#清零梯度
loss.backward()#计算梯度
#参数更新
optimizer.step()
list(model.parameters())