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

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

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
复制代码
# 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)

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

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

torch.nn --- PyTorch 1.13 documentation

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

特定网络结构

torch.nn --- PyTorch 1.13 documentation

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

网站地址:Linear --- PyTorch 1.10 documentation

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

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

5.代码实例 vgg16 model

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 = data
    print(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 = data
    print(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])

我们想实现下面这个:

复制代码
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 SummaryWriter

dataset = 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 output

tudui = Tudui()
writer = SummaryWriter("logs")
step = 0

for data in dataloader:
    imgs, targets = data
    print(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

运行后在 terminal 里输入:

复制代码
tensorboard --logdir=logs

运行结果如下:

6.Dropout Layers(不难,自学)

Dropout --- PyTorch 1.10 documentation

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

目的:防止过拟合

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

Embedding

Embedding --- PyTorch 1.10 documentation

用于自然语言处理

8.Distance Functions

计算两个值之间的误差

torch.nn --- PyTorch 1.13 documentation

9. Loss Functions

loss 误差大小

torch.nn --- PyTorch 1.13 documentation

  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(序列)

相关推荐
fakaifa2 分钟前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
TuringAcademy4 小时前
AAAI爆款:目标检测新范式,模块化设计封神之作
论文阅读·人工智能·目标检测·论文笔记
The Open Group7 小时前
英特尔公司Darren Pulsipher 博士:以架构之力推动政府数字化转型
大数据·人工智能·架构
Ronin-Lotus7 小时前
深度学习篇---卷积核的权重
人工智能·深度学习
.银河系.7 小时前
8.18 机器学习-决策树(1)
人工智能·决策树·机器学习
敬往事一杯酒哈7 小时前
第7节 神经网络
人工智能·深度学习·神经网络
三掌柜6667 小时前
NVIDIA 技术沙龙探秘:聚焦 Physical AI 专场前沿技术
大数据·人工智能
2502_927161287 小时前
DAY 42 Grad-CAM与Hook函数
人工智能
Hello123网站8 小时前
Flowith-节点式GPT-4 驱动的AI生产力工具
人工智能·ai工具
yzx9910138 小时前
Yolov模型的演变
人工智能·算法·yolo