TensorFlow.js
API Version
  • 0.12.5
  • 0.12.0
  • 0.11.7
  • 0.11.6
  • 0.11.2
  • 0.11.1
  • 0.10.0
  • 0.9.0
  • 0.8.0
  • 0.7.0
  • 0.6.1

Tensors are the core datastructure of TensorFlow.js They are a generalization of vectors and matrices to potentially higher dimensions.

We have utility functions for common cases like Scalar, 1D, 2D, 3D and 4D tensors, as well a number of functions to initialize tensors in ways useful for machine learning.

tf.tensor (values, shape?, dtype?) function Source

Creates a tf.Tensor with the provided values, shape and dtype.

// Pass an array of values to create a vector.
tf.tensor([1, 2, 3, 4]).print();
// Pass a nested array of values to make a matrix or a higher
// dimensional tensor.
tf.tensor([[1, 2], [3, 4]]).print();
// Pass a flat array and specify a shape yourself.
tf.tensor([1, 2, 3, 4], [2, 2]).print();
Parameters:
  • values (TypedArray|Array) The values of the tensor. Can be nested array of numbers, or a flat array, or a TypedArray.
  • shape (number[]) The shape of the tensor. Optional. If not provided, it is inferred from values. Optional
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Tensor
tf.scalar (value, dtype?) function Source

Creates rank-0 tf.Tensor (scalar) with the provided value and dtype.

The same functionality can be achieved with tf.tensor(), but in general we recommend using tf.scalar() as it makes the code more readable.

tf.scalar(3.14).print();
Parameters:
  • value (number|boolean) The value of the scalar.
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Scalar
tf.tensor1d (values, dtype?) function Source

Creates rank-1 tf.Tensor with the provided values, shape and dtype.

The same functionality can be achieved with tf.tensor(), but in general we recommend using tf.tensor1d() as it makes the code more readable.

tf.tensor1d([1, 2, 3]).print();
Parameters:
  • values (TypedArray|Array) The values of the tensor. Can be array of numbers, or a TypedArray.
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Tensor1D
tf.tensor2d (values, shape?, dtype?) function Source

Creates rank-2 tf.Tensor with the provided values, shape and dtype.

The same functionality can be achieved with tf.tensor(), but in general we recommend using tf.tensor2d() as it makes the code more readable.

// Pass a nested array.
tf.tensor2d([[1, 2], [3, 4]]).print();
// Pass a flat array and specify a shape.
tf.tensor2d([1, 2, 3, 4], [2, 2]).print();
Parameters:
  • values (TypedArray|Array) The values of the tensor. Can be nested array of numbers, or a flat array, or a TypedArray.
  • shape ([number, number]) The shape of the tensor. If not provided, it is inferred from values. Optional
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Tensor2D
tf.tensor3d (values, shape?, dtype?) function Source

Creates rank-3 tf.Tensor with the provided values, shape and dtype.

The same functionality can be achieved with tf.tensor(), but in general we recommend using tf.tensor3d() as it makes the code more readable.

// Pass a nested array.
tf.tensor3d([[[1], [2]], [[3], [4]]]).print();
// Pass a flat array and specify a shape.
tf.tensor3d([1, 2, 3, 4], [2, 2, 1]).print();
Parameters:
  • values (TypedArray|Array) The values of the tensor. Can be nested array of numbers, or a flat array, or a TypedArray.
  • shape ([number, number, number]) The shape of the tensor. If not provided, it is inferred from values. Optional
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Tensor3D
tf.tensor4d (values, shape?, dtype?) function Source

Creates rank-4 tf.Tensor with the provided values, shape and dtype.

The same functionality can be achieved with tf.tensor(), but in general we recommend using tf.tensor4d() as it makes the code more readable.

// Pass a nested array.
tf.tensor4d([[[[1], [2]], [[3], [4]]]]).print();
// Pass a flat array and specify a shape.
tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]).print();
Parameters:
  • values (TypedArray|Array) The values of the tensor. Can be nested array of numbers, or a flat array, or a TypedArray.
  • shape ([number, number, number, number]) The shape of the tensor. Optional. If not provided, it is inferred from values. Optional
  • dtype ('float32'|'int32'|'bool') The data type. Optional
Returns: tf.Tensor4D
tf.buffer (shape, dtype?, values?) function Source

Creates an empty tf.TensorBuffer with the specified shape and dtype.

The values are stored in cpu as TypedArray. Fill the buffer using buffer.set(), or by modifying directly buffer.values. When done, call buffer.toTensor() to get an immutable tf.Tensor with those values.

When done, call buffer.toTensor() to get an immutable tf.Tensor with those values.

// Create a buffer and set values at particular indices.
const buffer = tf.buffer([2, 2]);
buffer.set(3, 0, 0);
buffer.set(5, 1, 0);

// Convert the buffer back to a tensor.
buffer.toTensor().print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • dtype ('float32'|'int32'|'bool') The dtype of the buffer. Defaults to 'float32'. Optional
  • values (TypedArray) The values of the buffer as TypedArray. Defaults to zeros. Optional
Returns: tf.TensorBuffer
tf.clone (x) function Source

Creates a new tensor with the same values and shape as the specified tensor.

x.clone().print();
Parameters:
Returns: tf.Tensor
tf.fill (shape, value, dtype?) function Source

Creates a tf.Tensor filled with a scalar value.

tf.fill([2, 2], 4).print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • value (number) The scalar value to fill the tensor with.
  • dtype ('float32'|'int32'|'bool') The type of an element in the resulting tensor. Defaults to 'float'. Optional
Returns: tf.Tensor
tf.fromPixels (pixels, numChannels?) function Source

Creates a tf.Tensor from an image.

const image = new ImageData(1, 1);
image.data[0] = 100;
image.data[1] = 150;
image.data[2] = 200;
image.data[3] = 255;

tf.fromPixels(image).print();
Parameters:
  • pixels (ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement) The input image to construct the tensor from.
  • numChannels (number) The number of channels of the output tensor. The supported image types are all 4-channel by default, a numChannels value less than 4 allows you to ignore channels. Optional
Returns: tf.Tensor3D
tf.linspace (start, stop, num) function Source

Return an evenly spaced sequence of numbers over the given interval.

tf.linspace(0, 9, 10).print();
Parameters:
  • start (number) The start value of the sequence.
  • stop (number) The end value of the sequence.
  • num (number) The number of values to generate.
Returns: tf.Tensor1D
tf.oneHot (indices, depth, onValue?, offValue?) function Source

Creates a one-hot tf.Tensor. The locations represented by indices take value onValue (defaults to 1), while all other locations take value offValue (defaults to 0).

tf.oneHot(tf.tensor1d([0, 1]), 3).print();
Parameters:
  • indices (tf.Tensor1D) 1D Array of indices.
  • depth (number) The depth of the one hot dimension.
  • onValue (number) A number used to fill in output when the index matches the location. Optional
  • offValue (number) A number used to fill in the output when the index does not match the location. Optional
Returns: tf.Tensor2D
tf.ones (shape, dtype?) function Source

Creates a tf.Tensor with all elements set to 1.

tf.ones([2, 2]).print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • dtype ('float32'|'int32'|'bool') The type of an element in the resulting tensor. Defaults to 'float'. Optional
Returns: tf.Tensor
tf.onesLike (x) function Source

Creates a tf.Tensor with all elements set to 1 with the same shape as the given tensor.

const x = tf.tensor([1, 2]);
tf.onesLike(x).print();
Parameters:
Returns: tf.Tensor
tf.print (x, verbose?) function Source

Prints information about the tf.Tensor including its data.

const verbose = true;
tf.tensor2d([1, 2, 3, 4], [2, 2]).print(verbose);
Parameters:
  • x (tf.Tensor) The tensor to be printed.
  • verbose (boolean) Whether to print verbose information about the Tensor, including dtype and size. Optional
Returns: void
tf.randomNormal (shape, mean?, stdDev?, dtype?, seed?) function Source

Creates a tf.Tensor with values sampled from a normal distribution.

tf.randomNormal([2, 2]).print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • mean (number) The mean of the normal distribution. Optional
  • stdDev (number) The standard deviation of the normal distribution. Optional
  • dtype ('float32'|'int32') The data type of the output. Optional
  • seed (number) The seed for the random number generator. Optional
Returns: tf.Tensor
tf.randomUniform (shape, minval?, maxval?, dtype?) function Source

Creates a tf.Tensor with values sampled from a uniform distribution.

The generated values follow a uniform distribution in the range [minval, maxval). The lower bound minval is included in the range, while the upper bound maxval is excluded.

tf.randomUniform([2, 2]).print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • minval (number) The lower bound on the range of random values to generate. Defaults to 0. Optional
  • maxval (number) The upper bound on the range of random values to generate. Defaults to 1. Optional
  • dtype ('float32'|'int32'|'bool') The data type of the output tensor. Defaults to 'float32'. Optional
Returns: tf.Tensor
tf.range (start, stop, step?, dtype?) function Source

Creates a new tf.Tensor1D filled with the numbers in the range provided.

The tensor is a is half-open interval meaning it includes start, but excludes stop. Decrementing ranges and negative step values are also supported.

tf.range(0, 9, 2).print();
Parameters:
  • start (number) An integer start value
  • stop (number) An integer stop value
  • step (number) An integer increment (will default to 1 or -1) Optional
  • dtype ('float32'|'int32') The data type of the output tensor. Defaults to 'float32'. Optional
Returns: tf.Tensor1D
tf.truncatedNormal (shape, mean?, stdDev?, dtype?, seed?) function Source

Creates a tf.Tensor with values sampled from a truncated normal distribution.

tf.truncatedNormal([2, 2]).print();

The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.

Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • mean (number) The mean of the normal distribution. Optional
  • stdDev (number) The standard deviation of the normal distribution. Optional
  • dtype ('float32'|'int32') The data type of the output tensor. Optional
  • seed (number) The seed for the random number generator. Optional
Returns: tf.Tensor
tf.variable (initialValue, trainable?, name?, dtype?) function Source

Creates a new variable with the provided initial value.

const x = tf.variable(tf.tensor([1, 2, 3]));
x.assign(tf.tensor([4, 5, 6]));

x.print();
Parameters:
  • initialValue (tf.Tensor) Initial value for the tensor.
  • trainable (boolean) If true, optimizers are allowed to update it. Optional
  • name (string) Name of the variable. Defaults to a unique id. Optional
  • dtype ('float32'|'int32'|'bool') If set, initialValue will be converted to the given type. Optional
Returns: tf.Variable
tf.zeros (shape, dtype?) function Source

Creates a tf.Tensor with all elements set to 0.

tf.zeros([2, 2]).print();
Parameters:
  • shape (number[]) An array of integers defining the output tensor shape.
  • dtype ('float32'|'int32'|'bool') The type of an element in the resulting tensor. Can be 'float32', 'int32' or 'bool'. Defaults to 'float'. Optional
Returns: tf.Tensor
tf.zerosLike (x) function Source

Creates a tf.Tensor with all elements set to 0 with the same shape as the given tensor.

const x = tf.tensor([1, 2]);
tf.zerosLike(x).print();
Parameters:
  • x (tf.Tensor) The tensor of required shape.
Returns: tf.Tensor

This section shows the main Tensor related classes in TensorFlow.js and the methods we expose on them.

A tf.Tensor object represents an immutable, multidimensional array of numbers that has a shape and a data type.

See tf.tensor() for details on how to create a tf.Tensor.

flatten () method Source

Flatten a Tensor to a 1D array.

Returns: tf.Tensor1D
asScalar () method Source

Converts a size-1 tf.Tensor to a tf.Scalar.

Returns: tf.Scalar
as1D () method Source

Converts a tf.Tensor to a tf.Tensor1D.

Returns: tf.Tensor1D
as2D (rows, columns) method Source

Converts a tf.Tensor to a tf.Tensor2D.

Parameters:
Returns: tf.Tensor2D
as3D (rows, columns, depth) method Source

Converts a tf.Tensor to a tf.Tensor3D.

Parameters:
Returns: tf.Tensor3D
as4D (rows, columns, depth, depth2) method Source

Converts a tf.Tensor to a tf.Tensor4D.

Parameters:
Returns: tf.Tensor4D
asType (dtype) method Source

Casts a tf.Tensor to a specified dtype.

Parameters:
  • dtype ('float32'|'int32'|'bool') Data-type to cast the tensor to.
Returns: this
buffer () method Source

Returns a tf.TensorBuffer that holds the underlying data.

Returns: tf.TensorBuffer
data () method Source

Asynchronously downloads the values from the tf.Tensor. Returns a promise of TypedArray that resolves when the computation has finished.

Returns: Promise
dataSync () method Source

Synchronously downloads the values from the tf.Tensor. This blocks the UI thread until the values are ready, which can cause performance issues.

Returns: TypedArray
dispose () method Source

Disposes tf.Tensor from memory.

Returns: void
toFloat () method Source

Casts the array to type float32

Returns: this
toInt () method Source

Casts the array to type int32

Returns: this
toBool () method Source

Casts the array to type bool

Returns: this
print (verbose?) method Source

Prints the tf.Tensor. See tf.print() for details.

Parameters:
  • verbose (boolean) Whether to print verbose information about the tensor, including dtype and size. Optional
Returns: void
reshape (newShape) method Source

Reshapes the tensor into the provided shape. See tf.reshape() for more details.

Parameters:
  • newShape (number[]) An array of integers defining the output tensor shape.
Returns: tf.Tensor
reshapeAs (x) method Source

Reshapes the tensor into the shape of the provided tensor.

Parameters:
  • x (tf.Tensor) The tensor of required shape.
Returns: tf.Tensor
expandDims (axis?) method Source

Returns a tf.Tensor that has expanded rank, by inserting a dimension into the tensor's shape. See tf.expandDims() for details.

Parameters:
  • axis (number) The dimension index at which to insert shape of 1. Defaults to 0 (the first dimension). Optional
Returns: tf.Tensor
squeeze (axis?) method Source

Returns a tf.Tensor with dimensions of size 1 removed from the shape. See tf.squeeze() for more details.

Parameters:
  • axis (number[]) A list of numbers. If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Optional
Returns: tf.Tensor
clone () method Source

Returns a copy of the tensor. See tf.clone() for details.

Returns: tf.Tensor
toString (verbose?) method Source

Returns a human-readable description of the tensor. Useful for logging.

Parameters:
  • verbose (boolean) Optional
Returns: string
tf.Variable extends tf.Tensor class Source

A mutable tf.Tensor, useful for persisting state, e.g. for training.

assign (newValue) method Source

Assign a new tf.Tensor to this variable. The new tf.Tensor must have the same shape and dtype as the old tf.Tensor.

Parameters:
  • newValue (tf.Tensor) New tensor to be assigned to this variable.
Returns: void

A mutable object, similar to tf.Tensor, that allows users to set values at locations before converting to an immutable tf.Tensor.

See tf.buffer() for creating a tensor buffer.

set (value, locs) method Source

Sets a value in the buffer at a given location.

Parameters:
  • value (number) The value to set.
  • locs (number[]) The location indices.
Returns: void
get (locs) method Source

Returns the value in the buffer at the provided location.

Parameters:
  • locs (number[]) The location indices.
Returns: number
toTensor () method Source

Creates an immutable tf.Tensor object from the buffer.

Returns: tf.Tensor

This section describes some common Tensor transformations for reshaping and type-casting.

tf.cast (x, dtype) function Source

Casts a tf.Tensor to a new dtype.

const x = tf.tensor1d([1.5, 2.5, 3]);
tf.cast(x, 'int32').print();
Parameters:
  • x (tf.Tensor) The input tensor to be casted.
  • dtype ('float32'|'int32'|'bool') The dtype to cast the input tensor to.
Returns: tf.Tensor
tf.expandDims (x, axis?) function Source

Returns a tf.Tensor that has expanded rank, by inserting a dimension into the tensor's shape.

const x = tf.tensor1d([1, 2, 3, 4]);
const axis = 1;
x.expandDims(axis).print();
Parameters:
  • x (tf.Tensor) The input tensor whose dimensions to be expanded.
  • axis (number) The dimension index at which to insert shape of 1. Defaults to 0 (the first dimension). Optional
Returns: tf.Tensor
tf.pad (x, paddings, constantValue?) function Source

Pads a tf.Tensor with a given value and paddings.

This operation currently only implements the CONSTANT mode.

const x = tf.tensor1d([1, 2, 3, 4]);
x.pad([[1, 2]]).print();
Parameters:
  • x (tf.Tensor) The tensor to pad.
  • paddings (Array) An array of length R (the rank of the tensor), where each element is a length-2 tuple of ints [padBefore, padAfter], specifying how much to pad along each dimension of the tensor.
  • constantValue (number) The pad value to use. Defaults to 0. Optional
Returns: tf.Tensor
tf.reshape (x, shape) function Source

Reshapes a tf.Tensor to a given shape.

Given a input tensor, returns a new tensor with the same values as the input tensor with shape shape.

If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.

If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.

const x = tf.tensor1d([1, 2, 3, 4]);
x.reshape([2, 2]).print();
Parameters:
  • x (tf.Tensor) The input tensor to be reshaped.
  • shape (number[]) An array of integers defining the output tensor shape.
