栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch深度学习——卷积操作以及代码示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch深度学习——卷积操作以及代码示例

torch.nn.functional.conv2d

在官方文档可以查看参数
其中 input表示输入的图像,weight表示卷积核,图像和卷积核都有固定的shape,所以在卷积之前要用reshape()将两个矩阵的shape转换为固定的格式,stride表示每次移动的步数,padding是对原来输入图像的填充,默认为0,如果为1,将原来矩阵填充一圈


代码示例:

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]])

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

print("padding = 0:")
output = F.conv2d(input, kernel, stride=1)
print(output)

print("padding = 1:")
output1 = F.conv2d(input, kernel, stride=1, padding=1)
print(output1)

输出:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/961397.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号