当前位置: 首页 > news >正文

Pytorch-Padding Layers

文章目录

    • 1.nn.ReflectionPad2d()
    • 2.torch.nn.ReplicationPad2d()
    • 3.torch.nn.ZeroPad2d()
    • 4.torch.nn.ConstantPad2d()


1.nn.ReflectionPad2d()

torch.nn.ReflectionPad2d() 是 PyTorch 中的一个模块,用于执行二维反射填充操作。反射填充是一种常用的填充方法,它在输入张量的边界周围以对称方式复制边缘值来扩展输入的尺寸。

torch.nn.ReflectionPad2d(padding)
"""
参数:padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行反射填充操作的模块。
"""
import torch
import torch.nn as nn# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)# 创建一个反射填充层,指定填充大小为 1
reflection_pad = nn.ReflectionPad2d(1)# 应用反射填充操作
output = reflection_pad(input_tensor)print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[-0.4726, -0.9318, -0.4726,  0.0358, -1.3965,  0.0358],[ 0.6456, -0.0962,  0.6456,  0.1090,  1.0219,  0.1090],[-0.4726, -0.9318, -0.4726,  0.0358, -1.3965,  0.0358],[ 2.1055,  0.1147,  2.1055, -2.0517,  1.6568, -2.0517],[ 0.2304,  0.2164,  0.2304, -0.8812, -0.2803, -0.8812],[ 2.1055,  0.1147,  2.1055, -2.0517,  1.6568, -2.0517]],[[ 0.3650,  0.5981,  0.3650,  0.2102,  1.7905,  0.2102],[-0.0200, -0.0328, -0.0200,  0.2043, -0.0937,  0.2043],[ 0.3650,  0.5981,  0.3650,  0.2102,  1.7905,  0.2102],[ 0.5632,  1.2948,  0.5632, -0.0395,  0.5825, -0.0395],[ 0.7248, -0.5541,  0.7248, -0.2897,  0.5524, -0.2897],[ 0.5632,  1.2948,  0.5632, -0.0395,  0.5825, -0.0395]],[[ 1.3675, -1.0789,  1.3675,  0.1664,  0.4726,  0.1664],[ 0.5879, -1.2825,  0.5879, -0.0681, -1.1589, -0.0681],[ 1.3675, -1.0789,  1.3675,  0.1664,  0.4726,  0.1664],[-0.2540, -0.5877, -0.2540,  0.3633, -0.5502,  0.3633],[-1.0074,  0.4622, -1.0074, -0.0378,  0.8484, -0.0378],[-0.2540, -0.5877, -0.2540,  0.3633, -0.5502,  0.3633]]]])Process finished with exit code 0

2.torch.nn.ReplicationPad2d()

torch.nn.ReplicationPad2d() 是 PyTorch 中的一个模块,用于执行二维复制填充操作。复制填充是一种常用的填充方法,它在输入张量的边界周围以复制边缘值的方式进行填充,以扩展输入的尺寸。

torch.nn.ReplicationPad2d(padding)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行复制填充操作的模块。
"""
import torch
import torch.nn as nn# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)# 创建一个复制填充层,指定填充大小为 1
replication_pad = nn.ReplicationPad2d(1)# 应用复制填充操作
output = replication_pad(input_tensor)print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[-0.8918, -0.8918,  0.8248, -1.1001,  0.5579,  0.5579],[-0.8918, -0.8918,  0.8248, -1.1001,  0.5579,  0.5579],[-0.9271, -0.9271,  0.3101, -2.0943, -0.5726, -0.5726],[ 0.9032,  0.9032, -1.2071, -0.7429,  0.0117,  0.0117],[-0.6993, -0.6993,  0.6873,  0.4220,  0.0609,  0.0609],[-0.6993, -0.6993,  0.6873,  0.4220,  0.0609,  0.0609]],[[-1.0946, -1.0946,  1.0368, -0.6620, -2.5191, -2.5191],[-1.0946, -1.0946,  1.0368, -0.6620, -2.5191, -2.5191],[-0.7896, -0.7896, -0.3133,  1.6266,  0.4542,  0.4542],[ 1.5996,  1.5996,  0.5224, -0.5475,  0.8748,  0.8748],[ 1.0462,  1.0462,  0.4509,  0.7843, -1.5898, -1.5898],[ 1.0462,  1.0462,  0.4509,  0.7843, -1.5898, -1.5898]],[[ 0.4897,  0.4897, -1.4179,  0.7020,  0.1468,  0.1468],[ 0.4897,  0.4897, -1.4179,  0.7020,  0.1468,  0.1468],[-0.9953, -0.9953,  0.4198,  0.7341,  1.1312,  1.1312],[ 0.2715,  0.2715,  0.4392, -0.6059,  0.9667,  0.9667],[-2.0218, -2.0218, -1.4344,  1.3383,  0.0892,  0.0892],[-2.0218, -2.0218, -1.4344,  1.3383,  0.0892,  0.0892]]]])Process finished with exit code 0

3.torch.nn.ZeroPad2d()

torch.nn.ZeroPad2d() 是 PyTorch 中的一个模块,用于执行二维零填充操作。零填充是一种常用的填充方法,它在输入张量的边界周围以零值填充来扩展输入的尺寸。

torch.nn.ZeroPad2d(padding)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行零填充操作的模块。
"""
import torch
import torch.nn as nn# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)# 创建一个零填充层,指定填充大小为 1
zero_pad = nn.ZeroPad2d(2)# 应用零填充操作
output = zero_pad(input_tensor)print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 8, 8])
Output tensor:
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.0000,  0.0000,  1.0102,  1.2083,  1.6256,  0.3018,  0.0000,0.0000],[ 0.0000,  0.0000,  0.2626,  1.6651,  0.8745,  0.6719,  0.0000,0.0000],[ 0.0000,  0.0000, -0.3205, -0.2860,  0.6161,  1.1064,  0.0000,0.0000],[ 0.0000,  0.0000,  0.5857, -0.1732,  1.7060,  1.2323,  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.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.0000,0.0000],[ 0.0000,  0.0000,  0.8972, -0.5355, -0.4680, -0.9288,  0.0000,0.0000],[ 0.0000,  0.0000,  0.0075,  0.5603,  1.7817, -0.8891,  0.0000,0.0000],[ 0.0000,  0.0000,  1.0890,  0.4493, -0.6523, -0.0255,  0.0000,0.0000],[ 0.0000,  0.0000,  0.3344, -1.0198, -1.8107, -1.3935,  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.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.0000,0.0000],[ 0.0000,  0.0000, -1.5016, -0.2146,  0.6888, -1.0898,  0.0000,0.0000],[ 0.0000,  0.0000, -0.7264, -2.0722,  0.0202, -0.4005,  0.0000,0.0000],[ 0.0000,  0.0000, -0.0256,  0.5652, -0.3572,  1.6135,  0.0000,0.0000],[ 0.0000,  0.0000,  0.0191,  1.1539,  1.0094, -0.6924,  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.0000,0.0000]]]])Process finished with exit code 0

