2020LR = 0.02 # learning rate
2121
2222# show data
23- steps = np .linspace (0 , np .pi * 2 , 100 , dtype = np .float32 )
24- x_np = np .sin (steps ) # float32 for converting torch FloatTensor
23+ steps = np .linspace (0 , np .pi * 2 , 100 , dtype = np .float32 ) # float32 for converting torch FloatTensor
24+ x_np = np .sin (steps )
2525y_np = np .cos (steps )
2626plt .plot (steps , y_np , 'r-' , label = 'target (cos)' )
2727plt .plot (steps , x_np , 'b-' , label = 'input (sin)' )
@@ -55,7 +55,13 @@ def forward(self, x, h_state):
5555 # instead, for simplicity, you can replace above codes by follows
5656 # r_out = r_out.view(-1, 32)
5757 # outs = self.out(r_out)
58+ # outs = outs.view(-1, TIME_STEP, 1)
5859 # return outs, h_state
60+
61+ # or even simpler, since nn.Linear can accept inputs of any dimension
62+ # and returns outputs with same dimension except for the last
63+ # outs = self.out(r_out)
64+ # return outs
5965
6066rnn = RNN ()
6167print (rnn )
@@ -71,8 +77,8 @@ def forward(self, x, h_state):
7177for step in range (100 ):
7278 start , end = step * np .pi , (step + 1 )* np .pi # time range
7379 # use sin predicts cos
74- steps = np .linspace (start , end , TIME_STEP , dtype = np .float32 )
75- x_np = np .sin (steps ) # float32 for converting torch FloatTensor
80+ steps = np .linspace (start , end , TIME_STEP , dtype = np .float32 ) # float32 for converting torch FloatTensor
81+ x_np = np .sin (steps )
7682 y_np = np .cos (steps )
7783
7884 x = torch .from_numpy (x_np [np .newaxis , :, np .newaxis ]) # shape (batch, time_step, input_size)
0 commit comments