当前位置:网站首页>Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
2022-07-30 03:54:00 【柚子Roo】
Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
1. torch.nn.Module介绍
所有神经网络模块的基类。
你的模型也应该继承这个类。
模块还可以包含其他模块,允许将它们嵌套在树结构中。
注意:我们在使用nn.module构建神经网络时,需要在__init__()
方法中对继承的Module类中的属性进行调用,因此在初始化方法中需要添加一句代码:
super().__init__()
import torch
from torch import nn
class Test(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = input + 1
return output
test = Test()
x = torch.tensor(1)
output = test(x)
print(output)
2. torch.nn.functional.conv2d介绍
torch.nn.functional.conv2d
(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
在由多个输入平面组成的输入图像上应用 2D 卷积。
参数
- input – 形状的输入张量
(minibatch ,in_channels , iH , iW)
- weight- 形状过滤器
(out_channels, in_channels/groups , kH , kW)
- bias- 形状的可选偏差张量
(out_channels)
. 默认:None
- stride——卷积核的步幅。可以是单个数字或元组
(sH, sW)
。默认值:1 - padding-输入两侧的隐式填充。可以是字符串 {‘valid’, ‘same’}、单个数字或元组(padH, padW)。默认值:0
padding='valid'
与无填充相同。padding='same'
填充输入,使输出具有与输入相同的形状。但是,此模式不支持 1 以外的任何步幅值。
import torch
from torch import nn
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(input)
print(kernel)
output = F.conv2d(input, kernel, stride=1, padding=1)
print(output)
边栏推荐
猜你喜欢
How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?
ospf 综合实验(重发布,特殊区域)
CMake installation and testing
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
Public chains challenging the "Impossible Triangle"
day10--在Linux上安装mysql
spicy (1) basic definition
OA项目之待开会议&历史会议&所有会议
北京bgp机房和普通机房的区别
随机推荐
Nacos命名空间
MySQ deadlock
Has been empty, a straightforward, continue to copy the top off!
List获取差集产生的问题
逆向理论知识3【UI修改篇】
STM32 SPI+WM8978语音回环
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?
SQLSERVER将子查询数据合并拼接成一个字段
Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
spicy(二)unit hooks
Let's learn the layout components of flutter together
Operational configuration: How to run multiple EasyCVR programs as a service in one server?
淘宝/天猫获得淘宝店铺详情 API
EasyCVR启动时报错“no such file or directory”,该如何解决?
SDL player in action
(redistribute, special comprehensive experiment ospf area)
The difference between BGP room and ordinary room in Beijing
组织在线化:组织数字化变革的新趋势