Dropout正则化:提升PyTorch模型性能的神器!

news2024/9/22 15:30:35

本文展示了在PyTorch模型中添加Dropout正则化如何影响模型在损失和准确率方面的性能。

Dropout正则化在机器学习中的意义是什么?

Dropout正则化是机器学习中的一种方法,通过在神经网络中随机丢弃一些单元(神经元)来模拟同时训练多个网络架构的效果,这对于减少训练过程中的过拟合风险至关重要。

在PyTorch模型中集成Dropout

要在PyTorch模型中集成Dropout正则化,可以方便地使用torch.nn.Dropout类。

这个类需要一个输入参数,即dropout率,它表示神经元被关闭(即不参与训练)的概率。

Dropout可以应用于任何非输出层之后。

self.dropout = nn.Dropout(0.25)

观察Dropout对模型性能的影响

为了研究Dropout的效果,我们将训练一个用于图像分类的模型。

首先,我们将训练一个包含Dropout正则化的网络,然后训练一个不包含Dropout正则化的网络。

两个模型都将在Cifar-10数据集上进行8个周期的训练。

步骤 1: 导入必要的依赖和库
import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
import torchvision
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

import matplotlib.pyplot as plt
import numpy as np

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
步骤 2: 加载数据集并准备DataLoader
BATCH_SIZE = 32

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root="./data", train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root="./data", train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE,
                                         shuffle=False, num_workers=2)

CLASS_NAMES = ("plane", "car", "bird", "cat",
               "deer", "dog", "frog", "horse", "ship", "truck")
步骤 3: 加载并可视化部分数据
def show_batch(image_batch, label_batch):
  plt.figure(figsize=(10,10))
  for n in range(25):
      ax = plt.subplot(5,5,n+1)
      img = image_batch[n] / 2 + 0.5     # unnormalize
      img = img.numpy()
      plt.imshow(np.transpose(img, (1, 2, 0)))
      plt.title(CLASS_NAMES[label_batch[n]])
      plt.axis("off")
sample_images, sample_labels = next(iter(trainloader))
show_batch(sample_images, sample_labels)

输出:

步骤 4: 实现包含Dropout正则化的网络
class Net(nn.Module):
  def __init__(self, input_shape=(3,32,32)):
    super(Net, self).__init__()
    
    self.conv1 = nn.Conv2d(3, 32, 3)
    self.conv2 = nn.Conv2d(32, 64, 3)
    self.conv3 = nn.Conv2d(64, 128, 3)
    
    self.pool = nn.MaxPool2d(2,2)

    n_size = self._get_conv_output(input_shape)
    
    self.fc1 = nn.Linear(n_size, 512)
    self.fc2 = nn.Linear(512, 10)

    self.dropout = nn.Dropout(0.25)

  def _get_conv_output(self, shape):
    batch_size = 1
    input = torch.autograd.Variable(torch.rand(batch_size, *shape))
    output_feat = self._forward_features(input)
    n_size = output_feat.data.view(batch_size, -1).size(1)
    return n_size

  def _forward_features(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = self.pool(F.relu(self.conv3(x)))
    return x
      
  def forward(self, x):
    x = self._forward_features(x)
    x = x.view(x.size(0), -1)
    x = self.dropout(x)
    x = F.relu(self.fc1(x))
    x = self.dropout(x)
    x = self.fc2(x)
    return x
步骤 5: 实现训练过程
def train(model, device, train_loader, optimizer, criterion, epoch, steps_per_epoch=20):
  # Switch model to training mode. This is necessary for layers like dropout, batchnorm etc which behave differently in training and evaluation mode
  model.train()

  train_loss = 0
  train_total = 0
  train_correct = 0

  # We loop over the data iterator, and feed the inputs to the network and adjust the weights.
  for batch_idx, (data, target) in enumerate(train_loader, start=0):
    
    # Load the input features and labels from the training dataset
    data, target = data.to(device), target.to(device)
    
    # Reset the gradients to 0 for all learnable weight parameters
    optimizer.zero_grad()
    
    # Forward pass: Pass image data from training dataset, make predictions about class image belongs to (0-9 in this case)
    output = model(data)
    
    # Define our loss function, and compute the loss
    loss = criterion(output, target)
    train_loss += loss.item()

    scores, predictions = torch.max(output.data, 1)
    train_total += target.size(0)
    train_correct += int(sum(predictions == target))
            
    # Reset the gradients to 0 for all learnable weight parameters
    optimizer.zero_grad()

    # Backward pass: compute the gradients of the loss w.r.t. the model"s parameters
    loss.backward()
    
    # Update the neural network weights
    optimizer.step()

  acc = round((train_correct / train_total) * 100, 2)
  print("Epoch [{}], Loss: {}, Accuracy: {}".format(epoch, train_loss/train_total, acc), end="")
步骤 6: 实现测试函数
def test(model, device, test_loader, criterion, classes):
  # Switch model to evaluation mode. This is necessary for layers like dropout, batchnorm etc which behave differently in training and evaluation mode
  model.eval()

  test_loss = 0
  test_total = 0
  test_correct = 0

  example_images = []
  with torch.no_grad():
      for data, target in test_loader:
          # Load the input features and labels from the test dataset
          data, target = data.to(device), target.to(device)
          
          # Make predictions: Pass image data from test dataset, make predictions about class image belongs to (0-9 in this case)
          output = model(data)
          
          # Compute the loss sum up batch loss
          test_loss += criterion(output, target).item()
          
          scores, predictions = torch.max(output.data, 1)
          test_total += target.size(0)
          test_correct += int(sum(predictions == target))

  acc = round((test_correct / test_total) * 100, 2)
  print(" Test_loss: {}, Test_accuracy: {}".format(test_loss/test_total, acc))
步骤 7: 初始化网络的损失函数和优化器
net = Net().to(device)
print(net)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters())

