Hi, I am coming from the Keras world. I am trying to create a simple experiment for RNN - denoise a sine wave. Thus, given a sine wave with noise, the model should estimate a denoised version.
I have a few issues:


My code wouldn’t work without retain_graph=True. Why do I have to use retain_graph=True?


This code doesn’t work when I set the hidden_size to be more than 1.


Am I correctly using RNN? I’d really love to help with some more simple examples for RNNs/LSTMs
 import torch
 import torch.nn as nn
 from torch.nn import functional as F
 from torch.autograd import Variable
 from torch import optim
 import numpy as np
 import math, random
 import matplotlib.pyplot as plt

 # Borrowed from https://gist.github.com/spro/ef26915065225df65c1187562eca7ec4

 def sine_2(X, signal_freq=60.):
     return np.sin(2 * np.pi * (X) / signal_freq)

 def noisy(Y, noise_range=(-0.15, 0.15)):
     noise = np.random.uniform(noise_range[0], noise_range[1], size=Y.shape)
     return Y + noise

 def sample(sample_size):
     random_offset = random.randint(0, sample_size)
     X = np.arange(sample_size)
     out = sine_2(X + random_offset)
     inp = noisy(out)
     return inp, out


 input_dim = 1
 hidden_size = 1
 num_layers = 1
 rnn = nn.RNN(input_size=input_dim, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)
 hidden = None

 optimizer = torch.optim.Adam(rnn.parameters(), lr=1e-2)   
 loss_func = nn.MSELoss()

 for t in range(10):
     inp, out = sample(100)
     inp = Variable(torch.Tensor(inp.reshape((1, -1, 1))), requires_grad=True)
     out = Variable(torch.Tensor(out.reshape((1, -1, 1))) )
     pred, hidden = rnn(inp, hidden)
     optimizer.zero_grad()
     loss = loss_func(pred, out)   
     loss.backward(retain_graph=True)
     optimizer.step()