位置: IT常识 - 正文

PyTorch之F.pad的使用与报错记录(pytorch f)

编辑:rootadmin
PyTorch之F.pad的使用与报错记录 F.pad的使用与报错记录

推荐整理分享PyTorch之F.pad的使用与报错记录(pytorch f),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:pytorch pad,pytorch f,pytorch f,pytorch f,pytorch f,pytorch paddle,pytorch f.pad,pytorch paddlepaddle,内容如对您有帮助,希望把文章链接给更多的朋友!

原始文档:https://www.yuque.com/lart/ugkv9f/iftd9v

函数原型

函数文档:https://pytorch.org/docs/1.12/generated/torch.nn.functional.pad.html#torch-nn-functional-pad

torch.nn.functional.pad(input, pad, mode='constant', value=None) → TensorPadding格式1D-tensor:(p_left, p_right)2D-tensor:(p_left, p_right, p_top, p_bottom)3D-tensor:(p_left, p_right, p_top, p_bottom, p_front, p_back)四种模式

这一函数用于实现对高维tensor的形状补齐操作。PyTorch本身提供了四种padding模式:

constant:使用指定的常数value补齐指定的维度。对于数据012,使用0补齐,结果可以为0001200。reflect:使用tensor自身的值按照“反射”的方式补齐指定的维度。对于数据012,结果可以为2101210。replicate:使用tensor自身边界值补齐指定的维度。对于数据012,结果可以为0001222。circular:使用tensor自身的值按照“循环”的方式补齐指定的维度。对于数据012,结果可以为1201201。

需要注意的是,文档强调了这一点:

PyTorch之F.pad的使用与报错记录(pytorch f)

Constant padding is implemented for arbitrary dimensions. Replicate and reflection padding are implemented for padding the last 3 dimensions of a 4D or 5D input tensor, the last 2 dimensions of a 3D or 4D input tensor, or the last dimension of a 2D or 3D input tensor.

这四种模式使用输出展示会更便于理解一些,下面是一个例子:

import torchimport torch.nn.functional as Fpad = [2, 2, 2, 2]x = torch.arange(9, dtype=torch.float32).reshape(1, 1, 3, 3)print("x")print(x)print("F.pad(x, pad=pad, mode='constant', value=0)")print(F.pad(x, pad=pad, mode='constant', value=0))print("F.pad(x, pad=pad, mode='replicate')")print(F.pad(x, pad=pad, mode='replicate'))print("F.pad(x, pad=pad, mode='reflect')")print(F.pad(x, pad=pad, mode='reflect'))print("F.pad(x, pad=pad, mode='circular')")print(F.pad(x, pad=pad, mode='circular'))

对应的输出为:

