tf.einsum
Stay organized with collections
Save and categorize content based on your preferences.
Tensor contraction over specified indices and outer product.
tf.einsum(
equation, *inputs, **kwargs
)
Einsum allows defining Tensors by defining their element-wise computation.
This computation is defined by equation
, a shorthand form based on Einstein
summation. 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]
The corresponding equation
is:
ij,jk->ik
In general, to convert the element-wise equation into the equation
string,
use the following procedure (intermediate strings for matrix multiplication
example provided in parentheses):
- remove variable names, brackets, and commas, (
ik = sum_j ij * jk
)
- replace "*" with ",", (
ik = sum_j ij , jk
)
- drop summation signs, and (
ik = ij, jk
)
- move the output to the right, while replacing "=" with "->". (
ij,jk->ik
)
Many common operations can be expressed in this way. For example:
# Matrix multiplication
einsum('ij,jk->ik', m0, m1) # output[i,k] = sum_j m0[i,j] * m1[j, k]
# Dot product
einsum('i,i->', u, v) # output = sum_i u[i]*v[i]
# Outer product
einsum('i,j->ij', u, v) # output[i,j] = u[i]*v[j]
# Transpose
einsum('ij->ji', m) # output[j,i] = m[i,j]
# Trace
einsum('ii', m) # output[j,i] = trace(m) = sum_i m[i, i]
# Batch matrix multiplication
einsum('aij,ajk->aik', s, t) # out[a,i,k] = sum_j s[a,i,j] * t[a, j, k]
To enable and control broadcasting, use an ellipsis. For example, to perform
batch matrix multiplication with NumPy-style broadcasting across the batch
dimensions, use:
einsum('...ij,...jk->...ik', u, v)
Args |
equation
|
a str describing the contraction, in the same format as
numpy.einsum .
|
*inputs
|
the inputs to contract (each one a Tensor ), whose shapes should
be consistent with equation .
|
**kwargs
|
- optimize: Optimization strategy to use to find contraction path using
opt_einsum. Must be 'greedy', 'optimal', 'branch-2', 'branch-all' or
'auto'. (optional, default: 'greedy').
- name: A name for the operation (optional).
|
Returns |
The contracted Tensor , with shape determined by equation .
|
Raises |
ValueError
|
If
- the format of
equation is incorrect,
- number of inputs or their shapes are inconsistent with
equation .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.einsum\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/einsum) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/ops/special_math_ops.py#L606-L684) |\n\nTensor contraction over specified indices and outer product.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.linalg.einsum`](/api_docs/python/tf/einsum)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.einsum`](/api_docs/python/tf/einsum), [`tf.compat.v1.linalg.einsum`](/api_docs/python/tf/einsum)\n\n\u003cbr /\u003e\n\n tf.einsum(\n equation, *inputs, **kwargs\n )\n\nEinsum allows defining Tensors by defining their element-wise computation.\nThis computation is defined by `equation`, a shorthand form based on Einstein\nsummation. As an example, consider multiplying two matrices A and B to form a\nmatrix C. The elements of C are given by: \n\n C[i,k] = sum_j A[i,j] * B[j,k]\n\nThe corresponding `equation` is: \n\n ij,jk-\u003eik\n\nIn general, to convert the element-wise equation into the `equation` string,\nuse the following procedure (intermediate strings for matrix multiplication\nexample provided in parentheses):\n\n1. remove variable names, brackets, and commas, (`ik = sum_j ij * jk`)\n2. replace \"\\*\" with \",\", (`ik = sum_j ij , jk`)\n3. drop summation signs, and (`ik = ij, jk`)\n4. move the output to the right, while replacing \"=\" with \"-\\\u003e\". (`ij,jk-\u003eik`)\n\nMany common operations can be expressed in this way. For example: \n\n # Matrix multiplication\n einsum('ij,jk-\u003eik', m0, m1) # output[i,k] = sum_j m0[i,j] * m1[j, k]\n\n # Dot product\n einsum('i,i-\u003e', u, v) # output = sum_i u[i]*v[i]\n\n # Outer product\n einsum('i,j-\u003eij', u, v) # output[i,j] = u[i]*v[j]\n\n # Transpose\n einsum('ij-\u003eji', m) # output[j,i] = m[i,j]\n\n # Trace\n einsum('ii', m) # output[j,i] = trace(m) = sum_i m[i, i]\n\n # Batch matrix multiplication\n einsum('aij,ajk-\u003eaik', s, t) # out[a,i,k] = sum_j s[a,i,j] * t[a, j, k]\n\nTo enable and control broadcasting, use an ellipsis. For example, to perform\nbatch matrix multiplication with NumPy-style broadcasting across the batch\ndimensions, use: \n\n einsum('...ij,...jk-\u003e...ik', u, v)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `equation` | a `str` describing the contraction, in the same format as `numpy.einsum`. |\n| `*inputs` | the inputs to contract (each one a `Tensor`), whose shapes should be consistent with `equation`. |\n| `**kwargs` | \u003cbr /\u003e - optimize: Optimization strategy to use to find contraction path using opt_einsum. Must be 'greedy', 'optimal', 'branch-2', 'branch-all' or 'auto'. (optional, default: 'greedy'). - name: A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| The contracted `Tensor`, with shape determined by `equation`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If \u003cbr /\u003e - the format of `equation` is incorrect, - number of inputs or their shapes are inconsistent with `equation`. |"]]