multiply

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

Elementwise Mul Operator.

Multiply two tensors element-wise

The equation is:

\(Out = X \\odot 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.

  • 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

multiply two tensors element-wise. The equation is:

\[out = x * y\]

Note: paddle.multiply supports broadcasting. If you would like to know more about broadcasting, please refer to Broadcasting .

param x

the input tensor, its data type should be float32, float64, int32, int64.

type x

Tensor

param y

the input tensor, its data type should be float32, float64, int32, int64.

type y

Tensor

param name

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

type name

str, optional

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 paddle

x = paddle.to_tensor([[1, 2], [3, 4]])
y = paddle.to_tensor([[5, 6], [7, 8]])
res = paddle.multiply(x, y)
print(res) # [[5, 12], [21, 32]]

x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([2])
res = paddle.multiply(x, y)
print(res) # [[[2, 4, 6], [2, 4, 6]]]

Return type

out (Tensor)