Returns: tf.Tensor
tf.squeeze (x, axis?) function Source

Removes dimensions of size 1 from the shape of a tf.Tensor.

const x = tf.tensor([1, 2, 3, 4], [1, 1, 4]);
x.squeeze().print();
Parameters:
  • x (tf.Tensor) The input tensor to be squeezed.
  • axis (number[]) An optional list of numbers. If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Optional
Returns: tf.Tensor

TensorFlow.js provides several operations to slice or extract parts of a tensor, or join multiple tensors together.

tf.concat (tensors, axis?) function Source

Concatenates a list of tf.Tensors along a given axis.

The tensors ranks and types must match, and their sizes must match in all dimensions except axis.

const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
a.concat(b).print();  // or a.concat(b)
const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
const c = tf.tensor1d([5, 6]);
tf.concat([a, b, c]).print();
const a = tf.tensor2d([[1, 2], [10, 20]]);
const b = tf.tensor2d([[3, 4], [30, 40]]);
const axis = 1;
tf.concat([a, b], axis).print();
Parameters:
  • tensors (tf.Tensor[]) A list of tensors to concatenate.
  • axis (number) The axis to concate along. Defaults to 0 (the first dim). Optional
Returns: tf.Tensor
tf.gather (x, indices, axis?) function Source

Gather slices from tensor x's axis axis according to indices.

const x = tf.tensor1d([1, 2, 3, 4]);
const indices = tf.tensor1d([1, 3, 3]);

x.gather(indices).print();
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
const indices = tf.tensor1d([1, 1, 0]);

x.gather(indices).print();
Parameters:
  • x (tf.Tensor) The input tensor whose slices to be gathered.
  • indices (tf.Tensor1D) The indices of the values to extract.
  • axis (number) The axis over which to select values. Defaults to 0. Optional
Returns: tf.Tensor
tf.reverse (x, axis?) function Source

Reverses a tf.Tensor along a specified axis.

const x = tf.tensor1d([1, 2, 3, 4]);

x.reverse().print();
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.reverse(axis).print();
Parameters:
  • x (tf.Tensor) The input tensor to be reversed.
  • axis (number|number[]) The set of dimensions to reverse. Must be in the range [-rank(x), rank(x)). Defaults to all axes. Optional
Returns: tf.Tensor
tf.slice (x, begin, size) function Source

Extracts a slice from a tf.Tensor starting at coordinates begin and is of size size.

Also available are stricter rank-specific methods with the same signature as this method that assert that x is of the given rank:

  • tf.slice1d
  • tf.slice2d
  • tf.slice3d
  • tf.slice4d
const x = tf.tensor1d([1, 2, 3, 4]);

x.slice([1], [2]).print();
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

x.slice([1, 0], [1, 2]).print();
Parameters:
  • x (tf.Tensor) The input tf.Tensor to slice from.
  • begin (number[]) The coordinates to start the slice from. The length of this array should match the rank of x.
  • size (number[]) The size of the slice. The length of this array should match the rank of x.
Returns: tf.Tensor
tf.split (x, numOrSizeSplits, axis?) function Source

Splits a tf.Tensor into sub tensors.

If numOrSizeSplits is a number, splits x along dimension axis into numOrSizeSplits smaller tensors. Requires that numOrSizeSplits evenly divides x.shape[axis].

If numOrSizeSplits is a number array, splits x into (numOrSizeSplits.length pieces. The shape of the i-th piece has the same size as x except along dimension axis where the size is numOrSizeSplits[i].

const x = tf.tensor2d([1, 2, 3, 4, 5, 6, 7, 8], [2, 4]);
const [a, b] = tf.split(x, 2, 1);
a.print();
b.print();

const [c, d, e] = tf.split(x, [1, 2, 1], 1);
c.print();
d.print();
e.print();
Parameters:
  • x (tf.Tensor) The input tensor to split.
  • numOrSizeSplits (number[]|number) Either an integer indicating the number of splits along the axis or an array of integers containing the sizes of each output tensor along the axis. If a number then it must evenly divide x.shape[axis]; otherwise the sum of sizes must match x.shape[axis].
  • axis (number) The dimension along which to split. Defaults to 0 (the first dim). Optional
Returns: any
tf.stack (tensors, axis?) function Source

Stacks a list of rank-R tf.Tensors into one rank-(R+1) tf.Tensor.

const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
const c = tf.tensor1d([5, 6]);
tf.stack([a, b, c]).print();
Parameters:
  • tensors (tf.Tensor[]) A list of tensor objects with the same shape and dtype.
  • axis (number) The axis to stack along. Defaults to 0 (the first dim). Optional
Returns: tf.Tensor
tf.tile (x, reps) function Source

Construct an tensor by repeating it the number of times given by reps.

This operation creates a new tensor by replicating tf.input() reps times. The output tensor's i'th dimension has input.shape[i] * reps[i] elements, and the values of tf.input() are replicated reps[i] times along the i'th dimension. For example, tiling [a, b, c, d] by [2] produces [a, b, c, d, a, b, c, d].

const a = tf.tensor1d([1, 2]);

a.tile([2]).print();    // or a.tile([2])
const a = tf.tensor2d([1, 2, 3, 4], [2, 2]);

a.tile([1, 2]).print();  // or a.tile([1, 2])
Parameters:
  • x (tf.Tensor) The tensor to transpose.
  • reps (number[]) Determines the number of replications per dimension.
Returns: tf.Tensor

Models are one of the primary abstractions used in TensorFlow.js Layers. Models can be trained, evaluated, and used for prediction. A model's state (topology, and optionally, trained weights) can be restored from various formats.

Models are a collection of Layers, see Model Creation for details about how Layers can be connected.

There are two primary ways of creating models.

  • Sequential — Easiest, works if the models is a simple stack of each layer's input resting on the top of the previous layer's output.
  • Model — Offers more control if the layers need to be wired together in graph-like ways — multiple 'towers', layers that skip a layer, etc.
tf.sequential (config?) function Source

Creates a tf.Sequential model. A sequential model is any model where the outputs of one layer are the inputs to the next layer, i.e. the model topology is a simple 'stack' of layers, with no branching or skipping.

This means that the first layer passed to a Sequential model should have a defined input shape. What that means is that it should have received an inputShape or batchInputShape argument, or for some type of layers (recurrent, Dense...) an inputDim argument.

The key difference between tf.model() and tf.sequential() is that tf.sequential() is less generic, supporting only a linear stack of layers. tf.model() is more generic and supports an arbitrary graph (without cycles) of layers.

Examples:

const model = tf.sequential();

// First layer must have an input shape defined.
model.add(tf.layers.dense({units: 32, inputShape: [50]}));
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 4}));

// Inspect the inferred shape of the model's output, which equals
// `[null, 4]`. The 1st dimension is the undetermined batch dimension; the
// 2nd is the output size of the model's last layer.
console.log(JSON.stringify(model.outputs[0].shape));

It is also possible to specify a batch size (with potentially undetermined batch dimension, denoted by "null") for the first layer using the batchInputShape key. The following example is equivalent to the above:

const model = tf.sequential();

// First layer must have a defined input shape
model.add(tf.layers.dense({units: 32, batchInputShape: [null, 50]}));
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 4}));

// Inspect the inferred shape of the model's output.
console.log(JSON.stringify(model.outputs[0].shape));

You can also use an Array of already-constructed Layers to create a tf.Sequential model:

const model = tf.sequential({
   layers: [tf.layers.dense({units: 32, inputShape: [50]}),
            tf.layers.dense({units: 4})]
});
console.log(JSON.stringify(model.outputs[0].shape));
Parameters:
  • config (Object) Optional
  • layers (tf.layers.Layer[]) Stack of layers for the model. Optional
  • name (string) The name of this model. Optional
Returns: tf.Sequential
tf.model (config) function Source

A model is a data structure that consists of Layers and defines inputs and outputs.

The key difference between tf.model() and tf.sequential() is that tf.model() is more generic, supporting an arbitrary graph (without cycles) of layers. tf.sequential() is less generic and supports only a linear stack of layers.

When creating a tf.Model, specify its input(s) and output(s). Layers are used to wire input(s) to output(s).

For example, the following code snippet defines a model consisting of two dense layers, with 10 and 4 units, respectively.

// Define input, which has a size of 5 (not including batch dimension).
const input = tf.input({shape: [5]});

// First dense layer uses relu activation.
const denseLayer1 = tf.layers.dense({units: 10, activation: 'relu'});
// Second dense layer uses softmax activation.
const denseLayer2 = tf.layers.dense({units: 2, activation: 'softmax'});

// Obtain the output symbolic tensor by applying the layers on the input.
const output = denseLayer2.apply(denseLayer1.apply(input));

// Create the model based on the inputs.
const model = tf.model({inputs: input, outputs: output});

// The model can be used for training, evaluation and prediction.
// For example, the following line runs prediction with the model on
// some fake data.
model.predict(tf.ones([2, 5])).print();

See also: tf.sequential(), tf.loadModel().

Parameters:
Returns: tf.Model
tf.input (config) function Source

Used to instantiate an input to a model as a tf.SymbolicTensor.

Users should call the tf.input() factory function for consistency with other generator functions.

Example:

// Defines a simple logistic regression model with 32 dimensional input
// and 3 dimensional output.
const x = tf.input({shape: [32]});
const y = tf.layers.dense({units: 3, activation: 'softmax'}).apply(x);
const model = tf.model({inputs: x, outputs: y});
model.predict(tf.ones([2, 32])).print();

Note: tf.input() is only necessary when using tf.model(). When using tf.sequential(), specify inputShape for the first layer or use inputLayer as the first layer.

Parameters:
  • config (InputConfig)
  • shape (number[]) A shape, not including the batch size. For instance, shape=[32] indicates that the expected input will be batches of 32-dimensional vectors. Optional
  • batchShape (number[]) A shape tuple (integer), including the batch size. For instance, batchShape=[10, 32] indicates that the expected input will be batches of 10 32-dimensional vectors. batchShape=[null, 32] indicates batches of an arbitrary number of 32-dimensional vectors. Optional
  • name (string) An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. Optional
  • dtype ('float32'|'int32'|'bool') Optional
  • sparse (boolean) A boolean specifying whether the placeholder to be created is sparse. Optional
tf.loadModel (modelConfigPath) function Source

Load a model, including its topology and optionally weights. See the Tutorial named "How to import a Keras Model" for usage examples.

Parameters:
  • modelConfigPath (string) A path to the ModelAndWeightsConfig JSON describing the model in the canonical TensorFlow.js format.

    The content of the JSON file is assumed to be a JSON object with the following fields and values:

    • 'modelTopology': A JSON object that can be either of:
    1. a model architecture JSON consistent with the format of the return value of keras.Model.to_json()
    2. a full model JSON in the format of keras.models.save_model().
    • 'weightsManifest': A TensorFlow.js weights manifest.

    See the Python converter function save_model() for more details.

    It is also assumed that model weights can be accessed from relative paths described by the paths fields in weights manifest.

Returns: Promise
tf.Model extends Container class Source

A tf.Model is a directed, acyclic graph of Layers plus methods for training, evaluation, prediction and saving.

tf.Model is the basic unit of training, inference and evaluation in TensorFlow.js. To create a tf.Model, use tf.model().

See also: tf.Sequential, tf.loadModel().

compile (config) method Source

Configures and prepares the model for training and evaluation. Compiling outfits the model with an optimizer, loss, and/or metrics. Calling fit or evaluate on an un-compiled model will throw an error.

Parameters:
  • config (Object) a ModelCompileConfig specifying the loss, optimizer, and metrics to be used for fitting and evaluating this model.
  • optimizer (string|tf.train.Optimizer) An instance of tf.train.Optimizer or a string name for an Optimizer.
  • loss (string|string[]|{[outputName: string]: string}) String (name of objective function) or objective function. If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or an Array of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.
  • metrics (string[]|{[outputName: string]: string}) List of metrics to be evaluated by the model during training and testing. Typically you will use metrics=['accuracy']. To specify different metrics for different outputs of a multi-output model, you could also pass a dictionary. Optional
Returns: void
evaluate (x, y, config?) method Source

Returns the loss value & metrics values for the model in test mode.

Loss and metrics are specified during compile(), which needs to happen before calls to evaluate().

Computation is done in batches.

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const result = model.evaluate(
     tf.ones([8, 10]), tf.ones([8, 1]), {batchSize: 4});
result.print();
Parameters:
  • x (tf.Tensor|tf.Tensor[]) tf.Tensor of test data, or an Array of tf.Tensors if the model has multiple inputs.
  • y (tf.Tensor|tf.Tensor[]) tf.Tensor of target data, or an Array of tf.Tensors if the model has multiple outputs.
  • config (Object) A ModelEvaluateConfig, containing optional fields. Optional
  • batchSize (number) Batch size (Integer). If unspecified, it will default to 32. Optional
  • verbose (ModelLoggingVerbosity) Verbosity mode. Optional
  • sampleWeight (tf.Tensor) Tensor of weights to weight the contribution of different samples to the loss and metrics. Optional
  • steps (number) integer: total number of steps (batches of samples) before declaring the evaluation round finished. Ignored with the default value of undefined. Optional
Returns: tf.Scalar|tf.Scalar[]
predict (x, config?) method Source

Generates output predictions for the input samples.

Computation is done in batches.

Note: the "step" mode of predict() is currently not supported. This is because the TensorFlow.js core backend is imperative only.

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.predict(tf.ones([8, 10]), {batchSize: 4}).print();
Parameters:
  • x (tf.Tensor|tf.Tensor[]) The input data, as an Tensor, or an Array of tf.Tensors if the model has multiple inputs.
  • config (Object) A ModelPredictConfig object containing optional fields. Optional
  • batchSize (number) Batch size (Integer). If unspecified, it will default to 32. Optional
  • verbose (boolean) Verbosity mode. Defaults to false. Optional
Returns: tf.Tensor |tf.Tensor[]

Returns predictions for a single batch of samples.

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.predictOnBatch(tf.ones([8, 10])).print();
Parameters:
  • x (tf.Tensor) : Input samples, as an Tensor
Returns: tf.Tensor|tf.Tensor[]
fit (x, y, config?) method Source

Trains the model for a fixed number of epochs (iterations on a dataset).

const model = tf.sequential({
     layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
for (let i = 1; i < 5 ; ++i) {
   const h = await model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
       batchSize: 4,
       epochs: 3
   });
   console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
}
Parameters:
  • x (tf.Tensor|tf.Tensor[]|{[inputName: string]: tf.Tensor}) tf.Tensor of training data, or an array of tf.Tensors if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to tf.Tensors.
  • y (tf.Tensor|tf.Tensor[]|{[inputName: string]: tf.Tensor}) tf.Tensor of target (label) data, or an array of tf.Tensors if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to tf.Tensors.
  • config (Object) A ModelFitConfig, containing optional fields. Optional
  • batchSize (number) Number of samples per gradient update. If unspecified, it will default to 32. Optional
  • epochs (number) The number of times to iterate over the training data arrays. Optional
  • verbose (ModelLoggingVerbosity) Optional
  • callbacks (Callback[]|CustomCallbackConfig|CustomCallbackConfig[]) List of callbacks to be called during training. Optional
  • validationSplit (number) Float between 0 and 1: fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. Optional
  • validationData ([ tf.Tensor|tf.Tensor[], tf.Tensor|tf.Tensor[] ]|[tf.Tensor | tf.Tensor[], tf.Tensor|tf.Tensor[], tf.Tensor|tf.Tensor[]]) Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. This could be a tuple [xVal, yVal] or a tuple [xVal, yVal, valSampleWeights]. The model will not be trained on this data. validationData will override validationSplit. Optional
  • shuffle (boolean) Whether to shuffle the training data before each epoch. Has no effect when stepsPerEpoch is not null. Optional
  • classWeight ({[classIndex: string]: number}) Optional dictionary mapping class indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class. Optional
  • sampleWeight (tf.Tensor) Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequenceLength), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sampleWeightMode="temporal" in compile(). Optional
  • initialEpoch (number) Epoch at which to start training (useful for resuming a previous training run). Optional
  • stepsPerEpoch (number) Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with Input Tensors such as TensorFlow data tensors, the default null is equal to the number of unique samples in your dataset divided by the batch size, or 1 if that cannot be determined. Optional
  • validationSteps (number) Only relevant if stepsPerEpoch is specified. Total number of steps (batches of samples) to validate before stopping. Optional
Returns: Promise
tf.Sequential extends tf.Model class Source

A model with a stack of layers, feeding linearly from one to the next.

tf.sequential() is a factory function that creates an instance of tf.Sequential.

add (layer) method Source

Adds a layer instance on top of the layer stack.

Parameters:
Returns: void
evaluate (x, y, config?) method Source

Returns the loss value & metrics values for the model in test mode.

Loss and metrics are specified during compile(), which needs to happen before calls to evaluate().

