@vladmandic/human
Version:
Human: AI-powered 3D Face Detection & Rotation Tracking, Face Description & Recognition, Body Pose Tracking, 3D Hand & Finger Tracking, Iris Analysis, Age & Gender & Emotion Prediction, Gesture Recognition
1,364 lines (1,262 loc) • 365 kB
TypeScript
import { backend_util } from './tfjs-core';
import { BackendTimingInfo } from './tfjs-core';
import { BackendValues } from './tfjs-core';
import { DataId as DataId_2 } from './tfjs-core';
import { DataStorage } from './tfjs-core';
import { DataToGPUWebGLOption } from './tfjs-core';
import { DataType } from './tfjs-core';
import { DataTypeFor } from './tfjs-core';
import { DataTypeMap } from './tfjs-core';
import { GPUData } from './tfjs-core';
import { InferenceModel } from './tfjs-core';
import { io } from './tfjs-core';
import { KernelBackend } from './tfjs-core';
import { MemoryInfo } from './tfjs-core';
import { ModelPredictConfig } from './tfjs-core';
import { NamedAttrMap } from './tfjs-core';
import { NamedTensorMap } from './tfjs-core';
import { NumericDataType } from './tfjs-core';
import { Optimizer } from './tfjs-core';
import { PixelData } from './tfjs-core';
import { Rank } from './tfjs-core';
import { Scalar } from './tfjs-core';
import { serialization } from './tfjs-core';
import { Tensor } from './tfjs-core';
import { Tensor2D } from './tfjs-core';
import { Tensor3D } from './tfjs-core';
import { Tensor4D } from './tfjs-core';
import { TensorBuffer } from './tfjs-core';
import { TensorInfo as TensorInfo_2 } from './tfjs-core';
import * as tfc from './tfjs-core';
import { TimingInfo } from './tfjs-core';
import { TypedArray } from './tfjs-core';
import { WebGLData } from './tfjs-core';
import { WebGPUData } from './tfjs-core';
/**
* Base class for Activations.
*
* Special note: due to cross-language compatibility reasons, the
* static readonly className field in this family of classes must be set to
* the initialLowerCamelCase name of the activation.
*/
declare abstract class Activation extends serialization.Serializable {
abstract apply(tensor: Tensor, axis?: number): Tensor;
getConfig(): serialization.ConfigDict;
}
/**
* Applies an activation function to an output.
*
* This layer applies element-wise activation function. Other layers, notably
* `dense` can also apply activation functions. Use this isolated activation
* function to extract the values before and after the
* activation. For instance:
*
* ```js
* const input = tf.input({shape: [5]});
* const denseLayer = tf.layers.dense({units: 1});
* const activationLayer = tf.layers.activation({activation: 'relu6'});
*
* // Obtain the output symbolic tensors by applying the layers in order.
* const denseOutput = denseLayer.apply(input);
* const activationOutput = activationLayer.apply(denseOutput);
*
* // Create the model based on the inputs.
* const model = tf.model({
* inputs: input,
* outputs: [denseOutput, activationOutput]
* });
*
* // Collect both outputs and print separately.
* const [denseOut, activationOut] = model.predict(tf.randomNormal([6, 5]));
* denseOut.print();
* activationOut.print();
* ```
*
* @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'}
*/
declare function activation(args: ActivationLayerArgs): Activation_2;
declare class Activation_2 extends Layer {
/** @nocollapse */
static className: string;
activation: Activation;
constructor(args: ActivationLayerArgs);
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
getConfig(): serialization.ConfigDict;
}
/** @docinline */
declare type ActivationIdentifier = 'elu' | 'hardSigmoid' | 'linear' | 'relu' | 'relu6' | 'selu' | 'sigmoid' | 'softmax' | 'softplus' | 'softsign' | 'tanh' | 'swish' | 'mish';
declare interface ActivationLayerArgs extends LayerArgs {
/**
* Name of the activation function to use.
*/
activation: ActivationIdentifier;
}
declare type AdadeltaOptimizerConfig = {
learning_rate: number;
rho: number;
epsilon: number;
};
declare type AdadeltaSerialization = BaseSerialization<'Adadelta', AdadeltaOptimizerConfig>;
declare type AdagradOptimizerConfig = {
learning_rate: number;
initial_accumulator_value?: number;
};
declare type AdagradSerialization = BaseSerialization<'Adagrad', AdagradOptimizerConfig>;
declare type AdamaxOptimizerConfig = {
learning_rate: number;
beta1: number;
beta2: number;
epsilon?: number;
decay?: number;
};
declare type AdamaxSerialization = BaseSerialization<'Adamax', AdamaxOptimizerConfig>;
declare type AdamOptimizerConfig = {
learning_rate: number;
beta1: number;
beta2: number;
epsilon?: number;
};
declare type AdamSerialization = BaseSerialization<'Adam', AdamOptimizerConfig>;
/**
* @license
* Copyright 2022 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.
* =============================================================================
*/
declare class AdapterInfo {
private vendor;
private architecture;
intelGPUGeneration: number;
constructor(adapterInfo: GPUAdapterInfo);
private getIntelGPUGeneration;
isIntel(): boolean;
}
declare class Add extends Merge {
/** @nocollapse */
static className: string;
constructor(args?: LayerArgs);
protected mergeFunction(inputs: Tensor[]): Tensor;
}
/**
* Layer that performs element-wise addition on an `Array` of inputs.
*
* It takes as input a list of tensors, all of the same shape, and returns a
* single tensor (also of the same shape). The inputs are specified as an
* `Array` when the `apply` method of the `Add` layer instance is called. For
* example:
*
* ```js
* const input1 = tf.input({shape: [2, 2]});
* const input2 = tf.input({shape: [2, 2]});
* const addLayer = tf.layers.add();
* const sum = addLayer.apply([input1, input2]);
* console.log(JSON.stringify(sum.shape));
* // You get [null, 2, 2], with the first dimension as the undetermined batch
* // dimension.
* ```
*
* @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'}
*/
declare function add(args?: LayerArgs): Add;
declare const addImpl: SimpleBinaryKernelImpl;
/**
* Applies Alpha Dropout to the input.
*
* As it is a regularization layer, it is only active at training time.
*
* Alpha Dropout is a `Dropout` that keeps mean and variance of inputs
* to their original values, in order to ensure the self-normalizing property
* even after this dropout.
* Alpha Dropout fits well to Scaled Exponential Linear Units
* by randomly setting activations to the negative saturation value.
*
* Arguments:
* - `rate`: float, drop probability (as with `Dropout`).
* The multiplicative noise will have
* standard deviation `sqrt(rate / (1 - rate))`.
* - `noise_shape`: A 1-D `Tensor` of type `int32`, representing the
* shape for randomly generated keep/drop flags.
*
* Input shape:
* Arbitrary. Use the keyword argument `inputShape`
* (tuple of integers, does not include the samples axis)
* when using this layer as the first layer in a model.
*
* Output shape:
* Same shape as input.
*
* References:
* - [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
*/
declare class AlphaDropout extends Layer {
/** @nocollapse */
static className: string;
readonly rate: number;
readonly noiseShape: Shape;
constructor(args: AlphaDropoutArgs);
_getNoiseShape(inputs: Tensor | Tensor[]): any;
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
getConfig(): {
rate: number;
};
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
}
/**
* Applies Alpha Dropout to the input.
*
* As it is a regularization layer, it is only active at training time.
*
* Alpha Dropout is a `Dropout` that keeps mean and variance of inputs
* to their original values, in order to ensure the self-normalizing property
* even after this dropout.
* Alpha Dropout fits well to Scaled Exponential Linear Units
* by randomly setting activations to the negative saturation value.
*
* Arguments:
* - `rate`: float, drop probability (as with `Dropout`).
* The multiplicative noise will have
* standard deviation `sqrt(rate / (1 - rate))`.
* - `noise_shape`: A 1-D `Tensor` of type `int32`, representing the
* shape for randomly generated keep/drop flags.
*
* Input shape:
* Arbitrary. Use the keyword argument `inputShape`
* (tuple of integers, does not include the samples axis)
* when using this layer as the first layer in a model.
*
* Output shape:
* Same shape as input.
*
* References:
* - [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
*
* @doc {heading: 'Layers', subheading: 'Noise', namespace: 'layers'}
*/
declare function alphaDropout(args: AlphaDropoutArgs): AlphaDropout;
declare interface AlphaDropoutArgs extends LayerArgs {
/** drop probability. */
rate: number;
/**
* A 1-D `Tensor` of type `int32`, representing the
* shape for randomly generated keep/drop flags.
*/
noiseShape?: Shape;
}
declare function assertNotComplex(tensor: TensorInfo_2 | TensorInfo_2[], opName: string): void;
declare function assertNotComplex_2(tensor: TensorInfo_2 | TensorInfo_2[], opName: string): void;
declare namespace AttrValue {
/** Properties of a ListValue. */
interface IListValue {
/** ListValue s */
s?: (string[] | null);
/** ListValue i */
i?: ((number | string)[] | null);
/** ListValue f */
f?: (number[] | null);
/** ListValue b */
b?: (boolean[] | null);
/** ListValue type */
type?: (DataType_2[] | null);
/** ListValue shape */
shape?: (ITensorShape[] | null);
/** ListValue tensor */
tensor?: (ITensor[] | null);
/** ListValue func */
func?: (INameAttrList[] | null);
}
}
declare class Average extends Merge {
/** @nocollapse */
static className: string;
constructor(args?: LayerArgs);
protected mergeFunction(inputs: Tensor[]): Tensor;
}
/**
* Layer that performs element-wise averaging on an `Array` of inputs.
*
* It takes as input a list of tensors, all of the same shape, and returns a
* single tensor (also of the same shape). For example:
*
* ```js
* const input1 = tf.input({shape: [2, 2]});
* const input2 = tf.input({shape: [2, 2]});
* const averageLayer = tf.layers.average();
* const average = averageLayer.apply([input1, input2]);
* console.log(JSON.stringify(average.shape));
* // You get [null, 2, 2], with the first dimension as the undetermined batch
* // dimension.
* ```
*
* @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'}
*/
declare function average(args?: LayerArgs): Average;
declare class AveragePooling1D extends Pooling1D {
/** @nocollapse */
static className: string;
constructor(args: Pooling1DLayerArgs);
protected poolingFunction(inputs: Tensor, poolSize: [number, number], strides: [number, number], padding: PaddingMode, dataFormat: DataFormat): Tensor;
}
/**
* Average pooling operation for spatial data.
*
* Input shape: `[batchSize, inLength, channels]`
*
* Output shape: `[batchSize, pooledLength, channels]`
*
* `tf.avgPool1d` is an alias.
*
* @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'}
*/
declare function averagePooling1d(args: Pooling1DLayerArgs): AveragePooling1D;
declare class AveragePooling2D extends Pooling2D {
/** @nocollapse */
static className: string;
constructor(args: Pooling2DLayerArgs);
protected poolingFunction(inputs: Tensor, poolSize: [number, number], strides: [number, number], padding: PaddingMode, dataFormat: DataFormat): Tensor;
}
/**
* Average pooling operation for spatial data.
*
* Input shape:
* - If `dataFormat === CHANNEL_LAST`:
* 4D tensor with shape:
* `[batchSize, rows, cols, channels]`
* - If `dataFormat === CHANNEL_FIRST`:
* 4D tensor with shape:
* `[batchSize, channels, rows, cols]`
*
* Output shape
* - If `dataFormat === CHANNEL_LAST`:
* 4D tensor with shape:
* `[batchSize, pooledRows, pooledCols, channels]`
* - If `dataFormat === CHANNEL_FIRST`:
* 4D tensor with shape:
* `[batchSize, channels, pooledRows, pooledCols]`
*
* `tf.avgPool2d` is an alias.
*
* @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'}
*/
declare function averagePooling2d(args: Pooling2DLayerArgs): AveragePooling2D;
declare class AveragePooling3D extends Pooling3D {
/** @nocollapse */
static className: string;
constructor(args: Pooling3DLayerArgs);
protected poolingFunction(inputs: Tensor, poolSize: [number, number, number], strides: [number, number, number], padding: PaddingMode, dataFormat: DataFormat): Tensor;
}
/**
* Average pooling operation for 3D data.
*
* Input shape
* - If `dataFormat === channelsLast`:
* 5D tensor with shape:
* `[batchSize, depths, rows, cols, channels]`
* - If `dataFormat === channelsFirst`:
* 4D tensor with shape:
* `[batchSize, channels, depths, rows, cols]`
*
* Output shape
* - If `dataFormat=channelsLast`:
* 5D tensor with shape:
* `[batchSize, pooledDepths, pooledRows, pooledCols, channels]`
* - If `dataFormat=channelsFirst`:
* 5D tensor with shape:
* `[batchSize, channels, pooledDepths, pooledRows, pooledCols]`
*
* @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'}
*/
declare function averagePooling3d(args: Pooling3DLayerArgs): AveragePooling3D;
declare function avgPool1d(args: Pooling1DLayerArgs): AveragePooling1D;
declare function avgPool2d(args: Pooling2DLayerArgs): AveragePooling2D;
declare function avgPool3d(args: Pooling3DLayerArgs): AveragePooling3D;
declare function avgPooling1d(args: Pooling1DLayerArgs): AveragePooling1D;
declare function avgPooling2d(args: Pooling2DLayerArgs): AveragePooling2D;
declare function avgPooling3d(args: Pooling3DLayerArgs): AveragePooling3D;
export declare class BackendWasm extends KernelBackend {
wasm: BackendWasmModule | BackendWasmThreadedSimdModule;
private dataIdNextNumber;
dataIdMap: DataStorage<TensorData_2>;
constructor(wasm: BackendWasmModule | BackendWasmThreadedSimdModule);
write(values: backend_util.BackendValues | null, shape: number[], dtype: DataType): DataId_3;
numDataIds(): number;
time(f: () => void): Promise<BackendTimingInfo>;
move(dataId: DataId_3, values: backend_util.BackendValues | null, shape: number[], dtype: DataType, refCount: number): void;
read(dataId: DataId_3): Promise<backend_util.BackendValues>;
readSync(dataId: DataId_3, start?: number, end?: number): backend_util.BackendValues;
/**
* Dispose the memory if the dataId has 0 refCount. Return true if the memory
* is released, false otherwise.
* @param dataId
* @oaram force Optional, remove the data regardless of refCount
*/
disposeData(dataId: DataId_3, force?: boolean): boolean;
/** Return refCount of a `TensorData`. */
refCount(dataId: DataId_3): number;
incRef(dataId: DataId_3): void;
floatPrecision(): 32;
getMemoryOffset(dataId: DataId_3): number;
dispose(): void;
memory(): {
unreliable: boolean;
};
/**
* Make a tensor info for the output of an op. If `memoryOffset` is not
* present, this method allocates memory on the WASM heap. If `memoryOffset`
* is present, the memory was allocated elsewhere (in c++) and we just record
* the pointer where that memory lives.
*/
makeOutput(shape: number[], dtype: DataType, memoryOffset?: number, values?: backend_util.BackendValues): TensorInfo_2;
typedArrayFromHeap({ shape, dtype, dataId }: TensorInfo_2): backend_util.TypedArray;
}
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* 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.
* =============================================================================
*/
declare interface BackendWasmModule extends EmscriptenModule {
// Using the tfjs namespace to avoid conflict with emscripten's API.
tfjs: {
init(): void,
initWithThreadsCount(threadsCount: number): void,
getThreadsCount(): number,
registerTensor(id: number, size: number, memoryOffset: number): void,
// Disposes the data behind the data bucket.
disposeData(id: number): void,
// Disposes the backend and all of its associated data.
dispose(): void,
}
}
declare interface BackendWasmThreadedSimdModule extends BackendWasmModule {
PThread: {
// Terminates all webworkers
terminateAllThreads(): void,
};
}
/**
* Abstract base class used to build new callbacks.
*
* The `logs` dictionary that callback methods take as argument will contain
* keys for quantities relevant to the current batch or epoch.
*
* Currently, the `.fit()` method of the `Sequential` model class
* will include the following quantities in the `logs` that
* it passes to its callbacks:
*
* onEpochEnd: Logs include `acc` and `loss`, and optionally include `valLoss`
* (if validation is enabled in `fit`), and `valAcc` (if validation and
* accuracy monitoring are enabled).
* onBatchBegin: Logs include `size`, the number of samples in the current
* batch.
* onBatchEnd: Logs include `loss`, and optionally `acc` (if accuracy monitoring
* is enabled).
*/
declare abstract class BaseCallback {
validationData: Tensor | Tensor[];
/**
* Training parameters (eg. verbosity, batch size, number of epochs...).
*/
params: Params;
setParams(params: Params): void;
onEpochBegin(epoch: number, logs?: UnresolvedLogs): Promise<void>;
onEpochEnd(epoch: number, logs?: UnresolvedLogs): Promise<void>;
onBatchBegin(batch: number, logs?: UnresolvedLogs): Promise<void>;
onBatchEnd(batch: number, logs?: UnresolvedLogs): Promise<void>;
onTrainBegin(logs?: UnresolvedLogs): Promise<void>;
onTrainEnd(logs?: UnresolvedLogs): Promise<void>;
setModel(model: Container): void;
}
declare type BaseCallbackConstructor = {
new (): BaseCallback;
};
/**
* Abstract convolution layer.
*/
declare abstract class BaseConv extends Layer {
protected readonly rank: number;
protected readonly kernelSize: number[];
protected readonly strides: number[];
protected readonly padding: PaddingMode;
protected readonly dataFormat: DataFormat;
protected readonly activation: Activation;
protected readonly useBias: boolean;
protected readonly dilationRate: number[];
protected readonly biasInitializer?: Initializer;
protected readonly biasConstraint?: Constraint;
protected readonly biasRegularizer?: Regularizer;
protected bias: LayerVariable;
readonly DEFAULT_KERNEL_INITIALIZER: InitializerIdentifier;
readonly DEFAULT_BIAS_INITIALIZER: InitializerIdentifier;
constructor(rank: number, args: BaseConvLayerArgs);
protected static verifyArgs(args: BaseConvLayerArgs): void;
getConfig(): serialization.ConfigDict;
}
/**
* Base LayerConfig for depthwise and non-depthwise convolutional layers.
*/
declare interface BaseConvLayerArgs extends LayerArgs {
/**
* The dimensions of the convolution window. If kernelSize is a number, the
* convolutional window will be square.
*/
kernelSize: number | number[];
/**
* The strides of the convolution in each dimension. If strides is a number,
* strides in both dimensions are equal.
*
* Specifying any stride value != 1 is incompatible with specifying any
* `dilationRate` value != 1.
*/
strides?: number | number[];
/**
* Padding mode.
*/
padding?: PaddingMode;
/**
* Format of the data, which determines the ordering of the dimensions in
* the inputs.
*
* `channels_last` corresponds to inputs with shape
* `(batch, ..., channels)`
*
* `channels_first` corresponds to inputs with shape `(batch, channels,
* ...)`.
*
* Defaults to `channels_last`.
*/
dataFormat?: DataFormat;
/**
* The dilation rate to use for the dilated convolution in each dimension.
* Should be an integer or array of two or three integers.
*
* Currently, specifying any `dilationRate` value != 1 is incompatible with
* specifying any `strides` value != 1.
*/
dilationRate?: number | [number] | [number, number] | [number, number, number];
/**
* Activation function of the layer.
*
* If you don't specify the activation, none is applied.
*/
activation?: ActivationIdentifier;
/**
* Whether the layer uses a bias vector. Defaults to `true`.
*/
useBias?: boolean;
/**
* Initializer for the convolutional kernel weights matrix.
*/
kernelInitializer?: InitializerIdentifier | Initializer;
/**
* Initializer for the bias vector.
*/
biasInitializer?: InitializerIdentifier | Initializer;
/**
* Constraint for the convolutional kernel weights.
*/
kernelConstraint?: ConstraintIdentifier | Constraint;
/**
* Constraint for the bias vector.
*/
biasConstraint?: ConstraintIdentifier | Constraint;
/**
* Regularizer function applied to the kernel weights matrix.
*/
kernelRegularizer?: RegularizerIdentifier | Regularizer;
/**
* Regularizer function applied to the bias vector.
*/
biasRegularizer?: RegularizerIdentifier | Regularizer;
/**
* Regularizer function applied to the activation.
*/
activityRegularizer?: RegularizerIdentifier | Regularizer;
}
declare abstract class BaseRandomLayer extends Layer {
/** @nocollapse */
static className: string;
protected randomGenerator: RandomSeed;
constructor(args: BaseRandomLayerArgs);
getConfig(): serialization.ConfigDict;
}
declare interface BaseRandomLayerArgs extends LayerArgs {
seed?: number;
}
declare interface BaseRNNLayerArgs extends LayerArgs {
/**
* A RNN cell instance. A RNN cell is a class that has:
* - a `call()` method, which takes `[Tensor, Tensor]` as the
* first input argument. The first item is the input at time t, and
* second item is the cell state at time t.
* The `call()` method returns `[outputAtT, statesAtTPlus1]`.
* The `call()` method of the cell can also take the argument `constants`,
* see section "Note on passing external constants" below.
* Porting Node: PyKeras overrides the `call()` signature of RNN cells,
* which are Layer subtypes, to accept two arguments. tfjs-layers does
* not do such overriding. Instead we preseve the `call()` signature,
* which due to its `Tensor|Tensor[]` argument and return value is
* flexible enough to handle the inputs and states.
* - a `stateSize` attribute. This can be a single integer (single state)
* in which case it is the size of the recurrent state (which should be
* the same as the size of the cell output). This can also be an Array of
* integers (one size per state). In this case, the first entry
* (`stateSize[0]`) should be the same as the size of the cell output.
* It is also possible for `cell` to be a list of RNN cell instances, in which
* case the cells get stacked on after the other in the RNN, implementing an
* efficient stacked RNN.
*/
cell?: RNNCell | RNNCell[];
/**
* Whether to return the last output in the output sequence, or the full
* sequence.
*/
returnSequences?: boolean;
/**
* Whether to return the last state in addition to the output.
*/
returnState?: boolean;
/**
* If `true`, process the input sequence backwards and return the reversed
* sequence (default: `false`).
*/
goBackwards?: boolean;
/**
* If `true`, the last state for each sample at index i in a batch will be
* used as initial state of the sample of index i in the following batch
* (default: `false`).
*
* You can set RNN layers to be "stateful", which means that the states
* computed for the samples in one batch will be reused as initial states
* for the samples in the next batch. This assumes a one-to-one mapping
* between samples in different successive batches.
*
* To enable "statefulness":
* - specify `stateful: true` in the layer constructor.
* - specify a fixed batch size for your model, by passing
* - if sequential model:
* `batchInputShape: [...]` to the first layer in your model.
* - else for functional model with 1 or more Input layers:
* `batchShape: [...]` to all the first layers in your model.
* This is the expected shape of your inputs
* *including the batch size*.
* It should be a tuple of integers, e.g., `[32, 10, 100]`.
* - specify `shuffle: false` when calling `LayersModel.fit()`.
*
* To reset the state of your model, call `resetStates()` on either the
* specific layer or on the entire model.
*/
stateful?: boolean;
/**
* If `true`, the network will be unrolled, else a symbolic loop will be
* used. Unrolling can speed up a RNN, although it tends to be more
* memory-intensive. Unrolling is only suitable for short sequences (default:
* `false`).
* Porting Note: tfjs-layers has an imperative backend. RNNs are executed with
* normal TypeScript control flow. Hence this property is inapplicable and
* ignored in tfjs-layers.
*/
unroll?: boolean;
/**
* Dimensionality of the input (integer).
* This option (or alternatively, the option `inputShape`) is required when
* this layer is used as the first layer in a model.
*/
inputDim?: number;
/**
* Length of the input sequences, to be specified when it is constant.
* This argument is required if you are going to connect `Flatten` then
* `Dense` layers upstream (without it, the shape of the dense outputs cannot
* be computed). Note that if the recurrent layer is not the first layer in
* your model, you would need to specify the input length at the level of the
* first layer (e.g., via the `inputShape` option).
*/
inputLength?: number;
}
/**
* A Keras JSON entry representing a Keras object such as a Layer.
*
* The Keras JSON convention is to provide the `class_name` (e.g., the layer
* type) at the top level, and then to place the class-specific configuration in
* a `config` subtree. These class-specific configurations are provided by
* subtypes of `PyJsonDict`. Thus, this `*Serialization` has a type parameter
* giving the specific type of the wrapped `PyJsonDict`.
*/
declare interface BaseSerialization<N extends string, T extends PyJson<Extract<keyof T, string>>> extends PyJsonDict {
class_name: N;
config: T;
}
declare class BatchNormalization extends Layer {
/** @nocollapse */
static className: string;
private readonly axis;
private readonly momentum;
private readonly epsilon;
private readonly center;
private readonly scale;
private readonly betaInitializer;
private readonly gammaInitializer;
private readonly movingMeanInitializer;
private readonly movingVarianceInitializer;
private readonly betaConstraint;
private readonly gammaConstraint;
private readonly betaRegularizer;
private readonly gammaRegularizer;
private gamma;
private beta;
private movingMean;
private movingVariance;
constructor(args?: BatchNormalizationLayerArgs);
build(inputShape: Shape | Shape[]): void;
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
getConfig(): serialization.ConfigDict;
}
/**
* Batch normalization layer (Ioffe and Szegedy, 2014).
*
* Normalize the activations of the previous layer at each batch,
* i.e. applies a transformation that maintains the mean activation
* close to 0 and the activation standard deviation close to 1.
*
* Input shape:
* Arbitrary. Use the keyword argument `inputShape` (Array of integers, does
* not include the sample axis) when calling the constructor of this class,
* if this layer is used as a first layer in a model.
*
* Output shape:
* Same shape as input.
*
* References:
* - [Batch Normalization: Accelerating Deep Network Training by Reducing
* Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
*
* @doc {heading: 'Layers', subheading: 'Normalization', namespace: 'layers'}
*/
declare function batchNormalization(args?: BatchNormalizationLayerArgs): BatchNormalization;
declare interface BatchNormalizationLayerArgs extends LayerArgs {
/**
* The integer axis that should be normalized (typically the features axis).
* Defaults to -1.
*
* For instance, after a `Conv2D` layer with `data_format="channels_first"`,
* set `axis=1` in `batchNormalization`.
*/
axis?: number;
/**
* Momentum of the moving average. Defaults to 0.99.
*/
momentum?: number;
/**
* Small float added to the variance to avoid dividing by zero. Defaults to
* 1e-3.
*/
epsilon?: number;
/**
* If `true`, add offset of `beta` to normalized tensor.
* If `false`, `beta` is ignored.
* Defaults to `true`.
*/
center?: boolean;
/**
* If `true`, multiply by `gamma`.
* If `false`, `gamma` is not used.
* When the next layer is linear (also e.g. `nn.relu`),
* this can be disabled since the scaling will be done by the next layer.
* Defaults to `true`.
*/
scale?: boolean;
/**
* Initializer for the beta weight.
* Defaults to 'zeros'.
*/
betaInitializer?: InitializerIdentifier | Initializer;
/**
* Initializer for the gamma weight.
* Defaults to `ones`.
*/
gammaInitializer?: InitializerIdentifier | Initializer;
/**
* Initializer for the moving mean.
* Defaults to `zeros`
*/
movingMeanInitializer?: InitializerIdentifier | Initializer;
/**
* Initializer for the moving variance.
* Defaults to 'Ones'.
*/
movingVarianceInitializer?: InitializerIdentifier | Initializer;
/**
* Constraint for the beta weight.
*/
betaConstraint?: ConstraintIdentifier | Constraint;
/**
* Constraint for gamma weight.
*/
gammaConstraint?: ConstraintIdentifier | Constraint;
/**
* Regularizer for the beta weight.
*/
betaRegularizer?: RegularizerIdentifier | Regularizer;
/**
* Regularizer for the gamma weight.
*/
gammaRegularizer?: RegularizerIdentifier | Regularizer;
}
declare class Bidirectional extends Wrapper {
/** @nocollapse */
static className: string;
mergeMode: BidirectionalMergeMode;
private forwardLayer;
private backwardLayer;
private returnSequences;
private returnState;
private numConstants?;
private _trainable;
constructor(args: BidirectionalLayerArgs);
get trainable(): boolean;
set trainable(value: boolean);
getWeights(): Tensor[];
setWeights(weights: Tensor[]): void;
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
apply(inputs: Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[], kwargs?: Kwargs): Tensor | Tensor[] | SymbolicTensor | SymbolicTensor[];
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
resetStates(states?: Tensor | Tensor[]): void;
build(inputShape: Shape | Shape[]): void;
computeMask(inputs: Tensor | Tensor[], mask?: Tensor | Tensor[]): Tensor | Tensor[];
get trainableWeights(): LayerVariable[];
get nonTrainableWeights(): LayerVariable[];
setFastWeightInitDuringBuild(value: boolean): void;
getConfig(): serialization.ConfigDict;
/** @nocollapse */
static fromConfig<T extends serialization.Serializable>(cls: serialization.SerializableConstructor<T>, config: serialization.ConfigDict): T;
}
/** @doc {heading: 'Layers', subheading: 'Wrapper', namespace: 'layers'} */
declare function bidirectional(args: BidirectionalLayerArgs): Bidirectional;
declare interface BidirectionalLayerArgs extends WrapperLayerArgs {
/**
* The instance of an `RNN` layer to be wrapped.
*/
layer: RNN;
/**
* Mode by which outputs of the forward and backward RNNs are
* combined. If `null` or `undefined`, the output will not be
* combined, they will be returned as an `Array`.
*
* If `undefined` (i.e., not provided), defaults to `'concat'`.
*/
mergeMode?: BidirectionalMergeMode;
}
/** @docinline */
declare type BidirectionalMergeMode = 'sum' | 'mul' | 'concat' | 'ave';
/**
* Binary accuracy metric function.
*
* `yTrue` and `yPred` can have 0-1 values. Example:
* ```js
* const x = tf.tensor2d([[1, 1, 1, 1], [0, 0, 0, 0]], [2, 4]);
* const y = tf.tensor2d([[1, 0, 1, 0], [0, 0, 0, 1]], [2, 4]);
* const accuracy = tf.metrics.binaryAccuracy(x, y);
* accuracy.print();
* ```
*
* `yTrue` and `yPred` can also have floating-number values between 0 and 1, in
* which case the values will be thresholded at 0.5 to yield 0-1 values (i.e.,
* a value >= 0.5 and <= 1.0 is interpreted as 1).
*
* Example:
* ```js
* const x = tf.tensor1d([1, 1, 1, 1, 0, 0, 0, 0]);
* const y = tf.tensor1d([0.2, 0.4, 0.6, 0.8, 0.2, 0.3, 0.4, 0.7]);
* const accuracy = tf.metrics.binaryAccuracy(x, y);
* accuracy.print();
* ```
*
* @param yTrue Binary Tensor of truth.
* @param yPred Binary Tensor of prediction.
* @return Accuracy Tensor.
*
* @doc {heading: 'Metrics', namespace: 'metrics'}
*/
declare function binaryAccuracy(yTrue: Tensor, yPred: Tensor): Tensor;
/**
* Binary crossentropy metric function.
*
* Example:
* ```js
* const x = tf.tensor2d([[0], [1], [1], [1]]);
* const y = tf.tensor2d([[0], [0], [0.5], [1]]);
* const crossentropy = tf.metrics.binaryCrossentropy(x, y);
* crossentropy.print();
* ```
*
* @param yTrue Binary Tensor of truth.
* @param yPred Binary Tensor of prediction, probabilities for the `1` case.
* @return Accuracy Tensor.
*
* @doc {heading: 'Metrics', namespace: 'metrics'}
*/
declare function binaryCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor;
declare function bincountImpl(xVals: TypedArray, weightsVals: TypedArray, weightsDtype: DataType, weightsShape: number[], size: number): TypedArray;
declare function bincountReduceImpl<R extends Rank>(xBuf: TensorBuffer<R>, weightsBuf: TensorBuffer<R>, size: number, binaryOutput?: boolean): TensorBuffer<R>;
declare function bindCanvasToFramebuffer(gl: WebGLRenderingContext): void;
declare function bindColorTextureToFramebuffer(gl: WebGLRenderingContext, texture: WebGLTexture, framebuffer: WebGLFramebuffer): void;
declare function bindTextureToProgramUniformSampler(gl: WebGLRenderingContext, texture: WebGLTexture, uniformSamplerLocation: WebGLUniformLocation, textureUnit: number): void;
declare function bindTextureUnit(gl: WebGLRenderingContext, texture: WebGLTexture, textureUnit: number): void;
declare function bindVertexBufferToProgramAttribute(gl: WebGLRenderingContext, program: WebGLProgram, attribute: string, buffer: WebGLBuffer, arrayEntriesPerItem: number, itemStrideInBytes: number, itemOffsetInBytes: number): boolean;
declare function bindVertexProgramAttributeStreams(gl: WebGLRenderingContext, program: WebGLProgram, vertexBuffer: WebGLBuffer): boolean;
declare const bitwiseAndImpl: SimpleBinaryKernelImpl;
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* 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.
* =============================================================================
*/
declare class BufferManager {
private device;
private numUsedBuffers;
private numFreeBuffers;
private freeBuffers;
private usedBuffers;
numBytesUsed: number;
numBytesAllocated: number;
constructor(device: GPUDevice);
acquireBuffer(size: number, usage: GPUBufferUsageFlags, mappedAtCreation?: boolean, reuse?: boolean): any;
releaseBuffer(buffer: GPUBuffer, reuse?: boolean): void;
getNumUsedBuffers(): number;
getNumFreeBuffers(): number;
dispose(): void;
}
declare function callAndCheck<T>(gl: WebGLRenderingContext, func: () => T): T;
export declare abstract class Callback extends BaseCallback {
/** Instance of `keras.models.Model`. Reference of the model being trained. */
model: LayersModel;
setModel(model: Container): void;
}
/**
* Container abstracting a list of callbacks.
*/
export declare class CallbackList {
callbacks: BaseCallback[];
queueLength: number;
/**
* Constructor of CallbackList.
* @param callbacks Array of `Callback` instances.
* @param queueLength Queue length for keeping running statistics over
* callback execution time.
*/
constructor(callbacks?: BaseCallback[], queueLength?: number);
append(callback: BaseCallback): void;
setParams(params: Params): void;
setModel(model: Container): void;
/**
* Called at the start of an epoch.
* @param epoch Index of epoch.
* @param logs Dictionary of logs.
*/
onEpochBegin(epoch: number, logs?: UnresolvedLogs): Promise<void>;
/**
* Called at the end of an epoch.
* @param epoch Index of epoch.
* @param logs Dictionary of logs.
*/
onEpochEnd(epoch: number, logs?: UnresolvedLogs): Promise<void>;
/**
* Called right before processing a batch.
* @param batch Index of batch within the current epoch.
* @param logs Dictionary of logs.
*/
onBatchBegin(batch: number, logs?: UnresolvedLogs): Promise<void>;
/**
* Called at the end of a batch.
* @param batch Index of batch within the current epoch.
* @param logs Dictionary of logs.
*/
onBatchEnd(batch: number, logs?: UnresolvedLogs): Promise<void>;
/**
* Called at the beginning of training.
* @param logs Dictionary of logs.
*/
onTrainBegin(logs?: UnresolvedLogs): Promise<void>;
/**
* Called at the end of training.
* @param logs Dictionary of logs.
*/
onTrainEnd(logs?: UnresolvedLogs): Promise<void>;
}
export declare const callbacks: {
earlyStopping: typeof earlyStopping;
};
declare type CallHook = (inputs: Tensor | Tensor[], kwargs: Kwargs) => void;
declare function canBeRepresented(num: number): boolean;
declare function castImpl(values: TypedArray, shape: number[], inputType: DataType, dtype: DataType): [number[], DataType, TypedArray];
/**
* Categorical accuracy metric function.
*
* Example:
* ```js
* const x = tf.tensor2d([[0, 0, 0, 1], [0, 0, 0, 1]]);
* const y = tf.tensor2d([[0.1, 0.8, 0.05, 0.05], [0.1, 0.05, 0.05, 0.8]]);
* const accuracy = tf.metrics.categoricalAccuracy(x, y);
* accuracy.print();
* ```
*
* @param yTrue Binary Tensor of truth: one-hot encoding of categories.
* @param yPred Binary Tensor of prediction: probabilities or logits for the
* same categories as in `yTrue`.
* @return Accuracy Tensor.
*
* @doc {heading: 'Metrics', namespace: 'metrics'}
*/
declare function categoricalAccuracy(yTrue: Tensor, yPred: Tensor): Tensor;
/**
* Categorical crossentropy between an output tensor and a target tensor.
*
* @param target A tensor of the same shape as `output`.
* @param output A tensor resulting from a softmax (unless `fromLogits` is
* `true`, in which case `output` is expected to be the logits).
* @param fromLogits Boolean, whether `output` is the result of a softmax, or is
* a tensor of logits.
*
* @doc {heading: 'Metrics', namespace: 'metrics'}
*/
declare function categoricalCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor;
declare class CategoryEncoding extends Layer {
/** @nocollapse */
static className: string;
private readonly numTokens;
private readonly outputMode;
constructor(args: CategoryEncodingArgs);
getConfig(): serialization.ConfigDict;
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor[] | Tensor;
}
/**
* A preprocessing layer which encodes integer features.
*
* This layer provides options for condensing data into a categorical encoding
* when the total number of tokens are known in advance. It accepts integer
* values as inputs, and it outputs a dense representation of those
* inputs.
*
* Arguments:
*
* numTokens: The total number of tokens the layer should support. All
* inputs to the layer must integers in the range `0 <= value <
* numTokens`, or an error will be thrown.
*
* outputMode: Specification for the output of the layer.
* Defaults to `multiHot`. Values can be `oneHot`, `multiHot` or
* `count`, configuring the layer as follows:
*
* oneHot: Encodes each individual element in the input into an
* array of `numTokens` size, containing a 1 at the element index. If
* the last dimension is size 1, will encode on that dimension. If the
* last dimension is not size 1, will append a new dimension for the
* encoded output.
*
* multiHot: Encodes each sample in the input into a single array
* of `numTokens` size, containing a 1 for each vocabulary term
* present in the sample. Treats the last dimension as the sample
* dimension, if input shape is `(..., sampleLength)`, output shape
* will be `(..., numTokens)`.
*
* count: Like `multiHot`, but the int array contains a count of
* the number of times the token at that index appeared in the sample.
*
* For all output modes, currently only output up to rank 2 is supported.
* Call arguments:
* inputs: A 1D or 2D tensor of integer inputs.
* countWeights: A tensor in the same shape as `inputs` indicating the
* weight for each sample value when summing up in `count` mode. Not used
* in `multiHot` or `oneHot` modes.
*
*
* @doc {heading: 'Layers', subheading: 'CategoryEncoding', namespace: 'layers'}
*/
declare function categoryEncoding(args: CategoryEncodingArgs): CategoryEncoding;
declare interface CategoryEncodingArgs extends LayerArgs {
numTokens: number;
outputMode?: OutputMode;
}
declare const ceilImpl: SimpleUnaryImpl<number, number>;
declare class CenterCrop extends Layer {
/** @nocollapse */
static className: string;
private readonly height;
private readonly width;
constructor(args: CenterCropArgs);
centerCrop(inputs: Tensor3D | Tensor4D, hBuffer: number, wBuffer: number, height: number, width: number, inputHeight: number, inputWidth: number, dtype: DataType): Tensor | Tensor[];
upsize(inputs: Tensor3D | Tensor4D, height: number, width: number, dtype: DataType): Tensor | Tensor[];
call(inputs: Tensor3D | Tensor4D, kwargs: Kwargs): Tensor[] | Tensor;
getConfig(): serialization.ConfigDict;
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
}
/**
* A preprocessing layer which center crops images.
*
* This layers crops the central portion of the images to a target size. If an
* image is smaller than the target size, it will be resized and cropped so as
* to return the largest possible window in the image that matches the target
* aspect ratio.
*
* Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and
* of integer or floating point dtype.
*
* If the input height/width is even and the target height/width is odd (or
* inversely), the input image is left-padded by 1 pixel.
*
* Arguments:
* `height`: Integer, the height of the output shape.
* `width`: Integer, the width of the output shape.
*
* Input shape:
* 3D (unbatched) or 4D (batched) tensor with shape:
* `(..., height, width, channels)`, in `channelsLast` format.
*
* Output shape:
* 3D (unbatched) or 4D (batched) tensor with shape:
* `(..., targetHeight, targetWidth, channels)`.
*
*
* @doc {heading: 'Layers', subheading: 'CenterCrop', namespace: 'layers'}
*/
declare function centerCrop(args?: CenterCropArgs): CenterCrop;
declare interface CenterCropArgs extends LayerArgs {
height: number;
width: number;
}
/**
* For multi-class classification problems, this object is designed to store a
* mapping from class index to the "weight" of the class, where higher weighted
* classes have larger impact on loss, accuracy, and other metrics.
*
* This is useful for cases in which you want the model to "pay more attention"
* to examples from an under-represented class, e.g., in unbalanced datasets.
*/
export declare type ClassWeight = {
[classIndex: number]: number;
};
/**
* Class weighting for a model with multiple outputs.
*
* This object maps each output name to a class-weighting object.
*/
export declare type ClassWeightMap = {
[outputName: string]: ClassWeight;
};
declare const compileProgram: (device: GPUDevice, program: WebGPUProgram, inputsData: InputInfo[], output: TensorInfo_2, parallelCompilation: boolean) => GPUComputePipeline | Promise<GPUComputePipeline>;
declare type ComplexBinaryKernelImpl = (aShape: number[], bShape: number[], aRealVals: Float32Array, aImagVals: Float32Array, bRealVals: Float32Array, bImagVals: Float32Array) => [TypedArray, TypedArray, number[]];
declare function computeDispatch(layout: {
x: number[];
y?: number[];
z?: number[];
}, outputShape: number[], workgroupSize?: [number, number, number], elementsPerThread?: [number, number, number]): [number, number, number];
declare function computeWorkgroupInfoForMatMul(dimAOuter: number, dimInner: number, dimBOuter: number, transposeA?: boolean): WorkgroupInfo;
declare function computeWorkgroupSizeForConv2d(layout: {
x: number[];
y?: number[];
z?: number[];
}, outputShape: number[], isVec4?: boolean): [number, number, number];
declare function computeWorkPerThreadForConv2d(layout: {
x: number[];
y?: number[];
z?: number[];
}, outputShape: number[], isVec4?: boolean): [number, number, number];
declare class Concatenate extends Merge {
/** @nocollapse */
static className: string;
readonly DEFAULT_AXIS = -1;
private readonly axis;
constructor(args?: ConcatenateLayerArgs);
build(inputShape: Shape | Shape[]): void;
protected mergeFunction(inputs: Tensor[]): Tensor;
computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
computeMask(inputs: Tensor | Tensor[], mask?: Tensor | Tensor[]): Tensor;
getConfig(): serialization.ConfigDict;
}
/**
* Layer that concatenates an `Array` of inputs.
*
* It takes a list of tensors, all of the same shape except for the
* concatenation axis, and returns a single tensor, the concatenation
* of all inputs. For example:
*
* ```js
* const input1 = tf.input({shape: [2, 2]});
* const input2 = tf.input({shape: [2, 3]});
* const concatLayer = tf.layers.concatenate();
* const output = concatLayer.apply([input1, input2]);
* console.log(JSON.stringify(output.shape));
* // You get [null, 2, 5], with the first dimension as the undetermined batch
* // dimension. The last dimension (5) is the result of concatenating the
* // last dimensions of the inputs (2 and 3).
* ```
*
* @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'}
*/
declare function concatenate(args?: ConcatenateLayerArgs): Concatenate;
declare interface ConcatenateLayerArgs extends LayerArgs {
/**
* Axis along which to concatenate.
*/
axis?: number;
}
declare function concatImpl(inputs: Array<{
vals: BackendValues;
shape: number[];
}>, outShape: number[], dtype: DataType, simplyConcat: boolean): TypedArray | string[];
/**
* Initializer that generates values initialized to some constant.
*
* @doc {heading: 'Initializers', namespace: 'initializers'}
*/
declare function constant(args: ConstantArgs): Initializer;
declare interface ConstantArgs {
/** The value for each element in the variable. */
value: number;
}
/**
* Base class for functions that impose constraints on weight values
*
* @doc {
* heading: 'Constraints',
* subheading: 'Classes',
* namespace: 'constraints'
* }
*/
declare abstract class Constraint extends serialization.Serializable {
abstract apply(w: Tensor): Tensor;
getConfig(): serialization.ConfigDict;
}
/** @docinline */
declare type ConstraintIdentifier = 'maxNorm' | 'minMaxNorm' | 'nonNeg' | 'unitNorm' | string;
declare namespac