Finally, I did it in 2 ways:


One model
     optimizer.zero_grad()
     layer[:, 0,:]=train.reshape(batch_size, input_dim).float().clone()

     for i in range(1,time_dim):
         layer[:, i,:] = model(layer[:, i-1,:].clone()).reshape(batch_size, input_dim).float()
         loss[i-1]=error(layer[:, i,:].clone(), labels[:,i-1,:].clone())

     loss=torch.sum(loss)
     loss.backward()
 
     optimizer.step()



Many stacked models
     layer[:, 0,:]=train.reshape(batch_size, input_dim*2).float().clone()
     for i in range(1,len(models)):
         layer[:, i,:] = models[i-1](layer[:, i-1,:].clone()).reshape(batch_size, input_dim).float()
         loss[i-1]=error(layer[:, i,:].clone(), labels[:,i-1:i,:].clone()
     
     loss=torch.sum(loss)

     loss.backward()

     optimizer.step()



Is there something, I could miss?