Hey! Pytorch is amazing and I’m trying to learn how to use it at the moment. I have gotten stuck at training a simple RNN to predict the next value in a time series with a single feature value per timestep.
Here is my net:
class Net(nn.Module): def __init__(self,hidden_dim = 128,num_layers=1): super(Net,self).__init__() self.hidden_dim = hidden_dim self.lstm = nn.LSTM(input_size=1, hidden_size=hidden_dim, num_layers = num_layers) self.dense = nn.Linear(hidden_dim,1) self.hidden = self.init_hidden() def init_hidden(self): return ( Variable(torch.zeros(1,1,self.hidden_dim)).cuda(), Variable(torch.zeros(1,1,self.hidden_dim)).cuda() ) def forward(self,x): lstm_out, self.hidden = self.lstm(x,self.hidden) linear_out = self.dense(lstm_out) return linear_out
And here is my training loop:
opt = optim.Adam(net.parameters()) loss_fn = nn.MSELoss() for i in range(len(values)-1): net.zero_grad() x = np.array(values[i],dtype=np.float32).reshape((1,1,1)) x_var = Variable(torch.from_numpy(x)).cuda() y = np.array(values[i+1],dtype=np.float32).reshape((1,1,1)) y_var = Variable(torch.from_numpy(y)).cuda() out = net.forward(x_var) loss = loss_fn(out,y_var) loss.backward() opt.step()
I want to be able to do “online training” and thus only want to input 1 timestep at a time. I am getting the following error:
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
I am not calling
backward() twice as far as I can see? I must have misunderstood how RNN’s work in pytorch as they were pretty much “plug and play” in keras and you didnt have to hold on to the hidden state.
Thanks in advance!