1. We need to import library
import torch
from torch import nn
import numpy as np
import matplotlib. pyplot as plt
torch. manual_seed( 1 )
2. Define the parameters of our ultra
TIME_STEP = 10
INPUT_SIZE = 1
LR = 0.02
3. Check our goal
steps = np. linspace( 0 , np. pi* 2 , 100 , dtype= np. float32)
x_np = np. sin( steps)
y_np = np. cos( steps)
plt. plot( steps, x_np, 'b-' , label= 'input(sin)' )
plt. plot( steps, y_np, 'r-' , label= 'target(cos)' )
plt. legend( loc= 'best' )
plt. show( )
4. build our RNN
h_0 is our initial h_state is None, then when we finished every training session, after our first data our h_state will be saved, and then output, then the output (the previous memory) when we next training data.
Our argument is training more than the hidden layer, and our h_state (memory)
Below is a flow chart of us RNN
We note that the format of the data x (batch_size, time_size , input_size)
class RNN ( nn. Module) :
def __init__ ( self) :
super ( RNN, self) . __init__( )
self. rnn = nn. RNN(
input_size = INPUT_SIZE,
hidden_size= 32 ,
num_layers= 1 ,
batch_first= True ,
)
self. out = nn. Linear( 32 , 1 )
def forward ( self, x, h_state) :
r_out, h_state = self. rnn( x, h_state)
outs = [ ]
for time_step in range ( r_out. size( 1 ) ) :
outs. append( self. out( r_out[ : , time_step, : ] ) )
return torch. stack( outs, dim= 1 ) , h_state
rnn = RNN( )
print ( rnn)
optimizer = torch. optim. Adam( rnn. parameters( ) , lr= LR)
loss_func = nn. MSELoss( )
5. Training Our RNN
Precautions
Do not forget to pass initialization state, and the state of
Do not forget to change the format of the input data
h_state = None
plt. figure( 1 , figsize= ( 12 , 5 ) )
plt. ion( )
for step in range ( 100 ) :
start, end = step* np. pi , ( step+ 1 ) * np. pi
steps = np. linspace( start, end, TIME_STEP, dtype= np. float32, endpoint= False )
x_np = np. sin( steps)
y_np = np. cos( steps)
x = torch. from_numpy( x_np[ np. newaxis, : , np. newaxis] )
y = torch. from_numpy( y_np[ np. newaxis, : , np. newaxis] )
prediction, h_state = rnn( x, h_state)
h_state = h_state. data
loss = loss_func( prediction, y)
optimizer. zero_grad( )
loss. backward( )
optimizer. step( )
plt. plot( steps, y_np. flatten( ) , 'r-' )
plt. plot( steps, prediction. data. numpy( ) . flatten( ) , 'b-' )
plt. draw( ) ; plt. pause( 0.05 )
plt. ioff( )
plt. show( )