1. 卷积原理

① 卷积核不停的在原图上进行滑动,对应元素相乘再相加。

② 下图为每次滑动移动1格,然后再利用原图与卷积核上的数值进行计算得到缩略图矩阵的数据,如下图右所示。

python 复制代码
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]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output = F.conv2d(input, kernel, stride=1)
print(output)

结果:

效果:

python 复制代码
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]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output2 = F.conv2d(input, kernel, stride=2)  # 步伐为2
print(output2)

结果 :

python 复制代码
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]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output3 = F.conv2d(input, kernel, stride=1, padding=1)  # 周围只填充一层
print(output3)

效果:

相关推荐
东莞市云毅网络有限公司8 分钟前
把网页表格与列表还原成结构化条目:抽取与对账实现
python·文本处理·数据抽取
szxinmai主板定制专家10 分钟前
ZYNQ PL+PS 架构拆解|工业自动化与电力物联网高速数据采集、预处理与协议上云
arm开发·人工智能·物联网·fpga开发·数据采集卡·rk3576
小何好运暴富开心幸福10 分钟前
LangChain 与 LangGraph 核心要点总结
深度学习·语言模型·langchain·知识图谱
禁默11 分钟前
从“动作社区“到“技能商店“:机器人应用生态走到哪一步了
人工智能·机器人
话梅姐姐呀11 分钟前
IOT -- 设备入网与注册
大数据·网络·人工智能·物联网·iot
roman_日积跬步-终至千里12 分钟前
【数据Agent论文·表格事实与生成|15】LogicNLG:数值抄对了,逻辑结论就对了吗?
人工智能·深度学习·自然语言处理·数据分析
夜瞬13 分钟前
从零微调 Qwen3-8B:基于 LoRA 与 LLaMA-Factory 打造专属 AI 编程助教
人工智能·python·llama
碧海银沙音频科技研究院14 分钟前
面向长尾音频分类的知识蒸馏研究
人工智能·算法·音视频
zhilai116 分钟前
2026 企业 AI 办公工具选型指南:框架、全景与落地路径
人工智能
IT大白鼠17 分钟前
AI SSH 运维助手 V13.0内部版七项私有新特性全解
运维·人工智能·ssh