Use Output of a Pre Trained Network as Input for a Different Network

Given a pre trained Network hNet001 I would like its output to be a feature for hNet002.

How could I do that in the forward method hNet002?
The forward method of hNet002 should be something like:

def forward(self, x):
    y = hNet001(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = torch.cat((x, y), 1)
    x = self.layer(x)
    return x

Yet I want y to be treated as input (Namely no update or back propogation on hNet001).
Moreover, How could I send hNet001 as a parameter to the hNet002 network?
Could I do it in the __init__ method?

Hi,
Could can pass nn.Modules as arguments to the forward function.
If you don’t want gradients to be computed, you can use the torch.no_grad context manager:

def forward(self, hNet001, x):
    with torch.no_grad():    
        y = hNet001(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = torch.cat((x, y), 1)
    x = self.layer(x)
    return x