Computation is done in batches.

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
   batchSize: 4,
});
result.print();
Parameters:
  • x (tf.Tensor|tf.Tensor[]) tf.Tensor of test data, or an Array of tf.Tensors if the model has multiple inputs.
  • y (tf.Tensor|tf.Tensor[]) tf.Tensor of target data, or an Array of tf.Tensors if the model has multiple outputs.
  • config (Object) A ModelEvaluateConfig, containing optional fields. Optional
  • batchSize (number) Batch size (Integer). If unspecified, it will default to 32. Optional
  • verbose (ModelLoggingVerbosity) Verbosity mode. Optional
  • sampleWeight (tf.Tensor) Tensor of weights to weight the contribution of different samples to the loss and metrics. Optional
  • steps (number) integer: total number of steps (batches of samples) before declaring the evaluation round finished. Ignored with the default value of undefined. Optional
Returns: tf.Scalar|tf.Scalar[]
predict (x, config?) method Source

Generates output predictions for the input samples.

Computation is done in batches.

Note: the "step" mode of predict() is currently not supported. This is because the TensorFow.js core backend is imperative only.

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.predict(tf.ones([2, 10])).print();
Parameters:
  • x (tf.Tensor|tf.Tensor[]) The input data, as an Tensor, or an Array of tf.Tensors if the model has multiple inputs.
  • config (Object) Optional
  • batchSize (number) Batch size (Integer). If unspecified, it will default to 32. Optional
  • verbose (boolean) Verbosity mode. Defaults to false. Optional
Returns: tf.Tensor |tf.Tensor[]
fit (x, y, config?) method Source

Trains the model for a fixed number of epochs (iterations on a dataset).

const model = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [10]})]
});
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const history = await model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
   batchSize: 4,
   epochs: 3
});
Parameters:
  • x (tf.Tensor|tf.Tensor[]|{[inputName: string]: tf.Tensor}) tf.Tensor of training data, or an array of tf.Tensors if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to tf.Tensors.
  • y (tf.Tensor|tf.Tensor[]|{[inputName: string]: tf.Tensor}) tf.Tensor of target (label) data, or an array of tf.Tensors if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to tf.Tensors.
  • config (Object) A ModelFitConfig, containing optional fields. Optional
  • batchSize (number) Number of samples per gradient update. If unspecified, it will default to 32. Optional
  • epochs (number) The number of times to iterate over the training data arrays. Optional
  • verbose (ModelLoggingVerbosity) Optional
  • callbacks (Callback[]|CustomCallbackConfig|CustomCallbackConfig[]) List of callbacks to be called during training. Optional
  • validationSplit (number) Float between 0 and 1: fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. Optional
  • validationData ([ tf.Tensor|tf.Tensor[], tf.Tensor|tf.Tensor[] ]|[tf.Tensor | tf.Tensor[], tf.Tensor|tf.Tensor[], tf.Tensor|tf.Tensor[]]) Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. This could be a tuple [xVal, yVal] or a tuple [xVal, yVal, valSampleWeights]. The model will not be trained on this data. validationData will override validationSplit. Optional
  • shuffle (boolean) Whether to shuffle the training data before each epoch. Has no effect when stepsPerEpoch is not null. Optional
  • classWeight ({[classIndex: string]: number}) Optional dictionary mapping class indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class. Optional
  • sampleWeight (tf.Tensor) Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequenceLength), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sampleWeightMode="temporal" in compile(). Optional
  • initialEpoch (number) Epoch at which to start training (useful for resuming a previous training run). Optional
  • stepsPerEpoch (number) Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with Input Tensors such as TensorFlow data tensors, the default null is equal to the number of unique samples in your dataset divided by the batch size, or 1 if that cannot be determined. Optional
  • validationSteps (number) Only relevant if stepsPerEpoch is specified. Total number of steps (batches of samples) to validate before stopping. Optional
Returns: Promise
tf.SymbolicTensor extends TensorInterface class Source

tf.SymbolicTensor is a placeholder for a Tensor without any concrete value.

They are most often encountered when building a graph of Layers for a a tf.Model and the input data's shape, but not values are known.

Layers are the primary building block for constructing a Model. Each layer will typically perform some computation to transform its input to its output.

Layers will automatically take care of creating and initializing the various internal variables/weights they need to function.

tf.layers.elu (config?) function Source

Exponetial Linear Unit (ELU).

It follows: f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0.

Input shape: Arbitrary. Use the configuration inputShape when using this layer as the first layer in a model.

Output shape: Same shape as the input.

References:

Parameters:
  • config (Object) Optional
  • alpha (number) Float >= 0. Negative slope coefficient. Defaults to 1.0. Optional
Returns: tf.layers.Layer
tf.layers.leakyReLU (config?) function Source

Leaky version of a rectified linear unit.

It allows a small gradient when the unit is not active: f(x) = alpha * x for x < 0. f(x) = x for x >= 0.

Input shape: Arbitrary. Use the configuration inputShape when using this layer as the first layer in a model.

Output shape: Same shape as the input.

Parameters:
  • config (Object) Optional
  • alpha (number) Float >= 0. Negative slope coefficient. Defaults to 0.3. Optional
Returns: tf.layers.Layer
tf.layers.softmax (config?) function Source

Softmax activation layer.

Input shape: Arbitrary. Use the configuration inputShape when using this layer as the first layer in a model.

Output shape: Same shape as the input.

Parameters:
  • config (Object) Optional
  • axis (number) Integer, axis along which the softmax normalization is applied. Defaults to -1 (i.e., the last axis). Optional
Returns: tf.layers.Layer

Thresholded Rectified Linear Unit.

It follows: f(x) = x for x > theta, f(x) = 0 otherwise.

Input shape: Arbitrary. Use the configuration inputShape when using this layer as the first layer in a model.

Output shape: Same shape as the input.

References:

Parameters:
  • config (Object) Optional
  • theta (number) Float >= 0. Threshold location of activation. Optional
Returns: tf.layers.Layer
tf.layers.activation (config) function Source

Applies an activation function to an output.

Parameters:
  • config (Object)
  • activation ('elu'|'hardsigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|string) Name of the activation function to use.
Returns: tf.layers.Layer
tf.layers.dense (config) function Source

Creates a dense (fully connected) layer.

This layer implements the operation: output = activation(dot(input, kernel) + bias)

activation is the element-wise activation function passed as the activation argument.

kernel is a weights matrix created by the layer.

bias is a bias vector created by the layer (only applicable if useBias is true).

Input shape:

nD tf.Tensor with shape: (batchSize, ..., inputDim).

The most common situation would be a 2D input with shape (batchSize, inputDim).

Output shape:

nD tensor with shape: (batchSize, ..., units).

For instance, for a 2D input with shape (batchSize, inputDim), the output would have shape (batchSize, units).

Note: if the input to the layer has a rank greater than 2, then it is flattened prior to the initial dot product with the kernel.

Parameters:
  • config (Object)
  • units (number) Positive integer, dimensionality of the output space.
  • activation ('elu'|'hardsigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|string) Activation function to use.

    If unspecified, no activation is applied. Optional

  • useBias (boolean) Whether to apply a bias. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the dense kernel weights matrix. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • inputDim (number) If specified, defines inputShape as [inputDim]. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the kernel weights. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the dense kernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • activityRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the activation. Optional
Returns: tf.layers.Layer
tf.layers.dropout (config) function Source

Applies dropout to the input.

Dropout consists in randomly setting a fraction rate of input units to 0 at each update during training time, which helps prevent overfitting.

Parameters:
  • config (Object)
  • rate (number) Float between 0 and 1. Fraction of the input units to drop.
  • noiseShape (number[]) Integer array representing the shape of the binary dropout mask that will be multiplied with the input.

    For instance, if your inputs have shape (batchSize, timesteps, features) and you want the dropout mask to be the same for all timesteps, you can use noise_shape=(batch_size, 1, features). Optional

  • seed (number) An integer to use as random seed. Optional
Returns: tf.layers.Layer
tf.layers.embedding (config) function Source

Maps positive integers (indices) into dense vectors of fixed size. eg. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]

Input shape: 2D tensor with shape: [batchSize, sequenceLength].

Output shape: 3D tensor with shape: [batchSize, sequenceLength, outputDim].

Parameters:
  • config (Object)
  • inputDim (number) Integer > 0. Size of the vocabulary, i.e. maximum integer index + 1.
  • outputDim (number) Integer >= 0. Dimension of the dense embedding.
  • embeddingsInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the embeddings matrix. Optional
  • embeddingsRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the embeddings matrix. Optional
  • activityRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the activation. Optional
  • embeddingsConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the embeddings matrix. Optional
  • maskZero (boolean) Whether the input value 0 is a special "padding" value that should be masked out. This is useful when using recurrent layers which may take variable length input.

    If this is True then all subsequent layers in the model need to support masking or an exception will be raised. If maskZero is set to True, as a consequence, index 0 cannot be used in the vocabulary (inputDim should equal size of vocabulary + 1). Optional

  • inputLength (number|number[]) Length of input sequences, 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). Optional

Returns: tf.layers.Layer
tf.layers.flatten (config?) function Source

Flattens the input. Does not affect the batch size.

A Flatten layer flattens each batch in its inputs to 1D (making the output 2D).

For example:

const input = tf.input({shape: [4, 3]});
const flattenLayer = tf.layers.flatten();
// Inspect the inferred output shape of the flatten layer, which
// equals `[null, 12]`. The 2nd dimension is 4 * 3, i.e., the result of the
// flattening. (The 1st dimension is the undermined batch size.)
console.log(JSON.stringify(flattenLayer.apply(input).shape));
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer
tf.layers.repeatVector (config) function Source

Repeat the input n times.

Parameters:
  • config (Object)
  • n (number) The integer number of times to repeat the input.
Returns: tf.layers.Layer
tf.layers.conv1d (config) function Source

1D convolution layer (e.g., temporal convolution).

This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs.

If use_bias is True, a bias vector is created and added to the outputs.

If activation is not null, it is applied to the outputs as well.

When using this layer as the first layer in a model, provide an inputShape argument Array or null.

For example, inputShape would be:

  • [10, 128] for sequences of 10 vectors of 128-dimensional vectors
  • [null, 128] for variable-length sequences of 128-dimensional vectors.
