【PyTorch】常用网络层layers总结

news2024/9/23 11:24:36

文章目录

  • 前言
  • 一、Convolution Layers
  • 二、Pooling Layers
  • 三、Padding Layers
  • 总结

前言

PyTorch中网络搭建主要是通过调用layers实现的,这篇文章总结了putorch中最常用的几个网络层接口及其参数。


一、Convolution Layers

pytorch官方文档介绍了众多卷积层算法,以最新的pytorch2.4为例,针对处理的数据维度不同,有如下卷积层layers:

卷积层名描述
nn.Conv1dApplies a 1D convolution over an input signal composed of several input planes.
nn.Conv2dApplies a 2D convolution over an input signal composed of several input planes.
nn.Conv3dApplies a 3D convolution over an input signal composed of several input planes.
nn.ConvTranspose1dApplies a 1D transposed convolution operator over an input image composed of several input planes.
nn.ConvTranspose2dApplies a 2D transposed convolution operator over an input image composed of several input planes.
nn.ConvTranspose3dApplies a 3D transposed convolution operator over an input image composed of several input planes.
…………
这里以使用最多的二维卷积为例,介绍卷积层的基本算法。
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)

Parameters:

  • in_channels (int) – 输入通道数
  • out_channels (int) – 输出通道数
  • kernel_size (int or tuple) – 卷积核大小
  • stride (int or tuple, optional) – 卷积步长
  • padding (int, tuple or str, optional) – 对于输入图像的四周进行填充的数量进行控制,可指定填充像素数量,也可以指定填充模式,如"same", “valid”
  • padding_mode (str, optional) – 填充类型
  • dilation (int or tuple, optional) – 孔洞卷积的孔洞大小
  • groups (int, optional) – 分组卷积的分组
  • bias (bool, optional) – 是否采用偏置

对于卷积层的输入输出维度换算,官方文档给出了如下公式:

  • 输入尺度: ( N , C i n , H i n , W i n ) (N,C_{in},H_{in},W_{in}) (N,Cin,Hin,Win)
  • 输出尺度: ( N , C o u t , H o u t , W o u t ) (N,C_{out},H_{out},W_{out}) (N,Cout,Hout,Wout)
  • 换算关系:
    H o u t = ⌊ H i n + 2 × padding [ 0 ] − dilation [ 0 ] × ( kernel size [ 0 ] − 1 ) − 1 stride [ 0 ] + 1 ⌋ W o u t = ⌊ W i n + 2 × padding [ 1 ] − dilation [ 1 ] × ( kernel size [ 1 ] − 1 ) − 1 stride [ 1 ] + 1 ⌋ H_{out}=\left\lfloor\frac{H_{in}+2\times\text{padding}[0]-\text{dilation}[0]\times(\text{kernel size}[0]-1)-1}{\text{stride}[0]}+1\right\rfloor\\W_{out}=\left\lfloor\frac{W_{in}+2\times\text{padding}[1]-\text{dilation}[1]\times(\text{kernel size}[1]-1)-1}{\text{stride}[1]}+1\right\rfloor Hout=stride[0]Hin+2×padding[0]dilation[0]×(kernel size[0]1)1+1Wout=stride[1]Win+2×padding[1]dilation[1]×(kernel size[1]1)1+1

kernel_size可以以int或tuple传入,前者时公式中的kernel size[0]和kernel size[1]都为传参的int值,其他参数也类似。


二、Pooling Layers

池化层的作用是信息降维,针对二维信息,pytorch提供了以下池化算法

池化层名描述
nn.MaxPool2dApplies a 2D max pooling over an input signal composed of several input planes.
nn.MaxUnpool2dComputes a partial inverse of MaxPool2d.
nn.AvgPool2dApplies a 2D average pooling over an input signal composed of several input planes.
nn.FractionalMaxPool2dApplies a 2D fractional max pooling over an input signal composed of several input planes.
nn.LPPool2dApplies a 2D power-average pooling over an input signal composed of several input planes.
nn.AdaptiveMaxPool2dApplies a 2D adaptive max pooling over an input signal composed of several input planes.
nn.AdaptiveAvgPool2dApplies a 2D adaptive average pooling over an input signal composed of several input planes.

这里以常见的最大池化maxpool为例介绍池化层(其他类似)

torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

Parameters:

  • kernel_size (Union[int, Tuple[int, int]]) – 池化窗口大小
  • stride (Union[int, Tuple[int, int]]) – 池化步长
  • padding (Union[int, Tuple[int, int]]) – 填充大小
  • dilation (Union[int, Tuple[int, int]]) – 空洞大小
  • return_indices (bool) – 是否返回最大值所在位置
  • ceil_mode (bool) – 如果无法整除,选择向下取整还是向上取整,默认向下取整

