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.
PyTorch - nn.Linear nn. Linear(n,m) is a module that creates single layer feed forward network with n inputs and m output. Mathematically, this module is designed to calculate the linear equation Ax = b where x is input, b is output, A is weight. This is where the name 'Linear' came from.
PyTorch uses modules to represent neural networks. Modules are: Building blocks of stateful computation. PyTorch provides a robust library of modules and makes it simple to define new custom modules, allowing for easy construction of elaborate, multi-layer neural networks.
Parameters are Tensor subclasses, that have a very special property when used with Module s - when they're assigned as Module attributes they are automatically added to the list of its parameters, and will appear e.g. in parameters() iterator. Assigning a Tensor doesn't have such effect.
PyTorch nn linear initialization As we know the nn linear is a module which is used to create a single layer feed-forward network with the help of n inputs and m outputs. Pytorch nn linear initialization in which we can also create the feed-forward network with the help of the input and output.
The base class for all modules in PyTorch. The design and implementation of this class is largely based on the Python API. You may want to consult the python documentation for torch.nn.Module for further clarification on certain methods or behavior.
public torch::nn::Cloneable< TransformerEncoderLayerImpl > ( Template Class Cloneable) public torch::nn::Cloneable< TripletMarginWithDistanceLossImpl > ( Template Class Cloneable) The base class for all modules in PyTorch. The design and implementation of this class is largely based on the Python API.
Introduction to Python Class Constants. An unchangeable value, assiged, is called a constant. Meaning, constants are the containers that hold some information and cannot be changed further. Write once, read many times constants are created in the class of Python. Usually, constants are defined in the level of a module.
The Python constants are first created. These are identified to be constants as they are capitalized letters. They are given with an underscore in between to potrait it as a single variable. The variable is then assigned a value that is globally the same or consistent throughout the program. Once allocated, it is the same anytime.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With