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

相关推荐
大写-凌祁3 小时前
零基础入门深度学习:从理论到实战,GitHub+开源资源全指南(2025最新版)
人工智能·深度学习·开源·github
焦耳加热3 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
深空数字孪生4 小时前
储能调峰新实践:智慧能源平台如何保障风电消纳与电网稳定?
大数据·人工智能·物联网
wan5555cn4 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
格林威5 小时前
机器视觉检测的光源基础知识及光源选型
人工智能·深度学习·数码相机·yolo·计算机视觉·视觉检测
今天也要学习吖5 小时前
谷歌nano banana官方Prompt模板发布,解锁六大图像生成风格
人工智能·学习·ai·prompt·nano banana·谷歌ai
Hello123网站5 小时前
glean-企业级AI搜索和知识发现平台
人工智能·产品运营·ai工具
AKAMAI5 小时前
Queue-it 为数十亿用户增强在线体验
人工智能·云原生·云计算
索迪迈科技5 小时前
INDEMIND亮相2025科技创变者大会,以机器人空间智能技术解锁具身智能新边界
人工智能·机器人·扫地机器人·空间智能·陪伴机器人
栒U5 小时前
一文从零部署vLLM+qwen0.5b(mac本地版,不可以实操GPU单元)
人工智能·macos·vllm