21 线性层及其他层介绍

一、正则化层

加快神经网络的训练速度

image

  • BATCHNORM2D

image

C:代表channel 需要输入
image

image

二、Recurrent层

用于文字识别

image

三、Transformer层

image

四、线性层

使用比较多

image

image

image

  • 举例:
    image

  • 代码

点击查看代码
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
import torch
# 测试集

test_data=torchvision.datasets.CIFAR10(root="./CIFAR10_dataset",transform=torchvision.transforms.ToTensor(),train=False,download=True)
test_loader=DataLoader(dataset=test_data,batch_size=64)

#模型定义
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()

for data in test_loader:
    imgs,targets=data
    print(imgs.shape) #torch.Size([64, 3, 32, 32])

    # output=torch.reshape(imgs,(1,1,1,-1))#-1表示宽随大小自由设定
    # print(output.shape) #torch.Size([1, 1, 1, 196608])

    #展平输入向量
    output=torch.flatten(imgs)
    print(output.shape) #torch.Size([196608])
    #调用模型
    output=tudui(output)
    print(output.shape) #torch.Size([10])

  • 运行效果
    image

五、Dropout层

随机地将input的tensor中的一些元素变为0(会设定一个概率为p)
防止过拟合

image

六、Embedding层

用于自然语言处理当中

七、重点讲解

  • 棕色是在一个特定网络中一起使用
  • 本章重点讲解线性层,其他方法比较简单

image

八、pytorch中自带的模型

torchvision->torchvision.models(版本0.9.0)
image

  • 人脸检测+姿态检测
    image
posted @ 2022-05-22 21:36  Trouvaille_fighting  阅读(273)  评论(0)    收藏  举报