学习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
相关推荐
晚秋大魔王2 小时前
基于python的jlink单片机自动化批量烧录工具
前端·python·单片机
胖哥真不错2 小时前
Python基于PyTorch实现多输入多输出进行CNN卷积神经网络回归预测项目实战
pytorch·python·毕业设计·课程设计·毕设·多输入多输出·cnn卷积神经网络回归预测
程序员-小李2 小时前
基于PyTorch的动物识别模型训练与应用实战
人工智能·pytorch·python
闲人编程5 小时前
Python在网络安全中的应用:编写一个简单的端口扫描器
网络·python·web安全·硬件·端口·codecapsule·扫描器
Mr_Xuhhh8 小时前
GUI自动化测试--自动化测试的意义和应用场景
python·集成测试
2301_764441338 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天8 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
Q_Q5110082858 小时前
python+django/flask的眼科患者随访管理系统 AI智能模型
spring boot·python·django·flask·node.js·php
SunnyDays101110 小时前
如何使用Python高效转换Excel到HTML
python·excel转html
Q_Q51100828510 小时前
python+django/flask的在线学习系统的设计与实现 积分兑换礼物
spring boot·python·django·flask·node.js·php