Parameters:
  • config (Object)
  • kernelSize (number|number[]) The dimensions of the convolution window. If kernelSize is a number, the convolutional window will be square.
  • filters (number) The dimensionality of the output space (i.e. the number of filters in the convolution). Optional
  • strides (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. Optional

  • padding ('valid'|'same'|'casual') Padding mode. Optional
  • dataFormat ('channelsFirst'|'channelsLast') 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. Optional

  • dilationRate (number|[number, number]) The dilation rate to use for the dilated convolution in each dimension. Should be an integer or array of two integers.

    Currently, specifying any dilationRate value != 1 is incompatible with specifying any strides value != 1. Optional

  • activation (string) Activation function of the layer.

    If you don't specify the activation, none is applied. Optional

  • useBias (boolean) Whether the layer uses a bias vector. Defaults to false. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the convolutional kernel weights matrix. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the convolutional kernel weights. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the kernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • activityRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the activation. Optional
Returns: tf.layers.Layer
tf.layers.conv2d (config) function Source

2D convolution layer (e.g. spatial convolution over images).

This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs.

If useBias is True, a bias vector is created and added to the outputs.

If activation is not null, it is applied to the outputs as well.

When using this layer as the first layer in a model, provide the keyword argument inputShape (Array of integers, does not include the sample axis), e.g. inputShape=[128, 128, 3] for 128x128 RGB pictures in dataFormat='channelsLast'.

Parameters:
  • config (Object)
  • kernelSize (number|number[]) The dimensions of the convolution window. If kernelSize is a number, the convolutional window will be square.
  • filters (number) The dimensionality of the output space (i.e. the number of filters in the convolution). Optional
  • strides (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. Optional

  • padding ('valid'|'same'|'casual') Padding mode. Optional
  • dataFormat ('channelsFirst'|'channelsLast') 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. Optional

  • dilationRate (number|[number, number]) The dilation rate to use for the dilated convolution in each dimension. Should be an integer or array of two integers.

    Currently, specifying any dilationRate value != 1 is incompatible with specifying any strides value != 1. Optional

  • activation (string) Activation function of the layer.

    If you don't specify the activation, none is applied. Optional

  • useBias (boolean) Whether the layer uses a bias vector. Defaults to false. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the convolutional kernel weights matrix. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the convolutional kernel weights. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the kernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • activityRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the activation. Optional
Returns: tf.layers.Layer

Transposed convolutional layer (sometimes called Deconvolution).

The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.

When using this layer as the first layer in a model, provide the configuration inputShape (Array of integers, does not include the sample axis), e.g., inputShape: [128, 128, 3] for 128x128 RGB pictures in dataFormat: 'channelsLast'.

Input shape: 4D tensor with shape: [batch, channels, rows, cols] if dataFormat is 'channelsFirst'. or 4D tensor with shape [batch, rows, cols, channels] if dataFormat is 'channelsLast.

Output shape: 4D tensor with shape: [batch, filters, newRows, newCols] if dataFormat is 'channelsFirst'. or 4D tensor with shape: [batch, newRows, newCols, filters] if dataFormat is 'channelsLast'.

References:

Parameters:
  • config (Object)
  • kernelSize (number|number[]) The dimensions of the convolution window. If kernelSize is a number, the convolutional window will be square.
  • filters (number) The dimensionality of the output space (i.e. the number of filters in the convolution). Optional
  • strides (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. Optional

  • padding ('valid'|'same'|'casual') Padding mode. Optional
  • dataFormat ('channelsFirst'|'channelsLast') 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. Optional

  • dilationRate (number|[number, number]) The dilation rate to use for the dilated convolution in each dimension. Should be an integer or array of two integers.

    Currently, specifying any dilationRate value != 1 is incompatible with specifying any strides value != 1. Optional

  • activation (string) Activation function of the layer.

    If you don't specify the activation, none is applied. Optional

  • useBias (boolean) Whether the layer uses a bias vector. Defaults to false. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the convolutional kernel weights matrix. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the convolutional kernel weights. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the kernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • activityRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the activation. Optional
Returns: tf.layers.Layer

Depthwise separable 2D convolution.

Depthwise Separable convolutions consists in performing just the first step in a depthwise spatial convolution (which acts on each input channel separately). The depthMultplier argument controls how many output channels are generated per input channel in the depthwise step.

Parameters:
  • config (Object)
  • kernelSize (number|[number, number]) An integer or Array of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.
  • depthMultiplier (number) The number of depthwise convolution output channels for each input channel. The total number of depthwise convolution output channels will be equal to filtersIn * depthMultiplier. Default: 1. Optional
  • depthwiseInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the depthwise kernel matrix. Default: GlorotNormal. Optional
  • depthwiseConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the depthwise kernel matrix. Optional
  • depthwiseRegularizer ('l1l2'|string|Regularizer) Regulzarizer function for the depthwise kernel matrix. Optional
Returns: tf.layers.Layer

Depthwise separable 2D convolution.

Separable convolution consists of first performing a depthwise spatial convolution (which acts on each input channel separately) followed by a pointwise convolution which mixes together the resulting output channels. The depthMultiplier argument controls how many output channels are generated per input channel in the depthwise step.

Intuitively, separable convolutions can be understood as a way to factorize a convolution kernel into two smaller kernels, or as an extreme version of an Inception block.

Input shape: 4D tensor with shape: [batch, channels, rows, cols] if data_format='channelsFirst' or 4D tensor with shape: [batch, rows, cols, channels] if data_format='channelsLast'.

Output shape: 4D tensor with shape: [batch, filters, newRows, newCols] if data_format='channelsFirst' or 4D tensor with shape: [batch, newRows, newCols, filters] if data_format='channelsLast'. rows and cols values might have changed due to padding.

Parameters:
  • config (Object)
  • depthMultiplier (number) The number of depthwise convolution output channels for each input channel. The total number of depthwise convolution output channels will be equal to filtersIn * depthMultiplier. Default: 1. Optional
  • depthwiseInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the depthwise kernel matrix. Optional
  • pointwiseInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the pointwise kernel matrix. Optional
  • depthwiseRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the depthwise kernel matrix. Optional
  • pointwiseRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the pointwise kernel matrix. Optional
  • depthwiseConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the depthwise kernel matrix. Optional
  • pointwiseConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the pointwise kernel matrix. Optional
Returns: tf.layers.Layer
tf.layers.add (config?) function Source

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:

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.
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer
tf.layers.average (config?) function Source

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:

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.
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer
tf.layers.concatenate (config) function Source

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:

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).
Parameters:
  • config (Object)
  • axis (number) Axis along which to concatenate. Optional
Returns: tf.layers.Layer
tf.layers.maximum (config?) function Source

Layer that computes the element-wise maximum 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:

const input1 = tf.input({shape: [2, 2]});
const input2 = tf.input({shape: [2, 2]});
const maxLayer = tf.layers.maximum();
const max = maxLayer.apply([input1, input2]);
console.log(JSON.stringify(max.shape));
// You get [null, 2, 2], with the first dimension as the undetermined batch
// dimension.
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer
tf.layers.minimum (config?) function Source

Layer that computes the element-wise minimum of 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:

const input1 = tf.input({shape: [2, 2]});
const input2 = tf.input({shape: [2, 2]});
const minLayer = tf.layers.minimum();
const min = minLayer.apply([input1, input2]);
console.log(JSON.stringify(min.shape));
// You get [null, 2, 2], with the first dimension as the undetermined batch
// dimension.
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer
tf.layers.multiply (config?) function Source

Layer that multiplies (element-wise) an Array of inputs.

It takes as input an Array of tensors, all of the same shape, and returns a single tensor (also of the same shape). For example:

const input1 = tf.input({shape: [2, 2]});
const input2 = tf.input({shape: [2, 2]});
const input3 = tf.input({shape: [2, 2]});
const multiplyLayer = tf.layers.multiply();
const product = multiplyLayer.apply([input1, input2, input3]);
console.log(product.shape);
// You get [null, 2, 2], with the first dimension as the undetermined batch
// dimension.
Parameters:
  • config (Object) Optional
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer

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:

Parameters:
  • config (Object)
  • axis (number) 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 tf.batchNormalization(). Optional

  • momentum (number) Momentum of the moving average. Defaults to 0.99. Optional
  • epsilon (number) Small float added to the variance to avoid dividing by zero. Defaults to 1e-3. Optional
  • center (boolean) If true, add offset of beta to normalized tensor. If false, beta is ignored. Defaults to true. Optional
  • scale (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. Optional
  • betaInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the beta weight. Defaults to 'zeros'. Optional
  • gammaInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the gamma weight. Defaults to tf.ones(). Optional
  • movingMeanInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the moving mean. Defaults to tf.zeros() Optional
  • movingVarianceInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the moving variance. Defaults to 'Ones'. Optional
  • betaConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for the beta weight. Optional
  • gammaConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint for gamma weight. Optional
  • betaRegularizer ('l1l2'|string|Regularizer) Regularizer for the beta weight. Optional
  • gammaRegularizer ('l1l2'|string|Regularizer) Regularizer for the gamma weight. Optional
Returns: tf.layers.Layer

Average pooling operation for spatial data.

Input shape: [batchSize, inLength, channels]

Output shape: [batchSize, pooledLength, channels]

tf.avgPool1d is an alias.

Parameters:
  • config (Object)
  • poolSize (number) Size of the window to pool over, should be an integer. Optional
  • strides (number) Period at which to sample the pooled values.

    If null, defaults to poolSize. Optional

  • padding ('valid'|'same'|'casual') How to fill in data that's not an integer multiple of poolSize. Optional
Returns: tf.layers.Layer

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, pooleRows, pooledCols, channels]
  • If dataFormat === CHANNEL_FIRST: 4D tensor with shape: [batchSize, channels, pooleRows, pooledCols]

tf.avgPool2d is an alias.

Parameters:
  • config (Object)
  • poolSize (number|[number, number]) Factors by which to downscale in each dimension [vertical, horizontal]. Expects an integer or an array of 2 integers.

    For example, [2, 2] will halve the input in both spatial dimension. If only one integer is specified, the same window length will be used for both dimensions. Optional

  • strides ([number, number]) The size of the stride in each dimension of the pooling window. Expects an integer or an array of 2 integers. Integer, tuple of 2 integers, or None.

    If null, defaults to poolSize. Optional

  • padding ('valid'|'same'|'casual') The padding type to use for the pooling layer. Optional
  • dataFormat ('channelsFirst'|'channelsLast') The data format to use for the pooling layer. Optional
Returns: tf.layers.Layer

Global average pooling operation for temporal data.

Input Shape: 3D tensor with shape: [batchSize, steps, features].

Output Shape:2D tensor with shape: [batchSize, features].

Parameters:
  • config (Object)
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer

Global average pooling operation for spatial data.

Input shape:

  • If dataFormat is CHANNEL_LAST: 4D tensor with shape: [batchSize, rows, cols, channels].
  • If dataFormat is CHANNEL_FIRST: 4D tensor with shape: [batchSize, channels, rows, cols].

Output shape: 2D tensor with shape: [batchSize, channels].

Parameters:
  • config (Object)
  • dataFormat ('channelsFirst'|'channelsLast') One of CHANNEL_LAST (default) or CHANNEL_FIRST.

    The ordering of the dimensions in the inputs. CHANNEL_LAST corresponds to inputs with shape [batch, height, width, channels[ while CHANNEL_FIRST corresponds to inputs with shape [batch, channels, height, width]. Optional

Returns: tf.layers.Layer

Global max pooling operation for temporal data.

Input Shape: 3D tensor with shape: [batchSize, steps, features].

Output Shape:2D tensor with shape: [batchSize, features].

Parameters:
  • config (Object)
  • inputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchInputShape (number[]) If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model). Optional
  • batchSize (number) If inputShape is specified and batchInputShape is not specifiedd, batchSize is used to construct the batchInputShape: [batchSize, ...inputShape] Optional
  • dtype ('float32'|'int32'|'bool') The data-type for this layer. Defaults to 'float32'. This argument is only applicable to input layers (the first layer of a model). Optional
  • name (string) Name for this layer. Optional
  • trainable (boolean) Whether this layer is trainable. Defaults to true. Optional
  • updatable (boolean) Whether the weights of this layer are updatable by fit. Optional
  • weights (tf.Tensor[]) Initial weight values of the layer. Optional
  • inputDType ('float32'|'int32'|'bool') Legacy support. Do not use for new code. Optional
Returns: tf.layers.Layer

Global max pooling operation for spatial data.

Input shape:

  • If dataFormat is CHANNEL_LAST: 4D tensor with shape: [batchSize, rows, cols, channels].
  • If dataFormat is CHANNEL_FIRST: 4D tensor with shape: [batchSize, channels, rows, cols].

Output shape: 2D tensor with shape: [batchSize, channels].

Parameters:
  • config (Object)
  • dataFormat ('channelsFirst'|'channelsLast') One of CHANNEL_LAST (default) or CHANNEL_FIRST.

    The ordering of the dimensions in the inputs. CHANNEL_LAST corresponds to inputs with shape [batch, height, width, channels[ while CHANNEL_FIRST corresponds to inputs with shape [batch, channels, height, width]. Optional

Returns: tf.layers.Layer
tf.layers.maxPooling1d (config) function Source

Max pooling operation for temporal data.

Input shape: [batchSize, inLength, channels]

Output shape: [batchSize, pooledLength, channels]

Parameters:
  • config (Object)
  • poolSize (number) Size of the window to pool over, should be an integer. Optional
  • strides (number) Period at which to sample the pooled values.

    If null, defaults to poolSize. Optional

  • padding ('valid'|'same'|'casual') How to fill in data that's not an integer multiple of poolSize. Optional
Returns: tf.layers.Layer
tf.layers.maxPooling2d (config) function Source

Max 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, pooleRows, pooledCols, channels]
  • If dataFormat=CHANNEL_FIRST: 4D tensor with shape: [batchSize, channels, pooleRows, pooledCols]
Parameters:
  • config (Object)
  • poolSize (number|[number, number]) Factors by which to downscale in each dimension [vertical, horizontal]. Expects an integer or an array of 2 integers.

    For example, [2, 2] will halve the input in both spatial dimension. If only one integer is specified, the same window length will be used for both dimensions. Optional

  • strides ([number, number]) The size of the stride in each dimension of the pooling window. Expects an integer or an array of 2 integers. Integer, tuple of 2 integers, or None.

    If null, defaults to poolSize. Optional

  • padding ('valid'|'same'|'casual') The padding type to use for the pooling layer. Optional
  • dataFormat ('channelsFirst'|'channelsLast') The data format to use for the pooling layer. Optional
Returns: tf.layers.Layer
tf.layers.gru (config) function Source

Gated Recurrent Unit - Cho et al. 2014.

This is an RNN layer consisting of one GRUCell. However, unlike the underlying GRUCell, the apply method of SimpleRNN operates on a sequence of inputs. The shape of the input (not including the first, batch dimension) needs to be at least 2-D, with the first dimension being time steps. For example:

const rnn = tf.layers.gru({units: 8, returnSequences: true});

// Create an input with 10 time steps.
const input = tf.input({shape: [10, 20]});
const output = rnn.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the `GRUCell`'s number of units.
Parameters:
  • config (Object)
  • implementation (number) Implementation mode, either 1 or 2.

    Mode 1 will structure its operations as a larger number of smaller dot products and additions.

    Mode 2 will batch them into fewer, larger operations. These modes will have different performance profiles on different hardware and for different applications. Optional

Returns: tf.layers.Layer
tf.layers.gruCell (config) function Source

Cell class for GRU.

GRUCell is distinct from the RNN subclass GRU in that its apply method takes the input data of only a single time step and returns the cell's output at the time step, while GRU takes the input data over a number of time steps. For example:

const cell = tf.layers.gruCell({units: 2});
const input = tf.input({shape: [10]});
const output = cell.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10]: This is the cell's output at a single time step. The 1st
// dimension is the unknown batch size.

Instance(s) of GRUCell can be used to construct RNN layers. The most typical use of this workflow is to combine a number of cells into a stacked RNN cell (i.e., StackedRNNCell internally) and use it to create an RNN. For example:

const cells = [
   tf.layers.gruCell({units: 4}),
   tf.layers.gruCell({units: 8}),
];
const rnn = tf.layers.rnn({cell: cells, returnSequences: true});

// Create an input with 10 time steps and a length-20 vector at each step.
const input = tf.input({shape: [10, 20]});
const output = rnn.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the last `gruCell`'s number of units.

To create an RNN consisting of only one GRUCell, use the tf.layers.gru.

Parameters:
  • config (Object)
  • recurrentActivation (string) Activation function to use for the recurrent step.

    Defaults to hard sigmoid (hardSigomid).

    If null, no activation is applied. Optional

  • implementation (number) Implementation mode, either 1 or 2.

    Mode 1 will structure its operations as a larger number of smaller dot products and additions.

    Mode 2 will batch them into fewer, larger operations. These modes will have different performance profiles on different hardware and for different applications. Optional

Returns: tf.RNNCell
tf.layers.lstm (config) function Source

Long-Short Term Memory layer - Hochreiter 1997.

This is an RNN layer consisting of one LSTMCell. However, unlike the underlying LSTMCell, the apply method of LSTM operates on a sequence of inputs. The shape of the input (not including the first, batch dimension) needs to be at least 2-D, with the first dimension being time steps. For example:

const lstm = tf.layers.lstm({units: 8, returnSequences: true});

// Create an input with 10 time steps.
const input = tf.input({shape: [10, 20]});
const output = lstm.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the `LSTMCell`'s number of units.
Parameters:
  • config (Object)
  • unitForgetBias (boolean) If true, add 1 to the bias of the forget gate at initialization. Setting it to true will also force biasInitializer = 'zeros'. This is recommended in Jozefowicz et al.. Optional
  • implementation (1|2) Implementation mode, either 1 or 2. Mode 1 will structure its operations as a larger number of smaller dot products and additions, whereas mode 2 will batch them into fewer, larger operations. These modes will have different performance profiles on different hardware and for different applications. Optional
Returns: tf.layers.Layer
tf.layers.lstmCell (config) function Source

Cell class for LSTM.

LSTMCell is distinct from the RNN subclass LSTM in that its apply method takes the input data of only a single time step and returns the cell's output at the time step, while LSTM takes the input data over a number of time steps. For example:

const cell = tf.layers.lstmCell({units: 2});
const input = tf.input({shape: [10]});
const output = cell.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10]: This is the cell's output at a single time step. The 1st
// dimension is the unknown batch size.

Instance(s) of LSTMCell can be used to construct RNN layers. The most typical use of this workflow is to combine a number of cells into a stacked RNN cell (i.e., StackedRNNCell internally) and use it to create an RNN. For example:

const cells = [
   tf.layers.lstmCell({units: 4}),
   tf.layers.lstmCell({units: 8}),
];
const rnn = tf.layers.rnn({cell: cells, returnSequences: true});

// Create an input with 10 time steps and a length-20 vector at each step.
const input = tf.input({shape: [10, 20]});
const output = rnn.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the last `lstmCell`'s number of units.

To create an RNN consisting of only one LSTMCell, use the tf.layers.lstm.

Parameters:
  • config (Object)
  • recurrentActivation ('elu'|'hardsigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|string) Activation function to use for the recurrent step.

    Defaults to hard sigmoid (hardSigomid).

    If null, no activation is applied. Optional

  • unitForgetBias (boolean) If true, add 1 to the bias of the forget gate at initialization. Setting it to true will also force biasInitializer = 'zeros'. This is recommended in Jozefowicz et al.. Optional
  • implementation (1|2) Implementation mode, either 1 or 2.

    Mode 1 will structure its operations as a larger number of smaller dot products and additions.

    Mode 2 will batch them into fewer, larger operations. These modes will have different performance profiles on different hardware and for different applications. Optional

Returns: tf.RNNCell
tf.layers.rnn (config) function Source

Base class for recurrent layers.

Input shape: 3D tensor with shape [batchSize, timeSteps, inputDim].

Output shape:

  • if returnState, an Array of tensors (i.e., tf.Tensors). The first tensor is the output. The remaining tensors are the states at the last time step, each with shape [batchSize, units].
  • if returnSequences, the output will have shape [batchSize, timeSteps, units].
  • else, the output will have shape [batchSize, units].

Masking: This layer supports masking for input data with a variable number of timesteps. To introduce masks to your data, use an embedding layer with the mask_zero parameter set to True.

Notes on using statefulness in RNNs: 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 fit().

To reset the states of your model, call .reset_states() on either a specific layer, or on your entire model.

Note on specifying the initial state of RNNs You can specify the initial state of RNN layers symbolically by calling them with the option initialState. The value of initialState should be a tensor or list of tensors representing the initial state of the RNN layer.

You can specify the initial state of RNN layers numerically by calling resetStates with the keyword argument states. The value of states should be a numpy array or list of numpy arrays representing the initial state of the RNN layer.

Note on passing external constants to RNNs You can pass "external" constants to the cell using the constants keyword argument of RNN.call method. This requires that the cell.call method accepts the same keyword argument constants. Such constants can be used to conditon the cell transformation on additional static inputs (not changing over time), a.k.a an attention mechanism.

Parameters:
Returns: tf.layers.Layer
tf.layers.simpleRNN (config) function Source

Fully-connected RNN where the output is to be fed back to input.

This is an RNN layer consisting of one SimpleRNNCell. However, unlike the underlying SimpleRNNCell, the apply method of SimpleRNN operates on a sequence of inputs. The shape of the input (not including the first, batch dimension) needs to be at least 2-D, with the first dimension being time steps. For example:

const rnn = tf.layers.simpleRNN({units: 8, returnSequences: true});

// Create an input with 10 time steps.
const input = tf.input({shape: [10, 20]});
const output = rnn.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the `SimpleRNNCell`'s number of units.
Parameters:
  • config (Object)
  • units (number) Positive integer, dimensionality of the output space.
  • activation ('elu'|'hardsigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|string) Activation function to use.

    Defaults to hyperbolic tangent (tf.tanh())

    If you pass null, no activation will be applied. Optional

  • useBias (boolean) Whether the layer uses a bias vector. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the kernel weights matrix, used for the linear transformation of the inputs. Optional
  • recurrentInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the recurrentKernel weights matrix, used for linear transformation of the recurrent state. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the kernel weights matrix. Optional
  • recurrentRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the recurrentKernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the kernel weights matrix. Optional
  • recurrentConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the recurrentKernel weights matrix. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the bias vector. Optional
  • dropout (number) Number between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Optional
  • recurrentDropout (number) Number between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Optional
Returns: tf.layers.Layer
tf.layers.simpleRNNCell (config) function Source

Cell class for SimpleRNN.

SimpleRNNCell is distinct from the RNN subclass SimpleRNN in that its apply method takes the input data of only a single time step and returns the cell's output at the time step, while SimpleRNN takes the input data over a number of time steps. For example:

const cell = tf.layers.simpleRNNCell({units: 2});
const input = tf.input({shape: [10]});
const output = cell.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10]: This is the cell's output at a single time step. The 1st
// dimension is the unknown batch size.

Instance(s) of SimpleRNNCell can be used to construct RNN layers. The most typical use of this workflow is to combine a number of cells into a stacked RNN cell (i.e., StackedRNNCell internally) and use it to create an RNN. For example:

const cells = [
   tf.layers.simpleRNNCell({units: 4}),
   tf.layers.simpleRNNCell({units: 8}),
];
const rnn = tf.layers.rnn({cell: cells, returnSequences: true});

// Create an input with 10 time steps and a length-20 vector at each step.
const input = tf.input({shape: [10, 20]});
const output = rnn.apply(input);

console.log(JSON.stringify(output.shape));
// [null, 10, 8]: 1st dimension is unknown batch size; 2nd dimension is the
// same as the sequence length of [tf.input()](#input), due to `returnSequences`: `true`;
// 3rd dimension is the last `SimpleRNNCell`'s number of units.

To create an RNN consisting of only one SimpleRNNCell, use the tf.layers.simpleRNN.

Parameters:
  • config (Object)
  • units (number) units: Positive integer, dimensionality of the output space.
  • activation ('elu'|'hardsigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|string) Activation function to use. Default: hyperbolic tangent ('tanh'). If you pass null, 'linear' activation will be applied. Optional
  • useBias (boolean) Whether the layer uses a bias vector. Optional
  • kernelInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the kernel weights matrix, used for the linear transformation of the inputs. Optional
  • recurrentInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the recurrentKernel weights matrix, used for linear transformation of the recurrent state. Optional
  • biasInitializer ('constant'|'glorotNormal'|'glorotUniform'| 'heNormal'|'identity'|'leCunNormal'|'ones'|'orthogonal'|'randomNormal'| 'randomUniform'|'truncatedNormal'|'varianceScaling'|'zeros'|string|tf.initializers.Initializer) Initializer for the bias vector. Optional
  • kernelRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the kernel weights matrix. Optional
  • recurrentRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the recurrent_kernel weights matrix. Optional
  • biasRegularizer ('l1l2'|string|Regularizer) Regularizer function applied to the bias vector. Optional
  • kernelConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the kernel weights matrix. Optional
  • recurrentConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraint function applied to the recurrentKernel weights matrix. Optional
  • biasConstraint ('maxNorm'|'minMaxNorm'|'nonNeg'|'unitNorm'|string|tf.constraints.Constraint) Constraintfunction applied to the bias vector. Optional
  • dropout (number) Float number between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Optional
  • recurrentDropout (number) Float number between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Optional
Returns: tf.RNNCell

Base class for recurrent layers.

Input shape: 3D tensor with shape [batchSize, timeSteps, inputDim].

Output shape:

  • if returnState, an Array of tensors (i.e., tf.Tensors). The first tensor is the output. The remaining tensors are the states at the last time step, each with shape [batchSize, units].
  • if returnSequences, the output will have shape [batchSize, timeSteps, units].
  • else, the output will have shape [batchSize, units].

Masking: This layer supports masking for input data with a variable number of timesteps. To introduce masks to your data, use an embedding layer with the mask_zero parameter set to True.

Notes on using statefulness in RNNs: 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 fit().

To reset the states of your model, call .reset_states() on either a specific layer, or on your entire model.

Note on specifying the initial state of RNNs You can specify the initial state of RNN layers symbolically by calling them with the option initialState. The value of initialState should be a tensor or list of tensors representing the initial state of the RNN layer.

You can specify the initial state of RNN layers numerically by calling resetStates with the keyword argument states. The value of states should be a numpy array or list of numpy arrays representing the initial state of the RNN layer.

Note on passing external constants to RNNs You can pass "external" constants to the cell using the constants keyword argument of RNN.call method. This requires that the cell.call method accepts the same keyword argument constants. Such constants can be used to conditon the cell transformation on additional static inputs (not changing over time), a.k.a an attention mechanism.

Parameters:
Returns: tf.RNNCell
tf.layers.bidirectional (config) function Source
Parameters:
  • config (Object)
  • layer (RNN) The instance of an RNN layer to be wrapped.
  • mergeMode (BidirectionalMergeMode) Mode by which outputs of the forward and backward RNNs are combinied. If null or undefined, the output will not be combined, they will be returned as an Array. Optional
Returns: tf.layers.Layer

This wrapper applies a layer to every temporal slice of an input.

The input should be at least 3D, and the dimension of the index 1 will be considered to be the temporal dimension.

Consider a batch of 32 samples, where each sample is a sequence of 10 vectors of 16 dimensions. The batch input shape of the layer is then [32, 10, 16], and the inputShape, not including the sample dimension, is [10, 16].

You can then use TimeDistributed to apply a Dense layer to each of the 10 timesteps, independently:

const model = tf.sequential();
model.add(tf.layers.timeDistributed({
   layer: tf.layers.dense({units: 8}),
   inputShape: [10, 16],
}));

// Now model.outputShape = [null, 10, 8].
// The output will then have shape `[32, 10, 8]`.

// In subsequent layers, there is no need for `inputShape`:
model.add(tf.layers.timeDistributed({layer: tf.layers.dense({units: 32})}));
console.log(JSON.stringify(model.outputs[0].shape));
// Now model.outputShape = [null, 10, 32].

The output will then have shape [32, 10, 32].

TimeDistributed can be used with arbitrary layers, not just Dense, for instance a Conv2D layer.

const model = tf.sequential();
model.add(tf.layers.timeDistributed({
   layer: tf.layers.conv2d({filters: 64, kernelSize: [3, 3]}),
   inputShape: [10, 299, 299, 3],
}));
console.log(JSON.stringify(model.outputs[0].shape));
Parameters:
Returns: tf.layers.Layer

A layer is a grouping of operations and weights that can be composed to create a tf.Model.

Layers are constructed by using the functions under the tf.layers namespace.

apply (inputs, kwargs?) method Source

Builds or executes a `Layer's logic.

When called with tf.Tensor(s), execute the Layers computation and return Tensor(s). For example:

const denseLayer = tf.layers.dense({
   units: 1,
   kernelInitializer: 'zeros',
   useBias: false
});

// Invoke the layer's apply() method with a [tf.Tensor](#class:Tensor) (with concrete
// numeric values).
const input = tf.ones([2, 2]);
const output = denseLayer.apply(input);

// The output's value is expected to be [[0], [0]], due to the fact that
// the dense layer has a kernel initialized to all-zeros and does not have
// a bias.
output.print();

When called with tf.SymbolicTensor(s), this will prepare the layer for future execution. This entails internal book-keeping on shapes of expected Tensors, wiring layers together, and initializing weights.

Calling apply with tf.SymbolicTensors are typically used during the building of non-tf.Sequential models. For example:

const flattenLayer = tf.layers.flatten();
const denseLayer = tf.layers.dense({units: 1});

// Use tf.layers.input() to obtain a SymbolicTensor as input to apply().
const input = tf.input({shape: [2, 2]});
const output1 = flattenLayer.apply(input);

// output1.shape is [null, 4]. The first dimension is the undetermined
// batch size. The second dimension comes from flattening the [2, 2]
// shape.
console.log(JSON.stringify(output1.shape));

// The output SymbolicTensor of the flatten layer can be used to call
// the apply() of the dense layer:
const output2 = denseLayer.apply(output1);

// output2.shape is [null, 1]. The first dimension is the undetermined
// batch size. The second dimension matches the number of units of the
// dense layer.
console.log(JSON.stringify(output2.shape));

// The input and output and be used to construct a model that consists
// of the flatten and dense layers.
const model = tf.model({inputs: input, outputs: output2});
Parameters:
tf.RNNCell extends Layer class Source

An RNNCell layer.

tf.layers.inputLayer (config) function Source

An input layer is an entry point into a tf.Model.

InputLayer is generated automatically for tf.Sequential models by specifying the inputshape or batchInputShape for the first layer. It should not be specified explicitly.

// Define a model which simply adds two inputs.
const inputA = tf.input({shape: [3]});
const inputB = tf.input({shape: [3]});
const sum = tf.layers.add().apply([inputA, inputB]);
const model = tf.model({inputs: [inputA, inputB], outputs: sum});
const batchSize = 2;
model.predict([tf.ones([batchSize, 3]), tf.ones([batchSize, 3])]).print();
Parameters:
  • config (Object)
  • inputShape (number[]) Input shape, not including the batch axis. Optional
  • batchSize (number) Optional input batch size (integer or null). Optional
  • batchInputShape (number[]) Batch input shape, including the batch axis. Optional
  • dtype ('float32'|'int32'|'bool') Datatype of the input. Optional
  • sparse (boolean) Whether the placeholder created is meant to be sparse. Optional
  • name (string) Name of the layer. Optional
Returns: tf.layers.Layer
tf.layers.zeroPadding2d (config) function Source

Zero-padding layer for 2D input (e.g., image).

This layer can add rows and columns of zeros at the top, bottom, left and right side of an image tensor.

Input shape: 4D tensor with shape:

  • If dataFormat is "channelsLast": [batch, rows, cols, channels]
  • If data_format is "channels_first": [batch, channels, rows, cols].

Output shape: 4D with shape:

  • If dataFormat is "channelsLast": [batch, paddedRows, paddedCols, channels]
  • If dataFormat is "channelsFirst": [batch, channels, paddedRows, paddedCols].
Parameters:
  • config (Object)
  • padding (number|[number, number]|[[number, number], [number, number]]) Integer, or Array of 2 integers, or Array of 2 Arrays, each of which is an Array of 2 integers.

    • If integer, the same symmetric padding is applied to width and height.
    • If Arrayof 2 integers, interpreted as two different symmetric values for height and width:[symmetricHeightPad, symmetricWidthPad]`.
    • If Array of 2 Arrays, interpreted as: [[topPad, bottomPad], [leftPad, rightPad]].
    Optional
  • dataFormat ('channelsFirst'|'channelsLast') One of 'channelsLast' (default) and 'channelsFirst'.

    The ordering of the dimensions in the inputs. channelsLast corresponds to inputs with shape [batch, height, width, channels] while channelsFirst corresponds to inputs with shape [batch, channels, height, width]. Optional

Returns: tf.layers.Layer

To perform mathematical computation on Tensors, we use operations. Tensors are immutable, so all operations always return new Tensors and never modify input Tensors.

tf.add (a, b) function Source

Adds two tf.Tensors element-wise, A + B. Supports broadcasting.

We also expose addStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 2, 3, 4]);
const b = tf.tensor1d([10, 20, 30, 40]);

a.add(b).print();  // or tf.add(a, b)
// Broadcast add a with b.
const a = tf.scalar(5);
const b = tf.tensor1d([10, 20, 30, 40]);

a.add(b).print();  // or tf.add(a, b)
Parameters:
Returns: tf.Tensor
tf.sub (a, b) function Source

Subtracts two tf.Tensors element-wise, A - B. Supports broadcasting.

We also expose subStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.tensor1d([1, 2, 3, 4]);

a.sub(b).print();  // or tf.sub(a, b)
// Broadcast subtract a with b.
const a = tf.tensor1d([10, 20, 30, 40]);
const b = tf.scalar(5);

a.sub(b).print();  // or tf.sub(a, b)
Parameters:
Returns: tf.Tensor
tf.mul (a, b) function Source

Multiplies two tf.Tensors element-wise, A * B. Supports broadcasting.

We also expose mulStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 2, 3, 4]);
const b = tf.tensor1d([2, 3, 4, 5]);

