MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / tensordot

Function tensordot

tensorflow/python/ops/math_ops.py:3911–4081  ·  view source on GitHub ↗

r"""Tensor contraction of a and b along specified axes and outer product. Tensordot (also known as tensor contraction) sums the product of elements from `a` and `b` over the indices specified by `a_axes` and `b_axes`. The lists `a_axes` and `b_axes` specify those pairs of axes along which to

(a, b, axes, name=None)

Source from the content-addressed store, hash-verified

3909
3910@tf_export("tensordot", "linalg.tensordot")
3911def tensordot(a, b, axes, name=None):
3912 r"""Tensor contraction of a and b along specified axes and outer product.
3913
3914 Tensordot (also known as tensor contraction) sums the product of elements
3915 from `a` and `b` over the indices specified by `a_axes` and `b_axes`.
3916 The lists `a_axes` and `b_axes` specify those pairs of axes along which to
3917 contract the tensors. The axis `a_axes[i]` of `a` must have the same dimension
3918 as axis `b_axes[i]` of `b` for all `i` in `range(0, len(a_axes))`. The lists
3919 `a_axes` and `b_axes` must have identical length and consist of unique
3920 integers that specify valid axes for each of the tensors. Additionally
3921 outer product is supported by passing `axes=0`.
3922
3923 This operation corresponds to `numpy.tensordot(a, b, axes)`.
3924
3925 Example 1: When `a` and `b` are matrices (order 2), the case `axes = 1`
3926 is equivalent to matrix multiplication.
3927
3928 Example 2: When `a` and `b` are matrices (order 2), the case
3929 `axes = [[1], [0]]` is equivalent to matrix multiplication.
3930
3931 Example 3: When `a` and `b` are matrices (order 2), the case `axes=0` gives
3932 the outer product, a tensor of order 4.
3933
3934 Example 4: Suppose that \\(a_{ijk}\\) and \\(b_{lmn}\\) represent two
3935 tensors of order 3. Then, `contract(a, b, [[0], [2]])` is the order 4 tensor
3936 \\(c_{jklm}\\) whose entry
3937 corresponding to the indices \\((j,k,l,m)\\) is given by:
3938
3939 \\( c_{jklm} = \sum_i a_{ijk} b_{lmi} \\).
3940
3941 In general, `order(c) = order(a) + order(b) - 2*len(axes[0])`.
3942
3943 Args:
3944 a: `Tensor` of type `float32` or `float64`.
3945 b: `Tensor` with the same type as `a`.
3946 axes: Either a scalar `N`, or a list or an `int32` `Tensor` of shape [2, k].
3947 If axes is a scalar, sum over the last N axes of a and the first N axes of
3948 b in order. If axes is a list or `Tensor` the first and second row contain
3949 the set of unique integers specifying axes along which the contraction is
3950 computed, for `a` and `b`, respectively. The number of axes for `a` and
3951 `b` must be equal. If `axes=0`, computes the outer product between `a` and
3952 `b`.
3953 name: A name for the operation (optional).
3954
3955 Returns:
3956 A `Tensor` with the same type as `a`.
3957
3958 Raises:
3959 ValueError: If the shapes of `a`, `b`, and `axes` are incompatible.
3960 IndexError: If the values in axes exceed the rank of the corresponding
3961 tensor.
3962 """
3963
3964 def _tensordot_reshape(a, axes, flipped=False):
3965 """Helper method to perform transpose and reshape for contraction op.
3966
3967 This method is helpful in reducing `math_ops.tensordot` to `math_ops.matmul`
3968 using `array_ops.transpose` and `array_ops.reshape`. The method takes a

Callers

nothing calls this directly

Calls 7

_tensordot_axesFunction · 0.85
_tensordot_reshapeFunction · 0.85
reshapeMethod · 0.80
matmulFunction · 0.70
name_scopeMethod · 0.45
concatMethod · 0.45
set_shapeMethod · 0.45

Tested by

no test coverage detected