输出:

Net(
  (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
  (conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1))
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (fc1): Linear(in_features=512, out_features=512, bias=True)
  (fc2): Linear(in_features=512, out_features=10, bias=True)
  (dropout): Dropout(p=0.25, inplace=False)
)
步骤 8: 开始训练
ffor epoch in range(8):
  train(net, device, trainloader, optimizer, criterion, epoch)
  test(net, device, testloader, criterion, CLASS_NAMES)

print("Finished Training")

输出:

Epoch [0], Loss: 0.0461193907892704, Accuracy: 45.45 Test_loss: 0.036131812924146654, Test_accuracy: 58.58
Epoch [1], Loss: 0.03446852257728577, Accuracy: 60.85 Test_loss: 0.03089196290373802, Test_accuracy: 65.27
Epoch [2], Loss: 0.029333480607271194, Accuracy: 66.83 Test_loss: 0.027052838513255118, Test_accuracy: 70.41
Epoch [3], Loss: 0.02650276515007019, Accuracy: 70.21 Test_loss: 0.02630699208676815, Test_accuracy: 70.99
Epoch [4], Loss: 0.024451716771125794, Accuracy: 72.41 Test_loss: 0.024404651895165445, Test_accuracy: 73.03
Epoch [5], Loss: 0.022718262011408807, Accuracy: 74.35 Test_loss: 0.023125074282288553, Test_accuracy: 74.86
Epoch [6], Loss: 0.021408387248516084, Accuracy: 75.76 Test_loss: 0.023151200053095816, Test_accuracy: 74.43
Epoch [7], Loss: 0.02033562403023243, Accuracy: 76.91 Test_loss: 0.023537022879719736, Test_accuracy: 73.93
Finished Training

不含Dropout正则化的网络

接下来,我们将构建相同的网络,但不包含Dropout层。

我们将在相同的数据集和训练周期上训练这个网络,并评估Dropout对网络性能的影响,这里将使用相同的训练和测试函数。

步骤 1: 实现网络
class Net(nn.Module):
  def __init__(self, input_shape=(3,32,32)):
    super(Net, self).__init__()
    
    self.conv1 = nn.Conv2d(3, 32, 3)
    self.conv2 = nn.Conv2d(32, 64, 3)
    self.conv3 = nn.Conv2d(64, 128, 3)
    
    self.pool = nn.MaxPool2d(2,2)

    n_size = self._get_conv_output(input_shape)
    
    self.fc1 = nn.Linear(n_size, 512)
    self.fc2 = nn.Linear(512, 10)

  def _get_conv_output(self, shape):
    batch_size = 1
    input = torch.autograd.Variable(torch.rand(batch_size, *shape))
    output_feat = self._forward_features(input)
    n_size = output_feat.data.view(batch_size, -1).size(1)
    return n_size

  def _forward_features(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = self.pool(F.relu(self.conv3(x)))
    return x
      
  def forward(self, x):
    x = self._forward_features(x)
    x = x.view(x.size(0), -1)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return x
步骤 2: 初始化损失函数和优化器
net = Net().to(device)
print(net)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters())

输出:

Net(
  (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
  (conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1))
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (fc1): Linear(in_features=512, out_features=512, bias=True)
  (fc2): Linear(in_features=512, out_features=10, bias=True)
步骤 3: 开始训练
for epoch in range(8):
  train(net, device, trainloader, optimizer, criterion, epoch)
  test(net, device, testloader, criterion, CLASS_NAMES)

print("Finished Training")

输出:

Epoch [0], Loss: 0.04425342482566833, Accuracy: 48.29 Test_loss: 0.03506121407747269, Test_accuracy: 59.93
Epoch [1], Loss: 0.0317487561249733, Accuracy: 63.97 Test_loss: 0.029791217082738877, Test_accuracy: 66.4
Epoch [2], Loss: 0.026000032302737237, Accuracy: 70.83 Test_loss: 0.027046055325865747, Test_accuracy: 69.97
Epoch [3], Loss: 0.022179243130385877, Accuracy: 75.11 Test_loss: 0.02481114484965801, Test_accuracy: 72.95
Epoch [4], Loss: 0.01933788091301918, Accuracy: 78.26 Test_loss: 0.024382170912623406, Test_accuracy: 73.55
Epoch [5], Loss: 0.016771901984512807, Accuracy: 81.04 Test_loss: 0.024696413831412793, Test_accuracy: 73.53
Epoch [6], Loss: 0.014588635778725148, Accuracy: 83.41 Test_loss: 0.025593858751654625, Test_accuracy: 73.94
Epoch [7], Loss: 0.01255791916936636, Accuracy: 85.94 Test_loss: 0.026889967443048952, Test_accuracy: 73.69
Finished Training

如何学习AI大模型?

大模型时代,火爆出圈的LLM大模型让程序员们开始重新评估自己的本领。 “AI会取代那些行业?”“谁的饭碗又将不保了?”等问题热议不断。

不如成为「掌握AI工具的技术人」,毕竟AI时代,谁先尝试,谁就能占得先机!

但是LLM相关的内容很多,现在网上的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学,学习成本和门槛很高

针对所有自学遇到困难的同学们,我帮大家系统梳理大模型学习脉络,将这份 LLM大模型资料 分享出来:包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 😝有需要的小伙伴,可以 扫描下方二维码领取🆓↓↓↓

👉[CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)]()👈

学习路线

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

在这里插入图片描述

👉学会后的收获:👈

• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。

在这里插入图片描述

1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2117582.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

explicit 的作用(如何避免编译器进行隐式类型转换)

目录 1. 隐式转换(Implicit Conversion) 2. 显式转换(Explicit Conversion) 3. 隐式转换的风险与显式转换的必要性 4. 隐式类型转换的例子 5. explicit 的作用 6. explicit 在构造函数中的作用 7. explicit 适用于转换操作…

常见网络层(卷积、池化、全连接)与其计算

卷积可视化:卷积神经网络 (CNN) 基本原理和公式_cnn公式-CSDN博客 嵌入大小计算规则【注意评论区全连接层计算规则勘误:Pv Wv Bv】 Conv、MaxPool、FC 池化操作和conv在kernels上计算不一致,在图像尺寸上的规则都是一样的。 公式可以理解…

解决windows远程桌面连接报错:这可能由于GredSSP加密数据库修正

这种问题需要修改一些远程端的安全级别 1.使用快捷键winr打开运行窗口 ------- 输入‘gpedit.msc’ 2.依次展开“计算机配置”->“管理模板”->“系统”->“凭据分配”设置名称: 加密数据库修正 3.双击“加密数据库修正”,将状态改为“启用”&a…

LDRA Testbed(TBrun)软件单元测试_实例讲解(对多次调用的函数打桩)

系列文章目录 LDRA Testbed软件静态分析_操作指南 LDRA Testbed软件静态分析_自动提取静态分析数据生成文档 LDRA Testbed软件静态分析_Jenkins持续集成(自动静态分析并用邮件自动发送分析结果) LDRA Testbed软件静态分析_软件质量度量 LDRA Testbed软件…

YOLOv10改进:CA注意力机制【注意力系列篇】(附详细的修改步骤,以及代码,目标检测效果优于SE和CBAM注意力)

YOLOv10改进:CA注意力机制【注意力系列篇】(附详细的修改步骤,以及代码) 如果实验环境尚未搭建成功,可以参考这篇文章 ->【YOLOv10超详细环境搭建以及模型训练(GPU版本)】 文章链接为&…

电脑pe是什么意思_电脑pe系统作用详细分析

有些小白很好奇,电脑pe是什么意思?所谓的电脑pe系统其实就是当我们的电脑出现问题而不能进入正常系统时候的一种“紧急备用”系统。如果需要重装操作系统的话,以往采用光盘使用的比较多,随着技术的进步,用u盘制作一个pe启动盘去安…

Kubernetes 系列 | k8s入门运维

目录 一、K8S集群搭建1.1 部署方式1.2 了解kubeadm1.3 部署流程1.3.1 初始化配置1.3.2 安装容器运行时1.3.3 安装K8S软件包1.3.4 创建集群 二、集群高可用1.1 集群高可用-堆叠1.2 集群高可用-集群外etcd 三、Pod运维3.1 Pod运维3.2 Pod的生命周期3.3 Pod状况3.4 Pod阶段3.5 容器…