a.mul(b).print();  // or tf.mul(a, b)
// Broadcast mul a with b.
const a = tf.tensor1d([1, 2, 3, 4]);
const b = tf.scalar(5);

a.mul(b).print();  // or tf.mul(a, b)
Parameters:
  • a (tf.Tensor) The first tensor to multiply.
  • b (tf.Tensor) The second tensor to multiply. Must have the same dtype as a.
Returns: tf.Tensor
tf.div (a, b) function Source

Divides two tf.Tensors element-wise, A / B. Supports broadcasting.

We also expose divStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 4, 9, 16]);
const b = tf.tensor1d([1, 2, 3, 4]);

a.div(b).print();  // or tf.div(a, b)
// Broadcast div a with b.
const a = tf.tensor1d([2, 4, 6, 8]);
const b = tf.scalar(2);

a.div(b).print();  // or tf.div(a, b)
Parameters:
  • a (tf.Tensor) The first tensor as the numerator.
  • b (tf.Tensor) The second tensor as the denominator. Must have the same dtype as a.
Returns: tf.Tensor
tf.maximum (a, b) function Source

Returns the max of a and b (a > b ? a : b) element-wise. Supports broadcasting.

We also expose maximumStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 4, 3, 16]);
const b = tf.tensor1d([1, 2, 9, 4]);

a.maximum(b).print();  // or tf.maximum(a, b)
// Broadcast maximum a with b.
const a = tf.tensor1d([2, 4, 6, 8]);
const b = tf.scalar(5);

a.maximum(b).print();  // or tf.maximum(a, b)
Parameters:
  • a (tf.Tensor) The first tensor.
  • b (tf.Tensor) The second tensor. Must have the same type as a.
Returns: tf.Tensor
tf.minimum (a, b) function Source

Returns the min of a and b (a < b ? a : b) element-wise. Supports broadcasting.

We also expose minimumStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 4, 3, 16]);
const b = tf.tensor1d([1, 2, 9, 4]);

a.minimum(b).print();  // or tf.minimum(a, b)
// Broadcast minimum a with b.
const a = tf.tensor1d([2, 4, 6, 8]);
const b = tf.scalar(5);

a.minimum(b).print();  // or tf.minimum(a, b)
Parameters:
  • a (tf.Tensor) The first tensor.
  • b (tf.Tensor) The second tensor. Must have the same type as a.
Returns: tf.Tensor
tf.mod (a, b) function Source

Returns the mod of a and b element-wise. floor(x / y) * y + mod(x, y) = x Supports broadcasting.

We also expose modStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 4, 3, 16]);
const b = tf.tensor1d([1, 2, 9, 4]);

a.mod(b).print();  // or tf.mod(a, b)
// Broadcast a mod b.
const a = tf.tensor1d([2, 4, 6, 8]);
const b = tf.scalar(5);

a.mod(b).print();  // or tf.mod(a, b)
Parameters:
  • a (tf.Tensor) The first tensor.
  • b (tf.Tensor) The second tensor. Must have the same type as a.
Returns: tf.Tensor
tf.pow (base, exp) function Source

Computes the power of one tf.Tensor to another. Supports broadcasting.

Given a tf.Tensor x and a tf.Tensor y, this operation computes x^y for corresponding elements in x and y.

const a = tf.tensor([[2, 3], [4, 5]])
const b = tf.tensor([[1, 2], [3, 0]]).toInt();

a.pow(b).print();  // or tf.pow(a, b)
const a = tf.tensor([[1, 2], [3, 4]])
const b = tf.tensor(2).toInt();

a.pow(b).print();  // or tf.pow(a, b)

We also expose powStrict which has the same signature as this op and asserts that base and tf.exp() are the same shape (does not broadcast).

Parameters:
Returns: tf.Tensor
tf.squaredDifference (a, b) function Source

Returns (a - b) * (a - b) element-wise. Supports broadcasting.

We also expose squaredDifferenceStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

const a = tf.tensor1d([1, 4, 3, 16]);
const b = tf.tensor1d([1, 2, 9, 4]);

a.squaredDifference(b).print();  // or tf.squaredDifference(a, b)
// Broadcast squared difference  a with b.
const a = tf.tensor1d([2, 4, 6, 8]);
const b = tf.scalar(5);

a.squaredDifference(b).print();  // or tf.squaredDifference(a, b)
Parameters:
  • a (tf.Tensor) The first tensor.
  • b (tf.Tensor) The second tensor. Must have the same type as a.
Returns: tf.Tensor
tf.abs (x) function Source

Computes absolute value element-wise: abs(x)

const x = tf.tensor1d([-1, 2, -3, 4]);

x.abs().print();  // or tf.abs(x)
Parameters:
Returns: tf.Tensor
tf.acos (x) function Source

