UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

308 lines (307 loc) 11.7 kB
/** * @license * Copyright 2018 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import { Scalar, Tensor, Tensor1D, Tensor2D, Tensor3D, Tensor4D, Tensor5D, Tensor6D, Variable } from '../tensor'; import { TensorLike, TensorLike1D, TensorLike2D, TensorLike3D, TensorLike4D, TensorLike5D, TensorLike6D } from '../types'; import { DataType, Rank, ShapeMap } from '../types'; /** * Creates a `tf.Tensor` with the provided values, shape and dtype. * * ```js * // Pass an array of values to create a vector. * tf.tensor([1, 2, 3, 4]).print(); * ``` * * ```js * // Pass a nested array of values to make a matrix or a higher * // dimensional tensor. * tf.tensor([[1, 2], [3, 4]]).print(); * ``` * * ```js * // Pass a flat array and specify a shape yourself. * tf.tensor([1, 2, 3, 4], [2, 2]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. If the values are strings, * they will be encoded as utf-8 and kept as `Uint8Array[]`. * @param shape The shape of the tensor. Optional. If not provided, * it is inferred from `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor<R extends Rank>(values: TensorLike, shape?: ShapeMap[R], dtype?: DataType): Tensor<R>; /** * 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. * * ```js * tf.scalar(3.14).print(); * ``` * * @param value The value of the scalar. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function scalar(value: number | boolean | string | Uint8Array, dtype?: DataType): Scalar; /** * 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. * * ```js * tf.tensor1d([1, 2, 3]).print(); * ``` * * @param values The values of the tensor. Can be array of numbers, * or a `TypedArray`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor1d(values: TensorLike1D, dtype?: DataType): Tensor1D; /** * 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. * * ```js * // Pass a nested array. * tf.tensor2d([[1, 2], [3, 4]]).print(); * ``` * ```js * // Pass a flat array and specify a shape. * tf.tensor2d([1, 2, 3, 4], [2, 2]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. * @param shape The shape of the tensor. If not provided, it is inferred from * `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor2d(values: TensorLike2D, shape?: [number, number], dtype?: DataType): Tensor2D; /** * 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. * * ```js * // Pass a nested array. * tf.tensor3d([[[1], [2]], [[3], [4]]]).print(); * ``` * ```js * // Pass a flat array and specify a shape. * tf.tensor3d([1, 2, 3, 4], [2, 2, 1]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. * @param shape The shape of the tensor. If not provided, it is inferred from * `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor3d(values: TensorLike3D, shape?: [number, number, number], dtype?: DataType): Tensor3D; /** * 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. * * ```js * // Pass a nested array. * tf.tensor4d([[[[1], [2]], [[3], [4]]]]).print(); * ``` * ```js * // Pass a flat array and specify a shape. * tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. * @param shape The shape of the tensor. Optional. If not provided, * it is inferred from `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor4d(values: TensorLike4D, shape?: [number, number, number, number], dtype?: DataType): Tensor4D; /** * Creates rank-5 `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.tensor5d` as it makes the code more readable. * * ```js * // Pass a nested array. * tf.tensor5d([[[[[1], [2]], [[3], [4]]]]]).print(); * ``` * ```js * // Pass a flat array and specify a shape. * tf.tensor5d([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 2, 2, 1]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. * @param shape The shape of the tensor. Optional. If not provided, * it is inferred from `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor5d(values: TensorLike5D, shape?: [number, number, number, number, number], dtype?: DataType): Tensor5D; /** * Creates rank-6 `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.tensor6d` as it makes the code more readable. * * ```js * // Pass a nested array. * tf.tensor6d([[[[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]]]]).print(); * ``` * ```js * // Pass a flat array and specify a shape. * tf.tensor6d([1, 2, 3, 4, 5, 6, 7, 8], [1, 1, 2, 2, 2, 1]).print(); * ``` * * @param values The values of the tensor. Can be nested array of numbers, * or a flat array, or a `TypedArray`. * @param shape The shape of the tensor. Optional. If not provided, * it is inferred from `values`. * @param dtype The data type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function tensor6d(values: TensorLike6D, shape?: [number, number, number, number, number, number], dtype?: DataType): Tensor6D; /** * Creates a new variable with the provided initial value. * ```js * const x = tf.variable(tf.tensor([1, 2, 3])); * x.assign(tf.tensor([4, 5, 6])); * * x.print(); * ``` * * @param initialValue Initial value for the tensor. * @param trainable If true, optimizers are allowed to update it. * @param name Name of the variable. Defaults to a unique id. * @param dtype If set, initialValue will be converted to the given type. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function variable<R extends Rank>(initialValue: Tensor<R>, trainable?: boolean, name?: string, dtype?: DataType): Variable<R>; /** * Creates a `tf.Tensor` with all elements set to 1. * * ```js * tf.ones([2, 2]).print(); * ``` * * @param shape An array of integers defining the output tensor shape. * @param dtype The type of an element in the resulting tensor. Defaults to * 'float'. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function ones<R extends Rank>(shape: ShapeMap[R], dtype?: DataType): Tensor<R>; /** * Creates a `tf.Tensor` with all elements set to 0. * * ```js * tf.zeros([2, 2]).print(); * ``` * * @param shape An array of integers defining the output tensor shape. * @param dtype The type of an element in the resulting tensor. Can * be 'float32', 'int32' or 'bool'. Defaults to 'float'. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function zeros<R extends Rank>(shape: ShapeMap[R], dtype?: DataType): Tensor<R>; /** * Creates a `tf.Tensor` filled with a scalar value. * * ```js * tf.fill([2, 2], 4).print(); * ``` * * @param shape An array of integers defining the output tensor shape. * @param value The scalar value to fill the tensor with. * @param dtype The type of an element in the resulting tensor. Defaults to * 'float'. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function fill<R extends Rank>(shape: ShapeMap[R], value: number | string, dtype?: DataType): Tensor<R>; /** * Creates a `tf.Tensor` with all elements set to 1 with the same shape as the * given tensor. * * ```js * const x = tf.tensor([1, 2]); * tf.onesLike(x).print(); * ``` * @param x A tensor. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function onesLike_<T extends Tensor>(x: T | TensorLike): T; /** * Creates a `tf.Tensor` with all elements set to 0 with the same shape as the * given tensor. * * ```js * const x = tf.tensor([1, 2]); * tf.zerosLike(x).print(); * ``` * * @param x The tensor of required shape. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function zerosLike_<T extends Tensor>(x: T | TensorLike): T; /** * Return an evenly spaced sequence of numbers over the given interval. * * ```js * tf.linspace(0, 9, 10).print(); * ``` * @param start The start value of the sequence. * @param stop The end value of the sequence. * @param num The number of values to generate. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function linspace(start: number, stop: number, num: number): Tensor1D; /** * 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. * * ```js * tf.range(0, 9, 2).print(); * ``` * * @param start An integer start value * @param stop An integer stop value * @param step An integer increment (will default to 1 or -1) * @param dtype The data type of the output tensor. Defaults to 'float32'. */ /** @doc {heading: 'Tensors', subheading: 'Creation'} */ declare function range(start: number, stop: number, step?: number, dtype?: 'float32' | 'int32'): Tensor1D; export { fill, linspace, ones, range, scalar, tensor, tensor1d, tensor2d, tensor3d, tensor4d, tensor5d, tensor6d, variable, zeros }; export declare const onesLike: typeof onesLike_; export declare const zerosLike: typeof zerosLike_;