Pytorch nested nn modules

Hi everyone!

I’m new to PyTorch and I have a problem regarding nested PyTorch nn modules. The problem as follows.

I have a main nn model lets say model A. Which in turn uses two objects created from another nn class inside a for loop that contains lstm cells(single-cell).

First, the autograd complains that the graphs need to be retained because backward is going through multiple times over and over. After making retain_graph = True it gives the following error.**

**RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [2048]] is at version 2; expected version 1 instead. **

By the way, it does the first forward pass successfully. It would be greatly helpful if one of you guys could show me a consistent way to implement a subset of neural agent objects inside a for loop in the main nn.module class .

class ModelA(nn.Module):
   def __init__():
      super(ModelA, self).__init__()
      self.agent1 = Agent(#initialising parameters)
      self.agent2 = Agent(#initialising parrameters)
      out1 = torch.zeros(#required dimension)
      out2 = torch.zeros(#required dimension)

   def forward(self, inputs):
      for i in range(25):
         out1 = self.agent1(out2, #for loop ends = false)
         out2 = self.agent2(out1, #for loop ends = false)
      y1 = self.agent1(out2, # for loop ends = True)
      x1 = self.agent2(out1, # for loop ends = True)
      return x1, y1

class Agent(nn.Module):
    def __init__():
    super(Agent, self).__init__()
    # initialize all the layers and recurrent cells here

    def forward(#inputs, #whether for loop ends)
       if for_loop_ends = True
          return #result1.
          return #result2


Do you properly re-initialize the hidden state to make sure it does not backrprop through the previous iterations. Otherwise you will get the error that you see above with trying to backward though the graph multiple times.

1 Like

Hi i add re-initialisation at the if loop where it checks the condition for loop end. And it worked like a charm. Thanks a lot.

Hi alban,
Just another question. According to my structure are these agents sharing any parameters?.

If each of them is creating it’s own nn.Parameter then they won’t share no.

1 Like