前馈神经网络正则化例子

直接看代码:

python 复制代码
import torch  
import numpy as np  
import random  
from IPython import display  
from matplotlib import pyplot as plt  
import torchvision  
import torchvision.transforms as transforms   

mnist_train = torchvision.datasets.MNIST(root='/MNIST', train=True, download=True, transform=transforms.ToTensor())  
mnist_test = torchvision.datasets.MNIST(root='./MNIST', train=False,download=True, transform=transforms.ToTensor())  

batch_size = 256 

train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True,num_workers=0)  
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False,num_workers=0)  

num_inputs,num_hiddens,num_outputs =784, 256,10

def init_param():
    W1 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens,num_inputs)), dtype=torch.float32)  
    b1 = torch.zeros(1, dtype=torch.float32)  
    W2 = torch.tensor(np.random.normal(0, 0.01, (num_outputs,num_hiddens)), dtype=torch.float32)  
    b2 = torch.zeros(1, dtype=torch.float32)  
    params =[W1,b1,W2,b2]
    for param in params:
        param.requires_grad_(requires_grad=True)  
    return W1,b1,W2,b2

def relu(x):
    x = torch.max(input=x,other=torch.tensor(0.0))  
    return x

def net(X):  
    X = X.view((-1,num_inputs))  
    H = relu(torch.matmul(X,W1.t())+b1)  
    #myrelu =((matmal x,w1)+b1),return  matmal(myrelu,w2 )+ b2
    return relu(torch.matmul(H,W2.t())+b2 )
    return torch.matmul(H,W2.t())+b2


def SGD(paras,lr):  
    for param in params:  
        param.data -= lr * param.grad  
        
def l2_penalty(w):
    return (w**2).sum()/2


def train(net,train_iter,test_iter,loss,num_epochs,batch_size,lr=None,optimizer=None,mylambda=0):  
    
    train_ls, test_ls = [], []
    
    for epoch in range(num_epochs):
        
        ls, count = 0, 0
        
        for X,y in train_iter :
            X = X.reshape(-1,num_inputs)
            l=loss(net(X),y)+ mylambda*l2_penalty(W1) + mylambda*l2_penalty(W2)
            
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            
            ls += l.item()
            count += y.shape[0]
            
        train_ls.append(ls)
        
        ls, count = 0, 0
        
        for X,y in test_iter:
            X = X.reshape(-1,num_inputs)
            l=loss(net(X),y) + mylambda*l2_penalty(W1) + mylambda*l2_penalty(W2)
            ls += l.item()
            count += y.shape[0]
            
        test_ls.append(ls)
        
        if(epoch)%2==0:
            print('epoch: %d, train loss: %f, test loss: %f'%(epoch+1,train_ls[-1],test_ls[-1]))
            
    return train_ls,test_ls



lr = 0.01

num_epochs = 20

Lamda = [0,0.1,0.2,0.3,0.4,0.5]

Train_ls, Test_ls = [], []

for lamda in Lamda:
    print("current lambda is %f"%lamda)
    W1,b1,W2,b2 = init_param()
    loss = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD([W1,b1,W2,b2],lr = 0.001)
    train_ls, test_ls = train(net,train_iter,test_iter,loss,num_epochs,batch_size,lr,optimizer,lamda)   
    Train_ls.append(train_ls)
    Test_ls.append(test_ls)
    
x = np.linspace(0,len(Train_ls[1]),len(Train_ls[1]))

plt.figure(figsize=(10,8))

for i in range(0,len(Lamda)):
    plt.plot(x,Train_ls[i],label= f'L2_Regularization:{Lamda [i]}',linewidth=1.5)
    
    plt.xlabel('different epoch')
    
    plt.ylabel('loss')
    
plt.legend(loc=2, bbox_to_anchor=(1.1,1.0),borderAxesPad = 0.)

plt.title('train loss with L2_penalty')

plt.show()

运行结果:

疑问和心得:

  1. 画图的实现和细节还是有些模糊。
  2. 正则化系数一般是一个可以根据算法有一定变动的常数。
  3. 前馈神经网络中,二分类最后使用logistic函数返回,多分类一般返回softmax值,若是一般的回归任务,一般是直接relu返回。
  4. 前馈神经网络的实现,从物理层上应该是全连接的,但是网上的代码一般都是两层单个神经元,这个容易产生误解。个人感觉,还是要使用nn封装的函数比较正宗。
相关推荐
大得3696 分钟前
django生成迁移文件,执行生成到数据库
后端·python·django
大志说编程13 分钟前
LangChain框架入门17: 手把手教你创建LLM工具
python·langchain·ai编程
R-G-B41 分钟前
【P38 6】OpenCV Python——图片的运算(算术运算、逻辑运算)加法add、subtract减法、乘法multiply、除法divide
人工智能·python·opencv·图片的运算·图片加法add·图片subtract减法·图片乘法multiply
数据智能老司机1 小时前
MCP 实战——全局视角:为什么 MCP 将成为 AI 的颠覆者
python·llm·mcp
在星空下1 小时前
Fastapi-Vue3-Admin
前端·python·fastapi
cxyll12341 小时前
从接口自动化测试框架设计到开发(三)主流程封装、返回数据写入excel
前端·python·excel
Kyln.Wu1 小时前
【python实用小脚本-190】Python一键删除PDF任意页:输入页码秒出干净文件——再也不用在线裁剪排队
服务器·python·pdf
重启的码农1 小时前
llama.cpp 分布式推理介绍(2) 后端注册机制 (Backend Registration)
c++·人工智能·神经网络
重启的码农1 小时前
llama.cpp 分布式推理介绍(1) 远程计算设备 (RPC Device)
c++·人工智能·神经网络
九章云极AladdinEdu2 小时前
Scikit-learn通关秘籍:从鸢尾花分类到房价预测
人工智能·python·机器学习·分类·scikit-learn·gpu算力