手撕机器学习系列一—线性回归(np,torch分别实现)
一、 前言
虽然前几个系列都还没做完,但是最近面试发现很多地方都开始要求手撕xxx了,周围同学也有些笔试题开始考这东西了,因此再开一坑,之后慢慢填。
二、问题背景
随便给个函数
y
=
4.5
?
x
1
+
2
?
x
3
+
5
y = 4.5*x_1 + 2*x_3 + 5
y=4.5?x1?+2?x3?+5,给你一些列数据和label预测这里的三个系数
三、np实现
import numpy as np
num_inputs = 2
num_example = 1000
true_w = [4,5.2]
true_b = 5
features = np.random.randn(num_example,num_inputs)
labels = np.dot(features,true_w) + true_b
print(np.shape(labels), np.shape(features))
def data_iter(features,labels,batch_size):
index = np.array(range(len(features)))
np.random.shuffle(index)
for i in range(0,len(features),batch_size):
j = index[i:min(i+batch_size, len(features))]
yield features[j], labels[j]
def linear(features,w,b):
return np.dot(features,w) + b
def mse(pre,lab):
return (np.reshape(pre,-1) - np.reshape(lab,-1))**2
def sgd(params,lr,batch_size,X,Y):
w1 = params[0][0]
w2 = params[0][1]
b = params[1]
gd1,gd2,gd3 = 0,0,0
for x,y in zip(X,Y):
gd1 += 2*x[0]*(w1*x[0] + w2*x[1] + b - y)
gd2 += 2*x[1]*(w1*x[0] + w2*x[1] + b - y)
gd3 = (w1*x[0] + w2*x[1] + b - y)
w1 -= lr*gd1/batch_size
w2 -= lr*gd2/batch_size
b -= lr*gd3/batch_size
return w1,w2,b
w = np.random.normal(0,0.01,(num_inputs,1))
b = 0
lr = 0.1
batch_size = 10
num_epoch = 3
net = linear
loss = mse
for epoch in range(1,num_epoch+1):
for X,y in data_iter(features,labels,batch_size):
l = loss(net(X,w,b),y).mean()
w1,w2,b0 = sgd([w,b],lr,batch_size,X,y)
w[0], w[1], b = w1,w2,b0
train_l = loss(net(features,w,b),labels)
print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))
print(true_w, '\n', w)
print(true_b, '\n', b)
四、torch实现
import torch
import numpy as np
import random
import tornado
num_inputs = 2
num_example = 1000
true_w = [4,5.2]
true_b = [5]
features = torch.rand(num_example,num_inputs, dtype=torch.float32)
labels = features[:,0]*true_w[0] + features[:,1]*true_w[1] + true_b[0]
print(features.size(),labels.size())
def data_iter(features,labels,batch_size):
index = list(range(len(features)))
random.shuffle(index)
for i in range(0,len(features),batch_size):
j = torch.LongTensor(index[i:min(i+batch_size,len(features))])
yield features.index_select(0,j), labels.index_select(0,j)
for X,y in data_iter(features,labels,10):
print(X,y)
break
w = torch.tensor(np.random.normal(0,0.001,(num_inputs,1)),dtype=torch.float32)
b = torch.zeros(1,dtype=torch.float32)
w.requires_grad_(True)
b.requires_grad_(True)
def linear(X,w,b):
return torch.mm(X,w)+b
def mse(pre,lab):
return (pre.view(-1) - lab.view(-1))**2
def sgd(params, lr, batch_size):
for param in params:
param.data -= lr*param.grad/batch_size
lr = 0.1
num_epoch = 3
net = linear
loss = mse
batch_size = 10
for epoch in range(1,num_epoch+1):
for X,y in data_iter(features,labels,batch_size):
l = loss(net(X,w,b),y).sum()
l.backward()
sgd([w,b],lr,batch_size)
w.grad.data.zero_()
b.grad.data.zero_()
train_l = loss(net(X,w,b),y)
print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))
print(true_w, '\n', w)
print(true_b, '\n', b)
五、完全torch实现
快乐调包
import torch
import torch.nn as nn
import torch.optim as opt
import torch.utils.data as Data
num_inputs = 2
num_example = 1000
true_w = [4,5.2]
true_b = [5]
features = torch.rand(num_example,num_inputs, dtype=torch.float32)
labels = features[:,0]*true_w[0] + features[:,1]*true_w[1] + true_b[0]
print(features.size(),labels.size())
batch_size = 10
dataset = Data.TensorDataset(features,labels)
data_iter = Data.DataLoader(dataset, batch_size, shuffle= True)
class LinearNet(nn.Module):
def __init__(self, n_feature) -> None:
super(LinearNet,self).__init__()
self.linear = nn.Linear(n_feature,1)
def forward(self,x):
return self.linear(x)
net = LinearNet(2)
print(net)
nn.init.normal_(net.linear.weight,mean = 0, std=0.05)
nn.init.constant_(net.linear.bias, val = 0)
loss = nn.MSELoss()
optimizer = opt.SGD(net.parameters(), lr = 0.03)
print(optimizer)
num_epoch = 10
for epoch in range(1,num_epoch+1):
for X,y in data_iter:
out = net(X)
l = loss(out,y.view(-1,1))
optimizer.zero_grad()
l.backward()
optimizer.step()
print('epoch %d, loss: %f' % (epoch, l.item()))
print(true_w, net.linear.weight)
print(true_b, net.linear.bias)
|