Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constants in Pytorch Linear Module Class Definition

Tags:

pytorch

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.

like image 663
thinkdeep Avatar asked Aug 16 '19 10:08

thinkdeep


People also ask

What is linear function in PyTorch?

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.

What are modules in PyTorch?

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.

What are parameters in PyTorch?

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.

What is linear layer PyTorch?

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.

What is the base class of PyTorch?

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.

What is the Cloneable class in PyTorch?

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.

What is a class constant in Python?

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.

How are Python constants allocated?

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.


1 Answers

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
like image 139
kHarshit Avatar answered Oct 05 '22 07:10

kHarshit