1 | /**
|
2 | * @license
|
3 | * Copyright 2021 Google LLC. All Rights Reserved.
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | * =============================================================================
|
16 | */
|
17 | /// <amd-module name="@tensorflow/tfjs-core/dist/ops/einsum" />
|
18 | import { Tensor } from '../tensor';
|
19 | /**
|
20 | * Tensor contraction over specified indices and outer product.
|
21 | *
|
22 | * `einsum` allows defining Tensors by defining their element-wise computation.
|
23 | * This computation is based on
|
24 | * [Einstein summation](https://en.wikipedia.org/wiki/Einstein_notation).
|
25 | *
|
26 | * Some special cases include:
|
27 | *
|
28 | * Matrix multiplication:
|
29 | * ```js
|
30 | * const x = tf.tensor2d([[1, 2, 3], [4, 5, 6]]);
|
31 | * const y = tf.tensor2d([[0, 1], [2, 3], [4, 5]]);
|
32 | * x.print();
|
33 | * y.print();
|
34 | * tf.einsum('ij,jk->ik', x, y).print();
|
35 | * ```
|
36 | *
|
37 | * Dot product:
|
38 | * ```js
|
39 | * const x = tf.tensor1d([1, 2, 3]);
|
40 | * const y = tf.tensor1d([0, 1, 2]);
|
41 | * x.print();
|
42 | * y.print();
|
43 | * tf.einsum('i,i->', x, y).print();
|
44 | * ```
|
45 | *
|
46 | * Batch dot product:
|
47 | * ```js
|
48 | * const x = tf.tensor2d([[1, 2, 3], [4, 5, 6]]);
|
49 | * const y = tf.tensor2d([[0, 1, 2], [3, 4, 5]]);
|
50 | * x.print();
|
51 | * y.print();
|
52 | * tf.einsum('bi,bi->b', x, y).print();
|
53 | * ```
|
54 | *
|
55 | * Outer prouduct:
|
56 | * ```js
|
57 | * const x = tf.tensor1d([1, 3, 5]);
|
58 | * const y = tf.tensor1d([2, 4, 6]);
|
59 | * x.print();
|
60 | * y.print();
|
61 | * tf.einsum('i,j->ij', x, y).print();
|
62 | * ```
|
63 | *
|
64 | * Matrix transpose:
|
65 | * ```js
|
66 | * const x = tf.tensor2d([[1, 2], [3, 4]]);
|
67 | * x.print();
|
68 | * tf.einsum('ij->ji', x).print();
|
69 | * ```
|
70 | *
|
71 | * Batch matrix transpose:
|
72 | * ```js
|
73 | * const x = tf.tensor3d([[[1, 2], [3, 4]], [[-1, -2], [-3, -4]]]);
|
74 | * x.print();
|
75 | * tf.einsum('bij->bji', x).print();
|
76 | * ```
|
77 | *
|
78 | * Limitations:
|
79 | *
|
80 | * This implementation of einsum has the following limitations:
|
81 | *
|
82 | * - Does not support >2 input tensors.
|
83 | * - Does not support duplicate axes for any given input tensor. E.g., equation
|
84 | * 'ii->' is not suppoted.
|
85 | * - The `...` notation is not supported.
|
86 | *
|
87 | * @param equation a string describing the contraction, in the same format as
|
88 | * [numpy.einsum](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html).
|
89 | * @param tensors the input(s) to contract (each one a Tensor), whose shapes
|
90 | * should be consistent with equation.
|
91 | * @returns The output tensor.
|
92 | *
|
93 | * @doc {heading: 'Tensors', subheading: 'Matrices'}
|
94 | */
|
95 | export declare function einsum_(equation: string, ...tensors: Tensor[]): Tensor;
|
96 | export declare const einsum: typeof einsum_;
|