1 | /**
|
2 | * @license
|
3 | * Copyright 2020 Google LLC. All Rights Reserved.
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | * =============================================================================
|
16 | */
|
17 | import { DataType, DataTypeMap, FlatVector, NumericDataType, RecursiveArray, TensorLike, TypedArray } from './types';
|
18 | /**
|
19 | * Shuffles the array in-place using Fisher-Yates algorithm.
|
20 | *
|
21 | * ```js
|
22 | * const a = [1, 2, 3, 4, 5];
|
23 | * tf.util.shuffle(a);
|
24 | * console.log(a);
|
25 | * ```
|
26 | *
|
27 | * @param array The array to shuffle in-place.
|
28 | *
|
29 | * @doc {heading: 'Util', namespace: 'util'}
|
30 | */
|
31 | export declare function shuffle(array: any[] | Uint32Array | Int32Array | Float32Array): void;
|
32 | /**
|
33 | * Shuffles two arrays in-place the same way using Fisher-Yates algorithm.
|
34 | *
|
35 | * ```js
|
36 | * const a = [1,2,3,4,5];
|
37 | * const b = [11,22,33,44,55];
|
38 | * tf.util.shuffleCombo(a, b);
|
39 | * console.log(a, b);
|
40 | * ```
|
41 | *
|
42 | * @param array The first array to shuffle in-place.
|
43 | * @param array2 The second array to shuffle in-place with the same permutation
|
44 | * as the first array.
|
45 | *
|
46 | * @doc {heading: 'Util', namespace: 'util'}
|
47 | */
|
48 | export declare function shuffleCombo(array: any[] | Uint32Array | Int32Array | Float32Array, array2: any[] | Uint32Array | Int32Array | Float32Array): void;
|
49 | /** Clamps a value to a specified range. */
|
50 | export declare function clamp(min: number, x: number, max: number): number;
|
51 | export declare function nearestLargerEven(val: number): number;
|
52 | export declare function sum(arr: number[]): number;
|
53 | /**
|
54 | * Returns a sample from a uniform [a, b) distribution.
|
55 | *
|
56 | * @param a The minimum support (inclusive).
|
57 | * @param b The maximum support (exclusive).
|
58 | * @return A pseudorandom number on the half-open interval [a,b).
|
59 | */
|
60 | export declare function randUniform(a: number, b: number): number;
|
61 | /** Returns the squared Euclidean distance between two vectors. */
|
62 | export declare function distSquared(a: FlatVector, b: FlatVector): number;
|
63 | /**
|
64 | * Asserts that the expression is true. Otherwise throws an error with the
|
65 | * provided message.
|
66 | *
|
67 | * ```js
|
68 | * const x = 2;
|
69 | * tf.util.assert(x === 2, 'x is not 2');
|
70 | * ```
|
71 | *
|
72 | * @param expr The expression to assert (as a boolean).
|
73 | * @param msg A function that returns the message to report when throwing an
|
74 | * error. We use a function for performance reasons.
|
75 | *
|
76 | * @doc {heading: 'Util', namespace: 'util'}
|
77 | */
|
78 | export declare function assert(expr: boolean, msg: () => string): void;
|
79 | export declare function assertShapesMatch(shapeA: number[], shapeB: number[], errorMessagePrefix?: string): void;
|
80 | export declare function assertNonNull(a: TensorLike): void;
|
81 | /**
|
82 | * Flattens an arbitrarily nested array.
|
83 | *
|
84 | * ```js
|
85 | * const a = [[1, 2], [3, 4], [5, [6, [7]]]];
|
86 | * const flat = tf.util.flatten(a);
|
87 | * console.log(flat);
|
88 | * ```
|
89 | *
|
90 | * @param arr The nested array to flatten.
|
91 | * @param result The destination array which holds the elements.
|
92 | * @param skipTypedArray If true, avoids flattening the typed arrays. Defaults
|
93 | * to false.
|
94 | *
|
95 | * @doc {heading: 'Util', namespace: 'util'}
|
96 | */
|
97 | export declare function flatten<T extends number | boolean | string | Promise<number> | TypedArray>(arr: T | RecursiveArray<T>, result?: T[], skipTypedArray?: boolean): T[];
|
98 | /**
|
99 | * Returns the size (number of elements) of the tensor given its shape.
|
100 | *
|
101 | * ```js
|
102 | * const shape = [3, 4, 2];
|
103 | * const size = tf.util.sizeFromShape(shape);
|
104 | * console.log(size);
|
105 | * ```
|
106 | *
|
107 | * @doc {heading: 'Util', namespace: 'util'}
|
108 | */
|
109 | export declare function sizeFromShape(shape: number[]): number;
|
110 | export declare function isScalarShape(shape: number[]): boolean;
|
111 | export declare function arraysEqual(n1: FlatVector, n2: FlatVector): boolean;
|
112 | export declare function isInt(a: number): boolean;
|
113 | export declare function tanh(x: number): number;
|
114 | export declare function sizeToSquarishShape(size: number): [number, number];
|
115 | /**
|
116 | * Creates a new array with randomized indicies to a given quantity.
|
117 | *
|
118 | * ```js
|
119 | * const randomTen = tf.util.createShuffledIndices(10);
|
120 | * console.log(randomTen);
|
121 | * ```
|
122 | *
|
123 | * @param number Quantity of how many shuffled indicies to create.
|
124 | *
|
125 | * @doc {heading: 'Util', namespace: 'util'}
|
126 | */
|
127 | export declare function createShuffledIndices(n: number): Uint32Array;
|
128 | export declare function rightPad(a: string, size: number): string;
|
129 | export declare function repeatedTry(checkFn: () => boolean, delayFn?: (counter: number) => number, maxCounter?: number): Promise<void>;
|
130 | /**
|
131 | * Given the full size of the array and a shape that may contain -1 as the
|
132 | * implicit dimension, returns the inferred shape where -1 is replaced.
|
133 | * E.g. For shape=[2, -1, 3] and size=24, it will return [2, 4, 3].
|
134 | *
|
135 | * @param shape The shape, which may contain -1 in some dimension.
|
136 | * @param size The full size (number of elements) of the array.
|
137 | * @return The inferred shape where -1 is replaced with the inferred size.
|
138 | */
|
139 | export declare function inferFromImplicitShape(shape: number[], size: number): number[];
|
140 | export declare function parseAxisParam(axis: number | number[], shape: number[]): number[];
|
141 | /** Reduces the shape by removing all dimensions of shape 1. */
|
142 | export declare function squeezeShape(shape: number[], axis?: number[]): {
|
143 | newShape: number[];
|
144 | keptDims: number[];
|
145 | };
|
146 | export declare function getTypedArrayFromDType<D extends NumericDataType>(dtype: D, size: number): DataTypeMap[D];
|
147 | export declare function getArrayFromDType<D extends DataType>(dtype: D, size: number): DataTypeMap[D];
|
148 | export declare function checkConversionForErrors<D extends DataType>(vals: DataTypeMap[D] | number[], dtype: D): void;
|
149 | /** Returns true if the dtype is valid. */
|
150 | export declare function isValidDtype(dtype: DataType): boolean;
|
151 | /**
|
152 | * Returns true if the new type can't encode the old type without loss of
|
153 | * precision.
|
154 | */
|
155 | export declare function hasEncodingLoss(oldType: DataType, newType: DataType): boolean;
|
156 | export declare function isTypedArray(a: {}): a is Float32Array | Int32Array | Uint8Array;
|
157 | export declare function bytesPerElement(dtype: DataType): number;
|
158 | /**
|
159 | * Returns the approximate number of bytes allocated in the string array - 2
|
160 | * bytes per character. Computing the exact bytes for a native string in JS is
|
161 | * not possible since it depends on the encoding of the html page that serves
|
162 | * the website.
|
163 | */
|
164 | export declare function bytesFromStringArray(arr: Uint8Array[]): number;
|
165 | /** Returns true if the value is a string. */
|
166 | export declare function isString(value: {}): value is string;
|
167 | export declare function isBoolean(value: {}): boolean;
|
168 | export declare function isNumber(value: {}): boolean;
|
169 | export declare function inferDtype(values: TensorLike): DataType;
|
170 | export declare function isFunction(f: Function): boolean;
|
171 | export declare function nearestDivisor(size: number, start: number): number;
|
172 | export declare function computeStrides(shape: number[]): number[];
|
173 | export declare function toNestedArray(shape: number[], a: TypedArray, isComplex?: boolean): number | any[];
|
174 | export declare function makeOnesTypedArray<D extends DataType>(size: number, dtype: D): DataTypeMap[D];
|
175 | export declare function makeZerosTypedArray<D extends DataType>(size: number, dtype: D): DataTypeMap[D];
|
176 | /**
|
177 | * Make nested `TypedArray` filled with zeros.
|
178 | * @param shape The shape information for the nested array.
|
179 | * @param dtype dtype of the array element.
|
180 | */
|
181 | export declare function makeZerosNestedTypedArray<D extends DataType>(shape: number[], dtype: D): number | any[];
|
182 | export declare function assertNonNegativeIntegerDimensions(shape: number[]): void;
|
183 | /**
|
184 | * Computes flat index for a given location (multidimentionsal index) in a
|
185 | * Tensor/multidimensional array.
|
186 | *
|
187 | * @param locs Location in the tensor.
|
188 | * @param rank Rank of the tensor.
|
189 | * @param strides Tensor strides.
|
190 | */
|
191 | export declare function locToIndex(locs: number[], rank: number, strides: number[]): number;
|
192 | /**
|
193 | * Computes the location (multidimensional index) in a tensor/multidimentional
|
194 | * array for a given flat index.
|
195 | *
|
196 | * @param index Index in flat array.
|
197 | * @param rank Rank of tensor.
|
198 | * @param strides Strides of tensor.
|
199 | */
|
200 | export declare function indexToLoc(index: number, rank: number, strides: number[]): number[];
|
201 | /**
|
202 | * This method asserts whether an object is a Promise instance.
|
203 | * @param object
|
204 | */
|
205 | export declare function isPromise(object: any): boolean;
|