What do I need to inherit to make a custom nn module?

I want to make a class that could be any type of nn module (though right now I am using it to play around with only fully connected NNs while I learn pytorch). I was doing my class but now I’m getting python attribute errors e.g.:

*** AttributeError: 'NN' object has no attribute '_modules'

Which makes me think I am not calling the init method of something I clearly need to be calling. The example in the tutorials have as an example:

 super(TwoLayerNet, self).__init__()

but my FC NN might have more than 2 layers. Thus, I was wondering, is there some general super I can call that doesn’t restrict the types of NNs I might make? If not I’d like restrict to any type of feedforward NN (with convolutions or not). An example of the super I should call for an RNN would also be nice.

Just use the construct you added. You only need to inherit from nn.Module, and call the __init__ from it

class MyComplexModel(nn.Module):
    def __init__(self, net1, net2, net3):
        super(MyComplexModel, self).__init__()
        self.net1 = net1
        self.net2 = net2
        self.net3 = net3

    def forward(self, input):
        return self.net3(self.net2(self.net1(x) + 1)**2) / x
1 Like

can you explain what that does?

This is calling the __init__() method of the parent class of MyComplexModel, in this case, nn.Module. For more details, see for example http://www.python-course.eu/python3_inheritance.php

3 Likes

actually super(type(self),self).__init__() is better

1 Like

Could you elaborate on why that is?