Computes acos of the input tf.Tensor element-wise: acos(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.acos().print();  // or tf.acos(x)
Parameters:
Returns: tf.Tensor
tf.acosh (x) function Source

Computes the inverse hyperbolic cos of the input tf.Tensor element-wise: acosh(x)

const x = tf.tensor1d([10, 1, 3, 5.7]);

x.acosh().print();  // or tf.acosh(x)
Parameters:
Returns: tf.Tensor
tf.asin (x) function Source

Computes asin of the input tf.Tensor element-wise: asin(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.asin().print();  // or tf.asin(x)
Parameters:
Returns: tf.Tensor
tf.asinh (x) function Source

Computes inverse hyperbolic sin of the input tf.Tensor element-wise: asinh(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.asinh().print();  // or tf.asinh(x)
Parameters:
Returns: tf.Tensor
tf.atan (x) function Source

Computes atan of the input tf.Tensor element-wise: atan(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.atan().print();  // or tf.atan(x)
Parameters:
Returns: tf.Tensor
tf.atanh (x) function Source

Computes inverse hyperbolic tan of the input tf.Tensor element-wise: atanh(x)

const x = tf.tensor1d([0, .1, -.1, .7]);

x.atanh().print();  // or tf.atanh(x)
Parameters:
Returns: tf.Tensor
tf.ceil (x) function Source

Computes ceiling of input tf.Tensor element-wise: ceil(x)

const x = tf.tensor1d([.6, 1.1, -3.3]);

x.ceil().print();  // or tf.ceil(x)
Parameters:
Returns: tf.Tensor
tf.clipByValue (x, clipValueMin, clipValueMax) function Source

Clips values element-wise. max(min(x, clipValueMax), clipValueMin)

const x = tf.tensor1d([-1, 2, -3, 4]);

x.clipByValue(-2, 3).print();  // or tf.clipByValue(x, -2, 3)
Parameters:
  • x (tf.Tensor) The input tensor.
  • clipValueMin (number) Lower-bound of range to be clipped to.
  • clipValueMax (number) Upper-bound of range to be clipped to.
Returns: tf.Tensor
tf.cos (x) function Source

Computes cos of the input tf.Tensor element-wise: cos(x)

const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]);

x.cos().print();  // or tf.cos(x)
Parameters:
Returns: tf.Tensor
tf.cosh (x) function Source

Computes hyperbolic cos of the input tf.Tensor element-wise: cosh(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.cosh().print();  // or tf.cosh(x)
Parameters:
Returns: tf.Tensor
tf.elu (x) function Source

Computes exponential linear element-wise, x > 0 ? e ^ x - 1 : 0

const x = tf.tensor1d([-1, 1, -3, 2]);

x.elu().print();  // or tf.elu(x)
Parameters:
Returns: tf.Tensor
tf.exp (x) function Source

Computes exponential of the input tf.Tensor element-wise. e ^ x

const x = tf.tensor1d([1, 2, -3]);

x.exp().print();  // or tf.exp(x)
Parameters:
Returns: tf.Tensor
tf.expm1 (x) function Source

Computes exponential of the input tf.Tensor minus one element-wise. e ^ x - 1

const x = tf.tensor1d([1, 2, -3]);

x.expm1().print();  // or tf.expm1(x)
Parameters:
Returns: tf.Tensor
tf.floor (x) function Source

Computes floor of input tf.Tensor element-wise: floor(x).

const x = tf.tensor1d([.6, 1.1, -3.3]);

x.floor().print();  // or tf.floor(x)
Parameters:
Returns: tf.Tensor
tf.leakyRelu (x, alpha?) function Source

Computes leaky rectified linear element-wise.

See http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf

const x = tf.tensor1d([-1, 2, -3, 4]);

x.leakyRelu(0.1).print();  // or tf.leakyRelu(x, 0.1)
Parameters:
  • x (tf.Tensor) The input tensor.
  • alpha (number) The scaling factor for negative values, defaults to 0.2. Optional
Returns: tf.Tensor
tf.log (x) function Source

Computes natural logarithm of the input tf.Tensor element-wise: ln(x)

const x = tf.tensor1d([1, 2, Math.E]);

x.log().print();  // or tf.log(x)
Parameters:
Returns: tf.Tensor
tf.log1p (x) function Source

Computes natural logarithm of the input tf.Tensor plus one element-wise: ln(1 + x)

const x = tf.tensor1d([1, 2, Math.E - 1]);

x.log1p().print();  // or tf.log1p(x)
Parameters:
Returns: tf.Tensor
tf.neg (x) function Source

Computes -1 * x element-wise.

const x = tf.tensor2d([1, 2, -2, 0], [2, 2]);

x.neg().print();  // or tf.neg(x)
Parameters:
Returns: tf.Tensor
tf.prelu (x, alpha) function Source

Computes leaky rectified linear element-wise with parametric alphas.

x < 0 ? alpha * x : f(x) = x

const x = tf.tensor1d([-1, 2, -3, 4]);
const alpha = tf.scalar(0.1);

x.prelu(alpha).print();  // or tf.prelu(x, alpha)
Parameters:
Returns: tf.Tensor
tf.reciprocal (x) function Source

Computes reciprocal of x element-wise: 1 / x

const x = tf.tensor1d([0, 1, 2]);

x.reciprocal().print();  // or tf.reciprocal(x)
Parameters:
Returns: tf.Tensor
tf.relu (x) function Source

Computes rectified linear element-wise: max(x, 0)

const x = tf.tensor1d([-1, 2, -3, 4]);

x.relu().print();  // or tf.relu(x)
Parameters:
Returns: tf.Tensor
tf.round (x) function Source

Computes round of input tf.Tensor element-wise: round(x). It implements banker's rounding.

const x = dl.tensor1d([.6, 1.1, -3.3]);

x.round().print();  // or dl.round(x)
Parameters:
Returns: tf.Tensor
tf.rsqrt (x) function Source

Computes reciprocal of square root of the input tf.Tensor element-wise: y = 1 / sqrt(x)

const x = tf.tensor1d([1, 2, 4, -1]);

x.rsqrt().print();  // or tf.rsqrt(x)
Parameters:
Returns: tf.Tensor
tf.selu (x) function Source

Computes scaled exponential linear element-wise.

x < 0 ? scale * alpha * (exp(x) - 1) : x

const x = tf.tensor1d([-1, 2, -3, 4]);

x.selu().print();  // or tf.selu(x)
Parameters:
Returns: tf.Tensor
tf.sigmoid (x) function Source

Computes sigmoid element-wise, 1 / (1 + exp(-x))

const x = tf.tensor1d([0, -1, 2, -3]);

x.sigmoid().print();  // or tf.sigmoid(x)
Parameters:
Returns: tf.Tensor
tf.sign (x) function Source

Returns an element-wise indication of the sign of a number.

const x = tf.tensor1d([.6, 1.1, -3.3, NaN, 0]);

x.sign().print();  // or tf.sign(x)
Parameters:
Returns: tf.Tensor
tf.sin (x) function Source

Computes sin of the input Tensor element-wise: sin(x)

const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]);

x.sin().print();  // or tf.sin(x)
Parameters:
Returns: tf.Tensor
tf.sinh (x) function Source

Computes hyperbolic sin of the input tf.Tensor element-wise: sinh(x)

const x = tf.tensor1d([0, 1, -1, .7]);

x.sinh().print();  // or tf.sinh(x)
Parameters:
Returns: tf.Tensor
tf.sqrt (x) function Source

Computes square root of the input tf.Tensor element-wise: y = sqrt(x)

const x = tf.tensor1d([1, 2, 4, -1]);

x.sqrt().print();  // or tf.sqrt(x)
Parameters:
Returns: tf.Tensor
tf.square (x) function Source

Computes square of x element-wise: x ^ 2

const x = tf.tensor1d([1, 2, Math.sqrt(2), -1]);

x.square().print();  // or tf.square(x)
Parameters:
Returns: tf.Tensor
tf.step (x, alpha?) function Source

Computes step of the input tf.Tensor element-wise: x > 0 ? 1 : alpha * x

const x = tf.tensor1d([0, 2, -1, -3]);

x.step(.5).print();  // or tf.step(x, .5)
Parameters:
  • x (tf.Tensor) The input tensor.
  • alpha (number) The gradient when input is negative. Optional
Returns: tf.Tensor
tf.tan (x) function Source

Computes tan of the input tf.Tensor element-wise, tan(x)

const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]);

x.tan().print();  // or tf.tan(x)
Parameters:
Returns: tf.Tensor
tf.tanh (x) function Source

Computes hyperbolic tangent of the input tf.Tensor element-wise: tanh(x)

const x = tf.tensor1d([0, 1, -1, 70]);

x.tanh().print();  // or tf.tanh(x)
Parameters:
Returns: tf.Tensor
tf.matMul (a, b, transposeA?, transposeB?) function Source

Computes the dot product of two matrices, A * B. These must be matrices.

const a = tf.tensor2d([1, 2], [1, 2]);
const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);

a.matMul(b).print();  // or tf.matMul(a, b)
Parameters:
  • a (tf.Tensor2D) First matrix in dot product operation.
  • b (tf.Tensor2D) Second matrix in dot product operation.
  • transposeA (boolean) If true, a is transposed before multiplication. Optional
  • transposeB (boolean) If true, b is transposed before multiplication. Optional
Returns: tf.Tensor2D
tf.norm (x, ord?, axis?, keepDims?) function Source

Computes the norm of scalar, vectors, and matrices. This function can compute several different vector norms (the 1-norm, the Euclidean or 2-norm, the inf-norm, and in general the p-norm for p > 0) and matrix norms (Frobenius, 1-norm, and inf-norm).

const x = tf.tensor1d([1, 2, 3, 4]);

x.norm().print();  // or tf.norm(x)
Parameters:
  • x (tf.Tensor) The input array.
  • ord (number|'euclidean'|'fro') Optional. Order of the norm. Supported norm types are following:

    ord norm for matrices norm for vectors
    'euclidean' Frobenius norm 2-norm
    'fro' Frobenius norm
    Infinity max(sum(abs(x), axis=1)) max(abs(x))
    -Infinity min(sum(abs(x), axis=1)) min(abs(x))
    1 max(sum(abs(x), axis=0)) sum(abs(x))
    2 sum(abs(x)^2)^1/2*
    Optional
  • axis (number|number[]) Optional. If axis is null (the default), the input is considered a vector and a single vector norm is computed over the entire set of values in the Tensor, i.e. norm(x, ord) is equivalent to norm(x.reshape([-1]), ord). If axis is a integer, the input is considered a batch of vectors, and axis determines the axis in x over which to compute vector norms. If axis is a 2-tuple of integer it is considered a batch of matrices and axis determines the axes in NDArray over which to compute a matrix norm. Optional
  • keepDims (boolean) Optional. If true, the norm have the same dimensionality as the input. Optional
Returns: tf.Tensor
tf.outerProduct (v1, v2) function Source

Computes the outer product of two vectors, v1 and v2.

const a = tf.tensor1d([1, 2, 3]);
const b = tf.tensor1d([3, 4, 5]);

tf.outerProduct(a, b).print();
Parameters:
  • v1 (tf.Tensor1D) The first vector in the outer product operation.
  • v2 (tf.Tensor1D) The second vector in the dot product operation.
Returns: tf.Tensor2D
tf.transpose (x, perm?) function Source

Transposes the tf.Tensor. Permutes the dimensions according to perm.

The returned tf.Tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to [n-1...0], where n is the rank of the input tf.Tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input tf.Tensors.

const a = tf.tensor2d([1, 2, 3, 4, 5, 6], [2, 3]);

a.transpose().print();  // or tf.transpose(a)
Parameters:
  • x (tf.Tensor) The tensor to transpose.
  • perm (number[]) The permutation of the dimensions of a. Optional
Returns: tf.Tensor
tf.avgPool (x, filterSize, strides, pad, dimRoundingMode?) function Source

Computes the 2D average pooling of an image.

