Share weights between the same instance of a model and stacking models

I have a encoder-decoder architecture. I want to do two things:

  1. I wanna stack the same network three times
  2. and share the weights of some layers (the layers from green areas), and not all layers (not the pink ones)
    Some thing Iike this image:
    stacked%20net

I am new to pytorch and I am not shore how to do it.
I appreciate your help for any reference or sudo code.

Hi, to do so the easiest way is to generate two nn.Modules. One for the green part and another one for the pink one.

To share weights you only have to instantiate the class once. To have different ways you need to instantiate classifierX and axiliary classifierX 3 times.

something like:

class GlobalNet(nn.Module):
init---
    self.net_in_green = instance()
    self.classifier0 = pinkinstance()
    self.classifier1 = pinkinstance()
    self.classifier2 = pinkinstance()
forward(inputs)----
   Here you have to squeeze everything those 3 inputs into  batch dimension, assuming you have a single intput it would be similar to
 inputs #shape--> batch, 3, other dimensions.-..
inputs = inputs.view(-1,other dimensions)
inputs #shape (3*batch, other dimensions)

Then you apply green net
greenout = self.net_in_green(squeezed input)
Now unsqueeze them with view

output0 = self.classifier0(greenout_unsqueezed0)
output1 = self.classifier1(greenout_unsqueezed1)
output02= self.classifier2(greenout_unsqueezed2)

The same with auxiliar classifier

The only drawback is that batch normalization is also shared…
As far as I know it’s not straight forward to achieve different statistics.

1 Like

Hi,
I need to design a similar network where modules in the green area should weights, but there is a feedback from the pink area to the green area. Any idea how to do that?