tensordot#
- ivy.tensordot(x1, x2, /, *, axes=2, out=None)[source]#
Return a tensor contraction of x1 and x2 over specific axes.
Note
If either
x1
orx2
has a complex floating-point data type, neither argument must be complex-conjugated or transposed. If conjugation and/or transposition is desired, these operations should explicitly performed prior to computing the generalized matrix product.- Parameters:
x1 (
Union
[Array
,NativeArray
]) – First input array. Should have a numeric data type.x2 (
Union
[Array
,NativeArray
]) – second input array. Must be compatible with x1 for all non-contracted axes. Should have a numeric data type.axes (
Union
[int
,Tuple
[List
[int
],List
[int
]]], default:2
) – The axes to contract over. Default is 2.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – The tensor contraction of x1 and x2 over the specified axes.
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([[1., 2.], [2., 3.]]) >>> y = ivy.array([[3., 4.], [4., 5.]]) >>> res = ivy.tensordot(x, y, axes =0) >>> print(res) ivy.array([[[[3.,4.],[4.,5.]],[[6.,8.],[8.,10.]]],[[[6.,8.],[8.,10.]],[[9.,12.],[12.,15.]]]])
With :class:’ivy.NativeArray’ input:
>>> x = ivy.native_array([[1., 2.], [2., 3.]]) >>> y = ivy.native_array([[3., 4.], [4., 5.]]) >>> res = ivy.tensordot(x, y, axes = ([1],[1])) >>> print(res) ivy.array([[11., 14.], [18., 23.]])
With a mix of
ivy.Array
andivy.NativeArray
inputs:>>> x = ivy.array([[1., 0., 1.], [2., 3., 6.], [0., 7., 2.]]) >>> y = ivy.native_array([[1.], [2.], [3.]]) >>> res = ivy.tensordot(x, y, axes = 1) >>> print(res) ivy.array([[ 4.], [26.], [20.]])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([[1., 0., 3.], [2., 3., 4.]]), ... b=ivy.array([[5., 6., 7.], [3., 4., 8.]])) >>> y = ivy.Container(a=ivy.array([[2., 4., 5.], [9., 10., 6.]]), ... b=ivy.array([[1., 0., 3.], [2., 3., 4.]])) >>> res = ivy.tensordot(x, y) >>> print(res) { a: ivy.array(89.), b: ivy.array(76.) }