1 | /**
|
2 | * @license
|
3 | * Copyright 2018 Google LLC
|
4 | *
|
5 | * Use of this source code is governed by an MIT-style
|
6 | * license that can be found in the LICENSE file or at
|
7 | * https://opensource.org/licenses/MIT.
|
8 | * =============================================================================
|
9 | */
|
10 | /// <amd-module name="@tensorflow/tfjs-layers/dist/layers/recurrent" />
|
11 | /**
|
12 | * TensorFlow.js Layers: Recurrent Neural Network Layers.
|
13 | */
|
14 | import * as tfc from '@tensorflow/tfjs-core';
|
15 | import { serialization, Tensor } from '@tensorflow/tfjs-core';
|
16 | import { Activation } from '../activations';
|
17 | import { Constraint, ConstraintIdentifier } from '../constraints';
|
18 | import { InputSpec, SymbolicTensor } from '../engine/topology';
|
19 | import { Layer, LayerArgs } from '../engine/topology';
|
20 | import { Initializer, InitializerIdentifier } from '../initializers';
|
21 | import { ActivationIdentifier } from '../keras_format/activation_config';
|
22 | import { Shape } from '../keras_format/common';
|
23 | import { Regularizer, RegularizerIdentifier } from '../regularizers';
|
24 | import { Kwargs, RnnStepFunction } from '../types';
|
25 | import { LayerVariable } from '../variables';
|
26 | /**
|
27 | * Standardize `apply()` args to a single list of tensor inputs.
|
28 | *
|
29 | * When running a model loaded from file, the input tensors `initialState` and
|
30 | * `constants` are passed to `RNN.apply()` as part of `inputs` instead of the
|
31 | * dedicated kwargs fields. `inputs` consists of
|
32 | * `[inputs, initialState0, initialState1, ..., constant0, constant1]` in this
|
33 | * case.
|
34 | * This method makes sure that arguments are
|
35 | * separated and that `initialState` and `constants` are `Array`s of tensors
|
36 | * (or None).
|
37 | *
|
38 | * @param inputs Tensor or `Array` of tensors.
|
39 | * @param initialState Tensor or `Array` of tensors or `null`/`undefined`.
|
40 | * @param constants Tensor or `Array` of tensors or `null`/`undefined`.
|
41 | * @returns An object consisting of
|
42 | * inputs: A tensor.
|
43 | * initialState: `Array` of tensors or `null`.
|
44 | * constants: `Array` of tensors or `null`.
|
45 | * @throws ValueError, if `inputs` is an `Array` but either `initialState` or
|
46 | * `constants` is provided.
|
47 | */
|
48 | export declare function standardizeArgs(inputs: Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[], initialState: Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[], constants: Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[], numConstants?: number): {
|
49 | inputs: Tensor | SymbolicTensor;
|
50 | initialState: Tensor[] | SymbolicTensor[];
|
51 | constants: Tensor[] | SymbolicTensor[];
|
52 | };
|
53 | /**
|
54 | * Iterates over the time dimension of a tensor.
|
55 | *
|
56 | * @param stepFunction RNN step function.
|
57 | * Parameters:
|
58 | * inputs: tensor with shape `[samples, ...]` (no time dimension),
|
59 | * representing input for the batch of samples at a certain time step.
|
60 | * states: an Array of tensors.
|
61 | * Returns:
|
62 | * outputs: tensor with shape `[samples, outputDim]` (no time dimension).
|
63 | * newStates: list of tensors, same length and shapes as `states`. The first
|
64 | * state in the list must be the output tensor at the previous timestep.
|
65 | * @param inputs Tensor of temporal data of shape `[samples, time, ...]` (at
|
66 | * least 3D).
|
67 | * @param initialStates Tensor with shape `[samples, outputDim]` (no time
|
68 | * dimension), containing the initial values of the states used in the step
|
69 | * function.
|
70 | * @param goBackwards If `true`, do the iteration over the time dimension in
|
71 | * reverse order and return the reversed sequence.
|
72 | * @param mask Binary tensor with shape `[sample, time, 1]`, with a zero for
|
73 | * every element that is masked.
|
74 | * @param constants An Array of constant values passed at each step.
|
75 | * @param unroll Whether to unroll the RNN or to use a symbolic loop. *Not*
|
76 | * applicable to this imperative deeplearn.js backend. Its value is ignored.
|
77 | * @param needPerStepOutputs Whether the per-step outputs are to be
|
78 | * concatenated into a single tensor and returned (as the second return
|
79 | * value). Default: `false`. This arg is included so that the relatively
|
80 | * expensive concatenation of the stepwise outputs can be omitted unless
|
81 | * the stepwise outputs need to be kept (e.g., for an LSTM layer of which
|
82 | * `returnSequence` is `true`.)
|
83 | * @returns An Array: `[lastOutput, outputs, newStates]`.
|
84 | * lastOutput: the lastest output of the RNN, of shape `[samples, ...]`.
|
85 | * outputs: tensor with shape `[samples, time, ...]` where each entry
|
86 | * `output[s, t]` is the output of the step function at time `t` for sample
|
87 | * `s`. This return value is provided if and only if the
|
88 | * `needPerStepOutputs` is set as `true`. If it is set as `false`, this
|
89 | * return value will be `undefined`.
|
90 | * newStates: Array of tensors, latest states returned by the step function,
|
91 | * of shape `(samples, ...)`.
|
92 | * @throws ValueError If input dimension is less than 3.
|
93 | *
|
94 | * TODO(nielsene): This needs to be tidy-ed.
|
95 | */
|
96 | export declare function rnn(stepFunction: RnnStepFunction, inputs: Tensor, initialStates: Tensor[], goBackwards?: boolean, mask?: Tensor, constants?: Tensor[], unroll?: boolean, needPerStepOutputs?: boolean): [Tensor, Tensor, Tensor[]];
|
97 | export declare interface BaseRNNLayerArgs extends LayerArgs {
|
98 | /**
|
99 | * A RNN cell instance. A RNN cell is a class that has:
|
100 | * - a `call()` method, which takes `[Tensor, Tensor]` as the
|
101 | * first input argument. The first item is the input at time t, and
|
102 | * second item is the cell state at time t.
|
103 | * The `call()` method returns `[outputAtT, statesAtTPlus1]`.
|
104 | * The `call()` method of the cell can also take the argument `constants`,
|
105 | * see section "Note on passing external constants" below.
|
106 | * Porting Node: PyKeras overrides the `call()` signature of RNN cells,
|
107 | * which are Layer subtypes, to accept two arguments. tfjs-layers does
|
108 | * not do such overriding. Instead we preserve the `call()` signature,
|
109 | * which due to its `Tensor|Tensor[]` argument and return value is
|
110 | * flexible enough to handle the inputs and states.
|
111 | * - a `stateSize` attribute. This can be a single integer (single state)
|
112 | * in which case it is the size of the recurrent state (which should be
|
113 | * the same as the size of the cell output). This can also be an Array of
|
114 | * integers (one size per state). In this case, the first entry
|
115 | * (`stateSize[0]`) should be the same as the size of the cell output.
|
116 | * It is also possible for `cell` to be a list of RNN cell instances, in which
|
117 | * case the cells get stacked on after the other in the RNN, implementing an
|
118 | * efficient stacked RNN.
|
119 | */
|
120 | cell?: RNNCell | RNNCell[];
|
121 | /**
|
122 | * Whether to return the last output in the output sequence, or the full
|
123 | * sequence.
|
124 | */
|
125 | returnSequences?: boolean;
|
126 | /**
|
127 | * Whether to return the last state in addition to the output.
|
128 | */
|
129 | returnState?: boolean;
|
130 | /**
|
131 | * If `true`, process the input sequence backwards and return the reversed
|
132 | * sequence (default: `false`).
|
133 | */
|
134 | goBackwards?: boolean;
|
135 | /**
|
136 | * If `true`, the last state for each sample at index i in a batch will be
|
137 | * used as initial state of the sample of index i in the following batch
|
138 | * (default: `false`).
|
139 | *
|
140 | * You can set RNN layers to be "stateful", which means that the states
|
141 | * computed for the samples in one batch will be reused as initial states
|
142 | * for the samples in the next batch. This assumes a one-to-one mapping
|
143 | * between samples in different successive batches.
|
144 | *
|
145 | * To enable "statefulness":
|
146 | * - specify `stateful: true` in the layer constructor.
|
147 | * - specify a fixed batch size for your model, by passing
|
148 | * - if sequential model:
|
149 | * `batchInputShape: [...]` to the first layer in your model.
|
150 | * - else for functional model with 1 or more Input layers:
|
151 | * `batchShape: [...]` to all the first layers in your model.
|
152 | * This is the expected shape of your inputs
|
153 | * *including the batch size*.
|
154 | * It should be a tuple of integers, e.g., `[32, 10, 100]`.
|
155 | * - specify `shuffle: false` when calling `LayersModel.fit()`.
|
156 | *
|
157 | * To reset the state of your model, call `resetStates()` on either the
|
158 | * specific layer or on the entire model.
|
159 | */
|
160 | stateful?: boolean;
|
161 | /**
|
162 | * If `true`, the network will be unrolled, else a symbolic loop will be
|
163 | * used. Unrolling can speed up a RNN, although it tends to be more
|
164 | * memory-intensive. Unrolling is only suitable for short sequences (default:
|
165 | * `false`).
|
166 | * Porting Note: tfjs-layers has an imperative backend. RNNs are executed with
|
167 | * normal TypeScript control flow. Hence this property is inapplicable and
|
168 | * ignored in tfjs-layers.
|
169 | */
|
170 | unroll?: boolean;
|
171 | /**
|
172 | * Dimensionality of the input (integer).
|
173 | * This option (or alternatively, the option `inputShape`) is required when
|
174 | * this layer is used as the first layer in a model.
|
175 | */
|
176 | inputDim?: number;
|
177 | /**
|
178 | * Length of the input sequences, to be specified when it is constant.
|
179 | * This argument is required if you are going to connect `Flatten` then
|
180 | * `Dense` layers upstream (without it, the shape of the dense outputs cannot
|
181 | * be computed). Note that if the recurrent layer is not the first layer in
|
182 | * your model, you would need to specify the input length at the level of the
|
183 | * first layer (e.g., via the `inputShape` option).
|
184 | */
|
185 | inputLength?: number;
|
186 | }
|
187 | export declare class RNN extends Layer {
|
188 | /** @nocollapse */
|
189 | static className: string;
|
190 | readonly cell: RNNCell;
|
191 | readonly returnSequences: boolean;
|
192 | readonly returnState: boolean;
|
193 | readonly goBackwards: boolean;
|
194 | readonly unroll: boolean;
|
195 | stateSpec: InputSpec[];
|
196 | protected states_: Tensor[];
|
197 | protected keptStates: Tensor[][];
|
198 | private numConstants;
|
199 | constructor(args: RNNLayerArgs);
|
200 | getStates(): Tensor[];
|
201 | setStates(states: Tensor[]): void;
|
202 | computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
|
203 | computeMask(inputs: Tensor | Tensor[], mask?: Tensor | Tensor[]): Tensor | Tensor[];
|
204 | /**
|
205 | * Get the current state tensors of the RNN.
|
206 | *
|
207 | * If the state hasn't been set, return an array of `null`s of the correct
|
208 | * length.
|
209 | */
|
210 | get states(): Tensor[];
|
211 | set states(s: Tensor[]);
|
212 | build(inputShape: Shape | Shape[]): void;
|
213 | /**
|
214 | * Reset the state tensors of the RNN.
|
215 | *
|
216 | * If the `states` argument is `undefined` or `null`, will set the
|
217 | * state tensor(s) of the RNN to all-zero tensors of the appropriate
|
218 | * shape(s).
|
219 | *
|
220 | * If `states` is provided, will set the state tensors of the RNN to its
|
221 | * value.
|
222 | *
|
223 | * @param states Optional externally-provided initial states.
|
224 | * @param training Whether this call is done during training. For stateful
|
225 | * RNNs, this affects whether the old states are kept or discarded. In
|
226 | * particular, if `training` is `true`, the old states will be kept so
|
227 | * that subsequent backpropgataion through time (BPTT) may work properly.
|
228 | * Else, the old states will be discarded.
|
229 | */
|
230 | resetStates(states?: Tensor | Tensor[], training?: boolean): void;
|
231 | apply(inputs: Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[], kwargs?: Kwargs): Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[];
|
232 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
233 | getInitialState(inputs: Tensor): Tensor[];
|
234 | get trainableWeights(): LayerVariable[];
|
235 | get nonTrainableWeights(): LayerVariable[];
|
236 | setFastWeightInitDuringBuild(value: boolean): void;
|
237 | getConfig(): serialization.ConfigDict;
|
238 | /** @nocollapse */
|
239 | static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict, customObjects?: serialization.ConfigDict): T;
|
240 | }
|
241 | /**
|
242 | * An RNNCell layer.
|
243 | *
|
244 | * @doc {heading: 'Layers', subheading: 'Classes'}
|
245 | */
|
246 | export declare abstract class RNNCell extends Layer {
|
247 | /**
|
248 | * Size(s) of the states.
|
249 | * For RNN cells with only a single state, this is a single integer.
|
250 | */
|
251 | abstract stateSize: number | number[];
|
252 | dropoutMask: Tensor | Tensor[];
|
253 | recurrentDropoutMask: Tensor | Tensor[];
|
254 | }
|
255 | export declare interface SimpleRNNCellLayerArgs extends LayerArgs {
|
256 | /**
|
257 | * units: Positive integer, dimensionality of the output space.
|
258 | */
|
259 | units: number;
|
260 | /**
|
261 | * Activation function to use.
|
262 | * Default: hyperbolic tangent ('tanh').
|
263 | * If you pass `null`, 'linear' activation will be applied.
|
264 | */
|
265 | activation?: ActivationIdentifier;
|
266 | /**
|
267 | * Whether the layer uses a bias vector.
|
268 | */
|
269 | useBias?: boolean;
|
270 | /**
|
271 | * Initializer for the `kernel` weights matrix, used for the linear
|
272 | * transformation of the inputs.
|
273 | */
|
274 | kernelInitializer?: InitializerIdentifier | Initializer;
|
275 | /**
|
276 | * Initializer for the `recurrentKernel` weights matrix, used for
|
277 | * linear transformation of the recurrent state.
|
278 | */
|
279 | recurrentInitializer?: InitializerIdentifier | Initializer;
|
280 | /**
|
281 | * Initializer for the bias vector.
|
282 | */
|
283 | biasInitializer?: InitializerIdentifier | Initializer;
|
284 | /**
|
285 | * Regularizer function applied to the `kernel` weights matrix.
|
286 | */
|
287 | kernelRegularizer?: RegularizerIdentifier | Regularizer;
|
288 | /**
|
289 | * Regularizer function applied to the `recurrent_kernel` weights matrix.
|
290 | */
|
291 | recurrentRegularizer?: RegularizerIdentifier | Regularizer;
|
292 | /**
|
293 | * Regularizer function applied to the bias vector.
|
294 | */
|
295 | biasRegularizer?: RegularizerIdentifier | Regularizer;
|
296 | /**
|
297 | * Constraint function applied to the `kernel` weights matrix.
|
298 | */
|
299 | kernelConstraint?: ConstraintIdentifier | Constraint;
|
300 | /**
|
301 | * Constraint function applied to the `recurrentKernel` weights matrix.
|
302 | */
|
303 | recurrentConstraint?: ConstraintIdentifier | Constraint;
|
304 | /**
|
305 | * Constraint function applied to the bias vector.
|
306 | */
|
307 | biasConstraint?: ConstraintIdentifier | Constraint;
|
308 | /**
|
309 | * Float number between 0 and 1. Fraction of the units to drop for the linear
|
310 | * transformation of the inputs.
|
311 | */
|
312 | dropout?: number;
|
313 | /**
|
314 | * Float number between 0 and 1. Fraction of the units to drop for the linear
|
315 | * transformation of the recurrent state.
|
316 | */
|
317 | recurrentDropout?: number;
|
318 | /**
|
319 | * This is added for test DI purpose.
|
320 | */
|
321 | dropoutFunc?: Function;
|
322 | }
|
323 | export declare class SimpleRNNCell extends RNNCell {
|
324 | /** @nocollapse */
|
325 | static className: string;
|
326 | readonly units: number;
|
327 | readonly activation: Activation;
|
328 | readonly useBias: boolean;
|
329 | readonly kernelInitializer: Initializer;
|
330 | readonly recurrentInitializer: Initializer;
|
331 | readonly biasInitializer: Initializer;
|
332 | readonly kernelConstraint: Constraint;
|
333 | readonly recurrentConstraint: Constraint;
|
334 | readonly biasConstraint: Constraint;
|
335 | readonly kernelRegularizer: Regularizer;
|
336 | readonly recurrentRegularizer: Regularizer;
|
337 | readonly biasRegularizer: Regularizer;
|
338 | readonly dropout: number;
|
339 | readonly recurrentDropout: number;
|
340 | readonly dropoutFunc: Function;
|
341 | readonly stateSize: number;
|
342 | kernel: LayerVariable;
|
343 | recurrentKernel: LayerVariable;
|
344 | bias: LayerVariable;
|
345 | readonly DEFAULT_ACTIVATION = "tanh";
|
346 | readonly DEFAULT_KERNEL_INITIALIZER = "glorotNormal";
|
347 | readonly DEFAULT_RECURRENT_INITIALIZER = "orthogonal";
|
348 | readonly DEFAULT_BIAS_INITIALIZER: InitializerIdentifier;
|
349 | constructor(args: SimpleRNNCellLayerArgs);
|
350 | build(inputShape: Shape | Shape[]): void;
|
351 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
352 | getConfig(): serialization.ConfigDict;
|
353 | }
|
354 | export declare interface SimpleRNNLayerArgs extends BaseRNNLayerArgs {
|
355 | /**
|
356 | * Positive integer, dimensionality of the output space.
|
357 | */
|
358 | units: number;
|
359 | /**
|
360 | * Activation function to use.
|
361 | *
|
362 | * Defaults to hyperbolic tangent (`tanh`)
|
363 | *
|
364 | * If you pass `null`, no activation will be applied.
|
365 | */
|
366 | activation?: ActivationIdentifier;
|
367 | /**
|
368 | * Whether the layer uses a bias vector.
|
369 | */
|
370 | useBias?: boolean;
|
371 | /**
|
372 | * Initializer for the `kernel` weights matrix, used for the linear
|
373 | * transformation of the inputs.
|
374 | */
|
375 | kernelInitializer?: InitializerIdentifier | Initializer;
|
376 | /**
|
377 | * Initializer for the `recurrentKernel` weights matrix, used for
|
378 | * linear transformation of the recurrent state.
|
379 | */
|
380 | recurrentInitializer?: InitializerIdentifier | Initializer;
|
381 | /**
|
382 | * Initializer for the bias vector.
|
383 | */
|
384 | biasInitializer?: InitializerIdentifier | Initializer;
|
385 | /**
|
386 | * Regularizer function applied to the kernel weights matrix.
|
387 | */
|
388 | kernelRegularizer?: RegularizerIdentifier | Regularizer;
|
389 | /**
|
390 | * Regularizer function applied to the recurrentKernel weights matrix.
|
391 | */
|
392 | recurrentRegularizer?: RegularizerIdentifier | Regularizer;
|
393 | /**
|
394 | * Regularizer function applied to the bias vector.
|
395 | */
|
396 | biasRegularizer?: RegularizerIdentifier | Regularizer;
|
397 | /**
|
398 | * Constraint function applied to the kernel weights matrix.
|
399 | */
|
400 | kernelConstraint?: ConstraintIdentifier | Constraint;
|
401 | /**
|
402 | * Constraint function applied to the recurrentKernel weights matrix.
|
403 | */
|
404 | recurrentConstraint?: ConstraintIdentifier | Constraint;
|
405 | /**
|
406 | * Constraint function applied to the bias vector.
|
407 | */
|
408 | biasConstraint?: ConstraintIdentifier | Constraint;
|
409 | /**
|
410 | * Number between 0 and 1. Fraction of the units to drop for the linear
|
411 | * transformation of the inputs.
|
412 | */
|
413 | dropout?: number;
|
414 | /**
|
415 | * Number between 0 and 1. Fraction of the units to drop for the linear
|
416 | * transformation of the recurrent state.
|
417 | */
|
418 | recurrentDropout?: number;
|
419 | /**
|
420 | * This is added for test DI purpose.
|
421 | */
|
422 | dropoutFunc?: Function;
|
423 | }
|
424 | /**
|
425 | * RNNLayerConfig is identical to BaseRNNLayerConfig, except it makes the
|
426 | * `cell` property required. This interface is to be used with constructors
|
427 | * of concrete RNN layer subtypes.
|
428 | */
|
429 | export declare interface RNNLayerArgs extends BaseRNNLayerArgs {
|
430 | cell: RNNCell | RNNCell[];
|
431 | }
|
432 | export declare class SimpleRNN extends RNN {
|
433 | /** @nocollapse */
|
434 | static className: string;
|
435 | constructor(args: SimpleRNNLayerArgs);
|
436 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
437 | /** @nocollapse */
|
438 | static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict): T;
|
439 | }
|
440 | export declare interface GRUCellLayerArgs extends SimpleRNNCellLayerArgs {
|
441 | /**
|
442 | * Activation function to use for the recurrent step.
|
443 | *
|
444 | * Defaults to hard sigmoid (`hardSigmoid`).
|
445 | *
|
446 | * If `null`, no activation is applied.
|
447 | */
|
448 | recurrentActivation?: ActivationIdentifier;
|
449 | /**
|
450 | * Implementation mode, either 1 or 2.
|
451 | *
|
452 | * Mode 1 will structure its operations as a larger number of
|
453 | * smaller dot products and additions.
|
454 | *
|
455 | * Mode 2 will batch them into fewer, larger operations. These modes will
|
456 | * have different performance profiles on different hardware and
|
457 | * for different applications.
|
458 | *
|
459 | * Note: For superior performance, TensorFlow.js always uses implementation
|
460 | * 2, regardless of the actual value of this configuration field.
|
461 | */
|
462 | implementation?: number;
|
463 | /**
|
464 | * GRU convention (whether to apply reset gate after or before matrix
|
465 | * multiplication). false = "before", true = "after" (only false is
|
466 | * supported).
|
467 | */
|
468 | resetAfter?: boolean;
|
469 | }
|
470 | export declare class GRUCell extends RNNCell {
|
471 | /** @nocollapse */
|
472 | static className: string;
|
473 | readonly units: number;
|
474 | readonly activation: Activation;
|
475 | readonly recurrentActivation: Activation;
|
476 | readonly useBias: boolean;
|
477 | readonly kernelInitializer: Initializer;
|
478 | readonly recurrentInitializer: Initializer;
|
479 | readonly biasInitializer: Initializer;
|
480 | readonly kernelRegularizer: Regularizer;
|
481 | readonly recurrentRegularizer: Regularizer;
|
482 | readonly biasRegularizer: Regularizer;
|
483 | readonly kernelConstraint: Constraint;
|
484 | readonly recurrentConstraint: Constraint;
|
485 | readonly biasConstraint: Constraint;
|
486 | readonly dropout: number;
|
487 | readonly recurrentDropout: number;
|
488 | readonly dropoutFunc: Function;
|
489 | readonly stateSize: number;
|
490 | readonly implementation: number;
|
491 | readonly DEFAULT_ACTIVATION = "tanh";
|
492 | readonly DEFAULT_RECURRENT_ACTIVATION: ActivationIdentifier;
|
493 | readonly DEFAULT_KERNEL_INITIALIZER = "glorotNormal";
|
494 | readonly DEFAULT_RECURRENT_INITIALIZER = "orthogonal";
|
495 | readonly DEFAULT_BIAS_INITIALIZER: InitializerIdentifier;
|
496 | kernel: LayerVariable;
|
497 | recurrentKernel: LayerVariable;
|
498 | bias: LayerVariable;
|
499 | constructor(args: GRUCellLayerArgs);
|
500 | build(inputShape: Shape | Shape[]): void;
|
501 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
502 | getConfig(): serialization.ConfigDict;
|
503 | }
|
504 | export declare interface GRULayerArgs extends SimpleRNNLayerArgs {
|
505 | /**
|
506 | * Activation function to use for the recurrent step.
|
507 | *
|
508 | * Defaults to hard sigmoid (`hardSigmoid`).
|
509 | *
|
510 | * If `null`, no activation is applied.
|
511 | */
|
512 | recurrentActivation?: ActivationIdentifier;
|
513 | /**
|
514 | * Implementation mode, either 1 or 2.
|
515 | *
|
516 | * Mode 1 will structure its operations as a larger number of
|
517 | * smaller dot products and additions.
|
518 | *
|
519 | * Mode 2 will batch them into fewer, larger operations. These modes will
|
520 | * have different performance profiles on different hardware and
|
521 | * for different applications.
|
522 | *
|
523 | * Note: For superior performance, TensorFlow.js always uses implementation
|
524 | * 2, regardless of the actual value of this configuration field.
|
525 | */
|
526 | implementation?: number;
|
527 | }
|
528 | export declare class GRU extends RNN {
|
529 | /** @nocollapse */
|
530 | static className: string;
|
531 | constructor(args: GRULayerArgs);
|
532 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
533 | /** @nocollapse */
|
534 | static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict): T;
|
535 | }
|
536 | export declare interface LSTMCellLayerArgs extends SimpleRNNCellLayerArgs {
|
537 | /**
|
538 | * Activation function to use for the recurrent step.
|
539 | *
|
540 | * Defaults to hard sigmoid (`hardSigmoid`).
|
541 | *
|
542 | * If `null`, no activation is applied.
|
543 | */
|
544 | recurrentActivation?: ActivationIdentifier;
|
545 | /**
|
546 | * If `true`, add 1 to the bias of the forget gate at initialization.
|
547 | * Setting it to `true` will also force `biasInitializer = 'zeros'`.
|
548 | * This is recommended in
|
549 | * [Jozefowicz et
|
550 | * al.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
|
551 | */
|
552 | unitForgetBias?: boolean;
|
553 | /**
|
554 | * Implementation mode, either 1 or 2.
|
555 | *
|
556 | * Mode 1 will structure its operations as a larger number of
|
557 | * smaller dot products and additions.
|
558 | *
|
559 | * Mode 2 will batch them into fewer, larger operations. These modes will
|
560 | * have different performance profiles on different hardware and
|
561 | * for different applications.
|
562 | *
|
563 | * Note: For superior performance, TensorFlow.js always uses implementation
|
564 | * 2, regardless of the actual value of this configuration field.
|
565 | */
|
566 | implementation?: number;
|
567 | }
|
568 | export declare class LSTMCell extends RNNCell {
|
569 | /** @nocollapse */
|
570 | static className: string;
|
571 | readonly units: number;
|
572 | readonly activation: Activation;
|
573 | readonly recurrentActivation: Activation;
|
574 | readonly useBias: boolean;
|
575 | readonly kernelInitializer: Initializer;
|
576 | readonly recurrentInitializer: Initializer;
|
577 | readonly biasInitializer: Initializer;
|
578 | readonly unitForgetBias: boolean;
|
579 | readonly kernelConstraint: Constraint;
|
580 | readonly recurrentConstraint: Constraint;
|
581 | readonly biasConstraint: Constraint;
|
582 | readonly kernelRegularizer: Regularizer;
|
583 | readonly recurrentRegularizer: Regularizer;
|
584 | readonly biasRegularizer: Regularizer;
|
585 | readonly dropout: number;
|
586 | readonly recurrentDropout: number;
|
587 | readonly dropoutFunc: Function;
|
588 | readonly stateSize: number[];
|
589 | readonly implementation: number;
|
590 | readonly DEFAULT_ACTIVATION = "tanh";
|
591 | readonly DEFAULT_RECURRENT_ACTIVATION = "hardSigmoid";
|
592 | readonly DEFAULT_KERNEL_INITIALIZER = "glorotNormal";
|
593 | readonly DEFAULT_RECURRENT_INITIALIZER = "orthogonal";
|
594 | readonly DEFAULT_BIAS_INITIALIZER = "zeros";
|
595 | kernel: LayerVariable;
|
596 | recurrentKernel: LayerVariable;
|
597 | bias: LayerVariable;
|
598 | constructor(args: LSTMCellLayerArgs);
|
599 | build(inputShape: Shape | Shape[]): void;
|
600 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
601 | getConfig(): serialization.ConfigDict;
|
602 | }
|
603 | export declare interface LSTMLayerArgs extends SimpleRNNLayerArgs {
|
604 | /**
|
605 | * Activation function to use for the recurrent step.
|
606 | *
|
607 | * Defaults to hard sigmoid (`hardSigmoid`).
|
608 | *
|
609 | * If `null`, no activation is applied.
|
610 | */
|
611 | recurrentActivation?: ActivationIdentifier;
|
612 | /**
|
613 | * If `true`, add 1 to the bias of the forget gate at initialization.
|
614 | * Setting it to `true` will also force `biasInitializer = 'zeros'`.
|
615 | * This is recommended in
|
616 | * [Jozefowicz et
|
617 | * al.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
|
618 | */
|
619 | unitForgetBias?: boolean;
|
620 | /**
|
621 | * Implementation mode, either 1 or 2.
|
622 | * Mode 1 will structure its operations as a larger number of
|
623 | * smaller dot products and additions, whereas mode 2 will
|
624 | * batch them into fewer, larger operations. These modes will
|
625 | * have different performance profiles on different hardware and
|
626 | * for different applications.
|
627 | *
|
628 | * Note: For superior performance, TensorFlow.js always uses implementation
|
629 | * 2, regardless of the actual value of this config field.
|
630 | */
|
631 | implementation?: number;
|
632 | }
|
633 | export declare class LSTM extends RNN {
|
634 | /** @nocollapse */
|
635 | static className: string;
|
636 | constructor(args: LSTMLayerArgs);
|
637 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
638 | /** @nocollapse */
|
639 | static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict): T;
|
640 | }
|
641 | export declare interface StackedRNNCellsArgs extends LayerArgs {
|
642 | /**
|
643 | * An `Array` of `RNNCell` instances.
|
644 | */
|
645 | cells: RNNCell[];
|
646 | }
|
647 | export declare class StackedRNNCells extends RNNCell {
|
648 | /** @nocollapse */
|
649 | static className: string;
|
650 | protected cells: RNNCell[];
|
651 | constructor(args: StackedRNNCellsArgs);
|
652 | get stateSize(): number[];
|
653 | call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
|
654 | build(inputShape: Shape | Shape[]): void;
|
655 | getConfig(): serialization.ConfigDict;
|
656 | /** @nocollapse */
|
657 | static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict, customObjects?: serialization.ConfigDict): T;
|
658 | get trainableWeights(): LayerVariable[];
|
659 | get nonTrainableWeights(): LayerVariable[];
|
660 | /**
|
661 | * Retrieve the weights of a the model.
|
662 | *
|
663 | * @returns A flat `Array` of `tf.Tensor`s.
|
664 | */
|
665 | getWeights(): Tensor[];
|
666 | /**
|
667 | * Set the weights of the model.
|
668 | *
|
669 | * @param weights An `Array` of `tf.Tensor`s with shapes and types matching
|
670 | * the output of `getWeights()`.
|
671 | */
|
672 | setWeights(weights: Tensor[]): void;
|
673 | }
|
674 | export declare function generateDropoutMask(args: {
|
675 | ones: () => tfc.Tensor;
|
676 | rate: number;
|
677 | training?: boolean;
|
678 | count?: number;
|
679 | dropoutFunc?: Function;
|
680 | }): tfc.Tensor | tfc.Tensor[];
|