kron

paddle.fluid.layers.kron(x, y, out=None, name=None)[source]

Kron Operator.

This operator computes the Kronecker product of two tensors, a composite tensor made of blocks of the second tensor scaled by the first.

This operator assumes that the rank of the two tensors, \(X\) and \(Y\) are the same, if necessary prepending the smallest with ones. If the shape of \(X\) is [\(r_0\), \(r_1\), …, \(r_N\)] and the shape of \(Y\) is [\(s_0\), \(s_1\), …, \(s_N\)], then the shape of the output tensor is [\(r_{0}s_{0}\), \(r_{1}s_{1}\), …, \(r_{N}s_{N}\)]. The elements are products of elements from \(X\) and \(Y\).

The equation is: $$ output[k_{0}, k_{1}, …, k_{N}] = X[i_{0}, i_{1}, …, i_{N}] * Y[j_{0}, j_{1}, …, j_{N}] $$

where $$ k_{t} = i_{t} * s_{t} + j_{t}, t = 0, 1, …, N $$

Parameters
  • x (Variable) – the fist operand of kron op, data type: float16, float32, float64, int32 or int64.

  • y (Variable) – the second operand of kron op, data type: float16, float32, float64, int32 or int64. Its data type should be the same with x.

  • out (Variable, optional) – Optional output which can be any created Variable that meets the requirements to store the result of operation. If out is None, a new Varibale will be create to store the result. Defaults to None.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

The output of kron op, data type: float16, float32, float64, int32 or int64. Its data is the same with x.

Return type

Variable

Examples

import paddle
from paddle import fluid
import paddle.fluid.dygraph as dg
import numpy as np

a = np.arange(1, 5).reshape(2, 2).astype(np.float32)
b = np.arange(1, 10).reshape(3, 3).astype(np.float32)

place = fluid.CPUPlace()
with dg.guard(place):
    a_var = dg.to_variable(a)
    b_var = dg.to_variable(b)
    c_var = fluid.layers.kron(a_var, b_var)
    c_np = c_var.numpy()
print(c_np)

#[[ 1.  2.  3.  2.  4.  6.]
# [ 4.  5.  6.  8. 10. 12.]
# [ 7.  8.  9. 14. 16. 18.]
# [ 3.  6.  9.  4.  8. 12.]
# [12. 15. 18. 16. 20. 24.]
# [21. 24. 27. 28. 32. 36.]]