当前位置:网站首页>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)
边栏推荐
- Nacos集群分区
- Summary of Rpc and gRpc Introduction
- MySQ deadlock
- Alibaba search new product data API by keyword
- 防抖与节流
- Basic introduction to protect the network operations
- WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template
- CMake的安装和测试
- (6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
- 小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能
猜你喜欢

使命、愿景、价值观到底有什么区别

小程序毕设作品之微信积分商城小程序毕业设计成品(5)任务书

Monitor page deployment

Nacos 安装与部署

小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT

Problems caused by List getting the difference

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

写给技术人的管理入门知识1:什么是管理

day10--install mysql on linux
随机推荐
ospf map
SQLSERVER将子查询数据合并拼接成一个字段
防抖与节流
Tcp programming
(redistribute, special comprehensive experiment ospf area)
一起来学习flutter 的布局组件
Redis server启动后会做哪些操作?
Operational configuration: How to run multiple EasyCVR programs as a service in one server?
JIT vs AOT
Nacos service registration and discovery
day10--在Linux上安装mysql
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
Nacos集群分区
OA项目之待开会议&历史会议&所有会议
小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板
SDL播放器实战
CMake的安装和测试
Flutter records and learns different animations (1)
逆向理论知识3【UI修改篇】
List获取差集产生的问题