gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * @license
 * Copyright 2023 Google LLC.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */
/// <amd-module name="@tensorflow/tfjs-layers/dist/layers/nlp/einsum_dense" />
/**
 *  TFJS-based einsum dense layer.
 */
import { Tensor, Tensor2D, serialization } from '@tensorflow/tfjs-core';
import { ConstraintIdentifier } from '../../constraints';
import { Layer, LayerArgs } from '../../engine/topology';
import { InitializerIdentifier } from '../../initializers';
import { ActivationIdentifier } from '../../keras_format/activation_config';
import { Shape } from '../../keras_format/common';
import { RegularizerIdentifier } from '../../regularizers';
import { Kwargs } from '../../types';
import { LayerVariable } from '../../variables';
/**
 * Analyzes an einsum string to determine the required weight shape.
 */
export declare function analyzeEinsumString(equation: string, biasAxes: string, inputShape: Shape, outputShape: Shape): [Shape, Shape, Shape];
/**
 * Analyze an pre-split einsum string to find the weight shape.
 */
export declare function analyzeSplitString(splitString: RegExpMatchArray, biasAxes: string, inputShape: Shape, outputShape: Shape | number, leftElided?: boolean): [Shape, Shape, Shape];
export declare interface EinsumDenseArgs extends LayerArgs {
    /**
     * An equation describing the einsum to perform. This equation must be a
     * valid einsum string of the form `ab,bc->ac`, `...ab,bc->...ac`, or
     * `ab...,bc->ac...` where 'ab', 'bc', and 'ac' can be any valid einsum
     * axis expression sequence.
     */
    equation: string;
    /**
     * The expected shape of the output tensor (excluding the batch dimension and
     * any dimensions represented by ellipses). You can specify None for any
     * dimension that is unknown or can be inferred from the input shape.
     */
    outputShape: Shape | number;
    /**
     * Activation function to use. If you don't specify anything, no activation
     * is applied (that is, a "linear" activation: `a(x) = x`).
     */
    activation?: ActivationIdentifier;
    /**
     * A string containing the output dimension(s) to apply a bias to. Each
     * character in the `biasAxes` string should correspond to a character
     * in the output portion of the `equation` string.
     */
    biasAxes?: string;
    /**
     * Initializer for the `kernel` weights matrix.
     * Defaults to `"glorotUniform"`.
     */
    kernelInitializer?: InitializerIdentifier;
    /**
     * Initializer for the bias vector.
     * Defaults to `"zeros"`.
     */
    biasInitializer?: InitializerIdentifier;
    /**
     * Regularizer function applied to the `kernel` weights matrix.
     */
    kernelRegularizer?: RegularizerIdentifier;
    /**
     * Regularizer function applied to the bias vector.
     */
    biasRegularizer?: RegularizerIdentifier;
    /**
     * Regularizer function applied to the output of the layer (its "activation").
     */
    activityRegularizer?: RegularizerIdentifier;
    /**
     * Constraint function applied to the `kernel` weights matrix.
     */
    kernelConstraint?: ConstraintIdentifier;
    /**
     * Constraint function applied to the bias vector.
     */
    biasConstraint?: ConstraintIdentifier;
}
/**
 * A layer that uses `tf.einsum` as the backing computation.
 *
 * This layer can perform einsum calculations of arbitrary dimensionality.
 *
 * Examples:
 *
 * **Biased dense layer with einsums**
 *
 * This example shows how to instantiate a standard Keras dense layer using
 * einsum operations. This example is equivalent to
 * tf.layers.Dense({units: 64, useBias: true})`.
 *
 * const layer = new EinsumDense({
 *    equation: "ab,bc->ac", outputShape: 4, biasAxes: "c"});
 * const inputTensor = tf.input({shape: [32]});
 * const outputTensor = layer.call(inputTensor);
 * console.log(outputTensor);  // [null, 64]
 *
 * **Applying a dense layer to a sequence**
 *
 * This example shows how to instantiate a layer that applies the same dense
 * operation to every element in a sequence. Here, the `outputShape` has two
 * values (since there are two non-batch dimensions in the output); the first
 * dimension in the `outputShape` is `null`, because the sequence dimension
 * `b` has an unknown shape.
 *
 * ```js
 * const layer = new EinsumDense({
 *    equation: "abc,cd->abd", outputShape: [null, 64], biasAxes: "d"});
 * const inputTensor = tf.input({shape: [32, 128]});
 * const outputTensor = layer.call(inputTensor);
 * console.log(outputTensor);  // [null, 32, 64]
 * ```
 *
 * **Applying a dense layer to a sequence using ellipses**
 *
 * This example shows how to instantiate a layer that applies the same dense
 * operation to every element in a sequence, but uses the ellipsis notation
 * instead of specifying the batch and sequence dimensions.
 *
 * Because we are using ellipsis notation and have specified only one axis, the
 * `outputShape` arg is a single value. When instantiated in this way, the
 * layer can handle any number of sequence dimensions - including the case
 * where no sequence dimension exists.
 *
 * ```js
 * const layer = new EinsumDense({
 *    equation: "...x,xy->...y", outputShape: 64, biasAxes: "y"});
 * const inputTensor = tf.input({shape: [32, 128]});
 * const outputTensor = layer.call(inputTensor);
 * console.log(outputTensor);  // [null, 32, 64]
 * ``
 */
export declare class EinsumDense extends Layer {
    /** @nocollapse */
    static readonly className = "EinsumDense";
    private readonly equation;
    private readonly biasAxes;
    private readonly partialOutputShape;
    private readonly activation;
    private readonly kernelInitializer;
    private readonly biasInitializer;
    private readonly kernelRegularizer;
    private readonly biasRegularizer;
    private readonly kernelConstraint;
    private readonly biasConstraint;
    private fullOutputShape;
    private _kernel;
    private _bias;
    constructor(args: EinsumDenseArgs);
    get kernel(): LayerVariable;
    get bias(): LayerVariable;
    build(inputShape: Shape): void;
    computeOutputShape(_: Shape): Shape;
    getConfig(): serialization.ConfigDict;
    call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor2D;
}