Tensor contraction over specified indices and outer product. This function returns a tensor whose elements are defined by `equation`, 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 matri
(equation, *inputs, **kwargs)
| 169 | |
| 170 | @tf_export('einsum', 'linalg.einsum') |
| 171 | def einsum(equation, *inputs, **kwargs): |
| 172 | """Tensor contraction over specified indices and outer product. |
| 173 | |
| 174 | This function returns a tensor whose elements are defined by `equation`, |
| 175 | which is written in a shorthand form inspired by the Einstein summation |
| 176 | convention. As an example, consider multiplying two matrices |
| 177 | A and B to form a matrix C. The elements of C are given by: |
| 178 | |
| 179 | ``` |
| 180 | C[i,k] = sum_j A[i,j] * B[j,k] |
| 181 | ``` |
| 182 | |
| 183 | The corresponding `equation` is: |
| 184 | |
| 185 | ``` |
| 186 | ij,jk->ik |
| 187 | ``` |
| 188 | |
| 189 | In general, the `equation` is obtained from the more familiar element-wise |
| 190 | equation by |
| 191 | 1. removing variable names, brackets, and commas, |
| 192 | 2. replacing "*" with ",", |
| 193 | 3. dropping summation signs, and |
| 194 | 4. moving the output to the right, and replacing "=" with "->". |
| 195 | |
| 196 | Many common operations can be expressed in this way. For example: |
| 197 | |
| 198 | ```python |
| 199 | # Matrix multiplication |
| 200 | >>> einsum('ij,jk->ik', m0, m1) # output[i,k] = sum_j m0[i,j] * m1[j, k] |
| 201 | |
| 202 | # Dot product |
| 203 | >>> einsum('i,i->', u, v) # output = sum_i u[i]*v[i] |
| 204 | |
| 205 | # Outer product |
| 206 | >>> einsum('i,j->ij', u, v) # output[i,j] = u[i]*v[j] |
| 207 | |
| 208 | # Transpose |
| 209 | >>> einsum('ij->ji', m) # output[j,i] = m[i,j] |
| 210 | |
| 211 | # Trace |
| 212 | >>> einsum('ii', m) # output[j,i] = trace(m) = sum_i m[i, i] |
| 213 | |
| 214 | # Batch matrix multiplication |
| 215 | >>> einsum('aij,ajk->aik', s, t) # out[a,i,k] = sum_j s[a,i,j] * t[a, j, k] |
| 216 | ``` |
| 217 | |
| 218 | To enable and control broadcasting, use an ellipsis. For example, to do |
| 219 | batch matrix multiplication, you could use: |
| 220 | |
| 221 | ```python |
| 222 | >>> einsum('...ij,...jk->...ik', u, v) |
| 223 | ``` |
| 224 | |
| 225 | This function behaves like `numpy.einsum`, but does not support: |
| 226 | |
| 227 | * Subscripts where an axis appears more than once for a single input |
| 228 | (e.g. `ijj,k->ik`) unless it is a trace (e.g. `ijji`). |
nothing calls this directly
no test coverage detected