xtensor([[[[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]]])F.pad(x, pad=pad, mode='constant', value=0)tensor([[[[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 2., 0., 0.], [0., 0., 3., 4., 5., 0., 0.], [0., 0., 6., 7., 8., 0., 0.], [0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.]]]])F.pad(x, pad=pad, mode='replicate')tensor([[[[0., 0., 0., 1., 2., 2., 2.], [0., 0., 0., 1., 2., 2., 2.], [0., 0., 0., 1., 2., 2., 2.], [3., 3., 3., 4., 5., 5., 5.], [6., 6., 6., 7., 8., 8., 8.], [6., 6., 6., 7., 8., 8., 8.], [6., 6., 6., 7., 8., 8., 8.]]]])F.pad(x, pad=pad, mode='reflect')tensor([[[[8., 7., 6., 7., 8., 7., 6.], [5., 4., 3., 4., 5., 4., 3.], [2., 1., 0., 1., 2., 1., 0.], [5., 4., 3., 4., 5., 4., 3.], [8., 7., 6., 7., 8., 7., 6.], [5., 4., 3., 4., 5., 4., 3.], [2., 1., 0., 1., 2., 1., 0.]]]])F.pad(x, pad=pad, mode='circular')tensor([[[[4., 5., 3., 4., 5., 3., 4.], [7., 8., 6., 7., 8., 6., 7.], [1., 2., 0., 1., 2., 0., 1.], [4., 5., 3., 4., 5., 3., 4.], [7., 8., 6., 7., 8., 6., 7.], [1., 2., 0., 1., 2., 0., 1.], [4., 5., 3., 4., 5., 3., 4.]]]])可能会遇到的报错

常见的错误主要是因为padding的数量超过了对应模式的要求。

对于constant和replicate对于padding并没有限制。

但是另外两种模式replicate和circular就有要求了。

RuntimeError: Argument #4: Padding size should be less than the corresponding input dimension, but got: padding (3, 3) at dimension 3 of input 4

这发生在reflect模式中,padding的数量必须小于对应维度的大小。

import torchimport torch.nn.functional as Fpad = [3, 3, 3, 3]x = torch.arange(9, dtype=torch.float32).reshape(1, 1, 3, 3)print("F.pad(x, pad=pad, mode='reflect')")print(F.pad(x, pad=pad, mode='reflect'))"""F.pad(x, pad=pad, mode='reflect')Traceback (most recent call last): File "e:/Coding/PythonTools/TorchPadding/main.py", line 20, in <module> print(F.pad(x, pad=pad, mode='reflect')) File "D:\Programming\Python\envs\pt1102\lib\site-packages\torch\nn\functional.py", line 4189, in _pad return torch._C._nn.reflection_pad2d(input, pad)RuntimeError: Argument #4: Padding size should be less than the corresponding input dimension, but got: padding (3, 3) at dimension 3 of input 4"""AssertionError: Padding value causes wrapping around more than once.

这发生在circular模式中,padding的数量不得超出原始tensor对应维度的大小。

import torchimport torch.nn.functional as Fpad = [4, 4, 4, 4]x = torch.arange(9, dtype=torch.float32).reshape(1, 1, 3, 3)print("F.pad(x, pad=pad, mode='circular')")print(F.pad(x, pad=pad, mode='circular'))"""F.pad(x, pad=pad, mode='circular')Traceback (most recent call last): File "e:/Coding/PythonTools/TorchPadding/main.py", line 17, in <module> print(F.pad(x, pad=pad, mode='circular')) File "D:\Programming\Python\envs\pt1102\lib\site-packages\torch\nn\functional.py", line 4193, in _pad return _pad_circular(input, pad) File "D:\Programming\Python\envs\pt1102\lib\site-packages\torch\nn\functional.py", line 4585, in _pad_circular assert padding[-(idx * 2 + 1)] <= size, "Padding value causes wrapping around more than once."AssertionError: Padding value causes wrapping around more than once."""
本文链接地址:https://www.jiuchutong.com/zhishi/288989.html 转载请保留说明!

上一篇:js如何把时间戳转化为日期(js怎么把时间戳转为日期yyyy-mm-dd)

下一篇:触屏不灵敏怎么办(触屏不灵敏怎么调整oppo)

  • 月末只有进项税需要把转出未交增值税转到未交增值税
  • 以摊余成本计量的金融资产交易费用
  • 应付款为什么是负数
  • 单一环节征税有哪些类型
  • 其他债权投资公允价值变动影响摊余成本吗
  • 材料成本差异月初贷方余额表示什么
  • 商品流通企业成本核算的内容包括
  • 加盟费摊销会计分录
  • 房租发票上税的分录怎么写?
  • 发票税表抵扣了账务未抵扣账务处理怎么做?
  • 以存货抵偿债务结转的相关存货跌价准备
  • 小规模纳税人一年不超过多少万
  • 印花税税目错了能申报更正吗
  • 企业账户短信提醒可以用别人手机号吗
  • 增值税发票专票有效期
  • 财税[2001]10号
  • 案例讲解:将自己的房产用于办公使用,在税收的缴纳中该如何把控?
  • 15-算
  • 职工福利费扣除标准2022
  • 罚款费用报销单怎么写
  • 安装调试费计入什么科目
  • 工程收入怎么算
  • 累计折旧差错调整减少
  • 1697506445
  • 金蝶界面设置
  • 金融工具中股利是什么
  • 应付职工薪酬转入管理费用
  • 网络环境中存在不同网段的nvr
  • 键盘突然打不出来字
  • 华为路由器怎么设置wifi密码
  • php转换成html
  • 建筑行业有哪些岗位,从事的要求有哪些
  • php输入月份输出天数
  • thinkphp yii
  • js鼠标事件包括哪几种
  • jmeter接口串联
  • 如何修改python
  • find命令详解查找文件
  • 房地产开发企业的了解概述
  • 企业抵扣进项税条件
  • mongodb数据库教程
  • 员工出差预借差旅费属于
  • 公司有残疾人如何申报残保金
  • 培训费 会议费
  • sql server2019实例功能的选择
  • 短期借款的核算
  • 开户套餐费是什么意思
  • 红冲去年的成本怎么做账
  • 预计负债的账务处理
  • 短期借款利息的使用账户是
  • 文化传媒公司的主营业务
  • 产品成本的具体内容
  • 境外企业分红到境内
  • 专项资金补助经费如何入账
  • 购买金税盘未抵税怎么办
  • 公司购买的意外险计入什么科目
  • 印花税减免税额怎么填
  • 慧通年终奖怎么计算
  • 网上打印的银行流水有公章吗
  • 固定资产处置有净值怎么处理
  • 一次性开票分期确认收入已什么做原始凭证
  • sql2000语句
  • netbeui怎么安装
  • windows10预览版是什么
  • mongo 安装
  • 如何设置桌面背景颜色
  • ubuntucommand not found
  • win7系统怎么设置电源
  • jquery实现
  • windows visual studio openGL开发环境配置
  • vue路由怎么实现
  • shell命令大全shell脚本编程100例
  • shell脚本用法
  • js判断字符串字符出现的次数
  • node.js 生成pdf
  • js实现时间
  • ubuntu20.04 python
  • javascript gui
  • Javascript中Array.prototype.map()详解
  • 个人所得税完税证明怎么开具
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设