subtract

paddle. subtract ( x, y, name=None ) [source]

Substract two tensors element-wise. The equation is:

\[out = x - y\]

Note: paddle.subtract supports broadcasting. If you want know more about broadcasting, please refer to user_guide_broadcasting .

Parameters
  • x (Tensor) – the input tensor, it’s data type should be float32, float64, int32, int64.

  • y (Tensor) – the input tensor, it’s data type should be float32, float64, int32, int64.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

N-D Tensor. A location into which the result is stored. If x, y have different shapes and are “broadcastable”, the resulting tensor shape is the shape of x and y after broadcasting. If x, y have the same shape, its shape is the same as x and y.

Examples

import numpy as np
import paddle

x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[5, 6], [3, 4]])
res = paddle.subtract(x, y)
print(res)
#       [[-4, -4],
#        [4, 4]]

x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([1, 0, 4])
res = paddle.subtract(x, y)
print(res)
#       [[[ 0,  2, -1],
#         [ 0,  2, -1]]]

x = paddle.to_tensor([2, np.nan, 5], dtype='float32')
y = paddle.to_tensor([1, 4, np.nan], dtype='float32')
res = paddle.subtract(x, y)
print(res)
#       [ 1., nan, nan]

x = paddle.to_tensor([5, np.inf, -np.inf], dtype='float64')
y = paddle.to_tensor([1, 4, 5], dtype='float64')
res = paddle.subtract(x, y)
print(res)
#       [   4.,  inf., -inf.]