UNPKG

@tensorflow/tfjs-layers

Version:

TensorFlow layers API in JavaScript

406 lines (405 loc) 15.4 kB
/** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /// <amd-module name="@tensorflow/tfjs-layers/dist/layers/merge" /> import { serialization, Tensor } from '@tensorflow/tfjs-core'; import { Layer, LayerArgs, SymbolicTensor } from '../engine/topology'; import { Shape } from '../keras_format/common'; import { Kwargs } from '../types'; /** * Generic Merge layer for element-wise merge functions. * * Used to implement `Sum`, `Average`, `Concatenate`, etc. */ export declare abstract class Merge extends Layer { protected reshapeRequired: boolean; constructor(args?: LayerArgs); /** * Logic for merging multiple tensors, to be overridden by subclasses. * @param inputs */ protected mergeFunction(inputs: Tensor[]): Tensor; /** * Computes the shape of the result of an elementwise operation. * * @param shape1: Shape of the first tensor. * @param shape2: Shape of the second tensor. * @returns Expected output shape when an elementwise operation is carried * out on 2 tensors with shapes `shape1` and `shape2`. * @throws ValueError: If `shape1` and `shape2` are not compatible for * element-wise operations. */ private computeElementwiseOpOutputShape; build(inputShape: Shape | Shape[]): void; call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[]; computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[]; computeMask(inputs: Tensor | Tensor[], mask?: Tensor | Tensor[]): Tensor; } export declare class Add extends Merge { /** @nocollapse */ static className: string; constructor(args?: LayerArgs); protected mergeFunction(inputs: Tensor[]): Tensor; } /** * Calculate the element-wise sum of inputs, which all have the same shape. * * This function can be invoked in three ways. * * 1. Construct an instance of `Add` layer, by using no input argument * or a single configuration argument. The resultant `Add` layer can then * be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const addLayer = tf.layers.add(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = addLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = tf.layers.add([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([1, 2, 3, 4], [2, 2]); * const input2 = tf.tensor2d([10, 20, 30, 40], [2, 2]); * tf.layers.add([input1, input2]).print(); * // Gives [[11, 22], [33, 44]]. * */ export declare function add(config?: SymbolicTensor[] | Tensor[] | LayerArgs): Layer | SymbolicTensor | Tensor; export declare class Multiply extends Merge { /** @nocollapse */ static className: string; constructor(args?: LayerArgs); protected mergeFunction(inputs: Tensor[]): Tensor; } /** * Calculate the element-wise product of inputs, which all have the same shape. * * This function can be invoked in three ways. * * 1. Construct an instance of `Multiply` layer, by using no input argument * or a single configuration argument. The resultant `Multiply` layer can * then be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const multiplyLayer = tf.layers.multiply(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = multiplyLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = tf.layers.multiply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([1, 2, 3, 4], [2, 2]); * const input2 = tf.tensor2d([10, 20, 30, 40], [2, 2]); * tf.layers.multiply([input1, input2]).print(); * // Gives [[10, 40], [90, 160]]. * */ export declare function multiply(config?: SymbolicTensor[] | Tensor[] | LayerArgs): Layer | SymbolicTensor | Tensor; export declare class Average extends Merge { /** @nocollapse */ static className: string; constructor(args?: LayerArgs); protected mergeFunction(inputs: Tensor[]): Tensor; } /** * Calculate the element-wise arithmetic mean of inputs, which all have the same * shape. * * This function can be invoked in three ways. * * 1. Construct an instance of `Average` layer, by using no input argument * or a single configuration argument. The resultant `Average` layer can then * be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const averageLayer = tf.layers.average(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = averageLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = tf.layers.average([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([1, 2, 3, 4], [2, 2]); * const input2 = tf.tensor2d([10, 20, 30, 40], [2, 2]); * tf.layers.average([input1, input2]).print(); * // Gives [[5.5, 11], [16.5, 22]]. * */ export declare function average(config?: SymbolicTensor[] | Tensor[] | LayerArgs): Layer | SymbolicTensor | Tensor; export declare class Maximum extends Merge { /** @nocollapse */ static className: string; constructor(args?: LayerArgs); protected mergeFunction(inputs: Tensor[]): Tensor; } /** * Calculate the element-wise maximum of inputs, which all have the same shape. * * This function can be invoked in three ways. * * 1. Construct an instance of `Maximum` layer, by using no input argument * or a single configuration argument. The resultant `Maximum` layer can then * be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const maximumLayer = tf.layers.maximum(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = maximumLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = tf.layers.maximum([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([1, 20, 3, 40], [2, 2]); * const input2 = tf.tensor2d([10, 2, 30, 4], [2, 2]); * tf.layers.maximum([input1, input2]).print(); * // Gives [[10, 20], [30, 40]]. * */ export declare function maximum(config?: SymbolicTensor[] | Tensor[] | LayerArgs): Layer | SymbolicTensor | Tensor; export declare class Minimum extends Merge { /** @nocollapse */ static className: string; constructor(args?: LayerArgs); protected mergeFunction(inputs: Tensor[]): Tensor; } /** * Calculate the element-wise minimum of inputs, which all have the same shape. * * This function can be invoked in three ways. * * 1. Construct an instance of `Minimum` layer, by using no input argument * or a single configuration argument. The resultant `Minimum` layer can then * be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const minimumLayer = tf.layers.minimum(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = minimumLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const output = tf.layers.minimum([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([1, 20, 3, 40], [2, 2]); * const input2 = tf.tensor2d([10, 2, 30, 4], [2, 2]); * tf.layers.minimum([input1, input2]).print(); * // Gives [[1, 2], [3, 4]]. * */ export declare function minimum(config?: SymbolicTensor[] | Tensor[] | LayerArgs): Layer | SymbolicTensor | Tensor; export declare interface ConcatenateLayerArgs extends LayerArgs { /** * Axis along which to concatenate. */ axis?: number; } export 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; } /** * Concatenate an `Array` of inputs. * * This function can be invoked in three ways. * * 1. Construct an instance of `Concatenate` layer, by using no input argument * or a single configuration argument. The resultant `Concatenate` layer can * then be used on `tf.SymbolicTensor`s or `tf.Tensor`s. For example: * * ```js * const concatLayer = tf.layers.concatenate(); * * // The layer can be applied to inputs. * const input1 = tf.input({shape: [2, 3]}); * const input2 = tf.input({shape: [2, 4]}); * const output = concatLayer.apply([input1, input2]); * console.log(output.shape); * // You get [null, 2, 7], with the first dimension as the undetermined batch * // dimension and the last dimension as the result of concatenating the * // last dimensions of the two inputs. * ``` * * 2. Invoke directly on an `Array` of `tf.SymbolicTensor`s. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.SymbolicTensor`. For example: * * ```js * const input1 = tf.input({shape: [2, 3]}); * const input2 = tf.input({shape: [2, 4]}); * const output = tf.layers.concatenate([input1, input2]); * console.log(output.shape); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension and the last dimension as the result of concatenating the * // last dimensions of the two inputs. * ``` * * 3. Invoke directly on `tf.Tensor`s, i.e., concrete values. This constructs * an `Layer` object internally and calls its `apply` method on the inputs, * generating a new `tf.Tensor` as the result of the computation. For * example: * * ```js * const input1 = tf.tensor2d([[1, 2], [3, 4]], [2, 2]); * const input2 = tf.tensor2d([[10, 20], [30, 40]], [2, 2]); * tf.layers.concatenate([input1, input2]).print(); * // Gives [[1, 2, 10, 20], [3, 4, 30, 40]]. * */ export declare function concatenate(config?: SymbolicTensor[] | Tensor[] | ConcatenateLayerArgs): Layer | SymbolicTensor | Tensor; export declare interface DotLayerArgs extends LayerArgs { /** * Axis or axes along which the dot product will be taken. * * Integer or an Array of integers. */ axes: number | [number, number]; /** * Whether to L2-normalize samples along the dot product axis * before taking the dot product. * * If set to `true`, the output of the dot product is the cosine * proximity between the two samples. */ normalize?: boolean; } export declare class Dot extends Merge { /** @nocollapse */ static className: string; private axes; private normalize; constructor(args: DotLayerArgs); build(inputShape: Shape | Shape[]): void; protected mergeFunction(inputs: Tensor[]): Tensor; private interpretAxes; computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[]; computeMask(inputs: Tensor | Tensor[], mask?: Tensor | Tensor[]): Tensor; getConfig(): serialization.ConfigDict; }