19.神经网络 - 线性层及其他层介绍

embedded/2024/10/22 15:35:12/

神经网络 - 线性层及其他层介绍

1.批标准化层–归一化层(不难,自学看官方文档)

Normalization Layers

torch.nn — PyTorch 1.10 documentation

BatchNorm2d — PyTorch 1.10 documentation

对输入采用Batch Normalization,可以加快神经网络的训练速度

CLASS torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None)
# num_features C-输入的channel

image-20240808153449423

# With Learnable Parameters
m = nn.BatchNorm2d(100)
# Without Learnable Parameters
m = nn.BatchNorm2d(100, affine=False)  # 正则化层num_feature等于channel,即100
input = torch.randn(20, 100, 35, 45)   #batch_size=20,100个channel,35x45的输入
output = m(input)

image-20240808153318952

2.Recurrent Layers(特定网络中使用,自学)

RNN、LSTM等,用于文字识别中,特定的网络结构

torch.nn — PyTorch 1.13 documentation

image-20240808151920684

3.Transformer Layers(特定网络中使用,自学)

特定网络结构

torch.nn — PyTorch 1.13 documentation

image-20240808151936485

4.Linear Layers–线性层(本节讲解)–使用较多

网站地址:Linear — PyTorch 1.10 documentation

img

d代表特征数,L代表神经元个数 K和b在训练过程中神经网络会自行调整,以达到比较合理的预测

image-20240808152000232

image-20240808152017760

下面以一个简单的网络结果VGG16模型为例

5.代码实例 vgg16 model

img

flatten 摊平

torch.flatten — PyTorch 1.10 documentation

# Example
>>> t = torch.tensor([[[1, 2],[3, 4]],[[5, 6],[7, 8]]])   #3个中括号,所以是3维的
>>> torch.flatten(t)  #摊平
tensor([1, 2, 3, 4, 5, 6, 7, 8])
>>> torch.flatten(t, start_dim=1)  #变为1行
tensor([[1, 2, 3, 4],[5, 6, 7, 8]])
  • reshape():可以指定尺寸进行变换
  • flatten():变成1行,摊平
output = torch.flatten(imgs)
# 等价于
output = torch.reshape(imgs,(1,1,1,-1))for data in dataloader:imgs,targets = dataprint(imgs.shape)  #torch.Size([64, 3, 32, 32])output = torch.reshape(imgs,(1,1,1,-1))  # 想把图片展平print(output.shape)  # torch.Size([1, 1, 1, 196608])output = tudui(output)print(output.shape)  # torch.Size([1, 1, 1, 10])for data in dataloader:imgs,targets = dataprint(imgs.shape)  #torch.Size([64, 3, 32, 32])output = torch.flatten(imgs)   #摊平print(output.shape)   #torch.Size([196608])output = tudui(output)print(output.shape)   #torch.Size([10])

我们想实现下面这个:

image-20240808152109358

import torch
import torchvision.datasets
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriterdataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.linear1 = Linear(196608, 10)def forward(self, input):output = self.linear1(input)return outputtudui = Tudui()
writer = SummaryWriter("logs")
step = 0for data in dataloader:imgs, targets = dataprint(imgs.shape)  # torch.Size([64, 3, 32, 32])writer.add_images("input", imgs, step)output = torch.reshape(imgs,(1,1,1,-1))  # 方法一:用reshape把图片拉平,另一种办法直接用torch.flatten(imgs)摊平# print(output.shape)  # torch.Size([1, 1, 1, 196608])# output = tudui(output)# print(output.shape)  # torch.Size([1, 1, 1, 10])#output = torch.flatten(imgs)  #方法二 摊平print(output.shape)  # torch.Size([196608])output = tudui(output)print(output.shape)  # torch.Size([10])writer.add_images("output", output, step)step = step + 1

image-20240808161700359

运行后在 terminal 里输入:

tensorboard --logdir=logs

运行结果如下:

image-20240808161529487

6.Dropout Layers(不难,自学)

Dropout — PyTorch 1.10 documentation