池化层的输出大小计算和卷积是一样的。

  • 输入尺度: ( N , C i n , H i n , W i n ) (N,C_{in},H_{in},W_{in}) (N,Cin,Hin,Win)
  • 输出尺度: ( N , C o u t , H o u t , W o u t ) (N,C_{out},H_{out},W_{out}) (N,Cout,Hout,Wout)
  • 换算关系:
    H o u t = ⌊ H i n + 2 × padding [ 0 ] − dilation [ 0 ] × ( kernel size [ 0 ] − 1 ) − 1 stride [ 0 ] + 1 ⌋ W o u t = ⌊ W i n + 2 × padding [ 1 ] − dilation [ 1 ] × ( kernel size [ 1 ] − 1 ) − 1 stride [ 1 ] + 1 ⌋ H_{out}=\left\lfloor\frac{H_{in}+2\times\text{padding}[0]-\text{dilation}[0]\times(\text{kernel size}[0]-1)-1}{\text{stride}[0]}+1\right\rfloor\\W_{out}=\left\lfloor\frac{W_{in}+2\times\text{padding}[1]-\text{dilation}[1]\times(\text{kernel size}[1]-1)-1}{\text{stride}[1]}+1\right\rfloor Hout=stride[0]Hin+2×padding[0]dilation[0]×(kernel size[0]1)1+1Wout=stride[1]Win+2×padding[1]dilation[1]×(kernel size[1]1)1+1

三、Padding Layers

pytorch提供了ReflectionPadReplicationPadZeroPadConstantPadCircularPad多种padding策略。以二维信号处理为例,各自的介绍如下:

填充层名描述
nn.ReflectionPad2dPads the input tensor using the reflection of the input boundary.
nn.ReplicationPad2dPads the input tensor using replication of the input boundary.
nn.ZeroPad2dPads the input tensor boundaries with zero.
nn.ConstantPad2dPads the input tensor boundaries with a constant value.
nn.CircularPad2dPads the input tensor using circular padding of the input boundary.

padding层代码比较简单,只有一个参数需要传递。以nn.ZeroPad2d为例:

torch.nn.ZeroPad2d(padding)

Parameters:

  • padding (int, tuple) – the size of the padding. If is int, uses the same padding in all boundaries. If a 4-tuple, uses (padding_left, padding_right, padding_top, padding_bottom)
    当padding参数传入整数,则在张量四周都填充相同维度的数值,如果以有四个元素的元组传入,则元组各元素分别控制左边、右边、上边、下边的维度。

不妨看一下官网示例:

>>> m = nn.ZeroPad2d(2)
>>> input = torch.randn(1, 1, 3, 3)
>>> input
tensor([[[[-0.1678, -0.4418,  1.9466],
          [ 0.9604, -0.4219, -0.5241],
          [-0.9162, -0.5436, -0.6446]]]])
>>> m(input)
tensor([[[[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000, -0.1678, -0.4418,  1.9466,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.9604, -0.4219, -0.5241,  0.0000,  0.0000],
          [ 0.0000,  0.0000, -0.9162, -0.5436, -0.6446,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000]]]])
>>> # using different paddings for different sides
>>> m = nn.ZeroPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000, -0.1678, -0.4418,  1.9466,  0.0000],
          [ 0.0000,  0.9604, -0.4219, -0.5241,  0.0000],
          [ 0.0000, -0.9162, -0.5436, -0.6446,  0.0000]]]])

总结

本文对pytorch使用最多的layers进行了介绍,重点介绍了网络层接口以及参数。由于篇幅限制,其余众多网络层建议查看PyTorch官方文档学习。
在这里插入图片描述

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

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

相关文章

017.PL-SQL编程—函数

我 的 个 人 主 页:👉👉 失心疯的个人主页 👈👈 入 门 教 程 推 荐 :👉👉 Python零基础入门教程合集 👈👈 虚 拟 环 境 搭 建 :👉&…

网上花店管理系统小程序的设计

管理员账户功能包括:系统首页,个人中心,管理员管理,客服聊天管理,基础数据管理,论坛交流管理,公告信息管理,用户管理,轮播图信息 微信端账号功能包括:系统首…

Shader 渲染路径

