Can I train two networks, one network which contains another netwrok, at the same time?
I define two networks in my model.

def Net_One():
    conv2d
    conv2d
    ...
def Net_Two():
    Net_One(input)
    conv2d
    fc


So my question is: when I train Net_Two, using back propagation optimization, will pytorch automatically train Net_One or not? Why?

Firstly model class name's convention should be CapWords: NetOne,NetTwo, but this doesn't hurt anything, just a convention.

As for your questions, It depends on the processing of NetTwo().
if the final loss of NetTwo has nothing to do with NetOne(), then the back propagation will not flow through NetOne(), and therefore will not update NetOne()'s parameters.
Otherwise the back propagation will compute the NetOne()'s gradients and update its weights.

For code samples:

# NetTwo's loss has nothing to do with NetOne:
def NetOne():
    def __init__(self):
        super(NetOne, self).__init__()
        ...

    def forward(inputs):
        ...


def NetTwo():
    def __init__(self):
        super(NetTwo, self).__init__()
        ...

    def forward(inputs):
        # in this processing, temp is never used by NetTwo's layers..
        ...
        temp = NetOne(inputs)
        inputs = conv2d(inputs)
        ...


In the upper codes, temp is never used, so NetOne won't be updated when NetTwo is updating.

But if temp is used by NetTwo.forward(), it'll be updated, like following:

def NetTwo():
    def __init__(self):
        super(NetTwo, self).__init__()
        ...

    def forward(inputs):
        # in this processing, temp is used by NetTwo's layers.
        ...
        temp = NetOne(inputs)
        inputs = conv2d(temp)
        ...


Did this answer your question?
