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

news/2025/11/4 20:55:39/文章来源:https://www.cnblogs.com/Samar-blog/p/19191394

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

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

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

相关文章

25.11.4联考题解

CF1905F 首先判断特殊情况:\(\forall i,p_i=i\) 答案一定是 \(n-2\)。然后考虑一个位置如果已经满足条件我们先统计到答案中,对于不满足条件的位置,考虑去进行交换的贡献。如果存在一个位置满足 \(p_i=i\) 并且前面…

d11.4t4 answer

d11.4t4 answer 题目 题目描述 小 ∗ 有一条地铁线路。 有 \(n\) 个嘟嘟要乘坐地铁。第 \(i\) 个嘟嘟会在第 \(l_i\)站上车,第 \(r_i\) 站下车。为了方便,我们假定有 \(2n\) 个地铁站,且 \(l_i\) , \(r_i\) 互不相…

详细介绍:当AI化身数据炼金术士:初级Python开发者如何将创意炼成黄金代码?—— 老码农的炼金术指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

【学习笔记】kafka权威指南——第3章 kafka生产者—向kafka写入资料

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

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

P15.神经网路的基本骨架——nn.Module的使用打开PyTorch官网 1.找到troch.nn的Containers2.打开pycharm:代码-生成-重写方法-选择第一个要初始化的方法__init__3.pycharm运行代码如下点击查看代码 import torch from …

AGC052做题记录

A 其实是简单题,但我是唐诗。 \(2n\) 很简单,前 \(n\) 个和后 \(n\) 个全选 \(0/1\) 即可。可以感觉到正解只需要在这基础上改进,但是胡思乱想了很多没有任何进展。最后意识到最后凑个 \(0\) 即可。 B 做完 T1 ,就…

软工团队第一次作业

作业所属课程 软件工程作业要求 https://edu.cnblogs.com/campus/fzu/202501SoftwareEngineering/homework/13573作业目标 根据真实的需求调研结果,确定具有创新性与实用价值的软件开发项目选题,确保能将智能体合理地…

Windows11-GPT

Windows11-GPT导航 (返回顶部)1. 基于UEFI/GPT的硬盘分区 2. 分区要求2.1 启动分区(ESP) 2.2 微软保留分区(MSR) 2.3 Windows分区(OS) 2.4 恢复工具分区(WinRE) 2.5 数据分区(Other)3. 默认分区布局表 4. 其他实用分区…

1. markdown转word 第一步: markdown转html

1. 简介 最近因为项目需求需要将AI输出的结果导出到word中, 但AI输出的格式为markdown格式,因为word展示内容的时候需要有相应的格式(标题, 段落, 列表, 表格等), 所以不能直接将markdown输出到word中, 否则word中展示…

P14.Dataloader的使用

P14.Dataloader的使用14.1Pytorch官网打开torch.utils.data.DataLoader14.2在pycharm使用DataLoader它返回img和target代码如下:点击查看代码 import torchvision from torch.utils.data import DataLoader #from tor…

docker换源

创建/编辑 /etc/docker/daemon.json {"registry-mirrors": ["https://docker.1ms.run","https://docker-0.unsee.tech","https://docker.m.daocloud.io"],"live-restore…

pypinyin很好用

pypinyin很好用〔https://gitee.com/mirrors/python-pinyin#id4〕 〔https://github.com/mozillazg/pypinyin-dict〕 pip install就噌噌地装上了。 到灵格斯词典网站下载了成语、文化等词典,过几天出个1~2万词的用户…

小九源码-springboot078-java物业管理架构

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

VS 2017 项目文件不完整,缺少预期导入

VS 2017 项目文件不完整,缺少预期导入输入: dotnet --info缺少 .NET Core 2.0 运行时支持。安装:dotnet-runtime-2.0.0-win-x64.exe本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/v…

人性的弱点

抱怨、吵闹,和歇斯底里,所付出的代价。 那种喋喋不休的吵闹,是否对她有了某种帮助?还是把事情弄得更糟? 当你在深夜面对自己时,如果感受到的是充盈而非荒芜,那么这种生存方式就已通过生命的终极考核。

P13.torchvision中的数据集使用

P13.torchvision中的数据集使用13.1Transforms中的类 1.打开pytorch官网 2.找到CIFAR10,这个数据集比较小3.点击图片上红色的CIFAR104.这里的链接就是Pycharm下载到dataset里面的东西13.2CIFAR10数据集的下载与导入 1…

机器学习基础入门(第四篇):无监督学习与聚类途径

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

图上状压 DP

容易发现每年都在考这玩意。每年都不会。 AT_abc213_g [ABC213G] Connectivity 2 显然删边可以变成保留边。 定义状态函数 \(f_s\) 表示保留边,使得 \(s\) 中的点联通的方案数。那么对于 \(k=k0\) 来说,答案应该就是…

k8s删除Terminating状态的命名空间

k8s删除Terminating状态的命名空间原创于 2021-04-07 16:14:02 发布 CC 4.0 BY-SA版权 K8S/Kubernetes文章已被社区收录加入社区 K8S专栏收录该内容5 篇文章订阅专栏在部署Kubesphere时遇到命名空间kubesphere-system处…

【实用脚本】一键安装Oracle19c数据库

【实用脚本】一键安装Oracle19c数据库完整脚本: #!/bin/bash #Oracle 一键安装工具 log_file="/var/log/oracle_install_$(date +%Y%m%d_%H%M%S).log" # 屏幕输出只显示状态提示,日志文件记录完整详细信息…