手撕机器学习系列一---线性回归(np,torch分别实现)

手撕机器学习系列一—线性回归(np,torch分别实现)

一、 前言

虽然前几个系列都还没做完,但是最近面试发现很多地方都开始要求手撕xxx了,周围同学也有些笔试题开始考这东西了,因此再开一坑,之后慢慢填。

二、问题背景

随便给个函数 y = 4.5 ∗ x 1 + 2 ∗ x 3 + 5 y = 4.5*x_1 + 2*x_3 + 5 y=4.5x1+2x3+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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Pytorch实现的1DCNN-BiGRU-注意力机制的回归预测模型代码示例: ```python import torch import torch.nn as nn class CNN_GRU_Attention(nn.Module): def __init__(self, input_size, hidden_size, num_layers, output_size): super(CNN_GRU_Attention, self).__init__() # 1D Convolutional layer self.conv = nn.Conv1d(in_channels=input_size, out_channels=hidden_size, kernel_size=3, stride=1, padding=1) self.relu = nn.ReLU() # Bidirectional GRU layers self.gru = nn.GRU(hidden_size, hidden_size, num_layers, batch_first=True, bidirectional=True) # Attention layer self.attention = nn.Sequential( nn.Linear(hidden_size*2, hidden_size), nn.Tanh(), nn.Linear(hidden_size, 1), nn.Softmax(dim=1) ) # Output layer self.output = nn.Linear(hidden_size*2, output_size) def forward(self, x): # 1D Convolutional layer x = self.conv(x) x = self.relu(x) # Bidirectional GRU layers output, _ = self.gru(x) # Attention layer attention_weights = self.attention(output) context_vector = torch.sum(attention_weights * output, dim=1) # Output layer x = self.output(context_vector) return x ``` 该模型包括以下几个部分: 1. 1D卷积层:将输入的序列数据进行卷积操作,提取出序列中的特征。 2. 双向GRU层:将卷积层的输出作为双向GRU网络的输入,提取序列中的更高级别的特征。 3. 注意力层:将GRU层的输出作为注意力层的输入,计算每个时间步的权重,从而得到序列的上下文向量。 4. 输出层:将上下文向量作为输出层的输入,得到最终的回归预测结果。 可以根据自己的数据特点和任务需求,调整模型的超参数,如卷积核大小、GRU层数、隐藏层大小等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值