关于pytorch:PyTorch建立RNN相关模型

11次阅读

共计 2714 个字符,预计需要花费 7 分钟才能阅读完成。

之前写过 PyTorch 建设深度神经网络,这一次是上一篇文章的连续,将介绍 RNN,LSTM,GRU 相干模型搭建。
RNN、GRU、LSTM 均是领有“记忆”性能的网络模块,在模型参数上基本相同。

建设 RNN 模型

# 定义网络结构
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.rnn = nn.RNN(input_size=28, # 输出特色的大小
                           hidden_size=64, # RNN 模块(block)的数量,随便设置
                           num_layers=1,    # 示意 RNN 层的层数
                           batch_first=True) # RNN 默认输出的格局是[seq_len, batch, feature]
                                             # batch_first=True 示意格局变成[batch, seq_len, feature]
        
        # 这里的 64 和 hidden_size 的 64 绝对应
        self.out = nn.Linear(64, 10)
        self.softmax = nn.Softmax(dim=1)
        
    def forward(self, x):
        inputs = x.reshape((x.shape[0],28, -1))
        # output:[batch, seq_len, hidden_size]
        # 尽管 RNN 的 batch_first 为 True,然而 h_n,c_n 第一个维度还是 num_layers
        # h_n:[num_layers*num_directions, batch, hidden_size] 只蕴含最初一个序列的隐层后果,示意 h_t
        # num_directions: 取值为 1 或 2,示意是否为双向 RNN
        output, h_n = self.rnn(inputs)
        output_in_last_timestep = h_n[-1, :, :]
        x = self.out(output_in_last_timestep)
        out = self.softmax(x)
        return out

建设 LSTM 模型

# 定义网络结构
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.lstm = nn.LSTM(input_size=28, # 输出特色的大小
                           hidden_size=64, # LSTM 模块(block)的数量,随便设置
                           num_layers=1,    # 示意 LSTM 层的层数
                           batch_first=True) # LSTM 默认输出的格局是[seq_len, batch, feature]
                                             # batch_first=True 示意格局变成[batch, seq_len, feature]
        
        # 这里的 64 和 hidden_size 的 64 绝对应
        self.out = nn.Linear(64, 10)
        self.softmax = nn.Softmax(dim=1)
        
    def forward(self, x):
        inputs = x.reshape((x.shape[0],28, -1))
        # output:[batch, seq_len, hidden_size]
        # 尽管 LSTM 的 batch_first 为 True,然而 h_n,c_n 第一个维度还是 num_layers
        # h_n:[num_layers*num_directions, batch, hidden_size] 只蕴含最初一个序列的输入后果,示意 h_t
        # c_n:[num_layers*num_directions, batch, hidden_size] 只蕴含最初一个序列的 cell 后果,示意 c_t
        # num_directions: 取值为 1 或 2,示意是否为双向 LSTM
        output, (h_n, c_n) = self.lstm(inputs)
        output_in_last_timestep = h_n[-1, :, :]
        x = self.out(output_in_last_timestep)
        out = self.softmax(x)
        return out

定义双向 LSTM 模型须要在 nn.LSTM 中增加参数bidirectional=True

定义 GRU 模型

# 定义网络结构
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.gru = nn.GRU(input_size=28, # 输出特色的大小
                           hidden_size=64, # GRU 模块(block)的数量,随便设置
                           num_layers=1,    # 示意 GRU 层的层数
                           batch_first=True) # GRU 默认输出的格局是[seq_len, batch, feature]
                                             # batch_first=True 示意格局变成[batch, seq_len, feature]
        
        # 这里的 64 和 hidden_size 的 64 绝对应
        self.out = nn.Linear(64, 10)
        self.softmax = nn.Softmax(dim=1)
        
    def forward(self, x):
        inputs = x.reshape((x.shape[0],28, -1))
        # output:[batch, seq_len, hidden_size*num_directions]
        # 尽管 GRU 的 batch_first 为 True,然而 h_n,c_n 第一个维度还是 num_layers
        # h_n:[num_layers*num_directions, batch, hidden_size] 只蕴含最初一个序列的输入后果,示意 h_t
        # num_directions: 取值为 1 或 2,示意是否为双向 GRU
        output, h_n = self.gru(inputs)
        output_in_last_timestep = h_n[-1, :, :]
        x = self.out(output_in_last_timestep)
        out = self.softmax(x)
        return out

模型加载与保留

PyTorch 容许用户保留和加载已训练实现的模型

# 保留模型
torch.save(model.state_dict(), 'model/GRUModel.pth')
# 加载模型
model.load_state_dict(torch.load('model/GRUModel.pth'))

模型显示

Keras 创立的模型能够应用 summary 办法显示模型相干参数,PyTorch 也领有一个库 torchsummary 能够用来显示模型参数
应用 pip 命令即可装置

pip install torchsummary

显示模型参数只须要两行代码

from torchsummary import summary
summary(model, (28, 28))

正文完
 0