What are all the PyTorch operators, and what are their function equivalents?
Eg, is a @ b
equivalent to a.mm(b)
or a.matmul(b)
?
I'm after a canonical listing of operator -> function mappings.
I'd be happy to be given a PyTorch documentation link as an answer - my googlefu couldn't track it down.
mul() method is used to perform element-wise multiplication on tensors in PyTorch. It multiplies the corresponding elements of the tensors. We can multiply two or more tensors. We can also multiply scalar and tensors.
It automatically converts NumPy arrays and Python numerical values into PyTorch Tensors. It preserves the data structure, e.g., if each sample is a dictionary, it outputs a dictionary with the same set of keys but batched Tensors as values (or lists if the values can not be converted into Tensors).
To create a tensor with pre-existing data, use torch.tensor() . To create a tensor with specific size, use torch.* tensor creation ops (see Creation Ops). To create a tensor with the same size (and similar types) as another tensor, use torch.*_like tensor creation ops (see Creation Ops).
This defines tensor operations for 0.3.1 (it does also contain the definitions of the other operators): https://pytorch.org/docs/0.3.1/_modules/torch/tensor.html
The code for the current stable has been rearranged (I guess they do more in C now), but since the behaviour of matrix multiplication hasn't changed, I think it is save to assume that this is still valid.
See for the definition of __matmul__
:
def __matmul__(self, other):
if not torch.is_tensor(other):
return NotImplemented
return self.matmul(other)
and
def matmul(self, other):
r"""Matrix product of two tensors.
See :func:`torch.matmul`."""
return torch.matmul(self, other)
The operator @
was introduced with PEP 465 and is mapped to __matmul__
.
See also here for this:
What is the '@=' symbol for in Python?
The Python documentation table Mapping Operators to Functions provides canonical mappings from:
operator -> __function__()
Eg:
Matrix Multiplication a @ b matmul(a, b)
Elsewhere on the page, you will see the __matmul__
name as an alternate to matmul
.
The definitions of the PyTorch __functions__
are found either in:
The torch.Tensor
module documentation
python_variable_methods.cpp
You can look up the documentation for the named functions at:
https://pytorch.org/docs/stable/torch.html?#torch.<FUNCTION-NAME>
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