学习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
相关推荐
懂点Tech5 小时前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
comedate6 小时前
SMLib 的 Examples 导读:PyAPI / SM_API / SMLib 三组示例怎么读
python·cad·smlib·examples·sm_api
云和数据.ChenGuang6 小时前
Python 重写(Override)深度解析:从 MRO 到 super() 的完整机制
java·开发语言·人工智能·python·langchain·1024程序员节
川石课堂软件测试6 小时前
学习软件测试需掌握的一些知识
数据库·python·sql·功能测试·网络协议·mysql·http
万化冥合7 小时前
PyBox屏幕键盘如何辅助演示键盘操作?
python·计算机外设
车软学堂7 小时前
从测试转开发,到底要补什么
网络·人工智能·python·功能测试·单片机·车载系统·自动化
天空依然蔚蓝7 小时前
分享一个自动检测网站在线状态的python程序
python·运维开发·1024程序员节
岁月宁静7 小时前
一个 AI 项目从本地到上线,要经过哪些步骤?全流程讲清楚
python·docker·agent
codists8 小时前
《crafting interpreters》阅读笔记:p45-p45
python
曲径海盗9 小时前
【Python 学习第 41 天】for 循环
python·学习笔记·编程入门