Reputation: 1033
What is __constants__
in pytorch class Linear(Module):
defined in https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html?
What is its functionality and why is it used?
I have been searching around, but did not find any documentation. Please note that this does not mean the __constants__
in torch script.
Upvotes: 10
Views: 3321
Reputation: 12857
The __constants__
you're talking about is, in fact, the one related to TorchScript. You can confirm it by using git blame
(when it was added and by who) on GitHub. For example, for torch/nn/modules/linear.py
, check its git blame.
TorchScript also provides a way to use constants that are defined in Python. These can be used to hard-code hyper-parameters into the function, or to define universal constants.
-- Attributes of a ScriptModule can be marked constant by listing them as a member of the constants property of the class:
class Foo(torch.jit.ScriptModule):
__constants__ = ['a']
def __init__(self):
super(Foo, self).__init__(False)
self.a = 1 + 4
@torch.jit.script_method
def forward(self, input):
return self.a + input
Upvotes: 10