当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
Rpc 和 gRpc 简介汇总
Process priority nice
防抖与节流
cv2.polylines
redis分布式锁的原子保证
Nacos服务注册与发现
Transformation of traditional projects
Nacos集群分区
Nacos 安装与部署
New interface - API interface for "Taote" keyword search
Hystrix 服务熔断
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (5) Task Book
spicy(一)基本定义
Nacos配置中心
Mysql版本升级,直接复制Data文件,查询特别慢
小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
The curl command to get the network IP
sublime text 3 设置
The difference between BGP room and ordinary room in Beijing