实际的游戏开发中,场景中的光源肯定是更多、更复杂的,如果只有一个平行光的处理,完全不能满足需求。处理更多的光源,我们就需要了解Unity底层是如何处理这些光源的。 1、渲染路径是什么 渲染路径(Rendering Path&…

51单片机个人学习笔记11(AT24C02-I2C总线)

前言 本篇文章属于STC89C52单片机(以下简称单片机)的学习笔记,来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记,只能做参考,细节方面建议观看视频,肯定受益匪浅。 [1-1] 课程简介_哔哩…

海外域名有多好用?怎么购买海外域名?Namecheap+虚拟卡购买步骤详解

前言 为什么要用海外域名? 首先,博主要介绍一下为什么要使用海外域名,使用国内的域名不好吗?主要是出于以下几个原因供大家参考,主要是涵盖了品牌保护、市场拓展、灵活性和创新性等多个方面: 1.品牌保护&…

智慧环保解决方案

1. 建设背景与环境问题 随着经济和工业的快速发展,环境问题如污染和资源短缺日益严重,引起了国家层面的高度重视。政策密集出台,旨在加强大气污染防治,推动区域联合防治,实施大气污染物和温室气体的协同控制。 2. 环…

springboot 整合swagger

没有多余废话&#xff0c;就是干 spring-boot 2.7.8 springfox-boot-starter 3.0.0 结构 POM.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/…

PhotoZoom Pro 9.0.4中文特别版软件介绍

PhotoZoom Pro 9.0.4中文特别版软件介绍 PhotoZoom Pro 9.0.4中文特别版是一个十分强大的图片无损放大(图片放大不失真)软件。 它是一款采用国际领先插值算法的新颖的、技术上具有革命性的对数码图片无损放大的工具。 一般情况我们用通常的工具对数码图片进行放大时&#xff…

kali2023安装docker

在root用户下运行 先运行更新&#xff0c;然后升级 apt update apt upgrade -y 安装docker依赖包 apt install apt-transport-https ca-certificates curl software-properties-common -y 添加docker官方的GPG密钥 echo deb https://download.docker.com/linux/debian s…

华为OD机试真题 - 考古学家 - 递归(Python/JS/C/C++ 2024 D卷 200分)

华为OD机试 2024E卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试真题&#xff08;Python/JS/C/C&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;私信哪吒&#xff0c;备注华为OD&#xff0c;加入华为OD刷题交流群&#xff0c;…

检查Index对象是否单调递减pandas.Index.is_monotonic_decreasing

【小白从小学Python、C、Java】 【考研初试复试毕业设计】 【Python基础AI数据分析】 检查Index对象是否单调递减 pandas.Index.is_monotonic_decreasing [太阳]选择题 题目代码中输出结果是&#xff1f; import pandas as pd idx1 pd.Index([1, 2, 3, 4, 5]) idx2 pd.Index…

抢鲜体验 PolarDB PG 15 开源版

unsetunsetPolarDB 商业版unsetunset 8 月&#xff0c;PolarDB PostgreSQL 版兼容 PostgreSQL 15 版本&#xff08;商业版&#xff09;正式发布上线。 当前版本主要增强优化了以下方面&#xff1a; 改进排序功能&#xff1a;改进内存和磁盘排序算法。 增强SQL功能&#xff1a;支…

SprinBoot+Vue餐饮连锁店管理系统的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍&#xff1a;CSDN认证博客专家&#xff0c;CSDN平台Java领域优质…

基于聚类与LSTM对比特币价格深度分析与预测

1.项目背景 比特币作为全球最具影响力的加密货币之一&#xff0c;其价格受到多种复杂因素的共同作用&#xff0c;包括市场情绪、政策变化、大型机构的投资行为等&#xff0c;这些因素在不同的市场阶段对比特币价格波动产生直接或间接的影响。通过对比特币市场的深入分析&#…

在java中使用网易邮箱发送邮件保姆级教程,从零开始,完美避开各种坑

背景,开源工作流引擎AntFlow,gitcode地址通知系统需要配置一个邮箱地址才能供用户展示发送邮件通知功能.最初在开发阶段我使用的是个人163邮箱,本来没什么问题.然而当注册一个新的网易邮箱并在配置里替换掉原来个人邮箱后,竟然出现535 Error: authentication failed 异常,经过网…

【orin-nx Linux下创建简单C++项目 CMake构建编译系统】

【注意】&#xff1a;需要安装gcc 和 cmake 安装视频 #.sh 文件添加权限 chmod x cmake-3.30.3-linux-aarch64.sh1、在root下创建一个文件夹testaubo 2、在testaubo文件夹下创建5个文件夹以及一个cmake文件 2.1、【src】 文件夹存放C的 .cpp文件2.2、【include】 文件夹存…

【计算机网络】TCP协议(下)

上篇我们介绍了三次握手四次挥手&#xff0c;这次继续来进行tcp的介绍。 1 TINE_WAIT与CLOSE_WAIT 我们先使用客户端作为左端&#xff0c;服务端作为右方 当我们客户端close时&#xff0c;就会发起两次挥手&#xff0c;此时服务端就会进入CLOSE_WAIT状态&#xff0c;只要服务端…

链表算法题(下)

在链表算法题&#xff08;上&#xff09;长中我们已经学习了一系列的链表算法题&#xff0c;那么在本篇中我们将继续来学习链表的算法题&#xff0c;接下来就继续来破解链表的算法题吧&#xff01; 1.相交链表 160. 相交链表 - 力扣&#xff08;LeetCode&#xff09; 通过以上…

基于yolov8的人脸检测计数系统python源码+onnx模型+评估指标曲线+精美GUI界面

【算法介绍】 基于YOLOv8的人脸检测计数系统是一种利用深度学习技术的先进解决方案&#xff0c;它以其高效、准确的特点在人脸检测领域脱颖而出。该系统以YOLOv8为核心算法&#xff0c;该算法作为YOLO系列的最新迭代&#xff0c;不仅继承了前代算法的优点&#xff0c;还在实时…

YOLOv8-obb训练自己的数据集

一、YOLO OBB 格式 YOLO OBB 格式通过四个角点指定边界框&#xff0c;其坐标在 0 和 1 之间归一化&#xff1a; class_index x1 y1 x2 y2 x3 y3 x4 y4 YOLO 在内部处理损失和产出。 xywhr 格式&#xff0c;表示边界框的中心点&#xff08;xy&#xff09;、宽度、高度和旋转角度…