megengine.functional.sub#

sub(x, y)[source]#

Element-wise subtraction.

Calculates the difference for each element \(x_i\) of the input tensor \(x\) with the respective element \(y\) of the input tensor \(y\). The result of \(x_i - y_i\) must be the same as \(x_i + (-y_i)\) and must be governed by the same floating-point rules as addition. (See add ).

Parameters:
Return type:

Tensor

Returns:

A tensor containing the element-wise differences. The returned tensor must have a data type determined by Type promotion rules.

Note

The - operator can be used as a shorthand for sub on Tensors.

Examples

>>> F.sub(1.0, 4.0)
Tensor(-3.0, device=xpux:0)

Element-wise subtraction:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> y = Tensor([[1, 1, 1], [2, 2, 2]])
>>> F.sub(x, y)
Tensor([[0 1 2]
 [2 3 4]], dtype=int32, device=xpux:0)

Broadcasting:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> F.sub(x, 1)
Tensor([[0 1 2]
 [3 4 5]], dtype=int32, device=xpux:0)