Using models in a row

(Barthelemymp) #1

Hello I d like to stack models .
For example:

class m1(nn.Module):
    def __init__(self, n_wire):
        super(m1, self).__init__()
        ...
    def forward(self,):
        ...

class m2(nn.Module):
    def __init__(self, n_wire):
        super(m2, self).__init__()
        ...
    def forward(self,):
        ...

class m3(nn.Module):
    def __init__(self, n_wire):
        super(m3, self).__init__()
        ...
    def forward(self,):
        ...

and then optimise a cost between label and m3(m2(m1(x)))
PS: I want to keep them separatedbecause some of them are usual nn and other are using an interface specific for qml.

What are the condition in order not to break the backprop ?

Best regards,

B

(balamurali) #2

What do you mean by break the backprop ? This equation itself will work good provided you handle the dimensions well. loss.backward () will be applied on a graph. This three models are part of the graph

(Barthelemymp) #3

thank you!
It just that I wanted to combine different combination of models. To do so I want to make a big model which will call m1(m2()) or m2(m1()) or m1(m1()) etc according to some trainable parameters. And i was wondering if the backward was not stuck to one model. But according to your answer the compose of model will make only graph so one backward. That s exactly what I need :slight_smile:
thank you again!

1 Like