尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

P16.土堆说卷积(可选看)

P16.土堆说卷积(可选看)
📅 发布时间:2026/6/20 22:06:03

P16.土堆说卷积(可选看)

16.1torch.nn.functional.conv2d的参数(官网)

点击查看代码
input:input tensor of shape (minibatch,in_channels,iH,iW)
weight:filters of shape (out_channels,in_channelsgroups,kH,kW)
bias:optional bias tensor of shape (out_channels). Default: `None`
stride:the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1
padding:implicit paddings on both sides of the input. Can be a string {‘valid’, ‘same’}, single number or a tuple (padH, padW).Default: 0 `padding='valid'` is the same as no padding. `padding='same'` pads the input so the output has the same shape as the input. However, this mode doesn’t support any stride values other than 1.

16.2卷积原理

1

16.3根据以上图片的tensor,写入pycharm

代码如下:

点击查看代码
import torch#根据图片上的输入图像和卷积核
input = torch.tensor([[1,2,0,3,1],[0,1,2,3,1],[1,2,1,0,0],[5,2,3,1,1],[2,1,0,1,1]])
kernel = torch.tensor([[1,2,1],[0,1,0],[2,1,0]])#打印input和kernel的尺寸
print(input.shape)
print(kernel.shape)

运行结果如下:

点击查看代码
D:\anaconda3\envs\pytorch\python.exe D:/DeepLearning/Learn_torch/P16_nnConv.py
torch.Size([5, 5])
torch.Size([3, 3])进程已结束,退出代码0
很明显,他的尺寸输出只有两个参数,他不符合torch.nn.functional.conv2d的参数input – input tensor of shape (minibatch,in_channels,iH,iW)中对于input和kernel的四个参数的要求

16.4torch.reshape函数&torch.shape函数

1.PyTorch中的torch.reshape函数

(1)官方文档介绍:
torch.reshape(input, shape) → Tensor
Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.

(2)翻译为大白话:
在PyTorch中,torch.reshape函数是用来改变张量(Tensor)的形状(shape)的工具。这个函数不改变张量中的数据和元素数量,只是重新排列这些元素的形状。当可能的时候,torch.reshape会返回原始张量的一个视图(view),这意味着新的张量和原始张量共享相同的数据,但是展示的形状不同。如果不能返回一个视图,那么它会返回一个数据的拷贝。

2.PyTorch中的torch.shape函数

在PyTorch中,处理图像数据的张量默认遵循NCHW(或称为channels_first)的格式,即:
N: Number of images in the batch (批次中的图像数量)
C: Channels per image (每张图像的通道数)
H: Height of each image (每张图像的高度)
W: Width of each image (每张图像的宽度)

3.应用conv2d

代码如下:

点击查看代码
import torch
import torch.nn.functional as F
#根据图片上的输入图像和卷积核
input = torch.tensor([[1,2,0,3,1],[0,1,2,3,1],[1,2,1,0,0],[5,2,3,1,1],[2,1,0,1,1]])
kernel = torch.tensor([[1,2,1],[0,1,0],[2,1,0]])#修改尺寸:batch_size为1,channel为1,宽和高保持原来的:
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))#打印input和kernel的尺寸
print(input.shape)
print(kernel.shape)#应用conv2d
#import torch.nn.functional as F
output = F.conv2d(input,kernel,stride=1)
print(output)
输出结果如下:
点击查看代码
#输出结果如下:
D:\anaconda3\envs\pytorch\python.exe D:/DeepLearning/Learn_torch/P16_nnConv.py
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],[18, 16, 16],[13,  9,  3]]]])进程已结束,退出代码0
和图片上我们自己计算的结果一模一样,输出结果为tensor类型的3*3矩阵

4.修改stride参数

代码如下:

点击查看代码
#应用conv2d(其他代码同上)
#import torch.nn.functional as F
output2 = F.conv2d(input,kernel,stride=2)
print(output2)
输出结果如下:
点击查看代码
#输出结果如下:
tensor([[[[10, 12],[13,  3]]]])
输出结果为tensor类型的2*2矩阵

5.padding

【padding=1表示原input图像,上下左右各添加一个空白行(列),值为0】

代码如下:

点击查看代码
#应用conv2d(其他代码同上)
#import torch.nn.functional as F
#增加padding参数
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)

输出结果如下:

点击查看代码
#输出结果如下:
tensor([[[[ 1,  3,  4, 10,  8],[ 5, 10, 12, 12,  6],[ 7, 18, 16, 16,  8],[11, 13,  9,  3,  4],[14, 13,  9,  7,  4]]]])
输出结果为tensor类型的5*5矩阵,明显尺寸变大
如图,原始图像因为padding=1的设置,变成了7*7的矩阵:

2

相关新闻

  • P15.神经网路的基本骨架——nn.Module的使用
  • function
  • P14.Dataloader的使用

最新新闻

  • 仙桃音响改装难题终结者:音改坊汽车音响旗舰店3大核心优势揭秘,问界音响改装/问界原车音响升级,音响改装门店口碑推荐 - 音响改装门店分享
  • 永康黄金回收报价单位有猫腻吗?克和钱别换算错/金银金包银黄金回收/ 文娟珠宝黄金回收/老金黄金回收 - 回收测评
  • 从单点漏洞到批量挖掘:构建自动化RCE漏洞扫描体系实战
  • 小红书拓客赛道升温 专业服务商助力品牌精准获客 - 速递信息
  • Gemini零基础实战:三明治提问、分段编辑与知识胶囊
  • 【权威发布】172号卡平台2026年6月正式新增总部直营官方邀请码:08888 - 嗨是我

日新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号