Parameters:
  • x (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3 of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • filterSize ([number, number]|number) The filter size, a tuple [filterHeight, filterWidth].
  • strides ([number, number]|number) The strides of the pooling: [strideHeight, strideWidth].
  • pad ('valid'|'same'|number) The type of padding algorithm:

  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.conv1d (input, filter, stride, pad, dataFormat?, dilation?, dimRoundingMode?) function Source

Computes a 1D convolution over the input x.

Parameters:
  • input (tf.Tensor2D|tf.Tensor3D) The input tensor, of rank 3 or rank 2, of shape [batch, width, inChannels]. If rank 2, batch of 1 is assumed.
  • filter (tf.Tensor3D) The filter, rank 3, of shape [filterWidth, inDepth, outDepth].
  • stride (number) The number of entries by which the filter is moved right at each step.
  • pad ('valid'|'same'|number) The type of padding algorithm.

  • dataFormat ('NWC'|'NCW') An optional string from "NWC", "NCW". Defaults to "NWC", the data is stored in the order of [batch, in_width, in_channels]. Only "NWC" is currently supported. Optional
  • dilation (number) The dilation rate in which we sample input values in atrous convolution. Defaults to 1. If it is greater than 1, then stride must be 1. Optional
  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.conv2d (x, filter, strides, pad, dataFormat?, dilations?, dimRoundingMode?) function Source

Computes a 2D convolution over the input x.

Parameters:
  • x (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3, of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • filter (tf.Tensor4D) The filter, rank 4, of shape [filterHeight, filterWidth, inDepth, outDepth].
  • strides ([number, number]|number) The strides of the convolution: [strideHeight, strideWidth].
  • pad ('valid'|'same'|number) The type of padding algorithm.

  • dataFormat ('NHWC'|'NCHW') : An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data. With the default format "NHWC", the data is stored in the order of: [batch, height, width, channels]. Only "NHWC" is currently supported. Optional
  • dilations ([number, number]|number) The dilation rates: [dilationHeight, dilationWidth] in which we sample input values across the height and width dimensions in atrous convolution. Defaults to [1, 1]. If dilations is a single number, then dilationHeight == dilationWidth. If it is greater than 1, then all values of strides must be 1. Optional
  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.conv2dTranspose (x, filter, outputShape, strides, pad, dimRoundingMode?) function Source

Computes the transposed 2D convolution of an image, also known as a deconvolution.

Parameters:
  • x (tf.Tensor3D|tf.Tensor4D) The input image, of rank 4 or rank 3, of shape [batch, height, width, inDepth]. If rank 3, batch of 1 is assumed.
  • filter (tf.Tensor4D) The filter, rank 4, of shape [filterHeight, filterWidth, outDepth, inDepth]. inDepth must match inDepth in x.
  • outputShape ([number, number, number, number]|[number, number, number]) Output shape, of rank 4 or rank 3: [batch, height, width, outDepth]. If rank 3, batch of 1 is assumed.
  • strides ([number, number]|number) The strides of the original convolution: [strideHeight, strideWidth].
  • pad ('valid'|'same'|number) The type of padding algorithm used in the non-transpose version of the op.
  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.depthwiseConv2d (input, filter, strides, pad, dataFormat?, dilations?, dimRoundingMode?) function Source

Depthwise 2D convolution.

Given a 4D tf.input() array and a filter array of shape [filterHeight, filterWidth, inChannels, channelMultiplier] containing inChannels convolutional filters of depth 1, this op applies a different filter to each input channel (expanding from 1 channel to channelMultiplier channels for each), then concatenates the results together. The output has inChannels * channelMultiplier channels.

See https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d for more details.

Parameters:
  • input (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3, of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • filter (tf.Tensor4D) The filter tensor, rank 4, of shape [filterHeight, filterWidth, inChannels, channelMultiplier].
  • strides ([number, number]|number) The strides of the convolution: [strideHeight, strideWidth]. If strides is a single number, then strideHeight == strideWidth.
  • pad ('valid'|'same'|number) The type of padding algorithm.

  • dataFormat ('NHWC'|'NCHW') : An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data. With the default format "NHWC", the data is stored in the order of: [batch, height, width, channels]. Only "NHWC" is currently supported. Optional
  • dilations ([number, number]|number) The dilation rates: [dilationHeight, dilationWidth] in which we sample input values across the height and width dimensions in atrous convolution. Defaults to [1, 1]. If rate is a single number, then dilationHeight == dilationWidth. If it is greater than 1, then all values of strides must be 1. Optional
  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.maxPool (x, filterSize, strides, pad, dimRoundingMode?) function Source

Computes the 2D max pooling of an image.

Parameters:
  • x (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3 of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • filterSize ([number, number]|number) The filter size, a tuple [filterHeight, filterWidth].
  • strides ([number, number]|number) The strides of the pooling: [strideHeight, strideWidth].
  • pad ('valid'|'same'|number) The type of padding algorithm.

  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.minPool (input, filterSize, strides, pad, dimRoundingMode?) function Source

Computes the 2D min pooling of an image.

Parameters:
  • input (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3 of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • filterSize ([number, number]|number) The filter size, a tuple [filterHeight, filterWidth].
  • strides ([number, number]|number) The strides of the pooling: [strideHeight, strideWidth].
  • pad ('valid'|'same'|number) The type of padding algorithm.

  • dimRoundingMode ('floor'|'round'|'ceil') The rounding mode used when computing output dimensions if pad is a number. If none is provided, it will not round and error if the output is of fractional size. Optional
tf.separableConv2d (input, depthwiseFilter, pointwiseFilter, strides, pad, dilation?, dataFormat?) function Source

2-D convolution with separable filters.

Performs a depthwise convolution that acts separately on channels followed by a pointwise convolution that mixes channels. Note that this is separability between dimensions [1, 2] and 3, not spatial separability between dimensions 1 and 2.

See https://www.tensorflow.org/api_docs/python/tf/nn/separable_conv2d for more details.

Parameters:
  • input (tf.Tensor3D|tf.Tensor4D) The input tensor, of rank 4 or rank 3, of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • depthwiseFilter (tf.Tensor4D) The depthwise filter tensor, rank 4, of shape [filterHeight, filterWidth, inChannels, channelMultiplier]. This is the filter used in the first step.
  • pointwiseFilter (tf.Tensor4D) The pointwise filter tensor, rank 4, of shape [1, 1, inChannels * channelMultiplier, outChannels]. This is the filter used in the second step.
  • strides ([number, number]|number) The strides of the convolution: [strideHeight, strideWidth]. If strides is a single number, then strideHeight == strideWidth.
  • pad ('valid'|'same') The type of padding algorithm.

  • dilation ([number, number]|number) Optional
  • dataFormat ('NHWC'|'NCHW') : An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data. With the default format "NHWC", the data is stored in the order of: [batch, height, width, channels]. Only "NHWC" is currently supported. Optional
tf.argMax (x, axis?) function Source

Returns the indices of the maximum values along an axis.

The result has the same shape as tf.input() with the dimension along axis removed.

const x = tf.tensor1d([1, 2, 3]);

x.argMax().print();  // or tf.argMax(x)
const x = tf.tensor2d([1, 2, 4, 3], [2, 2]);

const axis = 1;
x.argMax(axis).print();  // or tf.argMax(x, axis)
Parameters:
  • x (tf.Tensor) The input tensor.
  • axis (number) The dimension to reduce. By default it reduces across all axes and returns the flat index Optional
Returns: tf.Tensor
tf.argMin (x, axis?) function Source

Returns the indices of the minimum values along an axis.

The result has the same shape as tf.input() with the dimension along axis removed.

const x = tf.tensor1d([1, 2, 3]);

x.argMin().print();  // or tf.argMin(x)
const x = tf.tensor2d([1, 2, 4, 3], [2, 2]);

const axis = 1;
x.argMin(axis).print();  // or tf.argMin(x, axis)
Parameters:
  • x (tf.Tensor) The input tensor.
  • axis (number) The dimension to reduce. By default it reduces across all axes and returns the flat index. Optional
Returns: tf.Tensor
tf.logSumExp (input, axis?, keepDims?) function Source

Computes the log(sum(exp(elements across the reduction dimensions)).

Reduces the input along the dimensions given in axis. Unless keepDims is true, the rank of the array is reduced by 1 for each entry in axis. If keepDims is true, the reduced dimensions are retained with length 1. If axis has no entries, all dimensions are reduced, and an array with a single element is returned.

const x = tf.tensor1d([1, 2, 3]);

x.logSumExp().print();  // or tf.logSumExp(x)
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.logSumExp(axis).print();  // or tf.logSumExp(a, axis)
Parameters:
  • input (tf.Tensor) The input tensor.
  • axis (number|number[]) The dimension(s) to reduce. If null (the default), reduces all dimensions. Optional
  • keepDims (boolean) If true, retains reduced dimensions with length of 1. Defaults to false. Optional
Returns: tf.Tensor
tf.max (x, axis?, keepDims?) function Source

Computes the maximum of elements across dimensions of a tf.Tensor.

Reduces the input along the dimensions given in axes. Unless keepDims is true, the rank of the tf.Tensor is reduced by 1 for each entry in axes. If keepDims is true, the reduced dimensions are retained with length 1. If axes has no entries, all dimensions are reduced, and an tf.Tensor with a single element is returned.

const x = tf.tensor1d([1, 2, 3]);

x.max().print();  // or tf.max(x)
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.max(axis).print();  // or tf.max(x, axis)
Parameters:
  • x (tf.Tensor) The input tensor.
  • axis (number|number[]) The dimension(s) to reduce. By default it reduces all dimensions. Optional
  • keepDims (boolean) If true, retains reduced dimensions with size 1. Optional
Returns: tf.Tensor
tf.mean (x, axis?, keepDims?) function Source

Computes the mean of elements across dimensions of a tf.Tensor.

Reduces x along the dimensions given in axis. Unless keepDims is true, the rank of the tf.Tensor is reduced by 1 for each entry in axis. If keepDims is true, the reduced dimensions are retained with length 1. If axis has no entries, all dimensions are reduced, and a tf.Tensor with a single element is returned.

const x = tf.tensor1d([1, 2, 3]);

x.mean().print();  // or tf.logSumExp(a)
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.mean(axis).print();  // or tf.mean(x, axis)
Parameters:
  • x (tf.Tensor) The input tensor.
  • axis (number|number[]) The dimension(s) to reduce. By default it reduces all dimensions. Optional
  • keepDims (boolean) If true, retains reduced dimensions with size 1. Optional
Returns: tf.Tensor
tf.min (x, axis?, keepDims?) function Source

Computes the minimum value from the input.

Reduces the input along the dimensions given in axes. Unless keepDims is true, the rank of the array is reduced by 1 for each entry in axes. If keepDims is true, the reduced dimensions are retained with length 1. If axes has no entries, all dimensions are reduced, and an array with a single element is returned.

const x = tf.tensor1d([1, 2, 3]);

x.min().print();  // or tf.min(x)
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.min(axis).print();  // or tf.min(x, axis)
Parameters:
  • x (tf.Tensor) The input Tensor.
  • axis (number|number[]) The dimension(s) to reduce. By default it reduces all dimensions. Optional
  • keepDims (boolean) If true, retains reduced dimensions with size 1. Optional
Returns: tf.Tensor
tf.sum (x, axis?, keepDims?) function Source

Computes the sum of elements across dimensions of a tf.Tensor.

Reduces the input along the dimensions given in axes. Unless keepDims is true, the rank of the tf.Tensor is reduced by 1 for each entry in axes. If keepDims is true, the reduced dimensions are retained with length 1. If axes has no entries, all dimensions are reduced, and a tf.Tensor with a single element is returned.

const x = tf.tensor1d([1, 2, 3]);

x.sum().print();  // or tf.logSumExp(x)
const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);

const axis = 1;
x.sum(axis).print();  // or tf.sum(x, axis)
Parameters:
  • x (tf.Tensor) The input tensor to compute the sum over.
  • axis (number|number[]) The dimension(s) to reduce. By default it reduces all dimensions. Optional
  • keepDims (boolean) If true, retains reduced dimensions with size 1. Optional
Returns: tf.Tensor
tf.batchNormalization (x, mean, variance, varianceEpsilon?, scale?, offset?) function Source

Batch normalization.

As described in http://arxiv.org/abs/1502.03167.

Mean, variance, scale, and offset can be of two shapes:

  • The same shape as the input.
  • In the common case, the depth dimension is the last dimension of x, so the values would be an tf.Tensor1D of shape [depth].
Parameters:
Returns: tf.Tensor
tf.localResponseNormalization (x, radius?, bias?, alpha?, beta?, normRegion?) function Source

Normalizes the activation of a local neighborhood across or within channels.

Parameters:
  • x (tf.Tensor3D|tf.Tensor4D) The input tensor. The 4-D input tensor is treated as a 3-D array of 1D vectors (along the last dimension), and each vector is normalized independently.
  • radius (number) The number of adjacent channels or spatial locations of the 1D normalization window. In Tensorflow this param is called 'depth_radius' because only 'acrossChannels' mode is supported. Optional
  • bias (number) A constant bias term for the basis. Optional
  • alpha (number) A scale factor, usually positive. Optional
  • beta (number) An exponent. Optional
  • normRegion ('acrossChannels'|'withinChannel') Default is 'acrossChannels'. Optional
tf.moments (x, axis?, keepDims?) function Source

Calculates the mean and variance of x. The mean and variance are calculated by aggregating the contents of x across axes. If x is 1-D and axes = [0] this is just the mean and variance of a vector.

Parameters:
  • x (tf.Tensor) The input tensor.
  • axis (number|number[]) The dimension(s) along with to compute mean and variance. By default it reduces all dimensions. Optional
  • keepDims (boolean) If true, the moments have the same dimensionality as the input. Optional
Returns: {mean: tf.Tensor, variance: tf.Tensor}
tf.softmax (logits, dim?) function Source

Computes the softmax normalized vector given the logits.

const a = tf.tensor1d([1, 2, 3]);

a.softmax().print();  // or tf.softmax(a)
const a = tf.tensor2d([2, 4, 6, 1, 2, 3], [2, 3]);

a.softmax().print();  // or tf.softmax(a)
Parameters:
  • logits (tf.Tensor) The logits array.
  • dim (number) The dimension softmax would be performed on. Defaults to -1 which indicates the last dimension. Optional
Returns: tf.Tensor
tf.image.resizeBilinear (images, size, alignCorners?) function Source

Bilinear resize a batch of 3D images to a new shape.

Parameters:
  • images (tf.Tensor3D|tf.Tensor4D) The images, of rank 4 or rank 3, of shape [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed.
  • size ([number, number]) The new shape [newHeight, newWidth] to resize the images to. Each channel is resized individually.
  • alignCorners (boolean) Defaults to False. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension. Optional
tf.basicLSTMCell (forgetBias, lstmKernel, lstmBias, data, c, h) function Source

Computes the next state and output of a BasicLSTMCell.

Returns [newC, newH].

Derived from tf.contrib.rnn.BasicLSTMCell.

Parameters:
Returns: [tf.Tensor2D, tf.Tensor2D]
tf.multiRNNCell (lstmCells, data, c, h) function Source

Computes the next states and outputs of a stack of LSTMCells.

Each cell output is used as input to the next cell.

Returns [cellState, cellOutput].

Derived from tf.contrib.rn.MultiRNNCell.

Parameters:
Returns: [tf.Tensor2D[], tf.Tensor2D[]]
tf.equal (a, b) function Source

Returns the truth value of (a == b) element-wise. Supports broadcasting.

We also expose equalStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.greater (a, b) function Source

Returns the truth value of (a > b) element-wise. Supports broadcasting.

We also expose greaterStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.greaterEqual (a, b) function Source

Returns the truth value of (a >= b) element-wise. Supports broadcasting.

We also expose greaterEqualStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.less (a, b) function Source

Returns the truth value of (a < b) element-wise. Supports broadcasting.

We also expose lessStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.lessEqual (a, b) function Source

Returns the truth value of (a <= b) element-wise. Supports broadcasting.

We also expose lessEqualStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.logicalAnd (a, b) function Source

Returns the truth value of a AND b element-wise. Supports broadcasting.

Parameters:
  • a (tf.Tensor) The first input tensor. Must be of dtype bool.
  • b (tf.Tensor) The second input tensor. Must be of dtype bool.
Returns: tf.Tensor
tf.logicalNot (x) function Source

Returns the truth value of NOT x element-wise.

Parameters:
  • x (tf.Tensor) The input tensor. Must be of dtype 'bool'.
Returns: tf.Tensor
tf.logicalOr (a, b) function Source

Returns the truth value of a OR b element-wise. Supports broadcasting.

Parameters:
  • a (tf.Tensor) The first input tensor. Must be of dtype bool.
  • b (tf.Tensor) The second input tensor. Must be of dtype bool.
Returns: tf.Tensor
tf.logicalXor (a, b) function Source

Returns the truth value of a XOR b element-wise. Supports broadcasting.

Parameters:
  • a (tf.Tensor) The first input tensor. Must be of dtype bool.
  • b (tf.Tensor) The second input tensor. Must be of dtype bool.
Returns: tf.Tensor
tf.notEqual (a, b) function Source

Returns the truth value of (a != b) element-wise. Supports broadcasting.

We also expose notEqualStrict which has the same signature as this op and asserts that a and b are the same shape (does not broadcast).

Parameters:
  • a (tf.Tensor) The first input tensor.
  • b (tf.Tensor) The second input tensor. Must have the same dtype as a.
Returns: tf.Tensor
tf.where (condition, a, b) function Source

Returns the elements, either a or b depending on the condition.

If the condition is true, select from a, otherwise select from b.

Parameters:
  • condition (tf.Tensor) The input condition. Must be of dtype bool.
  • a (tf.Tensor) If condition is rank 1, a may have a higher rank but its first dimension must match the size of condition.
  • b (tf.Tensor) A tensor with the same shape and type as a.
Returns: tf.Tensor

We also provide an API to do perform training, and compute gradients. We compute gradients eagerly, users provide a function that is a combination of operations and we automatically differentiate that function's output with respect to its inputs.

For those familiar with TensorFlow, the API we expose exactly mirrors the TensorFlow Eager API.

tf.grad (f) function Source

Provided f(x), returns another function g(x, dy?), which gives the gradient of f(x) with respect to x.

If dy is provided, the gradient of f(x).mul(dy).sum() with respect to x is computed instead. f(x) must take a single tensor x and return a single tensor y. If f() takes multiple inputs, use tf.grads() instead.

// f(x) = x ^ 2
const f = x => x.square();
// f'(x) = 2x
const g = tf.grad(f);

const x = tf.tensor1d([2, 3]);
g(x).print();
// f(x) = x ^ 3
const f = x => x.pow(tf.scalar(3, 'int32'));
// f'(x) = 3x ^ 2
const g = tf.grad(f);
// f''(x) = 6x
const gg = tf.grad(g);

const x = tf.tensor1d([2, 3]);
gg(x).print();
Parameters:
Returns: (x: tf.Tensor, dy?: tf.Tensor) => tf.Tensor
tf.grads (f) function Source

Provided f(x1, x2,...), returns another function g([x1, x2,...], dy?), which gives an array of gradients of f() with respect to each input [x1,x2,...].

If dy is passed when calling g(), the gradient of f(x1,...).mul(dy).sum() with respect to each input is computed instead. The provided f must take one or more tensors and return a single tensor y. If f() takes a single input, we recommend using tf.grad() instead.

// f(a, b) = a * b
const f = (a, b) => a.mul(b);
// df / da = b, df / db = a
const g = tf.grads(f);

const a = tf.tensor1d([2, 3]);
const b = tf.tensor1d([-2, -3]);
const [da, db] = g([a, b]);
console.log('da');
da.print();
console.log('db');
db.print();
Parameters:
  • f ((...args: tf.Tensor[]) => tf.Tensor) The function f(x1, x2,...) to compute gradients for.
Returns: (args: tf.Tensor[], dy?: tf.Tensor) => tf.Tensor[]
tf.customGrad (f) function Source

Overrides the gradient computation of a function f.

Takes a function f(...inputs) => {value: Tensor, gradFunc: dy => Tensor[]} and returns another function g(...inputs) which takes the same inputs as f. When called, g returns f().value. In backward mode, custom gradients with respect to each input of f are computed using f().gradFunc.

const customOp = tf.customGrad(x => {
   // Override gradient of our custom x ^ 2 op to be dy * abs(x);
   return {value: x.square(), gradFunc: dy => [dy.mul(x.abs())]};
});

const x = tf.tensor1d([-1, -2, 3]);
const dx = tf.grad(x => customOp(x));

console.log(`f(x):`);
customOp(x).print();
console.log(`f'(x):`);
dx(x).print();
Parameters:
  • f ((a: tf.Tensor, b: tf.Tensor,...) => { value: tf.Tensor, gradFunc: (dy: tf.Tensor) => tf.Tensor|tf.Tensor[] }) The function to evaluate in forward mode, which should return {value: Tensor, gradFunc: (dy) => Tensor[]}, where gradFunc returns the custom gradients of f with respect to its inputs.
Returns: (...args: tf.Tensor[]) => tf.Tensor
tf.valueAndGrad (f) function Source

Like tf.grad(), but also returns the value of f(). Useful when f() returns a metric you want to show.

The result is a rich object with the following properties:

  • grad: The gradient of f(x) w.r.t x (result of tf.grad()).
  • value: The value returned by f(x).
// f(x) = x ^ 2
const f = x => x.square();
// f'(x) = 2x
const g = tf.valueAndGrad(f);

const x = tf.tensor1d([2, 3]);
const {value, grad} = g(x);

console.log('value');
value.print();
console.log('grad');
grad.print();
Parameters:
Returns: (x: tf.Tensor, dy?: tf.Tensor) => { value: tf.Tensor; grad: tf.Tensor; }

Like tf.grads(), but returns also the value of f(). Useful when f() returns a metric you want to show.

The result is a rich object with the following properties:

  • grads: The gradients of f() w.r.t each input (result of tf.grads()).
  • value: The value returned by f(x).
// f(a, b) = a * b
const f = (a, b) => a.mul(b);
// df/da = b, df/db = a
const g = tf.valueAndGrads(f);

const a = tf.tensor1d([2, 3]);
const b = tf.tensor1d([-2, -3]);
const {value, grads} = g([a, b]);

const [da, db] = grads;

console.log('value');
value.print();

console.log('da');
da.print();
console.log('db');
db.print();
Parameters:
Returns: (args: tf.Tensor[], dy?: tf.Tensor) => { grads: tf.Tensor[]; value: tf.Tensor; }
tf.variableGrads (f, varList?) function Source

Computes and returns the gradient of f(x) with respect to the list of trainable variables provided by varList. If no list is provided, it defaults to all trainable variables.

const a = tf.variable(tf.tensor1d([3, 4]));
const b = tf.variable(tf.tensor1d([5, 6]));
const x = tf.tensor1d([1, 2]);

// f(a, b) = a * x ^ 2 + b * x
const f = () => a.mul(x.square()).add(b.mul(x)).sum();
// df/da = x ^ 2, df/db = x
const {value, grads} = tf.variableGrads(f);

Object.keys(grads).forEach(varName => grads[varName].print());
Parameters:
  • f (() => tf.Scalar) The function to execute. f() should return a scalar.
  • varList (tf.Variable[]) The list of trainable variables. Defaults to all variables. Optional
Returns: {value: tf.Scalar, grads: {[name: string]: tf.Tensor}}
tf.train.sgd (learningRate) function Source

Constructs a tf.SGDOptimizer that uses stochastic gradient descent.

// Fit a quadratic function by learning the coefficients a, b, c.
const xs = tf.tensor1d([0, 1, 2, 3]);
const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]);

const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
const c = tf.scalar(Math.random()).variable();

// y = a * x^2 + b * x + c.
const f = x => a.mul(x.square()).add(b.mul(x)).add(c);
const loss = (pred, label) => pred.sub(label).square().mean();

const learningRate = 0.01;
const optimizer = tf.train.sgd(learningRate);

// Train the model.
for (let i = 0; i < 10; i++) {
   optimizer.minimize(() => loss(f(xs), ys));
}

// Make predictions.
console.log(
     `a: ${a.dataSync()}, b: ${b.dataSync()}, c: ${c.dataSync()}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
   console.log(`x: ${i}, pred: ${pred}`);
});
Parameters:
  • learningRate (number) The learning rate to use for the SGD algorithm.
Returns: tf.SGDOptimizer
tf.train.momentum (learningRate, momentum, useNesterov?) function Source

Constructs a tf.MomentumOptimizer that uses momentum gradient descent.

See http://proceedings.mlr.press/v28/sutskever13.pdf

Parameters:
  • learningRate (number) The learning rate to use for the Momentum gradient descent algorithm.
  • momentum (number) The momentum to use for the momentum gradient descent algorithm.
  • useNesterov (boolean) Optional
tf.train.adagrad (learningRate, initialAccumulatorValue?) function Source
Parameters:
  • learningRate (number) The learning rate to use for the Adagrad gradient descent algorithm.
  • initialAccumulatorValue (number) Starting value for the accumulators, must be positive. Optional
tf.train.adadelta (learningRate?, rho?, epsilon?) function Source

Constructs a tf.AdadeltaOptimizer that uses the Adadelta algorithm. See https://arxiv.org/abs/1212.5701

Parameters:
  • learningRate (number) The learning rate to use for the Adadelta gradient descent algorithm. Optional
  • rho (number) The learning rate decay over each update. Optional
  • epsilon (number) A constant epsilon used to better condition the grad update. Optional
tf.train.adam (learningRate?, beta1?, beta2?, epsilon?) function Source

Constructs a AdamOptimizer that uses the Adam algorithm. See https://arxiv.org/abs/1412.6980

Parameters:
  • learningRate (number) The learning rate to use for the Adam gradient descent algorithm. Optional
  • beta1 (number) The exponential decay rate for the 1st moment estimates. Optional
  • beta2 (number) The exponential decay rate for the 2nd moment estimates. Optional
  • epsilon (number) A small constant for numerical stability. Optional
Returns: AdamOptimizer
tf.train.adamax (learningRate?, beta1?, beta2?, epsilon?, decay?) function Source

Constructs a AdamaxOptimizer that uses the Adamax algorithm. See https://arxiv.org/abs/1412.6980

Parameters:
  • learningRate (number) The learning rate to use for the Adamax gradient descent algorithm. Optional
  • beta1 (number) The exponential decay rate for the 1st moment estimates. Optional
  • beta2 (number) The exponential decay rate for the 2nd moment estimates. Optional
  • epsilon (number) A small constant for numerical stability. Optional
  • decay (number) The learning rate decay over each update. Optional
Returns: AdamaxOptimizer
tf.train.rmsprop (learningRate, decay?, momentum?, epsilon?, centered?) function Source

Constructs a tf.RMSPropOptimizer that uses RMSProp gradient descent. This implementation uses plain momentum and is not centered version of RMSProp.

See http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf

Parameters:
  • learningRate (number) The learning rate to use for the RMSProp gradient descent algorithm.
  • decay (number) The discounting factor for the history/coming gradient. Optional
  • momentum (number) The momentum to use for the RMSProp gradient descent algorithm. Optional
  • epsilon (number) Small value to avoid zero denominator. Optional
  • centered (boolean) If true, gradients are normalized by the estimated variance of the gradient. Optional
tf.losses.absoluteDifference (labels, predictions, weights?, reduction?) function Source

Computes the absolute difference loss between two tensors.

Parameters:
  • labels (tf.Tensor) The ground truth output tensor, same dimensions as 'predictions'.
  • predictions (tf.Tensor) The predicted outputs.
  • weights (tf.Tensor) Tensor whose rank is either 0, or the same rank as labels, and must be broadcastable to labels (i.e., all dimensions must be either 1, or the same as the corresponding losses dimension). Optional
  • reduction (Reduction) Type of reduction to apply to loss. Should be of type Reduction Optional
Returns: tf.Tensor
tf.losses.computeWeightedLoss (losses, weights?, reduction?) function Source

Computes the weighted loss between two tensors.

Parameters:
  • losses (tf.Tensor) Tensor of shape [batch_size, d1, ... dN].
  • weights (tf.Tensor) Tensor whose rank is either 0, or the same rank as losses, and must be broadcastable to losses (i.e., all dimensions must be either 1, or the same as the corresponding losses dimension). Optional
  • reduction (Reduction) Optional
Returns: tf.Tensor
tf.losses.softmaxCrossEntropy (labels, logits, dim?) function Source

Computes softmax cross entropy between logits and labels.

Measures the probability error in discrete classification tasks in which the classes are mutually exclusive (each entry is in exactly one class). For example, each CIFAR-10 image is labeled with one and only one label: an image can be a dog or a truck, but not both.

NOTE: While the classes are mutually exclusive, their probabilities need not be. All that is required is that each row of labels is a valid probability distribution. If they are not, the computation of the gradient will be incorrect.

WARNING: This op expects unscaled logits, since it performs a softmax on logits internally for efficiency. Do not call this op with the output of softmax, as it will produce incorrect results.

logits and labels must have the same shape, e.g. [batch_size, num_classes] and the same dtype.

Parameters:
  • labels (tf.Tensor) The labels array.
  • logits (tf.Tensor) The logits array.
  • dim (number) The dimension softmax would be performed on. Defaults to -1 which indicates the last dimension. Optional
Returns: tf.Tensor
minimize (f, returnCost?, varList?) method Source

Executes f() and minimizes the scalar output of f() by computing gradients of y with respect to the list of trainable variables provided by varList. If no list is provided, it defaults to all trainable variables.

Parameters:
  • f (() => tf.Scalar) The function to execute and whose output to minimize.
  • returnCost (boolean) Whether to return the scalar cost value produced by executing f(). Optional
  • varList (tf.Variable[]) An optional list of variables to update. If specified, only the trainable variables in varList will be updated by minimize. Defaults to all trainable variables. Optional
Returns: tf.Scalar |null
tf.tidy (nameOrFn, fn?, gradMode?) function Source

Executes the provided function f and after it is executed, cleans up all intermediate tensors allocated by f except those returned by f. f must not return a Promise (async functions not allowed). The returned result can be a complex object, however tidy only walks the top-level properties (depth 1) of that object to search for tensors, or lists of tensors that need to be tracked in the parent scope.

Using this method helps avoid memory leaks. In general, wrap calls to operations in tf.tidy() for automatic memory cleanup.

When in safe mode, you must enclose all tf.Tensor creation and ops inside a tf.tidy() to prevent memory leaks.

// y = 2 ^ 2 + 1
const y = tf.tidy(() => {
   // a, b, and one will be cleaned up when the tidy ends.
   const one = tf.scalar(1);
   const a = tf.scalar(2);
   const b = a.square();

   console.log('numTensors (in tidy): ' + tf.memory().numTensors);

   // The value returned inside the tidy function will return
   // through the tidy, in this case to the variable y.
   return b.add(one);
});

console.log('numTensors (outside tidy): ' + tf.memory().numTensors);
y.print();
Parameters:
  • nameOrFn (string|Function) The name of the closure, or the function to execute. If a name is provided, the 2nd argument should be the function. If debug mode is on, the timing and the memory usage of the function will be tracked and displayed on the console using the provided name.
  • fn (Function) The function to execute. Optional
  • gradMode (boolean) If true, starts a tape and doesn't dispose tensors. Optional
Returns: void|number|string|tf.Tensor|tf.Tensor[]|{[key: string]:tf.Tensor|number|string}
tf.keep (result) function Source

Keeps a tf.Tensor generated inside a tf.tidy() from being disposed automatically.

let b;
const y = tf.tidy(() => {
   const one = tf.scalar(1);
   const a = tf.scalar(2);

   // b will not be cleaned up by the tidy. a and one will be cleaned up
   // when the tidy ends.
   b = tf.keep(a.square());

   console.log('numTensors (in tidy): ' + tf.memory().numTensors);

   // The value returned inside the tidy function will return
   // through the tidy, in this case to the variable y.
   return b.add(one);
});

console.log('numTensors (outside tidy): ' + tf.memory().numTensors);
console.log('y:');
y.print();
console.log('b:');
b.print();
Parameters:
  • result (tf.Tensor) The tensor to keep from being disposed.
Returns: tf.Tensor
tf.memory () function Source

Returns memory info at the current time in the program. The result is an object with the following properties:

  • numBytes: Number of bytes allocated (undisposed) at this time.
  • numTensors: Number of unique tensors allocated.
  • numDataBuffers: Number of unique data buffers allocated (undisposed) at this time, which is ≤ the number of tensors (e.g. a.reshape(newShape) makes a new Tensor that shares the same data buffer with a).
  • unreliable: Optional boolean:
    • On WebGL, not present (always reliable).
    • On CPU, true. Due to automatic garbage collection, these numbers represent undisposed tensors, i.e. not wrapped in tidy(), or lacking a call to tensor.dispose().
Returns: MemoryInfo
tf.time (f) function Source

Executes f() and returns a promise that resolves with timing information.

The result is an object with the following properties:

  • wallMs: Wall execution time.
  • kernelMs: Kernel execution time, ignoring data transfer.
  • On WebGL The following additional properties exist:
    • uploadWaitMs: CPU blocking time on texture uploads.
    • downloadWaitMs: CPU blocking time on texture downloads (readPixels).
const x = tf.randomNormal([20, 20]);
const time = await tf.time(() => x.matMul(x));

console.log(`kernelMs: ${time.kernelMs}, wallTimeMs: ${time.wallMs}`);
Parameters:
  • f (() => void) The function to execute and time.
Returns: Promise
tf.nextFrame () function Source

Returns a promise that resolve when a requestAnimationFrame has completed.

This is simply a sugar method so that users can do the following: await tf.nextFrame();

Returns: Promise

TensorFlow.js can run mathematical operations on different backends. Currently, we support WebGL and JavaScript CPU. By default, we choose the 'best' backend available, but allow users to customize their backend.

tf.setBackend (backendType, safeMode?) function Source

Sets the backend (cpu, webgl, etc) responsible for creating tensors and executing operations on those tensors.

Parameters:
  • backendType ('webgl'|'cpu') The backend type. Currently supports 'webgl'|'cpu'.
  • safeMode (boolean) Defaults to false. In safe mode, you are forced to construct tensors and call math operations inside a tidy() which will automatically clean up intermediate tensors. Optional
Returns: void
tf.getBackend () function Source

Returns the current backend (cpu, webgl, etc). The backend is responsible for creating tensors and executing operations on those tensors.

Returns: 'webgl'|'cpu'

Constraints are added to attributes of a Layer (such as weights, kernels, or biases) at construction time to clamp, or otherwise enforce an allowed range, of values for different components of the Layer.

tf.constraints.maxNorm (config) function Source

MaxNorm weight constraint.

Constrains the weights incident to each hidden unit to have a norm less than or equal to a desired value.

References - Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014

Parameters:
  • config (Object)
  • maxValue (number) Maximum norm for incoming weights Optional
  • axis (number) Axis along which to calculate norms.

    For instance, in a Dense layer the weight matrix has shape [inputDim, outputDim], set axis to 0 to constrain each weight vector of length [inputDim,]. In a Conv2D layer with dataFormat="channels_last", the weight tensor has shape [rows, cols, inputDepth, outputDepth], set axis to [0, 1, 2] to constrain the weights of each filter tensor of size [rows, cols, inputDepth]. Optional

Parameters:
  • config (Object)
  • minValue (number) Minimum norm for incoming weights Optional
  • maxValue (number) Maximum norm for incoming weights Optional
  • axis (number) Axis along which to calculate norms. For instance, in a Dense layer the weight matrix has shape [inputDim, outputDim], set axis to 0 to constrain each weight vector of length [inputDim,]. In a Conv2D layer with dataFormat="channels_last", the weight tensor has shape [rows, cols, inputDepth, outputDepth], set axis to [0, 1, 2] to constrain the weights of each filter tensor of size [rows, cols, inputDepth]. Optional
  • rate (number) Rate for enforcing the constraint: weights will be rescaled to yield: (1 - rate) * norm + rate * norm.clip(minValue, maxValue). Effectively, this means that rate=1.0 stands for strict enforcement of the constraint, while rate<1.0 means that weights will be rescaled at each step to slowly move towards a value inside the desired interval. Optional

Constains the weight to be non-negative.

tf.constraints.unitNorm (config) function Source

Constrains the weights incident to each hidden unit to have unit norm.

Parameters:
  • config (Object)
  • axis (number) Axis along which to calculate norms.

    For instance, in a Dense layer the weight matrix has shape [inputDim, outputDim], set axis to 0 to constrain each weight vector of length [inputDim,]. In a Conv2D layer with dataFormat="channels_last", the weight tensor has shape [rows, cols, inputDepth, outputDepth], setaxisto[0, 1, 2]to constrain the weights of each filter tensor of size[rows, cols, inputDepth]`. Optional

Base class for functions that impose constraints on weight values

Initializers are used in Layers to establish the starting the values of weights, biases, kernels, etc.

Initializer that generates values initialized to some constant.

Parameters:
  • config (Object)
  • value (number) The value for each element in the variable.

Glorot normal initializer, also called Xavier normal initializer. It draws samples from a truncated normal distribution centered on 0 with stddev = sqrt(2 / (fan_in + fan_out)) where fan_in is the number of input units in the weight tensor and fan_out is the number of output units in the weight tensor.

Reference: Glorot & Bengio, AISTATS 2010 http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf

Parameters:
  • config (Object)
  • seed (number) Random number generator seed. Optional

Glorot uniform initializer, also called Xavier uniform initializer. It draws samples from a uniform distribution within [-limit, limit] where limit is sqrt(6 / (fan_in + fan_out)) where fan_in is the number of input units in the weight tensor and fan_out is the number of output units in the weight tensor

Reference: Glorot & Bengio, AISTATS 2010 http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf.

Parameters:
  • config (Object)
  • seed (number) Random number generator seed. Optional

He normal initializer.

It draws samples from a truncated normal distribution centered on 0 with stddev = sqrt(2 / fanIn) where fanIn is the number of input units in the weight tensor.

Reference: He et al., http://arxiv.org/abs/1502.01852

Parameters:
  • config (Object)
  • seed (number) Random number generator seed. Optional

Initializer that generates the identity matrix. Only use for square 2D matrices.

Parameters:
  • config (Object)
  • gain (number) Multiplicative factor to apply to the identity matrix. Optional

LeCun normal initializer.

It draws samples from a truncated normal distribution centered on 0 with stddev = sqrt(1 / fanIn) where fanIn is the number of input units in the weight tensor.

References: Self-Normalizing Neural Networks Efficient Backprop

Parameters:
  • config (Object)
  • seed (number) Random number generator seed. Optional

Initializer that generates tensors initialized to 1.

Initializer that generates a random orthogonal matrix.

Reference: Saxe et al., http://arxiv.org/abs/1312.6120

Parameters:
  • config (Object)
  • gain (number) Multiplicative factor to apply to the orthogonal matrix. Defaults to 1. Optional

Initializer that generates random values initialized to a normal distribution.

Parameters:
  • config (Object)
  • mean (number) Mean of the random values to generate. Optional
  • stddev (number) Standard deviation of the random values to generate. Optional
  • seed (number) Used to seed the random generator. Optional

Initializer that generates random values initialized to a uniform distribution.

Values will be distributed uniformly between the configured minval and maxval.

Parameters:
  • config (Object)
  • minval (number) Lower bound of the range of random values to generate. Optional
  • maxval (number) Upper bound of the range of random values to generate. Optional
  • seed (number) Used to seed the random generator. Optional

Initializer that generates random values initialized to a truncated normal. distribution.

These values are similar to values from a RandomNormal except that values more than two standard deviations from the mean are discarded and re-drawn. This is the recommended initializer for neural network weights and filters.

Parameters:
  • config (Object)
  • mean (number) Mean of the random values to generate. Optional
  • stddev (number) Standard deviation of the random values to generate. Optional
  • seed (number) Used to seed the random generator. Optional

Initializer capable of adapting its scale to the shape of weights. With distribution=NORMAL, samples are drawn from a truncated normal distribution centered on zero, with stddev = sqrt(scale / n) where n is:

  • number of input units in the weight tensor, if mode = FAN_IN.
  • number of output units, if mode = FAN_OUT.
  • average of the numbers of input and output units, if mode = FAN_AVG. With distribution=UNIFORM, samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).
Parameters:
  • config (Object)
  • scale (number) Scaling factor (positive float).
  • mode ('fanIn'|'fanOut'|'fanAvg') Fanning mode for inputs and outputs.
  • distribution ('normal'|'uniform') Probabilistic distribution of the values.
  • seed (number) Random number generator seed. Optional

Initializer that generates tensors initialized to 0.

Returns: Zeros

Initializer base class.

Regularizers can be attached to various components of a Layer to add a 'scoring' function to help drive weights, or other trainable values, away from excessively large values. They're typically used to promote a notion that a 'simpler' model is better than a complicated model, assuming equal performance.

tf.regularizers.L1L2 extends Regularizer class Source

Regularizer for L1 and L2 regularization.

Adds a term to the loss to penalize large weights: loss += sum(l1 * abs(x)) + sum(l2 * x^2)

tf.regularizers.l1 (config?) function Source

Regularizer for L1 and L2 regularization.

Adds a term to the loss to penalize large weights: loss += sum(l1 * abs(x)) + sum(l2 * x^2)

Parameters:
  • config (L1Config) Optional
Returns: Regularizer
tf.regularizers.l1l2 (config?) function Source

Regularizer for L1 and L2 regularization.

Adds a term to the loss to penalize large weights: loss += sum(l1 * abs(x)) + sum(l2 * x^2)

Parameters:
  • config (L1L2Config) Optional
Returns: Regularizer
tf.regularizers.l2 (config?) function Source

Regularizer for L1 and L2 regularization.

Adds a term to the loss to penalize large weights: loss += sum(l1 * abs(x)) + sum(l2 * x^2)

Parameters:
  • config (L2Config) Optional
Returns: Regularizer