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

pytorch学习(五)torch.nn.functional

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

pytorch学习(五)torch.nn.functional

1. torch.nn.functional官方解释

进入pytorch官方Docs
查看 torch.nn官方指南,讲的很详细

2 代码

1、

import torch
from torch import nn

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与kernel尺寸发现与要求的参数不一致(参数下方已截出)

输入与卷积核如下


卷积核

torch.nn.function部分参数要求:

  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))#minibatch=1,in_channels=1
kernel = torch.reshape(kernel, (1, 1, 3, 3))#minibatch=1,in_channels=1

output = F.conv2d(input, kernel, stride=1)#步长=1,padding=0
print(output)
output_1 = F.conv2d(input, kernel, stride=2)#步长=2,padding=0
print(output_1)
output_2 = F.conv2d(input, kernel, stride=1, padding=1)#步长=1,padding=1
print(output_2)

输出结果

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

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

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