在训练过程中,随机把一些 input(输入的tensor数据类型)中的一些元素变为0,变为0的概率为p

目的:防止过拟合

image-20240808152145889

7.Sparse Layers(特定网络中使用,自学)

Embedding

Embedding — PyTorch 1.10 documentation

用于自然语言处理

8.Distance Functions

计算两个值之间的误差

torch.nn — PyTorch 1.13 documentation

image-20240808152343889

9. Loss Functions

loss 误差大小

torch.nn — PyTorch 1.13 documentation

image-20240808152404986

  1. pytorch提供的一些网络模型

    图片相关:torchvision torchvision.models — Torchvision 0.11.0 documentation
    分类、语义分割、目标检测、实例分割、人体关键节点识别(姿态估计)等等

    文本相关:torchtext 无
    语音相关:torchaudio torchaudio.models — Torchaudio 0.10.0 documentation

下一节:Container ——> Sequential(序列)

hvision 0.11.0 documentation
分类、语义分割、目标检测、实例分割、人体关键节点识别(姿态估计)等等

文本相关:torchtext   无
语音相关:torchaudio  torchaudio.models — Torchaudio 0.10.0 documentation

下一节:Container ——> Sequential(序列)


http://www.ppmy.cn/embedded/105066.html

相关文章

C# SM2 SM3 SM4 使用

目录 效果 SM2 SM3 SM4 项目 代码 SM2Utils.cs Sm3Utils.cs Sm4Utils.cs 下载 效果 SM2 公钥:04ca3e272e11b5633681cb0fbbfd8c162be08918ce5b644cd33d49c17be8674caf6c20a11de8b65333924dfe7d42246abb4a4c36b663bef1aafc624a35acf4d2b1 私钥:…

Nginx SSL密码短语配置指南:增强负载均衡安全性

在Nginx负载均衡配置中,使用SSL密码短语(也称为SSL密码)为HTTPS连接提供了额外的安全性。SSL密码短语通常用于保护私钥文件,确保只有授权用户才能访问和使用它们。本文将详细介绍如何在Nginx中配置SSL密码短语,包括证书…

Java项目: 基于SpringBoot+mysql大学生租房平台(含源码+数据库+开题报告+毕业论文)

一、项目简介 本项目是一套基于SpringBootmysql大学生租房平台 包含:项目源码、数据库脚本等,该项目附带全部源码可作为毕设使用。 项目都经过严格调试,eclipse或者idea 确保可以运行! 该系统功能完善、界面美观、操作简单、功能…

利用Spring Boot的@Transactional注解保障业务数据的一致性

在现代软件开发中,特别是在分布式系统和微服务架构中,确保数据的一致性是一项至关重要的任务。当应用程序需要处理多个数据库操作时,保证这些操作要么全部成功,要么全部失败(即所谓的原子性),以…

kali——hydra的使用

目录 前言 查看帮助 ​编辑 ssh暴力破解 ftp暴力破解 总结 mysql数据库爆破,参数为mysql mssql数据库爆破,参数为mssql oracle数据库爆破,参数为oracle pgsql数据库爆破,参数为postgresql 远程桌面爆破,参数…

【舍入,取整,取小数,取余数丨Excel 函数】

数学函数 1、Round函数 Roundup函数 Rounddown函数 取整:(Int /Trunc)其他舍入函数: 2、Mod函数用Mod函数提取小数用Mod函数 分奇偶通过身份证号码判断性别 1、Round函数 Roundup函数 Rounddown函数 Round(数字,保留几位小数)(四…

用Python实现学生管理系统!(详细教程)

在文章开始前打个小广告——分享一份Python学习大礼包(激活码安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程)点击领取,100%免费! 学生信息管理系统的开发…

用ChatGPT提升论文质量:改进语法、用词和行文的有效方法

学境思源,一键生成论文初稿: AcademicIdeas - 学境思源AI论文写作 在学术写作中,语法、用词和行文的质量直接影响论文的可读性和学术价值。今天我们将介绍如何利用ChatGPT优化论文的语法结构、改进用词精准度以及提升行文流畅性。帮助写作者…