PyTorch基础之网络模块torch.nn中函数和模板类的使用详解(附源码)

需要源码请点赞关注收藏后评论区留言私信~~~

PyTorch中的torch.nn模块有许多自带的函数,如全连接层,卷积层,归一化层等,都已经被封装在该模块下,可以直接使用,在编写代码的过程中十分简便,下面对他们进行介绍

一、torch.nn函数简介

(1) nn.Linear()

用于设置网络中的全连接层 

import torch
from torch import nn
linear = torch.nn.Linear(in_features=64, out_features=1)
input = torch.rand(10, 64)
output = linear(input)
print(output.shape)  # torch.Size([10, 1])

(2) nn.Conv1d()

在由多个输入平面组成的信号上应用一维卷积

conv1 = nn.Conv1d(in_channels=256, out_channels=10, kernel_size=2, stride=1, padding=0)
input = torch.randn(32, 32, 256)  # [batch_size, L_in, in_channels]
input = input.permute(0, 2, 1)  # 交换维度:[batch_size, embedding_dim, max_len]
out = conv1(input)  # [batch_size, out_channels, L_out]
print(out.shape)  # torch.Size([32, 10, 31]),31=(32+2*0-1*1-1)/1+1

(3) nn.Conv2d()

在由多个输入平面组成的输入信号上应用二维卷积                

import torch
x = torch.randn(3, 1, 5, 4)  # [N, in_channels, H_in, W_in]
conv = torch.nn.Conv2d(1, 4, (2, 3))  # [in_channels, out_channels, kernel_size]
output = conv(x)
print(output.shape)  # torch.Size([3, 4, 4, 2]), [N, out_channels, H_out, W_out]

(4) nn.BatchNorm1d

为了加速神经网络的收敛过程以及提高训练过程中的稳定性

Bat = nn.BatchNorm1d(2)
input = torch.randn(2, 2)
output = Bat(input)
print(input, output)
# tensor([[ 0.5476, -1.9766],
#         [ 0.7412, -0.0297]]) tensor([[-0.9995, -1.0000],
#         [ 0.9995,  1.0000]], grad_fn=<NativeBatchNormBackward>)

(5) nn.BatchNorm2d

 二维批归一化层

Bat = nn.BatchNorm2d(2)
input = torch.randn(1, 2, 2, 2)
output = Bat(input)
print(input, output)
# tensor([[[[ 0.6798,  0.8453],
#           [-0.1841, -1.3340]],
# 
#          [[ 1.9479,  1.2375],
#           [ 1.0671,  0.9406]]]]) tensor([[[[ 0.7842,  0.9757],
#           [-0.2150, -1.5449]],
# 
#          [[ 1.6674, -0.1560],
#           [-0.5933, -0.9181]]]], grad_fn=<NativeBatchNormBackward>)

(6)nn.RNN  循环神经网络

feature_size = 32
num_steps = 35
batch_size = 2
num_hiddens = 2
X = torch.rand(num_steps, batch_size, feature_size)
RNN_layer = nn.RNN(input_size=feature_size, hidden_size=num_hiddens)
Y, state_new = RNN_layer(X)
print(X.shape, Y.shape, len(state_new), state_new.shape)
# torch.Size([35, 2, 32]) torch.Size([35, 2, 2]) 1 torch.Size([1, 2, 2])

(7) nn.LSTM

长短时记忆网络

import torch
from torch import nn
# 构建4层的LSTM,输入的每个词用10维向量表示,隐藏单元和记忆单元的尺寸是20
lstm = nn.LSTM(input_size=10, hidden_size=20, num_layers=4)

# 输入的x:其中batch_size是3表示有三句话,seq_len=5表示每句话5个单词,feature_len=10表示每个单词表示为长10的向量
x = torch.randn(5, 3, 10)
# 前向计算过程,这里不传入h_0和C_0则会默认初始化
out, (h, c) = lstm(x)
print(out.shape)  # torch.Size([5, 3, 20]) 最后一层10个时刻的输出
print(h.shape)  # torch.Size([4, 3, 20]) 隐藏单元
print(c.shape)  # torch.Size([4, 3, 20]) 记忆单元

(8) nn.ConvTranspose1d

  一维转置卷积神经网络(反卷积)

dconv1 = nn.ConvTranspose1d(1, 1, kernel_size=3, stride=3, padding=1, output_padding=1)

x = torch.randn(16, 1, 8)
print(x.size())  # torch.Size([16, 1, 8])

output = dconv1(x)
print(output.shape)  # torch.Size([16, 1, 23])

(9) nn.ConvTranspose2d

二维转置卷积神经网络(反卷积)

dconv2 = nn.ConvTranspose2d(1, 1, kernel_size=3, stride=3, padding=1, output_padding=1)

