PyTorch快速入门教程【小土堆】-神经网络的基本骨架nn.Module、土堆说卷积

1.torch.nn的基本构成模块

nn---Neural network torch.nn里面有神经网络的一些工具

torch.nn — PyTorch 1.11.0 documentationicon-default.png?t=M4ADhttps://pytorch.org/docs/stable/nn.html

(1)Containers 

基本构成模块

Module

Base class for all neural network modules.  常用

对于所有神经网络一个基本的类

Sequential

A sequential container.

ModuleList

Holds submodules in a list.

ModuleDict

Holds submodules in a dictionary.

ParameterList

Holds parameters in a list.

ParameterDict

Holds parameters in a dictionary.

Module简单的使用

class Tudui(nn.Module):#继承nn.Module  注意 Module 首字母大写!!!
    #初始化
    def __init__(self) -> None:#可以直接写 也可以alt+insert选择重写__init__
        super().__init__()
    #forward 定义每次调用时执行的计算,应该被所有子类重写
    def forward(self,input):
        output=input+1
        return output


tudui=Tudui()
x=torch.tensor(1.0)

output=tudui(x)
print(output)

2.Convolution Layers 卷积层

nn.Conv1d

Applies a 1D convolution over an input signal composed of several input planes. 一维卷积

nn.Conv2d

Applies a 2D convolution over an input signal composed of several input planes.二维卷积

(1)TORCH.NN.FUNCTIONAL.CONV2D参数

Parameters

 

 (2)演示TORCH.NN.FUNCTIONAL.CONV2

演示此过程

 

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]])
#conv2d 要求输入四个数据,我们只有两个高和宽,需要变换尺寸
input=torch.reshape(input,(1,1,5,5))#通道数是1,batch的大小是1,数据维度是5*5
kernel=torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)

output=F.conv2d(input,kernel,stride=1)
print(output)
output2=F.conv2d(input,kernel,stride=2)
print(output2)

output3=F.conv2d(input,kernel,stride=1,padding=1)#padding=1
print(output3)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值