How to share the common parts of two models in pytorch？
I had a problem implementing the model with pytorch.  

I want to build two models, some of which are shared, and share the encoder part like this

Model1: input_1 -&gt; encoder -&gt; decoder_1 -&gt; ouput_1
Model2: input_2 -&gt; encoder -&gt; decoder_2 -&gt; ouput_2  


What I want to do is make the two models use the encoder part together, but the decoder part is not the same. I looked up about parameter sharing, but it seems to be somewhat different from the requirements here.  

My own idea is to build a model that includes encode, decoder_1, decoder_2 and then choose which decoder to use based on input.

I'm not sure about this method, if possible, can you give simple examples for using the common parts of two models?

You could do something like:

import torch.nn as nn

class SharedModel(nn.Module):
    def __init__(self, mode):
        super(SharedModel, self).__init__()
        self.mode = mode # use 1 or 2

        self.encoder = ...
        self.decoder_1 = ...
        self.decoder_2 = ...


    def forward(self, x):
        x = self.encoder(x)
        if self.mode == 1:
            x = self.decoder_1(x)
        elif self.mode == 2:
            x = self.decoder_2(x)
        else:
            raise ValueError("Unkown mode.")
        return x

