matmul¶
- paddle.fluid.layers.nn. matmul ( x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None ) [source]
-
Applies matrix multiplication to two tensors.
Currently, the input tensors’ rank can be any, but when the rank of any inputs is bigger than 3, this two inputs’ rank should be equal.
The actual behavior depends on the shapes of \(x\), \(y\) and the flag values of
transpose_x
,transpose_y
. Specifically:If a transpose flag is specified, the last two dimensions of the tensor are transposed. If the tensor is rank-1 of shape \([D]\), then for \(x\) it is treated as \([1, D]\) in nontransposed form and as \([D, 1]\) in transposed form, whereas for \(y\) it is the opposite: It is treated as \([D, 1]\) in nontransposed form and as \([1, D]\) in transposed form.
After transpose, the two tensors are 2-D or n-D and matrix multiplication performs in the following way.
If both are 2-D, they are multiplied like conventional matrices.
If either is n-D, it is treated as a stack of matrices residing in the last two dimensions and a batched matrix multiply supporting broadcast applies on the two tensors.
Also note that if the raw tensor \(x\) or \(y\) is rank-1 and nontransposed, the prepended or appended dimension \(1\) will be removed after matrix multiplication.
- Parameters
-
x (Variable) – The input variable which is a Tensor or LoDTensor.
y (Variable) – The input variable which is a Tensor or LoDTensor.
transpose_x (bool) – Whether to transpose \(x\) before multiplication.
transpose_y (bool) – Whether to transpose \(y\) before multiplication.
alpha (float) – The scale of output. Default 1.0.
name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
- Returns
-
The product Tensor (or LoDTensor) variable.
- Return type
-
Variable
Examples
# Examples to clarify shapes of the inputs and output # x: [B, ..., M, K], y: [B, ..., K, N] # fluid.layers.matmul(x, y) # out: [B, ..., M, N] # x: [B, M, K], y: [B, K, N] # fluid.layers.matmul(x, y) # out: [B, M, N] # x: [B, M, K], y: [K, N] # fluid.layers.matmul(x, y) # out: [B, M, N] # x: [M, K], y: [K, N] # fluid.layers.matmul(x, y) # out: [M, N] # x: [B, M, K], y: [K] # fluid.layers.matmul(x, y) # out: [B, M] # x: [K], y: [K] # fluid.layers.matmul(x, y) # out: [1] # x: [M], y: [N] # fluid.layers.matmul(x, y, True, True) # out: [M, N] import paddle import paddle.fluid as fluid paddle.enable_static() x = fluid.layers.data(name='x', shape=[2, 3], dtype='float32') y = fluid.layers.data(name='y', shape=[3, 2], dtype='float32') out = fluid.layers.matmul(x, y, True, True)