How to use RNN and LSTM functions(pytorch)



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0


Release date:2023/7/2         

In Japanese


■Description of RNN and LSTM functions

Calculate Recurrent Neural Network (RNN) and Long Short-Term Memory (LSTM)

■How to use the RNN function

import torch
import torch.nn as nn

class CalRNN(nn.Module):
    def __init__(self , in_size , hidden , output , layer):
        super().__init__()
        self.rnn = nn.RNN(in_size, hidden, layer, batch_first=True, nonlinearity='relu') #Default is 'tanh'
        self.fc = nn.Linear(hidden, output)

    def forward(self, x):
        x_rnn, _ = self.rnn(x, None)
        y = self.fc(x_rnn[:, -1, :])
        return y

model_rnn = CalRNN(10, 64, 1, 2) #input:10, middle layer:64, output:1, 2 layers

x = torch.randn(5,1,10) #batch size:5、input:10
ans = model_rnn(x)
print(ans)

⇒tensor([[-0.0328],
               [ 0.0598],
               [-0.1709],
               [ 0.0603],
               [-0.1373]], grad_fn=<AddmmBackward>)


The "forward" part above is equivalent to the red part below. It can be confirmed that the above description and the result match.

    def forward(self, x):
        x_rnn, _ = self.rnn(x, None) # For comparison
        y = self.fc(x_rnn[:, -1, :])
# For comparison

        h0 = torch.zeros(2, 5, 64) # layer, batch size, middle layer
        x2_rnn, h = self.rnn(x, h0)
        y2 = self.fc(x2_rnn[:, -1, :])


        return y , y2


■How to use the LSTM function

Change the "RNN" part to "LSTM" and remove the nonlinearity part. (only tanh can be used)

import torch
import torch.nn as nn

class CalRNN(nn.Module):
    def __init__(self , in_size , hidden , output , layer):
        super().__init__()
        self.rnn = nn.LSTM(in_size, hidden, layer, batch_first=True) #Default is 'tanh'
        self.fc = nn.Linear(hidden, output)

    def forward(self, x):
        x_rnn, _ = self.rnn(x, None)
        y = self.fc(x_rnn[:, -1, :])
        return y

model_rnn = CalRNN(10, 64, 1, 2) #input:10, middle layer:64, output:1, 2 layers

x = torch.randn(5,1,10) #batch size:5、input:10
ans = model_rnn(x)
print(ans)

⇒tensor([[0.0127],
               [0.0224],
               [0.0103],
               [0.0214],
               [0.0134]], grad_fn=<AddmmBackward>)










List of related articles



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0