学习pytorch8 土堆说卷积操作

土堆说卷积操作

    • 官网
    • [debug torch版本只有nn 没有nn.functional](#debug torch版本只有nn 没有nn.functional)
    • 代码
    • 执行结果

B站小土堆视频学习笔记

官网

https://pytorch.org/docs/stable/nn.html#convolution-layers

常用torch.nn, nn是对nn.functional的封装,使函数更易用。

卷积核从输入图像左上角,先向右遍历行,stride为1 挪一个格位置,向右遍历完,向下一格,再从左向右遍历。

卷积核和输入图像对应位置相乘后结果想加,得到右边的输出结果。

stride

padding

debug torch版本只有nn 没有nn.functional

sh 复制代码
 conda activate pytorch
 conda install pytorch-cpu torchvision-cpu -c pytorch

在当前环境安装pytorch-cpu后,functional函数就可以调用啦

https://www.saoniuhuo.com/question/detail-2646442.html

代码

py 复制代码
import torch
from torch.nn import 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]])

input = torch.reshape(input, [1, 1, 5, 5])
kernel = torch.reshape(kernel, [1, 1, 3, 3])
print(input.shape)
print(kernel.shape)
output1 = F.conv2d(input, kernel, stride=1)
print(output1)

output2 = F.conv2d(input, kernel, stride=2)
print(output2)
# 默认padding=0
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)

执行结果

text 复制代码
p14_conv.py
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],
          [18, 16, 16],
          [13,  9,  3]]]])
tensor([[[[10, 12],
          [13,  3]]]])
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]]]])

Process finished with exit code 0
相关推荐
曲幽11 分钟前
Django入门指南:Python Web开发的“瑞士军刀”
python·django·flask·fastapi·web·pythonweb
m0_7482495414 分钟前
Java 语言提供了八种基本类型【文123】
java·开发语言·python
移幻漂流19 分钟前
Kotlin 如何解决 Java 的核心痛点:现代语言特性的深度剖析
java·python·kotlin
我的xiaodoujiao22 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 41--自定义定制化展示 Allure 测试报告内容
python·学习·测试工具·pytest
2501_9421917723 分钟前
YOLO11-Seg-SwinTransformer榛子缺陷识别实战
python
闻道且行之35 分钟前
基于 LLM 的 MCP 架构实战:服务端搭建、工具开发与 Dify 集成全流程
python·架构·nlp·dify·mcp
老蒋每日coding1 小时前
Python:数字时代的“万能钥匙”
开发语言·python
甄心爱学习1 小时前
KMP算法(小白理解)
开发语言·python·算法
培培说证1 小时前
2026 高职计算机专业考证选择哪个更实用?
python
ASD123asfadxv2 小时前
LCD屏幕表面缺陷检测系统凹陷识别与定位技术分析
python