【C++前缀和】3212. 统计 X 和 Y 频数相等的子矩阵数量|1672

本文涉及的基础知识点 C算法:前缀和、前缀乘积、前缀异或的原理、源码及测试用例 包括课程视频 LeetCode3212. 统计 X 和 Y 频数相等的子矩阵数量 难度分:1672 给你一个二维字符矩阵 grid,其中 grid[i][j] 可能是 ‘X’、‘Y’ 或 ‘.’&a…

中秋佳节,科技好礼献团圆!五款数码好物大推荐

中秋节的到来,总是让人充满了对团圆和美好生活的向往。在这个家人团聚、朋友欢聚的美好时刻,除了享受月饼和团圆饭,还可以通过一些先进的数码好物,提升节日的愉悦感和生活的舒适度。从提升运动体验的南卡Runner Pro5,到…

Windows系统 安装 Nacos

使用说明:安装 Nacos 服务端,以单机模式启动,不用集群。 一、下载Nacos Nacos Server 下载 | Nacos 官网 下载解压后: bin:启动脚本;conf:配置文件 这个文件可以修改端口、增加数据库连接等配…

从零到一:为未上架APP开通微信商户支付服务的步骤详解

随着移动支付的普及,微信商户支付已成为众多商家不可或缺的一部分。然而,对于某些仍处于开发阶段或特殊需求下的APP来说,它们可能尚未在应用商店上架,但仍需接入微信支付功能以满足用户需求。本文将为您提供一份详细的微信商户支付…

2024年印尼金融科技报告解读(1) | 印尼金融科技发展现状与挑战

概述 金融科技的创新正加速全球金融普及和经济增长,尤其在东南亚,其影响尤为显著。 印尼作为该区域的经济龙头,凭借其庞大的人口基数和独特的地理位置,对"非接触式"服务和中小企业融资的需求迅猛增长,成为数…

mp3和mp4区别是什么?音视频转换,用这2个工具就够了

在刷视频的时候,在看电影的时候,在听歌的时候……我们经常能听到“mp3”、“mp4”这两种格式的名字。它们是什么格式?mp3和mp4区别是什么?我们能做什么? mp3是一种流行的音频编码格式,以其高效的压缩算法著…

屏幕空间UV 警戒线

屏幕空间UV 警戒线 屏幕UV已经抗锯齿

ueditor抓取图片

在ueditor.all.js文件中 修改catchremoteimage方法 原来的: 修改后: function catchremoteimage(imgs, callbacks) { //创建一个请求var oReq = new XMLHttpRequest() //获取请求地址前缀,根据自身项目获取const baseURL = http://uat.********* //url为请求地址根路径+具…

openGauss 数据库管理工具:DataKit 6.0 部署与配置实战

目录 一、DataKit简述 二、部署概要 三、系统规划 四、软件环境准备 1、关闭防火墙 2、配置时间同步 3、禁用 SELinux 4、关闭RemoveIPC 5、字符集设置 6、安装依赖包 五、openGauss DataKit部署与配置 1、openGauss 安装与配置 1.openGauss参数配置 2.重启 open…

React Native 0.76,New Architecture 将成为默认模式,全新的 RN 来了

关于 React Native 的 New Architecture 概念,最早应该是从 2018 年 RN 团队决定重写大量底层实现开始,因为那时候 React Native 面临各种结构问题和性能瓶颈,最终迫使 RN 团队开始进行重构。 而从 React Native 0.68 开始,New A…

第三次去银行办事,核心是犯了抓不住重点这个毛病

手机银行不小心输错了两次密码,然后就限制了交易,只能在柜台操作。 由此引发了比如提示密码错误、定期转活期、转账等功能的异常。 前两次去银行,竟然只是去解决了这些附带问题。 核心问题是限制非柜面交易啊。 哎 这就是抓不住重点&…

数据结构-堆-详解

数据结构-堆-详解 1.性质大根堆小根堆 2.实现2.1struct Heap、HeapInit、HeapDestroy2.2HeapPushAdjustUp 2.3HeapPopAdjustDown 2.4HeapTop、HeapSize、HeapEmpty 3.应用3.1堆排建堆排序 3.2TopK问题 1.性质 堆是一种特殊的完全二叉树,其父节点总是不大于/不小于 …

手把手教你使用亚马逊云服务器创建EC2实例

陈老老老板🤴 🧙‍♂️本文专栏:生活(主要讲一下自己生活相关的内容)生活就像海洋,只有意志坚强的人,才能到达彼岸。 🧙‍♂️本文简述:如何使用亚马逊云服务器创建EC2实例。 🧙‍♂…