elementwise_sub

paddle.fluid.layers.nn. elementwise_sub ( x, y, axis=- 1, act=None, name=None ) [source]

Elementwise Sub Operator.

Substract two tensors element-wise

The equation is:

\(Out = X - Y\)

  • $X$: a tensor of any dimension.

  • $Y$: a tensor whose dimensions must be less than or equal to the dimensions of $X$.

There are two cases for this operator:

  1. The shape of $Y$ is the same with $X$.

  2. The shape of $Y$ is a continuous subsequence of $X$.

For case 2:

  1. Broadcast $Y$ to match the shape of $X$, where $axis$ is the start dimension index for broadcasting $Y$ onto $X$.

  2. If $axis$ is -1 (default), $axis = rank(X) - rank(Y)$.

  3. The trailing dimensions of size 1 for $Y$ will be ignored for the consideration of subsequence, such as shape(Y) = (2, 1) => (2).

For example:

shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
Parameters
  • x (Tensor) – (Variable), Tensor or LoDTensor of any dimensions. Its dtype should be int32, int64, float32, float64.

  • y (Tensor) – (Variable), Tensor or LoDTensor of any dimensions. Its dtype should be int32, int64, float32, float64.

  • with_quant_attr (BOOLEAN) – Whether the operator has attributes used by quantization.

  • axis (int32, optional) – If X.dimension != Y.dimension, Y.dimension must be a subsequence of x.dimension. And axis is the start dimension index for broadcasting Y onto X.

  • act (string, optional) – Activation applied to the output. Default is None. Details: Activation Function

  • name (string, optional) – Name of the output. Default is None. It’s used to print debug info for developers. Details: Name

Returns

N-dimension tensor. A location into which the result is stored. It’s dimension equals with x

Return type

out (Tensor)

Examples

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
    return {
        "x": np.array([2, 3, 4]).astype('float32'),
        "y": np.array([1, 5, 2]).astype('float32')
    }
paddle.enable_static()
x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z = fluid.layers.elementwise_sub(x, y)
# z = x - y

place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
                    fetch_list=[z.name])

print(z_value) # [1., -2., 2.]
import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
    return {
        "x": np.ones((2, 3, 4, 5)).astype('float32'),
        "y": np.zeros((3, 4)).astype('float32')
    }
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[3,4], dtype='float32')
z = fluid.layers.elementwise_sub(x, y, axis=1)
# z = x - y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
                    fetch_list=[z.name])

print(z_value) # z.shape=[2,3,4,5]
import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
    return {
        "x": np.random.randint(1, 5, size=[2, 3, 4, 5]).astype('float32'),
        "y": np.random.randint(1, 5, size=[5]).astype('float32')
    }
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[5], dtype='float32')
z = fluid.layers.elementwise_sub(x, y, axis=3)
# z = x - y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
                    fetch_list=[z.name])
print(z_value) # z.shape=[2,3,4,5]