4.torch.nn.ConstantPad2d()

torch.nn.ConstantPad2d() 是 PyTorch 中的一个模块,用于执行二维常数填充操作。常数填充是一种常用的填充方法,它在输入张量的边界周围以给定的常数值进行填充,以扩展输入的尺寸。

torch.nn.ConstantPad2d(padding, value)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
value:指定填充的常数值。
返回值:返回一个执行常数填充操作的模块。
"""
import torch
import torch.nn as nn# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)# 创建一个常数填充层,指定填充大小为 1,填充值为 0.5
constant_pad = nn.ConstantPad2d(1, 0.5)# 应用常数填充操作
output = constant_pad(input_tensor)print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],[ 0.5000,  0.1896,  0.6049,  1.2257, -0.0834,  0.5000],[ 0.5000, -1.4060,  1.2950,  0.1327,  0.7166,  0.5000],[ 0.5000, -0.3263,  0.5647,  1.0163, -0.1992,  0.5000],[ 0.5000,  2.1257, -0.0110,  1.0363,  0.8082,  0.5000],[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]],[[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],[ 0.5000,  0.2015, -0.7174, -0.1867,  0.0453,  0.5000],[ 0.5000,  0.3571, -2.7764,  1.8158, -0.3598,  0.5000],[ 0.5000, -0.9038,  0.7168, -1.8930, -0.1428,  0.5000],[ 0.5000, -0.3898, -0.3794, -1.7476,  0.2262,  0.5000],[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]],[[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],[ 0.5000,  1.9865,  1.1290, -0.5834, -1.1631,  0.5000],[ 0.5000,  0.9054, -0.3848,  0.6861, -0.7824,  0.5000],[ 0.5000, -0.2373, -1.3416,  0.0430, -0.8926,  0.5000],[ 0.5000, -0.2171,  1.5384,  0.2721, -2.4094,  0.5000],[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]]]])Process finished with exit code 0

相关文章:

  • 【定义通讯数据类型】LCM搭建系统通讯
  • 重温react-01
  • Mongodb学习
  • Nginx之HTTP模块详解
  • 【LeetCode 5.】 最长回文子串
  • 主窗体设计
  • 2023年的Top20 AI应用在近一年表现怎么样?
  • Postman如何做接口测试:什么?postman 还可以做压力测试?
  • Windows 服务器Nginx 下载、部署、配置流程(图文教程)
  • 一次基于 rebase 的 PR 提交
  • MEMS:Lecture 18 Feedback
  • 【Linux】基础IO [万字之作]
  • tcp和udp的例子
  • 用Copilot画漫画,Luma AI生成视频:解锁创意新玩法
  • 享元和代理模式
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 2017 前端面试准备 - 收藏集 - 掘金
  • co.js - 让异步代码同步化
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • ES2017异步函数现已正式可用
  • FineReport中如何实现自动滚屏效果
  • gulp 教程
  • JAVA SE 6 GC调优笔记
  • JavaScript设计模式系列一:工厂模式
  • Mac转Windows的拯救指南
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • Sass 快速入门教程
  • Vue UI框架库开发介绍
  • vuex 学习笔记 01
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 观察者模式实现非直接耦合
  • 警报:线上事故之CountDownLatch的威力
  • 如何设计一个微型分布式架构?
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 微信小程序开发问题汇总
  • 微信小程序设置上一页数据
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • #预处理和函数的对比以及条件编译
  • (3)nginx 配置(nginx.conf)
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (创新)基于VMD-CNN-BiLSTM的电力负荷预测—代码+数据
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (六)c52学习之旅-独立按键
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (三)Kafka 监控之 Streams 监控(Streams Monitoring)和其他
  • (算法设计与分析)第一章算法概述-习题
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)Unity3DUnity3D在android下调试
  • ..回顾17,展望18
  • .NET CLR基本术语
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET/C# 使用 ConditionalWeakTable 附加字段(CLR 版本的附加属性,也可用用来当作弱引用字典 WeakDictionary)