x = torch.randn(16, 1, 8, 8)
print(x.size()) # torch.Size([16, 1, 8, 8])

output = dconv2(x)
print(output.shape) # torch.Size([16, 1, 23, 23])

二、借助torch.nn.Module构建深度学习模型的类class

torch.nn.Module是nn中十分重要的类,包含网络各层的定义及forward方法,可以借助torch.nn.Module构建深度学习模型的类class,当面对复杂的模型,比如:多输入多输出、多分支模型、带有自定义层的模型时,需要自己来定义一个模型

nn.Module的一般定义如下:

class Module(object):         

def __init__(self):         

def forward(self, *input):         

def __call__(self, *input, **kwargs):         

def __repr__(self):       

 def __dir__(self):

接下来对于上述nn.Module中每个函数进行介绍:

def __init__(self):__init__是一个特殊方法用于在创建对象时进行初始化操作;

def forward(self,*input):forward函数为前向传播函数,需要自己重写,它用来实现模型的功能,并实现各个层的连接关系;

def __call__(self, *input, **kwargs):__call__()的作用是使class实例能够像函数一样被调用,以“对象名()”的形式使用;

def __repr__(self):__repr__函数为Python的一个内置函数,它能把一个对象用字符串的形式表达出来;

def __dir__(self):dir()也是Python提供的一个内置函数,它可以查看某个对象具有哪些属性。

部分示例代码如下

import torch
from torch import nn
class MyNet(nn.Module):
    def __init__(self):
        super(MyNet, self).__init__() # 使用父类的方法初始化子类
        self.linear1 = torch.nn.Linear(96, 1024)  # [96,1024]
        self.relu1 = torch.nn.ReLU(True)
        self.batchnorm1d_1 = torch.nn.BatchNorm1d(1024)
        self.linear2 = torch.nn.Linear(1024, 7 * 7 * 128)  # [1024,6272]
        self.relu2 = torch.nn.ReLU(True)
        self.batchnorm1d_2 = torch.nn.BatchNorm1d(7 * 7 * 128)
        self.ConvTranspose2d = nn.ConvTranspose2d(128, 64, 4, 2, padding=1)

    def forward(self, x):
        x = self.linear1(x)
        x = self.relu1(x)
        x = self.batchnorm1d_1(x)
        x = self.linear2(x)
        x = self.relu2(x)
        x = self.batchnorm1d_2(x)
        x = self.ConvTranspose2d(x)
        return x

model = MyNet()
print(model)
# 运行结果为:
# MyNet(
#   (linear1): Linear(in_features=96, out_features=1024, bias=True)
#   (relu1): ReLU(inplace=True)
#   (batchnorm1d_1): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
#   (linear2): Linear(in_features=1024, out_features=6272, bias=True)
#   (relu2): ReLU(inplace=True)
#   (batchnorm1d_2): BatchNorm1d(6272, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
#   (ConvTranspose2d): ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
# )

 三、模型类Class的使用

在前面介绍完class中各个函数并学习了如何构建class之后,接下来进一步介绍模型类class的使用。

model = MyNet()  # 实例化的过程中没有传入参数
input = torch.rand([32, 96])  # 输入的最后一个维度要与nn.Linear(96, 1024)中第一个维度96相同
target = model(input)
print(target.shape)  # torch.Size([32, 1, 28, 28])

上述实例化的过程中是没有参数输入的,也就是在构造类的过程中使用构造函数的第一种形式def__init__(self)。此外,在构造类的过程中很多情况下使用构造函数的第二种形式def__init__(self, 参数1,参数2,···,参数n),在这种情况下,对类实例化的过程中也需要输入相应的参数,举例代码如下:

#构建网络
class MyNet(nn.Module):
    def __init__(self,in_dim,n_hidden_1,n_hidden_2,out_dim):
        super(MyNet, self).__init__()
        self.layer1 = nn.Sequential(nn.Linear(in_dim,n_hidden_1),nn.BatchNorm1d(n_hidden_1))
        self.layer2 = nn.Sequential(nn.Linear(n_hidden_1,n_hidden_2),nn.BatchNorm1d(n_hidden_2))
        self.layer3 = nn.Sequential(nn.Linear(n_hidden_2,out_dim))

    def forward(self,x):
        x = torch.relu(self.layer1(x))
        x = torch.relu(self.layer2(x))
        x = self.layer3(x)
        return x

# 实例化网络层
model = MyNet(28 * 28, 300, 100, 10)  # 实例化的过程中传入参数
input = torch.rand(10, 28 * 28)
target = model(input)
print(target.shape)  # torch.Size([10, 10])

创作不易 觉得有帮助请点赞关注收藏~~~

猜你喜欢

转载自blog.csdn.net/jiebaoshayebuhui/article/details/130440945