当前位置:网站首页>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)
边栏推荐
- Flutter record learning different animation (2)
- JIT vs AOT
- Gateway 路由网关
- LoadBalancer load balancing
- (redistribute, special comprehensive experiment ospf area)
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
- Mysql版本升级,直接复制Data文件,查询特别慢
- Microservice CAP Principles
- Nacos service registration and discovery
- [ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
猜你喜欢
组织在线化:组织数字化变革的新趋势
数组和结构体
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
ospf 综合实验(重发布,特殊区域)
EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?
小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
LoadBalancer 负载均衡
Transformation of traditional projects
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
随机推荐
EasyCVR启动时报错“no such file or directory”,该如何解决?
CMake installation and testing
Nacos服务注册与发现
Nacos命名空间
数组和结构体
Advanced Microservices Cloud Alibaba
What is the difference between mission, vision and values?
OpenFeign实现降级
护网行动基本介绍
Nacos配置中心
STM32 SPI+WM8978语音回环
List获取差集产生的问题
小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
Sentinel Traffic Guard
How to solve the error "no such file or directory" when EasyCVR starts?
Alibaba search new product data API by keyword
sqlmap使用教程大全命令大全(图文)
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
ospf 综合实验(重发布,特殊区域)