Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTorch mapping operators to functions

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.

like image 777
Tom Hale Avatar asked Nov 19 '18 07:11

Tom Hale


People also ask

How do you multiply two tensors PyTorch?

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.

What does .data do in PyTorch?

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).

How do you make a torch tensor?

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).


2 Answers

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?

like image 39
MBT Avatar answered Oct 21 '22 21:10

MBT


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>
like image 187
Tom Hale Avatar answered Oct 21 '22 20:10

Tom Hale