How to add a new attribute to torch.nn

I created a class (named DividedLinear) which is a modified version of existing Linear class and tested the new class and it’s autograd in a model, it all worked.

Then I added the new class to the bottom of file: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/modules/linear.py

to be able to call torch.nn.DividedLinear in a model’s torch.nn.Sequential. However I get the following error: module ‘torch.nn’ has no attribute ‘DividedLinear’

What other modifications do I need to do or should I just keep the new class separate?
P.s. I am new.

I would keep the new Module separate. It works in Sequential just fine. :slight_smile:

In general, you shouldn’t modify library files. In this case, it is not visible because the exposed class list doesn’t include it. However, don’t modify that list! Instead:

from torch import nn

class DividedLinear(nn.Module):
  #write your code, remember to super(DividedLinear, self).__init__ 

and just use this class from here. Whether it is under torch.nn namespace won’t affect how pytorch deals with it. The important thing is that it subclasses nn.Module.

Thanks, I am using it exactly that way as a separate file in my own folders now, but I was hoping I could modify my torch library to be able to use it for the future. So i will keep it as it is and wont modify main files. But would like to know where is the exposed class list?

If that’s the case, I encourage you to either make your own helper file to import or maintain a fork so that you can also pull from master to keep up to date. The list is here.

Thanks a lot!
You are right upon pulling I lose my modifications.