tf.einsum( equation, *inputs, **kwargs )
See the guide: Math > Reduction
A generalized contraction between tensors of arbitrary dimension.
This function returns a tensor whose elements are defined by
which is written in a shorthand form inspired by the Einstein summation
convention. As an example, consider multiplying two matrices
A and B to form a matrix C. The elements of C are given by:
C[i,k] = sum_j A[i,j] * B[j,k]
In general, the
equation is obtained from the more familiar element-wise
1. removing variable names, brackets, and commas,
2. replacing "*" with ",",
3. dropping summation signs, and
4. moving the output to the right, and replacing "=" with "->".
Many common operations can be expressed in this way. For example:
.html# Matrix multiplication >>> einsum('ij,jk->ik', m0, m1) .html# output[i,k] = sum_j m0[i,j] * m1[j, k] .html# Dot product >>> einsum('i,i->', u, v) .html# output = sum_i u[i]*v[i] .html# Outer product >>> einsum('i,j->ij', u, v) .html# output[i,j] = u[i]*v[j] .html# Transpose >>> einsum('ij->ji', m) .html# output[j,i] = m[i,j] .html# Batch matrix multiplication >>> einsum('aij,ajk->aik', s, t) .html# out[a,i,k] = sum_j s[a,i,j] * t[a, j, k]
This function behaves like
numpy.einsum, but does not support:
- Ellipses (subscripts like
- Subscripts where an axis appears more than once for a single input
- Subscripts that are summed across multiple inputs (e.g.,
strdescribing the contraction, in the same format as
*inputs: the inputs to contract (each one a
Tensor), whose shapes should be consistent with
name: A name for the operation (optional).
Tensor, with shape determined by
- the format of
- the number of inputs implied by
equationdoes not match
- an axis appears in the output subscripts but not in any of the inputs,
- the number of dimensions of an input differs from the number of indices in its subscript, or
- the input shapes are inconsistent along a particular axis.
- the format of