Share the output of one DNN to another DNN

I have two DNNs the first one returns two outputs. I want to use one of these outputs in a second class that represents another DNN as in the following example:

import torch.nn as nn
    import torch.nn.functional as F
    import torch.optim as optim

    class Net(nn.Module):
        def __init__(self):
            ..
        def forward(self, x):
            ..
            return x, z


    class Net2(nn.Module):
        def __init__(self):
            ..
        def forward(self, v):
            y = torch.cat(v, x)
            return y

I want to pass the output (x) to the second class to be concatenated to another variable (v). I found a solution to make the variable (x) as a global variable, but I need another efficient solution

You can pass x as another argument to the forward of Net2 and wouldn’t need to use global variables.

do you mean in Net2 the forward function will be like as def forward(self, v, x):

Yes, exactly. This allows you to pass both inputs to it which avoids the usage of some global variables.