/** * @license * Copyright 2023 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 * as tfc from '@tensorflow/tfjs-core'; import { util, backend, tidy as tidy$1, tensor1d, serialization, zeros as zeros$2, ones as ones$3, mul as mul$1, scalar as scalar$1, randomUniform as randomUniform$1, truncatedNormal as truncatedNormal$1, eye, linalg, dispose, memory, cast as cast$2, env as env$1, nextFrame, add as add$3, div as div$1, keep, train, clone as clone$1, argMax, reshape as reshape$2, Tensor as Tensor$1, Optimizer, io, sum as sum$1, abs, relu, clipByValue, leakyRelu, prelu as prelu$1, elu as elu$2, greater as greater$2, sub as sub$1, exp as exp$1, logSumExp, transpose as transpose$1, any, notEqual, zerosLike as zerosLike$1, greaterEqual as greaterEqual$1, moments, stack as stack$1, tensor, range as range$1, unstack as unstack$1, image, expandDims as expandDims$2, denseBincount, max as max$1, min as min$1 } from '@tensorflow/tfjs-core'; function _mergeNamespaces(n, m) { m.forEach(function (e) { e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) { if (k !== 'default' && !(k in n)) { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); }); return n; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Explicit error types. * * See the following link for more information about why the code includes * calls to setPrototypeOf: * * https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work */ // tslint:enable /** * Equivalent of Python's AttributeError. */ class AttributeError extends Error { constructor(message) { super(message); // Set the prototype explicitly. Object.setPrototypeOf(this, AttributeError.prototype); } } /** * Equivalent of Python's RuntimeError. */ class RuntimeError extends Error { constructor(message) { super(message); // Set the prototype explicitly. Object.setPrototypeOf(this, RuntimeError.prototype); } } /** * Equivalent of Python's ValueError. */ class ValueError extends Error { constructor(message) { super(message); // Set the prototype explicitly. Object.setPrototypeOf(this, ValueError.prototype); } } /** * Equivalent of Python's NotImplementedError. */ class NotImplementedError extends Error { constructor(message) { super(message); // Set the prototype explicitly. Object.setPrototypeOf(this, NotImplementedError.prototype); } } /** * Equivalent of Python's AssertionError. */ class AssertionError extends Error { constructor(message) { super(message); // Set the prototype explicitly. Object.setPrototypeOf(this, AssertionError.prototype); } } /** * @license * Copyright 2022 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * LruCache: A mapping from the String to T. If the number of the entries is * exceeding the `maxEntries`, the LruCache will delete the least recently * used entry. */ class LruCache { constructor(maxEntries) { this.maxEntries = maxEntries || 100; this.cache = new Map(); } /** * Get the entry for the key and mark it as used recently. */ get(key) { let entry; if (this.cache.has(key)) { entry = this.cache.get(key); this.cache.delete(key); this.cache.set(key, entry); } return entry; } /** * Put the entry into the cache. If the key already existed, mark the key as * used recently. */ put(key, value) { if (this.cache.has(key)) { this.cache.delete(key); } else if (this.cache.size >= this.maxEntries) { const keyToDelete = this.cache.keys().next().value; this.cache.delete(keyToDelete); } this.cache.set(key, value); } /** * Get the MaxEntries of the cache. */ getMaxEntries() { return this.maxEntries; } /** * Set the MaxEntries of the cache. If the maxEntries is decreased, reduce * entries in the cache. */ setMaxEntries(maxEntries) { if (maxEntries < 0) { throw new Error(`The maxEntries of LRU caches must be at least 0, but got ${maxEntries}.`); } if (this.maxEntries > maxEntries) { for (let i = 0; i < this.maxEntries - maxEntries; i++) { const keyToDelete = this.cache.keys().next().value; this.cache.delete(keyToDelete); } } this.maxEntries = maxEntries; } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // tslint:enable /** * If `value` is an Array, equivalent to Python's `value * numValues`. * If `value` is not an Array, equivalent to Python's `[value] * numValues` */ // tslint:disable-next-line:no-any function pyListRepeat(value, numValues) { if (Array.isArray(value)) { // tslint:disable-next-line:no-any let newArray = []; for (let i = 0; i < numValues; i++) { newArray = newArray.concat(value); } return newArray; } else { const newArray = new Array(numValues); newArray.fill(value); return newArray; } } function assert$1(val, message) { if (!val) { throw new AssertionError(message); } } /** * Count the number of elements of the `array` that are equal to `reference`. */ function count(array, refernce) { let counter = 0; for (const item of array) { if (item === refernce) { counter++; } } return counter; } /** * If an array is of length 1, just return the first element. Otherwise, return * the full array. * @param tensors */ function singletonOrArray(xs) { if (xs.length === 1) { return xs[0]; } return xs; } /** * Normalizes a list/tensor into a list. * * If a tensor is passed, we return * a list of size 1 containing the tensor. * * @param x target object to be normalized. */ // tslint:disable-next-line:no-any function toList(x) { if (Array.isArray(x)) { return x; } return [x]; } /** * Converts string to snake-case. * @param name */ function toSnakeCase(name) { const intermediate = name.replace(/(.)([A-Z][a-z0-9]+)/g, '$1_$2'); const insecure = intermediate.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); /* If the class is private the name starts with "_" which is not secure for creating scopes. We prefix the name with "private" in this case. */ if (insecure[0] !== '_') { return insecure; } return 'private' + insecure; } function toCamelCase(identifier) { // quick return for empty string or single character strings if (identifier.length <= 1) { return identifier; } // Check for the underscore indicating snake_case if (identifier.indexOf('_') === -1) { return identifier; } return identifier.replace(/[_]+(\w|$)/g, (m, p1) => p1.toUpperCase()); } // tslint:disable-next-line:no-any let _GLOBAL_CUSTOM_OBJECTS = {}; function serializeKerasObject(instance) { if (instance === null || instance === undefined) { return null; } const dict = {}; dict['className'] = instance.getClassName(); dict['config'] = instance.getConfig(); return dict; } /** * Replace ndarray-style scalar objects in serialization objects with numbers. * * Background: In some versions of tf.keras, certain scalar values in the HDF5 * model save file can be serialized as: `{'type': 'ndarray', 'value': num}`, * where in `num` is a plain number. This method converts such serialization * to a `number`. * * @param config The keras-format serialization object to be processed * (in place). */ function convertNDArrayScalarsInConfig(config) { if (config == null || typeof config !== 'object') { return; } else if (Array.isArray(config)) { config.forEach(configItem => convertNDArrayScalarsInConfig(configItem)); } else { const fields = Object.keys(config); for (const field of fields) { const value = config[field]; if (value != null && typeof value === 'object') { if (!Array.isArray(value) && value['type'] === 'ndarray' && typeof value['value'] === 'number') { config[field] = value['value']; } else { convertNDArrayScalarsInConfig(value); } } } } } /** * Deserialize a saved Keras Object * @param identifier either a string ID or a saved Keras dictionary * @param moduleObjects a list of Python class names to object constructors * @param customObjects a list of Python class names to object constructors * @param printableModuleName debug text for the object being reconstituted * @param fastWeightInit Optional flag to use fast weight initialization * during deserialization. This is applicable to cases in which * the initialization will be immediately overwritten by loaded weight * values. Default: `false`. * @returns a TensorFlow.js Layers object */ // tslint:disable:no-any function deserializeKerasObject(identifier, moduleObjects = {}, customObjects = {}, printableModuleName = 'object', fastWeightInit = false) { // tslint:enable if (typeof identifier === 'string') { const functionName = identifier; let fn; if (functionName in customObjects) { fn = customObjects[functionName]; } else if (functionName in _GLOBAL_CUSTOM_OBJECTS) { fn = _GLOBAL_CUSTOM_OBJECTS[functionName]; } else { fn = moduleObjects[functionName]; if (fn == null) { throw new ValueError(`Unknown ${printableModuleName}: ${identifier}. ` + `This may be due to one of the following reasons:\n` + `1. The ${printableModuleName} is defined in Python, in which ` + `case it needs to be ported to TensorFlow.js or your JavaScript ` + `code.\n` + `2. The custom ${printableModuleName} is defined in JavaScript, ` + `but is not registered properly with ` + `tf.serialization.registerClass().`); // TODO(cais): Add link to tutorial page on custom layers. } } return fn; } else { // In this case we are dealing with a Keras config dictionary. const config = identifier; if (config['className'] == null || config['config'] == null) { throw new ValueError(`${printableModuleName}: Improper config format: ` + `${JSON.stringify(config)}.\n` + `'className' and 'config' must set.`); } const className = config['className']; let cls, fromConfig; if (className in customObjects) { [cls, fromConfig] = customObjects[className]; } else if (className in _GLOBAL_CUSTOM_OBJECTS) { [cls, fromConfig] = _GLOBAL_CUSTOM_OBJECTS['className']; } else if (className in moduleObjects) { [cls, fromConfig] = moduleObjects[className]; } if (cls == null) { throw new ValueError(`Unknown ${printableModuleName}: ${className}. ` + `This may be due to one of the following reasons:\n` + `1. The ${printableModuleName} is defined in Python, in which ` + `case it needs to be ported to TensorFlow.js or your JavaScript ` + `code.\n` + `2. The custom ${printableModuleName} is defined in JavaScript, ` + `but is not registered properly with ` + `tf.serialization.registerClass().`); // TODO(cais): Add link to tutorial page on custom layers. } if (fromConfig != null) { // Porting notes: Instead of checking to see whether fromConfig accepts // customObjects, we create a customObjects dictionary and tack it on to // config['config'] as config['config'].customObjects. Objects can use it, // if they want. // tslint:disable-next-line:no-any const customObjectsCombined = {}; for (const key of Object.keys(_GLOBAL_CUSTOM_OBJECTS)) { customObjectsCombined[key] = _GLOBAL_CUSTOM_OBJECTS[key]; } for (const key of Object.keys(customObjects)) { customObjectsCombined[key] = customObjects[key]; } // Add the customObjects to config const nestedConfig = config['config']; nestedConfig['customObjects'] = customObjectsCombined; const backupCustomObjects = Object.assign({}, _GLOBAL_CUSTOM_OBJECTS); for (const key of Object.keys(customObjects)) { _GLOBAL_CUSTOM_OBJECTS[key] = customObjects[key]; } convertNDArrayScalarsInConfig(config['config']); const returnObj = fromConfig(cls, config['config'], customObjects, fastWeightInit); _GLOBAL_CUSTOM_OBJECTS = Object.assign({}, backupCustomObjects); return returnObj; } else { // Then `cls` may be a function returning a class. // In this case by convention `config` holds // the kwargs of the function. const backupCustomObjects = Object.assign({}, _GLOBAL_CUSTOM_OBJECTS); for (const key of Object.keys(customObjects)) { _GLOBAL_CUSTOM_OBJECTS[key] = customObjects[key]; } // In python this is **config['config'], for tfjs-layers we require // classes that use this fall-through construction method to take // a config interface that mimics the expansion of named parameters. const returnObj = new cls(config['config']); _GLOBAL_CUSTOM_OBJECTS = Object.assign({}, backupCustomObjects); return returnObj; } } } /** * Compares two numbers for sorting. * @param a * @param b */ function numberCompare(a, b) { return (a < b) ? -1 : ((a > b) ? 1 : 0); } /** * Comparison of two numbers for reverse sorting. * @param a * @param b */ function reverseNumberCompare(a, b) { return -1 * numberCompare(a, b); } /** * Get the unique elements of an array. * @param xs Array. * @returns An Array consisting of the unique elements in `xs`. */ function unique(xs) { if (xs == null) { return xs; } const out = []; // TODO(cais): Maybe improve performance by sorting. for (const x of xs) { if (out.indexOf(x) === -1) { out.push(x); } } return out; } /** * Determine if an Object is empty (i.e., does not have own properties). * @param obj Object * @returns Whether the Object is empty. * @throws ValueError: If object is `null` or `undefined`. */ function isObjectEmpty(obj) { if (obj == null) { throw new ValueError(`Invalid value in obj: ${JSON.stringify(obj)}`); } for (const key in obj) { if (obj.hasOwnProperty(key)) { return false; } } return true; } /** * Helper function used to build type union/enum run-time checkers. * @param values The list of allowed values. * @param label A string name for the type * @param value The value to test. * @throws ValueError: If the value is not in values nor `undefined`/`null`. */ function checkStringTypeUnionValue(values, label, value) { if (value == null) { return; } if (values.indexOf(value) < 0) { throw new ValueError(`${value} is not a valid ${label}. Valid values are ${values} or null/undefined.`); } } /** * Helper function for verifying the types of inputs. * * Ensures that the elements of `x` are all of type `expectedType`. * Also verifies that the length of `x` is within bounds. * * @param x Object to test. * @param expectedType The string expected type of all of the elements in the * Array. * @param minLength Return false if x.length is less than this. * @param maxLength Return false if x.length is greater than this. * @returns true if and only if `x` is an `Array` with * length >= `minLength` and <= `maxLength`. */ // tslint:disable:no-any function checkArrayTypeAndLength(x, expectedType, minLength = 0, maxLength = Infinity) { assert$1(minLength >= 0); assert$1(maxLength >= minLength); return (Array.isArray(x) && x.length >= minLength && x.length <= maxLength && x.every(e => typeof e === expectedType)); } // tslint:enable:no-any /** * Assert that a value or an array of value are positive integer. * * @param value The value being asserted on. May be a single number or an array * of numbers. * @param name Name of the value, used to make the error message. */ function assertPositiveInteger(value, name) { if (Array.isArray(value)) { util.assert(value.length > 0, () => `${name} is unexpectedly an empty array.`); value.forEach((v, i) => assertPositiveInteger(v, `element ${i + 1} of ${name}`)); } else { util.assert(Number.isInteger(value) && value > 0, () => `Expected ${name} to be a positive integer, but got ` + `${formatAsFriendlyString(value)}.`); } } /** * Format a value into a display-friendly, human-readable fashion. * * - `null` is formatted as `'null'` * - Strings are formated with flanking pair of quotes. * - Arrays are formatted with flanking pair of square brackets. * * @param value The value to display. * @return Formatted string. */ // tslint:disable-next-line:no-any function formatAsFriendlyString(value) { if (value === null) { return 'null'; } else if (Array.isArray(value)) { return '[' + value.map(v => formatAsFriendlyString(v)).join(',') + ']'; } else if (typeof value === 'string') { return `"${value}"`; } else { return `${value}`; } } /** * Returns a function `f2` (decorator) which wraps the original function * `f`. `f2` guarantees that `f` can be called at most once * every `waitMs` ms. If `f2` is called more often, it will return * the last returned result of `f`. * * @param f The original function `f` to wrap. * @param waitMs The time between two consecutive calls to `f` in ms. */ function debounce(f, waitMs, nowFunc) { let lastTime = nowFunc != null ? nowFunc() : util.now(); let lastResult; const f2 = (...args) => { const now = nowFunc != null ? nowFunc() : util.now(); if (now - lastTime < waitMs) { return lastResult; } lastTime = now; lastResult = f(...args); return lastResult; }; return f2; } /** * Returns the fusable activation given a layers identifier. * * @param activationName The layers identifier string. * @return The name of the fusable activation. */ function mapActivationToFusedKernel(activationName) { if (activationName === 'relu') { return 'relu'; } if (activationName === 'linear') { return 'linear'; } if (activationName === 'elu') { return 'elu'; } return null; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Utilities related to persistent state in the backend. */ /** * An ID to track `tf.SymbolicTensor`s and derived classes. * Required in different places in engine/topology.ts to identify unique * tensors. */ let _nextUniqueTensorId = 0; function getNextUniqueTensorId() { return _nextUniqueTensorId++; } const _uidPrefixes = {}; /** * Provides a unique UID given a string prefix. * * @param prefix */ function getUid(prefix = '') { if (!(prefix in _uidPrefixes)) { _uidPrefixes[prefix] = 0; } _uidPrefixes[prefix] += 1; return prefix + _uidPrefixes[prefix].toString(); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ const VALID_DATA_FORMAT_VALUES = ['channelsFirst', 'channelsLast']; const VALID_INTERPOLATION_FORMAT_VALUES = ['nearest', 'bilinear']; const VALID_PADDING_MODE_VALUES = ['valid', 'same', 'causal']; const VALID_POOL_MODE_VALUES = ['max', 'avg']; const VALID_BIDIRECTIONAL_MERGE_MODES = ['sum', 'mul', 'concat', 'ave']; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // A map from the requested scoped name of a Tensor to the number of Tensors // wanting that name so far. This allows enforcing name uniqueness by appending // an incrementing index, e.g. scope/name, scope/name_1, scope/name_2, etc. const nameMap = new Map(); function checkDataFormat(value) { checkStringTypeUnionValue(VALID_DATA_FORMAT_VALUES, 'DataFormat', value); } function checkInterpolationFormat(value) { checkStringTypeUnionValue(VALID_INTERPOLATION_FORMAT_VALUES, 'InterpolationFormat', value); } function checkPaddingMode(value) { checkStringTypeUnionValue(VALID_PADDING_MODE_VALUES, 'PaddingMode', value); } function checkPoolMode(value) { checkStringTypeUnionValue(VALID_POOL_MODE_VALUES, 'PoolMode', value); } const _nameScopeStack = []; const _nameScopeDivider = '/'; /** * Enter namescope, which can be nested. */ function nameScope(name, fn) { _nameScopeStack.push(name); try { const val = fn(); _nameScopeStack.pop(); return val; } catch (e) { _nameScopeStack.pop(); throw e; } } /** * Get the current namescope as a flat, concatenated string. */ function currentNameScopePrefix() { if (_nameScopeStack.length === 0) { return ''; } else { return _nameScopeStack.join(_nameScopeDivider) + _nameScopeDivider; } } /** * Get the name a Tensor (or Variable) would have if not uniqueified. * @param tensorName * @return Scoped name string. */ function getScopedTensorName(tensorName) { if (!isValidTensorName(tensorName)) { throw new Error('Not a valid tensor name: \'' + tensorName + '\''); } return currentNameScopePrefix() + tensorName; } /** * Get unique names for Tensors and Variables. * @param scopedName The fully-qualified name of the Tensor, i.e. as produced by * `getScopedTensorName()`. * @return A unique version of the given fully scoped name. * If this is the first time that the scoped name is seen in this session, * then the given `scopedName` is returned unaltered. If the same name is * seen again (producing a collision), an incrementing suffix is added to the * end of the name, so it takes the form 'scope/name_1', 'scope/name_2', etc. */ function getUniqueTensorName(scopedName) { if (!isValidTensorName(scopedName)) { throw new Error('Not a valid tensor name: \'' + scopedName + '\''); } if (!nameMap.has(scopedName)) { nameMap.set(scopedName, 0); } const index = nameMap.get(scopedName); nameMap.set(scopedName, nameMap.get(scopedName) + 1); if (index > 0) { const result = `${scopedName}_${index}`; // Mark the composed name as used in case someone wants // to call getUniqueTensorName("name_1"). nameMap.set(result, 1); return result; } else { return scopedName; } } const tensorNameRegex = new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/); /** * Determine whether a string is a valid tensor name. * @param name * @returns A Boolean indicating whether `name` is a valid tensor name. */ function isValidTensorName(name) { return !!name.match(tensorNameRegex); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Determine if a number is an integer. */ function isInteger(x) { return x === parseInt(x.toString(), 10); } /** * Calculate the product of an array of numbers. * @param array The array to calculate the product over. * @param begin Beginning index, inclusive. * @param end Ending index, exclusive. * @return The product. */ function arrayProd(array, begin, end) { if (begin == null) { begin = 0; } if (end == null) { end = array.length; } let prod = 1; for (let i = begin; i < end; ++i) { prod *= array[i]; } return prod; } /** * Compute minimum value. * @param array * @return minimum value. */ function min(array) { // same behavior as tf.min() if (array.length === 0) { return Number.NaN; } let min = Number.POSITIVE_INFINITY; for (let i = 0; i < array.length; i++) { const value = array[i]; if (value < min) { min = value; } } return min; } /** * Compute maximum value. * @param array * @return maximum value */ function max(array) { // same behavior as tf.max() if (array.length === 0) { return Number.NaN; } let max = Number.NEGATIVE_INFINITY; for (let i = 0; i < array.length; i++) { const value = array[i]; if (value > max) { max = value; } } return max; } /** * Generate an array of integers in [begin, end). * @param begin Beginning integer, inclusive. * @param end Ending integer, exclusive. * @returns Range array. * @throws ValueError, iff `end` < `begin`. */ function range(begin, end) { if (end < begin) { throw new ValueError(`end (${end}) < begin (${begin}) is forbidden.`); } const out = []; for (let i = begin; i < end; ++i) { out.push(i); } return out; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ let _epsilon; /** * Returns the value of the fuzz factor used in numeric expressions. */ function epsilon() { if (_epsilon == null) { _epsilon = backend().epsilon(); } return _epsilon; } /** * Returns the default image data format convention. */ function imageDataFormat() { return 'channelsLast'; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Casts a tensor to a different dtype and returns it. * @param x Input tensor. * @param dtype String: 'float32'|'int32'|'bool'. * @returns Tensor of the specified `dtype`. */ function cast$1(x, dtype) { return tfc.cast(x, dtype); } /** * Adds a 1-sized dimension at index "axis". * @param x Input tensor. * @param axis Position where to add the new axis. * @returns Result of the dimension expansion. */ function expandDims$1(x, axis = -1) { const outShape = x.shape.slice(); if (axis < 0) { axis = outShape.length + axis + 1; } outShape.splice(axis, 0, 1); return tfc.reshape(x, outShape); } /** * Repeats a 2D tensor. * * If `x` has shape `[samples, dim]` and `n` is 2, for example, the output * will have shape `[samples, 2, dim]`. * * @param x Input tensor. * @param n Integer, number of times to repeat. * @returns The result of the repeat operation. * @throws ValueError: If input tensor is not 2D. */ function repeat(x, n) { return tidy$1(() => { if (x.shape.length !== 2) { throw new ValueError(`repeat() expects a rank-2 tensor, but received a ` + `rank-${x.shape.length} tensor.`); } const y = expandDims$1(x, 1); return tile$1(y, [1, n, 1]); }); } /** * Flatten a Tensor into 1D. * @param x Input tensor. * @return The result of the flattening `x`. */ function flatten$2(x) { const newShape = [arrayProd(x.shape)]; return tfc.reshape(x, newShape); } /** * Turn a nD tensor into a 2D tensor with same 0th dimension. * In other words, it flattens each data samples of a batch. * * @param x The tensor to flatten. The rank of this tensor is required to be 2 * or higher. * @return The result of the flattening. */ function batchFlatten(x) { if (x.rank <= 1) { throw new ValueError(`batchFlatten requires a minimum rank of 2. Got rank: ${x.rank}.`); } const newShape = [x.shape[0], arrayProd(x.shape, 1)]; return tfc.reshape(x, newShape); } /** * Do slicing along the first axis. * @param array input `tf.Tensor`. * @param start starting index, inclusive. * @param size size of the slice along the first axis. * @returns result of the slicing. * @throws ValueError: If `array` is of an unsupported subtype of `tf.Tensor`. */ function sliceAlongFirstAxis(array, start, size) { return tidy$1(() => { switch (array.rank) { case 1: return tfc.slice1d(array, start, size); case 2: return tfc.slice2d(array, [start, 0], [size, array.shape[1]]); case 3: return tfc.slice3d(array, [start, 0, 0], [size, array.shape[1], array.shape[2]]); case 4: return tfc.slice4d(array, [start, 0, 0, 0], [size, array.shape[1], array.shape[2], array.shape[3]]); case 5: return tfc.slice(array, [start, 0, 0, 0, 0], [ size, array.shape[1], array.shape[2], array.shape[3], array.shape[4] ]); case 6: return tfc.slice(array, [start, 0, 0, 0, 0, 0], [ size, array.shape[1], array.shape[2], array.shape[3], array.shape[4], array.shape[5] ]); default: throw new ValueError(`sliceAlongFirstAxis() received an unsupported tensor rank: ` + `${array.rank}`); } }); } /** * Do slicing along the last axis. * @param array input `tf.Tensor`. * @param start starting index, inclusive. * @param size size of the slice along the last axis. * @returns result of the slicing. * @throws ValueError: If `array` is of an unsupported subtype of `tf.Tensor`. */ function sliceAlongLastAxis(array, start, size) { return tidy$1(() => { switch (array.rank) { case 1: return tfc.slice1d(array, start, size); case 2: return tfc.slice2d(array, [0, start], [array.shape[0], size]); case 3: return tfc.slice3d(array, [0, 0, start], [array.shape[0], array.shape[1], size]); case 4: return tfc.slice4d(array, [0, 0, 0, start], [array.shape[0], array.shape[1], array.shape[2], size]); default: throw new ValueError(`sliceAlongLastAxis() received an unsupported tensor rank: ` + `${array.rank}`); } }); } /** * Do slicing along the sepcified axis. * @param array input `tf.Tensor`. * @param start starting index, inclusive. * @param size of the slice along the chosen axis. * @param choose an axis. * @returns result of the slicing. * @throws ValueError: If `array` is of an unsupported subtype of `tf.Tensor`. */ function sliceAlongAxis(array, start, size, axis) { return tidy$1(() => { switch (array.rank) { case 1: return tfc.slice1d(array, start, size); case 2: switch (axis) { case 1: return sliceAlongFirstAxis(array, start, size); case 2: return sliceAlongLastAxis(array, start, size); default: throw new ValueError(`The axis is not within the rank of the tensor ` + `${axis}`); } case 3: switch (axis) { case 1: return sliceAlongFirstAxis(array, start, size); case 2: return tfc.slice3d(array, [0, start, 0], [array.shape[0], size, array.shape[2]]); case 3: return sliceAlongLastAxis(array, start, size); default: throw new ValueError(`The axis is not within the rank of the tensor ` + `${axis}`); } case 4: switch (axis) { case 1: return sliceAlongFirstAxis(array, start, size); case 2: return tfc.slice4d(array, [0, start, 0, 0], [array.shape[0], size, array.shape[2], array.shape[3]]); case 3: return tfc.slice4d(array, [0, 0, start, 0], [array.shape[0], array.shape[1], size, array.shape[3]]); case 4: return sliceAlongLastAxis(array, start, size); default: throw new ValueError(`The axis is not within the rank of the tensor ` + `${axis}`); } default: throw new ValueError(`sliceAlongLastAxis() received an unsupported tensor rank: ` + `${array.rank}`); } }); } /** * Concatenates a list of tensors alongside the specified axis. * @param tensors `Array` of tensors to concatenate. * @param axis Concatenation axis. * @returns The result of the concatenation. */ function concatenate$1(tensors, axis = -1) { let rank; if (axis < 0) { rank = tensors[0].rank; if (rank !== 0) { axis = rank; } else { axis = 0; } } if (axis === tensors[0].rank) { // Porting Note: This is necessary because tfc.concat() requires axis to be // in the interval [-rank, rank). axis = -1; } // Porting Note: Sparse concat is not supported yet. return tfc.concat(tensors, axis); } /** * Concatenate two arrays along the first dimension. * @param a The 1st `tf.Tensor` to concatenate. * @param b The 2nd `tf.Tensor` to concatenate. * @returns Result of the concatenation. * @throws ValueError: If `a` is of an unsupported subtype of `tf.Tensor`. */ function concatAlongFirstAxis(a, b) { switch (a.rank) { case 1: return tfc.concat1d([a, b]); case 2: return tfc.concat2d([a, b], 0); case 3: return tfc.concat3d([a, b], 0); case 4: return tfc.concat4d([a, b], 0); default: throw new ValueError(`concatAlongFirstAxis() received an unsupported ` + `tensor rank: ${a.rank}`); } } /** * Creates a tensor by tiling `x` by `n`. * @param x A tensor. * @param n An Array of integers or a single integer. If an Array, the length * must be the same as the number of dimensions in `x`. If a single integer, * it will be treated as an Array of length 1. */ function tile$1(x, n) { if (!Array.isArray(n)) { n = [n]; } if (x.rank !== n.length) { throw new ValueError(`The length of input n (${n.length}) does not match ` + `the number of dimensions in input x (${x.rank})`); } return tfc.tile(x, n); } /* Creation of random tensors. */ /** * Get a tensor with normal distribution of values. * * @param shape Shape of the tensor. * @param mean mean value of the normal distribution. * @param stddev standard deviation of the normal distribution. * @param dtype * @param seed * @return The normal tensor. */ function randomNormal$1(shape, mean = 0.0, stddev = 1.0, dtype, seed) { return tfc.randomNormal(shape, mean, stddev, dtype, seed); } /* Linear Algebra */ /** * Multiply two tensors and returns the result as a tensor. * * For 2D tensors, this is equivalent to matrix multiplication (matMul). * For tensors of higher ranks, it follows the Theano behavior, * (e.g. `(2, 3) * (4, 3, 5) -> (2, 4, 5)`). From the Theano documentation: * * For N dimensions it is a sum product over the last axis of x and the * second-to-last of y: * * @param a A tensor of at least rank 2. * @param b A tensor of at least rank 2. * @param activation (optional) A string identifying the activation * function. * @return Result of the dot operation. */ function dot$1(a, b, activation, bias) { if ((a.rank < 2) || (b.rank < 2)) { throw new NotImplementedError(`dot requires both inputs to be rank >= 2` + ` but got x shape = ${a.shape} and y shape = ${b.shape}`); } if (b.rank >= 3) { const xLastDim = a.shape.slice(-1)[0]; const ySecondLastDim = b.shape.slice(-2)[0]; if (xLastDim !== ySecondLastDim) { throw new NotImplementedError(`If rank y >= 3, then the second last dim` + ` of y must equal the last dim of x but got x shape = ${a.shape} and ` + ` y shape = ${b.shape}`); } } // Handle basic 2D x 2D case. if ((a.rank === 2) && (b.rank === 2)) { const transposeA = false; const transposeB = false; // tfc.fused.matMul only fuses certain activation functions. Unsupported // activation functions are treated as 'linear' activations, which is // equivalent to a no-op. return tfc.fused.matMul({ a, b: b, transposeA, transposeB, bias: bias ? reshapeBias(a.rank, bias, imageDataFormat()) : null, activation }); } else { // Reshape x into the analogous 2D Tensor. const aFirstDims = a.shape.slice(); // Holds all but the last dim of x. const aLastDim = aFirstDims.pop(); a = tfc.reshape(a, [-1, aLastDim]); // Reshape y into the analogous 2D Tensor, and keep track of the // required dimensions to reproduce the output shape. const bShape = b.shape.slice(); const bLastDim = bShape.pop(); const ySecondLastDim = bShape.pop(); const yOtherDims = [...bShape, bLastDim]; // permutation should be like [r-2, 0, 1, 2, ... r-4, r-3, r-1] // where r is the rank of y. const perm = Array.from({ length: b.rank }, (_, i) => { if (i === 0) { return b.rank - 2; } else if (i <= b.rank - 2) { return i - 1; } return i; }); b = tfc.reshape(tfc.transpose(b, perm), [ySecondLastDim, -1]); // Multiply x and y as 2D Tensors, and then reshape back to original. const outputShape = [...aFirstDims, ...yOtherDims]; const transposeA = false; const transposeB = false; return tfc.reshape(tfc.fused.matMul({ a, b, transposeA, transposeB, bias: bias ? reshapeBias(a.rank, bias, imageDataFormat()) : null, activation }), outputShape); } } /* Elementary math functions. */ /** * Retrieves the elements of indices `indices` in the tensor `reference`. * @param reference A tensor. * @param indices An integer tensor of indices or an `Array` of integers. * @param axis Axis along which to perform the gather operation. * @returns The result of the gathering as a tensor. */ function gather$1(reference, indices, axis) { return tidy$1(() => { if (Array.isArray(indices)) { indices = tensor1d(indices, 'int32'); } else { indices = tfc.cast(indices, 'int32'); } return tfc.gather(reference, indices, axis); }); } /** * Element-wise square. * @param x Input tensor. * @return element-wise x^2 */ function square$1(x) { return tfc.mul(x, x); } /** * Reshapes bias tensor according to rank of x. */ function reshapeBias(xRank, bias, dataFormat) { const biasShape = bias.shape; if (bias.rank !== 1 && bias.rank !== xRank) { throw new ValueError(`Unexpected bias dimensions: ${bias.rank}` + `; expected it to be 1 or ${xRank}`); } if (xRank === 5) { if (dataFormat === 'channelsFirst') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, biasShape[0], 1, 1, 1]); } else { return tfc.reshape(bias, [1, biasShape[3], biasShape[0], biasShape[1], biasShape[2]]); } } else if (dataFormat === 'channelsLast') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, 1, 1, 1, biasShape[0]]); } else { return tfc.reshape(bias, [1].concat(biasShape)); } } } else if (xRank === 4) { if (dataFormat === 'channelsFirst') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, biasShape[0], 1, 1]); } else { return tfc.reshape(bias, [1, biasShape[2], biasShape[0], biasShape[1]]); } } else if (dataFormat === 'channelsLast') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, 1, 1, biasShape[0]]); } else { return tfc.reshape(bias, [1].concat(biasShape)); } } } else if (xRank === 3) { if (dataFormat === 'channelsFirst') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, biasShape[0], 1]); } else { return tfc.reshape(bias, [1, biasShape[1], biasShape[0]]); } } else if (dataFormat === 'channelsLast') { if (biasShape.length === 1) { return tfc.reshape(bias, [1, 1, biasShape[0]]); } else { return tfc.reshape(bias, [1].concat(biasShape)); } } } else if (xRank < 3) { return bias; } throw new ValueError(`Unsupported input rank by biasAdd: ${bias.rank}`); } /* Neural-network operations. */ /** * Add a bias to a tensor. * * @param x The tensor to add the bias to. * @param bias The bias to add to `x`. Must be 1D or the same rank as `x`. * @return Result of the bias adding. * @throws ValueError: If the rank of `bias` is incorrect. */ function biasAdd(x, bias, dataFormat) { return tidy$1(() => { if (dataFormat == null) { dataFormat = imageDataFormat(); } checkDataFormat(dataFormat); return tfc.add(x, reshapeBias(x.rank, bias, dataFormat)); }); } /** * Exponential linear unit (ELU). * @param x A tensor or variable to compute the activation function for. * @param alpha: A scalar, a scaling factor for the negative section. * @return Output of the ELU operation. */ function elu$1(x, alpha = 1) { // TODO(cais): Add support for alpha values other than 1. if (alpha !== 1) { throw new NotImplementedError(`Support for alpha values other than 1 (${alpha}) is not implemented ` + `yet.`); } return tfc.elu(x); } /** * Softsign of a tensor. * * Defined as x / (abs(x) + 1), element-wise. * * @param x: Input. * @returns Output. */ function softsign(x) { return tidy$1(() => tfc.div(x, tfc.add(tfc.abs(x), 1))); } /** * Sets entries in `x` to zero at random, while scaling the entire tensor. * * @param x input tensor. * @param level fraction of the entries in the tensor that will be set to 0. * @param noiseShape shape of randomly generated keep/drop flags, must be * broadcastable to the shape of `x`. Optional. * @param seed random seed to ensure determinism. Optional. * @returns Result of the dropout operation. */ function dropout$1(x, level, noiseShape, seed) { return tidy$1(() => tfc.dropout(x, level, noiseShape, seed)); } /** * Element-wise, segment-wise linear approximation of sigmoid. * * Returns `0.` if `x < -2.5`, `1.` if `x > 2.5`. * In `-2.5 <= x <= 2.5`, returns `0.2 * x + 0.5`. * * @param x Input tensor. * @returns Output tensor. */ function hardSigmoid(x) { return tidy$1(() => { const y = tfc.add(.5, tfc.mul(.2, x)); return tfc.clipByValue(y, 0, 1); }); } /** * Invoke `x` in the training phase, and `alt` otherwise. * * Porting Note: We do not create placeholder tensors for the `training` * boolean flag here, because there is no such thing in the TF.js imperative * backend. * * @param x The function to invoke iff `training` is `true`. * @param alt The function to invoke iff `training` is `false`. * @param training Boolean flag for whether training phase is active. * @returns The return value of `x()` if `training` is `true`, or the return * value of `alt()` if `training` is `false`. */ function inTrainPhase(x, alt, training = false) { return training ? x() : alt(); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ const VALID_FAN_MODE_VALUES = ['fanIn', 'fanOut', 'fanAvg']; const VALID_DISTRIBUTION_VALUES = ['normal', 'uniform', 'truncatedNormal']; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function checkFanMode(value) { checkStringTypeUnionValue(VALID_FAN_MODE_VALUES, 'FanMode', value); } function checkDistribution(value) { checkStringTypeUnionValue(VALID_DISTRIBUTION_VALUES, 'Distribution', value); } /** * Initializer base class. * * @doc { * heading: 'Initializers', subheading: 'Classes', namespace: 'initializers'} */ class Initializer extends serialization.Serializable { fromConfigUsesCustomObjects() { return false; } getConfig() { return {}; } } class Zeros extends Initializer { apply(shape, dtype) { return zeros$2(shape, dtype); } } /** @nocollapse */ Zeros.className = 'Zeros'; serialization.registerClass(Zeros); class Ones extends Initializer { apply(shape, dtype) { return ones$3(shape, dtype); } } /** @nocollapse */ Ones.className = 'Ones'; serialization.registerClass(Ones); class Constant extends Initializer { constructor(args) { super(); if (typeof args !== 'object') { throw new ValueError(`Expected argument of type ConstantConfig but got ${args}`); } if (args.value === undefined) { throw new ValueError(`config must have value set but got ${args}`); } this.value = args.value; } apply(shape, dtype) { return tidy$1(() => mul$1(scalar$1(this.value), ones$3(shape, dtype))); } getConfig() { return { value: this.value, }; } } /** @nocollapse */ Constant.className = 'Constant'; serialization.registerClass(Constant); class RandomUniform extends Initializer { constructor(args) { super(); this.DEFAULT_MINVAL = -0.05; this.DEFAULT_MAXVAL = 0.05; this.minval = args.minval || this.DEFAULT_MINVAL; this.maxval = args.maxval || this.DEFAULT_MAXVAL; this.seed = args.seed; } apply(shape, dtype) { return randomUniform$1(shape, this.minval, this.maxval, dtype, this.seed); } getConfig() { return { minval: this.minval, maxval: this.maxval, seed: this.seed }; } } /** @nocollapse */ RandomUniform.className = 'RandomUniform'; serialization.registerClass(RandomUniform); class RandomNormal extends Initializer { constructor(args) { super(); this.DEFAULT_MEAN = 0.; this.DEFAULT_STDDEV = 0.05; this.mean = args.mean || this.DEFAULT_MEAN; this.stddev = args.stddev || this.DEFAULT_STDDEV; this.seed = args.seed; } apply(shape, dtype) { dtype = dtype || 'float32'; if (dtype !== 'float32' && dtype !== 'int32') { throw new NotImplementedError(`randomNormal does not support dType ${dtype}.`); } return randomNormal$1(shape, this.mean, this.stddev, dtype, this.seed); } getConfig() { return { mean: this.mean, stddev: this.stddev, seed: this.seed }; } } /** @nocollapse */ RandomNormal.className = 'RandomNormal'; serialization.registerClass(RandomNormal); class TruncatedNormal extends Initializer { constructor(args) { super(); this.DEFAULT_MEAN = 0.; this.DEFAULT_STDDEV = 0.05; this.mean = args.mean || this.DEFAULT_MEAN; this.stddev = args.stddev || this.DEFAULT_STDDEV; this.seed = args.seed; } apply(shape, dtype) { dtype = dtype || 'float32'; if (dtype !== 'float32' && dtype !== 'int32') { throw new NotImplementedError(`truncatedNormal does not support dType ${dtype}.`); } return truncatedNormal$1(shape, this.mean, this.stddev, dtype, this.seed); } getConfig() { return { mean: this.mean, stddev: this.stddev, seed: this.seed }; } } /** @nocollapse */ TruncatedNormal.className = 'TruncatedNormal'; serialization.registerClass(TruncatedNormal); let Identity$1 = class Identity extends Initializer { constructor(args) { super(); this.gain = args.gain != null ? args.gain : 1.0; } apply(shape, dtype) { return tidy$1(() => { if (shape.length !== 2 || shape[0] !== shape[1]) { throw new ValueError('Identity matrix initializer can only be used for' + ' 2D square matrices.'); } else { return mul$1(this.gain, eye(shape[0])); } }); } getConfig() { return { gain: this.gain }; } }; /** @nocollapse */ Identity$1.className = 'Identity'; serialization.registerClass(Identity$1); /** * Computes the number of input and output units for a weight shape. * @param shape Shape of weight. * @param dataFormat data format to use for convolution kernels. * Note that all kernels in Keras are standardized on the * CHANNEL_LAST ordering (even when inputs are set to CHANNEL_FIRST). * @return An length-2 array: fanIn, fanOut. */ function computeFans(shape, dataFormat = 'channelsLast') { let fanIn; let fanOut; checkDataFormat(dataFormat); if (shape.length === 2) { fanIn = shape[0]; fanOut = shape[1]; } else if ([3, 4, 5].indexOf(shape.length) !== -1) { if (dataFormat === 'channelsFirst') { const receptiveFieldSize = arrayProd(shape, 2); fanIn = shape[1] * receptiveFieldSize; fanOut = shape[0] * receptiveFieldSize; } else if (dataFormat === 'channelsLast') { const receptiveFieldSize = arrayProd(shape, 0, shape.length - 2); fanIn = shape[shape.length - 2] * receptiveFieldSize; fanOut = shape[shape.length - 1] * receptiveFieldSize; } } else { const shapeProd = arrayProd(shape); fanIn = Math.sqrt(shapeProd); fanOut = Math.sqrt(shapeProd); } return [fanIn, fanOut]; } class VarianceScaling extends Initializer { /** * Constructor of VarianceScaling. * @throws ValueError for invalid value in scale. */ constructor(args) { super(); if (args.scale < 0.0) { throw new ValueError(`scale must be a positive float. Got: ${args.scale}`); } this.scale = args.scale == null ? 1.0 : args.scale; this.mode = args.mode == null ? 'fanIn' : args.mode; checkFanMode(this.mode); this.distribution = args.distribution == null ? 'normal' : args.distribution; checkDistribution(this.distribution); this.seed = args.seed; } apply(shape, dtype) { const fans = computeFans(shape); const fanIn = fans[0]; const fanOut = fans[1]; let scale = this.scale; if (this.mode === 'fanIn') { scale /= Math.max(1, fanIn); } else if (this.mode === 'fanOut') { scale /= Math.max(1, fanOut); } else { scale /= Math.max(1, (fanIn + fanOut) / 2); } if (this.distribution === 'normal') { const stddev = Math.sqrt(scale); dtype = dtype || 'float32'; if (dtype !== 'float32' && dtype !== 'int32') { throw new NotImplementedError(`${this.getClassName()} does not support dType ${dtype}.`); } return truncatedNormal$1(shape, 0, stddev, dtype, this.seed); } else { const limit = Math.sqrt(3 * scale); return randomUniform$1(shape, -limit, limit, dtype, this.seed); } } getConfig() { return { scale: this.scale, mode: this.mode, distribution: this.distribution, seed: this.seed }; } } /** @nocollapse */ VarianceScaling.className = 'VarianceScaling'; serialization.registerClass(VarianceScaling); class GlorotUniform extends VarianceScaling { /** * Constructor of GlorotUniform * @param scale * @param mode * @param distribution * @param seed */ constructor(args) { super({ scale: 1.0, mode: 'fanAvg', distribution: 'uniform', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, GlorotUniform is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ GlorotUniform.className = 'GlorotUniform'; serialization.registerClass(GlorotUniform); class GlorotNormal extends VarianceScaling { /** * Constructor of GlorotNormal. * @param scale * @param mode * @param distribution * @param seed */ constructor(args) { super({ scale: 1.0, mode: 'fanAvg', distribution: 'normal', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, GlorotNormal is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ GlorotNormal.className = 'GlorotNormal'; serialization.registerClass(GlorotNormal); class HeNormal extends VarianceScaling { constructor(args) { super({ scale: 2.0, mode: 'fanIn', distribution: 'normal', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, HeNormal is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ HeNormal.className = 'HeNormal'; serialization.registerClass(HeNormal); class HeUniform extends VarianceScaling { constructor(args) { super({ scale: 2.0, mode: 'fanIn', distribution: 'uniform', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, HeUniform is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ HeUniform.className = 'HeUniform'; serialization.registerClass(HeUniform); class LeCunNormal extends VarianceScaling { constructor(args) { super({ scale: 1.0, mode: 'fanIn', distribution: 'normal', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, LeCunNormal is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ LeCunNormal.className = 'LeCunNormal'; serialization.registerClass(LeCunNormal); class LeCunUniform extends VarianceScaling { constructor(args) { super({ scale: 1.0, mode: 'fanIn', distribution: 'uniform', seed: args == null ? null : args.seed }); } getClassName() { // In Python Keras, LeCunUniform is not a class, but a helper method // that creates a VarianceScaling object. Use 'VarianceScaling' as // class name to be compatible with that. return VarianceScaling.className; } } /** @nocollapse */ LeCunUniform.className = 'LeCunUniform'; serialization.registerClass(LeCunUniform); class Orthogonal extends Initializer { constructor(args) { super(); this.DEFAULT_GAIN = 1; this.ELEMENTS_WARN_SLOW = 2000; this.gain = args.gain == null ? this.DEFAULT_GAIN : args.gain; this.seed = args.seed; } apply(shape, dtype) { return tidy$1(() => { if (shape.length < 2) { throw new NotImplementedError('Shape must be at least 2D.'); } if (dtype !== 'int32' && dtype !== 'float32' && dtype !== undefined) { throw new TypeError(`Unsupported data type ${dtype}.`); } dtype = dtype; // flatten the input shape with the last dimension remaining its // original shape so it works for conv2d const numRows = util.sizeFromShape(shape.slice(0, -1)); const numCols = shape[shape.length - 1]; const numElements = numRows * numCols; if (numElements > this.ELEMENTS_WARN_SLOW) { console.warn(`Orthogonal initializer is being called on a matrix with more ` + `than ${this.ELEMENTS_WARN_SLOW} (${numElements}) elements: ` + `Slowness may result.`); } const flatShape = [Math.max(numCols, numRows), Math.min(numCols, numRows)]; // Generate a random matrix const randNormalMat = randomNormal$1(flatShape, 0, 1, dtype, this.seed); // Compute QR factorization const qr = linalg.qr(randNormalMat, false); let qMat = qr[0]; const rMat = qr[1]; // Make Q uniform const diag = rMat.flatten().stridedSlice([0], [Math.min(numCols, numRows) * Math.min(numCols, numRows)], [Math.min(numCols, numRows) + 1]); qMat = mul$1(qMat, diag.sign()); if (numRows < numCols) { qMat = qMat.transpose(); } return mul$1(scalar$1(this.gain), qMat.reshape(shape)); }); } getConfig() { return { gain: this.gain, seed: this.seed, }; } } /** @nocollapse */ Orthogonal.className = 'Orthogonal'; serialization.registerClass(Orthogonal); // Maps the JavaScript-like identifier keys to the corresponding registry // symbols. const INITIALIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP = { 'constant': 'Constant', 'glorotNormal': 'GlorotNormal', 'glorotUniform': 'GlorotUniform', 'heNormal': 'HeNormal', 'heUniform': 'HeUniform', 'identity': 'Identity', 'leCunNormal': 'LeCunNormal', 'leCunUniform': 'LeCunUniform', 'ones': 'Ones', 'orthogonal': 'Orthogonal', 'randomNormal': 'RandomNormal', 'randomUniform': 'RandomUniform', 'truncatedNormal': 'TruncatedNormal', 'varianceScaling': 'VarianceScaling', 'zeros': 'Zeros' }; function deserializeInitializer(config, customObjects = {}) { return deserializeKerasObject(config, serialization.SerializationMap.getMap().classNameMap, customObjects, 'initializer'); } function serializeInitializer(initializer) { return serializeKerasObject(initializer); } function getInitializer(identifier) { if (typeof identifier === 'string') { const className = identifier in INITIALIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP ? INITIALIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP[identifier] : identifier; /* We have four 'helper' classes for common initializers that all get serialized as 'VarianceScaling' and shouldn't go through the deserializeInitializer pathway. */ if (className === 'GlorotNormal') { return new GlorotNormal(); } else if (className === 'GlorotUniform') { return new GlorotUniform(); } else if (className === 'HeNormal') { return new HeNormal(); } else if (className === 'HeUniform') { return new HeUniform(); } else if (className === 'LeCunNormal') { return new LeCunNormal(); } else if (className === 'LeCunUniform') { return new LeCunUniform(); } else { const config = {}; config['className'] = className; config['config'] = {}; return deserializeInitializer(config); } } else if (identifier instanceof Initializer) { return identifier; } else { return deserializeInitializer(identifier); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // tslint:enable /** * Determine whether the input is an Array of Shapes. */ function isArrayOfShapes(x) { return Array.isArray(x) && Array.isArray(x[0]); } /** * Special case of normalizing shapes to lists. * * @param x A shape or list of shapes to normalize into a list of Shapes. * @return A list of Shapes. */ function normalizeShapeList(x) { if (x.length === 0) { return []; } if (!Array.isArray(x[0])) { return [x]; } return x; } /** * Helper function to obtain exactly one Tensor. * @param xs: A single `tf.Tensor` or an `Array` of `tf.Tensor`s. * @return A single `tf.Tensor`. If `xs` is an `Array`, return the first one. * @throws ValueError: If `xs` is an `Array` and its length is not 1. */ function getExactlyOneTensor(xs) { let x; if (Array.isArray(xs)) { if (xs.length !== 1) { throw new ValueError(`Expected Tensor length to be 1; got ${xs.length}`); } x = xs[0]; } else { x = xs; } return x; } /** * Helper function to obtain exactly on instance of Shape. * * @param shapes Input single `Shape` or Array of `Shape`s. * @returns If input is a single `Shape`, return it unchanged. If the input is * an `Array` containing exactly one instance of `Shape`, return the instance. * Otherwise, throw a `ValueError`. * @throws ValueError: If input is an `Array` of `Shape`s, and its length is not * 1. */ function getExactlyOneShape(shapes) { if (Array.isArray(shapes) && Array.isArray(shapes[0])) { if (shapes.length === 1) { shapes = shapes; return shapes[0]; } else { throw new ValueError(`Expected exactly 1 Shape; got ${shapes.length}`); } } else { return shapes; } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Count the elements in an Array of LayerVariables. * * @param weights: The LayerVariables of which the constituent numbers are to * be counted. * @returns A count of the elements in all the LayerVariables */ function countParamsInWeights(weights) { let count = 0; for (const weight of weights) { if (weight.shape.length === 0) { count += 1; } else { count += weight.shape.reduce((a, b) => a * b); } } return count; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ const DEFAULT_VARIABLE_NAME_PREFIX = 'Variable'; /** * A `tf.layers.LayerVariable` is similar to a `tf.Tensor` in that it has a * dtype and shape, but its value is mutable. The value is itself represented * as a`tf.Tensor`, and can be read with the `read()` method and updated with * the `write()` method. */ class LayerVariable { /** * Construct Variable from a `tf.Tensor`. * * If not explicitly named, the Variable will be given a name with the * prefix 'Variable'. Variable names are unique. In the case of name * collision, suffixies '_' will be added to the name. * * @param val Initial value of the Variable. * @param name Name of the variable. If `null` or `undefined` is provided, it * will default a name with the prefix 'Variable'. * @param constraint Optional, projection function to be applied to the * variable after optimize updates * @throws ValueError if `name` is `null` or `undefined`. */ constructor(val, dtype = 'float32', name = DEFAULT_VARIABLE_NAME_PREFIX, trainable = true, constraint = null) { this.dtype = dtype == null ? 'float32' : dtype; this.shape = val.shape; this.id = getNextUniqueTensorId(); name = name == null ? DEFAULT_VARIABLE_NAME_PREFIX : name; this.originalName = getScopedTensorName(name); this.name = getUniqueTensorName(this.originalName); this.trainable_ = trainable; this.constraint = constraint; this.val = tfc.variable(val, this.trainable_, this.name, this.dtype); } /** * Get a snapshot of the Variable's value. * * The returned value is a snapshot of the Variable's value at the time of * the invocation. Future mutations in the value of the tensor will only * be reflected by future calls to this method. */ read() { this.assertNotDisposed(); return this.val; } /** * Update the value of the Variable. * * @param newVal: The new value to update to. Must be consistent with the * dtype and shape of the Variable. * @return This Variable. */ write(newVal) { // TODO(cais): Once TF.js Core supports Tensor.dtype, check dtype match. this.assertNotDisposed(); checkShapesMatch(this.val, newVal); // Skip updating if this is the exact same tensor. if (this.val.id !== newVal.id) { this.val.assign(newVal); if (this.constraint != null) { this.val.assign(this.constraint.apply(this.val)); } } return this; } /** * Dispose this LayersVariable instance from memory. */ dispose() { this.assertNotDisposed(); this.val.dispose(); } assertNotDisposed() { if (this.val.isDisposed) { throw new Error(`LayersVariable ${this.name} is already disposed.`); } } get trainable() { return this.trainable_; } set trainable(trainable) { this.trainable_ = trainable; this.val.trainable = trainable; } } function checkShapesMatch(x, y) { if (x.shape.toString() !== y.shape.toString()) { throw new Error('Shape mismatch: ' + JSON.stringify(x.shape) + ' vs. ' + JSON.stringify(y.shape)); } } /** * Get the values of an array of Variables. * * @param tensors An `Array` of `Variable`s to get the values of. * @return The values of the inputs, as an `Array` of`tf.Tensor`s. */ function batchGetValue(xs) { return xs.map(x => x.read()); } /** * Update the value of multiple Variables at once. * * @param variablesAndValues An `Array`, each element is of type * [Variable, Tensor]. The first item is the * `Variable` of which the value is to be updated. The second item * carries the new value. */ function batchSetValue(variablesAndValues) { variablesAndValues.forEach(variableAndValue => { const variable = variableAndValue[0]; variable.write(variableAndValue[1]); }); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Specifies the ndim, dtype and shape of every input to a layer. * * Every layer should expose (if appropriate) an `inputSpec` attribute: * a list of instances of InputSpec (one per input tensor). * * A null entry in a shape is compatible with any dimension, * a null shape is compatible with any shape. */ class InputSpec { constructor(args) { this.dtype = args.dtype; this.shape = args.shape; /* TODO(michaelterry): Could throw error if ndim and shape are both defined (then backport). */ if (args.shape != null) { this.ndim = args.shape.length; } else { this.ndim = args.ndim; } this.maxNDim = args.maxNDim; this.minNDim = args.minNDim; this.axes = args.axes || {}; } } /** * `tf.SymbolicTensor` is a placeholder for a Tensor without any concrete value. * * They are most often encountered when building a graph of `Layer`s for a * `tf.LayersModel` and the input data's shape, but not values are known. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ class SymbolicTensor { /** * * @param dtype * @param shape * @param sourceLayer The Layer that produced this symbolic tensor. * @param inputs The inputs passed to sourceLayer's __call__() method. * @param nodeIndex * @param tensorIndex * @param callArgs The keyword arguments passed to the __call__() method. * @param name * @param outputTensorIndex The index of this tensor in the list of outputs * returned by apply(). */ constructor(dtype, shape, sourceLayer, inputs, callArgs, name, outputTensorIndex) { this.dtype = dtype; this.shape = shape; this.sourceLayer = sourceLayer; this.inputs = inputs; this.callArgs = callArgs; this.outputTensorIndex = outputTensorIndex; this.id = getNextUniqueTensorId(); if (name != null) { this.originalName = getScopedTensorName(name); this.name = getUniqueTensorName(this.originalName); } this.rank = shape.length; } } let _nextNodeID = 0; /** * A `Node` describes the connectivity between two layers. * * Each time a layer is connected to some new input, * a node is added to `layer.inboundNodes`. * * Each time the output of a layer is used by another layer, * a node is added to `layer.outboundNodes`. * * `nodeIndices` and `tensorIndices` are basically fine-grained coordinates * describing the origin of the `inputTensors`, verifying the following: * * `inputTensors[i] == * inboundLayers[i].inboundNodes[nodeIndices[i]].outputTensors[ * tensorIndices[i]]` * * A node from layer A to layer B is added to: * A.outboundNodes * B.inboundNodes */ class Node { constructor(args, // TODO(michaelterry): Define actual type for this. callArgs) { this.callArgs = callArgs; this.id = _nextNodeID++; /* Layer instance (NOT a list). this is the layer that takes a list of input tensors and turns them into a list of output tensors. the current node will be added to the inboundNodes of outboundLayer. */ this.outboundLayer = args.outboundLayer; /* The following 3 properties describe where the input tensors come from: which layers, and for each layer, which node and which tensor output of each node. */ // List of layer instances. this.inboundLayers = args.inboundLayers; // List of integers, 1:1 mapping with inboundLayers. this.nodeIndices = args.nodeIndices; // List of integers, 1:1 mapping with inboundLayers. this.tensorIndices = args.tensorIndices; /* Following 2 properties: tensor inputs and outputs of outboundLayer. */ // List of tensors. 1:1 mapping with inboundLayers. this.inputTensors = args.inputTensors; // List of tensors, created by outboundLayer.call(). this.outputTensors = args.outputTensors; /* Following 2 properties: input and output masks. List of tensors, 1:1 mapping with inputTensor. */ this.inputMasks = args.inputMasks; // List of tensors, created by outboundLayer.computeMask(). this.outputMasks = args.outputMasks; // Following 2 properties: input and output shapes. // List of shape tuples, shapes of inputTensors. this.inputShapes = args.inputShapes; // List of shape tuples, shapes of outputTensors. this.outputShapes = args.outputShapes; // Add nodes to all layers involved. for (const layer of args.inboundLayers) { if (layer != null) { layer.outboundNodes.push(this); } } args.outboundLayer.inboundNodes.push(this); } getConfig() { const inboundNames = []; for (const layer of this.inboundLayers) { if (layer != null) { inboundNames.push(layer.name); } else { inboundNames.push(null); } } return { outboundLayer: this.outboundLayer ? this.outboundLayer.name : null, inboundLayers: inboundNames, nodeIndices: this.nodeIndices, tensorIndices: this.tensorIndices }; } } let _nextLayerID = 0; /** * A layer is a grouping of operations and weights that can be composed to * create a `tf.LayersModel`. * * Layers are constructed by using the functions under the * [tf.layers](#Layers-Basic) namespace. * * @doc {heading: 'Layers', subheading: 'Classes', namespace: 'layers'} */ class Layer extends serialization.Serializable { constructor(args = {}) { super(); this._callHook = null; this._addedWeightNames = []; // Porting Notes: PyKeras does not have this property in this base Layer // class. Instead lets Layer subclass set it dynamically and checks the // value with `hasattr`. In tfjs-layers, we let this be a member of this // base class. this._stateful = false; this.id = _nextLayerID++; this.activityRegularizer = null; this.inputSpec = null; this.supportsMasking = false; // These properties will be set upon call of this.build() this._trainableWeights = []; this._nonTrainableWeights = []; this._losses = []; this._updates = []; this._built = false; /* These lists will be filled via successive calls to this.addInboundNode(). */ this.inboundNodes = []; this.outboundNodes = []; let name = args.name; if (!name) { const prefix = this.getClassName(); name = toSnakeCase(prefix) + '_' + getUid(prefix); } this.name = name; this.trainable_ = args.trainable == null ? true : args.trainable; if (args.inputShape != null || args.batchInputShape != null) { /* In this case we will later create an input layer to insert before the current layer */ let batchInputShape; if (args.batchInputShape != null) { batchInputShape = args.batchInputShape; } else if (args.inputShape != null) { let batchSize = null; if (args.batchSize != null) { batchSize = args.batchSize; } batchInputShape = [batchSize].concat(args.inputShape); } this.batchInputShape = batchInputShape; // Set dtype. let dtype = args.dtype; if (dtype == null) { dtype = args.inputDType; } if (dtype == null) { dtype = 'float32'; } this.dtype = dtype; } if (args.weights != null) { this.initialWeights = args.weights; } else { this.initialWeights = null; } // The value of `_refCount` is initialized to null. When the layer is used // in a symbolic way for the first time, it will be set to 1. this._refCount = null; this.fastWeightInitDuringBuild = false; } /** * Converts a layer and its index to a unique (immutable type) name. * This function is used internally with `this.containerNodes`. * @param layer The layer. * @param nodeIndex The layer's position (e.g. via enumerate) in a list of * nodes. * * @returns The unique name. */ static nodeKey(layer, nodeIndex) { return layer.name + '_ib-' + nodeIndex.toString(); } /** * Returns this.inboundNode at index nodeIndex. * * Porting note: This is a replacement for _get_node_attribute_at_index() * @param nodeIndex * @param attrName The name of the attribute related to request for this node. */ getNodeAtIndex(nodeIndex, attrName) { if (this.inboundNodes.length === 0) { throw new RuntimeError('The layer has never been called ' + `and thus has no defined ${attrName}.`); } if (this.inboundNodes.length <= nodeIndex) { throw new ValueError(`Asked to get ${attrName} at node ${nodeIndex}, ` + `but the layer has only ${this.inboundNodes.length} inbound nodes.`); } return this.inboundNodes[nodeIndex]; } /** * Retrieves the input tensor(s) of a layer at a given node. * * @param nodeIndex Integer, index of the node from which to retrieve the * attribute. E.g. `nodeIndex=0` will correspond to the first time the layer * was called. * * @return A tensor (or list of tensors if the layer has multiple inputs). */ getInputAt(nodeIndex) { return singletonOrArray(this.getNodeAtIndex(nodeIndex, 'input').inputTensors); } /** * Retrieves the output tensor(s) of a layer at a given node. * * @param nodeIndex Integer, index of the node from which to retrieve the * attribute. E.g. `nodeIndex=0` will correspond to the first time the layer * was called. * * @return A tensor (or list of tensors if the layer has multiple outputs). */ getOutputAt(nodeIndex) { return singletonOrArray(this.getNodeAtIndex(nodeIndex, 'output').outputTensors); } // Properties /** * Retrieves the input tensor(s) of a layer. * * Only applicable if the layer has exactly one inbound node, * i.e. if it is connected to one incoming layer. * * @return Input tensor or list of input tensors. * * @exception AttributeError if the layer is connected to more than one * incoming layers. */ get input() { if (this.inboundNodes.length > 1) { throw new AttributeError(`Layer ${this.name}` + ' has multiple inbound nodes, ' + 'hence the notion of "layer input" ' + 'is ill-defined. ' + 'Use `getInputAt(nodeIndex)` instead.'); } else if (this.inboundNodes.length === 0) { throw new AttributeError(`Layer ${this.name}` + ' is not connected, no input to return.'); } return singletonOrArray(this.getNodeAtIndex(0, 'input').inputTensors); } /** * Retrieves the output tensor(s) of a layer. * * Only applicable if the layer has exactly one inbound node, * i.e. if it is connected to one incoming layer. * * @return Output tensor or list of output tensors. * * @exception AttributeError if the layer is connected to more than one * incoming layers. */ get output() { if (this.inboundNodes.length === 0) { throw new AttributeError(`Layer ${this.name}` + ' has no inbound nodes.'); } if (this.inboundNodes.length > 1) { throw new AttributeError(`Layer ${this.name}` + ' has multiple inbound nodes, ' + 'hence the notion of "layer output" ' + 'is ill-defined. ' + 'Use `getOutputAt(nodeIndex)` instead.'); } return singletonOrArray(this.getNodeAtIndex(0, 'output').outputTensors); } get losses() { return this._losses; } /** * Retrieves the Layer's current loss values. * * Used for regularizers during training. */ calculateLosses() { // Porting Node: This is an augmentation to Layer.loss in PyKeras. // In PyKeras, Layer.loss returns symbolic tensors. Here a concrete // Tensor (specifically Scalar) values are returned. This is due to the // imperative backend. return this.losses.map(lossFn => lossFn()); } get updates() { return this._updates; } get built() { return this._built; } set built(built) { this._built = built; } get trainable() { return this.trainable_; } set trainable(trainable) { this._trainableWeights.forEach(w => w.trainable = trainable); this.trainable_ = trainable; } get trainableWeights() { if (this.trainable_) { return this._trainableWeights.filter(w => w.trainable); } else { return []; } } set trainableWeights(weights) { this._trainableWeights = weights; } get nonTrainableWeights() { if (this.trainable) { return this._trainableWeights.filter(w => !w.trainable) .concat(this._nonTrainableWeights); } else { return this._trainableWeights.concat(this._nonTrainableWeights); } } set nonTrainableWeights(weights) { this._nonTrainableWeights = weights; } /** * The concatenation of the lists trainableWeights and nonTrainableWeights * (in this order). */ get weights() { return this.trainableWeights.concat(this.nonTrainableWeights); } get stateful() { return this._stateful; } /** * Reset the states of the layer. * * This method of the base Layer class is essentially a no-op. * Subclasses that are stateful (e.g., stateful RNNs) should override this * method. */ resetStates() { if (!this.stateful) { throw new Error('Cannot call the resetStates() method of a non-stateful Layer ' + 'object.'); } } /** * Checks compatibility between the layer and provided inputs. * * This checks that the tensor(s) `input` * verify the input assumptions of the layer * (if any). If not, exceptions are raised. * * @param inputs Input tensor or list of input tensors. * * @exception ValueError in case of mismatch between * the provided inputs and the expectations of the layer. */ assertInputCompatibility(inputs) { const inputsList = toList(inputs); if (this.inputSpec == null || this.inputSpec.length === 0) { return; } const inputSpec = toList(this.inputSpec); if (inputsList.length !== inputSpec.length) { throw new ValueError(`Layer ${this.name} expects ${inputSpec.length} inputs, ` + `but it received ${inputsList.length} input tensors. ` + `Input received: ${inputs}`); } for (let inputIndex = 0; inputIndex < inputsList.length; inputIndex++) { const x = inputsList[inputIndex]; const spec = inputSpec[inputIndex]; if (spec == null) { continue; } // Check ndim. const ndim = x.rank; if (spec.ndim != null) { if (ndim !== spec.ndim) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ${this.name}: ` + `expected ndim=${spec.ndim}, found ndim=${ndim}`); } } if (spec.maxNDim != null) { if (ndim > spec.maxNDim) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ${this.name}` + `: expected max_ndim=${spec.maxNDim}, found ndim=${ndim}`); } } if (spec.minNDim != null) { if (ndim < spec.minNDim) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ${this.name}` + `: expected min_ndim=${spec.minNDim}, found ndim=${ndim}.`); } } // Check dtype. if (spec.dtype != null) { if (x.dtype !== spec.dtype) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ${this.name} ` + `: expected dtype=${spec.dtype}, found dtype=${x.dtype}.`); } } // Check specific shape axes. if (spec.axes) { const xShape = x.shape; for (const key in spec.axes) { const axis = Number(key); const value = spec.axes[key]; // Perform Python-style slicing in case axis < 0; // TODO(cais): Use https://github.com/alvivi/typescript-underscore to // ensure type safety through Underscore calls. const xShapeAtAxis = axis >= 0 ? xShape[axis] : xShape[xShape.length + axis]; if (value != null && [value, null].indexOf(xShapeAtAxis) === -1) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ` + `${this.name}: expected axis ${axis} of input shape to ` + `have value ${value} but got shape ${xShape}.`); } } } // Check shape. if (spec.shape != null) { for (let i = 0; i < spec.shape.length; ++i) { const specDim = spec.shape[i]; const dim = x.shape[i]; if (specDim != null && dim != null) { if (specDim !== dim) { throw new ValueError(`Input ${inputIndex} is incompatible with layer ` + `${this.name}: expected shape=${spec.shape}, ` + `found shape=${x.shape}.`); } } } } } } /** * This is where the layer's logic lives. * * @param inputs Input tensor, or list/tuple of input tensors. * @param kwargs Additional keyword arguments. * * @return A tensor or list/tuple of tensors. */ call(inputs, kwargs) { return inputs; } invokeCallHook(inputs, kwargs) { if (this._callHook != null) { this._callHook(inputs, kwargs); } } /** * Set call hook. * This is currently used for testing only. * @param callHook */ setCallHook(callHook) { this._callHook = callHook; } /** * Clear call hook. * This is currently used for testing only. */ clearCallHook() { this._callHook = null; } /** * Builds or executes a `Layer`'s logic. * * When called with `tf.Tensor`(s), execute the `Layer`'s computation and * return Tensor(s). For example: * * ```js * const denseLayer = tf.layers.dense({ * units: 1, * kernelInitializer: 'zeros', * useBias: false * }); * * // Invoke the layer's apply() method with a `tf.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.SymbolicTensor`s are typically used during the * building of non-`tf.Sequential` models. For example: * * ```js * 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 can be used to construct a model that consists * // of the flatten and dense layers. * const model = tf.model({inputs: input, outputs: output2}); * ``` * * @param inputs a `tf.Tensor` or `tf.SymbolicTensor` or an Array of them. * @param kwargs Additional keyword arguments to be passed to `call()`. * * @return Output of the layer's `call` method. * * @exception ValueError error in case the layer is missing shape information * for its `build` call. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ // Porting Note: This is a replacement for __call__() in Python. apply(inputs, kwargs) { kwargs = kwargs || {}; this.assertNotDisposed(); // Ensure inputs are all the same type. const inputsList = toList(inputs); const allAreSymbolic = checkAllSymbolic(inputs); const noneAreSymbolic = checkNoneSymbolic(inputs); if (allAreSymbolic === noneAreSymbolic) { throw new ValueError('Arguments to apply() must be all ' + 'SymbolicTensors or all Tensors'); } // TODO(michaelterry): nameScope() may not be necessary. return nameScope(this.name, () => { // Handle laying building (weight creating, input spec locking). if (!this.built) { /* Throw exceptions in case the input is not compatible with the inputSpec specified in the layer constructor. */ this.assertInputCompatibility(inputs); // Collect input shapes to build layer. const inputShapes = []; for (const xElem of toList(inputs)) { inputShapes.push(xElem.shape); } this.build(singletonOrArray(inputShapes)); this.built = true; // Load weights that were specified at layer instantiation. if (this.initialWeights) { this.setWeights(this.initialWeights); } if (this._refCount === null && noneAreSymbolic) { // The first use of this layer is a non-symbolic call, set ref count // to 1 so the Layer can be properly disposed if its dispose() method // is called. this._refCount = 1; } } /* Throw exceptions in case the input is not compatible with the inputSpec set at build time. */ this.assertInputCompatibility(inputs); // Handle mask propagation. // TODO(michaelterry): Mask propagation not currently implemented. // Actually call the layer, collecting output(s), mask(s), and shape(s). if (noneAreSymbolic) { let output = this.call(inputs, kwargs); // Apply masks to the output tensors if the layer supports it. if (this.supportsMasking) { // TODO(mattsoulanille): pass the input tensors' masks to computeMask this.setMaskMetadata(inputs, output); } // If the layer returns tensors from its inputs, unmodified, // we copy them to avoid loss of tensor metadata. const outputList = toList(output); const outputListCopy = []; // TODO(michaelterry): This copying may not be necessary given our eager // backend. for (let x of outputList) { if (inputsList.indexOf(x) !== -1) { x = x.clone(); } outputListCopy.push(x); } output = singletonOrArray(outputListCopy); if (this.activityRegularizer != null) { throw new NotImplementedError('Layer invocation in the presence of activity ' + 'regularizer(s) is not supported yet.'); } // TODO(michaelterry): Call addInboundNode()? return output; } else { const inputShape = collectInputShape(inputs); const outputShape = this.computeOutputShape(inputShape); let output; const outputDType = guessOutputDType(inputs); this.warnOnIncompatibleInputShape(Array.isArray(inputs) ? inputShape[0] : inputShape); if (outputShape != null && outputShape.length > 0 && Array.isArray(outputShape[0])) { // We have multiple output shapes. Create multiple output tensors. output = outputShape .map((shape, index) => new SymbolicTensor(outputDType, shape, this, toList(inputs), kwargs, this.name, index)); } else { output = new SymbolicTensor(outputDType, outputShape, this, toList(inputs), kwargs, this.name); } /* Add an inbound node to the layer, so that it keeps track of the call and of all new variables created during the call. This also updates the layer history of the output tensor(s). If the input tensor(s) had no previous history, this does nothing. */ this.addInboundNode(inputs, output, null, null, inputShape, outputShape, kwargs); this._refCount++; if (this.activityRegularizer != null) { throw new NotImplementedError('Layer invocation in the presence of activity ' + 'regularizer(s) is not supported yet.'); } return output; } }); } /** * Check compatibility between input shape and this layer's batchInputShape. * * Print warning if any incompatibility is found. * * @param inputShape Input shape to be checked. */ warnOnIncompatibleInputShape(inputShape) { if (this.batchInputShape == null) { return; } else if (inputShape.length !== this.batchInputShape.length) { console.warn(`The rank of the input tensor provided (shape: ` + `${JSON.stringify(inputShape)}) does not match that of the ` + `batchInputShape (${JSON.stringify(this.batchInputShape)}) ` + `of the layer ${this.name}`); } else { let dimMismatch = false; this.batchInputShape.forEach((dimension, i) => { if (dimension != null && inputShape[i] != null && inputShape[i] !== dimension) { dimMismatch = true; } }); if (dimMismatch) { console.warn(`The shape of the input tensor ` + `(${JSON.stringify(inputShape)}) does not ` + `match the expectation of layer ${this.name}: ` + `${JSON.stringify(this.batchInputShape)}`); } } } /** * Retrieves the output shape(s) of a layer. * * Only applicable if the layer has only one inbound node, or if all inbound * nodes have the same output shape. * * @returns Output shape or shapes. * @throws AttributeError: if the layer is connected to more than one incoming * nodes. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ get outputShape() { if (this.inboundNodes == null || this.inboundNodes.length === 0) { throw new AttributeError(`The layer ${this.name} has never been called and thus has no ` + `defined output shape.`); } const allOutputShapes = []; for (const node of this.inboundNodes) { const shapeString = JSON.stringify(node.outputShapes); if (allOutputShapes.indexOf(shapeString) === -1) { allOutputShapes.push(shapeString); } } if (allOutputShapes.length === 1) { const outputShapes = this.inboundNodes[0].outputShapes; if (Array.isArray(outputShapes) && Array.isArray(outputShapes[0]) && outputShapes.length === 1) { return outputShapes[0]; } else { return outputShapes; } } else { throw new AttributeError(`The layer ${this.name} has multiple inbound nodes with different ` + `output shapes. Hence the notion of "output shape" is ill-defined ` + `for the layer.`); // TODO(cais): Implement getOutputShapeAt(). } } /** * Counts the total number of numbers (e.g., float32, int32) in the * weights. * * @returns An integer count. * @throws RuntimeError: If the layer is not built yet (in which case its * weights are not defined yet.) * * @doc {heading: 'Models', 'subheading': 'Classes'} */ countParams() { if (!this.built) { throw new RuntimeError(`You tried to call countParams() on ${this.name}, ` + `but the layer is not built yet. Build it first by calling ` + `build(batchInputShape).`); } return countParamsInWeights(this.weights); } /** * Creates the layer weights. * * Must be implemented on all layers that have weights. * * Called when apply() is called to construct the weights. * * @param inputShape A `Shape` or array of `Shape` (unused). * * @doc {heading: 'Models', 'subheading': 'Classes'} */ build(inputShape) { this.built = true; } /** * Returns the current values of the weights of the layer. * * @param trainableOnly Whether to get the values of only trainable weights. * @returns Weight values as an `Array` of `tf.Tensor`s. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ getWeights(trainableOnly = false) { return batchGetValue(trainableOnly ? this.trainableWeights : this.weights); } /** * Sets the weights of the layer, from Tensors. * * @param weights a list of Tensors. The number of arrays and their shape * must match number of the dimensions of the weights of the layer (i.e. * it should match the output of `getWeights`). * * @exception ValueError If the provided weights list does not match the * layer's specifications. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ setWeights(weights) { tidy$1(() => { const params = this.weights; if (params.length !== weights.length) { // TODO(cais): Restore the following and use `providedWeights`, instead // of `weights` in the error message, once the deeplearn.js bug is // fixed: https://github.com/PAIR-code/deeplearnjs/issues/498 const // providedWeights = JSON.stringify(weights).slice(0, 50); throw new ValueError(`You called setWeights(weights) on layer "${this.name}" ` + `with a weight list of length ${weights.length}, ` + `but the layer was expecting ${params.length} weights. ` + `Provided weights: ${weights}...`); } if (params.length === 0) { return; } const weightValueTuples = []; const paramValues = batchGetValue(params); for (let i = 0; i < paramValues.length; ++i) { const pv = paramValues[i]; const p = params[i]; const w = weights[i]; if (!util.arraysEqual(pv.shape, w.shape)) { throw new ValueError(`Layer weight shape ${pv.shape} ` + `not compatible with provided weight shape ${w.shape}`); } weightValueTuples.push([p, w]); } batchSetValue(weightValueTuples); }); } /** * Adds a weight variable to the layer. * * @param name Name of the new weight variable. * @param shape The shape of the weight. * @param dtype The dtype of the weight. * @param initializer An initializer instance. * @param regularizer A regularizer instance. * @param trainable Whether the weight should be trained via backprop or not * (assuming that the layer itself is also trainable). * @param constraint An optional trainable. * @return The created weight variable. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ addWeight(name, shape, dtype, initializer, regularizer, trainable, constraint, getInitializerFunc) { // Reject duplicate weight names. if (this._addedWeightNames.indexOf(name) !== -1) { throw new ValueError(`Duplicate weight name ${name} for layer ${this.name}`); } this._addedWeightNames.push(name); if (dtype == null) { dtype = 'float32'; } if (this.fastWeightInitDuringBuild) { initializer = getInitializerFunc != null ? getInitializerFunc() : getInitializer('zeros'); } const initValue = initializer.apply(shape, dtype); const weight = new LayerVariable(initValue, dtype, name, trainable, constraint); initValue.dispose(); // Request backend not to dispose the weights of the model on scope() exit. if (regularizer != null) { this.addLoss(() => regularizer.apply(weight.read())); } if (trainable == null) { trainable = true; } if (trainable) { this._trainableWeights.push(weight); } else { this._nonTrainableWeights.push(weight); } return weight; } /** * Set the fast-weight-initialization flag. * * In cases where the initialized weight values will be immediately * overwritten by loaded weight values during model loading, setting * the flag to `true` saves unnecessary calls to potentially expensive * initializers and speeds up the loading process. * * @param value Target value of the flag. */ setFastWeightInitDuringBuild(value) { this.fastWeightInitDuringBuild = value; } /** * Add losses to the layer. * * The loss may potentially be conditional on some inputs tensors, * for instance activity losses are conditional on the layer's inputs. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ addLoss(losses) { if (losses == null || Array.isArray(losses) && losses.length === 0) { return; } // Update this.losses losses = toList(losses); if (this._losses !== undefined && this._losses !== null) { this.losses.push(...losses); } } /** * Computes the output shape of the layer. * * Assumes that the layer will be built to match that input shape provided. * * @param inputShape A shape (tuple of integers) or a list of shape tuples * (one per output tensor of the layer). Shape tuples can include null for * free dimensions, instead of an integer. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ computeOutputShape(inputShape) { return inputShape; } /** * Computes an output mask tensor. * * @param inputs Tensor or list of tensors. * @param mask Tensor or list of tensors. * * @return null or a tensor (or list of tensors, one per output tensor of the * layer). */ computeMask(inputs, mask) { if (!this.supportsMasking) { if (mask != null) { if (Array.isArray(mask)) { mask.forEach(maskElement => { if (maskElement != null) { throw new TypeError(`Layer ${this.name} does not support masking, ` + 'but was passed an inputMask.'); } }); } else { throw new TypeError(`Layer ${this.name} does not support masking, ` + 'but was passed an inputMask.'); } } // masking not explicitly supported: return null as mask return null; } // if masking is explictly supported, by default // carry over the input mask return mask; } setMaskMetadata(inputs, outputs, previousMask) { if (!this.supportsMasking) { return; } const outputMasks = this.computeMask(inputs, previousMask); const outputsList = toList(outputs); const outputMasksList = toList(outputMasks); if (outputsList.length !== outputMasksList.length) { throw new Error(`${this.name} outputs ${outputsList.length} tensors ` + `but ${outputsList.length} masks for those tensors`); } for (let i = 0; i < outputsList.length; i++) { outputsList[i].kerasMask = outputMasksList[i]; } } /** * Internal method to create an inbound node for the layer. * * @param inputTensors List of input tensors. * @param outputTensors List of output tensors. * @param inputMasks List of input masks (a mask can be a tensor, or null). * @param outputMasks List of output masks (a mask can be a tensor, or null). * @param inputShapes List of input shape tuples. * @param outputShapes List of output shape tuples. * @param kwargs Dictionary of keyword arguments that were passed to the * `call` method of the layer at the call that created the node. */ addInboundNode(inputTensors, outputTensors, inputMasks, outputMasks, inputShapes, outputShapes, kwargs = null) { const inputTensorList = toList(inputTensors); outputTensors = toList(outputTensors); inputMasks = toList(inputMasks); outputMasks = toList(outputMasks); inputShapes = normalizeShapeList(inputShapes); outputShapes = normalizeShapeList(outputShapes); // Collect input tensor(s) coordinates. const inboundLayers = []; const nodeIndices = []; const tensorIndices = []; for (const x of inputTensorList) { /* * TODO(michaelterry): Keras adds this value to tensors; it's not * clear whether we'll use this or not. */ inboundLayers.push(x.sourceLayer); nodeIndices.push(x.nodeIndex); tensorIndices.push(x.tensorIndex); } // Create node, add it to inbound nodes. // (This call has side effects.) // tslint:disable-next-line:no-unused-expression new Node({ outboundLayer: this, inboundLayers, nodeIndices, tensorIndices, inputTensors: inputTensorList, outputTensors, inputMasks, outputMasks, inputShapes, outputShapes }, kwargs); // Update tensor history for (let i = 0; i < outputTensors.length; i++) { // TODO(michaelterry: _uses_learning_phase not tracked. outputTensors[i].sourceLayer = this; outputTensors[i].nodeIndex = this.inboundNodes.length - 1; outputTensors[i].tensorIndex = i; } } /** * Returns the config of the layer. * * A layer config is a TS dictionary (serializable) * containing the configuration of a layer. * The same layer can be reinstantiated later * (without its trained weights) from this configuration. * * The config of a layer does not include connectivity * information, nor the layer class name. These are handled * by 'Container' (one layer of abstraction above). * * Porting Note: The TS dictionary follows TS naming standards for * keys, and uses tfjs-layers type-safe Enums. Serialization methods * should use a helper function to convert to the pythonic storage * standard. (see serialization_utils.convertTsToPythonic) * * @returns TS dictionary of configuration. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ getConfig() { const config = { name: this.name, trainable: this.trainable }; if (this.batchInputShape != null) { config['batchInputShape'] = this.batchInputShape; } if (this.dtype != null) { config['dtype'] = this.dtype; } return config; } /** * Dispose the weight variables that this Layer instance holds. * * @returns {number} Number of disposed variables. */ disposeWeights() { this.weights.forEach(weight => weight.dispose()); return this.weights.length; } assertNotDisposed() { if (this._refCount === 0) { throw new Error(`Layer '${this.name}' is already disposed.`); } } /** * Attempt to dispose layer's weights. * * This method decreases the reference count of the Layer object by 1. * * A Layer is reference-counted. Its reference count is incremented by 1 * the first item its `apply()` method is called and when it becomes a part * of a new `Node` (through calling the `apply()` method on a * `tf.SymbolicTensor`). * * If the reference count of a Layer becomes 0, all the weights will be * disposed and the underlying memory (e.g., the textures allocated in WebGL) * will be freed. * * Note: If the reference count is greater than 0 after the decrement, the * weights of the Layer will *not* be disposed. * * After a Layer is disposed, it cannot be used in calls such as `apply()`, * `getWeights()` or `setWeights()` anymore. * * @returns A DisposeResult Object with the following fields: * - refCountAfterDispose: The reference count of the Container after this * `dispose()` call. * - numDisposedVariables: Number of `tf.Variable`s (i.e., weights) disposed * during this `dispose()` call. * @throws {Error} If the layer is not built yet, or if the layer has already * been disposed. * * @doc {heading: 'Models', 'subheading': 'Classes'} */ dispose() { if (!this.built) { throw new Error(`Cannot dispose Layer ${this.name} because it has not been ` + `built yet.`); } if (this._refCount === null) { throw new Error(`Cannot dispose Layer ${this.name} because it has not been used ` + `yet.`); } this.assertNotDisposed(); let numDisposedVariables = 0; if (--this._refCount === 0) { numDisposedVariables = this.disposeWeights(); } return { refCountAfterDispose: this._refCount, numDisposedVariables }; } } /** * Collects the input shape(s) of a list of `tf.Tensor`s or * `tf.SymbolicTensor`s. * * TODO(michaelterry): Update PyKeras docs (backport). * * @param inputTensors List of input tensors (or single input tensor). * * @return List of shape tuples (or single tuple), one tuple per input. */ function collectInputShape(inputTensors) { inputTensors = toList(inputTensors); const shapes = []; for (const x of inputTensors) { shapes.push(x.shape); } return singletonOrArray(shapes); } /** * Guesses output dtype based on inputs. * * At present, just returns 'float32' for any input. * * @param inputTensors List of input tensors (or single input tensor). * * @return The guessed DType. At present, always returns 'float32'. */ function guessOutputDType(inputTensors) { return 'float32'; } /** * Returns the list of input tensors necessary to compute `tensor`. * * Output will always be a list of tensors (potentially with 1 element). * * @param tensor The tensor to start from. * @param layer Origin layer of the tensor. * @param nodeIndex Origin node index of the tensor. * * @return Array of input tensors. */ function getSourceInputs(tensor, layer, nodeIndex) { if (layer == null || (nodeIndex != null && nodeIndex > 0)) { layer = tensor.sourceLayer; nodeIndex = tensor.nodeIndex; } if (layer.inboundNodes.length === 0) { return [tensor]; } else { const node = layer.inboundNodes[nodeIndex]; if (node.inboundLayers.length === 0) { return node.inputTensors; } else { const sourceTensors = []; for (let i = 0; i < node.inboundLayers.length; i++) { const x = node.inputTensors[i]; const layer = node.inboundLayers[i]; const nodeIndex = node.nodeIndices[i]; const previousSources = getSourceInputs(x, layer, nodeIndex); // Avoid input redundancy. for (const x of previousSources) { if (sourceTensors.indexOf(x) === -1) { sourceTensors.push(x); } } } return sourceTensors; } } } function checkAllSymbolic(tensors) { let allAreSymbolic = true; for (const tensor of toList(tensors)) { if (!(tensor instanceof SymbolicTensor)) { allAreSymbolic = false; break; } } return allAreSymbolic; } function checkNoneSymbolic(tensors) { let noneAreSymbolic = true; for (const tensor of toList(tensors)) { if (tensor instanceof SymbolicTensor) { noneAreSymbolic = false; break; } } return noneAreSymbolic; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class InputLayer extends Layer { constructor(args) { super({ dtype: args.dtype, name: args.name != null ? args.name : getUid('input').toString() }); // Normalize config.batchSize and config.sparse if (args.batchSize == null) { args.batchSize = null; } if (args.sparse == null) { args.sparse = false; } this.trainable = false; this.built = true; this.sparse = args.sparse; if (args.inputShape != null && args.batchInputShape != null) { throw new ValueError('Only provide the inputShape OR ' + 'batchInputShape argument to inputLayer, not both at the same time.'); } let batchInputShape = args.batchInputShape; if (batchInputShape == null) { if (args.inputShape == null) { throw new ValueError('An InputLayer should be passed either a ' + '`batchInputShape` or an `inputShape`.'); } else { batchInputShape = [args.batchSize].concat(args.inputShape); } } else { // TODO(michaelterry): Backport to PyKeras if (args.batchSize != null) { throw new ValueError('Cannot specify batchSize if batchInputShape is ' + 'specified when creating an InputLayer.'); } } const dtype = args.dtype || 'float32'; this.batchInputShape = batchInputShape; this.dtype = dtype; // TODO(michaelterry): Backport this to PyKeras? this.inputSpec = [{ shape: batchInputShape }]; const inputTensor = new SymbolicTensor(this.dtype, this.batchInputShape, this, [], {}, this.name); inputTensor.nodeIndex = 0; inputTensor.tensorIndex = 0; // Create an input node to add to this.outboundNode. // (This call has side effects.) // tslint:disable-next-line:no-unused-expression new Node({ outboundLayer: this, inboundLayers: [], nodeIndices: [], tensorIndices: [], inputTensors: [inputTensor], outputTensors: [inputTensor], inputMasks: [null], outputMasks: [null], inputShapes: [batchInputShape], outputShapes: [batchInputShape] }); } apply(inputs, kwargs) { throw new ValueError('Cannot pass any input to an ' + `InputLayer's apply() method. InputLayer name: ${this.name}`); } dispose() { // dispose() for InputLayer is overridden as no-op. return { refCountAfterDispose: this._refCount, numDisposedVariables: 0 }; } getConfig() { return { batchInputShape: this.batchInputShape, dtype: this.dtype, sparse: this.sparse, name: this.name }; } } /** @nocollapse */ InputLayer.className = 'InputLayer'; serialization.registerClass(InputLayer); function Input(config) { if (config.batchShape == null && config.shape == null) { throw new Error('Please provide to Input either a `shape`' + ' or a `batchShape` argument. Note that ' + '`shape` does not include the batch ' + 'dimension.'); } if (config.batchShape != null && config.shape != null) { // TODO(michaelterry): Backport to PyKeras. throw new ValueError('Please provide either a `shape` or `batchShape` ' + 'argument to Input, but not both.'); } let batchShape = config.batchShape; if (config.shape != null && batchShape == null) { batchShape = [null].concat(config.shape); } let dtype = config.dtype; if (dtype == null) { dtype = 'float32'; } const inputLayer = new InputLayer({ batchInputShape: batchShape, name: config.name, dtype, sparse: config.sparse }); const outputs = inputLayer.inboundNodes[0].outputTensors; return outputs[0]; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Helper function to check the dtype and shape compatibility of a feed value. */ function assertFeedCompatibility(key, val) { // Check dtype compatibility. if (key.dtype == null || key.dtype === val.dtype) { // a. If types match, return val tensor as is. return val; } try { // b. Attempt to convert to expected type. return cast$2(val, key.dtype); } catch (err) { // c. If conversion fails, return helpful error. throw new ValueError(`The dtype of the feed (${val.dtype}) can not be cast to the dtype ` + `of the key '${key.name}' (${key.dtype}).`); } } /** * FeedDict: A mapping from unique SymbolicTensors to feed values for them. * A feed value is a concrete value represented as an `Tensor`. */ class FeedDict { /** * Constructor, optionally does copy-construction. * @param feeds An Array of `Feed`s, or another `FeedDict`, in which case * copy-construction will be performed. */ constructor(feeds) { this.id2Value = {}; this.id2Mask = {}; this.name2Id = {}; if (feeds instanceof FeedDict) { for (const id in feeds.id2Value) { this.id2Value[id] = feeds.id2Value[id]; if (id in feeds.id2Mask) { this.id2Mask[id] = feeds.id2Mask[id]; } } } else { if (feeds == null) { return; } for (const feed of feeds) { this.add(feed.key, feed.value); } } } /** * Add a key-value pair to the FeedDict. * * @param key The key of the feed. * @param value The value of the tensor feed. * @param mask The value of the mask feed (optional). * @returns This `FeedDict`. * @throws ValueError: If the key `SymbolicTensor` already exists in the * `FeedDict`. */ add(key, value, mask) { if (this.id2Value[key.id] == null) { this.id2Value[key.id] = assertFeedCompatibility(key, value); this.name2Id[key.name] = key.id; if (mask != null) { this.id2Mask[key.id] = mask; } } else { throw new ValueError(`Duplicate key: name=${key.name}, id=${key.id}`); } return this; } /** * Add a Feed to the FeedDict. * @param feed The new `Feed` to add. * @returns This `FeedDict`. */ addFeed(feed) { this.add(feed.key, feed.value); } /** * Probe whether a key already exists in the FeedDict. * @param key */ hasKey(key) { return this.id2Value[key.id] != null; } /** * Get all the SymbolicTensor available in this FeedDict. */ names() { return Object.keys(this.name2Id); } /** * Get the feed value for given key. * @param key The SymbolicTensor, or its name (as a string), of which the * value is sought. * @returns If `key` exists, the corresponding feed value. * @throws ValueError: If `key` does not exist in this `FeedDict`. */ getValue(key) { if (key instanceof SymbolicTensor) { if (this.id2Value[key.id] == null) { throw new ValueError(`Nonexistent key: ${key.name}`); } else { return this.id2Value[key.id]; } } else { const id = this.name2Id[key]; if (id == null) { throw new ValueError(`Feed dict has no SymbolicTensor name: ${key}`); } return this.id2Value[id]; } } /** * Get the feed mask for given key. * @param key The SymbolicTensor, or its name (as a string), of which the * value is sought. * @returns If `key` exists, the corresponding feed mask. * @throws ValueError: If `key` does not exist in this `FeedDict`. */ getMask(key) { if (key instanceof SymbolicTensor) { if (this.id2Value[key.id] == null) { throw new ValueError(`Nonexistent key: ${key.name}`); } else { return this.id2Mask[key.id]; } } else { const id = this.name2Id[key]; if (id == null) { throw new ValueError(`Feed dict has no SymbolicTensor name: ${key}`); } return this.id2Mask[id]; } } /** Dispose all mask Tensors held by this object. */ disposeMasks() { if (this.id2Mask != null) { dispose(this.id2Mask); } } } // Cache for topologically sorted SymbolicTensors for given execution // targets (i.e., fetches). const cachedSorted = new LruCache(); // Cache for recipient count maps for given execution targets (i.e., fetches). const cachedRecipientCounts = new LruCache(); function updateCacheMaxEntries(maxEntries) { if (cachedSorted != null) { cachedSorted.setMaxEntries(maxEntries); } if (cachedRecipientCounts != null) { cachedRecipientCounts.setMaxEntries(maxEntries); } } /** * Execute a SymbolicTensor by using concrete feed values. * * A `SymbolicTensor` object is a node in a computation graph of TF.js * Layers. The object is backed by a source layer and input * `SymbolicTensor`s to the source layer. This method evaluates * the `call()` method of the source layer, using concrete values of the * inputs obtained from either * * `feedDict`, if the input key exists in `feedDict`, or else, * * a recursive call to `execute()` itself. * * @param x: The `SymbolicTensor` to execute. * @param feedDict: The feed values, as base condition of the recursion. * execution. * @param kwargs: Optional keyword arguments. * @param probe: A probe object (of interface `ExecutionProbe`) used for * testing memory footprint of `execute` calls. * @returns Result of the execution. * @throws ValueError: If any `SymbolicTensor`s from `InputLayer`s * encountered during the execution lacks a feed value in `feedDict`. */ function execute(fetches, feedDict, kwargs, probe) { const training = kwargs == null ? false : kwargs['training']; const arrayFetches = Array.isArray(fetches); const fetchArray = arrayFetches ? fetches : [fetches]; const outputNames = fetchArray.map(t => t.name); const finalOutputs = []; const feedNames = feedDict.names(); for (const outputName of outputNames) { if (feedNames.indexOf(outputName) !== -1) { finalOutputs.push(feedDict.getValue(outputName)); } else { finalOutputs.push(null); } } if (probe != null) { // For optional probing of memory footprint during execution. probe.maxNumTensors = -Infinity; probe.minNumTensors = Infinity; } // Check cache. const fetchAndFeedKey = outputNames.join(',') + '|' + feedDict.names().sort().join(','); let sorted = cachedSorted.get(fetchAndFeedKey); let recipientCounts; if (sorted == null) { // Cache doesn't contain the desired combination of fetches. Compute // topological sort for the combination for the first time. const out = getTopologicalSortAndRecipientCounts(fetchArray, feedDict); sorted = out.sorted; recipientCounts = out.recipientCounts; // Store results in cache for future use. cachedSorted.put(fetchAndFeedKey, sorted); cachedRecipientCounts.put(fetchAndFeedKey, recipientCounts); } recipientCounts = {}; if (!training) { Object.assign(recipientCounts, cachedRecipientCounts.get(fetchAndFeedKey)); } const internalFeedDict = new FeedDict(feedDict); // Start iterative execution on the topologically-sorted SymbolicTensors. for (let i = 0; i < sorted.length; ++i) { if (probe != null) { // For optional probing of memory usage during execution. const numTensors = memory().numTensors; if (numTensors > probe.maxNumTensors) { probe.maxNumTensors = numTensors; } if (numTensors < probe.minNumTensors) { probe.minNumTensors = numTensors; } } const symbolic = sorted[i]; const srcLayer = symbolic.sourceLayer; if (srcLayer instanceof InputLayer) { continue; } const inputValues = []; const inputMasks = []; const tensorsToDispose = []; let maskExists = false; for (const input of symbolic.inputs) { const value = internalFeedDict.getValue(input); const mask = internalFeedDict.getMask(input); inputValues.push(value); inputMasks.push(mask); if (mask != null) { maskExists = true; } if (!training) { recipientCounts[input.name]--; if (recipientCounts[input.name] === 0 && !feedDict.hasKey(input) && outputNames.indexOf(input.name) === -1 && !value.isDisposed && input.sourceLayer.stateful !== true) { tensorsToDispose.push(value); } } } if (maskExists) { kwargs = kwargs || {}; kwargs['mask'] = inputMasks[0]; } const outputTensors = toList(srcLayer.apply(inputValues, kwargs)); let outputMask = null; if (srcLayer.supportsMasking) { outputMask = srcLayer.computeMask(inputValues, inputMasks); } const layerOutputs = getNodeOutputs(symbolic); const outputSymbolicTensors = Array.isArray(layerOutputs) ? layerOutputs : [layerOutputs]; for (let i = 0; i < outputSymbolicTensors.length; ++i) { if (!internalFeedDict.hasKey(outputSymbolicTensors[i])) { internalFeedDict.add(outputSymbolicTensors[i], outputTensors[i], Array.isArray(outputMask) ? outputMask[0] : outputMask); } const index = outputNames.indexOf(outputSymbolicTensors[i].name); if (index !== -1) { finalOutputs[index] = outputTensors[i]; } } if (!training) { // Clean up Tensors that are no longer needed. dispose(tensorsToDispose); } } // NOTE(cais): Unlike intermediate tensors, we don't discard mask // tensors as we go, because these tensors are sometimes passed over a // series of mutliple layers, i.e., not obeying the immediate input // relations in the graph. If this becomes a memory-usage concern, // we can improve this in the future. internalFeedDict.disposeMasks(); return arrayFetches ? finalOutputs : finalOutputs[0]; } /** * Sort the `SymbolicTensor`s topologically, for an array of fetches. * * This function calls getTopologicalSortAndRecipientCountsForOneFetch and * merges their results. * * @param fetch The array of fetches requested. Must be a non-empty array. * @param feedDict The dictionary of fed values. * @returns sorted: Topologically-sorted array of SymbolicTensors. * recipientCounts: Recipient counts for all SymbolicTensors in `sorted`. */ function getTopologicalSortAndRecipientCounts(fetches, feedDict) { util.assert(fetches != null && fetches.length > 0, () => `Expected at least one fetch, got none`); let finalSorted = []; let finalRecipientMap = {}; if (fetches.length === 1) { // Special-casing 1 fetch for efficiency. const out = getTopologicalSortAndRecipientCountsForOneFetch(fetches[0], feedDict); finalSorted = out.sorted; finalRecipientMap = out.recipientMap; } else { const visited = new Set(); for (const fetch of fetches) { const { sorted, recipientMap } = getTopologicalSortAndRecipientCountsForOneFetch(fetch, feedDict); // Merge sorted SymbolicTensor Arrays. for (const symbolicTensor of sorted) { if (!visited.has(symbolicTensor.name)) { finalSorted.push(symbolicTensor); visited.add(symbolicTensor.name); } } // Merge recipient maps. for (const name in recipientMap) { if (finalRecipientMap[name] == null) { finalRecipientMap[name] = new Set(); } recipientMap[name].forEach(recipient => finalRecipientMap[name].add(recipient)); } } } return { sorted: finalSorted, recipientCounts: recipientMap2Counts(finalRecipientMap) }; } function recipientMap2Counts(recipientMap) { const recipientCounts = {}; for (const name in recipientMap) { recipientCounts[name] = recipientMap[name].size; } return recipientCounts; } /** * Sort the `SymbolicTensor`s topologically, for a single fetch. * * This helper function processes the upstream SymbolicTensors of a single * fetch. * * @param fetch The single fetch requested. * @param feedDict The dictionary of fed values. * @returns sorted: Topologically-sorted array of SymbolicTensors. * recipientMap: Recipient names for all SymbolicTensors in `sorted`. */ function getTopologicalSortAndRecipientCountsForOneFetch(fetch, feedDict) { const visited = new Set(); const sorted = []; const recipientMap = {}; // Put keys of the feedDict into visited first, so they don't have to be // walked. This is needed in case where there are feeds for intermediate // SymbolicTensors of the graph. for (const key of feedDict.names()) { visited.add(key); } const stack = []; const marks = []; // Initial population of stack and marks. stack.push(fetch); while (stack.length > 0) { const top = stack[stack.length - 1]; if (visited.has(top.name)) { stack.pop(); continue; } const topIsMarked = marks[marks.length - 1] === stack.length - 1; if (top.inputs.length === 0 || topIsMarked) { // Input SymbolicTensor or all children have been visited. stack.pop(); sorted.push(top); visited.add(top.name); if (topIsMarked) { marks.pop(); } } else { // A non-input SymbolicTensor whose upstream SymbolicTensors haven't // been visited yet. Push them onto the stack. marks.push(stack.length - 1); for (const input of top.inputs) { // Increment the recipient count. Note that this needs to happen // regardless of whether the SymbolicTensor has been visited before. if (recipientMap[input.name] == null) { recipientMap[input.name] = new Set(); } recipientMap[input.name].add(top.name); if (visited.has(input.name)) { continue; // Avoid repeated visits to the same SymbolicTensor. } stack.push(input); } } } return { sorted, recipientMap }; } /** * Get the symbolic output tensors of the node to which a given fetch belongs. * @param fetch The fetched symbolic tensor. * @returns The Array of symbolic tensors output by the node to which `fetch` * belongs. */ function getNodeOutputs(fetch) { let layerOutputs; if (fetch.sourceLayer.inboundNodes.length === 1) { layerOutputs = fetch.sourceLayer.output; } else { let nodeIndex = null; for (let i = 0; i < fetch.sourceLayer.inboundNodes.length; ++i) { for (const outputTensor of fetch.sourceLayer.inboundNodes[i] .outputTensors) { if (outputTensor.id === fetch.id) { nodeIndex = i; break; } } } layerOutputs = fetch.sourceLayer.getOutputAt(nodeIndex); } return layerOutputs; } /** * @license * Copyright 2022 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. * ============================================================================= */ const ENV$2 = env$1(); /** The max number of entries for the caches of layers' topological sort. */ ENV$2.registerFlag('TOPOLOGICAL_SORT_CACHE_MAX_ENTRIES', () => 100, updateCacheMaxEntries); const Abs = 'Abs'; const Acos = 'Acos'; const Acosh = 'Acosh'; const Add$1 = 'Add'; const AddN = 'AddN'; const ArgMax = 'ArgMax'; const ArgMin = 'ArgMin'; const Asin = 'Asin'; const Asinh = 'Asinh'; const Atan = 'Atan'; const Atanh = 'Atanh'; const Atan2 = 'Atan2'; const AvgPool = 'AvgPool'; const AvgPoolGrad = 'AvgPoolGrad'; const AvgPool3D = 'AvgPool3D'; const AvgPool3DGrad = 'AvgPool3DGrad'; const BatchMatMul = 'BatchMatMul'; const BatchToSpaceND = 'BatchToSpaceND'; const BroadcastTo = 'BroadcastTo'; const Cast = 'Cast'; const Ceil = 'Ceil'; const ClipByValue = 'ClipByValue'; const Complex = 'Complex'; const ComplexAbs = 'ComplexAbs'; const Concat = 'Concat'; const Conv2D$1 = 'Conv2D'; const Conv2DBackpropFilter = 'Conv2DBackpropFilter'; const Conv2DBackpropInput = 'Conv2DBackpropInput'; const Conv3D$1 = 'Conv3D'; const Conv3DBackpropFilterV2 = 'Conv3DBackpropFilterV2'; const Conv3DBackpropInputV2 = 'Conv3DBackpropInputV2'; const Cos = 'Cos'; const Cosh = 'Cosh'; const Cumprod = 'Cumprod'; const Cumsum = 'Cumsum'; const DepthwiseConv2dNative = 'DepthwiseConv2dNative'; const DepthwiseConv2dNativeBackpropFilter = 'DepthwiseConv2dNativeBackpropFilter'; const DepthwiseConv2dNativeBackpropInput = 'DepthwiseConv2dNativeBackpropInput'; const Dilation2D = 'Dilation2D'; const Dilation2DBackpropInput = 'Dilation2DBackpropInput'; const Dilation2DBackpropFilter = 'Dilation2DBackpropFilter'; const RealDiv = 'RealDiv'; const Elu$1 = 'Elu'; const EluGrad = 'EluGrad'; const Erf = 'Erf'; const Equal = 'Equal'; const Exp = 'Exp'; const ExpandDims = 'ExpandDims'; const Expm1 = 'Expm1'; const Floor = 'Floor'; const FloorDiv = 'FloorDiv'; const FusedBatchNorm = 'FusedBatchNorm'; const GatherV2 = 'GatherV2'; const Greater = 'Greater'; const GreaterEqual = 'GreaterEqual'; const Identity = 'Identity'; const Imag = 'Imag'; const IsFinite = 'IsFinite'; const IsInf = 'IsInf'; const IsNan = 'IsNan'; const LeakyRelu = 'LeakyRelu'; const Less = 'Less'; const LessEqual = 'LessEqual'; const Log = 'Log'; const Log1p = 'Log1p'; const LogicalAnd = 'LogicalAnd'; const LogicalNot = 'LogicalNot'; const LogSoftmax$1 = 'LogSoftmax'; const LRN = 'LRN'; const LRNGrad = 'LRNGrad'; const Max = 'Max'; const Maximum$1 = 'Maximum'; const MaxPool = 'MaxPool'; const MaxPoolGrad = 'MaxPoolGrad'; const MaxPool3D = 'MaxPool3D'; const MaxPool3DGrad = 'MaxPool3DGrad'; const Mean = 'Mean'; const Min = 'Min'; const Minimum$1 = 'Minimum'; const MirrorPad = 'MirrorPad'; const Mod = 'Mod'; const Multiply$1 = 'Multiply'; const Neg = 'Neg'; const OnesLike = 'OnesLike'; const OneHot = 'OneHot'; const Pack = 'Pack'; const PadV2 = 'PadV2'; const Pow = 'Pow'; const Prelu = 'Prelu'; const Prod = 'Prod'; const Real = 'Real'; const Reciprocal = 'Reciprocal'; const Relu$1 = 'Relu'; const Reshape$1 = 'Reshape'; const ResizeNearestNeighbor = 'ResizeNearestNeighbor'; const ResizeNearestNeighborGrad = 'ResizeNearestNeighborGrad'; const ResizeBilinear = 'ResizeBilinear'; const ResizeBilinearGrad = 'ResizeBilinearGrad'; const Relu6$1 = 'Relu6'; const Reverse = 'Reverse'; const Round = 'Round'; const Rsqrt = 'Rsqrt'; const TensorScatterUpdate = 'TensorScatterUpdate'; const Select = 'Select'; const Selu$1 = 'Selu'; const Slice = 'Slice'; const Sin = 'Sin'; const Sinh = 'Sinh'; const Sign = 'Sign'; const Sigmoid$1 = 'Sigmoid'; const Softplus$1 = 'Softplus'; const Sqrt = 'Sqrt'; const Sum = 'Sum'; const SpaceToBatchND = 'SpaceToBatchND'; const SplitV = 'SplitV'; const Softmax$2 = 'Softmax'; const SquaredDifference = 'SquaredDifference'; const Square = 'Square'; const Sub = 'Sub'; const Tan = 'Tan'; const Tanh$1 = 'Tanh'; const Tile = 'Tile'; const Transpose = 'Transpose'; const Unpack = 'Unpack'; const UnsortedSegmentSum = 'UnsortedSegmentSum'; const ZerosLike = 'ZerosLike'; /** * TensorFlow.js-only kernels */ const Step = 'Step'; /** * @license * Copyright 2020 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. * ============================================================================= */ const EPSILON_FLOAT32 = 1e-7; const EPSILON_FLOAT16 = 1e-4; /** * The interface that defines the kernels that should be implemented when * adding a new backend. New backends don't need to implement every one of the * methods, this can be done gradually (throw an error for unimplemented * methods). */ class KernelBackend { refCount(dataId) { return notYetImplemented('refCount'); } incRef(dataId) { return notYetImplemented('incRef'); } timerAvailable() { return true; } time(f) { return notYetImplemented('time'); } read(dataId) { return notYetImplemented('read'); } readSync(dataId) { return notYetImplemented('readSync'); } readToGPU(dataId, options) { return notYetImplemented('readToGPU'); } numDataIds() { return notYetImplemented('numDataIds'); } disposeData(dataId, force) { return notYetImplemented('disposeData'); } write(values, shape, dtype) { return notYetImplemented('write'); } move(dataId, values, shape, dtype, refCount) { return notYetImplemented('move'); } createTensorFromGPUData(values, shape, dtype) { return notYetImplemented('createTensorFromGPUData'); } memory() { return notYetImplemented('memory'); } /** Returns the highest precision for floats in bits (e.g. 16 or 32) */ floatPrecision() { return notYetImplemented('floatPrecision'); } /** Returns the smallest representable number. */ epsilon() { return this.floatPrecision() === 32 ? EPSILON_FLOAT32 : EPSILON_FLOAT16; } dispose() { return notYetImplemented('dispose'); } } function notYetImplemented(kernelName) { throw new Error(`'${kernelName}' not yet implemented or not found in the registry. ` + `This kernel may not be supported by the tfjs backend you have chosen`); } /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Asserts that the expression is true. Otherwise throws an error with the * provided message. * * ```js * const x = 2; * tf.util.assert(x === 2, 'x is not 2'); * ``` * * @param expr The expression to assert (as a boolean). * @param msg A function that returns the message to report when throwing an * error. We use a function for performance reasons. * * @doc {heading: 'Util', namespace: 'util'} */ function assert(expr, msg) { if (!expr) { throw new Error(typeof msg === 'string' ? msg : msg()); } } function assertShapesMatch(shapeA, shapeB, errorMessagePrefix = '') { assert(arraysEqual(shapeA, shapeB), () => errorMessagePrefix + ` Shapes ${shapeA} and ${shapeB} must match`); } /** * Returns the size (number of elements) of the tensor given its shape. * * ```js * const shape = [3, 4, 2]; * const size = tf.util.sizeFromShape(shape); * console.log(size); * ``` * * @doc {heading: 'Util', namespace: 'util'} */ function sizeFromShape(shape) { if (shape.length === 0) { // Scalar. return 1; } let size = shape[0]; for (let i = 1; i < shape.length; i++) { size *= shape[i]; } return size; } function arraysEqual(n1, n2) { if (n1 === n2) { return true; } if (n1 == null || n2 == null) { return false; } if (n1.length !== n2.length) { return false; } for (let i = 0; i < n1.length; i++) { if (n1[i] !== n2[i]) { return false; } } return true; } function isInt(a) { return a % 1 === 0; } function rightPad(a, size) { if (size <= a.length) { return a; } return a + ' '.repeat(size - a.length); } function parseAxisParam(axis, shape) { const rank = shape.length; // Normalize input axis = axis == null ? shape.map((s, i) => i) : [].concat(axis); // Check for valid range assert(axis.every(ax => ax >= -rank && ax < rank), () => `All values in axis param must be in range [-${rank}, ${rank}) but ` + `got axis ${axis}`); // Check for only integers assert(axis.every(ax => isInt(ax)), () => `All values in axis param must be integers but ` + `got axis ${axis}`); // Handle negative axis. return axis.map(a => a < 0 ? rank + a : a); } function checkConversionForErrors(vals, dtype) { for (let i = 0; i < vals.length; i++) { const num = vals[i]; if (isNaN(num) || !isFinite(num)) { throw Error(`A tensor of type ${dtype} being uploaded contains ${num}.`); } } } /** Returns true if the dtype is valid. */ function isValidDtype(dtype) { return dtype === 'bool' || dtype === 'complex64' || dtype === 'float32' || dtype === 'int32' || dtype === 'string'; } function bytesPerElement(dtype) { if (dtype === 'float32' || dtype === 'int32') { return 4; } else if (dtype === 'complex64') { return 8; } else if (dtype === 'bool') { return 1; } else { throw new Error(`Unknown dtype ${dtype}`); } } /** * Returns the approximate number of bytes allocated in the string array - 2 * bytes per character. Computing the exact bytes for a native string in JS * is not possible since it depends on the encoding of the html page that * serves the website. */ function bytesFromStringArray(arr) { if (arr == null) { return 0; } let bytes = 0; arr.forEach(x => bytes += x.length); return bytes; } /** Returns true if the value is a string. */ function isString(value) { return typeof value === 'string' || value instanceof String; } function isBoolean(value) { return typeof value === 'boolean'; } function isNumber(value) { return typeof value === 'number'; } function inferDtype(values) { if (Array.isArray(values)) { return inferDtype(values[0]); } if (values instanceof Float32Array) { return 'float32'; } else if (values instanceof Int32Array || values instanceof Uint8Array || values instanceof Uint8ClampedArray) { return 'int32'; } else if (isNumber(values)) { return 'float32'; } else if (isString(values)) { return 'string'; } else if (isBoolean(values)) { return 'bool'; } return 'float32'; } function isFunction(f) { return !!(f && f.constructor && f.call && f.apply); } function computeStrides(shape) { const rank = shape.length; if (rank < 2) { return []; } // Last dimension has implicit stride of 1, thus having D-1 (instead of D) // strides. const strides = new Array(rank - 1); strides[rank - 2] = shape[rank - 1]; for (let i = rank - 3; i >= 0; --i) { strides[i] = strides[i + 1] * shape[i + 1]; } return strides; } function createNestedArray(offset, shape, a, isComplex = false) { const ret = new Array(); if (shape.length === 1) { const d = shape[0] * (isComplex ? 2 : 1); for (let i = 0; i < d; i++) { ret[i] = a[offset + i]; } } else { const d = shape[0]; const rest = shape.slice(1); const len = rest.reduce((acc, c) => acc * c) * (isComplex ? 2 : 1); for (let i = 0; i < d; i++) { ret[i] = createNestedArray(offset + i * len, rest, a, isComplex); } } return ret; } // Provide a nested array of TypedArray in given shape. function toNestedArray(shape, a, isComplex = false) { if (shape.length === 0) { // Scalar type should return a single number. return a[0]; } const size = shape.reduce((acc, c) => acc * c) * (isComplex ? 2 : 1); if (size === 0) { // A tensor with shape zero should be turned into empty list. return []; } if (size !== a.length) { throw new Error(`[${shape}] does not match the input size ${a.length}${isComplex ? ' for a complex tensor' : ''}.`); } return createNestedArray(0, shape, a, isComplex); } function makeOnesTypedArray(size, dtype) { const array = makeZerosTypedArray(size, dtype); for (let i = 0; i < array.length; i++) { array[i] = 1; } return array; } function makeZerosTypedArray(size, dtype) { if (dtype == null || dtype === 'float32' || dtype === 'complex64') { return new Float32Array(size); } else if (dtype === 'int32') { return new Int32Array(size); } else if (dtype === 'bool') { return new Uint8Array(size); } else { throw new Error(`Unknown data type ${dtype}`); } } function assertNonNegativeIntegerDimensions(shape) { shape.forEach(dimSize => { assert(Number.isInteger(dimSize) && dimSize >= 0, () => `Tensor must have a shape comprised of positive integers but got ` + `shape [${shape}].`); }); } /** * This method asserts whether an object is a Promise instance. * @param object */ // tslint:disable-next-line: no-any function isPromise(object) { // We chose to not use 'obj instanceOf Promise' for two reasons: // 1. It only reliably works for es6 Promise, not other Promise // implementations. // 2. It doesn't work with framework that uses zone.js. zone.js monkey // patch the async calls, so it is possible the obj (patched) is // comparing to a pre-patched Promise. return object && object.then && typeof object.then === 'function'; } /** * @license * Copyright 2017 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. * ============================================================================= */ // Expects flags from URL in the format ?tfjsflags=FLAG1:1,FLAG2:true. const TENSORFLOWJS_FLAGS_PREFIX = 'tfjsflags'; /** * The environment contains evaluated flags as well as the registered platform. * This is always used as a global singleton and can be retrieved with * `tf.env()`. * * @doc {heading: 'Environment'} */ class Environment { // tslint:disable-next-line: no-any constructor(global) { this.global = global; this.flags = {}; this.flagRegistry = {}; this.urlFlags = {}; // Jasmine spies on this in 'environment_test.ts' this.getQueryParams = getQueryParams; this.populateURLFlags(); } setPlatform(platformName, platform) { if (this.platform != null) { if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) { console.warn(`Platform ${this.platformName} has already been set. ` + `Overwriting the platform with ${platformName}.`); } } this.platformName = platformName; this.platform = platform; } registerFlag(flagName, evaluationFn, setHook) { this.flagRegistry[flagName] = { evaluationFn, setHook }; // Override the flag value from the URL. This has to happen here because // the environment is initialized before flags get registered. if (this.urlFlags[flagName] != null) { const flagValue = this.urlFlags[flagName]; if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) { console.warn(`Setting feature override from URL ${flagName}: ${flagValue}.`); } this.set(flagName, flagValue); } } async getAsync(flagName) { if (flagName in this.flags) { return this.flags[flagName]; } this.flags[flagName] = await this.evaluateFlag(flagName); return this.flags[flagName]; } get(flagName) { if (flagName in this.flags) { return this.flags[flagName]; } const flagValue = this.evaluateFlag(flagName); if (isPromise(flagValue)) { throw new Error(`Flag ${flagName} cannot be synchronously evaluated. ` + `Please use getAsync() instead.`); } this.flags[flagName] = flagValue; return this.flags[flagName]; } getNumber(flagName) { return this.get(flagName); } getBool(flagName) { return this.get(flagName); } getString(flagName) { return this.get(flagName); } getFlags() { return this.flags; } // For backwards compatibility. get features() { return this.flags; } set(flagName, value) { if (this.flagRegistry[flagName] == null) { throw new Error(`Cannot set flag ${flagName} as it has not been registered.`); } this.flags[flagName] = value; if (this.flagRegistry[flagName].setHook != null) { this.flagRegistry[flagName].setHook(value); } } evaluateFlag(flagName) { if (this.flagRegistry[flagName] == null) { throw new Error(`Cannot evaluate flag '${flagName}': no evaluation function found.`); } return this.flagRegistry[flagName].evaluationFn(); } setFlags(flags) { this.flags = Object.assign({}, flags); } reset() { this.flags = {}; this.urlFlags = {}; this.populateURLFlags(); } populateURLFlags() { if (typeof this.global === 'undefined' || typeof this.global.location === 'undefined' || typeof this.global.location.search === 'undefined') { return; } const urlParams = this.getQueryParams(this.global.location.search); if (TENSORFLOWJS_FLAGS_PREFIX in urlParams) { const keyValues = urlParams[TENSORFLOWJS_FLAGS_PREFIX].split(','); keyValues.forEach(keyValue => { const [key, value] = keyValue.split(':'); this.urlFlags[key] = parseValue(key, value); }); } } } function getQueryParams(queryString) { const params = {}; queryString.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g, (s, ...t) => { decodeParam(params, t[0], t[1]); return t.join('='); }); return params; } function decodeParam(params, name, value) { params[decodeURIComponent(name)] = decodeURIComponent(value || ''); } function parseValue(flagName, value) { const lowerCaseValue = value.toLowerCase(); if (lowerCaseValue === 'true' || lowerCaseValue === 'false') { return lowerCaseValue === 'true'; } else if (`${+lowerCaseValue}` === lowerCaseValue) { return +lowerCaseValue; } else { return value; } } /** * Returns the current environment (a global singleton). * * The environment object contains the evaluated feature values as well as the * active platform. * * @doc {heading: 'Environment'} */ function env() { return ENV$1; } let ENV$1 = null; function setEnvironmentGlobal(environment) { ENV$1 = environment; } /** * @license * Copyright 2020 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. * ============================================================================= */ // Note that the identifier globalNameSpace is scoped to this module, but will // always resolve to the same global object regardless of how the module is // resolved. // tslint:disable-next-line:no-any let globalNameSpace; // tslint:disable-next-line:no-any function getGlobalNamespace() { if (globalNameSpace == null) { // tslint:disable-next-line:no-any let ns; if (typeof (window) !== 'undefined') { ns = window; } else if (typeof (global) !== 'undefined') { ns = global; } else if (typeof (process) !== 'undefined') { ns = process; } else if (typeof (self) !== 'undefined') { ns = self; } else { throw new Error('Could not find a global object'); } globalNameSpace = ns; } return globalNameSpace; } // tslint:disable-next-line:no-any function getGlobalMap() { const ns = getGlobalNamespace(); if (ns._tfGlobals == null) { ns._tfGlobals = new Map(); } return ns._tfGlobals; } /** * Returns a globally accessible 'singleton' object. * * @param key the name of the object * @param init a function to initialize to initialize this object * the first time it is fetched. */ function getGlobal(key, init) { const globalMap = getGlobalMap(); if (globalMap.has(key)) { return globalMap.get(key); } else { const singleton = init(); globalMap.set(key, singleton); return globalMap.get(key); } } /** * @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. * ============================================================================= */ function warn(...msg) { if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) { console.warn(...msg); } } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ const kernelRegistry = getGlobal('kernelRegistry', () => new Map()); const gradRegistry = getGlobal('gradRegistry', () => new Map()); /** * Returns the kernel function (code) associated with the provided names. * * @param kernelName The official name of the kernel. * @param backendName The official name of the backend. */ function getKernel(kernelName, backendName) { const key = makeKey(kernelName, backendName); return kernelRegistry.get(key); } /** * Returns the registered gradient info associated with the provided kernel. * @param kernelName The official TF kernel name. */ function getGradient(kernelName) { return gradRegistry.get(kernelName); } function getKernelsForBackend(backendName) { const it = kernelRegistry.entries(); const result = []; while (true) { const { done, value } = it.next(); if (done) { break; } const [key, config] = value; const [backend,] = key.split('_'); if (backend === backendName) { result.push(config); } } return result; } /** * Registers a gradient function for a given kernel in the global registry, * to be used during the back-propagation of that kernel. * * @param config An object with the following properties: * - `kernelName` The name of the kernel that the gradient function is for. * - `gradFunc` The function to run during back-propagation. */ function registerGradient(config) { const { kernelName } = config; if (gradRegistry.has(kernelName)) { // TODO (yassogba) after 3.0 assess whether we need to keep this gated // to debug mode. if (env().getBool('DEBUG')) { warn(`Overriding the gradient for '${kernelName}'`); } } gradRegistry.set(kernelName, config); } function makeKey(kernelName, backendName) { return `${backendName}_${kernelName}`; } /** * @license * Copyright 2023 Google LLC. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function isTypedArrayBrowser(a) { return a instanceof Float32Array || a instanceof Int32Array || a instanceof Uint8Array || a instanceof Uint8ClampedArray; } var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function getAugmentedNamespace(n) { if (n.__esModule) return n; var f = n.default; if (typeof f == "function") { var a = function a () { if (this instanceof a) { var args = [null]; args.push.apply(args, arguments); var Ctor = Function.bind.apply(f, args); return new Ctor(); } return f.apply(this, arguments); }; a.prototype = f.prototype; } else a = {}; Object.defineProperty(a, '__esModule', {value: true}); Object.keys(n).forEach(function (k) { var d = Object.getOwnPropertyDescriptor(n, k); Object.defineProperty(a, k, d.get ? d : { enumerable: true, get: function () { return n[k]; } }); }); return a; } var long = Long$1; /** * wasm optimizations, to do native i64 multiplication and divide */ var wasm = null; try { wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 ])), {}).exports; } catch (e) { // no wasm support :( } /** * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. * See the from* functions below for more convenient ways of constructing Longs. * @exports Long * @class A Long class for representing a 64 bit two's-complement integer value. * @param {number} low The low (signed) 32 bits of the long * @param {number} high The high (signed) 32 bits of the long * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @constructor */ function Long$1(low, high, unsigned) { /** * The low 32 bits as a signed value. * @type {number} */ this.low = low | 0; /** * The high 32 bits as a signed value. * @type {number} */ this.high = high | 0; /** * Whether unsigned or not. * @type {boolean} */ this.unsigned = !!unsigned; } // The internal representation of a long is the two given signed, 32-bit values. // We use 32-bit pieces because these are the size of integers on which // Javascript performs bit-operations. For operations like addition and // multiplication, we split each number into 16 bit pieces, which can easily be // multiplied within Javascript's floating-point representation without overflow // or change in sign. // // In the algorithms below, we frequently reduce the negative case to the // positive case by negating the input(s) and then post-processing the result. // Note that we must ALWAYS check specially whether those values are MIN_VALUE // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as // a positive number, it overflows back into a negative). Not handling this // case would often result in infinite recursion. // // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* // methods on which they depend. /** * An indicator used to reliably determine if an object is a Long or not. * @type {boolean} * @const * @private */ Long$1.prototype.__isLong__; Object.defineProperty(Long$1.prototype, "__isLong__", { value: true }); /** * @function * @param {*} obj Object * @returns {boolean} * @inner */ function isLong(obj) { return (obj && obj["__isLong__"]) === true; } /** * Tests if the specified object is a Long. * @function * @param {*} obj Object * @returns {boolean} */ Long$1.isLong = isLong; /** * A cache of the Long representations of small integer values. * @type {!Object} * @inner */ var INT_CACHE = {}; /** * A cache of the Long representations of small unsigned integer values. * @type {!Object} * @inner */ var UINT_CACHE = {}; /** * @param {number} value * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromInt(value, unsigned) { var obj, cachedObj, cache; if (unsigned) { value >>>= 0; if (cache = (0 <= value && value < 256)) { cachedObj = UINT_CACHE[value]; if (cachedObj) return cachedObj; } obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true); if (cache) UINT_CACHE[value] = obj; return obj; } else { value |= 0; if (cache = (-128 <= value && value < 128)) { cachedObj = INT_CACHE[value]; if (cachedObj) return cachedObj; } obj = fromBits(value, value < 0 ? -1 : 0, false); if (cache) INT_CACHE[value] = obj; return obj; } } /** * Returns a Long representing the given 32 bit integer value. * @function * @param {number} value The 32 bit integer in question * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {!Long} The corresponding Long value */ Long$1.fromInt = fromInt; /** * @param {number} value * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromNumber(value, unsigned) { if (isNaN(value)) return unsigned ? UZERO : ZERO; if (unsigned) { if (value < 0) return UZERO; if (value >= TWO_PWR_64_DBL) return MAX_UNSIGNED_VALUE; } else { if (value <= -TWO_PWR_63_DBL) return MIN_VALUE; if (value + 1 >= TWO_PWR_63_DBL) return MAX_VALUE; } if (value < 0) return fromNumber(-value, unsigned).neg(); return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); } /** * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. * @function * @param {number} value The number in question * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {!Long} The corresponding Long value */ Long$1.fromNumber = fromNumber; /** * @param {number} lowBits * @param {number} highBits * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromBits(lowBits, highBits, unsigned) { return new Long$1(lowBits, highBits, unsigned); } /** * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is * assumed to use 32 bits. * @function * @param {number} lowBits The low 32 bits * @param {number} highBits The high 32 bits * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {!Long} The corresponding Long value */ Long$1.fromBits = fromBits; /** * @function * @param {number} base * @param {number} exponent * @returns {number} * @inner */ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) /** * @param {string} str * @param {(boolean|number)=} unsigned * @param {number=} radix * @returns {!Long} * @inner */ function fromString(str, unsigned, radix) { if (str.length === 0) throw Error('empty string'); if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") return ZERO; if (typeof unsigned === 'number') { // For goog.math.long compatibility radix = unsigned, unsigned = false; } else { unsigned = !! unsigned; } radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); var p; if ((p = str.indexOf('-')) > 0) throw Error('interior hyphen'); else if (p === 0) { return fromString(str.substring(1), unsigned, radix).neg(); } // Do several (8) digits each time through the loop, so as to // minimize the calls to the very expensive emulated div. var radixToPower = fromNumber(pow_dbl(radix, 8)); var result = ZERO; for (var i = 0; i < str.length; i += 8) { var size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix); if (size < 8) { var power = fromNumber(pow_dbl(radix, size)); result = result.mul(power).add(fromNumber(value)); } else { result = result.mul(radixToPower); result = result.add(fromNumber(value)); } } result.unsigned = unsigned; return result; } /** * Returns a Long representation of the given string, written using the specified radix. * @function * @param {string} str The textual representation of the Long * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 * @returns {!Long} The corresponding Long value */ Long$1.fromString = fromString; /** * @function * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromValue(val, unsigned) { if (typeof val === 'number') return fromNumber(val, unsigned); if (typeof val === 'string') return fromString(val, unsigned); // Throws for non-objects, converts non-instanceof Long: return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); } /** * Converts the specified value to a Long using the appropriate from* function for its type. * @function * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {!Long} */ Long$1.fromValue = fromValue; // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be // no runtime penalty for these. /** * @type {number} * @const * @inner */ var TWO_PWR_16_DBL = 1 << 16; /** * @type {number} * @const * @inner */ var TWO_PWR_24_DBL = 1 << 24; /** * @type {number} * @const * @inner */ var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; /** * @type {number} * @const * @inner */ var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; /** * @type {number} * @const * @inner */ var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; /** * @type {!Long} * @const * @inner */ var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); /** * @type {!Long} * @inner */ var ZERO = fromInt(0); /** * Signed zero. * @type {!Long} */ Long$1.ZERO = ZERO; /** * @type {!Long} * @inner */ var UZERO = fromInt(0, true); /** * Unsigned zero. * @type {!Long} */ Long$1.UZERO = UZERO; /** * @type {!Long} * @inner */ var ONE = fromInt(1); /** * Signed one. * @type {!Long} */ Long$1.ONE = ONE; /** * @type {!Long} * @inner */ var UONE = fromInt(1, true); /** * Unsigned one. * @type {!Long} */ Long$1.UONE = UONE; /** * @type {!Long} * @inner */ var NEG_ONE = fromInt(-1); /** * Signed negative one. * @type {!Long} */ Long$1.NEG_ONE = NEG_ONE; /** * @type {!Long} * @inner */ var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); /** * Maximum signed value. * @type {!Long} */ Long$1.MAX_VALUE = MAX_VALUE; /** * @type {!Long} * @inner */ var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); /** * Maximum unsigned value. * @type {!Long} */ Long$1.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; /** * @type {!Long} * @inner */ var MIN_VALUE = fromBits(0, 0x80000000|0, false); /** * Minimum signed value. * @type {!Long} */ Long$1.MIN_VALUE = MIN_VALUE; /** * @alias Long.prototype * @inner */ var LongPrototype = Long$1.prototype; /** * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. * @returns {number} */ LongPrototype.toInt = function toInt() { return this.unsigned ? this.low >>> 0 : this.low; }; /** * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). * @returns {number} */ LongPrototype.toNumber = function toNumber() { if (this.unsigned) return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); return this.high * TWO_PWR_32_DBL + (this.low >>> 0); }; /** * Converts the Long to a string written in the specified radix. * @param {number=} radix Radix (2-36), defaults to 10 * @returns {string} * @override * @throws {RangeError} If `radix` is out of range */ LongPrototype.toString = function toString(radix) { radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); if (this.isZero()) return '0'; if (this.isNegative()) { // Unsigned Longs are never negative if (this.eq(MIN_VALUE)) { // We need to change the Long value before it can be negated, so we remove // the bottom-most digit in this base and then recurse to do the rest. var radixLong = fromNumber(radix), div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this); return div.toString(radix) + rem1.toInt().toString(radix); } else return '-' + this.neg().toString(radix); } // Do several (6) digits each time through the loop, so as to // minimize the calls to the very expensive emulated div. var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), rem = this; var result = ''; while (true) { var remDiv = rem.div(radixToPower), intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, digits = intval.toString(radix); rem = remDiv; if (rem.isZero()) return digits + result; else { while (digits.length < 6) digits = '0' + digits; result = '' + digits + result; } } }; /** * Gets the high 32 bits as a signed integer. * @returns {number} Signed high bits */ LongPrototype.getHighBits = function getHighBits() { return this.high; }; /** * Gets the high 32 bits as an unsigned integer. * @returns {number} Unsigned high bits */ LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() { return this.high >>> 0; }; /** * Gets the low 32 bits as a signed integer. * @returns {number} Signed low bits */ LongPrototype.getLowBits = function getLowBits() { return this.low; }; /** * Gets the low 32 bits as an unsigned integer. * @returns {number} Unsigned low bits */ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { return this.low >>> 0; }; /** * Gets the number of bits needed to represent the absolute value of this Long. * @returns {number} */ LongPrototype.getNumBitsAbs = function getNumBitsAbs() { if (this.isNegative()) // Unsigned Longs are never negative return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); var val = this.high != 0 ? this.high : this.low; for (var bit = 31; bit > 0; bit--) if ((val & (1 << bit)) != 0) break; return this.high != 0 ? bit + 33 : bit + 1; }; /** * Tests if this Long's value equals zero. * @returns {boolean} */ LongPrototype.isZero = function isZero() { return this.high === 0 && this.low === 0; }; /** * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}. * @returns {boolean} */ LongPrototype.eqz = LongPrototype.isZero; /** * Tests if this Long's value is negative. * @returns {boolean} */ LongPrototype.isNegative = function isNegative() { return !this.unsigned && this.high < 0; }; /** * Tests if this Long's value is positive. * @returns {boolean} */ LongPrototype.isPositive = function isPositive() { return this.unsigned || this.high >= 0; }; /** * Tests if this Long's value is odd. * @returns {boolean} */ LongPrototype.isOdd = function isOdd() { return (this.low & 1) === 1; }; /** * Tests if this Long's value is even. * @returns {boolean} */ LongPrototype.isEven = function isEven() { return (this.low & 1) === 0; }; /** * Tests if this Long's value equals the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.equals = function equals(other) { if (!isLong(other)) other = fromValue(other); if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) return false; return this.high === other.high && this.low === other.low; }; /** * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.eq = LongPrototype.equals; /** * Tests if this Long's value differs from the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.notEquals = function notEquals(other) { return !this.eq(/* validates */ other); }; /** * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.neq = LongPrototype.notEquals; /** * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.ne = LongPrototype.notEquals; /** * Tests if this Long's value is less than the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.lessThan = function lessThan(other) { return this.comp(/* validates */ other) < 0; }; /** * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.lt = LongPrototype.lessThan; /** * Tests if this Long's value is less than or equal the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { return this.comp(/* validates */ other) <= 0; }; /** * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.lte = LongPrototype.lessThanOrEqual; /** * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.le = LongPrototype.lessThanOrEqual; /** * Tests if this Long's value is greater than the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.greaterThan = function greaterThan(other) { return this.comp(/* validates */ other) > 0; }; /** * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.gt = LongPrototype.greaterThan; /** * Tests if this Long's value is greater than or equal the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { return this.comp(/* validates */ other) >= 0; }; /** * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.gte = LongPrototype.greaterThanOrEqual; /** * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} */ LongPrototype.ge = LongPrototype.greaterThanOrEqual; /** * Compares this Long's value with the specified's. * @param {!Long|number|string} other Other value * @returns {number} 0 if they are the same, 1 if the this is greater and -1 * if the given one is greater */ LongPrototype.compare = function compare(other) { if (!isLong(other)) other = fromValue(other); if (this.eq(other)) return 0; var thisNeg = this.isNegative(), otherNeg = other.isNegative(); if (thisNeg && !otherNeg) return -1; if (!thisNeg && otherNeg) return 1; // At this point the sign bits are the same if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1; // Both are positive if at least one is unsigned return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; }; /** * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}. * @function * @param {!Long|number|string} other Other value * @returns {number} 0 if they are the same, 1 if the this is greater and -1 * if the given one is greater */ LongPrototype.comp = LongPrototype.compare; /** * Negates this Long's value. * @returns {!Long} Negated Long */ LongPrototype.negate = function negate() { if (!this.unsigned && this.eq(MIN_VALUE)) return MIN_VALUE; return this.not().add(ONE); }; /** * Negates this Long's value. This is an alias of {@link Long#negate}. * @function * @returns {!Long} Negated Long */ LongPrototype.neg = LongPrototype.negate; /** * Returns the sum of this and the specified Long. * @param {!Long|number|string} addend Addend * @returns {!Long} Sum */ LongPrototype.add = function add(addend) { if (!isLong(addend)) addend = fromValue(addend); // Divide each number into 4 chunks of 16 bits, and then sum the chunks. var a48 = this.high >>> 16; var a32 = this.high & 0xFFFF; var a16 = this.low >>> 16; var a00 = this.low & 0xFFFF; var b48 = addend.high >>> 16; var b32 = addend.high & 0xFFFF; var b16 = addend.low >>> 16; var b00 = addend.low & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 + b00; c16 += c00 >>> 16; c00 &= 0xFFFF; c16 += a16 + b16; c32 += c16 >>> 16; c16 &= 0xFFFF; c32 += a32 + b32; c48 += c32 >>> 16; c32 &= 0xFFFF; c48 += a48 + b48; c48 &= 0xFFFF; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; /** * Returns the difference of this and the specified Long. * @param {!Long|number|string} subtrahend Subtrahend * @returns {!Long} Difference */ LongPrototype.subtract = function subtract(subtrahend) { if (!isLong(subtrahend)) subtrahend = fromValue(subtrahend); return this.add(subtrahend.neg()); }; /** * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}. * @function * @param {!Long|number|string} subtrahend Subtrahend * @returns {!Long} Difference */ LongPrototype.sub = LongPrototype.subtract; /** * Returns the product of this and the specified Long. * @param {!Long|number|string} multiplier Multiplier * @returns {!Long} Product */ LongPrototype.multiply = function multiply(multiplier) { if (this.isZero()) return ZERO; if (!isLong(multiplier)) multiplier = fromValue(multiplier); // use wasm support if present if (wasm) { var low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high); return fromBits(low, wasm.get_high(), this.unsigned); } if (multiplier.isZero()) return ZERO; if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO; if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO; if (this.isNegative()) { if (multiplier.isNegative()) return this.neg().mul(multiplier.neg()); else return this.neg().mul(multiplier).neg(); } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg(); // If both longs are small, use float multiplication if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. // We can skip products that would overflow. var a48 = this.high >>> 16; var a32 = this.high & 0xFFFF; var a16 = this.low >>> 16; var a00 = this.low & 0xFFFF; var b48 = multiplier.high >>> 16; var b32 = multiplier.high & 0xFFFF; var b16 = multiplier.low >>> 16; var b00 = multiplier.low & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 * b00; c16 += c00 >>> 16; c00 &= 0xFFFF; c16 += a16 * b00; c32 += c16 >>> 16; c16 &= 0xFFFF; c16 += a00 * b16; c32 += c16 >>> 16; c16 &= 0xFFFF; c32 += a32 * b00; c48 += c32 >>> 16; c32 &= 0xFFFF; c32 += a16 * b16; c48 += c32 >>> 16; c32 &= 0xFFFF; c32 += a00 * b32; c48 += c32 >>> 16; c32 &= 0xFFFF; c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; c48 &= 0xFFFF; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; /** * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}. * @function * @param {!Long|number|string} multiplier Multiplier * @returns {!Long} Product */ LongPrototype.mul = LongPrototype.multiply; /** * Returns this Long divided by the specified. The result is signed if this Long is signed or * unsigned if this Long is unsigned. * @param {!Long|number|string} divisor Divisor * @returns {!Long} Quotient */ LongPrototype.divide = function divide(divisor) { if (!isLong(divisor)) divisor = fromValue(divisor); if (divisor.isZero()) throw Error('division by zero'); // use wasm support if present if (wasm) { // guard against signed division overflow: the largest // negative number / -1 would be 1 larger than the largest // positive number, due to two's complement. if (!this.unsigned && this.high === -0x80000000 && divisor.low === -1 && divisor.high === -1) { // be consistent with non-wasm code path return this; } var low = (this.unsigned ? wasm.div_u : wasm.div_s)( this.low, this.high, divisor.low, divisor.high ); return fromBits(low, wasm.get_high(), this.unsigned); } if (this.isZero()) return this.unsigned ? UZERO : ZERO; var approx, rem, res; if (!this.unsigned) { // This section is only relevant for signed longs and is derived from the // closure library as a whole. if (this.eq(MIN_VALUE)) { if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE else if (divisor.eq(MIN_VALUE)) return ONE; else { // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. var halfThis = this.shr(1); approx = halfThis.div(divisor).shl(1); if (approx.eq(ZERO)) { return divisor.isNegative() ? ONE : NEG_ONE; } else { rem = this.sub(divisor.mul(approx)); res = approx.add(rem.div(divisor)); return res; } } } else if (divisor.eq(MIN_VALUE)) return this.unsigned ? UZERO : ZERO; if (this.isNegative()) { if (divisor.isNegative()) return this.neg().div(divisor.neg()); return this.neg().div(divisor).neg(); } else if (divisor.isNegative()) return this.div(divisor.neg()).neg(); res = ZERO; } else { // The algorithm below has not been made for unsigned longs. It's therefore // required to take special care of the MSB prior to running it. if (!divisor.unsigned) divisor = divisor.toUnsigned(); if (divisor.gt(this)) return UZERO; if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true return UONE; res = UZERO; } // Repeat the following until the remainder is less than other: find a // floating-point that approximates remainder / other *from below*, add this // into the result, and subtract it from the remainder. It is critical that // the approximate value is less than or equal to the real value so that the // remainder never becomes negative. rem = this; while (rem.gte(divisor)) { // Approximate the result of division. This may be a little greater or // smaller than the actual value. approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); // We will tweak the approximate result by changing it in the 48-th digit or // the smallest non-fractional digit, whichever is larger. var log2 = Math.ceil(Math.log(approx) / Math.LN2), delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), // Decrease the approximation until it is smaller than the remainder. Note // that if it is too large, the product overflows and is negative. approxRes = fromNumber(approx), approxRem = approxRes.mul(divisor); while (approxRem.isNegative() || approxRem.gt(rem)) { approx -= delta; approxRes = fromNumber(approx, this.unsigned); approxRem = approxRes.mul(divisor); } // We know the answer can't be zero... and actually, zero would cause // infinite recursion since we would make no progress. if (approxRes.isZero()) approxRes = ONE; res = res.add(approxRes); rem = rem.sub(approxRem); } return res; }; /** * Returns this Long divided by the specified. This is an alias of {@link Long#divide}. * @function * @param {!Long|number|string} divisor Divisor * @returns {!Long} Quotient */ LongPrototype.div = LongPrototype.divide; /** * Returns this Long modulo the specified. * @param {!Long|number|string} divisor Divisor * @returns {!Long} Remainder */ LongPrototype.modulo = function modulo(divisor) { if (!isLong(divisor)) divisor = fromValue(divisor); // use wasm support if present if (wasm) { var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)( this.low, this.high, divisor.low, divisor.high ); return fromBits(low, wasm.get_high(), this.unsigned); } return this.sub(this.div(divisor).mul(divisor)); }; /** * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. * @function * @param {!Long|number|string} divisor Divisor * @returns {!Long} Remainder */ LongPrototype.mod = LongPrototype.modulo; /** * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. * @function * @param {!Long|number|string} divisor Divisor * @returns {!Long} Remainder */ LongPrototype.rem = LongPrototype.modulo; /** * Returns the bitwise NOT of this Long. * @returns {!Long} */ LongPrototype.not = function not() { return fromBits(~this.low, ~this.high, this.unsigned); }; /** * Returns the bitwise AND of this Long and the specified. * @param {!Long|number|string} other Other Long * @returns {!Long} */ LongPrototype.and = function and(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low & other.low, this.high & other.high, this.unsigned); }; /** * Returns the bitwise OR of this Long and the specified. * @param {!Long|number|string} other Other Long * @returns {!Long} */ LongPrototype.or = function or(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low | other.low, this.high | other.high, this.unsigned); }; /** * Returns the bitwise XOR of this Long and the given one. * @param {!Long|number|string} other Other Long * @returns {!Long} */ LongPrototype.xor = function xor(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); }; /** * Returns this Long with bits shifted to the left by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shiftLeft = function shiftLeft(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); if ((numBits &= 63) === 0) return this; else if (numBits < 32) return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); else return fromBits(0, this.low << (numBits - 32), this.unsigned); }; /** * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shl = LongPrototype.shiftLeft; /** * Returns this Long with bits arithmetically shifted to the right by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shiftRight = function shiftRight(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); if ((numBits &= 63) === 0) return this; else if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); else return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); }; /** * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shr = LongPrototype.shiftRight; /** * Returns this Long with bits logically shifted to the right by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); numBits &= 63; if (numBits === 0) return this; else { var high = this.high; if (numBits < 32) { var low = this.low; return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); } else if (numBits === 32) return fromBits(high, 0, this.unsigned); else return fromBits(high >>> (numBits - 32), 0, this.unsigned); } }; /** * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shru = LongPrototype.shiftRightUnsigned; /** * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long */ LongPrototype.shr_u = LongPrototype.shiftRightUnsigned; /** * Converts this Long to signed. * @returns {!Long} Signed long */ LongPrototype.toSigned = function toSigned() { if (!this.unsigned) return this; return fromBits(this.low, this.high, false); }; /** * Converts this Long to unsigned. * @returns {!Long} Unsigned long */ LongPrototype.toUnsigned = function toUnsigned() { if (this.unsigned) return this; return fromBits(this.low, this.high, true); }; /** * Converts this Long to its byte representation. * @param {boolean=} le Whether little or big endian, defaults to big endian * @returns {!Array.} Byte representation */ LongPrototype.toBytes = function toBytes(le) { return le ? this.toBytesLE() : this.toBytesBE(); }; /** * Converts this Long to its little endian byte representation. * @returns {!Array.} Little endian byte representation */ LongPrototype.toBytesLE = function toBytesLE() { var hi = this.high, lo = this.low; return [ lo & 0xff, lo >>> 8 & 0xff, lo >>> 16 & 0xff, lo >>> 24 , hi & 0xff, hi >>> 8 & 0xff, hi >>> 16 & 0xff, hi >>> 24 ]; }; /** * Converts this Long to its big endian byte representation. * @returns {!Array.} Big endian byte representation */ LongPrototype.toBytesBE = function toBytesBE() { var hi = this.high, lo = this.low; return [ hi >>> 24 , hi >>> 16 & 0xff, hi >>> 8 & 0xff, hi & 0xff, lo >>> 24 , lo >>> 16 & 0xff, lo >>> 8 & 0xff, lo & 0xff ]; }; /** * Creates a Long from its byte representation. * @param {!Array.} bytes Byte representation * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @param {boolean=} le Whether little or big endian, defaults to big endian * @returns {Long} The corresponding Long value */ Long$1.fromBytes = function fromBytes(bytes, unsigned, le) { return le ? Long$1.fromBytesLE(bytes, unsigned) : Long$1.fromBytesBE(bytes, unsigned); }; /** * Creates a Long from its little endian byte representation. * @param {!Array.} bytes Little endian byte representation * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {Long} The corresponding Long value */ Long$1.fromBytesLE = function fromBytesLE(bytes, unsigned) { return new Long$1( bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24, bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24, unsigned ); }; /** * Creates a Long from its big endian byte representation. * @param {!Array.} bytes Big endian byte representation * @param {boolean=} unsigned Whether unsigned or not, defaults to signed * @returns {Long} The corresponding Long value */ Long$1.fromBytesBE = function fromBytesBE(bytes, unsigned) { return new Long$1( bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7], bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], unsigned ); }; var long$1 = /*@__PURE__*/getDefaultExportFromCjs(long); var LongExports = /*#__PURE__*/_mergeNamespaces({ __proto__: null, default: long$1 }, [long]); /** * @license * Copyright 2021 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. * ============================================================================= */ // tslint:disable-next-line const Long = // tslint:disable-next-line long$1 || LongExports; function hexToLong(hex) { return Long.fromString(hex, true, 16); } // Some primes between 2^63 and 2^64 for various uses. // Hex 0xc3a5c85c97cb3127 hexToLong('c3a5c85c97cb3127'); // Hex 0xb492b66fbe98f273 hexToLong('b492b66fbe98f273'); // Hex 0x9ae16a3b2f90404f hexToLong('9ae16a3b2f90404f'); /** * @license * Copyright 2017 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. * ============================================================================= */ function noConversionNeeded(a, dtype) { return (a instanceof Float32Array && dtype === 'float32') || (a instanceof Int32Array && dtype === 'int32') || (a instanceof Uint8Array && dtype === 'bool'); } function toTypedArray(a, dtype) { if (dtype === 'string') { throw new Error('Cannot convert a string[] to a TypedArray'); } if (Array.isArray(a)) { a = flatten$1(a); } if (env().getBool('DEBUG')) { checkConversionForErrors(a, dtype); } if (noConversionNeeded(a, dtype)) { return a; } if (dtype == null || dtype === 'float32' || dtype === 'complex64') { return new Float32Array(a); } else if (dtype === 'int32') { return new Int32Array(a); } else if (dtype === 'bool') { const bool = new Uint8Array(a.length); for (let i = 0; i < bool.length; ++i) { if (Math.round(a[i]) !== 0) { bool[i] = 1; } } return bool; } else { throw new Error(`Unknown data type ${dtype}`); } } /** * Returns the current high-resolution time in milliseconds relative to an * arbitrary time in the past. It works across different platforms (node.js, * browsers). * * ```js * console.log(tf.util.now()); * ``` * * @doc {heading: 'Util', namespace: 'util'} */ function now() { return env().platform.now(); } /** * Encodes the provided string into bytes using the provided encoding scheme. * * @param s The string to encode. * @param encoding The encoding scheme. Defaults to utf-8. * * @doc {heading: 'Util'} */ function encodeString(s, encoding = 'utf-8') { encoding = encoding || 'utf-8'; return env().platform.encode(s, encoding); } /** * Decodes the provided bytes into a string using the provided encoding scheme. * @param bytes The bytes to decode. * * @param encoding The encoding scheme. Defaults to utf-8. * * @doc {heading: 'Util'} */ function decodeString(bytes, encoding = 'utf-8') { encoding = encoding || 'utf-8'; return env().platform.decode(bytes, encoding); } function isTypedArray(a) { // TODO(mattsoulanille): Remove this fallback in 5.0.0 if (env().platform.isTypedArray != null) { return env().platform.isTypedArray(a); } else { return isTypedArrayBrowser(a); } } // NOTE: We explicitly type out what T extends instead of any so that // util.flatten on a nested array of number doesn't try to infer T as a // number[][], causing us to explicitly type util.flatten(). /** * Flattens an arbitrarily nested array. * * ```js * const a = [[1, 2], [3, 4], [5, [6, [7]]]]; * const flat = tf.util.flatten(a); * console.log(flat); * ``` * * @param arr The nested array to flatten. * @param result The destination array which holds the elements. * @param skipTypedArray If true, avoids flattening the typed arrays. Defaults * to false. * * @doc {heading: 'Util', namespace: 'util'} */ function flatten$1(arr, result = [], skipTypedArray = false) { if (result == null) { result = []; } if (typeof arr === 'boolean' || typeof arr === 'number' || typeof arr === 'string' || isPromise(arr) || arr == null || isTypedArray(arr) && skipTypedArray) { result.push(arr); } else if (Array.isArray(arr) || isTypedArray(arr)) { for (let i = 0; i < arr.length; ++i) { flatten$1(arr[i], result, skipTypedArray); } } else { let maxIndex = -1; for (const key of Object.keys(arr)) { // 0 or positive integer. if (/^([1-9]+[0-9]*|0)$/.test(key)) { maxIndex = Math.max(maxIndex, Number(key)); } } for (let i = 0; i <= maxIndex; i++) { // tslint:disable-next-line: no-unnecessary-type-assertion flatten$1(arr[i], result, skipTypedArray); } } return result; } /** * @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. * ============================================================================= */ class Profiler { constructor(backendTimer, logger) { this.backendTimer = backendTimer; this.logger = logger; if (logger == null) { this.logger = new Logger(); } } profileKernel(kernelName, inputs, f) { let outputs; const holdResultWrapperFn = () => { outputs = f(); }; let timer; const start = now(); if (this.backendTimer.timerAvailable()) { timer = this.backendTimer.time(holdResultWrapperFn); } else { holdResultWrapperFn(); for (const output of outputs) { output.dataSync(); } timer = Promise.resolve({ kernelMs: now() - start }); } if (env().getBool('CHECK_COMPUTATION_FOR_ERRORS')) { for (let i = 0; i < outputs.length; i++) { const output = outputs[i]; // Dangling promise here because we don't want to propagate up // asynchronicity. output.data().then(tensorVals => { checkComputationForErrors(tensorVals, output.dtype, kernelName); }); } } const kernelProfile = { kernelName, outputs, inputs, timeMs: timer.then(timing => timing.kernelMs), extraInfo: timer.then(timing => timing.getExtraProfileInfo != null ? timing.getExtraProfileInfo() : '') }; return kernelProfile; } logKernelProfile(kernelProfile) { const { kernelName, outputs, timeMs, inputs, extraInfo } = kernelProfile; outputs.forEach(result => { Promise.all([result.data(), timeMs, extraInfo]).then(valueContainer => { this.logger.logKernelProfile(kernelName, result, valueContainer[0], valueContainer[1], inputs, valueContainer[2]); }); }); } } function checkComputationForErrors(vals, dtype, kernelName) { if (dtype !== 'float32') { // Only floating point computations will generate NaN values return false; } for (let i = 0; i < vals.length; i++) { const num = vals[i]; if (isNaN(num) || !isFinite(num)) { // Throwing custom exception so behavior is testable. console.warn(`Found ${num} in the result of '${kernelName}'`); return true; } } return false; } class Logger { logKernelProfile(name, result, vals, timeMs, inputs, extraInfo) { const time = typeof timeMs === 'number' ? rightPad(`${timeMs}ms`, 9) : timeMs['error']; const paddedName = rightPad(name, 25); const rank = result.rank; const size = result.size; const shape = rightPad(result.shape.toString(), 14); let inputShapesDescription = ''; for (const name in inputs) { const input = inputs[name]; if (input != null) { // The input might be a non-tensor (e.g HTMLImageElement), in which case // we claim the output shape as input shape. const inputShape = input.shape || result.shape; const inputRank = inputShape.length; inputShapesDescription += `${name}: ${inputRank}D ${inputRank > 0 ? inputShape : ''} `; } } console.log(`%c${paddedName}\t%c${time}\t%c${rank}D ${shape}\t%c${size}\t%c${inputShapesDescription}\t%c${extraInfo}`, 'font-weight:bold', 'color:red', 'color:blue', 'color: orange', 'color: green', 'color: steelblue'); } } /** * @license * Copyright 2017 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. * ============================================================================= */ /** * Computes a list of TapeNodes that connect x to y, filtering everything else * out and preserving the order of the original tape elements. * * @param tape The tape elements to filter. * @param xs The input Tensors. * @param y The output Tensor. */ function getFilteredNodesXToY(tape, xs, y) { // Forward pass to compute all the nodes and Tensors that are transitively a // function of x. const tensorsFromX = {}; const nodesFromX = {}; for (let i = 0; i < xs.length; i++) { tensorsFromX[xs[i].id] = true; } for (let i = 0; i < tape.length; i++) { const node = tape[i]; const nodeInputs = node.inputs; for (const inputName in nodeInputs) { const input = nodeInputs[inputName]; let anyInputFromX = false; for (let j = 0; j < xs.length; j++) { if (tensorsFromX[input.id]) { node.outputs.forEach(output => tensorsFromX[output.id] = true); anyInputFromX = true; nodesFromX[node.id] = true; break; } } if (anyInputFromX) { break; } } } // Backward pass to find all of the nodes and Tensors that lead to y. const tensorsLeadToY = {}; tensorsLeadToY[y.id] = true; const nodesToY = {}; for (let i = tape.length - 1; i >= 0; i--) { const node = tape[i]; const nodeInputs = node.inputs; // If any of the outputs lead to y, mark all of the inputs as leading to y. for (let j = 0; j < node.outputs.length; j++) { if (tensorsLeadToY[node.outputs[j].id]) { for (const inputName in nodeInputs) { tensorsLeadToY[nodeInputs[inputName].id] = true; nodesToY[node.id] = true; } break; } } } // Return the paths that come from x and lead to y. const filteredTape = []; for (let i = 0; i < tape.length; i++) { const node = tape[i]; if (nodesFromX[node.id] && nodesToY[node.id]) { // Prune the inputs from the node that aren't a function of x. const prunedInputs = {}; for (const inputName in node.inputs) { const nodeInput = node.inputs[inputName]; if (tensorsFromX[nodeInput.id]) { prunedInputs[inputName] = nodeInput; } } // Copy the node and overwrite inputsAndArgs to the pruned version. const prunedNode = Object.assign({}, node); prunedNode.inputs = prunedInputs; prunedNode.outputs = node.outputs; filteredTape.push(prunedNode); } } return filteredTape; } /** * Backpropagate gradients through the filtered TapeNodes. * * @param tensorAccumulatedGradientMap A map of Tensor to its gradient. This map * is mutated by this method. * @param filteredTape The filtered TapeNodes to backprop through. */ function backpropagateGradients(tensorAccumulatedGradientMap, filteredTape, tidy, add) { // Walk the tape backward and keep a map of Tensor to its gradient. for (let i = filteredTape.length - 1; i >= 0; i--) { const node = filteredTape[i]; const dys = []; node.outputs.forEach(o => { const gradTensor = tensorAccumulatedGradientMap[o.id]; if (gradTensor != null) { dys.push(gradTensor); } else { // This particular output is not in the back-propagation subgraph, so it // does not affect the final output, thus we put null for its dy. dys.push(null); } }); if (node.gradient == null) { throw new Error(`Cannot compute gradient: gradient function not found ` + `for ${node.kernelName}.`); } // Backprop dy through this node and accumulate gradients over the inputs. const inputGradients = node.gradient(dys); for (const inputName in node.inputs) { if (!(inputName in inputGradients)) { throw new Error(`Cannot backprop through input ${inputName}. ` + `Available gradients found: ${Object.keys(inputGradients)}.`); } // Call the gradient function. const dx = tidy(() => inputGradients[inputName]()); if (dx.dtype !== 'float32') { throw new Error(`Error in gradient for op ${node.kernelName}. The gradient of input ` + `${inputName} must have 'float32' dtype, but has '${dx.dtype}'`); } const x = node.inputs[inputName]; if (!arraysEqual(dx.shape, x.shape)) { throw new Error(`Error in gradient for op ${node.kernelName}. The gradient of input ` + `'${inputName}' has shape '${dx.shape}', which does not match ` + `the shape of the input '${x.shape}'`); } if (tensorAccumulatedGradientMap[x.id] == null) { tensorAccumulatedGradientMap[x.id] = dx; } else { const curGradient = tensorAccumulatedGradientMap[x.id]; tensorAccumulatedGradientMap[x.id] = add(curGradient, dx); curGradient.dispose(); } } } } /** * @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. * ============================================================================= */ // Maximum number of values before we decide to show ellipsis. const FORMAT_LIMIT_NUM_VALS = 20; // Number of first and last values to show when displaying a, b,...,y, z. const FORMAT_NUM_FIRST_LAST_VALS = 3; // Number of significant digits to show. const FORMAT_NUM_SIG_DIGITS = 7; function tensorToString(vals, shape, dtype, verbose) { const strides = computeStrides(shape); const padPerCol = computeMaxSizePerColumn(vals, shape, dtype, strides); const rank = shape.length; const valsLines = subTensorToString(vals, shape, dtype, strides, padPerCol); const lines = ['Tensor']; if (verbose) { lines.push(` dtype: ${dtype}`); lines.push(` rank: ${rank}`); lines.push(` shape: [${shape}]`); lines.push(` values:`); } lines.push(valsLines.map(l => ' ' + l).join('\n')); return lines.join('\n'); } function computeMaxSizePerColumn(vals, shape, dtype, strides) { const n = sizeFromShape(shape); const numCols = strides[strides.length - 1]; const padPerCol = new Array(numCols).fill(0); const rank = shape.length; const valuesOrTuples = dtype === 'complex64' ? createComplexTuples(vals) : vals; if (rank > 1) { for (let row = 0; row < n / numCols; row++) { const offset = row * numCols; for (let j = 0; j < numCols; j++) { padPerCol[j] = Math.max(padPerCol[j], valToString(valuesOrTuples[offset + j], 0, dtype).length); } } } return padPerCol; } function valToString(val, pad, dtype) { let valStr; if (Array.isArray(val)) { valStr = `${parseFloat(val[0].toFixed(FORMAT_NUM_SIG_DIGITS))} + ` + `${parseFloat(val[1].toFixed(FORMAT_NUM_SIG_DIGITS))}j`; } else if (isString(val)) { valStr = `'${val}'`; } else if (dtype === 'bool') { valStr = boolNumToString(val); } else { valStr = parseFloat(val.toFixed(FORMAT_NUM_SIG_DIGITS)).toString(); } return rightPad(valStr, pad); } function boolNumToString(v) { return v === 0 ? 'false' : 'true'; } function subTensorToString(vals, shape, dtype, strides, padPerCol, isLast = true) { const storagePerElement = dtype === 'complex64' ? 2 : 1; const size = shape[0]; const rank = shape.length; if (rank === 0) { if (dtype === 'complex64') { const complexTuple = createComplexTuples(vals); return [valToString(complexTuple[0], 0, dtype)]; } if (dtype === 'bool') { return [boolNumToString(vals[0])]; } return [vals[0].toString()]; } if (rank === 1) { if (size > FORMAT_LIMIT_NUM_VALS) { const firstValsSize = FORMAT_NUM_FIRST_LAST_VALS * storagePerElement; let firstVals = Array.from(vals.slice(0, firstValsSize)); let lastVals = Array.from(vals.slice((size - FORMAT_NUM_FIRST_LAST_VALS) * storagePerElement, size * storagePerElement)); if (dtype === 'complex64') { firstVals = createComplexTuples(firstVals); lastVals = createComplexTuples(lastVals); } return [ '[' + firstVals.map((x, i) => valToString(x, padPerCol[i], dtype)) .join(', ') + ', ..., ' + lastVals .map((x, i) => valToString(x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i], dtype)) .join(', ') + ']' ]; } const displayVals = dtype === 'complex64' ? createComplexTuples(vals) : Array.from(vals); return [ '[' + displayVals.map((x, i) => valToString(x, padPerCol[i], dtype)) .join(', ') + ']' ]; } // The array is rank 2 or more. const subshape = shape.slice(1); const substrides = strides.slice(1); const stride = strides[0] * storagePerElement; const lines = []; if (size > FORMAT_LIMIT_NUM_VALS) { for (let i = 0; i < FORMAT_NUM_FIRST_LAST_VALS; i++) { const start = i * stride; const end = start + stride; lines.push(...subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, false /* isLast */)); } lines.push('...'); for (let i = size - FORMAT_NUM_FIRST_LAST_VALS; i < size; i++) { const start = i * stride; const end = start + stride; lines.push(...subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, i === size - 1 /* isLast */)); } } else { for (let i = 0; i < size; i++) { const start = i * stride; const end = start + stride; lines.push(...subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, i === size - 1 /* isLast */)); } } const sep = rank === 2 ? ',' : ''; lines[0] = '[' + (size > 0 ? lines[0] + sep : ''); for (let i = 1; i < lines.length - 1; i++) { lines[i] = ' ' + lines[i] + sep; } let newLineSep = ',\n'; for (let i = 2; i < rank; i++) { newLineSep += '\n'; } lines[lines.length - 1] = ' ' + lines[lines.length - 1] + ']' + (isLast ? '' : newLineSep); return lines; } function createComplexTuples(vals) { const complexTuples = []; for (let i = 0; i < vals.length; i += 2) { complexTuples.push([vals[i], vals[i + 1]]); } return complexTuples; } /** * @license * Copyright 2017 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. * ============================================================================= */ // For tracking tensor creation and disposal. let trackerFn = null; // Used by chaining methods to call into ops. let opHandler = null; /** * An external consumer can register itself as the tensor tracker. This way * the Tensor class can notify the tracker for every tensor created and * disposed. */ function setTensorTracker(fn) { trackerFn = fn; } /** * A `tf.Tensor` object represents an immutable, multidimensional array of * numbers that has a shape and a data type. * * For performance reasons, functions that create tensors do not necessarily * perform a copy of the data passed to them (e.g. if the data is passed as a * `Float32Array`), and changes to the data will change the tensor. This is not * a feature and is not supported. To avoid this behavior, use the tensor before * changing the input data or create a copy with `copy = tf.add(yourTensor, 0)`. * * See `tf.tensor` for details on how to create a `tf.Tensor`. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ class Tensor { constructor(shape, dtype, dataId, id) { /** Whether this tensor has been globally kept. */ this.kept = false; this.isDisposedInternal = false; this.shape = shape.slice(); this.dtype = dtype || 'float32'; this.size = sizeFromShape(shape); this.strides = computeStrides(shape); this.dataId = dataId; this.id = id; this.rankType = (this.rank < 5 ? this.rank.toString() : 'higher'); } get rank() { return this.shape.length; } /** * Returns a promise of `tf.TensorBuffer` that holds the underlying data. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ async buffer() { const vals = await this.data(); return opHandler.buffer(this.shape, this.dtype, vals); } /** * Returns a `tf.TensorBuffer` that holds the underlying data. * @doc {heading: 'Tensors', subheading: 'Classes'} */ bufferSync() { return opHandler.buffer(this.shape, this.dtype, this.dataSync()); } /** * Returns the tensor data as a nested array. The transfer of data is done * asynchronously. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ async array() { const vals = await this.data(); return toNestedArray(this.shape, vals, this.dtype === 'complex64'); } /** * Returns the tensor data as a nested array. The transfer of data is done * synchronously. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ arraySync() { return toNestedArray(this.shape, this.dataSync(), this.dtype === 'complex64'); } /** * Asynchronously downloads the values from the `tf.Tensor`. Returns a * promise of `TypedArray` that resolves when the computation has finished. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ async data() { this.throwIfDisposed(); const data = trackerFn().read(this.dataId); if (this.dtype === 'string') { const bytes = await data; try { return bytes.map(b => decodeString(b)); } catch (_a) { throw new Error('Failed to decode the string bytes into utf-8. ' + 'To get the original bytes, call tensor.bytes().'); } } return data; } /** * Copy the tensor's data to a new GPU resource. Comparing to the `dataSync()` * and `data()`, this method prevents data from being downloaded to CPU. * * For WebGL backend, the data will be stored on a densely packed texture. * This means that the texture will use the RGBA channels to store value. * * For WebGPU backend, the data will be stored on a buffer. There is no * parameter, so can not use a user-defined size to create the buffer. * * @param options: * For WebGL, * - customTexShape: Optional. If set, will use the user defined * texture shape to create the texture. * * @returns For WebGL backend, a GPUData contains the new texture and * its information. * { * tensorRef: The tensor that is associated with this texture, * texture: WebGLTexture, * texShape: [number, number] // [height, width] * } * * For WebGPU backend, a GPUData contains the new buffer. * { * tensorRef: The tensor that is associated with this buffer, * buffer: GPUBuffer, * } * * Remember to dispose the GPUData after it is used by * `res.tensorRef.dispose()`. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ dataToGPU(options) { this.throwIfDisposed(); return trackerFn().readToGPU(this.dataId, options); } /** * Synchronously downloads the values from the `tf.Tensor`. This blocks the * UI thread until the values are ready, which can cause performance issues. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ dataSync() { this.throwIfDisposed(); const data = trackerFn().readSync(this.dataId); if (this.dtype === 'string') { try { return data.map(b => decodeString(b)); } catch (_a) { throw new Error('Failed to decode the string bytes into utf-8. ' + 'To get the original bytes, call tensor.bytes().'); } } return data; } /** Returns the underlying bytes of the tensor's data. */ async bytes() { this.throwIfDisposed(); const data = await trackerFn().read(this.dataId); if (this.dtype === 'string') { return data; } else { return new Uint8Array(data.buffer); } } /** * Disposes `tf.Tensor` from memory. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ dispose() { if (this.isDisposed) { return; } if (this.kerasMask) { this.kerasMask.dispose(); } trackerFn().disposeTensor(this); this.isDisposedInternal = true; } get isDisposed() { return this.isDisposedInternal; } throwIfDisposed() { if (this.isDisposed) { throw new Error(`Tensor is disposed.`); } } /** * Prints the `tf.Tensor`. See `tf.print` for details. * * @param verbose Whether to print verbose information about the tensor, * including dtype and size. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ print(verbose = false) { return opHandler.print(this, verbose); } /** * Returns a copy of the tensor. See `tf.clone` for details. * @doc {heading: 'Tensors', subheading: 'Classes'} */ clone() { this.throwIfDisposed(); return opHandler.clone(this); } /** * Returns a human-readable description of the tensor. Useful for logging. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ toString(verbose = false) { const vals = this.dataSync(); return tensorToString(vals, this.shape, this.dtype, verbose); } cast(dtype) { this.throwIfDisposed(); return opHandler.cast(this, dtype); } variable(trainable = true, name, dtype) { this.throwIfDisposed(); return trackerFn().makeVariable(this, trainable, name, dtype); } } Object.defineProperty(Tensor, Symbol.hasInstance, { value: (instance) => { // Implementation note: we should use properties of the object that will be // defined before the constructor body has finished executing (methods). // This is because when this code is transpiled by babel, babel will call // classCallCheck before the constructor body is run. // See https://github.com/tensorflow/tfjs/issues/3384 for backstory. return !!instance && instance.data != null && instance.dataSync != null && instance.throwIfDisposed != null; } }); function getGlobalTensorClass() { // Use getGlobal so that we can augment the Tensor class across package // boundaries becase the node resolution alg may result in different modules // being returned for this file depending on the path they are loaded from. return getGlobal('Tensor', () => { return Tensor; }); } // Global side effect. Cache global reference to Tensor class getGlobalTensorClass(); /** * A mutable `tf.Tensor`, useful for persisting state, e.g. for training. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ class Variable extends Tensor { constructor(initialValue, trainable, name, tensorId) { super(initialValue.shape, initialValue.dtype, initialValue.dataId, tensorId); this.trainable = trainable; this.name = name; } /** * Assign a new `tf.Tensor` to this variable. The new `tf.Tensor` must have * the same shape and dtype as the old `tf.Tensor`. * * @param newValue New tensor to be assigned to this variable. * * @doc {heading: 'Tensors', subheading: 'Classes'} */ assign(newValue) { if (newValue.dtype !== this.dtype) { throw new Error(`dtype of the new value (${newValue.dtype}) and ` + `previous value (${this.dtype}) must match`); } if (!arraysEqual(newValue.shape, this.shape)) { throw new Error(`shape of the new value (${newValue.shape}) and ` + `previous value (${this.shape}) must match`); } trackerFn().disposeTensor(this); this.dataId = newValue.dataId; trackerFn().incRef(this, null /* backend */); } dispose() { trackerFn().disposeVariable(this); this.isDisposedInternal = true; } } Object.defineProperty(Variable, Symbol.hasInstance, { value: (instance) => { return instance instanceof Tensor && instance.assign != null && instance.assign instanceof Function; } }); /** * @license * Copyright 2017 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. * ============================================================================= */ var Rank; (function (Rank) { Rank["R0"] = "R0"; Rank["R1"] = "R1"; Rank["R2"] = "R2"; Rank["R3"] = "R3"; Rank["R4"] = "R4"; Rank["R5"] = "R5"; Rank["R6"] = "R6"; })(Rank || (Rank = {})); // Looks for upcasting types. Used, for example, in operations with mixed dtype // inputs. var UpcastInt32AndMap; (function (UpcastInt32AndMap) { UpcastInt32AndMap["float32"] = "float32"; UpcastInt32AndMap["int32"] = "int32"; UpcastInt32AndMap["bool"] = "int32"; UpcastInt32AndMap["complex64"] = "complex64"; })(UpcastInt32AndMap || (UpcastInt32AndMap = {})); var UpcastBoolAndMap; (function (UpcastBoolAndMap) { UpcastBoolAndMap["float32"] = "float32"; UpcastBoolAndMap["int32"] = "int32"; UpcastBoolAndMap["bool"] = "bool"; UpcastBoolAndMap["complex64"] = "complex64"; })(UpcastBoolAndMap || (UpcastBoolAndMap = {})); var UpcastFloat32AndMap; (function (UpcastFloat32AndMap) { UpcastFloat32AndMap["float32"] = "float32"; UpcastFloat32AndMap["int32"] = "float32"; UpcastFloat32AndMap["bool"] = "float32"; UpcastFloat32AndMap["complex64"] = "complex64"; })(UpcastFloat32AndMap || (UpcastFloat32AndMap = {})); var UpcastComplex64AndMap; (function (UpcastComplex64AndMap) { UpcastComplex64AndMap["float32"] = "complex64"; UpcastComplex64AndMap["int32"] = "complex64"; UpcastComplex64AndMap["bool"] = "complex64"; UpcastComplex64AndMap["complex64"] = "complex64"; })(UpcastComplex64AndMap || (UpcastComplex64AndMap = {})); const upcastTypeMap = { 'float32': UpcastFloat32AndMap, 'int32': UpcastInt32AndMap, 'bool': UpcastBoolAndMap, 'complex64': UpcastComplex64AndMap }; function upcastType(typeA, typeB) { if (typeA === 'string' || typeB === 'string') { if (typeA === 'string' && typeB === 'string') { return 'string'; } throw new Error(`Can not upcast ${typeA} with ${typeB}`); } return upcastTypeMap[typeA][typeB]; } function isWebGLData(values) { return values != null && typeof values === 'object' && 'texture' in values && values.texture instanceof WebGLTexture; } function isWebGPUData(values) { return typeof GPUBuffer !== 'undefined' && values != null && typeof values === 'object' && 'buffer' in values && values.buffer instanceof GPUBuffer; } /** * @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. * ============================================================================= */ function makeTypesMatch(a, b) { if (a.dtype === b.dtype) { return [a, b]; } const dtype = upcastType(a.dtype, b.dtype); return [a.cast(dtype), b.cast(dtype)]; } /** * Extracts any `Tensor`s found within the provided object. * * @param container an object that may be a `Tensor` or may directly contain * `Tensor`s, such as a `Tensor[]` or `{key: Tensor, ...}`. In general it * is safe to pass any object here, except that `Promise`s are not * supported. * @returns An array of `Tensors` found within the passed object. If the * argument is simply a `Tensor', a list containing that `Tensor` is * returned. If the object is not a `Tensor` or does not * contain `Tensors`, an empty list is returned. */ function getTensorsInContainer(result) { const list = []; const seen = new Set(); walkTensorContainer(result, list, seen); return list; } function walkTensorContainer(container, list, seen) { if (container == null) { return; } if (container instanceof Tensor) { list.push(container); return; } if (!isIterable(container)) { return; } // Iteration over keys works also for arrays. const iterable = container; for (const k in iterable) { const val = iterable[k]; if (!seen.has(val)) { seen.add(val); walkTensorContainer(val, list, seen); } } } // tslint:disable-next-line:no-any function isIterable(obj) { return Array.isArray(obj) || typeof obj === 'object'; } /** * @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. * ============================================================================= */ function isRegisteredKernelInvocation(kernelInvocation) { return kernelInvocation.kernelName != null; } class EngineState { constructor() { // Public since optimizers will use it. this.registeredVariables = {}; this.nextTapeNodeId = 0; this.numBytes = 0; this.numTensors = 0; this.numStringTensors = 0; this.numDataBuffers = 0; // Number of nested tf.grad() statements when computing higher-order // gradients. E.g. `1` for first-order gradients and `2` for second-order // gradients. Used to track if the tape should be removed after a backprop. this.gradientDepth = 0; // Number of nested kernel calls. When kernel depth is greater than 1, we turn // off the tape. this.kernelDepth = 0; this.scopeStack = []; /** * Keeps track of the number of data moves during a kernel execution. We * maintain a stack since kernels can call other kernels, recursively. */ this.numDataMovesStack = []; this.nextScopeId = 0; this.tensorInfo = new WeakMap(); this.profiling = false; this.activeProfile = { newBytes: 0, newTensors: 0, peakBytes: 0, kernels: [], result: null, get kernelNames() { return Array.from(new Set(this.kernels.map(k => k.name))); } }; } dispose() { for (const variableName in this.registeredVariables) { this.registeredVariables[variableName].dispose(); } } } class Engine { constructor(ENV) { this.ENV = ENV; this.registry = {}; this.registryFactory = {}; this.pendingBackendInitId = 0; this.state = new EngineState(); } async ready() { if (this.pendingBackendInit != null) { return this.pendingBackendInit.then(() => { }); } if (this.backendInstance != null) { return; } const sortedBackends = this.getSortedBackends(); for (let i = 0; i < sortedBackends.length; i++) { const backendName = sortedBackends[i]; const success = await this.initializeBackend(backendName).success; if (success) { await this.setBackend(backendName); return; } } throw new Error(`Could not initialize any backends, all backend initializations ` + `failed.`); } get backend() { if (this.pendingBackendInit != null) { throw new Error(`Backend '${this.backendName}' has not yet been initialized. Make ` + `sure to await tf.ready() or await tf.setBackend() before calling ` + `other methods`); } if (this.backendInstance == null) { const { name, asyncInit } = this.initializeBackendsAndReturnBest(); if (asyncInit) { throw new Error(`The highest priority backend '${name}' has not yet been ` + `initialized. Make sure to await tf.ready() or ` + `await tf.setBackend() before calling other methods`); } this.setBackend(name); } return this.backendInstance; } backendNames() { return Object.keys(this.registryFactory); } findBackend(backendName) { if (!(backendName in this.registry)) { // If the backend hasn't been initialized but we have a registry entry for // it, initialize it and return it. if (backendName in this.registryFactory) { const { asyncInit } = this.initializeBackend(backendName); if (asyncInit) { // Backend is not ready yet. return null; } } else { return null; } } return this.registry[backendName]; } findBackendFactory(backendName) { if (!(backendName in this.registryFactory)) { return null; } return this.registryFactory[backendName].factory; } registerBackend(backendName, factory, priority = 1) { if (backendName in this.registryFactory) { warn(`${backendName} backend was already registered. ` + `Reusing existing backend factory.`); return false; } this.registryFactory[backendName] = { factory, priority }; return true; } async setBackend(backendName) { if (this.registryFactory[backendName] == null) { throw new Error(`Backend name '${backendName}' not found in registry`); } this.backendName = backendName; if (this.registry[backendName] == null) { this.backendInstance = null; const { success, asyncInit } = this.initializeBackend(backendName); const result = asyncInit ? await success : success; if (!result) { return false; } } this.backendInstance = this.registry[backendName]; this.setupRegisteredKernels(); // Reset the profiler. this.profiler = new Profiler(this.backendInstance); return true; } setupRegisteredKernels() { const kernels = getKernelsForBackend(this.backendName); kernels.forEach(kernel => { if (kernel.setupFunc != null) { kernel.setupFunc(this.backendInstance); } }); } disposeRegisteredKernels(backendName) { const kernels = getKernelsForBackend(backendName); kernels.forEach(kernel => { if (kernel.disposeFunc != null) { kernel.disposeFunc(this.registry[backendName]); } }); } /** * Initializes a backend by looking up the backend name in the factory * registry and calling the factory method. Returns a boolean representing * whether the initialization of the backend suceeded. Throws an error if * there is no backend in the factory registry. */ initializeBackend(backendName) { const registryFactoryEntry = this.registryFactory[backendName]; if (registryFactoryEntry == null) { throw new Error(`Cannot initialize backend ${backendName}, no registration found.`); } try { const backend = registryFactoryEntry.factory(); /* Test if the factory returns a promise. Done in a more liberal way than previous 'Promise.resolve(backend)===backend' as we needed to account for custom Promise implementations (e.g. Angular) */ if (backend && !(backend instanceof KernelBackend) && typeof backend.then === 'function') { const promiseId = ++this.pendingBackendInitId; const success = backend .then(backendInstance => { // Outdated promise. Another backend was set in the meantime. if (promiseId < this.pendingBackendInitId) { return false; } this.registry[backendName] = backendInstance; this.pendingBackendInit = null; return true; }) .catch(err => { // Outdated promise. Another backend was set in the meantime. if (promiseId < this.pendingBackendInitId) { return false; } this.pendingBackendInit = null; warn(`Initialization of backend ${backendName} failed`); warn(err.stack || err.message); return false; }); this.pendingBackendInit = success; return { success, asyncInit: true }; } else { this.registry[backendName] = backend; return { success: true, asyncInit: false }; } } catch (err) { warn(`Initialization of backend ${backendName} failed`); warn(err.stack || err.message); return { success: false, asyncInit: false }; } } removeBackend(backendName) { if (!(backendName in this.registryFactory)) { throw new Error(`${backendName} backend not found in registry`); } if (this.backendName === backendName && this.pendingBackendInit != null) { // There is a pending promise of the backend we want to remove. Make it // obsolete. this.pendingBackendInitId++; } if (backendName in this.registry) { this.disposeRegisteredKernels(backendName); this.registry[backendName].dispose(); delete this.registry[backendName]; } delete this.registryFactory[backendName]; // Unset the backend if it is active. if (this.backendName === backendName) { this.pendingBackendInit = null; this.backendName = null; this.backendInstance = null; } } getSortedBackends() { if (Object.keys(this.registryFactory).length === 0) { throw new Error('No backend found in registry.'); } return Object.keys(this.registryFactory).sort((a, b) => { // Highest priority comes first. return this.registryFactory[b].priority - this.registryFactory[a].priority; }); } initializeBackendsAndReturnBest() { const sortedBackends = this.getSortedBackends(); for (let i = 0; i < sortedBackends.length; i++) { const backendName = sortedBackends[i]; const { success, asyncInit } = this.initializeBackend(backendName); if (asyncInit || success) { return { name: backendName, asyncInit }; } } throw new Error(`Could not initialize any backends, all backend initializations ` + `failed.`); } moveData(backend, dataId) { const info = this.state.tensorInfo.get(dataId); const srcBackend = info.backend; const values = this.readSync(dataId); const refCount = srcBackend.refCount(dataId); // Delete the tensor from the old backend and move it to the new // backend. srcBackend.disposeData(dataId, true); info.backend = backend; backend.move(dataId, values, info.shape, info.dtype, refCount); if (this.shouldCheckForMemLeaks()) { // Track the number of moves during a kernel execution to correctly // detect memory leaks. this.state.numDataMovesStack[this.state.numDataMovesStack.length - 1]++; } } tidy(nameOrFn, fn) { let name = null; if (fn == null) { // Called with only 1 argument. if (typeof nameOrFn !== 'function') { throw new Error('Please provide a function to tidy()'); } fn = nameOrFn; } else { // Called with 2 arguments. if (typeof nameOrFn !== 'string' && !(nameOrFn instanceof String)) { throw new Error('When calling with two arguments, the first argument ' + 'to tidy() must be a string'); } if (typeof fn !== 'function') { throw new Error('When calling with two arguments, the 2nd argument ' + 'to tidy() must be a function'); } name = nameOrFn; // TODO(nsthorat,smilkov): Do operation logging and performance // profiling. } let result; return this.scopedRun(() => this.startScope(name), () => this.endScope(result), () => { result = fn(); if (result instanceof Promise) { console.error('Cannot return a Promise inside of tidy.'); } return result; }); } scopedRun(start, end, f) { start(); try { const res = f(); end(); return res; } catch (ex) { end(); throw ex; } } nextTensorId() { return Engine.nextTensorId++; } nextVariableId() { return Engine.nextVariableId++; } /** * This method is called instead of the public-facing tensor.clone() when * saving a tensor for backwards pass. It makes sure to add the clone * operation to the tape regardless of being called inside a kernel * execution. */ clone(x) { const y = ENGINE.runKernel(Identity, { x }); const inputs = { x }; const grad = (dy) => ({ x: () => { const dtype = 'float32'; const gradInputs = { x: dy }; const attrs = { dtype }; return ENGINE.runKernel(Cast, gradInputs, // tslint:disable-next-line: no-unnecessary-type-assertion attrs); } }); const saved = []; this.addTapeNode(this.state.activeScope.name, inputs, [y], grad, saved, {}); return y; } /** * Execute a kernel with the given name and return the output tensor. * * @param kernelName The name of the kernel to execute. * @param inputs A map of input names to tensors. * @param attrs A map of attribute names to their values. An attribute is a * primitive (non-tensor) input to the kernel. * @param inputsToSave A list of tensors, inputs to save for the backprop * computation. * @param outputsToSave A list of booleans, specifying which output to save * for the backprop computation. These are booleans since the output * tensors are not visible to the user. */ runKernel(kernelName, inputs, attrs) { if (this.backendName == null) { // backend has not been initialized yet (backend initialization is lazy // can be deferred until an op/ kernel is run). // The below getter has side effects that will try to initialize the // backend and set properties like this.backendName // tslint:disable-next-line: no-unused-expression this.backend; } const hasKernel = getKernel(kernelName, this.backendName) != null; if (!hasKernel) { throw new Error(`Kernel '${kernelName}' not registered for backend '${this.backendName}'`); } return this.runKernelFunc({ kernelName, inputs, attrs }); } shouldCheckForMemLeaks() { return this.ENV.getBool('IS_TEST'); } checkKernelForMemLeak(kernelName, numDataIdsBefore, outInfos) { const numDataIdsAfter = this.backend.numDataIds(); // Count the number of data ids associated with the result of the kernel. let numOutputDataIds = 0; outInfos.forEach(info => { // Complex numbers allocate 3 data ids, one for 'real', one for // 'imaginary', and one for the container that holds the former two. numOutputDataIds += (info.dtype === 'complex64' ? 3 : 1); }); // Account for the number of moves during kernel execution. A "data move" // can happen in the middle of a kernel execution, placing a new (key,value) // pair in the data storage. Since data moves have net zero effect (we // always remove the data from the old backend), we have to cancel them out // when detecting memory leaks. const numMoves = this.state.numDataMovesStack[this.state.numDataMovesStack.length - 1]; const dataIdsLeaked = numDataIdsAfter - numDataIdsBefore - numOutputDataIds - numMoves; if (dataIdsLeaked > 0) { throw new Error(`Backend '${this.backendName}' has an internal memory leak ` + `(${dataIdsLeaked} data ids) after running '${kernelName}'`); } } /** * Internal helper method to execute a kernel Func * * Use `runKernel` to execute kernels from outside of engine. */ runKernelFunc(kernelParams) { let outputs; let saved = []; const isTapeOn = this.isTapeOn(); const startingBytecount = this.state.numBytes; const startingNumTensors = this.state.numTensors; if (this.shouldCheckForMemLeaks()) { this.state.numDataMovesStack.push(0); } let kernelFunc; if (this.backendName == null) { // backend has not been initialized yet (backend initialization is lazy // can be deferred until an op/ kernel is run). // The below getter has side effects that will try to initialize the // backend and set properties like this.backendName // tslint:disable-next-line: no-unused-expression this.backend; } let out; const kernelOrScopeName = isRegisteredKernelInvocation(kernelParams) ? kernelParams.kernelName : this.state.activeScope != null ? this.state.activeScope.name : ''; // Create the kernelFunc from either a registered kernel OR passed in // forward/backward functions (used by custom grad). In this context a // kernelFunc wraps a kernel implementation with some bookkeeping. if (isRegisteredKernelInvocation(kernelParams)) { const { kernelName, inputs, attrs } = kernelParams; if (this.backendName == null) { // backend has not been initialized yet (backend initialization is lazy // can be deferred until an op/ kernel is run). // The below getter has side effects that will try to initialize the // backend and set properties like this.backendName // tslint:disable-next-line: no-unused-expression this.backend; } const kernel = getKernel(kernelName, this.backendName); assert(kernel != null, () => `Cannot find registered kernel '${kernelName}' for backend '${this.backendName}'`); kernelFunc = () => { const numDataIdsBefore = this.backend.numDataIds(); out = kernel.kernelFunc({ inputs, attrs, backend: this.backend }); const outInfos = Array.isArray(out) ? out : [out]; if (this.shouldCheckForMemLeaks()) { this.checkKernelForMemLeak(kernelName, numDataIdsBefore, outInfos); } const outTensors = outInfos.map((outInfo) => { // todo (yassogba) remove this option (Tensor) when node backend // methods have been modularized and they all return tensorInfo. // TensorInfos do not have a rank attribute. if (outInfo.rank != null) { return outInfo; } return this.makeTensorFromTensorInfo(outInfo); }); // Save any required inputs and outputs. // Do not save unless we are recording to the tape. Otherwise it would // cause a mem leak since there would be no backprop for these tensors // (which would otherwise dispose them). if (isTapeOn) { const tensorsToSave = this.getTensorsForGradient(kernelName, inputs, outTensors); saved = this.saveTensorsForBackwardMode(tensorsToSave); } return outTensors; }; } else { const { forwardFunc } = kernelParams; // Running a customGrad op. const saveFunc = (tensors) => { // Do not save unless we are recording to the tape. Otherwise it would // cause a mem leak since we would never run backprop, which disposes // the kept tensors. if (!isTapeOn) { return; } saved = tensors.map(tensor => this.keep(this.clone(tensor))); }; kernelFunc = () => { const numDataIdsBefore = this.backend.numDataIds(); out = this.tidy(() => forwardFunc(this.backend, saveFunc)); const outs = (Array.isArray(out) ? out : [out]); if (this.shouldCheckForMemLeaks()) { // Scope name is used to print a more helpful error message if needed. this.checkKernelForMemLeak(kernelOrScopeName, numDataIdsBefore, outs); } return outs; }; } // // Run the kernelFunc. Optionally profiling it. // const { inputs, attrs } = kernelParams; const backwardsFunc = isRegisteredKernelInvocation(kernelParams) ? null : kernelParams.backwardsFunc; let kernelProfile; this.scopedRun( // Stop recording to a tape when running a kernel. () => this.state.kernelDepth++, () => this.state.kernelDepth--, () => { if (!this.ENV.getBool('DEBUG') && !this.state.profiling) { outputs = kernelFunc(); } else { kernelProfile = this.profiler.profileKernel(kernelOrScopeName, inputs, () => kernelFunc()); if (this.ENV.getBool('DEBUG')) { this.profiler.logKernelProfile(kernelProfile); } outputs = kernelProfile.outputs; } }); if (isTapeOn) { this.addTapeNode(kernelOrScopeName, inputs, outputs, backwardsFunc, saved, attrs); } if (this.state.profiling) { this.state.activeProfile.kernels.push({ name: kernelOrScopeName, bytesAdded: this.state.numBytes - startingBytecount, totalBytesSnapshot: this.state.numBytes, tensorsAdded: this.state.numTensors - startingNumTensors, totalTensorsSnapshot: this.state.numTensors, inputShapes: Object.keys(inputs).map(key => inputs[key] != null ? inputs[key].shape : null), outputShapes: outputs.map(item => item.shape), kernelTimeMs: kernelProfile.timeMs, extraInfo: kernelProfile.extraInfo }); } return (Array.isArray(out) ? outputs : outputs[0]); } /** * Saves tensors used in forward mode for use in backward mode. * * @param tensors the list of tensors to save. */ saveTensorsForBackwardMode(tensors) { const saved = tensors.map(tensor => this.keep(this.clone(tensor))); return saved; } /** * Returns a list of tensors to save for a given gradient calculation. * * @param kernelName name of kernel to look up gradient for. * @param inputs a map of input tensors. * @param outputs an array of output tensors from forward mode of kernel. */ getTensorsForGradient(kernelName, inputs, outputs) { const gradConfig = getGradient(kernelName); if (gradConfig != null) { const inputsToSave = gradConfig.inputsToSave || []; const outputsToSave = gradConfig.outputsToSave || []; // If saveAllInputs is true, all inputs will be saved. Otherwise, inputs // specified in inputsToSave will be saved. let inputTensorsToSave; if (gradConfig.saveAllInputs) { assert(Array.isArray(inputs), () => 'saveAllInputs is true, expected inputs to be an array.'); inputTensorsToSave = Object.keys(inputs).map((key) => inputs[key]); } else { inputTensorsToSave = inputsToSave.map((inputName) => inputs[inputName]); } const outputTensorsToSave = outputs.filter((_, i) => outputsToSave[i]); return inputTensorsToSave.concat(outputTensorsToSave); } // We return an empty list rather than throw an error because the kernel we // are looking up may not actually be relevant to backproping through the // overall function // // See 'does not error if irrelevant (pruned) ops are missing grads' test // in gradients_test.ts for an example. return []; } /** * Internal method used by public APIs for tensor creation. Makes a new * tensor with the provided shape, dtype and values. It always * creates a new data id and writes the values to the underlying backend. */ makeTensor(values, shape, dtype, backend) { if (values == null) { throw new Error('Values passed to engine.makeTensor() are null'); } dtype = dtype || 'float32'; backend = backend || this.backend; let backendVals = values; if (dtype === 'string' && isString(values[0])) { backendVals = values.map(d => encodeString(d)); } const dataId = backend.write(backendVals, shape, dtype); const t = new Tensor(shape, dtype, dataId, this.nextTensorId()); this.trackTensor(t, backend); // Count bytes for string tensors. if (dtype === 'string') { const info = this.state.tensorInfo.get(dataId); const newBytes = bytesFromStringArray(backendVals); this.state.numBytes += newBytes - info.bytes; info.bytes = newBytes; } return t; } /** * Internal method used by backends. Makes a new tensor * that is a wrapper around an existing data id. It doesn't create * a new data id, only increments the ref count used in memory tracking. * @deprecated */ makeTensorFromDataId(dataId, shape, dtype, backend) { dtype = dtype || 'float32'; const tensorInfo = { dataId, shape, dtype }; return this.makeTensorFromTensorInfo(tensorInfo, backend); } /** * Internal method used by backends. Makes a new tensor that is a wrapper * around an existing data id in TensorInfo. It doesn't create a new data id, * only increments the ref count used in memory tracking. */ makeTensorFromTensorInfo(tensorInfo, backend) { const { dataId, shape, dtype } = tensorInfo; const t = new Tensor(shape, dtype, dataId, this.nextTensorId()); this.trackTensor(t, backend); return t; } makeVariable(initialValue, trainable = true, name, dtype) { name = name || this.nextVariableId().toString(); if (dtype != null && dtype !== initialValue.dtype) { initialValue = initialValue.cast(dtype); } const v = new Variable(initialValue, trainable, name, this.nextTensorId()); if (this.state.registeredVariables[v.name] != null) { throw new Error(`Variable with name ${v.name} was already registered`); } this.state.registeredVariables[v.name] = v; this.incRef(v, this.backend); return v; } trackTensor(a, backend) { this.state.numTensors++; if (a.dtype === 'string') { this.state.numStringTensors++; } // Bytes for complex numbers are counted by their components. Bytes for // string tensors are counted when writing values. let bytes = 0; if (a.dtype !== 'complex64' && a.dtype !== 'string') { bytes = a.size * bytesPerElement(a.dtype); } this.state.numBytes += bytes; if (!this.state.tensorInfo.has(a.dataId)) { this.state.numDataBuffers++; this.state.tensorInfo.set(a.dataId, { backend: backend || this.backend, dtype: a.dtype, shape: a.shape, bytes }); } if (!(a instanceof Variable)) { this.track(a); } } // Track the tensor by dataId and increase the refCount for the dataId in the // backend. // TODO(pyu10055): This is currently used by makeVariable method, to increase // refCount on the backend for the dataId. It can potentially be replaced with // Identity op indead of calling backend directly. incRef(a, backend) { this.trackTensor(a, backend); this.backend.incRef(a.dataId); } removeDataId(dataId, backend) { if (this.state.tensorInfo.has(dataId) && this.state.tensorInfo.get(dataId).backend === backend) { this.state.tensorInfo.delete(dataId); this.state.numDataBuffers--; } } disposeTensor(a) { if (!this.state.tensorInfo.has(a.dataId)) { return; } const info = this.state.tensorInfo.get(a.dataId); this.state.numTensors--; if (a.dtype === 'string') { this.state.numStringTensors--; this.state.numBytes -= info.bytes; } // Don't count bytes for complex numbers as they are counted by their // components. if (a.dtype !== 'complex64' && a.dtype !== 'string') { const bytes = a.size * bytesPerElement(a.dtype); this.state.numBytes -= bytes; } // Remove the reference to dataId if backend dispose the data successfully if (info.backend.disposeData(a.dataId)) { this.removeDataId(a.dataId, info.backend); } // TODO(nsthorat): Construct an error and save the stack trace for // debugging when in debug mode. Creating a stack trace is too expensive // to do unconditionally. } disposeVariables() { for (const varName in this.state.registeredVariables) { const v = this.state.registeredVariables[varName]; this.disposeVariable(v); } } disposeVariable(v) { this.disposeTensor(v); if (this.state.registeredVariables[v.name] != null) { delete this.state.registeredVariables[v.name]; } } memory() { const info = this.backend.memory(); info.numTensors = this.state.numTensors; info.numDataBuffers = this.state.numDataBuffers; info.numBytes = this.state.numBytes; if (this.state.numStringTensors > 0) { info.unreliable = true; if (info.reasons == null) { info.reasons = []; } info.reasons.push('Memory usage by string tensors is approximate ' + '(2 bytes per character)'); } return info; } async profile(query) { this.state.profiling = true; const startBytes = this.state.numBytes; const startNumTensors = this.state.numTensors; this.state.activeProfile.kernels = []; this.state.activeProfile.result = await query(); this.state.profiling = false; this.state.activeProfile.peakBytes = Math.max(...this.state.activeProfile.kernels.map(d => d.totalBytesSnapshot)); this.state.activeProfile.newBytes = this.state.numBytes - startBytes; this.state.activeProfile.newTensors = this.state.numTensors - startNumTensors; for (const kernel of this.state.activeProfile.kernels) { kernel.kernelTimeMs = await kernel.kernelTimeMs; kernel.extraInfo = await kernel.extraInfo; } return this.state.activeProfile; } isTapeOn() { return this.state.gradientDepth > 0 && this.state.kernelDepth === 0; } addTapeNode(kernelName, inputs, outputs, gradientsFunc, saved, attrs) { const tapeNode = { id: this.state.nextTapeNodeId++, kernelName, inputs, outputs, saved }; const gradConfig = getGradient(kernelName); if (gradConfig != null) { gradientsFunc = gradConfig.gradFunc; } if (gradientsFunc != null) { tapeNode.gradient = (dys) => { // TODO(smilkov): To optimize back-prop, pass dys that are not used in // the backprop graph to the user as null instead of zeros dys = dys.map((dy, i) => { if (dy == null) { const output = outputs[i]; const vals = makeZerosTypedArray(output.size, output.dtype); return this.makeTensor(vals, output.shape, output.dtype); } return dy; }); // Grad functions of ops with single outputs expect a dy, while ops // with multiple outputs expect dys (array of dy). return gradientsFunc(dys.length > 1 ? dys : dys[0], saved, attrs); }; } this.state.activeTape.push(tapeNode); } keep(result) { result.kept = true; return result; } startTape() { if (this.state.gradientDepth === 0) { this.state.activeTape = []; } this.state.gradientDepth++; } endTape() { this.state.gradientDepth--; } /** * Start a scope. Use this with endScope() to achieve the same functionality * as scope() without the need for a function closure. */ startScope(name) { const scopeInfo = { track: [], name: 'unnamed scope', id: this.state.nextScopeId++ }; if (name) { scopeInfo.name = name; } this.state.scopeStack.push(scopeInfo); this.state.activeScope = scopeInfo; } /** * End a scope. Use this with startScope() to achieve the same functionality * as scope() without the need for a function closure. */ endScope(result) { const tensorsToTrackInParent = getTensorsInContainer(result); const tensorsToTrackInParentSet = new Set(tensorsToTrackInParent.map(t => t.id)); // Dispose the arrays tracked in this scope. for (let i = 0; i < this.state.activeScope.track.length; i++) { const tensor = this.state.activeScope.track[i]; if (!tensor.kept && !tensorsToTrackInParentSet.has(tensor.id)) { tensor.dispose(); } } const oldScope = this.state.scopeStack.pop(); this.state.activeScope = this.state.scopeStack.length === 0 ? null : this.state.scopeStack[this.state.scopeStack.length - 1]; // Track the current result in the parent scope. tensorsToTrackInParent.forEach(tensor => { // Only track the tensor if was allocated in the inner scope and is not // globally kept. if (!tensor.kept && tensor.scopeId === oldScope.id) { this.track(tensor); } }); } /** * Returns gradients of `f` with respect to each of the `xs`. The gradients * returned are of the same length as `xs`, but some might be null if `f` * was not a function of that `x`. It also takes optional dy to multiply the * gradient, which defaults to `1`. */ gradients(f, xs, dy, allowNoGradients = false) { assert(xs.length > 0, () => 'gradients() received an empty list of xs.'); if (dy != null && dy.dtype !== 'float32') { throw new Error(`dy must have 'float32' dtype, but has '${dy.dtype}'`); } const y = this.scopedRun(() => this.startTape(), () => this.endTape(), () => this.tidy('forward', f)); assert(y instanceof Tensor, () => 'The result y returned by f() must be a tensor.'); // Filter out the nodes that don't connect x => y. const filteredTape = getFilteredNodesXToY(this.state.activeTape, xs, y); if (!allowNoGradients && filteredTape.length === 0 && xs.length > 0) { throw new Error('Cannot compute gradient of y=f(x) with respect to x. Make sure ' + 'that the f you passed encloses all operations that lead from x ' + 'to y.'); } return this.tidy('backward', () => { const accumulatedGradientMap = {}; accumulatedGradientMap[y.id] = (dy == null) ? ones$2(y.shape) : dy; // Backprop gradients through the filtered nodes. backpropagateGradients(accumulatedGradientMap, filteredTape, // Pass the tidy function to avoid circular dep with `tape.ts`. f => this.tidy(f), // Pass an add function to avoide a circular dep with `tape.ts`. add$2); const grads = xs.map(x => accumulatedGradientMap[x.id]); if (this.state.gradientDepth === 0) { // This means that we are not computing higher-order gradients // and can clean up the tape. this.state.activeTape.forEach(node => { for (const tensor of node.saved) { tensor.dispose(); } }); this.state.activeTape = null; } return { value: y, grads }; }); } customGrad(f) { assert(isFunction(f), () => 'The f passed in customGrad(f) must be a function.'); return (...inputs) => { assert(inputs.every(t => t instanceof Tensor), () => 'The args passed in customGrad(f)(x1, x2,...) must all be ' + 'tensors'); let res; const inputMap = {}; inputs.forEach((input, i) => { inputMap[i] = input; }); const forwardFunc = (_, save) => { res = f(...[...inputs, save]); assert(res.value instanceof Tensor, () => 'The function f passed in customGrad(f) must return an ' + 'object where `obj.value` is a tensor'); assert(isFunction(res.gradFunc), () => 'The function f passed in customGrad(f) must return an ' + 'object where `obj.gradFunc` is a function.'); return res.value; }; const backwardsFunc = (dy, saved) => { const gradRes = res.gradFunc(dy, saved); const grads = Array.isArray(gradRes) ? gradRes : [gradRes]; assert(grads.length === inputs.length, () => 'The function f passed in customGrad(f) must return an ' + 'object where `obj.gradFunc` is a function that returns ' + 'the same number of tensors as inputs passed to f(...).'); assert(grads.every(t => t instanceof Tensor), () => 'The function f passed in customGrad(f) must return an ' + 'object where `obj.gradFunc` is a function that returns ' + 'a list of only tensors.'); const gradMap = {}; grads.forEach((grad, i) => { gradMap[i] = () => grad; }); return gradMap; }; return this.runKernelFunc({ forwardFunc, backwardsFunc, inputs: inputMap, }); }; } readSync(dataId) { // Route the read to the correct backend. const info = this.state.tensorInfo.get(dataId); return info.backend.readSync(dataId); } read(dataId) { // Route the read to the correct backend. const info = this.state.tensorInfo.get(dataId); return info.backend.read(dataId); } readToGPU(dataId, options) { // Route the read to the correct backend. const info = this.state.tensorInfo.get(dataId); return info.backend.readToGPU(dataId, options); } async time(query) { const start = now(); const timingInfo = await this.backend.time(query); timingInfo.wallMs = now() - start; return timingInfo; } /** * Tracks a Tensor in the current scope to be automatically cleaned up * when the current scope ends, and returns the value. * * @param result The Tensor to track in the current scope. */ track(result) { if (this.state.activeScope != null) { result.scopeId = this.state.activeScope.id; this.state.activeScope.track.push(result); } return result; } get registeredVariables() { return this.state.registeredVariables; } /** * Resets the engine state. Removes all backends but does not remove * registered backend factories. */ reset() { // Make any pending promise obsolete. this.pendingBackendInitId++; this.state.dispose(); this.ENV.reset(); this.state = new EngineState(); for (const backendName in this.registry) { this.disposeRegisteredKernels(backendName); this.registry[backendName].dispose(); delete this.registry[backendName]; } this.backendName = null; this.backendInstance = null; this.pendingBackendInit = null; } } Engine.nextTensorId = 0; Engine.nextVariableId = 0; function ones$2(shape) { const values = makeOnesTypedArray(sizeFromShape(shape), 'float32'); return ENGINE.makeTensor(values, shape, 'float32'); } function getOrMakeEngine() { const ns = getGlobalNamespace(); if (ns._tfengine == null) { const environment = new Environment(ns); ns._tfengine = new Engine(environment); } setEnvironmentGlobal(ns._tfengine.ENV); // Tell the current tensor interface that the global engine is responsible // for tracking. setTensorTracker(() => ns._tfengine); return ns._tfengine; } const ENGINE = getOrMakeEngine(); /** * A implementation of the add op for use within engine and tape. * * This allows us to avoid a circular dependency between add.ts and engine. * It is exported to be available in tape tests. */ function add$2(a, b) { // We duplicate Add here to avoid a circular dependency with add.ts. const inputs = { a, b }; return ENGINE.runKernel(Add$1, inputs); } /** * @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. * ============================================================================= */ function inferShape(val, dtype) { let firstElem = val; if (isTypedArray(val)) { return dtype === 'string' ? [] : [val.length]; } if (isWebGLData(val)) { const usedChannels = val.channels || 'RGBA'; return [val.height, val.width * usedChannels.length]; } else if (isWebGPUData(val)) { return [val.buffer.size / (dtype == null ? 4 : bytesPerElement(dtype))]; } if (!Array.isArray(val)) { return []; // Scalar. } const shape = []; while (Array.isArray(firstElem) || isTypedArray(firstElem) && dtype !== 'string') { shape.push(firstElem.length); firstElem = firstElem[0]; } if (Array.isArray(val) && env().getBool('TENSORLIKE_CHECK_SHAPE_CONSISTENCY')) { deepAssertShapeConsistency(val, shape, []); } return shape; } function deepAssertShapeConsistency(val, shape, indices) { indices = indices || []; if (!(Array.isArray(val)) && !isTypedArray(val)) { assert(shape.length === 0, () => `Element arr[${indices.join('][')}] is a primitive, ` + `but should be an array/TypedArray of ${shape[0]} elements`); return; } assert(shape.length > 0, () => `Element arr[${indices.join('][')}] should be a primitive, ` + `but is an array of ${val.length} elements`); assert(val.length === shape[0], () => `Element arr[${indices.join('][')}] should have ${shape[0]} ` + `elements, but has ${val.length} elements`); const subShape = shape.slice(1); for (let i = 0; i < val.length; ++i) { deepAssertShapeConsistency(val[i], subShape, indices.concat(i)); } } function assertDtype(expectedDtype, actualDType, argName, functionName) { if (expectedDtype === 'string_or_numeric') { return; } if (expectedDtype == null) { throw new Error(`Expected dtype cannot be null.`); } if (expectedDtype !== 'numeric' && expectedDtype !== actualDType || expectedDtype === 'numeric' && actualDType === 'string') { throw new Error(`Argument '${argName}' passed to '${functionName}' must ` + `be ${expectedDtype} tensor, but got ${actualDType} tensor`); } } function convertToTensor(x, argName, functionName, parseAsDtype = 'numeric') { if (x instanceof getGlobalTensorClass()) { assertDtype(parseAsDtype, x.dtype, argName, functionName); return x; } let inferredDtype = inferDtype(x); // If the user expects a bool/int/float, use that info to update the // inferredDtype when it is not a string. if (inferredDtype !== 'string' && ['bool', 'int32', 'float32'].indexOf(parseAsDtype) >= 0) { inferredDtype = parseAsDtype; } assertDtype(parseAsDtype, inferredDtype, argName, functionName); if ((x == null) || (!isTypedArray(x) && !Array.isArray(x) && typeof x !== 'number' && typeof x !== 'boolean' && typeof x !== 'string')) { const type = x == null ? 'null' : x.constructor.name; throw new Error(`Argument '${argName}' passed to '${functionName}' must be a ` + `Tensor or TensorLike, but got '${type}'`); } const inferredShape = inferShape(x, inferredDtype); if (!isTypedArray(x) && !Array.isArray(x)) { x = [x]; } const skipTypedArray = true; const values = inferredDtype !== 'string' ? toTypedArray(x, inferredDtype) : flatten$1(x, [], skipTypedArray); return ENGINE.makeTensor(values, inferredShape, inferredDtype); } function convertToTensorArray(arg, argName, functionName, parseAsDtype = 'numeric') { if (!Array.isArray(arg)) { throw new Error(`Argument ${argName} passed to ${functionName} must be a ` + '`Tensor[]` or `TensorLike[]`'); } const tensors = arg; return tensors.map((t, i) => convertToTensor(t, `${argName}[${i}]`, functionName, parseAsDtype)); } /** * @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. * ============================================================================= */ const OP_SCOPE_SUFFIX = '__op'; /** * Used for wrapping functions that perform math operations on * Tensors. The function will be wrapped in a named scope that cleans all * memory usage after the function is done. */ function op(f) { const keys = Object.keys(f); if (keys.length !== 1) { throw new Error(`Please provide an object with a single key ` + `(operation name) mapping to a function. Got an object with ` + `${keys.length} keys.`); } let opName = keys[0]; const fn = f[opName]; // Strip the underscore from the end of the function name. if (opName.endsWith('_')) { opName = opName.substring(0, opName.length - 1); } // add an __op suffix to distinguish ops from kernels in tf.profile opName = opName + OP_SCOPE_SUFFIX; // tslint:disable-next-line:no-any const f2 = (...args) => { ENGINE.startScope(opName); try { const result = fn(...args); if (isPromise(result)) { console.error('Cannot return a Promise inside of tidy.'); } ENGINE.endScope(result); return result; } catch (ex) { ENGINE.endScope(null); throw ex; } }; Object.defineProperty(f2, 'name', { value: opName, configurable: true }); // tslint:disable-next-line:no-any return f2; } /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ /** * Casts a `tf.Tensor` to a new dtype. * * ```js * const x = tf.tensor1d([1.5, 2.5, 3]); * tf.cast(x, 'int32').print(); * ``` * @param x The input tensor to be casted. * @param dtype The dtype to cast the input tensor to. * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function cast_(x, dtype) { const $x = convertToTensor(x, 'x', 'cast'); // Sanity checks. if (!isValidDtype(dtype)) { throw new Error(`Failed to cast to unknown dtype ${dtype}`); } if (dtype === 'string' && $x.dtype !== 'string' || dtype !== 'string' && $x.dtype === 'string') { throw new Error('Only strings can be casted to strings'); } const inputs = { x: $x }; const attrs = { dtype }; return ENGINE.runKernel(Cast, inputs, attrs); } const cast = /* @__PURE__ */ op({ cast_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Multiplies two `tf.Tensor`s element-wise, A * B. Supports broadcasting. * * We also expose `tf.mulStrict` which has the same signature as this op and * asserts that `a` and `b` are the same shape (does not broadcast). * * ```js * 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) * ``` * * ```js * // 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) * ``` * @param a The first tensor to multiply. * @param b The second tensor to multiply. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function mul_(a, b) { let $a = convertToTensor(a, 'a', 'mul'); let $b = convertToTensor(b, 'b', 'mul'); [$a, $b] = makeTypesMatch($a, $b); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Multiply$1, inputs); } const mul = /* @__PURE__ */ op({ mul_ }); /** * @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. * ============================================================================= */ /** * Computes step of the input `tf.Tensor` element-wise: `x > 0 ? 1 : alpha` * * ```js * const x = tf.tensor1d([0, 2, -1, -3]); * * x.step(.5).print(); // or tf.step(x, .5) * ``` * @param x The input tensor. * @param alpha The gradient when input is negative. Defaults to 0. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function step_(x, alpha = 0.0) { const $x = convertToTensor(x, 'x', 'step'); const inputs = { x: $x }; const attrs = { alpha }; return ENGINE.runKernel(Step, inputs, attrs); } const step = /* @__PURE__ */ op({ step_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const absGradConfig = { kernelName: Abs, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(dy, step(cast(x, 'float32'), -1)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Divides two `tf.Tensor`s element-wise, A / B. Supports broadcasting. * The result is rounded with floor function. * * * ```js * const a = tf.tensor1d([1, 4, 9, 16]); * const b = tf.tensor1d([1, 2, 3, 4]); * * a.floorDiv(b).print(); // or tf.div(a, b) * ``` * * ```js * // Broadcast div a with b. * const a = tf.tensor1d([2, 4, 6, 8]); * const b = tf.scalar(2); * * a.floorDiv(b).print(); // or tf.floorDiv(a, b) * ``` * * @param a The first tensor as the numerator. * @param b The second tensor as the denominator. Must have the same dtype as * `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function floorDiv_(a, b) { let $a = convertToTensor(a, 'a', 'floorDiv'); let $b = convertToTensor(b, 'b', 'floorDiv'); [$a, $b] = makeTypesMatch($a, $b); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(FloorDiv, inputs); } const floorDiv = /* @__PURE__ */ op({ floorDiv_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Divides two `tf.Tensor`s element-wise, A / B. Supports broadcasting. * * ```js * 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) * ``` * * ```js * // 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) * ``` * * @param a The first tensor as the numerator. * @param b The second tensor as the denominator. Must have the same dtype as * `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function div_(a, b) { let $a = convertToTensor(a, 'a', 'div'); let $b = convertToTensor(b, 'b', 'div'); [$a, $b] = makeTypesMatch($a, $b); if ($a.dtype === 'int32' && $b.dtype === 'int32') { return floorDiv($a, $b); } const inputs = { a: $a, b: $b }; const attrs = {}; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(RealDiv, inputs, attrs); } const div = /* @__PURE__ */ op({ div_ }); /** * @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. * ============================================================================= */ /** * Computes `-1 * x` element-wise. * * ```js * const x = tf.tensor2d([1, 2, -2, 0], [2, 2]); * * x.neg().print(); // or tf.neg(x) * ``` * * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function neg_(x) { const $x = convertToTensor(x, 'x', 'neg'); const inputs = { x: $x }; return ENGINE.runKernel(Neg, inputs); } const neg = /* @__PURE__ */ op({ neg_ }); /** * @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. * ============================================================================= */ /** This is shared code across all tensor creation methods. */ function makeTensor(values, shape, inferredShape, dtype) { if (dtype == null) { dtype = inferDtype(values); } else if (dtype === 'complex64') { throw new Error(`Cannot construct a complex64 tensor directly. ` + `Please use tf.complex(real, imag).`); } if (isWebGPUData(values) || isWebGLData(values)) { if (dtype !== 'float32' && dtype !== 'int32') { throw new Error(`Creating tensor from GPU data only supports ` + `'float32'|'int32' dtype, while the dtype is ${dtype}.`); } return ENGINE.backend.createTensorFromGPUData(values, shape || inferredShape, dtype); } if (!isTypedArray(values) && !Array.isArray(values) && typeof values !== 'number' && typeof values !== 'boolean' && typeof values !== 'string') { throw new Error('values passed to tensor(values) must be a number/boolean/string or ' + 'an array of numbers/booleans/strings, or a TypedArray'); } // Verify that the shape matches the inferred shape. if (shape != null) { assertNonNegativeIntegerDimensions(shape); const providedSize = sizeFromShape(shape); const inferredSize = sizeFromShape(inferredShape); assert(providedSize === inferredSize, () => `Based on the provided shape, [${shape}], the tensor should have ` + `${providedSize} values but has ${inferredSize}`); for (let i = 0; i < inferredShape.length; ++i) { const inferred = inferredShape[i]; const flatDimsDontMatch = i === inferredShape.length - 1 ? inferred !== sizeFromShape(shape.slice(i)) : true; assert(inferredShape[i] === shape[i] || !flatDimsDontMatch, () => `Error creating a new Tensor. Inferred shape ` + `(${inferredShape}) does not match the provided ` + `shape (${shape}). `); } } if (!isTypedArray(values) && !Array.isArray(values)) { values = [values]; } shape = shape || inferredShape; values = dtype !== 'string' ? toTypedArray(values, dtype) : flatten$1(values, [], true); return ENGINE.makeTensor(values, shape, dtype); } /** * @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. * ============================================================================= */ /** * 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'} */ function scalar(value, dtype) { if (((isTypedArray(value) && dtype !== 'string') || Array.isArray(value)) && dtype !== 'complex64') { throw new Error('Error creating a new Scalar: value must be a primitive ' + '(number|boolean|string)'); } if (dtype === 'string' && isTypedArray(value) && !(value instanceof Uint8Array)) { throw new Error('When making a scalar from encoded string, ' + 'the value must be `Uint8Array`.'); } const shape = []; const inferredShape = []; return makeTensor(value, shape, inferredShape, dtype); } /** * @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. * ============================================================================= */ /** * Computes square root of the input `tf.Tensor` element-wise: `y = sqrt(x)` * * ```js * const x = tf.tensor1d([1, 2, 4, -1]); * * x.sqrt().print(); // or tf.sqrt(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function sqrt_(x) { const $x = convertToTensor(x, 'x', 'sqrt', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Sqrt, inputs); } const sqrt = /* @__PURE__ */ op({ sqrt_ }); /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ /** * Computes square of `x` element-wise: `x ^ 2` * * ```js * const x = tf.tensor1d([1, 2, Math.sqrt(2), -1]); * * x.square().print(); // or tf.square(x) * ``` * @param x The input Tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function square_(x) { const $x = convertToTensor(x, 'x', 'square'); const attrs = {}; return ENGINE.runKernel('Square', { x: $x }, attrs); } const square = /* @__PURE__ */ op({ square_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Subtracts two `tf.Tensor`s element-wise, A - B. Supports broadcasting. * * ```js * 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) * ``` * * ```js * // 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) * ``` * @param a The first `tf.Tensor` to subtract from. * @param b The second `tf.Tensor` to be subtracted. Must have the same dtype as * `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function sub_(a, b) { let $a = convertToTensor(a, 'a', 'sub'); let $b = convertToTensor(b, 'b', 'sub'); [$a, $b] = makeTypesMatch($a, $b); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Sub, inputs); } const sub = /* @__PURE__ */ op({ sub_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const acosGradConfig = { kernelName: Acos, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => { const a = square(cast(x, 'float32')); const b = sqrt(sub(scalar(1), a)); return neg(div(dy, b)); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const acoshGradConfig = { kernelName: Acosh, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => { const a = sqrt(sub(square(cast(x, 'float32')), 1)); return div(dy, a); } }; } }; /** * @license * Copyright 2017 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. * ============================================================================= */ /** * Returns the axes in the output space that should be reduced to produce * the input space. */ function getReductionAxes(inShape, outShape) { const result = []; for (let i = 0; i < outShape.length; i++) { const inDim = inShape[inShape.length - i - 1]; const outAxis = outShape.length - i - 1; const outDim = outShape[outAxis]; if (inDim == null || (inDim === 1 && outDim > 1)) { result.unshift(outAxis); } } return result; } function assertAndGetBroadcastShape(shapeA, shapeB) { const l = Math.max(shapeA.length, shapeB.length); const result = new Array(l); for (let i = 0; i < l; i++) { let a = shapeA[shapeA.length - i - 1]; if (a == null) { a = 1; } let b = shapeB[shapeB.length - i - 1]; if (b == null) { b = 1; } if (a === 1) { result[l - i - 1] = b; } else if (b === 1) { result[l - i - 1] = a; } else if (a !== b) { const errMsg = `Operands could not be broadcast together with shapes ` + `${shapeA} and ${shapeB}.`; throw Error(errMsg); } else { result[l - i - 1] = a; } } return result; } /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Reshapes a `tf.Tensor` to a given shape. * * Given an 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. * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * x.reshape([2, 2]).print(); * ``` * * @param x The input tensor to be reshaped. * @param shape An array of integers defining the output tensor shape. * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function reshape_(x, shape) { const $x = convertToTensor(x, 'x', 'reshape', 'string_or_numeric'); const inputs = { x: $x }; const attrs = { shape }; return ENGINE.runKernel(Reshape$1, inputs, attrs); } const reshape$1 = /* @__PURE__ */ op({ reshape_ }); /** * @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. * ============================================================================= */ /** * 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. * * ```js * const x = tf.tensor1d([1, 2, 3]); * * x.sum().print(); // or tf.sum(x) * ``` * * ```js * const x = tf.tensor2d([1, 2, 3, 4], [2, 2]); * * const axis = 1; * x.sum(axis).print(); // or tf.sum(x, axis) * ``` * * @param x The input tensor to compute the sum over. If the dtype is `bool` * it will be converted to `int32` and the output dtype will be `int32`. * @param axis The dimension(s) to reduce. By default it reduces * all dimensions. * @param keepDims If true, retains reduced dimensions with size 1. * * @doc {heading: 'Operations', subheading: 'Reduction'} */ function sum_(x, axis = null, keepDims = false) { let $x = convertToTensor(x, 'x', 'sum'); if ($x.dtype === 'bool') { $x = cast($x, 'int32'); } const inputs = { x: $x }; const attrs = { axis, keepDims }; return ENGINE.runKernel(Sum, inputs, attrs); } const sum = /* @__PURE__ */ op({ sum_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const addGradConfig = { kernelName: Add$1, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { let res = dy; const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, a.shape); }; const derB = () => { let res = dy; const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, b.shape); }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const addNGradConfig = { kernelName: AddN, saveAllInputs: true, gradFunc: (dy, saved) => { const ders = {}; saved.forEach((_, i) => { ders[i] = () => dy.clone(); }); return ders; } }; /** * @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. * ============================================================================= */ /** * 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'} */ function zerosLike_(x) { const $x = convertToTensor(x, 'x', 'zerosLike'); const inputs = { x: $x }; return ENGINE.runKernel(ZerosLike, inputs); } const zerosLike = /* @__PURE__ */ op({ zerosLike_ }); /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ const argMaxGradConfig = { kernelName: ArgMax, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => zerosLike(x) }; } }; /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ const argMinGradConfig = { kernelName: ArgMin, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => zerosLike(x) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const asinGradConfig = { kernelName: Asin, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, sqrt(sub(scalar(1), square(cast(x, 'float32'))))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Adds two `tf.Tensor`s element-wise, A + B. Supports broadcasting. * * * ```js * 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) * ``` * * ```js * // 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) * ``` * @param a The first `tf.Tensor` to add. * @param b The second `tf.Tensor` to add. Must have the same type as `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function add_(a, b) { let $a = convertToTensor(a, 'a', 'add'); let $b = convertToTensor(b, 'b', 'add'); [$a, $b] = makeTypesMatch($a, $b); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Add$1, inputs); } const add$1 = /* @__PURE__ */ op({ add_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const asinhGradConfig = { kernelName: Asinh, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => { const a = sqrt(add$1(scalar(1), square(cast(x, 'float32')))); return div(dy, a); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const atan2GradConfig = { kernelName: Atan2, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { const d = add$1(square(a), square(b)); let res = mul(dy, div(b, d)); const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, a.shape); }; const derB = () => { const d = add$1(square(a), square(b)); let res = neg(mul(dy, div(a, d))); const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, b.shape); }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const atanGradConfig = { kernelName: Atan, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, add$1(square(cast(x, 'float32')), 1)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const atanhGradConfig = { kernelName: Atanh, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, sub(scalar(1), square(cast(x, 'float32')))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ function parseTupleParam(param) { if (typeof param === 'number') { return [param, param, param]; } if (param.length === 2) { return [param[0], param[1], 1]; } return param; } function tupleValuesAreOne(param) { const [dimA, dimB, dimC] = parseTupleParam(param); return dimA === 1 && dimB === 1 && dimC === 1; } function eitherStridesOrDilationsAreOne(strides, dilations) { return tupleValuesAreOne(strides) || tupleValuesAreOne(dilations); } function stridesOrDilationsArePositive(values) { return parseTupleParam(values).every(value => value > 0); } /** * Check validity of pad when using dimRoundingMode. * @param opDesc A string of op description * @param pad The type of padding algorithm. * - `same` and stride 1: output will be of same size as input, * regardless of filter size. * - `valid` output will be smaller than input if filter is larger * than 1x1. * - For more info, see this guide: * [https://www.tensorflow.org/api_docs/python/tf/nn/convolution]( * https://www.tensorflow.org/api_docs/python/tf/nn/convolution) * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. * @throws unknown padding parameter */ function checkPadOnDimRoundingMode(opDesc, pad, dimRoundingMode) { if (dimRoundingMode != null) { if (typeof pad === 'string') { throw Error(`Error in ${opDesc}: pad must be an integer when using ` + `dimRoundingMode ${dimRoundingMode} but got pad ${pad}.`); } else if (typeof pad === 'number') { assert(isInt(pad), () => `Error in ${opDesc}: pad must be an integer when using ` + `dimRoundingMode ${dimRoundingMode} but got pad ${pad}.`); } else if (typeof pad === 'object') { pad.forEach(p => { p.forEach(v => { assert(isInt(v), () => `Error in ${opDesc}: pad must be an integer when using ` + `dimRoundingMode ${dimRoundingMode} but got pad ${v}.`); }); }); } else { throw Error(`Error in ${opDesc}: Unknown padding parameter: ${pad}`); } } } /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the backprop of a 3d avg pool. * * @param dy The dy error, of rank 5 of shape * [batchSize, depth, height, width, channels]. * assumed. * @param input The original input image, of rank 5 or rank4 of shape * [batchSize, depth, height, width, channels]. * @param filterSize The filter size: * `[filterDepth, filterHeight, filterWidth]`. * `filterSize` is a single number, * then `filterDepth == filterHeight == filterWidth`. * @param strides The strides of the pooling: * `[strideDepth, strideHeight, strideWidth]`. If * `strides` is a single number, then `strideHeight == strideWidth`. * @param pad A string from: 'same', 'valid'. The type of padding algorithm * used in the forward prop of the op. * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. */ function avgPool3dGrad_(dy, input, filterSize, strides, pad, dimRoundingMode) { const $dy = convertToTensor(dy, 'dy', 'avgPool3dGrad'); const $input = convertToTensor(input, 'input', 'avgPool3dGrad'); let dy5D = $dy; let input5D = $input; let reshapedTo5D = false; if ($input.rank === 4) { reshapedTo5D = true; dy5D = reshape$1($dy, [1, $dy.shape[0], $dy.shape[1], $dy.shape[2], $dy.shape[3]]); input5D = reshape$1($input, [ 1, $input.shape[0], $input.shape[1], $input.shape[2], $input.shape[3] ]); } assert(dy5D.rank === 5, () => `Error in avgPool3dGrad: dy must be rank 5 but got rank ` + `${dy5D.rank}.`); assert(input5D.rank === 5, () => `Error in avgPool3dGrad: input must be rank 5 but got rank ` + `${input5D.rank}.`); checkPadOnDimRoundingMode('avgPool3dGrad', pad, dimRoundingMode); const inputs = { dy: dy5D, input: input5D }; const attrs = { filterSize, strides, pad, dimRoundingMode }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(AvgPool3DGrad, inputs, attrs); if (reshapedTo5D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]); } return res; } const avgPool3dGrad = /* @__PURE__ */ op({ avgPool3dGrad_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const avgPool3DGradConfig = { kernelName: AvgPool3D, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { filterSize, strides, pad, dimRoundingMode } = attrs; return { x: () => avgPool3dGrad(dy, x, filterSize, strides, pad, dimRoundingMode) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the backprop of an 2D avg pool. * * @param dy The dy error, of rank 4 or rank 3 of shape * [batchSize, height, width, channels]. If rank 3, batch of 1 is * assumed. * @param input The input image, of rank 4 or rank 3 of shape * [batchSize, height, width, channels]. If rank 3, batch of 1 is * assumed. * @param filterSize The filter size: `[filterHeight, filterWidth]`. If * `filterSize` is a single number, then `filterHeight == filterWidth`. * @param strides The strides of the pooling: `[strideHeight, strideWidth]`. If * `strides` is a single number, then `strideHeight == strideWidth`. * @param pad The type of padding algorithm used in the forward prop of the op. * 'same', 'valid', for more info, see this guide: * [https://www.tensorflow.org/api_docs/python/tf/nn/convolution]( * https://www.tensorflow.org/api_docs/python/tf/nn/convolution) */ function avgPoolGrad_(dy, input, filterSize, strides, pad) { const $dy = convertToTensor(dy, 'dy', 'avgPoolGrad'); const $input = convertToTensor(input, 'input', 'avgPoolGrad'); assert($input.rank === $dy.rank, () => `Rank of input (${$input.rank}) does not match rank of dy (${$dy.rank})`); let input4D = $input; let dy4D = $dy; let reshapedTo4D = false; if ($input.rank === 3) { reshapedTo4D = true; input4D = reshape$1($input, [1, $input.shape[0], $input.shape[1], $input.shape[2]]); dy4D = reshape$1($dy, [1, $dy.shape[0], $dy.shape[1], $dy.shape[2]]); } assert(dy4D.rank === 4, () => `Error in avgPoolGrad: dy must be rank 4 but got rank ` + `${dy4D.rank}.`); assert(input4D.rank === 4, () => `Error in avgPoolGrad: input must be rank 4 but got rank ` + `${input4D.rank}.`); const inputs = { dy: dy4D, input: input4D }; const attrs = { filterSize, strides, pad }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(AvgPoolGrad, inputs, attrs); if (reshapedTo4D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3]]); } return res; } const avgPoolGrad = /* @__PURE__ */ op({ avgPoolGrad_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const avgPoolGradConfig = { kernelName: AvgPool, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { filterSize, strides, pad } = attrs; return { x: () => avgPoolGrad(dy, x, filterSize, strides, pad) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the dot product of two matrices, A * B. These must be matrices. * * ```js * 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) * ``` * @param a First matrix in dot product operation. * @param b Second matrix in dot product operation. * @param transposeA If true, `a` is transposed before multiplication. * @param transposeB If true, `b` is transposed before multiplication. * * @doc {heading: 'Operations', subheading: 'Matrices'} */ function matMul_(a, b, transposeA = false, transposeB = false) { let $a = convertToTensor(a, 'a', 'matMul'); let $b = convertToTensor(b, 'b', 'matMul'); [$a, $b] = makeTypesMatch($a, $b); const inputs = { a: $a, b: $b }; const attrs = { transposeA, transposeB }; return ENGINE.runKernel(BatchMatMul, inputs, attrs); } const matMul = /* @__PURE__ */ op({ matMul_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const batchMatMulGradConfig = { kernelName: BatchMatMul, inputsToSave: ['a', 'b'], gradFunc: (dy, saved, attrs) => { const [a, b] = saved; const { transposeA, transposeB } = attrs; if (!transposeA && !transposeB) { return { a: () => matMul(dy, b, false, true), b: () => matMul(a, dy, true, false) }; } else if (!transposeA && transposeB) { return { a: () => matMul(dy, b, false, false), b: () => matMul(dy, a, true, false) }; } else if (transposeA && !transposeB) { return { a: () => matMul(b, dy, false, true), b: () => matMul(a, dy, false, false) }; } else { return { a: () => matMul(b, dy, true, true), b: () => matMul(dy, a, true, true) }; } } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * This operation divides "spatial" dimensions `[1, ..., M]` of the input into * a grid of blocks of shape `blockShape`, and interleaves these blocks with * the "batch" dimension (0) such that in the output, the spatial * dimensions `[1, ..., M]` correspond to the position within the grid, * and the batch dimension combines both the position within a spatial block * and the original batch position. Prior to division into blocks, * the spatial dimensions of the input are optionally zero padded * according to `paddings`. See below for a precise description. * * ```js * const x = tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]); * const blockShape = [2, 2]; * const paddings = [[0, 0], [0, 0]]; * * x.spaceToBatchND(blockShape, paddings).print(); * ``` * * @param x A `tf.Tensor`. N-D with `x.shape` = `[batch] + spatialShape + * remainingShape`, where spatialShape has `M` dimensions. * @param blockShape A 1-D array. Must have shape `[M]`, all values must * be >= 1. * @param paddings A 2-D array. Must have shape `[M, 2]`, all values must be >= * 0. `paddings[i] = [padStart, padEnd]` specifies the amount to zero-pad * from input dimension `i + 1`, which corresponds to spatial dimension `i`. It * is required that * `(inputShape[i + 1] + padStart + padEnd) % blockShape[i] === 0` * * This operation is equivalent to the following steps: * * 1. Zero-pad the start and end of dimensions `[1, ..., M]` of the input * according to `paddings` to produce `padded` of shape paddedShape. * * 2. Reshape `padded` to `reshapedPadded` of shape: * `[batch] + [paddedShape[1] / blockShape[0], blockShape[0], ..., * paddedShape[M] / blockShape[M-1], blockShape[M-1]] + remainingShape` * * 3. Permute dimensions of `reshapedPadded` to produce `permutedReshapedPadded` * of shape: `blockShape + [batch] + [paddedShape[1] / blockShape[0], ..., * paddedShape[M] / blockShape[M-1]] + remainingShape` * * 4. Reshape `permutedReshapedPadded` to flatten `blockShape` into the * batch dimension, producing an output tensor of shape: * `[batch * prod(blockShape)] + [paddedShape[1] / blockShape[0], ..., * paddedShape[M] / blockShape[M-1]] + remainingShape` * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function spaceToBatchND_(x, blockShape, paddings) { const $x = convertToTensor(x, 'x', 'spaceToBatchND'); assert($x.rank >= 1 + blockShape.length, () => `input rank ${$x.rank} should be > than [blockShape] ${blockShape.length}`); assert(paddings.length === blockShape.length, () => `paddings.shape[0] ${paddings.length} must be equal to [blockShape] ${blockShape.length}`); assert($x.shape.reduce((a, b, i) => { if (i > 0 && i <= blockShape.length) { return a && ((b + paddings[i - 1][0] + paddings[i - 1][1]) % blockShape[i - 1] === 0); } return a; }, true), () => `input spatial dimensions ${$x.shape.slice(1)} with paddings ${paddings.toString()} must be divisible by blockShapes ${blockShape.toString()}`); const inputs = { x: $x }; const attrs = { blockShape, paddings }; return ENGINE.runKernel(SpaceToBatchND, inputs, attrs); } const spaceToBatchND = /* @__PURE__ */ op({ spaceToBatchND_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const batchToSpaceNDGradConfig = { kernelName: BatchToSpaceND, gradFunc: (dy, saved, attrs) => { const { blockShape, crops } = attrs; return { x: () => spaceToBatchND(dy, blockShape, crops) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const broadcastToGradConfig = { kernelName: BroadcastTo, gradFunc: (dy, saved, attrs) => { const broadCastToAttrs = attrs; const inputShape = broadCastToAttrs.inputShape; const outputShape = broadCastToAttrs.shape; const reps = Array.from(outputShape); for (let i = inputShape.length - 1; i >= 0; i--) { if (inputShape[i] === outputShape[i]) { reps[i] = 1; } else if (inputShape[i] !== 1) { throw new Error(`broadcastTo(): [${inputShape}] cannot be broadcast to [${outputShape}].`); } } const axes = []; for (let i = 0; i < reps.length; i++) { if (reps[i] > 1) { axes.push(i); } } return { x: () => sum(dy, axes, true /* keepDims */) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const castGradConfig = { kernelName: Cast, gradFunc: (dy) => { return { x: () => dy.clone() }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const ceilGradConfig = { kernelName: Ceil, gradFunc: (dy) => { // TODO(manrajgrover): Return null for gradients when backprop supports it. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of (a >= b) element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([1, 2, 3]); * const b = tf.tensor1d([2, 2, 2]); * * a.greaterEqual(b).print(); * ``` * * @param a The first input tensor. * @param b The second input tensor. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function greaterEqual_(a, b) { let $a = convertToTensor(a, 'a', 'greaterEqual', 'string_or_numeric'); let $b = convertToTensor(b, 'b', 'greaterEqual', 'string_or_numeric'); [$a, $b] = makeTypesMatch($a, $b); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(GreaterEqual, inputs); } const greaterEqual = /* @__PURE__ */ op({ greaterEqual_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of (a <= b) element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([1, 2, 3]); * const b = tf.tensor1d([2, 2, 2]); * * a.lessEqual(b).print(); * ``` * * @param a The first input tensor. * @param b The second input tensor. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function lessEqual_(a, b) { let $a = convertToTensor(a, 'a', 'lessEqual', 'string_or_numeric'); let $b = convertToTensor(b, 'b', 'lessEqual', 'string_or_numeric'); [$a, $b] = makeTypesMatch($a, $b); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(LessEqual, inputs); } const lessEqual = /* @__PURE__ */ op({ lessEqual_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of `a AND b` element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([false, false, true, true], 'bool'); * const b = tf.tensor1d([false, true, false, true], 'bool'); * * a.logicalAnd(b).print(); * ``` * * @param a The first input tensor. Must be of dtype bool. * @param b The second input tensor. Must be of dtype bool. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function logicalAnd_(a, b) { const $a = convertToTensor(a, 'a', 'logicalAnd', 'bool'); const $b = convertToTensor(b, 'b', 'logicalAnd', 'bool'); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(LogicalAnd, inputs); } const logicalAnd = /* @__PURE__ */ op({ logicalAnd_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Creates a new tensor with the same values and shape as the specified * tensor. * * ```js * const x = tf.tensor([1, 2]); * * x.clone().print(); * ``` * * @param x The tensor to clone. * * @doc {heading: 'Tensors', subheading: 'Creation'} */ function clone_(x) { const $x = convertToTensor(x, 'x', 'clone', 'string_or_numeric'); const inputs = { x: $x }; // Note this op is called tf.identity in python. Hence the kernel name used // here. return ENGINE.runKernel(Identity, inputs); } const clone = /* @__PURE__ */ op({ clone_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Broadcast an array to a compatible shape NumPy-style. * * The tensor's shape is compared to the broadcast shape from end to beginning. * Ones are prepended to the tensor's shape until it has the same length as * the broadcast shape. If input.shape[i]==shape[i], the (i+1)-th axis is * already broadcast-compatible. If input.shape[i]==1 and shape[i]==N, then * the input tensor is tiled N times along that axis (using tf.tile). * * @param input The tensor that is to be broadcasted. * @param shape The input is to be broadcast to this shape. * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function broadcastTo_(x, shape) { let input = convertToTensor(x, 'broadcastTo', 'x'); const xShape = input.shape; assertNonNegativeIntegerDimensions(shape); if (shape.length < input.rank) { throw new Error(`broadcastTo(): shape.length=${shape.length} < input.rank=${input.rank}.`); } if (shape.length > input.rank) { const newShape = input.shape.slice(); while (newShape.length < shape.length) { newShape.unshift(1); } input = reshape$1(input, newShape); } const inputShape = input.shape; const reps = Array.from(shape); for (let i = shape.length - 1; i >= 0; i--) { if (inputShape[i] === shape[i]) { reps[i] = 1; } else if (input.shape[i] !== 1) { throw new Error(`broadcastTo(): [${xShape}] cannot be broadcast to [${shape}].`); } } const axes = reps.map((n, i) => n > 1 ? i : -1).filter(i => i >= 0); if (axes.length === 0) { return clone(input); } // TODO call broadcastTo kernel directly once backends implement broadcstTo const inputs = { x: input }; const attrs = { reps }; return ENGINE.runKernel(Tile, inputs, attrs); } const broadcastTo = /* @__PURE__ */ op({ broadcastTo_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the elements, either `a` or `b` depending on the `condition`. * * If the condition is true, select from `a`, otherwise select from `b`. * * ```js * const cond = tf.tensor1d([false, false, true], 'bool'); * const a = tf.tensor1d([1 , 2, 3]); * const b = tf.tensor1d([-1, -2, -3]); * * a.where(cond, b).print(); * ``` * * @param condition The input condition. Must be of dtype bool. * @param a If `condition` is rank 1, `a` may have a higher rank but * its first dimension must match the size of `condition`. * @param b A tensor with the same dtype as `a` and with shape that is * compatible with `a`. * @return A tensor with same dtype as `a` and `b`, and shape that is * broadcastable from `a` and `b`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function where_(condition, a, b) { const $a = convertToTensor(a, 'a', 'where'); const $b = convertToTensor(b, 'b', 'where'); const $condition = convertToTensor(condition, 'condition', 'where', 'bool'); // TODO: move this logic to forward function when the broadcastTo op is // implemented in WASM. // Find the broadcastable shape for $condition, $a, and $b. const broadcastShape = assertAndGetBroadcastShape(assertAndGetBroadcastShape($condition.shape, $a.shape), $b.shape); const $broadcastedCondition = broadcastTo($condition, broadcastShape); const $broadcastedA = broadcastTo($a, broadcastShape); const $broadcastedB = broadcastTo($b, broadcastShape); const inputs = { condition: $broadcastedCondition, t: $broadcastedA, e: $broadcastedB }; return ENGINE.runKernel(Select, inputs); } const where = /* @__PURE__ */ op({ where_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const clipByValueGradConfig = { kernelName: ClipByValue, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { clipValueMin, clipValueMax } = attrs; return { x: () => where(logicalAnd(greaterEqual(x, clipValueMin), lessEqual(x, clipValueMax)), dy, zerosLike(dy)), }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const complexAbsGradConfig = { kernelName: ComplexAbs, inputsToSave: ['x'], gradFunc: absGradConfig.gradFunc, }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * 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]`. * * ```js * 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(); * ``` * * @param x The input tensor to split. * @param numOrSizeSplits 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]`. * Can contain one -1 indicating that dimension is to be inferred. * @param axis The dimension along which to split. Defaults to 0 (the first * dim). * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function split_(x, numOrSizeSplits, axis = 0) { const $x = convertToTensor(x, 'x', 'split'); const inputs = { x: $x }; const attr = { numOrSizeSplits, axis }; return ENGINE.runKernel(SplitV, inputs, attr); } const split = /* @__PURE__ */ op({ split_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const concatGradConfig = { kernelName: Concat, saveAllInputs: true, gradFunc: (dy, saved, attrs) => { const shapes = saved.map(t => t.shape); const { axis } = attrs; const $axis = parseAxisParam(axis, saved[0].shape)[0]; const sizeSplits = shapes.map(s => s[$axis]); const derTensors = split(dy, sizeSplits, $axis); return derTensors.map(t => () => t); } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the derivative of the filter of a 2D convolution. * * @param x The input tensor, of rank 4 or rank 3 of shape * [batch, height, width, inChannels]. If rank 3, batch of 1 is assumed. * @param dy The dy image, of rank 4 or rank 3, of shape * [batch, height, width, outDepth]. If rank 3, batch of 1 is assumed. * @param filterShape The shape of the filter, length 4, * [filterHeight, filterWidth, inDepth, outDepth]. * @param strides The strides of the convolution: [strideHeight, * strideWidth]. * @param pad A string from: 'same', 'valid'. The type of padding algorithm * used in the forward prop of the op. * @param dataFormat: 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]. * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. */ function conv2DBackpropFilter_(x, dy, filterShape, strides, pad, dataFormat = 'NHWC', dimRoundingMode) { let x4D = x; if (x.rank === 3) { x4D = reshape$1(x, [1, x.shape[0], x.shape[1], x.shape[2]]); } let dy4D = dy; if (dy4D.rank === 3) { dy4D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]); } assert(x4D.rank === 4, () => `Error in conv2dDerFilter: input must be rank 4, but got shape ` + `${x4D.shape}.`); assert(dy4D.rank === 4, () => `Error in conv2dDerFilter: dy must be rank 4, but got shape ` + `${dy4D.shape}.`); assert(filterShape.length === 4, () => `Error in conv2dDerFilter: filterShape must be length 4, but got ` + `${filterShape}.`); const inDepth = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1]; const outDepth = dataFormat === 'NHWC' ? dy4D.shape[3] : dy4D.shape[1]; assert(inDepth === filterShape[2], () => `Error in conv2dDerFilter: depth of input ${inDepth}) must ` + `match input depth in filter (${filterShape[2]}.`); assert(outDepth === filterShape[3], () => `Error in conv2dDerFilter: depth of dy (${outDepth}) must ` + `match output depth for filter (${filterShape[3]}).`); checkPadOnDimRoundingMode('conv2dDerFilter', pad, dimRoundingMode); const inputs = { x: x4D, dy: dy4D }; const attrs = { strides, pad, dataFormat, dimRoundingMode, filterShape }; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(Conv2DBackpropFilter, inputs, attrs); } const conv2DBackpropFilter = /* @__PURE__ */ op({ conv2DBackpropFilter_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the derivative of the input of a 2D convolution. * * @param xShape The shape of the input: [batch, height, width, inDepth]. * If length of 3, batch of 1 is assumed. * @param dy The derivative of the output, of rank 4 or rank 3 of shape * `[batch, outHeight, outWidth, outDepth]`. If rank 3, batch of 1 is * assumed. * @param filter The filter, rank 4, of shape * `[filterHeight, filterWidth, inDepth, outDepth]`. * @param strides The strides of the convolution: `[strideHeight, * strideWidth]`. * @param pad The type of padding algorithm used: * - `same` and stride 1: output will be of same size as input, * regardless of filter size. * - `valid`: output will be smaller than input if filter is larger * than 1x1. * @param dataFormat: 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]. * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. */ function conv2DBackpropInput_(xShape, dy, filter, strides, pad, dataFormat = 'NHWC', dimRoundingMode) { assert(xShape.length === dy.rank, () => `Length of inShape ` + `(${xShape.length}) and rank of dy (${dy.rank}) must match`); let xShape4D = xShape; let dy4D = dy; let reshapedTo4D = false; if (dy.rank === 3) { reshapedTo4D = true; dy4D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]); xShape4D = [1, xShape[0], xShape[1], xShape[2]]; } assert(xShape4D.length === 4, () => `Error in conv2dDerInput: inShape must be length 4, but got length ` + `${xShape4D.length}.`); assert(dy4D.rank === 4, () => `Error in conv2dDerInput: dy must be rank 4, but got ` + `rank ${dy4D.rank}`); assert(filter.rank === 4, () => `Error in conv2dDerInput: filter must be rank 4, but got ` + `rank ${filter.rank}`); const inDepth = dataFormat === 'NHWC' ? xShape4D[3] : xShape4D[1]; const outDepth = dataFormat === 'NHWC' ? dy4D.shape[3] : dy4D.shape[1]; assert(inDepth === filter.shape[2], () => `Error in conv2dDerInput: depth of input (${inDepth}) must ` + `match input depth for filter ${filter.shape[2]}.`); assert(outDepth === filter.shape[3], () => `Error in conv2dDerInput: depth of output (${outDepth}) must ` + `match output depth for filter ${filter.shape[3]}.`); checkPadOnDimRoundingMode('conv2dDerInput', pad, dimRoundingMode); const inputs = { dy: dy4D, filter }; const attrs = { strides, pad, dataFormat, dimRoundingMode, inputShape: xShape4D }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(Conv2DBackpropInput, inputs, attrs); if (reshapedTo4D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3]]); } return res; } const conv2DBackpropInput = /* @__PURE__ */ op({ conv2DBackpropInput_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const conv2DGradConfig = { kernelName: Conv2D$1, inputsToSave: ['x', 'filter'], gradFunc: (dy, saved, attrs) => { const [x4D, $filter] = saved; const { dilations, strides, pad, dataFormat } = attrs; assert(tupleValuesAreOne(dilations), () => 'Error in gradient of conv2D: dilation rates greater than 1 ' + `are not yet supported in gradients. Got dilations '${dilations}'`); return { x: () => conv2DBackpropInput(x4D.shape, dy, $filter, strides, pad, dataFormat), filter: () => conv2DBackpropFilter(x4D, dy, $filter.shape, strides, pad, dataFormat) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes a 2D convolution over the input x. * * @param x The input tensor, of rank 4 or rank 3, of shape * `[batch, height, width, inChannels]`. If rank 3, batch of 1 is * assumed. * @param filter The filter, rank 4, of shape * `[filterHeight, filterWidth, inDepth, outDepth]`. * @param strides The strides of the convolution: `[strideHeight, * strideWidth]`. * @param pad The type of padding algorithm. * - `same` and stride 1: output will be of same size as input, * regardless of filter size. * - `valid`: output will be smaller than input if filter is larger * than 1x1. * - For more info, see this guide: * [https://www.tensorflow.org/api_docs/python/tf/nn/convolution]( * https://www.tensorflow.org/api_docs/python/tf/nn/convolution) * @param dataFormat: 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]. * @param dilations 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. * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. * * @doc {heading: 'Operations', subheading: 'Convolution'} */ function conv2d_(x, filter, strides, pad, dataFormat = 'NHWC', dilations = [1, 1], dimRoundingMode) { const $x = convertToTensor(x, 'x', 'conv2d', 'float32'); const $filter = convertToTensor(filter, 'filter', 'conv2d', 'float32'); let x4D = $x; let reshapedTo4D = false; if ($x.rank === 3) { reshapedTo4D = true; x4D = reshape$1($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]); } assert(x4D.rank === 4, () => `Error in conv2d: input must be rank 4, but got rank ${x4D.rank}.`); assert($filter.rank === 4, () => `Error in conv2d: filter must be rank 4, but got rank ` + `${$filter.rank}.`); checkPadOnDimRoundingMode('conv2d', pad, dimRoundingMode); const inDepth = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1]; assert(inDepth === $filter.shape[2], () => `Error in conv2d: depth of input (${inDepth}) must match ` + `input depth for filter ${$filter.shape[2]}.`); assert(eitherStridesOrDilationsAreOne(strides, dilations), () => 'Error in conv2D: Either strides or dilations must be 1. ' + `Got strides ${strides} and dilations '${dilations}'`); assert(stridesOrDilationsArePositive(dilations), () => 'Error in conv2D: Dilated rates should be larger than 0.'); assert(stridesOrDilationsArePositive(strides), () => 'Error in conv2D: Strides should be larger than 0.'); const inputs = { x: x4D, filter: $filter }; const attrs = { strides, pad, dataFormat, dilations, dimRoundingMode }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(Conv2D$1, inputs, attrs); if (reshapedTo4D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3]]); } return res; } const conv2d$1 = /* @__PURE__ */ op({ conv2d_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const conv2DBackpropInputGradConfig = { kernelName: Conv2DBackpropInput, inputsToSave: ['dy', 'filter'], gradFunc: (ddx, saved, attrs) => { const [dy, filter] = saved; const { strides, pad, dataFormat, dimRoundingMode } = attrs; return { dy: () => conv2d$1(ddx, filter, strides, pad, dataFormat, 1 /* dilations */, dimRoundingMode), filter: () => conv2DBackpropFilter(ddx, dy, filter.shape, strides, pad, dataFormat, dimRoundingMode) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the derivative of the filter of a 3D convolution. * * @param x The input tensor, of rank 5 or rank 4 of shape * [batch, depth, height, width, inChannels]. If rank 4, batch of 1 is * assumed. * @param dy The dy image, of rank 5 or rank 4, of shape * [batch, depth, height, width, outDepth]. If rank 4, batch of 1 is * assumed. * @param filterShape The shape of the filter, length 5, * [filterDepth, filterHeight, filterWidth, inDepth, outDepth]. * @param strides The strides of the convolution: [strideDepth, strideHeight, * strideWidth]. * @param pad A string from: 'same', 'valid'. The type of padding algorithm * used in the forward prop of the op. */ function conv3DBackpropFilter_(x, dy, filterShape, strides, pad) { let x5D = x; if (x.rank === 4) { x5D = reshape$1(x, [1, x.shape[0], x.shape[1], x.shape[2], x.shape[3]]); } let dy5D = dy; if (dy5D.rank === 4) { dy5D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2], dy.shape[3]]); } assert(x5D.rank === 5, () => `Error in conv3dDerFilter: input must be rank 5, but got shape ` + `${x5D.shape}.`); assert(dy5D.rank === 5, () => `Error in conv3dDerFilter: dy must be rank 5, but got shape ` + `${dy5D.shape}.`); assert(filterShape.length === 5, () => `Error in conv3dDerFilter: filterShape must be length 5, but got ` + `${filterShape}.`); assert(x5D.shape[4] === filterShape[3], () => `Error in conv3dDerFilter: depth of input ${x5D.shape[4]}) must ` + `match input depth in filter (${filterShape[3]}.`); assert(dy5D.shape[4] === filterShape[4], () => `Error in conv3dDerFilter: depth of dy (${dy5D.shape[4]}) must ` + `match output depth for filter (${filterShape[4]}).`); const inputs = { x: x5D, dy: dy5D }; const attrs = { strides, pad, filterShape }; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(Conv3DBackpropFilterV2, inputs, attrs); } const conv3DBackpropFilter = /* @__PURE__ */ op({ conv3DBackpropFilter_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the derivative of the input of a 3D convolution. * * @param xShape The shape of the input: [batch, depth, height, width, * in_channels]. If length of 4, batch of 1 is assumed. * @param dy The derivative of the output, of rank 5 or rank 4 of shape * `[batch, outDepth, outHeight, outWidth, in_channels]`. * If rank 4, batch of 1 is assumed. * @param filter The filter, rank 5, of shape * `[filterDepth, filterHeight, filterWidth, inDepth, outDepth]`. * @param strides The strides of the convolution: `[strideDepth, strideHeight, * strideWidth]`. * @param pad The type of padding algorithm used: * - `same` and stride 1: output will be of same size as input, * regardless of filter size. * - `valid`: output will be smaller than input if filter is larger * than 1x1. */ function conv3DBackpropInput_(xShape, dy, filter, strides, pad) { assert(xShape.length === dy.rank, () => `Length of inShape ` + `(${xShape.length}) and rank of dy (${dy.rank}) must match`); let xShape5D = xShape; let dy5D = dy; let reshapedTo5D = false; if (dy.rank === 4) { reshapedTo5D = true; dy5D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2], dy.shape[3]]); xShape5D = [1, xShape[0], xShape[1], xShape[2], xShape[3]]; } const inDepth = xShape5D[4]; const outDepth = dy5D.shape[4]; assert(xShape5D.length === 5, () => `Error in conv3dDerInput: inShape must be length 5, but got length ` + `${xShape5D.length}.`); assert(dy5D.rank === 5, () => `Error in conv3dDerInput: dy must be rank 5, but got ` + `rank ${dy5D.rank}`); assert(filter.rank === 5, () => `Error in conv3dDerInput: filter must be rank 5, but got ` + `rank ${filter.rank}`); assert(inDepth === filter.shape[3], () => `Error in conv3dDerInput: depth of input (${inDepth}) must ` + `match input depth for filter ${filter.shape[3]}.`); assert(outDepth === filter.shape[4], () => `Error in conv3dDerInput: depth of output (${outDepth}) must ` + `match output depth for filter ${filter.shape[4]}.`); const inputs = { dy: dy5D, filter }; const attrs = { pad, strides, inputShape: xShape5D }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(Conv3DBackpropInputV2, inputs, attrs); if (reshapedTo5D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]); } return res; } const conv3DBackpropInput = /* @__PURE__ */ op({ conv3DBackpropInput_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const conv3DGradConfig = { kernelName: Conv3D$1, inputsToSave: ['x', 'filter'], gradFunc: (dy, saved, attrs) => { const { dilations, strides, pad } = attrs; assert(tupleValuesAreOne(dilations), () => 'Error in gradient of conv3D: dilation rates greater than 1 are ' + `not yet supported in gradients. Got dilations '${dilations}'`); const [x5D, $filter] = saved; return { x: () => conv3DBackpropInput(x5D.shape, dy, $filter, strides, pad), filter: () => conv3DBackpropFilter(x5D, dy, $filter.shape, strides, pad) }; } }; /** * @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. * ============================================================================= */ /** * Computes sin of the input Tensor element-wise: `sin(x)` * * ```js * const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]); * * x.sin().print(); // or tf.sin(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function sin_(x) { const $x = convertToTensor(x, 'x', 'sin', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Sin, inputs); } const sin = /* @__PURE__ */ op({ sin_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const cosGradConfig = { kernelName: Cos, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(neg(sin(cast(x, 'float32'))), dy) }; } }; /** * @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. * ============================================================================= */ /** * Computes hyperbolic sin of the input `tf.Tensor` element-wise: `sinh(x)` * * ```js * const x = tf.tensor1d([0, 1, -1, .7]); * * x.sinh().print(); // or tf.sinh(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function sinh_(x) { const $x = convertToTensor(x, 'x', 'sinh'); const inputs = { x: $x }; return ENGINE.runKernel(Sinh, inputs); } const sinh = /* @__PURE__ */ op({ sinh_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const coshGradConfig = { kernelName: Cosh, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(sinh(cast(x, 'float32')), dy) }; } }; /** * @license * Copyright 2017 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. * ============================================================================= */ /** * Returns true if the axis specifies the inner most dimensions of the * array. */ function axesAreInnerMostDims(axes, rank) { for (let i = 0; i < axes.length; ++i) { if (axes[axes.length - i - 1] !== rank - 1 - i) { return false; } } return true; } function combineLocations(outputLoc, reduceLoc, axes) { const rank = outputLoc.length + reduceLoc.length; const loc = []; let outIdx = 0; let reduceIdx = 0; for (let dim = 0; dim < rank; dim++) { if (axes.indexOf(dim) === -1) { loc.push(outputLoc[outIdx++]); } else { loc.push(reduceLoc[reduceIdx++]); } } return loc; } function computeOutAndReduceShapes(aShape, axes) { const outShape = []; const rank = aShape.length; for (let dim = 0; dim < rank; dim++) { if (axes.indexOf(dim) === -1) { outShape.push(aShape[dim]); } } const reduceShape = axes.map(dim => aShape[dim]); return [outShape, reduceShape]; } function expandShapeToKeepDim(shape, axes) { const reduceSubShape = axes.map(x => 1); return combineLocations(shape, reduceSubShape, axes); } /** * Returns the axes permutation to be used with `tf.transpose`, if such * permutation is necessary. Otherwise it returns null. This method is used by * operations that operate only on inner-most axes. */ function getAxesPermutation(axes, rank) { if (axesAreInnerMostDims(axes, rank)) { return null; } const result = []; for (let i = 0; i < rank; ++i) { if (axes.indexOf(i) === -1) { result.push(i); } } axes.forEach(axis => result.push(axis)); return result; } /** Returns the axes permutation that undoes the original permutation. */ function getUndoAxesPermutation(axes) { return axes.map((axis, i) => [i, axis]) .sort((a, b) => a[1] - b[1]) .map(x => x[0]); } /** * @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. * ============================================================================= */ /** * Computes the cumulative sum of a `tf.Tensor` along `axis`. * * ```js * const x = tf.tensor([1, 2, 3, 4]); * x.cumsum().print(); * ``` * ```js * const x = tf.tensor([[1, 2], [3, 4]]); * x.cumsum().print(); * ``` * * @param x The input tensor to be summed. * @param axis The axis along which to sum. Optional. Defaults to 0. * @param exclusive Whether to perform exclusive cumulative sum. Optional. * Defaults to false. If set to true then the sum of each tensor entry * does not include its own value, but only the values previous to it * along the specified axis. * @param reverse Whether to sum in the opposite direction. Optional. * Defaults to false. * * @doc {heading: 'Operations', subheading: 'Scan'} */ function cumsum_(x, axis = 0, exclusive = false, reverse = false) { const $x = convertToTensor(x, 'x', 'cumsum'); const inputs = { x: $x }; const attrs = { axis, exclusive, reverse }; return ENGINE.runKernel(Cumsum, inputs, attrs); } const cumsum = /* @__PURE__ */ op({ cumsum_ }); /** * @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. * ============================================================================= */ /** * Executes the provided function `fn` and after it is executed, cleans up all * intermediate tensors allocated by `fn` except those returned by `fn`. * `fn` must not return a Promise (async functions not allowed). The returned * result can be a complex object. * * Using this method helps avoid memory leaks. In general, wrap calls to * operations in `tf.tidy` for automatic memory cleanup. * * NOTE: Variables do *not* get cleaned up when inside a tidy(). If you want to * dispose variables, please use `tf.disposeVariables` or call dispose() * directly on variables. * * ```js * // 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(); * ``` * * @param nameOrFn 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. * @param fn The function to execute. * * @doc {heading: 'Performance', subheading: 'Memory'} */ function tidy(nameOrFn, fn) { return ENGINE.tidy(nameOrFn, fn); } /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Converts two real numbers to a complex number. * * Given a tensor `real` representing the real part of a complex number, and a * tensor `imag` representing the imaginary part of a complex number, this * operation returns complex numbers elementwise of the form [r0, i0, r1, i1], * where r represents the real part and i represents the imag part. * * The input tensors real and imag must have the same shape. * * ```js * const real = tf.tensor1d([2.25, 3.25]); * const imag = tf.tensor1d([4.75, 5.75]); * const complex = tf.complex(real, imag); * * complex.print(); * ``` * * @doc {heading: 'Tensors', subheading: 'Creation'} */ function complex_(real, imag) { const $real = convertToTensor(real, 'real', 'complex'); const $imag = convertToTensor(imag, 'imag', 'complex'); assertShapesMatch($real.shape, $imag.shape, `real and imag shapes, ${$real.shape} and ${$imag.shape}, ` + `must match in call to tf.complex().`); const inputs = { real: $real, imag: $imag }; return ENGINE.runKernel(Complex, inputs); } const complex = /* @__PURE__ */ op({ complex_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the imaginary part of a complex (or real) tensor. * * Given a tensor input, this operation returns a tensor of type float that is * the imaginary part of each element in input considered as a complex number. * If input is real, a tensor of all zeros is returned. * * ```js * const x = tf.complex([-2.25, 3.25], [4.75, 5.75]); * tf.imag(x).print(); * ``` * * @doc {heading: 'Tensors', subheading: 'Creation'} */ function imag_(input) { const $input = convertToTensor(input, 'input', 'imag'); const inputs = { input: $input }; return ENGINE.runKernel(Imag, inputs); } const imag = /* @__PURE__ */ op({ imag_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the real part of a complex (or real) tensor. * * Given a tensor input, this operation returns a tensor of type float that is * the real part of each element in input considered as a complex number. * * If the input is real, it simply makes a clone. * * ```js * const x = tf.complex([-2.25, 3.25], [4.75, 5.75]); * tf.real(x).print(); * ``` * * @doc {heading: 'Tensors', subheading: 'Creation'} */ function real_(input) { const $input = convertToTensor(input, 'input', 'real'); const inputs = { input: $input }; return ENGINE.runKernel(Real, inputs); } const real = /* @__PURE__ */ op({ real_ }); /** * @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. * ============================================================================= */ /** * 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.Tensor`s. * * ```js * const a = tf.tensor2d([1, 2, 3, 4, 5, 6], [2, 3]); * * a.transpose().print(); // or tf.transpose(a) * ``` * * @param x The tensor to transpose. * @param perm The permutation of the dimensions of a. * @param conjugate Will conjugate complex input if true. * * @doc {heading: 'Operations', subheading: 'Matrices'} */ function transpose_(x, perm, conjugate) { const $x = convertToTensor(x, 'x', 'transpose'); if (perm == null) { perm = $x.shape.map((s, i) => i).reverse(); } assert($x.rank === perm.length, () => `Error in transpose: rank of input ${$x.rank} ` + `must match length of perm ${perm}.`); perm.forEach(axis => { assert(axis >= 0 && axis < $x.rank, () => `All entries in 'perm' must be between 0 and ${$x.rank - 1}` + ` but got ${perm}`); }); if ($x.rank <= 1) { return $x.clone(); } const inputs = { x: $x }; const attrs = { perm }; if ($x.dtype === 'complex64') { return tidy(() => { let $real = real($x); let $imag = imag($x); $real = ENGINE.runKernel(Transpose, { x: $real }, attrs); $imag = ENGINE.runKernel(Transpose, { x: $imag }, attrs); if (conjugate) { $imag = neg($imag); } return complex($real, $imag); }); } return ENGINE.runKernel(Transpose, inputs, attrs); } const transpose = /* @__PURE__ */ op({ transpose_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const cumsumGradConfig = { kernelName: Cumsum, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { axis, exclusive, reverse } = attrs; return { x: () => { const permutation = getAxesPermutation([axis], x.rank); let out = cumsum(dy, axis, exclusive, !reverse); if (permutation != null) { out = transpose(out, permutation); } return out; } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ function depthwiseConv2dNativeBackpropFilter_(x, dy, filterShape, strides, pad, dilations = [1, 1], dimRoundingMode) { let x4D = x; if (x.rank === 3) { x4D = reshape$1(x, [1, x.shape[0], x.shape[1], x.shape[2]]); } let dy4D = dy; if (dy4D.rank === 3) { dy4D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]); } const inputs = { x: x4D, dy: dy4D }; const attrs = { strides, pad, dimRoundingMode, dilations, filterShape }; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(DepthwiseConv2dNativeBackpropFilter, inputs, attrs); } const depthwiseConv2dNativeBackpropFilter = op({ depthwiseConv2dNativeBackpropFilter_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ function depthwiseConv2dNativeBackpropInput_(xShape, dy, filter, strides, pad, dilations = [1, 1], dimRoundingMode) { let dy4D = dy; let reshapedTo4D = false; if (dy.rank === 3) { reshapedTo4D = true; dy4D = reshape$1(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]); } const inputs = { dy: dy4D, filter }; const attrs = { strides, pad, dimRoundingMode, dilations, inputShape: xShape }; const res = // tslint:disable-next-line: no-unnecessary-type-assertion ENGINE.runKernel(DepthwiseConv2dNativeBackpropInput, inputs, attrs); if (reshapedTo4D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3]]); } return res; } const depthwiseConv2dNativeBackpropInput = op({ depthwiseConv2dNativeBackpropInput_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const depthwiseConv2dNativeGradConfig = { kernelName: DepthwiseConv2dNative, inputsToSave: ['x', 'filter'], gradFunc: (dy, saved, attrs) => { const { dilations, strides, pad, dimRoundingMode } = attrs; const $dilations = dilations == null ? [1, 1] : dilations; assert(tupleValuesAreOne($dilations), () => 'Error in gradient of depthwiseConv2dNative: dilation rates ' + `greater than 1 are not yet supported. Got dilations ` + `'${$dilations}'`); const [x, filter] = saved; assert(x.rank === 4, () => `Error in gradient of depthwiseConv2dNative: input must be ` + `rank 4, but got rank ${x.rank}.`); assert(filter.rank === 4, () => `Error in gradient of depthwiseConv2dNative: filter must be ` + `rank 4, but got rank ${filter.rank}.`); assert(x.shape[3] === filter.shape[2], () => `Error in gradient of depthwiseConv2d: number of input ` + `channels (${x.shape[3]}) must match the inChannels dimension ` + `in filter ${filter.shape[2]}.`); assert(eitherStridesOrDilationsAreOne(strides, $dilations), () => 'Error in gradient of depthwiseConv2d: Either strides or ' + `dilations must be 1. Got strides ${strides} and dilations ` + `'${$dilations}'.`); checkPadOnDimRoundingMode('depthwiseConv2d', pad, dimRoundingMode); return { x: () => depthwiseConv2dNativeBackpropInput(x.shape, dy, filter, strides, pad, $dilations, dimRoundingMode), filter: () => depthwiseConv2dNativeBackpropFilter(x, dy, filter.shape, strides, pad, $dilations, dimRoundingMode), }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const dilation2dGradConfig = { kernelName: Dilation2D, inputsToSave: ['x', 'filter'], gradFunc: (dy, saved, attrs) => { const [x, filter] = saved; const inputInputs = { x, filter, dy }; const filterInputs = { x, filter, dy }; return { x: () => ENGINE.runKernel(Dilation2DBackpropInput, inputInputs, attrs), filter: () => ENGINE.runKernel(Dilation2DBackpropFilter, filterInputs, attrs) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const eluGradConfig = { kernelName: Elu$1, outputsToSave: [true], gradFunc: (dy, saved) => { const [y] = saved; const inputs = { dy, y }; return { x: () => ENGINE.runKernel(EluGrad, inputs) }; } }; /** * @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. * ============================================================================= */ /** * Computes exponential of the input `tf.Tensor` element-wise. `e ^ x` * * ```js * const x = tf.tensor1d([1, 2, -3]); * * x.exp().print(); // or tf.exp(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function exp_(x) { const $x = convertToTensor(x, 'x', 'exp'); const inputs = { x: $x }; return ENGINE.runKernel(Exp, inputs); } const exp = /* @__PURE__ */ op({ exp_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const erfGradConfig = { kernelName: Erf, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; const a = mul(exp(neg(square(x))), 2 / Math.sqrt(Math.PI)); return { x: () => mul(dy, a) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const expGradConfig = { kernelName: Exp, outputsToSave: [true], gradFunc: (dy, saved) => { const [y] = saved; return { x: () => mul(dy, y) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const expandDimsGradConfig = { kernelName: ExpandDims, inputsToSave: ['input'], gradFunc: (dy, saved) => { const [input] = saved; return { input: () => reshape$1(dy, input.shape) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const expm1GradConfig = { kernelName: Expm1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(dy, exp(x)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const floorGradConfig = { kernelName: Floor, gradFunc: (dy) => { return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const floorDivGradConfig = { kernelName: FloorDiv, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { const res = div(dy, cast(b, 'float32')); const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(res, reduceAxes), a.shape); } return res; }; const derB = () => { let res = mul(dy, cast(a, 'float32')); const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { res = reshape$1(sum(res, reduceAxes), b.shape); } const tmp = square(b); return neg(div(res, cast(tmp, 'float32'))); }; return { a: derA, b: derB }; } }; /** * @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. * ============================================================================= */ /** * Computes reciprocal of square root of the input `tf.Tensor` element-wise: * `y = 1 / sqrt(x)` * * ```js * const x = tf.tensor1d([1, 2, 4, -1]); * * x.rsqrt().print(); // or tf.rsqrt(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function rsqrt_(x) { const $x = convertToTensor(x, 'x', 'rsqrt', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Rsqrt, inputs); } const rsqrt = /* @__PURE__ */ op({ rsqrt_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Construct a tensor by repeating it the number of times given by reps. * * This operation creates a new tensor by replicating `input` `reps` * times. The output tensor's `i`th dimension has `input.shape[i] * * reps[i]` elements, and the values of `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]`. * * ```js * const a = tf.tensor1d([1, 2]); * * a.tile([2]).print(); // or tf.tile(a, [2]) * ``` * * ```js * const a = tf.tensor2d([1, 2, 3, 4], [2, 2]); * * a.tile([1, 2]).print(); // or tf.tile(a, [1,2]) * ``` * @param x The tensor to tile. * @param reps Determines the number of replications per dimension. * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function tile_(x, reps) { const $x = convertToTensor(x, 'x', 'tile', 'string_or_numeric'); assert($x.rank === reps.length, () => `Error in transpose: rank of input ${$x.rank} ` + `must match length of reps ${reps}.`); const inputs = { x: $x }; const attrs = { reps }; return ENGINE.runKernel(Tile, inputs, attrs); } const tile = /* @__PURE__ */ op({ tile_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const fusedBatchNormGradConfig = { kernelName: FusedBatchNorm, inputsToSave: ['x', 'mean', 'variance', 'scale'], gradFunc: (dy, saved, attrs) => { const { varianceEpsilon } = attrs; const [x, mean, variance, scale] = saved; const scaleValue = scale == null ? scalar(1) : scale; const reductionAxes = getReductionAxes(mean.shape, x.shape); const tileShape = []; if (mean.rank === 1) { for (let i = 0; i < x.shape.length - 1; ++i) { tileShape.push(x.shape[i]); } tileShape.push(1); } const xMinusMean = sub(x, mean); const dyTimesScaleValue = mul(dy, scaleValue); const oneOverSqrtVariance = rsqrt(add$1(variance, scalar(varianceEpsilon))); const minusHalfRCube = mul(mul(mul(oneOverSqrtVariance, oneOverSqrtVariance), oneOverSqrtVariance), scalar(-0.5)); const derX = () => { if (mean.rank === 1) { return reshape$1(mul(mul(dy, tile(reshape$1(oneOverSqrtVariance, [1, 1, 1, mean.shape[0]]), tileShape)), scaleValue), x.shape); } else { return reshape$1(mul(mul(dy, oneOverSqrtVariance), scaleValue), x.shape); } }; const derMean = () => { let meanDer = mul(mul(oneOverSqrtVariance, scalar(-1)), dyTimesScaleValue); if (mean.rank === 1) { meanDer = sum(meanDer, reductionAxes); } return reshape$1(meanDer, mean.shape); }; const derVariance = () => { let varianceDer = mul(mul(minusHalfRCube, xMinusMean), dyTimesScaleValue); if (mean.rank === 1) { varianceDer = sum(varianceDer, reductionAxes); } return reshape$1(varianceDer, mean.shape); }; const derScale = () => { const xMinusMean2TimesRsqrt = mul(xMinusMean, oneOverSqrtVariance); let scaleDer = mul(dy, xMinusMean2TimesRsqrt); if (mean.rank === 1) { scaleDer = sum(scaleDer, reductionAxes); } return reshape$1(scaleDer, mean.shape); }; const derOffset = () => { let offsetDer = dy; if (mean.rank === 1) { offsetDer = sum(offsetDer, reductionAxes); } return reshape$1(offsetDer, mean.shape); }; return { x: derX, mean: derMean, variance: derVariance, scale: derScale, offset: derOffset }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Stacks a list of rank-`R` `tf.Tensor`s into one rank-`(R+1)` `tf.Tensor`. * * ```js * const a = tf.tensor1d([1, 2]); * const b = tf.tensor1d([3, 4]); * const c = tf.tensor1d([5, 6]); * tf.stack([a, b, c]).print(); * ``` * * @param tensors A list of tensor objects with the same shape and dtype. * @param axis The axis to stack along. Defaults to 0 (the first dim). * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function stack_(tensors, axis = 0) { const $tensors = convertToTensorArray(tensors, 'tensors', 'stack', 'string_or_numeric'); assert($tensors.length >= 1, () => 'Pass at least one tensor to tf.stack'); if ($tensors.length > 0) { assert(axis <= $tensors[0].rank, () => 'Axis must be <= rank of the tensor'); } const inputs = $tensors; const attrs = { axis }; return ENGINE.runKernel(Pack, inputs, attrs); } const stack = /* @__PURE__ */ op({ stack_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the sum along segments of a `tf.Tensor`. * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * const segmentIds = tf.tensor1d([1, 2, 0, 1], 'int32'); * const numSegments = 3; * * x.unsortedSegmentSum(segmentIds, numSegments).print() * //or tf.unsortedSegmentSum(x, segmentIds, numSegments) * ``` * @param x The `tf.Tensor` that will be summed along its segments. * @param segmentIds A `tf.Tensor1D` whose rank is equal to the rank of `x`'s * dimension along the `axis`. Maps each element of `x` to a segment. * @param numSegments The number of distinct `segmentIds`. * * @doc {heading: 'Operations', subheading: 'Segment'} */ function unsortedSegmentSum_(x, segmentIds, numSegments) { const $x = convertToTensor(x, 'x', 'unsortedSegmentSum'); const $segmentIds = convertToTensor(segmentIds, 'segmentIds', 'unsortedSegmentSum', 'int32'); assert(isInt(numSegments), () => 'numSegments must be of dtype int'); const inputs = { x: $x, segmentIds: $segmentIds }; const attrs = { numSegments }; return ENGINE.runKernel(UnsortedSegmentSum, inputs, attrs); } const unsortedSegmentSum = /* @__PURE__ */ op({ unsortedSegmentSum_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const gatherGradConfig = { kernelName: GatherV2, inputsToSave: ['x', 'indices'], gradFunc: (dy, saved, attrs) => { const [x, indices] = saved; const { axis, batchDims } = attrs; const parsedAxis = parseAxisParam(axis, x.shape)[0]; const derXBatch = (x, indices, dy) => { return () => { const paramsShape = x.shape; const indicesSize = indices.size; const outerShape = paramsShape.slice(0, parsedAxis); const outerDims = outerShape.length; const innerShape = paramsShape.slice(axis, paramsShape.length).slice(1); const innerDims = innerShape.length; const outerAxesIndices = arrayRange(0, outerDims); const innerAxesIndices = arrayRange(outerDims + 1, outerDims + 1 + innerDims); const valuesShape = arrayConcat([outerShape, [indicesSize], innerShape]); const values = reshape$1(dy, valuesShape); const reshapedIndices = reshape$1(indices, [indicesSize]); const transposeDims = arrayConcat([[outerDims], outerAxesIndices, innerAxesIndices]); const valuesTranspose = transpose(values, transposeDims); let paramsGrad = unsortedSegmentSum(valuesTranspose, reshapedIndices, x.shape[parsedAxis]); const invertTransposeDims = getUndoAxesPermutation(transposeDims); paramsGrad = transpose(paramsGrad, invertTransposeDims); return paramsGrad; }; }; if (batchDims === 1) { const batchSize = x.shape[0]; const xBatch = x.split(batchSize, 0); const derXBatched = () => { const stacked = stack(xBatch.map((x, i) => { return derXBatch(x, indices.slice(i, 1), dy.slice(i, 1))(); })); return stacked.reshape(x.shape); }; return { x: derXBatched, indices: () => indices }; } else { return { x: derXBatch(x, indices, dy), indices: () => indices }; } } }; function arrayRange(start, stop) { const result = []; for (let i = start; i < stop; ++i) { result.push(i); } return result; } function arrayConcat(arrays) { const result = []; for (let i = 0; i < arrays.length; ++i) { for (let j = 0; j < arrays[i].length; ++j) { result.push(arrays[i][j]); } } return result; } /** * @license * Copyright 2020 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. * ============================================================================= */ const greaterEqualGradConfig = { kernelName: GreaterEqual, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; return { a: () => zerosLike(a), b: () => zerosLike(b) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const identityGradConfig = { kernelName: Identity, gradFunc: (dy) => { return { x: () => cast(dy, 'float32') }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const isFiniteGradConfig = { kernelName: IsFinite, gradFunc: (dy) => { // TODO(nsthorat): Let gradients be null for cases where we want to stop // backpropgation. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const isInfGradConfig = { kernelName: IsInf, gradFunc: (dy) => { // TODO(nsthorat): Let gradients be null for cases where we want to stop // backpropgation. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const isNanGradConfig = { kernelName: IsNan, gradFunc: (dy) => { // TODO(nsthorat): Let gradients be null for cases where we want to stop // backpropgation. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of (a > b) element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([1, 2, 3]); * const b = tf.tensor1d([2, 2, 2]); * * a.greater(b).print(); * ``` * * @param a The first input tensor. * @param b The second input tensor. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function greater_(a, b) { let $a = convertToTensor(a, 'a', 'greater', 'string_or_numeric'); let $b = convertToTensor(b, 'b', 'greater', 'string_or_numeric'); [$a, $b] = makeTypesMatch($a, $b); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Greater, inputs); } const greater$1 = /* @__PURE__ */ op({ greater_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const leakyReluGradConfig = { kernelName: LeakyRelu, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { alpha } = attrs; const mask = greater$1(x, 0); // Returns `gradients * (features > 0) + alpha * gradients * (features <= // 0)`. return { x: () => where(mask, dy, mul(dy, alpha)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const log1pGradConfig = { kernelName: Log1p, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, add$1(x, 1)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const logGradConfig = { kernelName: Log, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, cast(x, 'float32')) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const logSoftmaxGradConfig = { kernelName: LogSoftmax$1, inputsToSave: [], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const [value] = saved; const { axis } = attrs; return { logits: () => { const keepDims = true; const softmax = exp(value); return sub(dy, mul(sum(dy, axis, keepDims), softmax)); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ function localResponseNormalizationBackprop_(x, y, dy, depthRadius = 5, bias = 1, alpha = 1, beta = 0.5) { const inputs = { x, y, dy }; const attrs = { depthRadius, bias, alpha, beta }; return ENGINE.runKernel(LRNGrad, inputs, attrs); } const localResponseNormalizationBackprop = op({ localResponseNormalizationBackprop_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const lrnGradConfig = { kernelName: LRN, inputsToSave: ['x'], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const [x, y] = saved; const { depthRadius, bias, alpha, beta } = attrs; return { x: () => localResponseNormalizationBackprop(x, y, dy, depthRadius, bias, alpha, beta) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of (a == b) element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([1, 2, 3]); * const b = tf.tensor1d([2, 2, 2]); * * a.equal(b).print(); * ``` * * @param a The first input tensor. * @param b The second input tensor. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function equal_(a, b) { let $a = convertToTensor(a, 'a', 'equal', 'string_or_numeric'); let $b = convertToTensor(b, 'b', 'equal', 'string_or_numeric'); [$a, $b] = makeTypesMatch($a, $b); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Equal, inputs); } const equal = /* @__PURE__ */ op({ equal_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Gradient helper function for the min and max operations. */ function gradForMinAndMax(dy, y, xOrig, origAxes) { if (y.rank < xOrig.rank) { y = reshape$1(y, expandShapeToKeepDim(y.shape, origAxes)); } if (dy.rank < xOrig.rank) { dy = reshape$1(dy, expandShapeToKeepDim(dy.shape, origAxes)); } return { x: () => { const dx = mul(dy, cast(equal(xOrig, y), dy.dtype)); return dx; } }; } /** * @license * Copyright 2020 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. * ============================================================================= */ const maxGradConfig = { kernelName: Max, inputsToSave: ['x'], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const maxAttrs = attrs; const { reductionIndices } = maxAttrs; const x = saved[0]; const y = saved[1]; const origAxes = parseAxisParam(reductionIndices, x.shape); const maxGrad = gradForMinAndMax(dy, y, x, origAxes); return { x: () => { return maxGrad['x'](); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of (a < b) element-wise. Supports broadcasting. * * ```js * const a = tf.tensor1d([1, 2, 3]); * const b = tf.tensor1d([2, 2, 2]); * * a.less(b).print(); * ``` * @param a The first input tensor. * @param b The second input tensor. Must have the same dtype as `a`. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function less_(a, b) { let $a = convertToTensor(a, 'a', 'less', 'string_or_numeric'); let $b = convertToTensor(b, 'b', 'less', 'string_or_numeric'); [$a, $b] = makeTypesMatch($a, $b); assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Less, inputs); } const less$1 = /* @__PURE__ */ op({ less_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const maximumGradConfig = { kernelName: Maximum$1, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const derA = () => mul(dy, cast(greaterEqual(a, b), 'float32')); const derB = () => mul(dy, cast(less$1(a, b), 'float32')); return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the backprop of a 3d max pool. * * @param dy The dy error, of rank 5 of shape * [batchSize, depth, height, width, channels]. * assumed. * @param input The original input image, of rank 5 or rank 4 of shape * [batchSize, depth, height, width, channels]. * @param output The original output image, of rank 5 of shape * [batchSize, outDepth, outHeight, outWidth, channels]. * @param filterSize The filter size: * `[filterDepth, filterHeight, filterWidth]`. * `filterSize` is a single number, * then `filterDepth == filterHeight == filterWidth`. * @param strides The strides of the pooling: * `[strideDepth, strideHeight, strideWidth]`. If * `strides` is a single number, then `strideHeight == strideWidth`. * @param pad A string from: 'same', 'valid'. The type of padding algorithm * used in the forward prop of the op. * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. */ function maxPool3dGrad_(dy, input, output, filterSize, strides, pad, dimRoundingMode) { const $dy = convertToTensor(dy, 'dy', 'maxPool3dGrad'); const $input = convertToTensor(input, 'input', 'maxPool3dGrad'); const $output = convertToTensor(output, 'output', 'maxPool3dGrad'); let dy5D = $dy; let input5D = $input; let output5D = $output; let reshapedTo5D = false; if ($input.rank === 4) { reshapedTo5D = true; dy5D = reshape$1($dy, [1, $dy.shape[0], $dy.shape[1], $dy.shape[2], $dy.shape[3]]); input5D = reshape$1($input, [ 1, $input.shape[0], $input.shape[1], $input.shape[2], $input.shape[3] ]); output5D = reshape$1($output, [ 1, $output.shape[0], $output.shape[1], $output.shape[2], $output.shape[3] ]); } assert(dy5D.rank === 5, () => `Error in maxPool3dGrad: dy must be rank 5 but got rank ` + `${dy5D.rank}.`); assert(input5D.rank === 5, () => `Error in maxPool3dGrad: input must be rank 5 but got rank ` + `${input5D.rank}.`); assert(output5D.rank === 5, () => `Error in maxPool3dGrad: output must be rank 5 but got rank ` + `${output5D.rank}.`); checkPadOnDimRoundingMode('maxPool3dGrad', pad, dimRoundingMode); const inputs = { dy: dy5D, input: input5D, output: output5D }; const attrs = { filterSize, strides, pad, dimRoundingMode }; // tslint:disable-next-line: no-unnecessary-type-assertion const res = ENGINE.runKernel(MaxPool3DGrad, inputs, attrs); if (reshapedTo5D) { return reshape$1(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]); } return res; } const maxPool3dGrad = /* @__PURE__ */ op({ maxPool3dGrad_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const maxPool3DGradConfig = { kernelName: MaxPool3D, inputsToSave: ['x'], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const [x, y] = saved; const { filterSize, strides, pad, dimRoundingMode } = attrs; return { x: () => maxPool3dGrad(dy, x, y, filterSize, strides, pad, dimRoundingMode) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Computes the backprop of a 2D max pool. * * @param dy The dy error, of rank 4 or rank 3 of shape * [batchSize, height, width, channels]. If rank 3, batch of 1 is * assumed. * @param input The original input image, of rank 4, of shape * [batchSize, height, width, channels]. * @param output The original output image, of rank 4, of shape * [batchSize, outHeight, outWidth, channels]. * @param filterSize The filter size: `[filterHeight, filterWidth]`. If * `filterSize` is a single number, then `filterHeight == filterWidth`. * @param strides The strides of the pooling: `[strideHeight, strideWidth]`. If * `strides` is a single number, then `strideHeight == strideWidth`. * @param pad The type of padding algorithm used in the forward prop of the op. * 'same', 'valid', for more info, see this guide: * [https://www.tensorflow.org/api_docs/python/tf/nn/convolution]( * https://www.tensorflow.org/api_docs/python/tf/nn/convolution) * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is * provided, it will default to truncate. */ function maxPoolGrad_(dy, input, output, filterSize, strides, pad, dimRoundingMode) { const $dy = convertToTensor(dy, 'dy', 'maxPoolGrad'); const $input = convertToTensor(input, 'input', 'maxPoolGrad'); const $output = convertToTensor(output, 'output', 'maxPoolGrad'); assert($input.rank === $dy.rank, () => `Rank of input (${$input.rank}) does not match rank of dy ` + `(${$dy.rank})`); assert($dy.rank === 4, () => `Error in maxPoolGrad: dy must be rank 4 but got rank ` + `${$dy.rank}.`); assert($input.rank === 4, () => `Error in maxPoolGrad: input must be rank 4 but got rank ` + `${$input.rank}.`); checkPadOnDimRoundingMode('maxPoolGrad', pad, dimRoundingMode); const inputs = { dy: $dy, input: $input, output: $output }; const attrs = { filterSize, strides, pad, dimRoundingMode }; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(MaxPoolGrad, inputs, attrs); } const maxPoolGrad = /* @__PURE__ */ op({ maxPoolGrad_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const maxPoolGradConfig = { kernelName: MaxPool, inputsToSave: ['x'], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const [x, y] = saved; const { filterSize, strides, pad } = attrs; return { x: () => maxPoolGrad(dy, x, y, filterSize, strides, pad) }; } }; /** * @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. * ============================================================================= */ /** * 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'} */ function zeros$1(shape, dtype = 'float32') { assertNonNegativeIntegerDimensions(shape); if (dtype === 'complex64') { const real = zeros$1(shape, 'float32'); const imag = zeros$1(shape, 'float32'); return complex(real, imag); } const values = makeZerosTypedArray(sizeFromShape(shape), dtype); return ENGINE.makeTensor(values, shape, dtype); } /** * @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. * ============================================================================= */ /** * 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'} */ function ones$1(shape, dtype = 'float32') { assertNonNegativeIntegerDimensions(shape); if (dtype === 'complex64') { const real = ones$1(shape, 'float32'); const imag = zeros$1(shape, 'float32'); return complex(real, imag); } const values = makeOnesTypedArray(sizeFromShape(shape), dtype); return ENGINE.makeTensor(values, shape, dtype); } /** * @license * Copyright 2020 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. * ============================================================================= */ const meanGradConfig = { kernelName: Mean, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { axis } = attrs; const axes = parseAxisParam(axis, x.shape); const shapes = computeOutAndReduceShapes(x.shape, axes); const reduceShape = shapes[1]; const reduceSize = sizeFromShape(reduceShape); const derX = () => { const expandedDyShape = x.shape.slice(); axes.forEach(axis => { expandedDyShape[axis] = 1; }); const expandedDy = reshape$1(dy, expandedDyShape); const res = div(mul(expandedDy, ones$1(x.shape, 'float32')), reduceSize); return res; }; return { x: derX }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const minGradConfig = { kernelName: Min, inputsToSave: ['x'], outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const minAttrs = attrs; const { axis } = minAttrs; const [x, y] = saved; const origAxes = parseAxisParam(axis, x.shape); const minGrad = gradForMinAndMax(dy, y, x, origAxes); return { x: () => { return minGrad['x'](); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const minimumGradConfig = { kernelName: Minimum$1, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const derA = () => mul(dy, cast(lessEqual(a, b), 'float32')); const derB = () => mul(dy, cast(greater$1(a, b), 'float32')); return { a: derA, b: derB }; } }; /** * @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. * ============================================================================= */ /** * 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` * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * * x.slice([1], [2]).print(); * ``` * * ```js * const x = tf.tensor2d([1, 2, 3, 4], [2, 2]); * * x.slice([1, 0], [1, 2]).print(); * ``` * @param x The input `tf.Tensor` to slice from. * @param begin The coordinates to start the slice from. The length can be * less than the rank of x - the rest of the axes will have implicit 0 as * start. Can also be a single number, in which case it specifies the * first axis. * @param size The size of the slice. The length can be less than the rank of * x - the rest of the axes will have implicit -1. A value of -1 requests * the rest of the dimensions in the axis. Can also be a single number, * in which case it specifies the size of the first axis. * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function slice_(x, begin, size) { const $x = convertToTensor(x, 'x', 'slice', 'string_or_numeric'); if ($x.rank === 0) { throw new Error('Slicing scalar is not possible'); } const inputs = { x: $x }; const attrs = { begin, size }; return ENGINE.runKernel(Slice, inputs, attrs); } const slice = /* @__PURE__ */ op({ slice_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const mirrorPadGradConfig = { kernelName: MirrorPad, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { // Pad introduces values around the original tensor, so the gradient // slices the original shape out of the gradient. const x = saved[0]; const { paddings } = attrs; const begin = paddings.map(p => p[0]); return { x: () => slice(dy, begin, x.shape) }; } }; /** * @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. * ============================================================================= */ /** * Computes floor of input `tf.Tensor` element-wise: `floor(x)`. * * ```js * const x = tf.tensor1d([.6, 1.1, -3.3]); * * x.floor().print(); // or tf.floor(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function floor_(x) { const $x = convertToTensor(x, 'x', 'floor', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Floor, inputs); } const floor = /* @__PURE__ */ op({ floor_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const modGradConfig = { kernelName: Mod, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(dy, reduceAxes), a.shape); } return dy; }; const derB = () => { const res = mul(dy, neg(floor(div(a, b)))); const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(res, reduceAxes), b.shape); } return res; }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const multiplyGradConfig = { kernelName: Multiply$1, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { const res = mul(dy, cast(b, 'float32')); const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(res, reduceAxes), a.shape); } return res; }; const derB = () => { const res = mul(dy, cast(a, 'float32')); const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(res, reduceAxes), b.shape); } return res; }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const negGradConfig = { kernelName: Neg, gradFunc: (dy) => { return { x: () => neg(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const oneHotGradConfig = { kernelName: OneHot, inputsToSave: ['indices'], gradFunc: (dy, saved) => { const indices = saved[0]; return { indices: () => zeros$1(indices.shape, 'float32') }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const onesLikeGradConfig = { kernelName: OnesLike, gradFunc: (dy) => { return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Unstacks a `tf.Tensor` of rank-`R` into a list of rank-`(R-1)` `tf.Tensor`s. * * ```js * const a = tf.tensor2d([1, 2, 3, 4], [2, 2]); * * tf.unstack(a).forEach(tensor => tensor.print()); * ``` * * @param x A tensor object. * @param axis The axis to unstack along. Defaults to 0 (the first dim). * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function unstack_(x, axis = 0) { const $x = convertToTensor(x, 'x', 'unstack', 'string_or_numeric'); assert(axis >= -$x.shape.length && axis < $x.shape.length, () => `Axis = ${axis} is not in [-${$x.shape.length}, ${$x.shape.length})`); const inputs = { value: $x }; const attrs = { axis }; return ENGINE.runKernel(Unpack, inputs, attrs); } const unstack = /* @__PURE__ */ op({ unstack_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const packGradConfig = { kernelName: Pack, saveAllInputs: true, gradFunc: (dy, saved, attrs) => { const { axis } = attrs; const derTensors = unstack(dy, axis); return derTensors.map(t => () => t); } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const padV2GradConfig = { kernelName: PadV2, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { // Pad introduces values around the original tensor, so the gradient // slices the original shape out of the gradient. const x = saved[0]; const { paddings } = attrs; const begin = paddings.map(p => p[0]); return { x: () => slice(dy, begin, x.shape) }; } }; /** * @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. * ============================================================================= */ /** * Computes natural logarithm of the input `tf.Tensor` element-wise: `ln(x)` * * ```js * const x = tf.tensor1d([1, 2, Math.E]); * * x.log().print(); // or tf.log(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function log_(x) { const $x = convertToTensor(x, 'x', 'log', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Log, inputs); } const log = /* @__PURE__ */ op({ log_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * 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. The result's dtype will be the upcasted * type of the `base` and `exp` dtypes. * * ```js * 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) * ``` * * ```js * 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 `exp` are the same shape (does not broadcast). * * @param base The base `tf.Tensor` to pow element-wise. * @param exp The exponent `tf.Tensor` to pow element-wise. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function pow_(base, exp) { let $base = convertToTensor(base, 'base', 'pow'); let $exp = convertToTensor(exp, 'exp', 'pow'); [$base, $exp] = makeTypesMatch($base, $exp); const inputs = { a: $base, b: $exp }; return ENGINE.runKernel(Pow, inputs); } const pow = /* @__PURE__ */ op({ pow_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ const powGradConfig = { kernelName: Pow, inputsToSave: ['a', 'b'], outputsToSave: [true], gradFunc: (dy, saved) => { const [a, b, y] = saved; const base = a; const exp = b; const outShape = assertAndGetBroadcastShape(base.shape, exp.shape); const derBase = () => { const expFloat = cast(exp, 'float32'); let res = mul(dy, mul(expFloat, pow(base, sub(expFloat, scalar(1))))); const reduceAxes = getReductionAxes(base.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, base.shape); }; const derExp = () => { const condition = greater$1(base, 0); const logBase = where(condition, log(base), zerosLike(base)); let res = mul(dy, mul(y, logBase)); const reduceAxes = getReductionAxes(exp.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, exp.shape); }; return { a: derBase, b: derExp }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const preluGradConfig = { kernelName: Prelu, inputsToSave: ['x', 'alpha'], gradFunc: (dy, saved) => { const [x, alpha] = saved; const mask = greater$1(x, 0); return { x: () => where(mask, dy, mul(dy, alpha)), alpha: () => { let res = where(mask, zerosLike(dy), mul(dy, x)); const reduceAxes = getReductionAxes(alpha.shape, dy.shape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, alpha.shape); } }; } }; /** * @license * Copyright 2017 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. * ============================================================================= */ function isBrowser() { return (typeof window !== 'undefined' && window.document != null) || //@ts-ignore (typeof WorkerGlobalScope !== 'undefined'); } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ const ENV = env(); /** * This file contains environment-related flag registrations. */ /** Whether to enable debug mode. */ ENV.registerFlag('DEBUG', () => false, debugValue => { if (debugValue) { console.warn('Debugging mode is ON. The output of every math call will ' + 'be downloaded to CPU and checked for NaNs. ' + 'This significantly impacts performance.'); } }); /** Whether we are in a browser (as versus, say, node.js) environment. */ ENV.registerFlag('IS_BROWSER', () => isBrowser()); /** Whether we are in a browser (as versus, say, node.js) environment. */ ENV.registerFlag('IS_NODE', () => (typeof process !== 'undefined') && (typeof process.versions !== 'undefined') && (typeof process.versions.node !== 'undefined')); /** Whether this browser is Chrome. */ ENV.registerFlag('IS_CHROME', () => typeof navigator !== 'undefined' && navigator != null && navigator.userAgent != null && /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)); /** Whether this browser is Safari. */ ENV.registerFlag('IS_SAFARI', () => typeof navigator !== 'undefined' && navigator != null && navigator.userAgent != null && /Safari/.test(navigator.userAgent) && /Apple/.test(navigator.vendor)); /** * True when the environment is "production" where we disable safety checks * to gain performance. */ ENV.registerFlag('PROD', () => false); /** * Whether to do sanity checks when inferring a shape from user-provided * values, used when creating a new tensor. */ ENV.registerFlag('TENSORLIKE_CHECK_SHAPE_CONSISTENCY', () => ENV.getBool('DEBUG')); /** Whether deprecation warnings are enabled. */ ENV.registerFlag('DEPRECATION_WARNINGS_ENABLED', () => true); /** True if running unit tests. */ ENV.registerFlag('IS_TEST', () => false); /** Whether to check computation result for errors. */ ENV.registerFlag('CHECK_COMPUTATION_FOR_ERRORS', () => ENV.getBool('DEBUG')); /** Whether the backend needs to wrap input to imageBitmap. */ ENV.registerFlag('WRAP_TO_IMAGEBITMAP', () => false); /** Whether to enable canvas2d willReadFrequently for GPU backends */ ENV.registerFlag('CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU', () => false); /** Whether to use setTimeoutCustom */ ENV.registerFlag('USE_SETTIMEOUTCUSTOM', () => false); /** * Wraps a list of ArrayBuffers into a `slice()`-able object without allocating * a large ArrayBuffer. * * Allocating large ArrayBuffers (~2GB) can be unstable on Chrome. TFJS loads * its weights as a list of (usually) 4MB ArrayBuffers and then slices the * weight tensors out of them. For small models, it's safe to concatenate all * the weight buffers into a single ArrayBuffer and then slice the weight * tensors out of it, but for large models, a different approach is needed. */ class CompositeArrayBuffer { /** * Concatenate a number of ArrayBuffers into one. * * @param buffers An array of ArrayBuffers to concatenate, or a single * ArrayBuffer. * @returns Result of concatenating `buffers` in order. */ static join(buffers) { return new CompositeArrayBuffer(buffers).slice(); } constructor(buffers) { this.shards = []; this.previousShardIndex = 0; if (buffers == null) { return; } // Normalize the `buffers` input to be `ArrayBuffer[]`. if (!(buffers instanceof Array)) { buffers = [buffers]; } buffers = buffers.map((bufferOrTypedArray) => { if (isTypedArray(bufferOrTypedArray)) { return bufferOrTypedArray.buffer; } return bufferOrTypedArray; }); // Skip setting up shards if there are no buffers. if (buffers.length === 0) { return; } this.bufferUniformSize = buffers[0].byteLength; let start = 0; for (let i = 0; i < buffers.length; i++) { const buffer = buffers[i]; // Check that all buffers except the last one have the same length. if (i !== buffers.length - 1 && buffer.byteLength !== this.bufferUniformSize) { // Unset the buffer uniform size, since the buffer sizes are not // uniform. this.bufferUniformSize = undefined; } // Create the shards, including their start and end points. const end = start + buffer.byteLength; this.shards.push({ buffer, start, end }); start = end; } // Set the byteLenghth if (this.shards.length === 0) { this.byteLength = 0; } this.byteLength = this.shards[this.shards.length - 1].end; } slice(start = 0, end = this.byteLength) { // If there are no shards, then the CompositeArrayBuffer was initialized // with no data. if (this.shards.length === 0) { return new ArrayBuffer(0); } // NaN is treated as zero for slicing. This matches ArrayBuffer's behavior. start = isNaN(Number(start)) ? 0 : start; end = isNaN(Number(end)) ? 0 : end; // Fix the bounds to within the array. start = Math.max(0, start); end = Math.min(this.byteLength, end); if (end <= start) { return new ArrayBuffer(0); } const startShardIndex = this.findShardForByte(start); if (startShardIndex === -1) { // This should not happen since the start and end indices are always // within 0 and the composite array's length. throw new Error(`Could not find start shard for byte ${start}`); } const size = end - start; const outputBuffer = new ArrayBuffer(size); const outputArray = new Uint8Array(outputBuffer); let sliced = 0; for (let i = startShardIndex; i < this.shards.length; i++) { const shard = this.shards[i]; const globalStart = start + sliced; const localStart = globalStart - shard.start; const outputStart = sliced; const globalEnd = Math.min(end, shard.end); const localEnd = globalEnd - shard.start; const outputSlice = new Uint8Array(shard.buffer, localStart, localEnd - localStart); outputArray.set(outputSlice, outputStart); sliced += outputSlice.length; if (end < shard.end) { break; } } return outputBuffer; } /** * Get the index of the shard that contains the byte at `byteIndex`. */ findShardForByte(byteIndex) { if (this.shards.length === 0 || byteIndex < 0 || byteIndex >= this.byteLength) { return -1; } // If the buffers have a uniform size, compute the shard directly. if (this.bufferUniformSize != null) { this.previousShardIndex = Math.floor(byteIndex / this.bufferUniformSize); return this.previousShardIndex; } // If the buffers don't have a uniform size, we need to search for the // shard. That means we need a function to check where the byteIndex lies // relative to a given shard. function check(shard) { if (byteIndex < shard.start) { return -1; } if (byteIndex >= shard.end) { return 1; } return 0; } // For efficiency, try the previous shard first. if (check(this.shards[this.previousShardIndex]) === 0) { return this.previousShardIndex; } // Otherwise, use a generic search function. // This should almost never end up being used in practice since the weight // entries should always be in order. const index = search(this.shards, check); if (index === -1) { return -1; } this.previousShardIndex = index; return this.previousShardIndex; } } /** * Search for an element of a sorted array. * * @param sortedArray The sorted array to search * @param compare A function to compare the current value against the searched * value. Return 0 on a match, negative if the searched value is less than * the value passed to the function, and positive if the searched value is * greater than the value passed to the function. * @returns The index of the element, or -1 if it's not in the array. */ function search(sortedArray, compare) { // Binary search let min = 0; let max = sortedArray.length; while (min <= max) { const middle = Math.floor((max - min) / 2) + min; const side = compare(sortedArray[middle]); if (side === 0) { return middle; } else if (side < 0) { max = middle; } else { min = middle + 1; } } return -1; } /** * @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. * ============================================================================= */ // Use Buffer on Node.js instead of Blob/atob/btoa const useNodeBuffer = typeof Buffer !== 'undefined' && (typeof Blob === 'undefined' || typeof atob === 'undefined' || typeof btoa === 'undefined'); /** * Calculate the byte length of a JavaScript string. * * Note that a JavaScript string can contain wide characters, therefore the * length of the string is not necessarily equal to the byte length. * * @param str Input string. * @returns Byte length. */ function stringByteLength(str) { if (useNodeBuffer) { return Buffer.byteLength(str, 'utf8'); } return new Blob([str]).size; } /** * Encode an ArrayBuffer as a base64 encoded string. * * @param buffer `ArrayBuffer` to be converted. * @returns A string that base64-encodes `buffer`. */ function arrayBufferToBase64String(buffer) { if (useNodeBuffer) { return Buffer.from(buffer).toString('base64'); } const buf = new Uint8Array(buffer); let s = ''; for (let i = 0, l = buf.length; i < l; i++) { s += String.fromCharCode(buf[i]); } return btoa(s); } /** * Decode a base64 string as an ArrayBuffer. * * @param str Base64 string. * @returns Decoded `ArrayBuffer`. */ function base64StringToArrayBuffer(str) { if (useNodeBuffer) { const buf = Buffer.from(str, 'base64'); return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); } const s = atob(str); const buffer = new Uint8Array(s.length); for (let i = 0; i < s.length; ++i) { buffer.set([s.charCodeAt(i)], i); } return buffer.buffer; } /** * Create `ModelJSON` from `ModelArtifacts`. * * @param artifacts Model artifacts, describing the model and its weights. * @param manifest Weight manifest, describing where the weights of the * `ModelArtifacts` are stored, and some metadata about them. * @returns Object representing the `model.json` file describing the model * artifacts and weights */ function getModelJSONForModelArtifacts(artifacts, manifest) { const result = { modelTopology: artifacts.modelTopology, format: artifacts.format, generatedBy: artifacts.generatedBy, convertedBy: artifacts.convertedBy, weightsManifest: manifest }; if (artifacts.signature != null) { result.signature = artifacts.signature; } if (artifacts.userDefinedMetadata != null) { result.userDefinedMetadata = artifacts.userDefinedMetadata; } if (artifacts.modelInitializer != null) { result.modelInitializer = artifacts.modelInitializer; } if (artifacts.initializerSignature != null) { result.initializerSignature = artifacts.initializerSignature; } if (artifacts.trainingConfig != null) { result.trainingConfig = artifacts.trainingConfig; } return result; } /** * Create `ModelArtifacts` from a JSON file and weights. * * @param modelJSON Object containing the parsed JSON of `model.json` * @param weightSpecs The list of WeightsManifestEntry for the model. Must be * passed if the modelJSON has a weightsManifest. * @param weightData An ArrayBuffer or array of ArrayBuffers of weight data for * the model corresponding to the weights in weightSpecs. Must be passed if * the modelJSON has a weightsManifest. * @returns A Promise of the `ModelArtifacts`, as described by the JSON file. */ function getModelArtifactsForJSONSync(modelJSON, weightSpecs, weightData) { const modelArtifacts = { modelTopology: modelJSON.modelTopology, format: modelJSON.format, generatedBy: modelJSON.generatedBy, convertedBy: modelJSON.convertedBy }; if (modelJSON.trainingConfig != null) { modelArtifacts.trainingConfig = modelJSON.trainingConfig; } if (modelJSON.weightsManifest != null) { if (!weightSpecs) { throw new Error('modelJSON has weightsManifest but weightSpecs is null'); } if (!weightData) { throw new Error('modelJSON has weightsManifest but weightData is null'); } modelArtifacts.weightSpecs = weightSpecs; modelArtifacts.weightData = weightData; } if (modelJSON.signature != null) { modelArtifacts.signature = modelJSON.signature; } if (modelJSON.userDefinedMetadata != null) { modelArtifacts.userDefinedMetadata = modelJSON.userDefinedMetadata; } if (modelJSON.modelInitializer != null) { modelArtifacts.modelInitializer = modelJSON.modelInitializer; } if (modelJSON.initializerSignature != null) { modelArtifacts.initializerSignature = modelJSON.initializerSignature; } return modelArtifacts; } /** * Create `ModelArtifacts` from a JSON file. * * @param modelJSON Object containing the parsed JSON of `model.json` * @param loadWeights Function that takes the JSON file's weights manifest, * reads weights from the listed path(s), and returns a Promise of the * weight manifest entries along with the weights data. * @returns A Promise of the `ModelArtifacts`, as described by the JSON file. */ async function getModelArtifactsForJSON(modelJSON, loadWeights) { let weightSpecs; let weightData; if (modelJSON.weightsManifest != null) { [weightSpecs, weightData] = await loadWeights(modelJSON.weightsManifest); } return getModelArtifactsForJSONSync(modelJSON, weightSpecs, weightData); } /** * Populate ModelArtifactsInfo fields for a model with JSON topology. * @param modelArtifacts * @returns A ModelArtifactsInfo object. */ function getModelArtifactsInfoForJSON(modelArtifacts) { if (modelArtifacts.modelTopology instanceof ArrayBuffer) { throw new Error('Expected JSON model topology, received ArrayBuffer.'); } return { dateSaved: new Date(), modelTopologyType: 'JSON', modelTopologyBytes: modelArtifacts.modelTopology == null ? 0 : stringByteLength(JSON.stringify(modelArtifacts.modelTopology)), weightSpecsBytes: modelArtifacts.weightSpecs == null ? 0 : stringByteLength(JSON.stringify(modelArtifacts.weightSpecs)), weightDataBytes: modelArtifacts.weightData == null ? 0 : new CompositeArrayBuffer(modelArtifacts.weightData).byteLength, }; } /** * Concatenate the weights stored in a WeightsManifestConfig into a list of * WeightsManifestEntry * * @param weightsManifest The WeightsManifestConfig to extract weights from. * @returns A list of WeightsManifestEntry of the weights in the weightsManifest */ function getWeightSpecs(weightsManifest) { const weightSpecs = []; for (const entry of weightsManifest) { weightSpecs.push(...entry.weights); } return weightSpecs; } /** * @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. * ============================================================================= */ class IORouterRegistry { constructor() { this.saveRouters = []; this.loadRouters = []; } static getInstance() { if (IORouterRegistry.instance == null) { IORouterRegistry.instance = new IORouterRegistry(); } return IORouterRegistry.instance; } /** * Register a save-handler router. * * @param saveRouter A function that maps a URL-like string onto an instance * of `IOHandler` with the `save` method defined or `null`. */ static registerSaveRouter(saveRouter) { IORouterRegistry.getInstance().saveRouters.push(saveRouter); } /** * Register a load-handler router. * * @param loadRouter A function that maps a URL-like string onto an instance * of `IOHandler` with the `load` method defined or `null`. */ static registerLoadRouter(loadRouter) { IORouterRegistry.getInstance().loadRouters.push(loadRouter); } /** * Look up IOHandler for saving, given a URL-like string. * * @param url * @returns If only one match is found, an instance of IOHandler with the * `save` method defined. If no match is found, `null`. * @throws Error, if more than one match is found. */ static getSaveHandlers(url) { return IORouterRegistry.getHandlers(url, 'save'); } /** * Look up IOHandler for loading, given a URL-like string. * * @param url * @param loadOptions Optional, custom load options. * @returns All valid handlers for `url`, given the currently registered * handler routers. */ static getLoadHandlers(url, loadOptions) { return IORouterRegistry.getHandlers(url, 'load', loadOptions); } static getHandlers(url, handlerType, loadOptions) { const validHandlers = []; const routers = handlerType === 'load' ? IORouterRegistry.getInstance().loadRouters : IORouterRegistry.getInstance().saveRouters; routers.forEach(router => { const handler = router(url, loadOptions); if (handler !== null) { validHandlers.push(handler); } }); return validHandlers; } } /** * @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. * ============================================================================= */ const DATABASE_NAME = 'tensorflowjs'; const DATABASE_VERSION = 1; // Model data and ModelArtifactsInfo (metadata) are stored in two separate // stores for efficient access of the list of stored models and their metadata. // 1. The object store for model data: topology, weights and weight manifests. const MODEL_STORE_NAME = 'models_store'; // 2. The object store for ModelArtifactsInfo, including meta-information such // as the type of topology (JSON vs binary), byte size of the topology, byte // size of the weights, etc. const INFO_STORE_NAME = 'model_info_store'; function getIndexedDBFactory() { if (!env().getBool('IS_BROWSER')) { // TODO(cais): Add more info about what IOHandler subtypes are available. // Maybe point to a doc page on the web and/or automatically determine // the available IOHandlers and print them in the error message. throw new Error('Failed to obtain IndexedDB factory because the current environment' + 'is not a web browser.'); } // tslint:disable-next-line:no-any const theWindow = typeof window === 'undefined' ? self : window; const factory = theWindow.indexedDB || theWindow.mozIndexedDB || theWindow.webkitIndexedDB || theWindow.msIndexedDB || theWindow.shimIndexedDB; if (factory == null) { throw new Error('The current browser does not appear to support IndexedDB.'); } return factory; } function setUpDatabase(openRequest) { const db = openRequest.result; db.createObjectStore(MODEL_STORE_NAME, { keyPath: 'modelPath' }); db.createObjectStore(INFO_STORE_NAME, { keyPath: 'modelPath' }); } /** * IOHandler subclass: Browser IndexedDB. * * See the doc string of `browserIndexedDB` for more details. */ class BrowserIndexedDB { constructor(modelPath) { this.indexedDB = getIndexedDBFactory(); if (modelPath == null || !modelPath) { throw new Error('For IndexedDB, modelPath must not be null, undefined or empty.'); } this.modelPath = modelPath; } async save(modelArtifacts) { // TODO(cais): Support saving GraphDef models. if (modelArtifacts.modelTopology instanceof ArrayBuffer) { throw new Error('BrowserLocalStorage.save() does not support saving model topology ' + 'in binary formats yet.'); } return this.databaseAction(this.modelPath, modelArtifacts); } async load() { return this.databaseAction(this.modelPath); } /** * Perform database action to put model artifacts into or read model artifacts * from IndexedDB object store. * * Whether the action is put or get depends on whether `modelArtifacts` is * specified. If it is specified, the action will be put; otherwise the action * will be get. * * @param modelPath A unique string path for the model. * @param modelArtifacts If specified, it will be the model artifacts to be * stored in IndexedDB. * @returns A `Promise` of `SaveResult`, if the action is put, or a `Promise` * of `ModelArtifacts`, if the action is get. */ databaseAction(modelPath, modelArtifacts) { return new Promise((resolve, reject) => { const openRequest = this.indexedDB.open(DATABASE_NAME, DATABASE_VERSION); openRequest.onupgradeneeded = () => setUpDatabase(openRequest); openRequest.onsuccess = () => { const db = openRequest.result; if (modelArtifacts == null) { // Read model out from object store. const modelTx = db.transaction(MODEL_STORE_NAME, 'readonly'); const modelStore = modelTx.objectStore(MODEL_STORE_NAME); const getRequest = modelStore.get(this.modelPath); getRequest.onsuccess = () => { if (getRequest.result == null) { db.close(); return reject(new Error(`Cannot find model with path '${this.modelPath}' ` + `in IndexedDB.`)); } else { resolve(getRequest.result.modelArtifacts); } }; getRequest.onerror = error => { db.close(); return reject(getRequest.error); }; modelTx.oncomplete = () => db.close(); } else { // Put model into object store. // Concatenate all the model weights into a single ArrayBuffer. Large // models (~1GB) have problems saving if they are not concatenated. // TODO(mattSoulanille): Save large models to multiple indexeddb // records. modelArtifacts.weightData = CompositeArrayBuffer.join(modelArtifacts.weightData); const modelArtifactsInfo = getModelArtifactsInfoForJSON(modelArtifacts); // First, put ModelArtifactsInfo into info store. const infoTx = db.transaction(INFO_STORE_NAME, 'readwrite'); let infoStore = infoTx.objectStore(INFO_STORE_NAME); let putInfoRequest; try { putInfoRequest = infoStore.put({ modelPath: this.modelPath, modelArtifactsInfo }); } catch (error) { return reject(error); } let modelTx; putInfoRequest.onsuccess = () => { // Second, put model data into model store. modelTx = db.transaction(MODEL_STORE_NAME, 'readwrite'); const modelStore = modelTx.objectStore(MODEL_STORE_NAME); let putModelRequest; try { putModelRequest = modelStore.put({ modelPath: this.modelPath, modelArtifacts, modelArtifactsInfo }); } catch (error) { // Sometimes, the serialized value is too large to store. return reject(error); } putModelRequest.onsuccess = () => resolve({ modelArtifactsInfo }); putModelRequest.onerror = error => { // If the put-model request fails, roll back the info entry as // well. infoStore = infoTx.objectStore(INFO_STORE_NAME); const deleteInfoRequest = infoStore.delete(this.modelPath); deleteInfoRequest.onsuccess = () => { db.close(); return reject(putModelRequest.error); }; deleteInfoRequest.onerror = error => { db.close(); return reject(putModelRequest.error); }; }; }; putInfoRequest.onerror = error => { db.close(); return reject(putInfoRequest.error); }; infoTx.oncomplete = () => { if (modelTx == null) { db.close(); } else { modelTx.oncomplete = () => db.close(); } }; } }; openRequest.onerror = error => reject(openRequest.error); }); } } BrowserIndexedDB.URL_SCHEME = 'indexeddb://'; const indexedDBRouter = (url) => { if (!env().getBool('IS_BROWSER')) { return null; } else { if (!Array.isArray(url) && url.startsWith(BrowserIndexedDB.URL_SCHEME)) { return browserIndexedDB(url.slice(BrowserIndexedDB.URL_SCHEME.length)); } else { return null; } } }; IORouterRegistry.registerSaveRouter(indexedDBRouter); IORouterRegistry.registerLoadRouter(indexedDBRouter); /** * Creates a browser IndexedDB IOHandler for saving and loading models. * * ```js * const model = tf.sequential(); * model.add( * tf.layers.dense({units: 1, inputShape: [100], activation: 'sigmoid'})); * * const saveResult = await model.save('indexeddb://MyModel')); * console.log(saveResult); * ``` * * @param modelPath A unique identifier for the model to be saved. Must be a * non-empty string. * @returns An instance of `BrowserIndexedDB` (sublcass of `IOHandler`), * which can be used with, e.g., `tf.Model.save`. */ function browserIndexedDB(modelPath) { return new BrowserIndexedDB(modelPath); } /** * @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. * ============================================================================= */ const PATH_SEPARATOR = '/'; const PATH_PREFIX = 'tensorflowjs_models'; const INFO_SUFFIX = 'info'; const MODEL_TOPOLOGY_SUFFIX = 'model_topology'; const WEIGHT_SPECS_SUFFIX = 'weight_specs'; const WEIGHT_DATA_SUFFIX = 'weight_data'; const MODEL_METADATA_SUFFIX = 'model_metadata'; function getModelKeys(path) { return { info: [PATH_PREFIX, path, INFO_SUFFIX].join(PATH_SEPARATOR), topology: [PATH_PREFIX, path, MODEL_TOPOLOGY_SUFFIX].join(PATH_SEPARATOR), weightSpecs: [PATH_PREFIX, path, WEIGHT_SPECS_SUFFIX].join(PATH_SEPARATOR), weightData: [PATH_PREFIX, path, WEIGHT_DATA_SUFFIX].join(PATH_SEPARATOR), modelMetadata: [PATH_PREFIX, path, MODEL_METADATA_SUFFIX].join(PATH_SEPARATOR) }; } function removeItems(keys) { for (const key of Object.values(keys)) { window.localStorage.removeItem(key); } } /** * IOHandler subclass: Browser Local Storage. * * See the doc string to `browserLocalStorage` for more details. */ class BrowserLocalStorage { constructor(modelPath) { if (!env().getBool('IS_BROWSER') || typeof window === 'undefined' || typeof window.localStorage === 'undefined') { // TODO(cais): Add more info about what IOHandler subtypes are // available. // Maybe point to a doc page on the web and/or automatically determine // the available IOHandlers and print them in the error message. throw new Error('The current environment does not support local storage.'); } this.LS = window.localStorage; if (modelPath == null || !modelPath) { throw new Error('For local storage, modelPath must not be null, undefined or empty.'); } this.modelPath = modelPath; this.keys = getModelKeys(this.modelPath); } /** * Save model artifacts to browser local storage. * * See the documentation to `browserLocalStorage` for details on the saved * artifacts. * * @param modelArtifacts The model artifacts to be stored. * @returns An instance of SaveResult. */ async save(modelArtifacts) { if (modelArtifacts.modelTopology instanceof ArrayBuffer) { throw new Error('BrowserLocalStorage.save() does not support saving model topology ' + 'in binary formats yet.'); } else { const topology = JSON.stringify(modelArtifacts.modelTopology); const weightSpecs = JSON.stringify(modelArtifacts.weightSpecs); const modelArtifactsInfo = getModelArtifactsInfoForJSON(modelArtifacts); // TODO(mattsoulanille): Support saving models over 2GB that exceed // Chrome's ArrayBuffer size limit. const weightBuffer = CompositeArrayBuffer.join(modelArtifacts.weightData); try { this.LS.setItem(this.keys.info, JSON.stringify(modelArtifactsInfo)); this.LS.setItem(this.keys.topology, topology); this.LS.setItem(this.keys.weightSpecs, weightSpecs); this.LS.setItem(this.keys.weightData, arrayBufferToBase64String(weightBuffer)); // Note that JSON.stringify doesn't write out keys that have undefined // values, so for some keys, we set undefined instead of a null-ish // value. const metadata = { format: modelArtifacts.format, generatedBy: modelArtifacts.generatedBy, convertedBy: modelArtifacts.convertedBy, signature: modelArtifacts.signature != null ? modelArtifacts.signature : undefined, userDefinedMetadata: modelArtifacts.userDefinedMetadata != null ? modelArtifacts.userDefinedMetadata : undefined, modelInitializer: modelArtifacts.modelInitializer != null ? modelArtifacts.modelInitializer : undefined, initializerSignature: modelArtifacts.initializerSignature != null ? modelArtifacts.initializerSignature : undefined, trainingConfig: modelArtifacts.trainingConfig != null ? modelArtifacts.trainingConfig : undefined }; this.LS.setItem(this.keys.modelMetadata, JSON.stringify(metadata)); return { modelArtifactsInfo }; } catch (err) { // If saving failed, clean up all items saved so far. removeItems(this.keys); throw new Error(`Failed to save model '${this.modelPath}' to local storage: ` + `size quota being exceeded is a possible cause of this failure: ` + `modelTopologyBytes=${modelArtifactsInfo.modelTopologyBytes}, ` + `weightSpecsBytes=${modelArtifactsInfo.weightSpecsBytes}, ` + `weightDataBytes=${modelArtifactsInfo.weightDataBytes}.`); } } } /** * Load a model from local storage. * * See the documentation to `browserLocalStorage` for details on the saved * artifacts. * * @returns The loaded model (if loading succeeds). */ async load() { const info = JSON.parse(this.LS.getItem(this.keys.info)); if (info == null) { throw new Error(`In local storage, there is no model with name '${this.modelPath}'`); } if (info.modelTopologyType !== 'JSON') { throw new Error('BrowserLocalStorage does not support loading non-JSON model ' + 'topology yet.'); } const out = {}; // Load topology. const topology = JSON.parse(this.LS.getItem(this.keys.topology)); if (topology == null) { throw new Error(`In local storage, the topology of model '${this.modelPath}' ` + `is missing.`); } out.modelTopology = topology; // Load weight specs. const weightSpecs = JSON.parse(this.LS.getItem(this.keys.weightSpecs)); if (weightSpecs == null) { throw new Error(`In local storage, the weight specs of model '${this.modelPath}' ` + `are missing.`); } out.weightSpecs = weightSpecs; // Load meta-data fields. const metadataString = this.LS.getItem(this.keys.modelMetadata); if (metadataString != null) { const metadata = JSON.parse(metadataString); out.format = metadata.format; out.generatedBy = metadata.generatedBy; out.convertedBy = metadata.convertedBy; if (metadata.signature != null) { out.signature = metadata.signature; } if (metadata.userDefinedMetadata != null) { out.userDefinedMetadata = metadata.userDefinedMetadata; } if (metadata.modelInitializer != null) { out.modelInitializer = metadata.modelInitializer; } if (metadata.initializerSignature != null) { out.initializerSignature = metadata.initializerSignature; } if (metadata.trainingConfig != null) { out.trainingConfig = metadata.trainingConfig; } } // Load weight data. const weightDataBase64 = this.LS.getItem(this.keys.weightData); if (weightDataBase64 == null) { throw new Error(`In local storage, the binary weight values of model ` + `'${this.modelPath}' are missing.`); } out.weightData = base64StringToArrayBuffer(weightDataBase64); return out; } } BrowserLocalStorage.URL_SCHEME = 'localstorage://'; const localStorageRouter = (url) => { if (!env().getBool('IS_BROWSER')) { return null; } else { if (!Array.isArray(url) && url.startsWith(BrowserLocalStorage.URL_SCHEME)) { return browserLocalStorage(url.slice(BrowserLocalStorage.URL_SCHEME.length)); } else { return null; } } }; IORouterRegistry.registerSaveRouter(localStorageRouter); IORouterRegistry.registerLoadRouter(localStorageRouter); /** * Factory function for local storage IOHandler. * * This `IOHandler` supports both `save` and `load`. * * For each model's saved artifacts, four items are saved to local storage. * - `${PATH_SEPARATOR}/${modelPath}/info`: Contains meta-info about the * model, such as date saved, type of the topology, size in bytes, etc. * - `${PATH_SEPARATOR}/${modelPath}/topology`: Model topology. For Keras- * style models, this is a stringized JSON. * - `${PATH_SEPARATOR}/${modelPath}/weight_specs`: Weight specs of the * model, can be used to decode the saved binary weight values (see * item below). * - `${PATH_SEPARATOR}/${modelPath}/weight_data`: Concatenated binary * weight values, stored as a base64-encoded string. * * Saving may throw an `Error` if the total size of the artifacts exceed the * browser-specific quota. * * @param modelPath A unique identifier for the model to be saved. Must be a * non-empty string. * @returns An instance of `IOHandler`, which can be used with, e.g., * `tf.Model.save`. */ function browserLocalStorage(modelPath) { return new BrowserLocalStorage(modelPath); } /** * @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. * ============================================================================= */ const DEFAULT_FILE_NAME_PREFIX = 'model'; const DEFAULT_JSON_EXTENSION_NAME = '.json'; const DEFAULT_WEIGHT_DATA_EXTENSION_NAME = '.weights.bin'; function defer(f) { return new Promise(resolve => setTimeout(resolve)).then(f); } class BrowserDownloads { constructor(fileNamePrefix) { if (!env().getBool('IS_BROWSER')) { // TODO(cais): Provide info on what IOHandlers are available under the // current environment. throw new Error('browserDownloads() cannot proceed because the current environment ' + 'is not a browser.'); } if (fileNamePrefix.startsWith(BrowserDownloads.URL_SCHEME)) { fileNamePrefix = fileNamePrefix.slice(BrowserDownloads.URL_SCHEME.length); } if (fileNamePrefix == null || fileNamePrefix.length === 0) { fileNamePrefix = DEFAULT_FILE_NAME_PREFIX; } this.modelJsonFileName = fileNamePrefix + DEFAULT_JSON_EXTENSION_NAME; this.weightDataFileName = fileNamePrefix + DEFAULT_WEIGHT_DATA_EXTENSION_NAME; } async save(modelArtifacts) { if (typeof (document) === 'undefined') { throw new Error('Browser downloads are not supported in ' + 'this environment since `document` is not present'); } // TODO(mattsoulanille): Support saving models over 2GB that exceed // Chrome's ArrayBuffer size limit. const weightBuffer = CompositeArrayBuffer.join(modelArtifacts.weightData); const weightsURL = window.URL.createObjectURL(new Blob([weightBuffer], { type: 'application/octet-stream' })); if (modelArtifacts.modelTopology instanceof ArrayBuffer) { throw new Error('BrowserDownloads.save() does not support saving model topology ' + 'in binary formats yet.'); } else { const weightsManifest = [{ paths: ['./' + this.weightDataFileName], weights: modelArtifacts.weightSpecs }]; const modelJSON = getModelJSONForModelArtifacts(modelArtifacts, weightsManifest); const modelJsonURL = window.URL.createObjectURL(new Blob([JSON.stringify(modelJSON)], { type: 'application/json' })); // If anchor elements are not provided, create them without attaching them // to parents, so that the downloaded file names can be controlled. const jsonAnchor = this.modelJsonAnchor == null ? document.createElement('a') : this.modelJsonAnchor; jsonAnchor.download = this.modelJsonFileName; jsonAnchor.href = modelJsonURL; // Trigger downloads by evoking a click event on the download anchors. // When multiple downloads are started synchronously, Firefox will only // save the last one. await defer(() => jsonAnchor.dispatchEvent(new MouseEvent('click'))); if (modelArtifacts.weightData != null) { const weightDataAnchor = this.weightDataAnchor == null ? document.createElement('a') : this.weightDataAnchor; weightDataAnchor.download = this.weightDataFileName; weightDataAnchor.href = weightsURL; await defer(() => weightDataAnchor.dispatchEvent(new MouseEvent('click'))); } return { modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts) }; } } } BrowserDownloads.URL_SCHEME = 'downloads://'; const browserDownloadsRouter = (url) => { if (!env().getBool('IS_BROWSER')) { return null; } else { if (!Array.isArray(url) && url.startsWith(BrowserDownloads.URL_SCHEME)) { return browserDownloads(url.slice(BrowserDownloads.URL_SCHEME.length)); } else { return null; } } }; IORouterRegistry.registerSaveRouter(browserDownloadsRouter); /** * Creates an IOHandler that triggers file downloads from the browser. * * The returned `IOHandler` instance can be used as model exporting methods such * as `tf.Model.save` and supports only saving. * * ```js * const model = tf.sequential(); * model.add(tf.layers.dense( * {units: 1, inputShape: [10], activation: 'sigmoid'})); * const saveResult = await model.save('downloads://mymodel'); * // This will trigger downloading of two files: * // 'mymodel.json' and 'mymodel.weights.bin'. * console.log(saveResult); * ``` * * @param fileNamePrefix Prefix name of the files to be downloaded. For use with * `tf.Model`, `fileNamePrefix` should follow either of the following two * formats: * 1. `null` or `undefined`, in which case the default file * names will be used: * - 'model.json' for the JSON file containing the model topology and * weights manifest. * - 'model.weights.bin' for the binary file containing the binary weight * values. * 2. A single string or an Array of a single string, as the file name prefix. * For example, if `'foo'` is provided, the downloaded JSON * file and binary weights file will be named 'foo.json' and * 'foo.weights.bin', respectively. * @param config Additional configuration for triggering downloads. * @returns An instance of `BrowserDownloads` `IOHandler`. * * @doc { * heading: 'Models', * subheading: 'Loading', * namespace: 'io', * ignoreCI: true * } */ function browserDownloads(fileNamePrefix = 'model') { return new BrowserDownloads(fileNamePrefix); } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ /** * Monitor Promise.all progress, fire onProgress callback function. * * @param promises Promise list going to be monitored * @param onProgress Callback function. Fired when a promise resolved. * @param startFraction Optional fraction start. Default to 0. * @param endFraction Optional fraction end. Default to 1. */ function monitorPromisesProgress(promises, onProgress, startFraction, endFraction) { checkPromises(promises); startFraction = startFraction == null ? 0 : startFraction; endFraction = endFraction == null ? 1 : endFraction; checkFraction(startFraction, endFraction); let resolvedPromise = 0; const registerMonitor = (promise) => { promise.then(value => { const fraction = startFraction + ++resolvedPromise / promises.length * (endFraction - startFraction); // pass fraction as parameter to callback function. onProgress(fraction); return value; }); return promise; }; function checkPromises(promises) { assert(promises != null && Array.isArray(promises) && promises.length > 0, () => 'promises must be a none empty array'); } function checkFraction(startFraction, endFraction) { assert(startFraction >= 0 && startFraction <= 1, () => `Progress fraction must be in range [0, 1], but ` + `got startFraction ${startFraction}`); assert(endFraction >= 0 && endFraction <= 1, () => `Progress fraction must be in range [0, 1], but ` + `got endFraction ${endFraction}`); assert(endFraction >= startFraction, () => `startFraction must be no more than endFraction, but ` + `got startFraction ${startFraction} and endFraction ` + `${endFraction}`); } return Promise.all(promises.map(registerMonitor)); } /** * @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. * ============================================================================= */ /** * Reads binary weights data from a number of URLs. * * @param fetchURLs URLs to send the HTTP requests at, using `fetch` calls. * @param requestOptions RequestInit (options) for the HTTP requests. * @param fetchFunc Optional overriding value for the `window.fetch` function. * @param onProgress Optional, progress callback function, fired periodically * before the load is completed. * @returns A `Promise` of an Array of `ArrayBuffer`. The Array has the same * length as `fetchURLs`. */ async function loadWeightsAsArrayBuffer(fetchURLs, loadOptions) { if (loadOptions == null) { loadOptions = {}; } const fetchFunc = loadOptions.fetchFunc == null ? env().platform.fetch : loadOptions.fetchFunc; // Create the requests for all of the weights in parallel. const requests = fetchURLs.map(fetchURL => fetchFunc(fetchURL, loadOptions.requestInit, { isBinary: true })); const fetchStartFraction = 0; const fetchEndFraction = 0.5; const responses = loadOptions.onProgress == null ? await Promise.all(requests) : await monitorPromisesProgress(requests, loadOptions.onProgress, fetchStartFraction, fetchEndFraction); const bufferPromises = responses.map(response => response.arrayBuffer()); const bufferStartFraction = 0.5; const bufferEndFraction = 1; const buffers = loadOptions.onProgress == null ? await Promise.all(bufferPromises) : await monitorPromisesProgress(bufferPromises, loadOptions.onProgress, bufferStartFraction, bufferEndFraction); return buffers; } function streamWeights(fetchURLs, loadOptions) { var _a; const fetchFunc = loadOptions.fetchFunc == null ? env().platform.fetch : loadOptions.fetchFunc; let fetchIndex = 0; let chunkReader; (_a = loadOptions.onProgress) === null || _a === void 0 ? void 0 : _a.call(loadOptions, 0); return new ReadableStream({ pull: async (controller) => { var _a; while (fetchIndex < fetchURLs.length) { if (!chunkReader) { const body = (await fetchFunc(fetchURLs[fetchIndex], loadOptions.requestInit, { isBinary: true })).body; chunkReader = body.getReader(); } const { done, value } = await chunkReader.read(); if (done) { fetchIndex++; chunkReader = undefined; (_a = loadOptions.onProgress) === null || _a === void 0 ? void 0 : _a.call(loadOptions, fetchIndex / fetchURLs.length); continue; } controller.enqueue(value); return; } controller.close(); }, }); } /** * @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. * ============================================================================= */ const OCTET_STREAM_MIME_TYPE = 'application/octet-stream'; const JSON_TYPE = 'application/json'; class HTTPRequest { constructor(path, loadOptions) { this.DEFAULT_METHOD = 'POST'; if (loadOptions == null) { loadOptions = {}; } this.weightPathPrefix = loadOptions.weightPathPrefix; this.weightUrlConverter = loadOptions.weightUrlConverter; if (loadOptions.fetchFunc != null) { assert(typeof loadOptions.fetchFunc === 'function', () => 'Must pass a function that matches the signature of ' + '`fetch` (see ' + 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)'); this.fetch = loadOptions.fetchFunc; } else { this.fetch = env().platform.fetch; } assert(path != null && path.length > 0, () => 'URL path for http must not be null, undefined or ' + 'empty.'); if (Array.isArray(path)) { assert(path.length === 2, () => 'URL paths for http must have a length of 2, ' + `(actual length is ${path.length}).`); } this.path = path; if (loadOptions.requestInit != null && loadOptions.requestInit.body != null) { throw new Error('requestInit is expected to have no pre-existing body, but has one.'); } this.requestInit = loadOptions.requestInit || {}; this.loadOptions = loadOptions; } async save(modelArtifacts) { if (modelArtifacts.modelTopology instanceof ArrayBuffer) { throw new Error('BrowserHTTPRequest.save() does not support saving model topology ' + 'in binary formats yet.'); } const init = Object.assign({ method: this.DEFAULT_METHOD }, this.requestInit); init.body = new FormData(); const weightsManifest = [{ paths: ['./model.weights.bin'], weights: modelArtifacts.weightSpecs, }]; const modelTopologyAndWeightManifest = getModelJSONForModelArtifacts(modelArtifacts, weightsManifest); init.body.append('model.json', new Blob([JSON.stringify(modelTopologyAndWeightManifest)], { type: JSON_TYPE }), 'model.json'); if (modelArtifacts.weightData != null) { // TODO(mattsoulanille): Support saving models over 2GB that exceed // Chrome's ArrayBuffer size limit. const weightBuffer = CompositeArrayBuffer.join(modelArtifacts.weightData); init.body.append('model.weights.bin', new Blob([weightBuffer], { type: OCTET_STREAM_MIME_TYPE }), 'model.weights.bin'); } const response = await this.fetch(this.path, init); if (response.ok) { return { modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts), responses: [response], }; } else { throw new Error(`BrowserHTTPRequest.save() failed due to HTTP response status ` + `${response.status}.`); } } async loadModelJSON() { const modelConfigRequest = await this.fetch(this.path, this.requestInit); if (!modelConfigRequest.ok) { throw new Error(`Request to ${this.path} failed with status code ` + `${modelConfigRequest.status}. Please verify this URL points to ` + `the model JSON of the model to load.`); } let modelJSON; try { modelJSON = await modelConfigRequest.json(); } catch (e) { let message = `Failed to parse model JSON of response from ${this.path}.`; // TODO(nsthorat): Remove this after some time when we're comfortable that // .pb files are mostly gone. if (this.path.endsWith('.pb')) { message += ' Your path contains a .pb file extension. ' + 'Support for .pb models have been removed in TensorFlow.js 1.0 ' + 'in favor of .json models. You can re-convert your Python ' + 'TensorFlow model using the TensorFlow.js 1.0 conversion scripts ' + 'or you can convert your.pb models with the \'pb2json\'' + 'NPM script in the tensorflow/tfjs-converter repository.'; } else { message += ' Please make sure the server is serving valid ' + 'JSON for this request.'; } throw new Error(message); } // We do not allow both modelTopology and weightsManifest to be missing. const modelTopology = modelJSON.modelTopology; const weightsManifest = modelJSON.weightsManifest; if (modelTopology == null && weightsManifest == null) { throw new Error(`The JSON from HTTP path ${this.path} contains neither model ` + `topology or manifest for weights.`); } return modelJSON; } /** * Load model artifacts via HTTP request(s). * * See the documentation to `tf.io.http` for details on the saved * artifacts. * * @returns The loaded model artifacts (if loading succeeds). */ async load() { if (this.loadOptions.streamWeights) { return this.loadStream(); } const modelJSON = await this.loadModelJSON(); return getModelArtifactsForJSON(modelJSON, (weightsManifest) => this.loadWeights(weightsManifest)); } async loadStream() { const modelJSON = await this.loadModelJSON(); const fetchURLs = await this.getWeightUrls(modelJSON.weightsManifest); const weightSpecs = getWeightSpecs(modelJSON.weightsManifest); const stream = () => streamWeights(fetchURLs, this.loadOptions); return Object.assign(Object.assign({}, modelJSON), { weightSpecs, getWeightStream: stream }); } async getWeightUrls(weightsManifest) { const weightPath = Array.isArray(this.path) ? this.path[1] : this.path; const [prefix, suffix] = parseUrl(weightPath); const pathPrefix = this.weightPathPrefix || prefix; const fetchURLs = []; const urlPromises = []; for (const weightsGroup of weightsManifest) { for (const path of weightsGroup.paths) { if (this.weightUrlConverter != null) { urlPromises.push(this.weightUrlConverter(path)); } else { fetchURLs.push(pathPrefix + path + suffix); } } } if (this.weightUrlConverter) { fetchURLs.push(...await Promise.all(urlPromises)); } return fetchURLs; } async loadWeights(weightsManifest) { const fetchURLs = await this.getWeightUrls(weightsManifest); const weightSpecs = getWeightSpecs(weightsManifest); const buffers = await loadWeightsAsArrayBuffer(fetchURLs, this.loadOptions); return [weightSpecs, buffers]; } } HTTPRequest.URL_SCHEME_REGEX = /^https?:\/\//; /** * Extract the prefix and suffix of the url, where the prefix is the path before * the last file, and suffix is the search params after the last file. * ``` * const url = 'http://tfhub.dev/model/1/tensorflowjs_model.pb?tfjs-format=file' * [prefix, suffix] = parseUrl(url) * // prefix = 'http://tfhub.dev/model/1/' * // suffix = '?tfjs-format=file' * ``` * @param url the model url to be parsed. */ function parseUrl(url) { const lastSlash = url.lastIndexOf('/'); const lastSearchParam = url.lastIndexOf('?'); const prefix = url.substring(0, lastSlash); const suffix = lastSearchParam > lastSlash ? url.substring(lastSearchParam) : ''; return [prefix + '/', suffix]; } function isHTTPScheme(url) { return url.match(HTTPRequest.URL_SCHEME_REGEX) != null; } const httpRouter = (url, loadOptions) => { if (typeof fetch === 'undefined' && (loadOptions == null || loadOptions.fetchFunc == null)) { // `http` uses `fetch` or `node-fetch`, if one wants to use it in // an environment that is not the browser or node they have to setup a // global fetch polyfill. return null; } else { let isHTTP = true; if (Array.isArray(url)) { isHTTP = url.every(urlItem => isHTTPScheme(urlItem)); } else { isHTTP = isHTTPScheme(url); } if (isHTTP) { return http(url, loadOptions); } } return null; }; IORouterRegistry.registerSaveRouter(httpRouter); IORouterRegistry.registerLoadRouter(httpRouter); /** * Creates an IOHandler subtype that sends model artifacts to HTTP server. * * An HTTP request of the `multipart/form-data` mime type will be sent to the * `path` URL. The form data includes artifacts that represent the topology * and/or weights of the model. In the case of Keras-style `tf.Model`, two * blobs (files) exist in form-data: * - A JSON file consisting of `modelTopology` and `weightsManifest`. * - A binary weights file consisting of the concatenated weight values. * These files are in the same format as the one generated by * [tfjs_converter](https://js.tensorflow.org/tutorials/import-keras.html). * * The following code snippet exemplifies the client-side code that uses this * function: * * ```js * const model = tf.sequential(); * model.add( * tf.layers.dense({units: 1, inputShape: [100], activation: 'sigmoid'})); * * const saveResult = await model.save(tf.io.http( * 'http://model-server:5000/upload', {requestInit: {method: 'PUT'}})); * console.log(saveResult); * ``` * * If the default `POST` method is to be used, without any custom parameters * such as headers, you can simply pass an HTTP or HTTPS URL to `model.save`: * * ```js * const saveResult = await model.save('http://model-server:5000/upload'); * ``` * * The following GitHub Gist * https://gist.github.com/dsmilkov/1b6046fd6132d7408d5257b0976f7864 * implements a server based on [flask](https://github.com/pallets/flask) that * can receive the request. Upon receiving the model artifacts via the requst, * this particular server reconstitutes instances of [Keras * Models](https://keras.io/models/model/) in memory. * * * @param path A URL path to the model. * Can be an absolute HTTP path (e.g., * 'http://localhost:8000/model-upload)') or a relative path (e.g., * './model-upload'). * @param requestInit Request configurations to be used when sending * HTTP request to server using `fetch`. It can contain fields such as * `method`, `credentials`, `headers`, `mode`, etc. See * https://developer.mozilla.org/en-US/docs/Web/API/Request/Request * for more information. `requestInit` must not have a body, because the * body will be set by TensorFlow.js. File blobs representing the model * topology (filename: 'model.json') and the weights of the model (filename: * 'model.weights.bin') will be appended to the body. If `requestInit` has a * `body`, an Error will be thrown. * @param loadOptions Optional configuration for the loading. It includes the * following fields: * - weightPathPrefix Optional, this specifies the path prefix for weight * files, by default this is calculated from the path param. * - fetchFunc Optional, custom `fetch` function. E.g., in Node.js, * the `fetch` from node-fetch can be used here. * - onProgress Optional, progress callback function, fired periodically * before the load is completed. * @returns An instance of `IOHandler`. * * @doc { * heading: 'Models', * subheading: 'Loading', * namespace: 'io', * ignoreCI: true * } */ function http(path, loadOptions) { return new HTTPRequest(path, loadOptions); } /** * Check whether updates.shape = indices.shape[:batchDim] + * shape[sliceDim:] * * @param x The input tensor. */ function validateUpdateShape(shape, indices, updates) { const sliceDim = (indices.rank > 1) ? indices.shape[indices.rank - 1] : 1; const batchDim = (indices.rank > 1) ? indices.rank - 1 : 1; const shapeError = 'Must have updates.shape = indices.shape[:batchDim] + ' + `shape[sliceDim:], got updates.shape: ${updates.shape}` + `, indices.shape: ${indices.shape}, shape: ${shape}` + `, sliceDim: ${sliceDim}, and batchDim: ${batchDim}.`; if (updates.rank < batchDim) { throw new Error(shapeError + ` update.rank < ${batchDim}. `); } if (shape.length < sliceDim + (updates.rank - batchDim)) { throw new Error(shapeError + ` Output shape length < ${sliceDim + (updates.rank - batchDim)}`); } if (updates.rank !== batchDim + shape.length - sliceDim) { throw new Error(shapeError + ` update.rank != ${batchDim + shape.length - sliceDim}`); } for (let d = 0; d < batchDim; ++d) { if (updates.shape[d] !== indices.shape[d]) { throw new Error(shapeError + ` updates.shape[${d}] (${updates.shape[d]}) != indices.shape[${d}] (${indices.shape[d]}).`); } } for (let d = 0; d < updates.rank - batchDim; ++d) { if (updates.shape[d + batchDim] !== shape[d + sliceDim]) { throw new Error(shapeError + ` updates.shape[${d + batchDim}] (${updates.shape[d + batchDim]}) != shape[${d + batchDim}] (${shape[d + batchDim]})`); } } } /** * Validate scatter nd inputs. * * @param update The tensor contains the update values. * @param indices The tensor contains the indices for the update values. * @param shape The shape of the output tensor. */ function validateInput(updates, indices, shape) { if (indices.rank < 1) { throw new Error('tf.scatterND() expects the indices to be rank 1 or higher,' + ` but the rank was ${indices.rank}.`); } if (updates.rank < 1) { throw new Error('tf.scatterND() expects the updates to be rank 1 or higher,' + ` but the rank was ${updates.rank}.`); } if (indices.dtype !== 'int32') { throw new Error(`The dtype of 'indices' should be int32, but got dtype: ${indices.dtype}`); } if (shape.length < 1) { throw new Error(`Output rank must be greater or equal to 1, but got shape: ${shape}`); } if (shape.length === 0) { if (indices.size === 0) { throw new Error(`Indices specified for empty output. indices shape: ${indices.shape}`); } if (updates.size === 0) { throw new Error(`Updates specified for empty output. updates shape: ${updates.shape}`); } } validateUpdateShape(shape, indices, updates); } /** * @license * Copyright 2021 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. * ============================================================================= */ function parseSliceParams(x, begin, size) { // The following logic allows for more ergonomic calls. let begin_; const xRank = x.shape.length; if (typeof begin === 'number') { begin_ = [begin, ...new Array(xRank - 1).fill(0)]; } else if (begin.length < xRank) { begin_ = begin.concat(new Array(xRank - begin.length).fill(0)); } else { begin_ = begin.slice(); } begin_.forEach(d => { assert(d !== -1, () => 'slice() does not support negative begin indexing.'); }); let size_; if (size == null) { size_ = new Array(xRank).fill(-1); } else if (typeof size === 'number') { size_ = [size, ...new Array(xRank - 1).fill(-1)]; } else if (size.length < xRank) { size_ = size.concat(new Array(xRank - size.length).fill(-1)); } else { size_ = size; } size_ = size_.map((d, i) => { if (d >= 0) { return d; } else { assert(d === -1, () => `Negative size values should be exactly -1 but got ` + `${d} for the slice() size at index ${i}.`); return x.shape[i] - begin_[i]; } }); return [begin_, size_]; } /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Concatenates a list of `tf.Tensor`s along a given axis. * * The tensors ranks and types must match, and their sizes must match in all * dimensions except `axis`. * * Also available are stricter rank-specific methods that assert that * `tensors` are of the given rank: * - `tf.concat1d` * - `tf.concat2d` * - `tf.concat3d` * - `tf.concat4d` * * Except `tf.concat1d` (which does not have axis param), all methods have * same signature as this method. * * ```js * const a = tf.tensor1d([1, 2]); * const b = tf.tensor1d([3, 4]); * a.concat(b).print(); // or a.concat(b) * ``` * * ```js * const a = tf.tensor1d([1, 2]); * const b = tf.tensor1d([3, 4]); * const c = tf.tensor1d([5, 6]); * tf.concat([a, b, c]).print(); * ``` * * ```js * 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(); * ``` * @param tensors A list of tensors to concatenate. * @param axis The axis to concatenate along. Defaults to 0 (the first dim). * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function concat_(tensors, axis = 0) { assert(tensors.length >= 1, () => 'Pass at least one tensor to concat'); const $tensors = convertToTensorArray(tensors, 'tensors', 'concat', 'string_or_numeric'); if ($tensors[0].dtype === 'complex64') { $tensors.forEach(tensor => { if (tensor.dtype !== 'complex64') { throw new Error(`Cannot concatenate complex64 tensors with a tensor with dtype ${tensor.dtype}. `); } }); } if ($tensors.length === 1) { return clone($tensors[0]); } const inputs = $tensors; const attr = { axis }; return ENGINE.runKernel(Concat, inputs, attr); } const concat = /* @__PURE__ */ op({ concat_ }); /** * @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. * ============================================================================= */ /** * Computes sigmoid element-wise, `1 / (1 + exp(-x))` * * ```js * const x = tf.tensor1d([0, -1, 2, -3]); * * x.sigmoid().print(); // or tf.sigmoid(x) * ``` * @param x The input tensor. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function sigmoid_(x) { const $x = convertToTensor(x, 'x', 'sigmoid', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Sigmoid$1, inputs); } const sigmoid = /* @__PURE__ */ op({ sigmoid_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * This operation reshapes the "batch" dimension 0 into `M + 1` dimensions of * shape `blockShape + [batch]`, interleaves these blocks back into the grid * defined by the spatial dimensions `[1, ..., M]`, to obtain a result with * the same rank as the input. The spatial dimensions of this intermediate * result are then optionally cropped according to `crops` to produce the * output. This is the reverse of `tf.spaceToBatchND`. See below for a precise * description. * * ```js * const x = tf.tensor4d([1, 2, 3, 4], [4, 1, 1, 1]); * const blockShape = [2, 2]; * const crops = [[0, 0], [0, 0]]; * * x.batchToSpaceND(blockShape, crops).print(); * ``` * * @param x A `tf.Tensor`. N-D with `x.shape` = `[batch] + spatialShape + * remainingShape`, where spatialShape has `M` dimensions. * @param blockShape A 1-D array. Must have shape `[M]`, all values must * be >= 1. * @param crops A 2-D array. Must have shape `[M, 2]`, all values must be >= 0. * `crops[i] = [cropStart, cropEnd]` specifies the amount to crop from input * dimension `i + 1`, which corresponds to spatial dimension `i`. It is required * that `cropStart[i] + cropEnd[i] <= blockShape[i] * inputShape[i + 1]` * * This operation is equivalent to the following steps: * * 1. Reshape `x` to `reshaped` of shape: `[blockShape[0], ..., * blockShape[M-1], batch / prod(blockShape), x.shape[1], ..., * x.shape[N-1]]` * * 2. Permute dimensions of `reshaped` to produce `permuted` of shape `[batch / * prod(blockShape),x.shape[1], blockShape[0], ..., x.shape[M], * blockShape[M-1],x.shape[M+1], ..., x.shape[N-1]]` * * 3. Reshape `permuted` to produce `reshapedPermuted` of shape `[batch / * prod(blockShape),x.shape[1] * blockShape[0], ..., x.shape[M] * * blockShape[M-1],x.shape[M+1], ..., x.shape[N-1]]` * * 4. Crop the start and end of dimensions `[1, ..., M]` of `reshapedPermuted` * according to `crops` to produce the output of shape: `[batch / * prod(blockShape),x.shape[1] * blockShape[0] - crops[0,0] - crops[0,1], * ..., x.shape[M] * blockShape[M-1] - crops[M-1,0] - * crops[M-1,1],x.shape[M+1], ..., x.shape[N-1]]` * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function batchToSpaceND_(x, blockShape, crops) { const $x = convertToTensor(x, 'x', 'batchToSpaceND'); const prod = blockShape.reduce((a, b) => a * b); assert($x.rank >= 1 + blockShape.length, () => `input rank is ${$x.rank} but should be > than blockShape.length ${blockShape.length}`); assert(crops.length === blockShape.length, () => `crops.length is ${crops.length} but should be equal to blockShape.length ${blockShape.length}`); assert($x.shape[0] % prod === 0, () => `input tensor batch is ${$x.shape[0]} but is not divisible by the product of ` + `the elements of blockShape ${blockShape.join(' * ')} === ${prod}`); const inputs = { x: $x }; const attrs = { blockShape, crops }; return ENGINE.runKernel(BatchToSpaceND, inputs, attrs); } const batchToSpaceND = /* @__PURE__ */ op({ batchToSpaceND_ }); /** * @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. * ============================================================================= */ /** * Computes cos of the input `tf.Tensor` element-wise: `cos(x)` * * ```js * const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]); * * x.cos().print(); // or tf.cos(x) * ``` * @param x The input tensor. Must be float32 type. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function cos_(x) { const $x = convertToTensor(x, 'x', 'cos', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Cos, inputs); } const cos = /* @__PURE__ */ op({ cos_ }); /** * @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. * ============================================================================= */ /** * Computes hyperbolic cos of the input `tf.Tensor` element-wise: `cosh(x)` * * ```js * const x = tf.tensor1d([0, 1, -1, .7]); * * x.cosh().print(); // or tf.cosh(x) * ``` * @param x The input tensor. Must be float32 type. * * @doc {heading: 'Operations', subheading: 'Basic math'} */ function cosh_(x) { const $x = convertToTensor(x, 'x', 'cosh', 'float32'); const inputs = { x: $x }; return ENGINE.runKernel(Cosh, inputs); } const cosh = /* @__PURE__ */ op({ cosh_ }); /** * @license * Copyright 2022 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. * ============================================================================= */ /** * Computes the cumulative product of a `tf.Tensor` along `axis`. * * ```js * const x = tf.tensor([1, 2, 3, 4]); * x.cumprod().print(); * ``` * ```js * const x = tf.tensor([[1, 2], [3, 4]]); * x.cumprod().print(); * ``` * * @param x The input tensor to cumulatively multiply. * @param axis The axis along which to multiply. Optional. Defaults to 0. * @param exclusive Whether to perform exclusive cumulative product. Optional. * Defaults to false. If set to true then the product of each tensor entry * does not include its own value, but only the values previous to it * along the specified axis. * @param reverse Whether to multiply in the opposite direction. Optional. * Defaults to false. * * @doc {heading: 'Operations', subheading: 'Scan'} */ function cumprod_(x, axis = 0, exclusive = false, reverse = false) { const $x = convertToTensor(x, 'x', 'cumprod'); const inputs = { x: $x }; const attrs = { axis, exclusive, reverse }; return ENGINE.runKernel(Cumprod, inputs, attrs); } const cumprod = /* @__PURE__ */ op({ cumprod_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns a `tf.Tensor` that has expanded rank, by inserting a dimension * into the tensor's shape. * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * const axis = 1; * x.expandDims(axis).print(); * ``` * * @param x The input tensor whose dimensions are to be expanded. * @param axis The dimension index at which to insert shape of `1`. Defaults * to 0 (the first dimension). * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function expandDims_(x, axis = 0) { const $x = convertToTensor(x, 'x', 'expandDims', 'string_or_numeric'); assert(axis <= $x.rank, () => 'Axis must be <= rank of the tensor'); const inputs = { input: $x }; const attrs = { dim: axis }; return ENGINE.runKernel(ExpandDims, inputs, attrs); } const expandDims = /* @__PURE__ */ op({ expandDims_ }); /** * @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. * ============================================================================= */ /** * Gather slices from tensor `x`'s axis `axis` according to `indices`. * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * const indices = tf.tensor1d([1, 3, 3], 'int32'); * * x.gather(indices).print(); * ``` * * ```js * const x = tf.tensor2d([1, 2, 3, 4], [2, 2]); * const indices = tf.tensor1d([1, 1, 0], 'int32'); * * x.gather(indices).print(); * ``` * @param x The input tensor whose slices are to be gathered. * @param indices The indices of the values to extract. * @param axis The axis over which to select values. Defaults to 0. * @param batchDims Optional. The number of batch dimensions. It must be less * than or equal to rank(indices). Defaults to 0. * The output tensor will have shape of * `x.shape[:axis] + indices.shape[batchDims:] + x.shape[axis + 1:]` * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function gather_(x, indices, axis = 0, batchDims = 0) { const $x = convertToTensor(x, 'x', 'gather'); const $indices = convertToTensor(indices, 'indices', 'gather', 'int32'); const inputs = { x: $x, indices: $indices }; const attrs = { axis, batchDims }; return ENGINE.runKernel(GatherV2, inputs, attrs); } const gather = /* @__PURE__ */ op({ gather_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the truth value of `NOT x` element-wise. * * ```js * const a = tf.tensor1d([false, true], 'bool'); * * a.logicalNot().print(); * ``` * * @param x The input tensor. Must be of dtype 'bool'. * * @doc {heading: 'Operations', subheading: 'Logical'} */ function logicalNot_(x) { const $x = convertToTensor(x, 'x', 'logicalNot', 'bool'); const inputs = { x: $x }; return ENGINE.runKernel(LogicalNot, inputs); } const logicalNot = /* @__PURE__ */ op({ logicalNot_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Returns the max of a and b (`a > b ? a : b`) element-wise. * Supports broadcasting. * * We also expose `tf.maximumStrict` which has the same signature as this op and * asserts that `a` and `b` are the same shape (does not broadcast). * * ```js * 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) * ``` * * ```js * // 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) * ``` * * @param a The first tensor. * @param b The second tensor. Must have the same type as `a`. * * @doc {heading: 'Operations', subheading: 'Arithmetic'} */ function maximum_(a, b) { let $a = convertToTensor(a, 'a', 'maximum'); let $b = convertToTensor(b, 'b', 'maximum'); [$a, $b] = makeTypesMatch($a, $b); if ($a.dtype === 'bool') { $a = cast($a, 'int32'); $b = cast($b, 'int32'); } assertAndGetBroadcastShape($a.shape, $b.shape); const inputs = { a: $a, b: $b }; return ENGINE.runKernel(Maximum$1, inputs); } const maximum$1 = /* @__PURE__ */ op({ maximum_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ /** * Pads a `tf.Tensor` with a given value and paddings. * * This operation implements `CONSTANT` mode. For `REFLECT` and `SYMMETRIC`, * refer to `tf.mirrorPad`. * * Also available are stricter rank-specific methods with the same signature * as this method that assert that `paddings` is of given length. * - `tf.pad1d` * - `tf.pad2d` * - `tf.pad3d` * - `tf.pad4d` * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * x.pad([[1, 2]]).print(); * ``` * @param x The tensor to pad. * @param paddings 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. * @param constantValue The pad value to use. Defaults to 0. * * @doc {heading: 'Tensors', subheading: 'Transformations'} */ function pad_(x, paddings, constantValue = 0) { const $x = convertToTensor(x, 'x', 'pad'); if ($x.rank === 0) { throw new Error('pad(scalar) is not defined. Pass non-scalar to pad'); } const attrs = { paddings, constantValue }; const inputs = { x: $x }; return ENGINE.runKernel(PadV2, inputs, attrs); } const pad = /* @__PURE__ */ op({ pad_ }); var alea$1 = {exports: {}}; (function (module) { // A port of an algorithm by Johannes Baagøe , 2010 // http://baagoe.com/en/RandomMusings/javascript/ // https://github.com/nquinlan/better-random-numbers-for-javascript-mirror // Original work is under MIT license - // Copyright (C) 2010 by Johannes Baagøe // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. (function(global, module, define) { function Alea(seed) { var me = this, mash = Mash(); me.next = function() { var t = 2091639 * me.s0 + me.c * 2.3283064365386963e-10; // 2^-32 me.s0 = me.s1; me.s1 = me.s2; return me.s2 = t - (me.c = t | 0); }; // Apply the seeding algorithm from Baagoe. me.c = 1; me.s0 = mash(' '); me.s1 = mash(' '); me.s2 = mash(' '); me.s0 -= mash(seed); if (me.s0 < 0) { me.s0 += 1; } me.s1 -= mash(seed); if (me.s1 < 0) { me.s1 += 1; } me.s2 -= mash(seed); if (me.s2 < 0) { me.s2 += 1; } mash = null; } function copy(f, t) { t.c = f.c; t.s0 = f.s0; t.s1 = f.s1; t.s2 = f.s2; return t; } function impl(seed, opts) { var xg = new Alea(seed), state = opts && opts.state, prng = xg.next; prng.int32 = function() { return (xg.next() * 0x100000000) | 0; }; prng.double = function() { return prng() + (prng() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53 }; prng.quick = prng; if (state) { if (typeof(state) == 'object') copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } function Mash() { var n = 0xefc8249d; var mash = function(data) { data = String(data); for (var i = 0; i < data.length; i++) { n += data.charCodeAt(i); var h = 0.02519603282416938 * n; n = h >>> 0; h -= n; h *= n; n = h >>> 0; h -= n; n += h * 0x100000000; // 2^32 } return (n >>> 0) * 2.3283064365386963e-10; // 2^-32 }; return mash; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.alea = impl; } })( commonjsGlobal, module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (alea$1)); var aleaExports = alea$1.exports; var xor128$1 = {exports: {}}; (function (module) { // A Javascript implementaion of the "xor128" prng algorithm by // George Marsaglia. See http://www.jstatsoft.org/v08/i14/paper (function(global, module, define) { function XorGen(seed) { var me = this, strseed = ''; me.x = 0; me.y = 0; me.z = 0; me.w = 0; // Set up generator function. me.next = function() { var t = me.x ^ (me.x << 11); me.x = me.y; me.y = me.z; me.z = me.w; return me.w ^= (me.w >>> 19) ^ t ^ (t >>> 8); }; if (seed === (seed | 0)) { // Integer seed. me.x = seed; } else { // String seed. strseed += seed; } // Mix in string seed, then discard an initial batch of 64 values. for (var k = 0; k < strseed.length + 64; k++) { me.x ^= strseed.charCodeAt(k) | 0; me.next(); } } function copy(f, t) { t.x = f.x; t.y = f.y; t.z = f.z; t.w = f.w; return t; } function impl(seed, opts) { var xg = new XorGen(seed), state = opts && opts.state, prng = function() { return (xg.next() >>> 0) / 0x100000000; }; prng.double = function() { do { var top = xg.next() >>> 11, bot = (xg.next() >>> 0) / 0x100000000, result = (top + bot) / (1 << 21); } while (result === 0); return result; }; prng.int32 = xg.next; prng.quick = prng; if (state) { if (typeof(state) == 'object') copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.xor128 = impl; } })( commonjsGlobal, module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (xor128$1)); var xor128Exports = xor128$1.exports; var xorwow$1 = {exports: {}}; (function (module) { // A Javascript implementaion of the "xorwow" prng algorithm by // George Marsaglia. See http://www.jstatsoft.org/v08/i14/paper (function(global, module, define) { function XorGen(seed) { var me = this, strseed = ''; // Set up generator function. me.next = function() { var t = (me.x ^ (me.x >>> 2)); me.x = me.y; me.y = me.z; me.z = me.w; me.w = me.v; return (me.d = (me.d + 362437 | 0)) + (me.v = (me.v ^ (me.v << 4)) ^ (t ^ (t << 1))) | 0; }; me.x = 0; me.y = 0; me.z = 0; me.w = 0; me.v = 0; if (seed === (seed | 0)) { // Integer seed. me.x = seed; } else { // String seed. strseed += seed; } // Mix in string seed, then discard an initial batch of 64 values. for (var k = 0; k < strseed.length + 64; k++) { me.x ^= strseed.charCodeAt(k) | 0; if (k == strseed.length) { me.d = me.x << 10 ^ me.x >>> 4; } me.next(); } } function copy(f, t) { t.x = f.x; t.y = f.y; t.z = f.z; t.w = f.w; t.v = f.v; t.d = f.d; return t; } function impl(seed, opts) { var xg = new XorGen(seed), state = opts && opts.state, prng = function() { return (xg.next() >>> 0) / 0x100000000; }; prng.double = function() { do { var top = xg.next() >>> 11, bot = (xg.next() >>> 0) / 0x100000000, result = (top + bot) / (1 << 21); } while (result === 0); return result; }; prng.int32 = xg.next; prng.quick = prng; if (state) { if (typeof(state) == 'object') copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.xorwow = impl; } })( commonjsGlobal, module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (xorwow$1)); var xorwowExports = xorwow$1.exports; var xorshift7$1 = {exports: {}}; (function (module) { // A Javascript implementaion of the "xorshift7" algorithm by // François Panneton and Pierre L'ecuyer: // "On the Xorgshift Random Number Generators" // http://saluc.engr.uconn.edu/refs/crypto/rng/panneton05onthexorshift.pdf (function(global, module, define) { function XorGen(seed) { var me = this; // Set up generator function. me.next = function() { // Update xor generator. var X = me.x, i = me.i, t, v; t = X[i]; t ^= (t >>> 7); v = t ^ (t << 24); t = X[(i + 1) & 7]; v ^= t ^ (t >>> 10); t = X[(i + 3) & 7]; v ^= t ^ (t >>> 3); t = X[(i + 4) & 7]; v ^= t ^ (t << 7); t = X[(i + 7) & 7]; t = t ^ (t << 13); v ^= t ^ (t << 9); X[i] = v; me.i = (i + 1) & 7; return v; }; function init(me, seed) { var j, X = []; if (seed === (seed | 0)) { // Seed state array using a 32-bit integer. X[0] = seed; } else { // Seed state using a string. seed = '' + seed; for (j = 0; j < seed.length; ++j) { X[j & 7] = (X[j & 7] << 15) ^ (seed.charCodeAt(j) + X[(j + 1) & 7] << 13); } } // Enforce an array length of 8, not all zeroes. while (X.length < 8) X.push(0); for (j = 0; j < 8 && X[j] === 0; ++j); if (j == 8) X[7] = -1; else X[j]; me.x = X; me.i = 0; // Discard an initial 256 values. for (j = 256; j > 0; --j) { me.next(); } } init(me, seed); } function copy(f, t) { t.x = f.x.slice(); t.i = f.i; return t; } function impl(seed, opts) { if (seed == null) seed = +(new Date); var xg = new XorGen(seed), state = opts && opts.state, prng = function() { return (xg.next() >>> 0) / 0x100000000; }; prng.double = function() { do { var top = xg.next() >>> 11, bot = (xg.next() >>> 0) / 0x100000000, result = (top + bot) / (1 << 21); } while (result === 0); return result; }; prng.int32 = xg.next; prng.quick = prng; if (state) { if (state.x) copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.xorshift7 = impl; } })( commonjsGlobal, module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (xorshift7$1)); var xorshift7Exports = xorshift7$1.exports; var xor4096$1 = {exports: {}}; (function (module) { // A Javascript implementaion of Richard Brent's Xorgens xor4096 algorithm. // // This fast non-cryptographic random number generator is designed for // use in Monte-Carlo algorithms. It combines a long-period xorshift // generator with a Weyl generator, and it passes all common batteries // of stasticial tests for randomness while consuming only a few nanoseconds // for each prng generated. For background on the generator, see Brent's // paper: "Some long-period random number generators using shifts and xors." // http://arxiv.org/pdf/1004.3115v1.pdf // // Usage: // // var xor4096 = require('xor4096'); // random = xor4096(1); // Seed with int32 or string. // assert.equal(random(), 0.1520436450538547); // (0, 1) range, 53 bits. // assert.equal(random.int32(), 1806534897); // signed int32, 32 bits. // // For nonzero numeric keys, this impelementation provides a sequence // identical to that by Brent's xorgens 3 implementaion in C. This // implementation also provides for initalizing the generator with // string seeds, or for saving and restoring the state of the generator. // // On Chrome, this prng benchmarks about 2.1 times slower than // Javascript's built-in Math.random(). (function(global, module, define) { function XorGen(seed) { var me = this; // Set up generator function. me.next = function() { var w = me.w, X = me.X, i = me.i, t, v; // Update Weyl generator. me.w = w = (w + 0x61c88647) | 0; // Update xor generator. v = X[(i + 34) & 127]; t = X[i = ((i + 1) & 127)]; v ^= v << 13; t ^= t << 17; v ^= v >>> 15; t ^= t >>> 12; // Update Xor generator array state. v = X[i] = v ^ t; me.i = i; // Result is the combination. return (v + (w ^ (w >>> 16))) | 0; }; function init(me, seed) { var t, v, i, j, w, X = [], limit = 128; if (seed === (seed | 0)) { // Numeric seeds initialize v, which is used to generates X. v = seed; seed = null; } else { // String seeds are mixed into v and X one character at a time. seed = seed + '\0'; v = 0; limit = Math.max(limit, seed.length); } // Initialize circular array and weyl value. for (i = 0, j = -32; j < limit; ++j) { // Put the unicode characters into the array, and shuffle them. if (seed) v ^= seed.charCodeAt((j + 32) % seed.length); // After 32 shuffles, take v as the starting w value. if (j === 0) w = v; v ^= v << 10; v ^= v >>> 15; v ^= v << 4; v ^= v >>> 13; if (j >= 0) { w = (w + 0x61c88647) | 0; // Weyl. t = (X[j & 127] ^= (v + w)); // Combine xor and weyl to init array. i = (0 == t) ? i + 1 : 0; // Count zeroes. } } // We have detected all zeroes; make the key nonzero. if (i >= 128) { X[(seed && seed.length || 0) & 127] = -1; } // Run the generator 512 times to further mix the state before using it. // Factoring this as a function slows the main generator, so it is just // unrolled here. The weyl generator is not advanced while warming up. i = 127; for (j = 4 * 128; j > 0; --j) { v = X[(i + 34) & 127]; t = X[i = ((i + 1) & 127)]; v ^= v << 13; t ^= t << 17; v ^= v >>> 15; t ^= t >>> 12; X[i] = v ^ t; } // Storing state as object members is faster than using closure variables. me.w = w; me.X = X; me.i = i; } init(me, seed); } function copy(f, t) { t.i = f.i; t.w = f.w; t.X = f.X.slice(); return t; } function impl(seed, opts) { if (seed == null) seed = +(new Date); var xg = new XorGen(seed), state = opts && opts.state, prng = function() { return (xg.next() >>> 0) / 0x100000000; }; prng.double = function() { do { var top = xg.next() >>> 11, bot = (xg.next() >>> 0) / 0x100000000, result = (top + bot) / (1 << 21); } while (result === 0); return result; }; prng.int32 = xg.next; prng.quick = prng; if (state) { if (state.X) copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.xor4096 = impl; } })( commonjsGlobal, // window object or global module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (xor4096$1)); var xor4096Exports = xor4096$1.exports; var tychei$1 = {exports: {}}; (function (module) { // A Javascript implementaion of the "Tyche-i" prng algorithm by // Samuel Neves and Filipe Araujo. // See https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf (function(global, module, define) { function XorGen(seed) { var me = this, strseed = ''; // Set up generator function. me.next = function() { var b = me.b, c = me.c, d = me.d, a = me.a; b = (b << 25) ^ (b >>> 7) ^ c; c = (c - d) | 0; d = (d << 24) ^ (d >>> 8) ^ a; a = (a - b) | 0; me.b = b = (b << 20) ^ (b >>> 12) ^ c; me.c = c = (c - d) | 0; me.d = (d << 16) ^ (c >>> 16) ^ a; return me.a = (a - b) | 0; }; /* The following is non-inverted tyche, which has better internal * bit diffusion, but which is about 25% slower than tyche-i in JS. me.next = function() { var a = me.a, b = me.b, c = me.c, d = me.d; a = (me.a + me.b | 0) >>> 0; d = me.d ^ a; d = d << 16 ^ d >>> 16; c = me.c + d | 0; b = me.b ^ c; b = b << 12 ^ d >>> 20; me.a = a = a + b | 0; d = d ^ a; me.d = d = d << 8 ^ d >>> 24; me.c = c = c + d | 0; b = b ^ c; return me.b = (b << 7 ^ b >>> 25); } */ me.a = 0; me.b = 0; me.c = 2654435769 | 0; me.d = 1367130551; if (seed === Math.floor(seed)) { // Integer seed. me.a = (seed / 0x100000000) | 0; me.b = seed | 0; } else { // String seed. strseed += seed; } // Mix in string seed, then discard an initial batch of 64 values. for (var k = 0; k < strseed.length + 20; k++) { me.b ^= strseed.charCodeAt(k) | 0; me.next(); } } function copy(f, t) { t.a = f.a; t.b = f.b; t.c = f.c; t.d = f.d; return t; } function impl(seed, opts) { var xg = new XorGen(seed), state = opts && opts.state, prng = function() { return (xg.next() >>> 0) / 0x100000000; }; prng.double = function() { do { var top = xg.next() >>> 11, bot = (xg.next() >>> 0) / 0x100000000, result = (top + bot) / (1 << 21); } while (result === 0); return result; }; prng.int32 = xg.next; prng.quick = prng; if (state) { if (typeof(state) == 'object') copy(state, xg); prng.state = function() { return copy(xg, {}); }; } return prng; } if (module && module.exports) { module.exports = impl; } else if (define && define.amd) { define(function() { return impl; }); } else { this.tychei = impl; } })( commonjsGlobal, module, // present in node.js (typeof undefined) == 'function' // present with an AMD loader ); } (tychei$1)); var tycheiExports = tychei$1.exports; var seedrandom = {exports: {}}; var _nodeResolve_empty = {}; var _nodeResolve_empty$1 = { __proto__: null, default: _nodeResolve_empty }; var require$$0 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1); /* Copyright 2019 David Bau. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function (module) { (function (global, pool, math) { // // The following constants are related to IEEE 754 limits. // var width = 256, // each RC4 output is 0 <= x < 256 chunks = 6, // at least six RC4 outputs for each double digits = 52, // there are 52 significant digits in a double rngname = 'random', // rngname: name for Math.random and Math.seedrandom startdenom = math.pow(width, chunks), significance = math.pow(2, digits), overflow = significance * 2, mask = width - 1, nodecrypto; // node.js crypto module, initialized at the bottom. // // seedrandom() // This is the seedrandom function described above. // function seedrandom(seed, options, callback) { var key = []; options = (options == true) ? { entropy: true } : (options || {}); // Flatten the seed string or build one from local entropy if needed. var shortseed = mixkey(flatten( options.entropy ? [seed, tostring(pool)] : (seed == null) ? autoseed() : seed, 3), key); // Use the seed to initialize an ARC4 generator. var arc4 = new ARC4(key); // This function returns a random double in [0, 1) that contains // randomness in every bit of the mantissa of the IEEE 754 value. var prng = function() { var n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48 d = startdenom, // and denominator d = 2 ^ 48. x = 0; // and no 'extra last byte'. while (n < significance) { // Fill up all significant digits by n = (n + x) * width; // shifting numerator and d *= width; // denominator and generating a x = arc4.g(1); // new least-significant-byte. } while (n >= overflow) { // To avoid rounding up, before adding n /= 2; // last byte, shift everything d /= 2; // right using integer math until x >>>= 1; // we have exactly the desired bits. } return (n + x) / d; // Form the number within [0, 1). }; prng.int32 = function() { return arc4.g(4) | 0; }; prng.quick = function() { return arc4.g(4) / 0x100000000; }; prng.double = prng; // Mix the randomness into accumulated entropy. mixkey(tostring(arc4.S), pool); // Calling convention: what to return as a function of prng, seed, is_math. return (options.pass || callback || function(prng, seed, is_math_call, state) { if (state) { // Load the arc4 state from the given state if it has an S array. if (state.S) { copy(state, arc4); } // Only provide the .state method if requested via options.state. prng.state = function() { return copy(arc4, {}); }; } // If called as a method of Math (Math.seedrandom()), mutate // Math.random because that is how seedrandom.js has worked since v1.0. if (is_math_call) { math[rngname] = prng; return seed; } // Otherwise, it is a newer calling convention, so return the // prng directly. else return prng; })( prng, shortseed, 'global' in options ? options.global : (this == math), options.state); } // // ARC4 // // An ARC4 implementation. The constructor takes a key in the form of // an array of at most (width) integers that should be 0 <= x < (width). // // The g(count) method returns a pseudorandom integer that concatenates // the next (count) outputs from ARC4. Its return value is a number x // that is in the range 0 <= x < (width ^ count). // function ARC4(key) { var t, keylen = key.length, me = this, i = 0, j = me.i = me.j = 0, s = me.S = []; // The empty key [] is treated as [0]. if (!keylen) { key = [keylen++]; } // Set up S using the standard key scheduling algorithm. while (i < width) { s[i] = i++; } for (i = 0; i < width; i++) { s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))]; s[j] = t; } // The "g" method returns the next (count) outputs as one number. (me.g = function(count) { // Using instance members instead of closure state nearly doubles speed. var t, r = 0, i = me.i, j = me.j, s = me.S; while (count--) { t = s[i = mask & (i + 1)]; r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))]; } me.i = i; me.j = j; return r; // For robust unpredictability, the function call below automatically // discards an initial batch of values. This is called RC4-drop[256]. // See http://google.com/search?q=rsa+fluhrer+response&btnI })(width); } // // copy() // Copies internal state of ARC4 to or from a plain object. // function copy(f, t) { t.i = f.i; t.j = f.j; t.S = f.S.slice(); return t; } // // flatten() // Converts an object tree to nested arrays of strings. // function flatten(obj, depth) { var result = [], typ = (typeof obj), prop; if (depth && typ == 'object') { for (prop in obj) { try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {} } } return (result.length ? result : typ == 'string' ? obj : obj + '\0'); } // // mixkey() // Mixes a string seed into a key that is an array of integers, and // returns a shortened string seed that is equivalent to the result key. // function mixkey(seed, key) { var stringseed = seed + '', smear, j = 0; while (j < stringseed.length) { key[mask & j] = mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++)); } return tostring(key); } // // autoseed() // Returns an object for autoseeding, using window.crypto and Node crypto // module if available. // function autoseed() { try { var out; if (nodecrypto && (out = nodecrypto.randomBytes)) { // The use of 'out' to remember randomBytes makes tight minified code. out = out(width); } else { out = new Uint8Array(width); (global.crypto || global.msCrypto).getRandomValues(out); } return tostring(out); } catch (e) { var browser = global.navigator, plugins = browser && browser.plugins; return [+new Date, global, plugins, global.screen, tostring(pool)]; } } // // tostring() // Converts an array of charcodes to a string // function tostring(a) { return String.fromCharCode.apply(0, a); } // // When seedrandom.js is loaded, we immediately mix a few bits // from the built-in RNG into the entropy pool. Because we do // not want to interfere with deterministic PRNG state later, // seedrandom will not call math.random on its own again after // initialization. // mixkey(math.random(), pool); // // Nodejs and AMD support: export the implementation as a module using // either convention. // if (module.exports) { module.exports = seedrandom; // When in node.js, try using crypto package for autoseeding. try { nodecrypto = require$$0; } catch (ex) {} } else { // When included as a plain script, set up Math.seedrandom global. math['seed' + rngname] = seedrandom; } // End anonymous scope, and pass initial values. })( // global: `self` in browsers (including strict mode and web workers), // otherwise `this` in Node and other environments (typeof self !== 'undefined') ? self : commonjsGlobal, [], // pool: entropy pool starts empty Math // math: package containing random, pow, and seedrandom ); } (seedrandom)); var seedrandomExports = seedrandom.exports; // A library of seedable RNGs implemented in Javascript. // // Usage: // // var seedrandom = require('seedrandom'); // var random = seedrandom(1); // or any seed. // var x = random(); // 0 <= x < 1. Every bit is random. // var x = random.quick(); // 0 <= x < 1. 32 bits of randomness. // alea, a 53-bit multiply-with-carry generator by Johannes Baagøe. // Period: ~2^116 // Reported to pass all BigCrush tests. var alea = aleaExports; // xor128, a pure xor-shift generator by George Marsaglia. // Period: 2^128-1. // Reported to fail: MatrixRank and LinearComp. var xor128 = xor128Exports; // xorwow, George Marsaglia's 160-bit xor-shift combined plus weyl. // Period: 2^192-2^32 // Reported to fail: CollisionOver, SimpPoker, and LinearComp. var xorwow = xorwowExports; // xorshift7, by François Panneton and Pierre L'ecuyer, takes // a different approach: it adds robustness by allowing more shifts // than Marsaglia's original three. It is a 7-shift generator // with 256 bits, that passes BigCrush with no systmatic failures. // Period 2^256-1. // No systematic BigCrush failures reported. var xorshift7 = xorshift7Exports; // xor4096, by Richard Brent, is a 4096-bit xor-shift with a // very long period that also adds a Weyl generator. It also passes // BigCrush with no systematic failures. Its long period may // be useful if you have many generators and need to avoid // collisions. // Period: 2^4128-2^32. // No systematic BigCrush failures reported. var xor4096 = xor4096Exports; // Tyche-i, by Samuel Neves and Filipe Araujo, is a bit-shifting random // number generator derived from ChaCha, a modern stream cipher. // https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf // Period: ~2^127 // No systematic BigCrush failures reported. var tychei = tycheiExports; // The original ARC4-based prng included in this library. // Period: ~2^1600 var sr = seedrandomExports; sr.alea = alea; sr.xor128 = xor128; sr.xorwow = xorwow; sr.xorshift7 = xorshift7; sr.xor4096 = xor4096; sr.tychei = tychei; /** * @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. * ============================================================================= */ /** * Reverses a `tf.Tensor` along a specified axis. * * Also available are stricter rank-specific methods that assert that `x` is * of the given rank: * - `tf.reverse1d` * - `tf.reverse2d` * - `tf.reverse3d` * - `tf.reverse4d` * * Except `tf.reverse1d` (which does not have axis param), all methods have * same signature as this method. * * ```js * const x = tf.tensor1d([1, 2, 3, 4]); * * x.reverse().print(); * ``` * * ```js * const x = tf.tensor2d([1, 2, 3, 4], [2, 2]); * * const axis = 1; * x.reverse(axis).print(); * ``` * @param x The input tensor to be reversed. * @param axis The set of dimensions to reverse. Must be in the * range [-rank(x), rank(x)). Defaults to all axes. * * @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function reverse_(x, axis) { const $x = convertToTensor(x, 'x', 'reverse'); const inputs = { x: $x }; const attrs = { dims: axis }; return ENGINE.runKernel(Reverse, inputs, attrs); } const reverse = /* @__PURE__ */ op({ reverse_ }); /** * @license * Copyright 2022 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. * ============================================================================= */ /** * Creates a new tensor by applying sparse updates to individual * values or slices to the passed in tensor according to * indices. This operator is the similar to scatterNd op, except that the * udpates are scattered on an existing tensor (as opposed to a zero-tensor). * * If indices contains duplicates, then we pick the last update for the index. * * If an out of bound index is found on CPU, an error is returned. * * Warning: There are some GPU specific semantics for this operation. * - If an out of bound index is found, the index is ignored. * - The order in which updates are applied is nondeterministic, so the output * will be nondeterministic if indices contains duplicates. * ```js * const shape = [8]; * const tensor = tf.ones(shape); * const indices = tf.tensor2d([4, 3, 1, 7], [4, 1], 'int32'); * const updates = tf.tensor1d([9, 10, 11, 12]); * * tf.tensorScatterUpdate(tensor, indices, updates).print(); * //[1, 11, 1, 10, 9, 1, 1, 12] * ``` * * @param tensor A Tensor. Tensor to copy/update. * @param indices The tensor contains the indices into the output tensor, must * have at least 2 axes: (num_updates, index_depth). * @param updates The tensor contains the value for the indices. * * @doc {heading: 'Operations', subheading: 'Slicing and Joining'} */ function tensorScatterUpdate_(tensor, indices, updates) { const $tensor = convertToTensor(tensor, 'tensor', 'tensorScatterupdate'); const $indices = convertToTensor(indices, 'indices', 'tensorScatterupdate', 'int32'); const $updates = convertToTensor(updates, 'updates', 'tensorScatterupdate'); validateInput($updates, $indices, $tensor.shape); if ($tensor.dtype !== $updates.dtype) { throw new Error(`tensor and updates must have the same dtype, instead they are ${$tensor.dtype} and ${$updates.dtype}.`); } const inputs = { tensor: $tensor, indices: $indices, updates: $updates }; const attrs = {}; // tslint:disable-next-line: no-unnecessary-type-assertion return ENGINE.runKernel(TensorScatterUpdate, inputs, attrs); } op({ tensorScatterUpdate_ }); /** * @license * Copyright 2020 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. * ============================================================================= */ var Reduction; (function (Reduction) { Reduction[Reduction["NONE"] = 0] = "NONE"; Reduction[Reduction["MEAN"] = 1] = "MEAN"; Reduction[Reduction["SUM"] = 2] = "SUM"; Reduction[Reduction["SUM_BY_NONZERO_WEIGHTS"] = 3] = "SUM_BY_NONZERO_WEIGHTS"; })(Reduction || (Reduction = {})); /** * @license * Copyright 2017 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. * ============================================================================= */ (() => { if (typeof requestAnimationFrame !== 'undefined') { return requestAnimationFrame; } else if (typeof setImmediate !== 'undefined') { return setImmediate; } return (f) => f(); // no delays })(); /** * @license * Copyright 2022 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. * ============================================================================= */ var RowPartitionType; (function (RowPartitionType) { RowPartitionType[RowPartitionType["FIRST_DIM_SIZE"] = 0] = "FIRST_DIM_SIZE"; RowPartitionType[RowPartitionType["VALUE_ROWIDS"] = 1] = "VALUE_ROWIDS"; RowPartitionType[RowPartitionType["ROW_LENGTHS"] = 2] = "ROW_LENGTHS"; RowPartitionType[RowPartitionType["ROW_SPLITS"] = 3] = "ROW_SPLITS"; RowPartitionType[RowPartitionType["ROW_LIMITS"] = 4] = "ROW_LIMITS"; RowPartitionType[RowPartitionType["ROW_STARTS"] = 5] = "ROW_STARTS"; })(RowPartitionType || (RowPartitionType = {})); /** * @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. * ============================================================================= */ const SELU_SCALEALPHA = 1.7580993408473768599402175208123; const SELU_SCALE = 1.0507009873554804934193349852946; /** * @license * Copyright 2022 Google Inc. 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. * ============================================================================= */ // Gradient for product operation on a single axis. function prodGradFn_(x, dy, axis) { // The gradient tensor (dy) has a set of axes removed, so we create re-shaped // versions (of size 1) for the removed axis; this supports broadcasting over // those dimensions. const expandedYShape = x.shape.slice(); expandedYShape[axis] = 1; // The actual gradient computation. const expandedDy = reshape$1(dy, expandedYShape); const xCumProd = cumprod(x, axis, true, false); const xCumRevProd = cumprod(x, axis, true, true); const dx = mul(xCumProd, xCumRevProd); return mul(expandedDy, dx); } // Support gradients when the product is done on many axes at once. // This done py pushing all the axes on which the product is applied into a // single axis. function prodsGradFn_(x, dy, axis) { // Move all axes for doing prod over to the end of the tensor. const xRank = x.shape.length; const finalProdAxis = xRank - axis.length; const xPermutation = getAxesPermutation(axis, xRank); let permutedX = x; if (xPermutation != null) { permutedX = transpose(x, xPermutation); } // Reshape all the prod dimensions into a single one, and do compute prod // gradients on that. const newShape = permutedX.shape.slice(); const removedShape = newShape.splice(xRank - axis.length, axis.length); const endPartShape = removedShape.reduce((p, c) => p * c, 1); newShape.push(endPartShape); const reshapedPermutedX = permutedX.reshape(newShape); let prodGrad = prodGradFn_(reshapedPermutedX, dy, finalProdAxis); // Undo the re-shaping now we have the dx vector, and permute back to // original axes order. prodGrad = prodGrad.reshape(permutedX.shape); if (xPermutation != null) { const undoPermutation = getUndoAxesPermutation(xPermutation); prodGrad = transpose(prodGrad, undoPermutation); } return prodGrad; } // Running example: // [ // [ // [3.0, 4.0], // [5.0, 6.0], // [7.0, 8.0] // ], // [ // [3.0, 5.0], // [0.0, 6.0], // [5.0, 6.0] // ] // ] // const prodGradConfig = { kernelName: Prod, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { axis } = attrs; let axisArr = []; if (axis === undefined || axis === null) { axisArr = x.shape.map((_, i) => i); } else if (typeof axis === 'number') { axisArr = [axis]; } else { axisArr = axis; } return { x: () => prodsGradFn_(x, dy, axisArr) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const divGradConfig = { kernelName: RealDiv, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { const res = div(dy, cast(b, 'float32')); const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { return reshape$1(sum(res, reduceAxes), a.shape); } return res; }; const derB = () => { let res = mul(dy, cast(a, 'float32')); const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { res = reshape$1(sum(res, reduceAxes), b.shape); } const tmp = square(b); return neg(div(res, cast(tmp, 'float32'))); }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const reciprocalGradConfig = { kernelName: Reciprocal, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, neg(square(x))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const relu6GradConfig = { kernelName: Relu6$1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; const mask = mul(lessEqual(x, 6), step(x)); return { x: () => mul(dy, cast(mask, 'float32')) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const reluGradConfig = { kernelName: Relu$1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(dy, cast(step(x), 'float32')) }; } }; /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ const reshapeGradConfig = { kernelName: Reshape$1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => reshape$1(dy, x.shape) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const resizeBilinearGradConfig = { kernelName: ResizeBilinear, inputsToSave: ['images'], gradFunc: (dy, saved, attrs) => { const [images] = saved; const inputs = { dy, images }; const imagesDer = () => // tslint:disable-next-line: no-unnecessary-type-assertion ENGINE.runKernel(ResizeBilinearGrad, inputs, attrs); return { images: imagesDer }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const resizeNearestNeighborGradConfig = { kernelName: ResizeNearestNeighbor, inputsToSave: ['images'], gradFunc: (dy, saved, attrs) => { const [images] = saved; const inputs = { dy, images }; const imagesDer = () => // tslint:disable-next-line: no-unnecessary-type-assertion ENGINE.runKernel(ResizeNearestNeighborGrad, inputs, attrs); return { images: imagesDer }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const reverseGradConfig = { kernelName: Reverse, gradFunc: (dy, saved, attrs) => { const { dims } = attrs; const axes = parseAxisParam(dims, dy.shape); return { x: () => reverse(dy, axes) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const roundGradConfig = { kernelName: Round, gradFunc: (dy) => { // TODO(nsthorat): Let gradients be null for cases where we want to stop // backpropgation. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const rsqrtGradConfig = { kernelName: Rsqrt, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => neg(div(dy, mul(pow(x, 1.5), 2))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const selectGradConfig = { kernelName: Select, inputsToSave: ['condition'], gradFunc: (dy, saved) => { const [condition] = saved; return { // TODO(julianoks): Return null for condition gradient // when backprop supports it. condition: () => cast(zerosLike(condition), 'float32'), t: () => mul(dy, cast(condition, dy.dtype)), e: () => mul(dy, cast(logicalNot(condition), dy.dtype)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const seluGradConfig = { kernelName: Selu$1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => { const mask = greater$1(x, scalar(0)); const scaleAlpha = scalar(SELU_SCALEALPHA); const scale = scalar(SELU_SCALE); const greaterThanZeroDer = mul(dy, scale); const lessEqualZeroDer = mul(mul(dy, scaleAlpha), exp(cast(x, 'float32'))); return where(mask, greaterThanZeroDer, lessEqualZeroDer); } }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const sigmoidGradConfig = { kernelName: Sigmoid$1, outputsToSave: [true], gradFunc: (dy, saved) => { const [y] = saved; return { x: () => mul(dy, mul(y, sub(scalar(1), y))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const signGradConfig = { kernelName: Sign, gradFunc: (dy) => { return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const sinGradConfig = { kernelName: Sin, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(cos(cast(x, 'float32')), dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const sinhGradConfig = { kernelName: Sinh, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(cosh(cast(x, 'float32')), dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const sliceGradConfig = { kernelName: Slice, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { begin, size } = attrs; const inputShape = x.shape; const [begin_, size_] = parseSliceParams(x, begin, size); // Create an Nx2 padding where the first column represents how many // zeros are prepended (at start) for each dimension, and the second // column indicates how many zeros are appended (at end). // The number of zeros to append is the shape of the input // elementwise-subtracted by both the begin vector and sizes vector. const paddings = []; for (let i = 0; i < dy.rank; i++) { paddings.push([begin_[i], inputShape[i] - begin_[i] - size_[i]]); } return { x: () => pad(dy, paddings) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const softmaxGradConfig = { kernelName: Softmax$2, outputsToSave: [true], gradFunc: (dy, saved, attrs) => { const [y] = saved; const { dim } = attrs; const keepDims = true; const dyTimesY = mul(dy, y); return { logits: () => sub(dyTimesY, mul(sum(dyTimesY, [dim], keepDims), y)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const softplusGradConfig = { kernelName: Softplus$1, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(dy, sigmoid(x)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const spaceToBatchNDGradConfig = { kernelName: SpaceToBatchND, gradFunc: (dy, saved, attrs) => { const { blockShape, paddings } = attrs; return { x: () => batchToSpaceND(dy, blockShape, paddings) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const splitVGradConfig = { kernelName: SplitV, gradFunc: (dy, saved, attrs) => { const { axis } = attrs; return { x: () => concat(dy, axis) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const sqrtGradConfig = { kernelName: Sqrt, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, mul(sqrt(cast(x, 'float32')), 2)) }; } }; /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ const squareGradConfig = { kernelName: Square, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => mul(dy, mul(cast(x, 'float32'), 2)) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const squaredDifferenceGradConfig = { kernelName: SquaredDifference, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const two = scalar(2); const derA = () => mul(dy, mul(two, sub(a, b))); const derB = () => mul(dy, mul(two, sub(b, a))); return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const stepGradConfig = { kernelName: Step, gradFunc: (dy) => { // TODO(manrajgrover): Return null for gradients when backprop supports // it. return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const subGradConfig = { kernelName: Sub, inputsToSave: ['a', 'b'], gradFunc: (dy, saved) => { const [a, b] = saved; const outShape = assertAndGetBroadcastShape(a.shape, b.shape); const derA = () => { let res = dy; const reduceAxes = getReductionAxes(a.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(res, a.shape); }; const derB = () => { let res = dy; const reduceAxes = getReductionAxes(b.shape, outShape); if (reduceAxes.length > 0) { res = sum(res, reduceAxes); } return reshape$1(neg(res), b.shape); }; return { a: derA, b: derB }; } }; /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ const sumGradConfig = { kernelName: Sum, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const expandedDyShape = x.shape.slice(); const { axis } = attrs; const axes = parseAxisParam(axis, x.shape); axes.forEach(axis => { expandedDyShape[axis] = 1; }); const expandedDy = reshape$1(dy, expandedDyShape); const derX = mul(expandedDy, ones$1(x.shape, 'float32')); return { x: () => derX }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const tanGradConfig = { kernelName: Tan, inputsToSave: ['x'], gradFunc: (dy, saved) => { const [x] = saved; return { x: () => div(dy, square(cos(x))) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const tanhGradConfig = { kernelName: Tanh$1, outputsToSave: [true], gradFunc: (dy, saved) => { const [y] = saved; return { x: () => mul(sub(scalar(1), square(y)), dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const tileGradConfig = { kernelName: Tile, inputsToSave: ['x'], gradFunc: (dy, saved, attrs) => { const [x] = saved; const { reps } = attrs; const derX = () => { let xGrad = zerosLike(x); // TODO(cais): Maybe reduce memory footprint by avoiding repeated // slicing. if (x.rank === 1) { for (let i = 0; i < reps[0]; ++i) { xGrad = add$1(xGrad, slice(dy, [i * x.shape[0]], [x.shape[0]])); } } else if (x.rank === 2) { for (let i = 0; i < reps[0]; ++i) { for (let j = 0; j < reps[1]; ++j) { xGrad = add$1(xGrad, slice(dy, [i * x.shape[0], j * x.shape[1]], [ x.shape[0], x.shape[1] ])); } } } else if (x.rank === 3) { for (let i = 0; i < reps[0]; ++i) { for (let j = 0; j < reps[1]; ++j) { for (let k = 0; k < reps[2]; ++k) { xGrad = add$1(xGrad, slice(dy, [i * x.shape[0], j * x.shape[1], k * x.shape[2]], [x.shape[0], x.shape[1], x.shape[2]])); } } } } else if (x.rank === 4) { for (let i = 0; i < reps[0]; ++i) { for (let j = 0; j < reps[1]; ++j) { for (let k = 0; k < reps[2]; ++k) { for (let l = 0; l < reps[3]; ++l) { xGrad = add$1(xGrad, slice(dy, [ i * x.shape[0], j * x.shape[1], k * x.shape[2], l * x.shape[3] ], [x.shape[0], x.shape[1], x.shape[2], x.shape[3]])); } } } } } else { throw new Error(`Gradient for tile operation is not implemented for rank-` + `${x.rank} tensors yet.`); } return xGrad; }; return { x: derX }; }, }; /** * @license * Copyright 2020 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. * ============================================================================= */ const transposeGradConfig = { kernelName: Transpose, gradFunc: (dy, saved, attrs) => { const transposeAttrs = attrs; const { perm } = transposeAttrs; const undoPerm = getUndoAxesPermutation(perm); return { x: () => transpose(dy, undoPerm) }; } }; /** * @license * Copyright 2020 Google Inc. 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. * ============================================================================= */ const unpackGradConfig = { kernelName: Unpack, gradFunc: (dy, saved, attrs) => { const unpackAttrs = attrs; const { axis } = unpackAttrs; return { value: () => stack(dy, axis) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ const unsortedSegmentSumGradConfig = { kernelName: UnsortedSegmentSum, inputsToSave: ['segmentIds'], gradFunc: (dy, saved) => { const [segmentIds] = saved; const derX = () => { return gatherDropNegatives(dy, segmentIds); }; return { x: derX }; } }; function gatherDropNegatives(x, indices) { // Helper function for unsorted segment ops. Gathers params for // positive segment ids and gathers 0 for inputs with negative segment id. // Mirrors _GatherDropNegatives from tensorflow/python/ops/math_grad.py const zeroClippedIndices = maximum$1(indices, zerosLike(indices)); const gathered = gather(x, zeroClippedIndices); let isPositive = greaterEqual(indices, scalar(0, 'int32')); const numIters = gathered.rank - isPositive.rank; for (let i = 0; i < numIters; ++i) { isPositive = expandDims(isPositive, i + 1); } isPositive = logicalAnd(isPositive, ones$1(gathered.shape, 'bool')); const zeroSlice = zerosLike(gathered); return where(isPositive, gathered, zeroSlice); } /** * @license * Copyright 2020 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. * ============================================================================= */ const zerosLikeGradConfig = { kernelName: ZerosLike, gradFunc: (dy) => { return { x: () => zerosLike(dy) }; } }; /** * @license * Copyright 2020 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. * ============================================================================= */ // Export all kernel configs here so that the package can auto register them const gradConfigs = [ absGradConfig, acosGradConfig, acoshGradConfig, addGradConfig, addNGradConfig, argMaxGradConfig, argMinGradConfig, asinGradConfig, asinhGradConfig, atan2GradConfig, atanGradConfig, atanhGradConfig, avgPool3DGradConfig, avgPoolGradConfig, batchMatMulGradConfig, batchToSpaceNDGradConfig, broadcastToGradConfig, castGradConfig, ceilGradConfig, clipByValueGradConfig, complexAbsGradConfig, concatGradConfig, conv2DBackpropInputGradConfig, conv2DGradConfig, conv3DGradConfig, cosGradConfig, coshGradConfig, cumsumGradConfig, depthwiseConv2dNativeGradConfig, dilation2dGradConfig, divGradConfig, eluGradConfig, erfGradConfig, expGradConfig, expandDimsGradConfig, expm1GradConfig, floorDivGradConfig, floorGradConfig, fusedBatchNormGradConfig, gatherGradConfig, greaterEqualGradConfig, identityGradConfig, isFiniteGradConfig, isInfGradConfig, isNanGradConfig, leakyReluGradConfig, log1pGradConfig, logGradConfig, logSoftmaxGradConfig, lrnGradConfig, maxGradConfig, maxGradConfig, maximumGradConfig, maxPool3DGradConfig, maxPoolGradConfig, meanGradConfig, minGradConfig, minimumGradConfig, mirrorPadGradConfig, modGradConfig, multiplyGradConfig, negGradConfig, oneHotGradConfig, onesLikeGradConfig, packGradConfig, padV2GradConfig, padV2GradConfig, powGradConfig, preluGradConfig, prodGradConfig, reciprocalGradConfig, relu6GradConfig, reluGradConfig, reshapeGradConfig, resizeBilinearGradConfig, resizeNearestNeighborGradConfig, reverseGradConfig, roundGradConfig, rsqrtGradConfig, selectGradConfig, seluGradConfig, sigmoidGradConfig, signGradConfig, sinGradConfig, sinhGradConfig, sliceGradConfig, softmaxGradConfig, softplusGradConfig, spaceToBatchNDGradConfig, spaceToBatchNDGradConfig, splitVGradConfig, splitVGradConfig, sqrtGradConfig, squaredDifferenceGradConfig, squareGradConfig, stepGradConfig, subGradConfig, sumGradConfig, tanGradConfig, tanhGradConfig, tileGradConfig, transposeGradConfig, unpackGradConfig, unsortedSegmentSumGradConfig, zerosLikeGradConfig ]; for (const gradientConfig of gradConfigs) { registerGradient(gradientConfig); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Helper function used by many of the Constraints to find the L2Norms. */ function calcL2Norms(w, axis) { return tidy$1(() => tfc.sqrt(tfc.sum(tfc.mul(w, w), axis, true))); } /** * Base class for functions that impose constraints on weight values * * @doc { * heading: 'Constraints', * subheading: 'Classes', * namespace: 'constraints' * } */ class Constraint extends serialization.Serializable { getConfig() { return {}; } } class MaxNorm extends Constraint { constructor(args) { super(); this.defaultMaxValue = 2; this.defaultAxis = 0; this.maxValue = args.maxValue != null ? args.maxValue : this.defaultMaxValue; this.axis = args.axis != null ? args.axis : this.defaultAxis; } apply(w) { return tidy$1(() => { const norms = calcL2Norms(w, this.axis); const desired = tfc.clipByValue(norms, 0, this.maxValue); return tfc.mul(w, tfc.div(desired, tfc.add(epsilon(), norms))); }); } getConfig() { return { maxValue: this.maxValue, axis: this.axis }; } } /** @nocollapse */ MaxNorm.className = 'MaxNorm'; serialization.registerClass(MaxNorm); class UnitNorm extends Constraint { constructor(args) { super(); this.defaultAxis = 0; this.axis = args.axis != null ? args.axis : this.defaultAxis; } apply(w) { return tidy$1(() => tfc.div(w, tfc.add(epsilon(), calcL2Norms(w, this.axis)))); } getConfig() { return { axis: this.axis }; } } /** @nocollapse */ UnitNorm.className = 'UnitNorm'; serialization.registerClass(UnitNorm); class NonNeg extends Constraint { apply(w) { return tfc.relu(w); } } /** @nocollapse */ NonNeg.className = 'NonNeg'; serialization.registerClass(NonNeg); class MinMaxNorm extends Constraint { constructor(args) { super(); this.defaultMinValue = 0.0; this.defaultMaxValue = 1.0; this.defaultRate = 1.0; this.defaultAxis = 0; this.minValue = args.minValue != null ? args.minValue : this.defaultMinValue; this.maxValue = args.maxValue != null ? args.maxValue : this.defaultMaxValue; this.rate = args.rate != null ? args.rate : this.defaultRate; this.axis = args.axis != null ? args.axis : this.defaultAxis; } apply(w) { return tidy$1(() => { const norms = calcL2Norms(w, this.axis); const desired = tfc.add(tfc.mul(this.rate, tfc.clipByValue(norms, this.minValue, this.maxValue)), tfc.mul(1.0 - this.rate, norms)); return tfc.mul(w, tfc.div(desired, tfc.add(epsilon(), norms))); }); } getConfig() { return { minValue: this.minValue, maxValue: this.maxValue, rate: this.rate, axis: this.axis }; } } /** @nocollapse */ MinMaxNorm.className = 'MinMaxNorm'; serialization.registerClass(MinMaxNorm); // Maps the JavaScript-like identifier keys to the corresponding registry // symbols. const CONSTRAINT_IDENTIFIER_REGISTRY_SYMBOL_MAP = { 'maxNorm': 'MaxNorm', 'minMaxNorm': 'MinMaxNorm', 'nonNeg': 'NonNeg', 'unitNorm': 'UnitNorm' }; function serializeConstraint(constraint) { return serializeKerasObject(constraint); } function deserializeConstraint(config, customObjects = {}) { return deserializeKerasObject(config, serialization.SerializationMap.getMap().classNameMap, customObjects, 'constraint'); } function getConstraint(identifier) { if (identifier == null) { return null; } if (typeof identifier === 'string') { const className = identifier in CONSTRAINT_IDENTIFIER_REGISTRY_SYMBOL_MAP ? CONSTRAINT_IDENTIFIER_REGISTRY_SYMBOL_MAP[identifier] : identifier; const config = { className, config: {} }; return deserializeConstraint(config); } else if (identifier instanceof Constraint) { return identifier; } else { return deserializeConstraint(identifier); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * 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](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf) * * @doc {heading: 'Constraints',namespace: 'constraints'} */ function maxNorm(args) { return new MaxNorm(args); } /** * Constrains the weights incident to each hidden unit to have unit norm. * * @doc {heading: 'Constraints', namespace: 'constraints'} */ function unitNorm(args) { return new UnitNorm(args); } /** * Constrains the weight to be non-negative. * * @doc {heading: 'Constraints', namespace: 'constraints'} */ function nonNeg() { return new NonNeg(); } /** @doc {heading: 'Constraints', namespace: 'constraints'} */ function minMaxNorm(config) { return new MinMaxNorm(config); } var exports_constraints = { __proto__: null, maxNorm: maxNorm, minMaxNorm: minMaxNorm, nonNeg: nonNeg, unitNorm: unitNorm }; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Initializer that generates tensors initialized to 0. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function zeros() { return new Zeros(); } /** * Initializer that generates tensors initialized to 1. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function ones() { return new Ones(); } /** * Initializer that generates values initialized to some constant. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function constant(args) { return new Constant(args); } /** * Initializer that generates random values initialized to a uniform * distribution. * * Values will be distributed uniformly between the configured minval and * maxval. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function randomUniform(args) { return new RandomUniform(args); } /** * Initializer that generates random values initialized to a normal * distribution. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function randomNormal(args) { return new RandomNormal(args); } /** * 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. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function truncatedNormal(args) { return new TruncatedNormal(args); } /** * Initializer that generates the identity matrix. * Only use for square 2D matrices. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function identity(args) { return new Identity$1(args); } /** * 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)`. * * @doc {heading: 'Initializers',namespace: 'initializers'} */ function varianceScaling(config) { return new VarianceScaling(config); } /** * 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. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function glorotUniform(args) { return new GlorotUniform(args); } /** * 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 * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function glorotNormal(args) { return new GlorotNormal(args); } /** * 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 * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function heNormal(args) { return new HeNormal(args); } /** * He uniform initializer. * * It draws samples from a uniform distribution within [-limit, limit] * where `limit` is `sqrt(6 / fan_in)` * where `fanIn` is the number of input units in the weight tensor. * * Reference: * He et al., http://arxiv.org/abs/1502.01852 * * @doc {heading: 'Initializers',namespace: 'initializers'} */ function heUniform(args) { return new HeUniform(args); } /** * 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](https://arxiv.org/abs/1706.02515) * [Efficient Backprop](http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf) * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function leCunNormal(args) { return new LeCunNormal(args); } /** * LeCun uniform initializer. * * It draws samples from a uniform distribution in the interval * `[-limit, limit]` with `limit = sqrt(3 / fanIn)`, * where `fanIn` is the number of input units in the weight tensor. * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function leCunUniform(args) { return new LeCunUniform(args); } /** * Initializer that generates a random orthogonal matrix. * * Reference: * [Saxe et al., http://arxiv.org/abs/1312.6120](http://arxiv.org/abs/1312.6120) * * @doc {heading: 'Initializers', namespace: 'initializers'} */ function orthogonal(args) { return new Orthogonal(args); } var exports_initializers = { __proto__: null, constant: constant, glorotNormal: glorotNormal, glorotUniform: glorotUniform, heNormal: heNormal, heUniform: heUniform, identity: identity, leCunNormal: leCunNormal, leCunUniform: leCunUniform, ones: ones, orthogonal: orthogonal, randomNormal: randomNormal, randomUniform: randomUniform, truncatedNormal: truncatedNormal, varianceScaling: varianceScaling, zeros: zeros }; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Turn any Scalar values in a Logs object into actual number values. * * @param logs The `Logs` object to be resolved in place. */ async function resolveScalarsInLogs(logs) { if (logs == null) { return; } const promises = []; const keys = []; const scalarsToDispose = []; for (const key in logs) { const value = logs[key]; if (typeof value !== 'number') { const valueScalar = value; promises.push(valueScalar.data()); keys.push(key); scalarsToDispose.push(valueScalar); } } if (promises.length > 0) { const values = await Promise.all(promises); for (let i = 0; i < values.length; ++i) { logs[keys[i]] = values[i][0]; } // Dispose the original scalar tensors. dispose(scalarsToDispose); } } /** * Dispose all Tensors in an UnresolvedLogs object. * * @param logs An `UnresolvedLogs` object potentially containing `tf.Tensor`s in * places where the values can be `tf.Tensor` or `number`. */ function disposeTensorsInLogs(logs) { if (logs == null) { return; } for (const key in logs) { const value = logs[key]; if (typeof value !== 'number') { value.dispose(); } } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** Verbosity logging level when fitting a model. */ var ModelLoggingVerbosity; (function (ModelLoggingVerbosity) { ModelLoggingVerbosity[ModelLoggingVerbosity["SILENT"] = 0] = "SILENT"; ModelLoggingVerbosity[ModelLoggingVerbosity["VERBOSE"] = 1] = "VERBOSE"; })(ModelLoggingVerbosity || (ModelLoggingVerbosity = {})); /** How often to yield to the main thread when training (in ms). */ const DEFAULT_YIELD_EVERY_MS = 125; /** * Abstract base class used to build new callbacks. * * The `logs` dictionary that callback methods take as argument will contain * keys for quantities relevant to the current batch or epoch. * * Currently, the `.fit()` method of the `Sequential` model class * will include the following quantities in the `logs` that * it passes to its callbacks: * * onEpochEnd: Logs include `acc` and `loss`, and optionally include `valLoss` * (if validation is enabled in `fit`), and `valAcc` (if validation and * accuracy monitoring are enabled). * onBatchBegin: Logs include `size`, the number of samples in the current * batch. * onBatchEnd: Logs include `loss`, and optionally `acc` (if accuracy monitoring * is enabled). */ class BaseCallback { constructor() { // TODO(michaelterry): This type is a best guess. this.validationData = null; } setParams(params) { this.params = params; } async onEpochBegin(epoch, logs) { } async onEpochEnd(epoch, logs) { } async onBatchBegin(batch, logs) { } async onBatchEnd(batch, logs) { } async onTrainBegin(logs) { } async onTrainEnd(logs) { } // LayersModel needs to call Callback.setModel(), but cannot actually depend // on Callback because that creates a cyclic dependency. Providing this no-op // method on BaseCallback breaks the cycle: this way LayersModel can depend on // BaseCallback but not on Callback. The argument is typed as `Container` // (the superclass of LayersModel) to avoid recapitulating the cycle. Callback // overrides this method and enforces that the argument is really a // LayersModel. setModel(model) { // Do nothing. Use Callback instead of BaseCallback to track the model. } } /** * Container abstracting a list of callbacks. */ class CallbackList { // TODO(cais): When the need arises, uncomment the following lines and // implement the queue for time values. // private deltaTBatch: number; // private deltaTsBatchBegin: Array; // private deltaTsBatchEnd: Array; /** * Constructor of CallbackList. * @param callbacks Array of `Callback` instances. * @param queueLength Queue length for keeping running statistics over * callback execution time. */ constructor(callbacks, queueLength = 10) { // TODO(cais): Make use of queueLength when implementing the queue for time // values. if (callbacks == null) { callbacks = []; } this.callbacks = callbacks; this.queueLength = queueLength; } append(callback) { this.callbacks.push(callback); } setParams(params) { for (const callback of this.callbacks) { callback.setParams(params); } } setModel(model) { for (const callback of this.callbacks) { callback.setModel(model); } } /** * Called at the start of an epoch. * @param epoch Index of epoch. * @param logs Dictionary of logs. */ async onEpochBegin(epoch, logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onEpochBegin(epoch, logs); } } /** * Called at the end of an epoch. * @param epoch Index of epoch. * @param logs Dictionary of logs. */ async onEpochEnd(epoch, logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onEpochEnd(epoch, logs); } } /** * Called right before processing a batch. * @param batch Index of batch within the current epoch. * @param logs Dictionary of logs. */ async onBatchBegin(batch, logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onBatchBegin(batch, logs); } } /** * Called at the end of a batch. * @param batch Index of batch within the current epoch. * @param logs Dictionary of logs. */ async onBatchEnd(batch, logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onBatchEnd(batch, logs); } } /** * Called at the beginning of training. * @param logs Dictionary of logs. */ async onTrainBegin(logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onTrainBegin(logs); } } /** * Called at the end of training. * @param logs Dictionary of logs. */ async onTrainEnd(logs) { if (logs == null) { logs = {}; } for (const callback of this.callbacks) { await callback.onTrainEnd(logs); } } } /** * Callback that accumulates epoch averages of metrics. * * This callback is automatically applied to every LayersModel. */ class BaseLogger extends BaseCallback { constructor() { super(); } async onEpochBegin(epoch) { this.seen = 0; this.totals = {}; } async onBatchEnd(batch, logs) { if (logs == null) { logs = {}; } const batchSize = logs['size'] == null ? 0 : logs['size']; this.seen += batchSize; for (const key in logs) { const value = logs[key]; if (typeof value === 'number') { if (!this.totals.hasOwnProperty(key)) { this.totals[key] = 0; } this.totals[key] = this.totals[key] + value * batchSize; } else { let oldTotalsToDispose; if (key in this.totals) { oldTotalsToDispose = this.totals[key]; } else { this.totals[key] = 0; } const total = tidy$1(() => add$3((this.totals[key]), mul$1(value, batchSize))); this.totals[key] = total; if (oldTotalsToDispose != null) { oldTotalsToDispose.dispose(); } } } } async onEpochEnd(epoch, logs) { if (logs != null) { for (const key of this.params['metrics']) { if (this.totals[key] == null) { continue; } if (typeof this.totals[key] === 'number') { logs[key] = this.totals[key] / this.seen; } else { tidy$1(() => { const log = mul$1(div$1(1, this.seen), this.totals[key]); logs[key] = log; this.totals[key].dispose(); keep(logs[key]); }); } } } } } /** * Callback that records events into a `History` object. This callback is * automatically applied to every TF.js Layers model. The `History` object * gets returned by the `fit` method of models. */ class History extends BaseCallback { async onTrainBegin(logs) { this.epoch = []; this.history = {}; } async onEpochEnd(epoch, logs) { if (logs == null) { logs = {}; } this.epoch.push(epoch); for (const key in logs) { if (this.history[key] == null) { this.history[key] = []; } this.history[key].push(logs[key]); } } /** * Await the values of all losses and metrics. */ async syncData() { const promises = []; const keys = []; const indices = []; for (const key in this.history) { const valueArray = this.history[key]; for (let i = 0; i < valueArray.length; ++i) { if (typeof valueArray[i] !== 'number') { const valueScalar = valueArray[i]; promises.push(valueScalar.data()); keys.push(key); indices.push(i); } } } const values = await Promise.all(promises); for (let n = 0; n < values.length; ++n) { const tensorToDispose = this.history[keys[n]][indices[n]]; tensorToDispose.dispose(); this.history[keys[n]][indices[n]] = values[n][0]; } } } /** * Custom callback for training. */ class CustomCallback extends BaseCallback { constructor(args, yieldEvery) { super(); this.currentEpoch = 0; this.nowFunc = args.nowFunc; this.nextFrameFunc = args.nextFrameFunc || nextFrame; this.yieldEvery = yieldEvery || 'auto'; if (this.yieldEvery === 'auto') { this.yieldEvery = DEFAULT_YIELD_EVERY_MS; } if (this.yieldEvery === 'never' && args.onYield != null) { throw new Error('yieldEvery is `never` but you provided an `onYield` callback. ' + 'Either change `yieldEvery` or remove the callback'); } if (util.isNumber(this.yieldEvery)) { // Decorate `maybeWait` so it will be called at most once every // `yieldEvery` ms. this.maybeWait = debounce(this.maybeWait.bind(this), this.yieldEvery, this.nowFunc); } this.trainBegin = args.onTrainBegin; this.trainEnd = args.onTrainEnd; this.epochBegin = args.onEpochBegin; this.epochEnd = args.onEpochEnd; this.batchBegin = args.onBatchBegin; this.batchEnd = args.onBatchEnd; this.yield = args.onYield; } async maybeWait(epoch, batch, logs) { const ps = []; if (this.yield != null) { await resolveScalarsInLogs(logs); ps.push(this.yield(epoch, batch, logs)); } ps.push(this.nextFrameFunc()); await Promise.all(ps); } async onEpochBegin(epoch, logs) { this.currentEpoch = epoch; if (this.epochBegin != null) { await resolveScalarsInLogs(logs); await this.epochBegin(epoch, logs); } } async onEpochEnd(epoch, logs) { const ps = []; if (this.epochEnd != null) { await resolveScalarsInLogs(logs); ps.push(this.epochEnd(epoch, logs)); } if (this.yieldEvery === 'epoch') { ps.push(this.nextFrameFunc()); } await Promise.all(ps); } async onBatchBegin(batch, logs) { if (this.batchBegin != null) { await resolveScalarsInLogs(logs); await this.batchBegin(batch, logs); } } async onBatchEnd(batch, logs) { const ps = []; if (this.batchEnd != null) { await resolveScalarsInLogs(logs); ps.push(this.batchEnd(batch, logs)); } if (this.yieldEvery === 'batch') { ps.push(this.nextFrameFunc()); } else if (util.isNumber(this.yieldEvery)) { ps.push(this.maybeWait(this.currentEpoch, batch, logs)); } await Promise.all(ps); } async onTrainBegin(logs) { if (this.trainBegin != null) { await resolveScalarsInLogs(logs); await this.trainBegin(logs); } } async onTrainEnd(logs) { if (this.trainEnd != null) { await resolveScalarsInLogs(logs); await this.trainEnd(logs); } } } /** * Standardize callbacks or configurations of them to an Array of callbacks. */ function standardizeCallbacks(callbacks, yieldEvery) { if (callbacks == null) { callbacks = {}; } if (callbacks instanceof BaseCallback) { return [callbacks]; } if (Array.isArray(callbacks) && callbacks[0] instanceof BaseCallback) { return callbacks; } // Convert custom callback configs to custom callback objects. const callbackConfigs = toList(callbacks); return callbackConfigs.map(callbackConfig => new CustomCallback(callbackConfig, yieldEvery)); } /** * A global registry for callback constructors to be used during * LayersModel.fit(). */ class CallbackConstructorRegistry { /** * Blocks public access to constructor. */ constructor() { } /** * Register a tf.LayersModel.fit() callback constructor. * * The registered callback constructor will be used to instantiate * callbacks for every tf.LayersModel.fit() call afterwards. * * @param verbosityLevel Level of verbosity at which the `callbackConstructor` * is to be reigstered. * @param callbackConstructor A no-arg constructor for `tf.Callback`. * @throws Error, if the same callbackConstructor has been registered before, * either at the same or a different `verbosityLevel`. */ static registerCallbackConstructor(verbosityLevel, callbackConstructor) { util.assert(verbosityLevel >= 0 && Number.isInteger(verbosityLevel), () => `Verbosity level is expected to be an integer >= 0, ` + `but got ${verbosityLevel}`); CallbackConstructorRegistry.checkForDuplicate(callbackConstructor); if (CallbackConstructorRegistry.constructors[verbosityLevel] == null) { CallbackConstructorRegistry.constructors[verbosityLevel] = []; } CallbackConstructorRegistry.constructors[verbosityLevel].push(callbackConstructor); } static checkForDuplicate(callbackConstructor) { for (const levelName in CallbackConstructorRegistry.constructors) { const constructors = CallbackConstructorRegistry.constructors[+levelName]; constructors.forEach(ctor => { if (ctor === callbackConstructor) { throw new ValueError('Duplicate callback constructor.'); } }); } } /** * Clear all registered callback constructors. */ static clear() { CallbackConstructorRegistry.constructors = {}; } /** * Create callbacks using the registered callback constructors. * * Given `verbosityLevel`, all constructors registered at that level or above * will be called and the instantiated callbacks will be used. * * @param verbosityLevel: Level of verbosity. */ static createCallbacks(verbosityLevel) { const constructors = []; for (const levelName in CallbackConstructorRegistry.constructors) { const level = +levelName; if (verbosityLevel >= level) { constructors.push(...CallbackConstructorRegistry.constructors[level]); } } return constructors.map(ctor => new ctor()); } } CallbackConstructorRegistry.constructors = {}; function configureCallbacks(callbacks, verbose, epochs, initialEpoch, numTrainSamples, stepsPerEpoch, batchSize, doValidation, callbackMetrics) { const history = new History(); const actualCallbacks = [ new BaseLogger(), ...CallbackConstructorRegistry.createCallbacks(verbose) ]; if (callbacks != null) { actualCallbacks.push(...callbacks); } actualCallbacks.push(history); const callbackList = new CallbackList(actualCallbacks); // TODO(cais): Figure out when this LayersModel instance can have a // dynamically // set property called 'callback_model' as in PyKeras. callbackList.setParams({ epochs, initialEpoch, samples: numTrainSamples, steps: stepsPerEpoch, batchSize, verbose, doValidation, metrics: callbackMetrics, }); return { callbackList, history }; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Instantiate a layer from a config dictionary. * @param config dict of the form {class_name: str, config: dict} * @param customObjects dict mapping class names (or function names) * of custom (non-Keras) objects to class/functions * @param fastWeightInit Optional flag to use fast weight initialization * during deserialization. This is applicable to cases in which * the initialization will be immediately overwritten by loaded weight * values. Default: `false`. * @returns Layer instance (may be LayersModel, Sequential, Layer...) */ function deserialize(config, customObjects = {}, fastWeightInit = false) { return deserializeKerasObject(config, serialization.SerializationMap.getMap().classNameMap, customObjects, 'layer', fastWeightInit); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Normalizes a tensor wrt the L2 norm alongside the specified axis. * @param x * @param axis Axis along which to perform normalization. */ function l2Normalize(x, axis) { return tidy$1(() => { if (x.dtype !== 'float32') { x = tfc.cast(x, 'float32'); } const squareSum = tfc.sum(square$1(x), axis, true); const epsilonTensor = tfc.fill(squareSum.shape, epsilon()); const norm = tfc.sqrt(tfc.maximum(squareSum, epsilonTensor)); return tfc.div(x, norm); }); } function meanSquaredError$1(yTrue, yPred) { return tidy$1(() => tfc.mean(square$1(tfc.sub(yPred, yTrue)), -1)); } function meanAbsoluteError$1(yTrue, yPred) { return tidy$1(() => tfc.mean(tfc.abs(tfc.sub(yPred, yTrue)), -1)); } function meanAbsolutePercentageError$1(yTrue, yPred) { return tidy$1(() => { const diff = tfc.sub(yTrue, yPred); const clippedTrue = tfc.clipByValue(tfc.abs(yTrue), epsilon(), Number.MAX_VALUE); const absResult = tfc.abs(tfc.div(diff, clippedTrue)); return tfc.mul(100, tfc.mean(absResult, -1)); }); } function meanSquaredLogarithmicError(yTrue, yPred) { return tidy$1(() => { const clippedPred = tfc.clipByValue(yPred, epsilon(), Number.MAX_VALUE); const firstLog = tfc.log(tfc.add(1, clippedPred)); const clippedTrue = tfc.clipByValue(yTrue, epsilon(), Number.MAX_VALUE); const secondLog = tfc.log(tfc.add(1, clippedTrue)); return tfc.mean(square$1(tfc.sub(firstLog, secondLog)), -1); }); } function squaredHinge(yTrue, yPred) { return tidy$1(() => { const maxResult = tfc.maximum(0, tfc.sub(1, tfc.mul(yTrue, yPred))); return tfc.mean(square$1(maxResult), -1); }); } function hinge(yTrue, yPred) { return tidy$1(() => { const maxResult = tfc.maximum(0, tfc.sub(1, tfc.mul(yTrue, yPred))); return tfc.mean(maxResult, -1); }); } function categoricalHinge(yTrue, yPred) { return tidy$1(() => { const pos = tfc.sum(tfc.mul(yTrue, yPred), -1); const neg = tfc.max(tfc.mul(tfc.sub(1, yTrue), yPred), -1); return tfc.maximum(0, tfc.add(1, tfc.sub(neg, pos))); }); } /** * Logarithm of the hyperbolic cosine of the prediction error. * * `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small `x` and * to `abs(x) - log(2)` for large `x`. This means that 'logcosh' works mostly * like the mean squared error, but will not be so strongly affected by the * occasional wildly incorrect prediction. */ function logcosh(yTrue, yPred) { return tidy$1(() => { const log2 = Math.log(2); const predictionDiff = tfc.sub(yPred, yTrue); const logcoshResult = tfc.sub(tfc.add(predictionDiff, tfc.softplus(tfc.mul(-2, predictionDiff))), log2); return tfc.mean(logcoshResult, -1); }); } function categoricalCrossentropy$2(target, output, fromLogits = false) { return tidy$1(() => { if (fromLogits) { output = tfc.softmax(output); } else { // scale preds so that the class probabilities of each sample sum to 1. const outputSum = tfc.sum(output, output.shape.length - 1, true); output = tfc.div(output, outputSum); } output = tfc.clipByValue(output, epsilon(), 1 - epsilon()); return tfc.neg(tfc.sum(tfc.mul(tfc.cast(target, 'float32'), tfc.log(output)), output.shape.length - 1)); }); } /** * Categorical crossentropy with integer targets. * * @param target An integer tensor. * @param output A tensor resulting from a softmax (unless `fromLogits` is * `true`, in which case `output` is expected to be the logits). * @param fromLogits Boolean, whether `output` is the result of a softmax, or is * a tensor of logits. */ function sparseCategoricalCrossentropy$1(target, output, fromLogits = false) { return tidy$1(() => { const flatTarget = tfc.cast(tfc.floor(flatten$2(target)), 'int32'); output = tfc.clipByValue(output, epsilon(), 1 - epsilon()); const outputShape = output.shape; const oneHotTarget = tfc.reshape(tfc.oneHot(flatTarget, outputShape[outputShape.length - 1]), outputShape); return categoricalCrossentropy$2(oneHotTarget, output, fromLogits); }); } /** * From TensorFlow's implementation in nn_impl.py: * * For brevity, let `x = logits`, `z = labels`. The logistic loss is * z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x)) * = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x))) * = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x))) * = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x)) * = (1 - z) * x + log(1 + exp(-x)) * = x - x * z + log(1 + exp(-x)) * For x < 0, to avoid overflow in exp(-x), we reformulate the above * x - x * z + log(1 + exp(-x)) * = log(exp(x)) - x * z + log(1 + exp(-x)) * = - x * z + log(1 + exp(x)) * Hence, to ensure stability and avoid overflow, the implementation uses this * equivalent formulation * max(x, 0) - x * z + log(1 + exp(-abs(x))) * * @param labels The labels. * @param logits The logits. */ function sigmoidCrossEntropyWithLogits(labels, logits) { if (!util.arraysEqual(labels.shape, logits.shape)) { throw new ValueError(`logits and labels must have the same shape, but got shapes ` + `${JSON.stringify(labels.shape)} and ${JSON.stringify(logits.shape)}`); } return tidy$1(() => { // The logistic loss formula from above is // x - x * z + log(1 + exp(-x)) // For x < 0, a more numerically stable formula is // -x * z + log(1 + exp(x)) // Note that these two expressions can be combined into the following: // max(x, 0) - x * z + log(1 + exp(-abs(x))) const reluLogits = tfc.relu(logits); const negAbsLogits = tfc.neg(tfc.abs(logits)); return tfc.add(tfc.sub(reluLogits, tfc.mul(logits, labels)), tfc.log1p(tfc.exp(negAbsLogits))); }); } function binaryCrossentropy$2(yTrue, yPred) { return tidy$1(() => { let y; y = tfc.clipByValue(yPred, epsilon(), 1 - epsilon()); y = tfc.log(tfc.div(y, tfc.sub(1, y))); return tfc.mean(sigmoidCrossEntropyWithLogits(yTrue, y), -1); }); } function kullbackLeiblerDivergence(yTrue, yPred) { return tidy$1(() => { const clippedTrue = tfc.clipByValue(yTrue, epsilon(), 1); const clippedPred = tfc.clipByValue(yPred, epsilon(), 1); return tfc.sum(tfc.mul(yTrue, tfc.log(tfc.div(clippedTrue, clippedPred))), -1); }); } function poisson(yTrue, yPred) { return tidy$1(() => { const logPred = tfc.log(tfc.add(epsilon(), yPred)); return tfc.mean(tfc.sub(yPred, tfc.mul(yTrue, logPred)), -1); }); } function cosineProximity$1(yTrue, yPred) { return tidy$1(() => { const trueNormalized = l2Normalize(yTrue, -1); const predNormalized = l2Normalize(yPred, -1); const trueXPred = tfc.mul(trueNormalized, predNormalized); return tfc.neg(tfc.sum(trueXPred, -1)); }); } // TODO(michaelterry): Add deserialize() function. const lossesMap = { meanSquaredError: meanSquaredError$1, meanAbsoluteError: meanAbsoluteError$1, meanAbsolutePercentageError: meanAbsolutePercentageError$1, meanSquaredLogarithmicError, squaredHinge, hinge, categoricalHinge, logcosh, categoricalCrossentropy: categoricalCrossentropy$2, sparseCategoricalCrossentropy: sparseCategoricalCrossentropy$1, binaryCrossentropy: binaryCrossentropy$2, kullbackLeiblerDivergence, poisson, cosineProximity: cosineProximity$1 }; // Porting note: This diverges from the PyKeras implementation and may need to // change based on (de)serialization requirements. function get$1(identifierOrFn) { if (typeof identifierOrFn === 'string') { if (identifierOrFn in lossesMap) { return lossesMap[identifierOrFn]; } let errMsg = `Unknown loss ${identifierOrFn}`; if (identifierOrFn.toLowerCase().includes('softmaxcrossentropy')) { errMsg = `Unknown loss ${identifierOrFn}. ` + 'Use "categoricalCrossentropy" as the string name for ' + 'tf.losses.softmaxCrossEntropy'; } throw new ValueError(errMsg); } else { return identifierOrFn; } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function binaryAccuracy$1(yTrue, yPred) { return tidy$1(() => { const threshold = tfc.mul(.5, tfc.onesLike(yPred)); const yPredThresholded = cast$1(tfc.greater(yPred, threshold), yTrue.dtype); return tfc.mean(tfc.equal(yTrue, yPredThresholded), -1); }); } function categoricalAccuracy$1(yTrue, yPred) { return tidy$1(() => cast$1(tfc.equal(tfc.argMax(yTrue, -1), tfc.argMax(yPred, -1)), 'float32')); } function truePositives(yTrue, yPred) { return tidy$1(() => { return tfc.cast(tfc.sum(tfc.logicalAnd(tfc.equal(yTrue, 1), tfc.equal(yPred, 1))), 'float32'); }); } function falseNegatives(yTrue, yPred) { return tidy$1(() => { return tfc.cast(tfc.sum(tfc.logicalAnd(tfc.equal(yTrue, 1), tfc.equal(yPred, 0))), 'float32'); }); } function falsePositives(yTrue, yPred) { return tidy$1(() => { return tfc.cast(tfc.sum(tfc.logicalAnd(tfc.equal(yTrue, 0), tfc.equal(yPred, 1))), 'float32'); }); } function precision$1(yTrue, yPred) { return tidy$1(() => { const tp = truePositives(yTrue, yPred); const fp = falsePositives(yTrue, yPred); const denominator = tfc.add(tp, fp); return tfc.cast(tfc.where(tfc.greater(denominator, 0), tfc.div(tp, denominator), 0), 'float32'); }); } function recall$1(yTrue, yPred) { return tidy$1(() => { const tp = truePositives(yTrue, yPred); const fn = falseNegatives(yTrue, yPred); const denominator = tfc.add(tp, fn); return tfc.cast(tfc.where(tfc.greater(denominator, 0), tfc.div(tp, denominator), 0), 'float32'); }); } function binaryCrossentropy$1(yTrue, yPred) { return binaryCrossentropy$2(yTrue, yPred); } function sparseCategoricalAccuracy$1(yTrue, yPred) { if (yTrue.rank === yPred.rank) { yTrue = tfc.squeeze(yTrue, [yTrue.rank - 1]); } yPred = tfc.argMax(yPred, -1); if (yPred.dtype !== yTrue.dtype) { yPred = tfc.cast(yPred, yTrue.dtype); } return tfc.cast(tfc.equal(yTrue, yPred), 'float32'); } // Aliases. const mse$1 = meanSquaredError$1; const MSE$1 = meanSquaredError$1; const mae = meanAbsoluteError$1; const MAE = meanAbsoluteError$1; const mape$1 = meanAbsolutePercentageError$1; const MAPE$1 = meanAbsolutePercentageError$1; const categoricalCrossentropy$1 = categoricalCrossentropy$2; const cosine = cosineProximity$1; const sparseCategoricalCrossentropy = sparseCategoricalCrossentropy$1; // TODO(cais, nielsene): Add serialize(). const metricsMap = { binaryAccuracy: binaryAccuracy$1, categoricalAccuracy: categoricalAccuracy$1, precision: precision$1, categoricalCrossentropy: categoricalCrossentropy$1, sparseCategoricalCrossentropy, mse: mse$1, MSE: MSE$1, mae, MAE, mape: mape$1, MAPE: MAPE$1, cosine }; function get(identifier) { if (typeof identifier === 'string' && identifier in metricsMap) { return metricsMap[identifier]; } else if (typeof identifier !== 'string' && identifier != null) { return identifier; } else { throw new ValueError(`Unknown metric ${identifier}`); } } /** * Get the shortcut function name. * * If the fn name is a string, * directly return the string name. * If the function is included in metricsMap or lossesMap, * return key of the map. * - If the function relative to multiple keys, * return the first found key as the function name. * - If the function exists in both lossesMap and metricsMap, * search lossesMap first. * If the function is not included in metricsMap or lossesMap, * return the function name. * * @param fn loss function, metric function, or short cut name. * @returns Loss or Metric name in string. */ function getLossOrMetricName(fn) { assert$1(fn !== null, `Unknown LossOrMetricFn ${fn}`); if (typeof fn === 'string') { return fn; } else { let fnName; for (const key of Object.keys(lossesMap)) { if (lossesMap[key] === fn) { fnName = key; break; } } if (fnName !== undefined) { return fnName; } for (const key of Object.keys(metricsMap)) { if (metricsMap[key] === fn) { fnName = key; break; } } if (fnName !== undefined) { return fnName; } return fn.name; } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // Add (de)serialize() // Porting note: This diverges from the PyKeras implementation and may need to // change based on (de)serialization requirements. function getOptimizer(identifier) { const optimizerMap = { 'Adagrad': () => train.adagrad(0.01), 'Adadelta': () => train.adadelta(1, 0.95, epsilon()), 'Adam': () => train.adam(0.001, 0.9, 0.999, epsilon()), 'Adamax': () => train.adamax(0.002, 0.9, 0.999, epsilon(), 0), 'RMSProp': () => train.rmsprop(0.001, 0.9, 0, epsilon()), 'SGD': () => train.sgd(0.01) }; optimizerMap['adagrad'] = optimizerMap['Adagrad']; optimizerMap['adadelta'] = optimizerMap['Adadelta']; optimizerMap['adam'] = optimizerMap['Adam']; optimizerMap['adamax'] = optimizerMap['Adamax']; optimizerMap['rmsprop'] = optimizerMap['RMSProp']; optimizerMap['sgd'] = optimizerMap['SGD']; if (identifier in optimizerMap) { return optimizerMap[identifier](); } throw new ValueError(`Unknown Optimizer ${identifier}`); } /** * @license * Copyright 2019 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** Utility functions related to user-defined metadata. */ // Maximum recommended serialized size for user-defined metadata. // Beyond this limit, a warning message will be printed during model loading and // saving. const MAX_USER_DEFINED_METADATA_SERIALIZED_LENGTH = 1 * 1024 * 1024; /** * Check validity of user-defined metadata. * * @param userDefinedMetadata * @param modelName Name of the model that the user-defined metadata belongs to. * Used during construction of error messages. * @param checkSize Whether to check the size of the metadata is under * recommended limit. Default: `false`. If `true`, will try stringify the * JSON object and print a console warning if the serialzied size is above the * limit. * @throws Error if `userDefinedMetadata` is not a plain JSON object. */ function checkUserDefinedMetadata(userDefinedMetadata, modelName, checkSize = false) { if (userDefinedMetadata == null || typeof userDefinedMetadata !== 'object' || Object.getPrototypeOf(userDefinedMetadata) !== Object.prototype || !plainObjectCheck(userDefinedMetadata)) { throw new Error('User-defined metadata is expected to be a JSON object, but is not.'); } if (checkSize) { const out = JSON.stringify(userDefinedMetadata); if (out.length > MAX_USER_DEFINED_METADATA_SERIALIZED_LENGTH) { console.warn(`User-defined metadata of model "${modelName}" is too large in ` + `size (length=${out.length} when serialized). It is not ` + `recommended to store such large objects in user-defined metadata. ` + `Please make sure its serialized length is <= ` + `${MAX_USER_DEFINED_METADATA_SERIALIZED_LENGTH}.`); } } } /** * Check if an input is plain JSON object or any valid subfield of it. * * @param x The input to be checked. * @param assertObject Whether to assert `x` is a JSON object, i.e., reject * cases of arrays and primitives. * @return Returns `true` if and only if `x` is a plain JSON object, * a JSON-valid primitive including string, number, boolean and null, * or an array of the said types. */ // tslint:disable-next-line:no-any function plainObjectCheck(x) { if (x === null) { // Note: typeof `null` is 'object', and `null` is valid in JSON. return true; } else if (typeof x === 'object') { if (Object.getPrototypeOf(x) === Object.prototype) { // `x` is a JavaScript object and its prototype is Object. const keys = Object.keys(x); for (const key of keys) { if (typeof key !== 'string') { // JSON keys must be strings. return false; } if (!plainObjectCheck(x[key])) { // Recursive call. return false; } } return true; } else { // `x` is a JavaScript object but its prototype is not Object. if (Array.isArray(x)) { // `x` is a JavaScript array. for (const item of x) { if (!plainObjectCheck(item)) { // Recursive call. return false; } } return true; } else { // `x` is a JavaScript object and its prototype is not Object, // and it's not an Array. I.e., it's a complex object such as // `Error` and `Date`. return false; } } } else { // `x` is not a JavaScript object or `null`. const xType = typeof x; return xType === 'string' || xType === 'number' || xType === 'boolean'; } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Print the summary of a LayersModel object. * * @param model tf.LayersModel instance. * @param lineLength Total length of printed lines. Set this to adapt to the * display to different terminal or console sizes. * @param positions Relative or absolute positions of log elements in each * line. Each number corresponds to right-most (i.e., ending) position of a * column. * If not provided, defaults to `[0.45, 0.85, 1]` for sequential-like * models and `[0.33, 0.55, 0.67, 1]` for non-sequential like models. * @param printFn Print function to use. * It will be called on each line of the summary. You can provide a custom * function in order to capture the string summary. Defaults to `console.log`. */ function printSummary(model, lineLength, positions, // tslint:disable-next-line:no-any printFn = console.log) { const sequentialLike = isModelSequentialLike(model); // Header names for different log elements. const toDisplay = ['Layer (type)', 'Input Shape', 'Output shape', 'Param #']; if (sequentialLike) { lineLength = lineLength || 90; positions = positions || [0.32, 0.61, 0.89, 1]; } else { lineLength = lineLength || 115; positions = positions || [0.24, 0.48, 0.70, 0.80, 1]; // Header names for different log elements. } if (positions[positions.length - 1] <= 1) { // `positions` is relative. Convert it to absolute positioning. positions = positions.map(p => Math.floor(lineLength * p)); } let relevantNodes; if (!sequentialLike) { toDisplay.push('Receives inputs'); relevantNodes = []; for (const depth in model.nodesByDepth) { relevantNodes.push(...model.nodesByDepth[depth]); } } printFn('_'.repeat(lineLength)); printRow(toDisplay, positions, printFn); printFn('='.repeat(lineLength)); const layers = model.layers; for (let i = 0; i < layers.length; ++i) { if (sequentialLike) { printLayerSummary(layers[i], positions, printFn); } else { printLayerSummaryWithConnections(layers[i], positions, relevantNodes, printFn); } printFn((i === layers.length - 1 ? '=' : '_').repeat(lineLength)); } // tslint:disable-next-line:no-any model.checkTrainableWeightsConsistency(); const trainableCount = countTrainableParams(model); const nonTrainableCount = countParamsInWeights(model.nonTrainableWeights); printFn(`Total params: ${trainableCount + nonTrainableCount}`); printFn(`Trainable params: ${trainableCount}`); printFn(`Non-trainable params: ${nonTrainableCount}`); printFn('_'.repeat(lineLength)); } function countTrainableParams(model) { let trainableCount; // tslint:disable:no-any if (model.collectedTrainableWeights != null) { trainableCount = countParamsInWeights(model.collectedTrainableWeights); } else { trainableCount = countParamsInWeights(model.trainableWeights); } // tslint:enable:no-any return trainableCount; } function isModelSequentialLike(model) { let sequentialLike = true; const nodesByDepth = []; const nodes = []; for (const depth in model.nodesByDepth) { nodesByDepth.push(model.nodesByDepth[depth]); } for (const depthNodes of nodesByDepth) { if (depthNodes.length > 1 || depthNodes.length === 1 && depthNodes[0].inboundLayers.length > 1) { sequentialLike = false; break; } nodes.push(...depthNodes); } if (sequentialLike) { // Search for shared layers. for (const layer of model.layers) { let flag = false; for (const node of layer.inboundNodes) { if (nodes.indexOf(node) !== -1) { if (flag) { sequentialLike = false; break; } else { flag = true; } } } if (!sequentialLike) { break; } } } return sequentialLike; } function printRow(fields, positions, // tslint:disable-next-line:no-any printFn = console.log) { let line = ''; for (let i = 0; i < fields.length; ++i) { if (i > 0) { line = line.slice(0, line.length - 1) + ' '; } line += fields[i]; line = line.slice(0, positions[i]); line += ' '.repeat(positions[i] - line.length); } printFn(line); } /** * Prints a summary for a single Layer, without connectivity information. * * @param layer: Layer instance to print. */ function printLayerSummary(layer, positions, // tslint:disable-next-line:no-any printFn) { let outputShape; let inputShape; try { inputShape = (layer.inboundNodes.map(x => JSON.stringify(x.inputShapes))).join(','); } catch (err) { inputShape = 'multiple'; } try { outputShape = JSON.stringify(layer.outputShape); } catch (err) { outputShape = 'multiple'; } const name = layer.name; const className = layer.getClassName(); const fields = [`${name} (${className})`, inputShape, outputShape, layer.countParams().toString()]; printRow(fields, positions, printFn); } /** * Prints a summary for a single Layer, with connectivity information. */ function printLayerSummaryWithConnections(layer, positions, relevantNodes, // tslint:disable-next-line:no-any printFn) { let outputShape; let inputShape; try { inputShape = (layer.inboundNodes.map(x => JSON.stringify(x.inputShapes))).join(','); } catch (err) { inputShape = 'multiple'; } try { outputShape = JSON.stringify(layer.outputShape); } catch (err) { outputShape = 'multiple'; } const connections = []; for (const node of layer.inboundNodes) { if (relevantNodes != null && relevantNodes.length > 0 && relevantNodes.indexOf(node) === -1) { continue; } for (let i = 0; i < node.inboundLayers.length; ++i) { const inboundLayer = node.inboundLayers[i].name; const inboundLayerIndex = node.nodeIndices[i]; const inboundTensorIndex = node.tensorIndices[i]; connections.push(`${inboundLayer}[${inboundLayerIndex}][${inboundTensorIndex}]`); } } const name = layer.name; const className = layer.getClassName(); const firstConnection = connections.length === 0 ? '' : connections[0]; const fields = [ `${name} (${className})`, inputShape, outputShape, layer.countParams().toString(), firstConnection ]; printRow(fields, positions, printFn); for (let i = 1; i < connections.length; ++i) { printRow(['', '', '', '', connections[i]], positions, printFn); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // tslint:enable /** * Test whether a value in an array is the name of a LayersModel or Layer. * @param key The key name that the value is found under. Note that the key * may not be at the level immediately above the value, if the value is in a * nested array. * @param index Index of the value in the Array that it is found in. * @param value The value object. * @returns A boolean indicating whether value is a name. */ function isArrayItemInputOrOutputName(key, index, value) { return (key === 'inboundNodes' || key === 'outputLayers' || key === 'inputLayers') && index === 0 && typeof value === 'string'; } /** * Convert a Pythonic config object to TypeScript config object. * @param pythonicConfig The config object to convert. * @param key Optional key name of the object being converted. * @returns Result of the conversion. */ function convertPythonicToTs(pythonicConfig, key) { if (pythonicConfig === null) { return null; } else if (typeof pythonicConfig === 'string') { return toCamelCase(pythonicConfig); } else if ((typeof pythonicConfig === 'number') || (typeof pythonicConfig === 'boolean')) { return pythonicConfig; } else if (pythonicConfig instanceof Array) { const tsArray = []; const arrayLength = pythonicConfig.length; for (let i = 0; i < arrayLength; ++i) { const item = pythonicConfig[i]; if (isArrayItemInputOrOutputName(key, i, item)) { tsArray.push(item); } else { tsArray.push(convertPythonicToTs(item, key)); } } return tsArray; } else { const tsDict = {}; for (const pythonicKey of Object.keys(pythonicConfig)) { const pythonicValue = pythonicConfig[pythonicKey]; if (pythonicKey === 'name' && typeof pythonicValue === 'string') { // Special case the 'name' key with a string value. Name values, such as // the names of LayersModel and Layer instances, should not undergo the // camel-case conversion. tsDict[pythonicKey] = pythonicValue; } else { const tsKey = toCamelCase(pythonicKey); tsDict[tsKey] = convertPythonicToTs(pythonicValue, tsKey); } } return tsDict; } } /** * Convert a TypeScript config object to Python config object. * @param tsConfig The config object to convert. * @param key Optional key name of the object being converted. * @returns Result of the conversion. */ function convertTsToPythonic(tsConfig, key) { if (tsConfig === null || tsConfig === undefined) { return null; } else if (typeof tsConfig === 'string') { return toSnakeCase(tsConfig); } else if ((typeof tsConfig === 'number') || (typeof tsConfig === 'boolean')) { return tsConfig; } else if (tsConfig instanceof Array) { const pyArray = []; const arrayLength = tsConfig.length; for (let i = 0; i < arrayLength; ++i) { const item = tsConfig[i]; if (isArrayItemInputOrOutputName(key, i, item)) { pyArray.push(item); } else { pyArray.push(convertTsToPythonic(item, key)); } } return pyArray; } else { const pyDict = {}; for (const tsKey of Object.keys(tsConfig)) { const tsValue = tsConfig[tsKey]; const pyKey = toSnakeCase(tsKey); if ((tsKey === 'name' || tsKey === 'className') && typeof tsValue === 'string') { // Special case the 'name' key with a string value. Name values, such as // the names of LayersModel and Layer instances, should not undergo the // snake-case conversion. pyDict[pyKey] = tsValue; } else { pyDict[pyKey] = convertTsToPythonic(tsValue, tsKey); } } return pyDict; } } /** @license See the LICENSE file. */ // This code is auto-generated, do not modify this file! const version = '4.15.0'; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // get weights key from tensor map in order to check if it is from keras v3. // e.g. dense/0 const isKerasSavedModelFormat = (weights) => { const keys = Object.keys(weights); if (keys.length === 0) { return false; } const key = keys[0].split('/'); return !isNaN(parseInt(key[key.length - 1], 10)); }; /** * A Container is a directed acyclic graph of layers. * * It is the topological form of a "model". A LayersModel * is simply a Container with added training routines. * */ class Container extends Layer { constructor(args) { // No args passed to super's constructor. super({}); this.containerNodes = new Set(); this.name = args.name; if (this.name == null) { const prefix = this.getClassName().toLowerCase(); this.name = getUid(prefix); } this.supportsMasking = false; this.trainable_ = true; // TODO(michaelterry): Initialize perInputLosses/Updates here. // Container-specific properties. if (Array.isArray(args.inputs)) { this.inputs = args.inputs.slice(); } else { this.inputs = [args.inputs]; } if (Array.isArray(args.outputs)) { this.outputs = args.outputs.slice(); } else { this.outputs = [args.outputs]; } // Check for redundancy in inputs. if (unique(this.inputs).length !== this.inputs.length) { throw new ValueError('The list of inputs passed to the model is ' + 'redundant. All inputs should only appear once. Found: ' + `${this.inputs.map(x => x.name)}`); } // Check for redundancy in outputs. if (unique(this.outputs).length !== this.outputs.length) { console.warn('The list of outputs passed to the model is redundant. ' + 'All outputs should only appear once. Found: ' + `${this.outputs.map(x => x.name)}`); } /* List of initial layers (1 to 1 mapping with this.inputs, hence the same layer might appear twice) */ this.inputLayers = []; this.inputLayersNodeIndices = []; this.inputLayersTensorIndices = []; /* List of layers (1 to 1 mapping with this.outputs, hence the same layer might appear twice) */ this.outputLayers = []; this.outputLayersNodeIndices = []; this.outputLayersTensorIndices = []; /* All layers in order of horizontal graph traversal. Entries are unique. Includes input and output layers. */ this.layers = []; /* References to container layers that were constructed internally. We need these to properly dispose of tensors from nested containers. */ this.internalContainerRefs = []; // TODO(michaelterry): Determine if caching still needed with eager // backend. /* This is for performance optimization when calling the Container on new inputs. Every time the Container is called on a set on input tensors, we compute the output tensors, output masks and output shapes in one pass, then cache them here. When one of these outputs is queried later, we retrieve it from there instead of recomputing it. */ // this.outputTensorCache = {}; // this.outputShapeCache = {}; // Build this.outputLayers: for (const x of this.outputs) { const layer = x.sourceLayer; const nodeIndex = x.nodeIndex; const tensorIndex = x.tensorIndex; this.outputLayers.push(layer); this.outputLayersNodeIndices.push(nodeIndex); this.outputLayersTensorIndices.push(tensorIndex); } // TODO(michaelterry): Add output mask cache code. // Build this.inputLayers: for (const x of this.inputs) { const layer = x.sourceLayer; const nodeIndex = x.nodeIndex; const tensorIndex = x.tensorIndex; /* It's supposed to be an input layer, so only one node and one tensor output. */ assert$1(nodeIndex === 0, 'input layer has >1 nodes'); assert$1(tensorIndex === 0, 'input layer has >1 tensors'); this.inputLayers.push(layer); this.inputLayersNodeIndices.push(nodeIndex); this.inputLayersTensorIndices.push(tensorIndex); } // Build this.inputNames and this.outputNames. this.inputNames = []; this.outputNames = []; this.feedInputShapes = []; this.feedInputNames = []; this.feedOutputNames = []; for (let i = 0; i < this.inputLayers.length; i++) { const layer = this.inputLayers[i]; // Check that layer is an InputLayer. if (!(layer instanceof InputLayer)) { throw new TypeError('Input layers to a LayersModel must be InputLayer objects. ' + `Received inputs: ${args.inputs}. ` + `Input ${i} (0-based) originates ` + `from layer type ${layer.getClassName()}.`); } this.inputNames.push(layer.name); this.feedInputShapes.push(layer.batchInputShape); this.feedInputNames.push(layer.name); } for (const layer of this.outputLayers) { this.outputNames.push(layer.name); } this.internalInputShapes = this.inputs.map(x => x.shape); this.internalOutputShapes = this.outputs.map(x => x.shape); /* Container_nodes: set of nodes included in the graph (not all nodes included in the layers are relevant to the current graph). */ // ids of all nodes relevant to the Container: const nodesDepths = {}; // To recover nodes from their ID. const nodeIDToNode = {}; const layersDepths = {}; // To layers from their ID. const layerIDToLayer = {}; const layerIndices = {}; const nodesInDecreasingDepth = []; /** * Builds a map of the graph of layers. * * This recursively updates the map `layerIndices`, * the list `nodesInDecreasingDepth` and the set `containerNodes`. * * @param tensor Some tensor in a graph. * @param finishedNodes Set of nodes whose subgraphs have been traversed * completely. Useful to prevent duplicated work. * @param nodesInProgress Set of nodes that are currently active on the * recursion stack. Useful to detect cycles. * @param layer Layer from which `tensor` comes from. If not provided, * will be obtained from tensor.sourceLayer. * @param nodeIndex Node index from which `tensor` comes from. * @param tensorIndex TensorIndex from which `tensor` comes from. * * @exception RuntimeError if a cycle is detected. */ const buildMapOfGraph = (tensor, finishedNodes, nodesInProgress, layer, nodeIndex, tensorIndex) => { if (layer == null || nodeIndex == null || tensorIndex == null) { layer = tensor.sourceLayer; nodeIndex = tensor.nodeIndex; tensorIndex = tensor.tensorIndex; } const node = layer.inboundNodes[nodeIndex]; // Prevent cycles. if (nodesInProgress.indexOf(node) !== -1) { throw new RuntimeError(`The tensor ${tensor.name} at layer "${layer.name}" ` + 'is part of a cycle.'); } // Don't repeat work for shared subgraphs if (finishedNodes.indexOf(node) !== -1) { return; } // Update containerNodes. this.containerNodes.add(Container.nodeKey(layer, nodeIndex)); // Store the traversal order for layer sorting. if (!(layer.id in layerIndices)) { layerIndices[layer.id] = Object.keys(layerIndices).length; } if (nodesInProgress.indexOf(node) === -1) { nodesInProgress.push(node); } // Propagate to all previous tensors connected to this node. const numInboundLayers = node.inboundLayers.length; for (let i = 0; i < numInboundLayers; i++) { const x = node.inputTensors[i]; const layer = node.inboundLayers[i]; const nodeIndex = node.nodeIndices[i]; const tensorIndex = node.tensorIndices[i]; buildMapOfGraph(x, finishedNodes, nodesInProgress, layer, nodeIndex, tensorIndex); } finishedNodes.push(node); while (nodesInProgress.indexOf(node) >= 0) { nodesInProgress.splice(nodesInProgress.indexOf(node), 1); } nodesInDecreasingDepth.push(node); }; const finishedNodes = []; const nodesInProgress = []; for (const x of this.outputs) { buildMapOfGraph(x, finishedNodes, nodesInProgress); } const reversedNodesInDecreasingDepth = nodesInDecreasingDepth.slice().reverse(); for (const node of reversedNodesInDecreasingDepth) { nodeIDToNode[node.id] = node; // If the depth is not set, the node has no outbound nodes (depth 0). if (!(node.id in nodesDepths)) { nodesDepths[node.id] = 0; } let depth = nodesDepths[node.id]; // Update the depth of the corresponding layer const previousDepth = (layersDepths[node.outboundLayer.id] == null ? 0 : layersDepths[node.outboundLayer.id]); /* If we've seen this layer before at a higher depth, we should use that depth instead of the node depth. This is necessary for shared layers that have inputs at different depth levels in the graph. */ depth = Math.max(depth, previousDepth); layersDepths[node.outboundLayer.id] = depth; layerIDToLayer[node.outboundLayer.id] = node.outboundLayer; nodesDepths[node.id] = depth; // Update the depth of inbound nodes. for (let i = 0; i < node.inboundLayers.length; i++) { const inboundLayer = node.inboundLayers[i]; const nodeIndex = node.nodeIndices[i]; const inboundNode = inboundLayer.inboundNodes[nodeIndex]; const previousDepth = (nodesDepths[inboundNode.id] == null ? 0 : nodesDepths[inboundNode.id]); nodesDepths[inboundNode.id] = Math.max(depth + 1, previousDepth); nodeIDToNode[inboundNode.id] = inboundNode; } } // Build a dict {depth: list of nodes with this depth} const nodesByDepth = {}; for (const nodeID in nodesDepths) { const depth = nodesDepths[nodeID]; if (!(depth in nodesByDepth)) { nodesByDepth[depth] = []; } nodesByDepth[depth].push(nodeIDToNode[nodeID]); } // Build a dict {depth: list of layers with this depth} const layersByDepth = {}; for (const layerID in layersDepths) { const depth = layersDepths[layerID]; if (!(depth in layersByDepth)) { layersByDepth[depth] = []; } layersByDepth[depth].push(layerIDToLayer[layerID]); } // Get sorted list of layer depths. let depthKeys = Object.keys(layersByDepth) .map(x => parseInt(x, 10)) .sort(reverseNumberCompare); // Set this.layers and this.layersByDepth. this.layers = []; for (const depth of depthKeys) { const layersForDepth = layersByDepth[depth]; // Container.layers needs to have a deterministic order: // here we order them by traversal order. layersForDepth.sort((a, b) => { const aIndex = layerIndices[a.id]; const bIndex = layerIndices[b.id]; if (aIndex < bIndex) { return -1; } if (aIndex > bIndex) { return 1; } return 0; }); for (const layer of layersForDepth) { if (layer instanceof Container) { this.internalContainerRefs.push(layer); } this.layers.push(layer); } } this.layersByDepth = layersByDepth; // Get sorted list of node depths; depthKeys = Object.keys(nodesByDepth) .map(x => parseInt(x, 10)) .sort(reverseNumberCompare); // Check that all tensors required are computable. // computable_tensors: all tensors in the graph // that can be computed from the inputs provided. const computableTensors = this.inputs.slice(); // To provide a better error msg. const layersWithCompleteInput = []; for (const depth of depthKeys) { for (const node of nodesByDepth[depth]) { const layer = node.outboundLayer; if (layer != null) { for (const x of node.inputTensors) { if (computableTensors.indexOf(x) === -1) { throw new RuntimeError(`Graph disconnected: cannot obtain value for tensor ${x}` + ` at layer "${layer.name}". ` + 'The following previous layers were accessed without ' + `issue: ${layersWithCompleteInput}`); } } for (const x of node.outputTensors) { computableTensors.push(x); } layersWithCompleteInput.push(layer.name); } } } // Set this.containerNodes and this.nodesByDepth. this.nodesByDepth = nodesByDepth; // Ensure name unicity, which will be crucial for serialization // (since serialized nodes refer to layers by their name). const allNames = this.layers.map(x => x.name); for (const name of allNames) { const numOccurrences = allNames.filter(x => x === name).length; if (numOccurrences !== 1) { throw new RuntimeError(`The name "${name}" is used ${numOccurrences} times ` + 'in the model. All layer names should be unique. Layer names: ' + JSON.stringify(allNames)); } } // Layer parameters. // The new container starts with a single inbound node // for its inputs, and no outbound nodes. // Will be appended to by future calls to apply(). this.outboundNodes = []; // Will be appended to below, and by future calls to apply(). this.inboundNodes = []; // Create the node linking internal inputs to internal outputs. // (This call has side effects.) // tslint:disable-next-line:no-unused-expression new Node({ outboundLayer: this, inboundLayers: [], nodeIndices: [], tensorIndices: [], inputTensors: this.inputs, outputTensors: this.outputs, inputMasks: this.inputs.map(x => null), outputMasks: this.outputs.map(x => null), inputShapes: this.inputs.map(x => x.shape), outputShapes: this.outputs.map(x => x.shape) }); this.built = true; this._refCount = 1; // The ref count of a container always start at 1. } assertNotDisposed() { if (this._refCount === 0) { throw new Error(`Container '${this.name}' is already disposed.`); } } /** * Attempt to dispose a LayersModel's weights. * * This method decrease the reference count of the LayersModel object by 1. * * A LayersModel is reference-counted. Its reference count is incremented by 1 * when it is first constructed and when it is used as a Layer of another * LayersModel. * * If the reference count of a LayersModel becomes 0, the `dispose` method of * all its constituent `Layer`s will be called. * * Note: If the reference count is greater than 0 after the decrement, the * `dispose` method of its constituent `Layer`s will *not* be called. * * After a LayersModel is disposed, it cannot be used in calls such as * 'predict`, `evaluate` or `fit` anymore. * * @returns A DisposeResult Object with the following fields: * - refCountAfterDispose: The reference count of the LayersModel after this * `dispose()` call. * - numDisposedVariables: Number of `tf.Variable`s (i.e., weights) disposed * during this `dispose()` call. * @throws {Error} If the layer is not built yet, or if the LayersModel has * already been disposed. */ dispose() { this.assertNotDisposed(); const result = { refCountAfterDispose: null, numDisposedVariables: 0 }; if (--this._refCount === 0) { for (const layer of this.layers) { result.numDisposedVariables += layer.dispose().numDisposedVariables; } // Call dispose on each internally created container layer again to ensure // their refCounts hit zero and their tensors are subsequently deleted. for (const container of this.internalContainerRefs) { result.numDisposedVariables += container.dispose().numDisposedVariables; } } result.refCountAfterDispose = this._refCount; return result; } get trainable() { return this.trainable_; } set trainable(trainable) { this.layers.forEach(layer => { // tslint:disable-next-line:no-any layer._trainableWeights .forEach(w => w.trainable = trainable); }); this.trainable_ = trainable; } get trainableWeights() { // Porting Note: This check below is to prevent errors where the // _trainableWeights inherited from the parent class (Layer) gets // inadvertently used. if (this._trainableWeights.length > 0) { throw new ValueError('Container instance unexpectedly contains _trainableWeights.' + 'The trainable weights of a Container are a union of the ' + 'trainable weights of its consituent Layers. Its own ' + '_trainableWeights must remain an empty Array.'); } if (!this.trainable) { return []; } let weights = []; for (const layer of this.layers) { weights = weights.concat(layer.trainableWeights); } return weights; } get nonTrainableWeights() { const weights = []; for (const layer of this.layers) { weights.push(...layer.nonTrainableWeights); } if (!this.trainable) { const trainableWeights = []; for (const layer of this.layers) { trainableWeights.push(...layer.trainableWeights); } return trainableWeights.concat(weights); } return weights; } get weights() { return this.trainableWeights.concat(this.nonTrainableWeights); } /** * Loads all layer weights from a JSON object. * * Porting Note: HDF5 weight files cannot be directly loaded in JavaScript / * TypeScript. The utility script at `scripts/pykeras.py` offers means * to convert them into JSON strings compatible with this method. * Porting Note: TensorFlow.js Layers supports only loading by name currently. * * @param weights A JSON mapping weight names to weight values as nested * arrays of numbers, or a `NamedTensorMap`, i.e., a JSON mapping weight * names to `tf.Tensor` objects. * @param strict Require that the provided weights exactly match those * required by the container. Default: `true`. Passing `false` means that * extra weights and missing weights will be silently ignored. */ loadWeights(weights, strict = true) { const nameToWeight = {}; let totalWeightsCount = 0; const modelIsKerasSavedModelFormat = isKerasSavedModelFormat(weights); if (modelIsKerasSavedModelFormat) { this.parseWeights(weights); } // Check if weights from keras v3. for (const layer of this.layers) { for (const [index, weight] of layer.weights.entries()) { // Parse the name to layerName/index. // e.g. dense/0, dense/1, dense_1/0, dense_1/1 const parsedName = modelIsKerasSavedModelFormat ? `${weight.name.split('/').slice(0, -1).join('/') + '/'}${index}` : weight.originalName; if (nameToWeight[parsedName] != null) { throw new ValueError(`Duplicate weight name: ${parsedName}`); } nameToWeight[parsedName] = weight; totalWeightsCount++; } } const weightValueTuples = []; for (const name in weights) { // TF 2.2.0 added cell name to the weight name in the format of // layer_name/cell_name/weight_name, we need to remove // the inner cell name. let validatedName = name; if (nameToWeight[name] == null) { const tokens = name.split('/'); const shortenNameArray = tokens.slice(0, -2).concat([tokens[tokens.length - 1]]); validatedName = shortenNameArray.join('/'); } if (nameToWeight[validatedName] != null) { weightValueTuples.push([nameToWeight[validatedName], weights[name]]); } else if (strict) { throw new ValueError(`Provided weight data has no target variable: ${name}`); } delete nameToWeight[validatedName]; } if (strict) { // Check that all weights are set. const unsetNames = []; for (const name in nameToWeight) { unsetNames.push(name); } if (unsetNames.length > 0) { throw new ValueError(`${unsetNames.length} of ${totalWeightsCount} weights are not set: ` + `${unsetNames}`); } } batchSetValue(weightValueTuples); } parseWeights(weights) { for (const key in Object.keys(weights)) { const listParts = key.split('/'); const list = ['vars', 'layer_checkpoint_dependencies']; // For keras v3, the weights name are saved based on the folder structure. // e.g. _backbone/_layer_checkpoint_dependencies/transformer/_self../ // _output_dense/vars/0 // Therefore we discard the `vars` and `layer_checkpoint_depencies` within // the saved name and only keeps the layer name and weights. // This can help to mapping the actual name of the layers and load each // weight accordingly. const newKey = listParts .map(str => { if (str.startsWith('_')) { return str.slice(1); } return str; }) .filter(str => !list.includes(str)) .join('/'); if (newKey !== key) { weights[newKey] = weights[key]; delete weights[key]; } } } /** * Util shared between different serialization methods. * @returns LayersModel config with Keras version information added. */ updatedConfig() { const theConfig = this.getConfig(); const modelConfig = {}; modelConfig['className'] = this.getClassName(); modelConfig['config'] = theConfig; modelConfig['kerasVersion'] = `tfjs-layers ${version}`; // TODO(nielsene): Replace something like K.backend() once // possible. modelConfig['backend'] = 'TensorFlow.js'; return modelConfig; } /** * Returns a JSON string containing the network configuration. * * To load a network from a JSON save file, use * models.modelFromJSON(jsonString); * @param extraJsonArgs Unused in tfjs-layers, maintained for PyKeras * @param returnString Whether the return value should be stringified * (default: `true`). * @returns a JSON string if `returnString` (default), or a JSON object if * `!returnString`. */ // tslint:disable-next-line:no-any toJSON(unused, returnString = true) { const modelConfig = convertTsToPythonic(this.updatedConfig()); return returnString ? JSON.stringify(modelConfig) : modelConfig; } /** * Call the model on new inputs. * * In this case `call` just reapplies all ops in the graph to the new inputs * (e.g. build a new computational graph from the provided inputs). * * @param inputs A tensor or list of tensors. * @param mask A mask or list of masks. A mask can be either a tensor or null * (no mask). * * @return A tensor if there is a single output, or a list of tensors if there * are more than one outputs. */ call(inputs, kwargs) { return tidy$1(() => { inputs = toList(inputs); const feedDict = new FeedDict(); for (let i = 0; i < this.inputs.length; ++i) { feedDict.add(this.inputs[i], inputs[i]); } return execute(this.outputs, feedDict, kwargs); }); } /** * Computes an output mask tensor. * * @param inputs Tensor or list of tensors. * @param mask Tensor or list of tensors. * * @return null or a tensor (or list of tensors, one per output tensor of the * layer). */ computeMask(inputs, mask) { return tidy$1(() => { inputs = toList(inputs); let masks; if (mask == null) { masks = pyListRepeat(null, inputs.length); } else { masks = toList(mask); } // TODO(michaelterry): Add support for mask caching. return this.runInternalGraph(inputs, masks)[1]; }); } /** * Computes the output shape of the layer. * * Assumes that the layer will be built to match that input shape provided. * * @param inputShape A shape (tuple of integers) or a list of shape tuples * (one per output tensor of the layer). Shape tuples can include null for * free dimensions, instead of an integer. */ computeOutputShape(inputShape) { const inputShapes = normalizeShapeList(inputShape); if (inputShapes.length !== this.inputLayers.length) { throw new ValueError(`Invalid inputShape argument ${inputShape}: ` + `model has ${this.inputLayers.length} tensor inputs.`); } // TODO(michaelterry): Add caching const layersToOutputShapes = {}; for (let i = 0; i < inputShapes.length; i++) { const layer = this.inputLayers[i]; const inputShape = inputShapes[i]; // It's an input layer: computeOutputShape is identity, // and there is only one node and one tensor output. const shapeKey = layer.name + '_0_0'; layersToOutputShapes[shapeKey] = inputShape; } const depthKeys = Object.keys(this.nodesByDepth) .map(x => parseInt(x, 10)) .sort(reverseNumberCompare); // Iterate over nodes, by depth level. if (depthKeys.length > 1) { for (const depth of depthKeys) { const nodes = this.nodesByDepth[depth]; for (const node of nodes) { // This is always a single layer, never a list. const layer = node.outboundLayer; if (this.inputLayers.map(x => x.id).indexOf(layer.id) !== -1) { // We've already covered the input layers a few lines above. continue; } // Potentially redundant list, same size of node.inputTensors. const inputShapes = []; for (let j = 0; j < node.inboundLayers.length; j++) { const inboundLayer = node.inboundLayers[j]; const nodeIndex = node.nodeIndices[j]; const tensorIndex = node.tensorIndices[j]; const shapeKey = `${inboundLayer.name}_${nodeIndex}_${tensorIndex}`; const inputShape = layersToOutputShapes[shapeKey]; inputShapes.push(inputShape); } const outputShape = layer.computeOutputShape(singletonOrArray(inputShapes)); const outputShapes = normalizeShapeList(outputShape); const nodeIndex = layer.inboundNodes.indexOf(node); for (let j = 0; j < outputShapes.length; j++) { const shapeKey = `${layer.name}_${nodeIndex}_${j}`; layersToOutputShapes[shapeKey] = outputShapes[j]; } } } } // Read final output shapes from layersToOutputShapes. const outputShapes = []; const outputShapeKeys = []; for (let i = 0; i < this.outputLayers.length; i++) { const layer = this.outputLayers[i]; const nodeIndex = this.outputLayersNodeIndices[i]; const tensorIndex = this.outputLayersTensorIndices[i]; const shapeKey = `${layer.name}_${nodeIndex}_${tensorIndex}`; outputShapeKeys.push(shapeKey); } for (let i = 0; i < outputShapeKeys.length; i++) { const key = outputShapeKeys[i]; assert$1(key in layersToOutputShapes); outputShapes.push(layersToOutputShapes[key]); } // TODO(michaelterry): Update cache return singletonOrArray(outputShapes); } /** * Computes output tensors for new inputs. * * Note: * - Expects `inputs` to be a list (potentially with 1 element). * * @param inputs List of tensors * @param masks List of masks (tensors or null). * @return Three lists: outputTensors, outputMasks, outputShapes */ runInternalGraph(inputs, masks) { if (masks == null) { masks = pyListRepeat(null, inputs.length); } // Dictionary mapping reference tensors to tuples // (computed tensor, compute mask) // we assume a 1:1 mapping from tensor to mask // TODO: raise exception when a `.computeMask()` call // does not return a list the same size as `call` const tensorMap = {}; for (let i = 0; i < this.inputs.length; ++i) { const x = this.inputs[i]; const y = inputs[i]; const mask = masks[i]; tensorMap[x.id] = [y, mask]; } const depthKeys = Object.keys(this.nodesByDepth) .map(x => parseInt(x, 10)) .sort(reverseNumberCompare); for (const depth of depthKeys) { const nodes = this.nodesByDepth[depth]; for (const node of nodes) { // This is always a single layer, never a list. const layer = node.outboundLayer; const referenceInputTensors = node.inputTensors; const referenceOutputTensors = node.outputTensors; // If all previous input tensors are available in tensorMap, // then call node.inboundLayer on them. // List of tuples [input, mask]: const computedData = new Array(); for (const x of referenceInputTensors) { if (x.id in tensorMap) { computedData.push(tensorMap[x.id]); } } if (computedData.length === referenceInputTensors.length) { // TODO(michaelterry): Add K.name_scope here, if we need it. let kwargs = {}; let computedTensors; let computedMasks; let outputTensors; let outputMasks; // call layer if (node.callArgs != null) { kwargs = node.callArgs; } if (computedData.length === 1) { const [computedTensor, computedMask] = computedData[0]; if (kwargs['mask'] == null) { kwargs['mask'] = computedMask; } outputTensors = toList(layer.call(computedTensor, kwargs)); outputMasks = toList(layer.computeMask(computedTensor, computedMask)); computedTensors = [computedTensor]; computedMasks = [computedMask]; } else { computedTensors = computedData.map(x => x[0]); computedMasks = computedData.map(x => x[1]); if (kwargs['mask'] == null) { kwargs['mask'] = computedMasks; } outputTensors = toList(layer.call(computedTensors, kwargs)); outputMasks = toList(layer.computeMask(computedTensors, computedMasks)); } if (layer.activityRegularizer) { throw new NotImplementedError('LayersModel invocation with concrete Tensor value(s) in the ' + 'presence of activity regularizer(s) is not supported yet.'); } // TODO(michaelterry): Add model updates and losses // Update tensor map. for (let i = 0; i < referenceOutputTensors.length; ++i) { const x = referenceOutputTensors[i]; const y = outputTensors[i]; const mask = outputMasks[i]; tensorMap[x.id] = [y, mask]; } } } } const outputTensors = []; const outputMasks = []; const outputShapes = []; for (const x of this.outputs) { assert$1(x.id in tensorMap, `Could not compute output ${x.name} : ${x.id}`); const [tensor, mask] = tensorMap[x.id]; outputShapes.push(tensor.shape); outputTensors.push(tensor); outputMasks.push(mask); } // TODO(michaelterry): Add support for caches. return [outputTensors, outputMasks, outputShapes]; } /** * Builds a map of internal node keys to node ordering. * Used in serializaion a node orderings may change as unused nodes are * dropped. Porting Note: This helper method was pulled out of getConfig to * improve readability. * @param layers An array of Layers in the model. * @returns Map of Node Keys to index order within the layer. */ buildNodeConversionMap(layers) { const nodeConversionMap = {}; let keptNodes; for (const layer of this.layers) { keptNodes = layer instanceof Container ? 1 : 0; for (let originalNodeIndex = 0; originalNodeIndex < layer.inboundNodes.length; originalNodeIndex++) { const nodeKey = Container.nodeKey(layer, originalNodeIndex); if (this.containerNodes.has(nodeKey)) { // i.e. we mark it to be saved nodeConversionMap[nodeKey] = keptNodes; keptNodes += 1; } } } return nodeConversionMap; } getLayer(nameOrIndex, index) { if (index != null) { return this.findLayer(index); } else { if (nameOrIndex == null) { throw new ValueError('Provide either a layer name or layer index'); } if (typeof nameOrIndex === 'number') { return this.findLayer(nameOrIndex); } } for (const layer of this.layers) { if (layer.name === nameOrIndex) { return layer; } } throw new ValueError(`No such layer: ${nameOrIndex}`); } findLayer(index) { if (this.layers.length <= index) { throw new ValueError(`Was asked to retrieve layer at index ${index}, but model only ` + `has ${this.layers.length} layer(s).`); } else { return this.layers[index]; } } /** * Retrieves the Container's current loss values. * * Used for regularizers during training. */ calculateLosses() { // Porting Node: This is an augmentation to Container.loss in PyKeras. // In PyKeras, Container.loss returns symbolic tensors. Here a concrete // Tensor (specifically Scalar) values are returned. This is due to the // imperative backend. return tidy$1(() => { const losses = []; for (const layer of this.layers) { for (let nodeIndex = 0; nodeIndex < layer.inboundNodes.length; ++nodeIndex) { const nodeKey = Container.nodeKey(layer, nodeIndex); if (this.containerNodes.has(nodeKey)) { losses.push(...layer.calculateLosses()); } } } // TODO(cais): Add any unconditional model-level losses? return losses; }); } getConfig() { const config = { name: this.name }; // Build a map from layer unique name (self._node_key) // to the index of the nodes that are saved in the config. // Only nodes in container_nodes are saved. const nodeConversionMap = this.buildNodeConversionMap(this.layers); // Serialize and save the layers in layerConfigs const layerConfigs = []; for (const layer of this.layers) { const layerClassName = layer.getClassName(); const layerConfig = layer.getConfig(); const filteredInboundNodes = []; for (let originalNodeIndex = 0; originalNodeIndex < layer.inboundNodes.length; originalNodeIndex++) { const node = layer.inboundNodes[originalNodeIndex]; const nodeKey = Container.nodeKey(layer, originalNodeIndex); let kwargs = {}; if (this.containerNodes.has(nodeKey)) { // The node is relevant to the model: // add to filteredInboundNodes. if (node.callArgs) { try { JSON.stringify(node.callArgs); kwargs = node.callArgs; } catch (err) { console.warn(`Layer ${layer.name} was passed ` + `non-serializable keyword arguments: ` + `${node.callArgs}. They will not be included ` + `in the serialized model (and thus will be ` + `missing at deserialization time).`); kwargs = {}; } } if (node.inboundLayers.length > 0) { const nodeData = []; for (let i = 0; i < node.inboundLayers.length; i++) { const inboundLayer = node.inboundLayers[i]; const nodeIndex = node.nodeIndices[i]; const tensorIndex = node.tensorIndices[i]; const nodeKey = Container.nodeKey(inboundLayer, nodeIndex); let newNodeIndex = nodeConversionMap[nodeKey]; if (newNodeIndex == null) { newNodeIndex = 0; } nodeData.push([inboundLayer.name, newNodeIndex, tensorIndex, kwargs]); } filteredInboundNodes.push(nodeData); } } } const dict = {}; dict['name'] = layer.name; dict['className'] = layerClassName; dict['config'] = layerConfig; dict['inboundNodes'] = filteredInboundNodes; layerConfigs.push(dict); } config['layers'] = layerConfigs; // Gather info about inputs and outputs const modelInputs = []; for (let i = 0; i < this.inputLayers.length; i++) { const layer = this.inputLayers[i]; const nodeIndex = this.inputLayersNodeIndices[i]; const nodeKey = Container.nodeKey(layer, nodeIndex); if (!this.containerNodes.has(nodeKey)) { continue; } let newNodeIndex = nodeConversionMap[nodeKey]; if (newNodeIndex === null || newNodeIndex === undefined) { newNodeIndex = 0; } const tensorIndex = this.inputLayersTensorIndices[i]; modelInputs.push([layer.name, newNodeIndex, tensorIndex]); } config['inputLayers'] = modelInputs; const modelOutputs = []; for (let i = 0; i < this.outputLayers.length; i++) { const layer = this.outputLayers[i]; const nodeIndex = this.outputLayersNodeIndices[i]; const nodeKey = Container.nodeKey(layer, nodeIndex); if (!this.containerNodes.has(nodeKey)) { continue; } let newNodeIndex = nodeConversionMap[nodeKey]; if (newNodeIndex === null || newNodeIndex === undefined) { newNodeIndex = 0; } const tensorIndex = this.outputLayersTensorIndices[i]; modelOutputs.push([layer.name, newNodeIndex, tensorIndex]); } config['outputLayers'] = modelOutputs; return config; } /** * Instantiates a LayersModel from its config (output of `get_config()`). * @param cls the class to create * @param config LayersModel config dictionary. * @param customObjects An optional dictionary of custom objects. * @param fastWeightInit Optional flag to use fast weight initialization * during deserialization. This is applicable to cases in which * the initialization will be immediately overwritten by loaded weight * values. Default: `false`. * @returns A LayersModel instance. * @throws ValueError: In case of improperly formatted config dict. */ /** @nocollapse */ static fromConfig(cls, config, customObjects = {}, fastWeightInit = false) { // Layer instances created during // the graph reconstruction process const createdLayers = {}; // Dictionary mapping layer instances to // node data that specifies a layer call. // It acts as a queue that maintains any unprocessed // layer call until it becomes possible to process it // (i.e. until the input tensors to the call all exist). const unprocessedNodes = {}; function addUnprocessedNode(layer, nodeData) { if (!(layer.name in unprocessedNodes)) { unprocessedNodes[layer.name] = [nodeData]; } else { unprocessedNodes[layer.name].push(nodeData); } } function processNode(layer, nodeData) { const inputTensors = []; let kwargs; for (const inputData of nodeData) { const inboundLayerName = inputData[0]; const inboundNodeIndex = inputData[1]; const inboundTensorIndex = inputData[2]; kwargs = inputData[3] == null ? {} : inputData[3]; if (!(inboundLayerName in createdLayers)) { addUnprocessedNode(layer, nodeData); return; } const inboundLayer = createdLayers[inboundLayerName]; if (inboundLayer.inboundNodes.length <= inboundNodeIndex) { addUnprocessedNode(layer, nodeData); return; } const inboundNode = inboundLayer.inboundNodes[inboundNodeIndex]; inputTensors.push(inboundNode.outputTensors[inboundTensorIndex]); } // Call layer on its inputs, thus creating the node // and building the layer if needed. // Note: This has Eager vs Graph Implications. if (inputTensors.length > 0) { layer.apply(singletonOrArray(inputTensors), kwargs); // was ** kwargs } } /** * Deserialize a layer, then call it on appropriate inputs. * @param layerData: layer config dict. * @throws ValueError: In case of improperly formatted `layer_data` * dict. */ function processLayer(layerData) { const layerName = layerData['name']; // Instantiate layer. const layer = deserialize(layerData, config['customObjects'] != null ? config['customObjects'] : {}); layer.setFastWeightInitDuringBuild(fastWeightInit); createdLayers[layerName] = layer; // Gather layer inputs. const inboundNodesData = layerData['inboundNodes']; inboundNodesData.forEach(nodeData => { if (!(nodeData instanceof Array)) { throw new ValueError(`Corrupted configuration, expected array for nodeData: ${nodeData}`); } // We don't process nodes (i.e. make layer calls) // on the fly because the inbound node may not yet exist, // in case of layer shared at different topological depths // (e.g.a model such as A(B(A(B(x))))) addUnprocessedNode(layer, nodeData); }); } // First, we create all layers and enqueue nodes to be processed. const name = config['name']; const layersFromConfig = config['layers']; for (const layerData of layersFromConfig) { processLayer(layerData); } // Then we process nodes in order of layer depth. // Nodes that cannot yet be processed(if the inbound node // does not yet exist) are re - enqueued, and the process // is repeated until all nodes are processed. while (!isObjectEmpty(unprocessedNodes)) { for (const layerData of layersFromConfig) { const layer = createdLayers[layerData['name']]; if (layer.name in unprocessedNodes) { const currentUnprocessedNodesForLayer = unprocessedNodes[layer.name]; delete unprocessedNodes[layer.name]; for (const nodeData of currentUnprocessedNodesForLayer) { processNode(layer, nodeData); } } } } const inputTensors = []; const outputTensors = []; const inputLayersFromConfig = config['inputLayers']; for (const layerData of inputLayersFromConfig) { const layerName = layerData[0]; const nodeIndex = layerData[1]; const tensorIndex = layerData[2]; assert$1(layerName in createdLayers); const layer = createdLayers[layerName]; const layerOutputTensors = layer.inboundNodes[nodeIndex].outputTensors; inputTensors.push(layerOutputTensors[tensorIndex]); } const outputLayersFromConfig = config['outputLayers']; for (const layerData of outputLayersFromConfig) { const layerName = layerData[0]; const nodeIndex = layerData[1]; const tensorIndex = layerData[2]; assert$1(layerName in createdLayers); const layer = createdLayers[layerName]; const layerOutputTensors = layer.inboundNodes[nodeIndex].outputTensors; outputTensors.push(layerOutputTensors[tensorIndex]); } return new cls({ inputs: inputTensors, outputs: outputTensors, name }); } /** * Determine whether the container is stateful. * * Porting Note: this is the equivalent of the stateful @property of * the Container class in PyKeras. */ get stateful() { // Porting Note: This check is to prevent inadvertent setting of the // _stateful property of the Container instance. if (this._stateful) { throw new ValueError('Container instance unexpectedly has _stateful = true. The ' + 'statefulness of a Container is determined by the Layers it ' + 'contains. Its _stateful property must remain the default false.'); } for (const layer of this.layers) { if (layer.stateful) { return true; } } return false; } /** * Reset the state of all stateful constituent layers (if any). * * Examples of stateful layers include RNN layers whose `stateful` property * is set as `true`. */ resetStates() { tidy$1(() => { this.layers.forEach(layer => { // tslint:disable:no-any if (layer.stateful) { layer.resetStates(); } // tslint:enable:no-any }); }); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function standardizeSampleOrClassWeights(xWeight, outputNames, weightType) { const numOutputs = outputNames.length; if (xWeight == null || (Array.isArray(xWeight) && xWeight.length === 0)) { return outputNames.map(name => null); } if (numOutputs === 1) { if (Array.isArray(xWeight) && xWeight.length === 1) { return xWeight; } else if (typeof xWeight === 'object' && outputNames[0] in xWeight) { return [xWeight[outputNames[0]]]; } else { return [xWeight]; } } if (Array.isArray(xWeight)) { if (xWeight.length !== numOutputs) { throw new Error(`Provided ${weightType} is an array of ${xWeight.length} ` + `element(s), but the model has ${numOutputs} outputs. ` + `Make sure a set of weights is provided for each model output.`); } return xWeight; } else if (typeof xWeight === 'object' && Object.keys(xWeight).length > 0 && typeof xWeight[Object.keys(xWeight)[0]] === 'object') { const output = []; outputNames.forEach(outputName => { if (outputName in xWeight) { output.push(xWeight[outputName]); } else { output.push(null); } }); return output; } else { throw new Error(`The model has multiple (${numOutputs}) outputs, ` + `so ${weightType} must be either an array with ` + `${numOutputs} elements or an object with ${outputNames} keys. ` + `Provided ${weightType} not understood: ${JSON.stringify(xWeight)}`); } } /** * Standardize class weighting objects. * * This function takes a single class-weighting object, an array of them, * or a map from output name to class-weighting object. It compares it to the * output name(s) of the model, base on which it outputs an array of * class-weighting objects of which the length matches the number of outputs. * * @param classWeight Input class-weighting object(s). * @param outputNames All output name(s) of the model. * @return An array of class-weighting objects. The length of the array matches * the model's number of outputs. */ function standardizeClassWeights(classWeight, outputNames) { return standardizeSampleOrClassWeights(classWeight, outputNames, 'classWeight'); } /** * Standardize by-sample and/or by-class weights for training. * * Note that this function operates on one model output at a time. For a model * with multiple outputs, you must call this function multiple times. * * @param y The target tensor that the by-sample and/or by-class weight is for. * The values of y are assumed to encode the classes, either directly * as an integer index, or as one-hot encoding. * @param sampleWeight By-sample weights. * @param classWeight By-class weights: an object 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. * @param sampleWeightMode The mode for the sample weights. * @return A Promise of weight tensor, of which the size of the first dimension * matches that of `y`. */ async function standardizeWeights(y, sampleWeight, classWeight, sampleWeightMode) { if (sampleWeight != null || sampleWeightMode != null) { // TODO(cais): Once 'temporal' mode is implemented, document it in the doc // string. throw new Error('Support sampleWeight is not implemented yet'); } if (classWeight != null) { // Apply class weights per sample. const yClasses = tidy$1(() => { if (y.shape.length === 1) { // Assume class indices. return clone$1(y); } else if (y.shape.length === 2) { if (y.shape[1] > 1) { // Assume one-hot encoding of classes. const axis = 1; return argMax(y, axis); } else if (y.shape[1] === 1) { // Class index. return reshape$2(y, [y.shape[0]]); } else { throw new Error(`Encountered unexpected last-dimension size (${y.shape[1]}) ` + `during handling of class weights. The size is expected to be ` + `>= 1.`); } } else { throw new Error(`Unexpected rank of target (y) tensor (${y.rank}) during ` + `handling of class weights. The rank is expected to be 1 or 2.`); } }); const yClassIndices = Array.from(await yClasses.data()); dispose(yClasses); const classSampleWeight = []; yClassIndices.forEach(classIndex => { if (classWeight[classIndex] == null) { throw new Error(`classWeight must contain all classes in the training data. ` + `The class ${classIndex} exists in the data but not in ` + `classWeight`); } else { classSampleWeight.push(classWeight[classIndex]); } }); return tensor1d(classSampleWeight, 'float32'); } else { return null; } } /** * Apply per-sample weights on the loss values from a number of samples. * * @param losses Loss tensor of shape `[batchSize]`. * @param sampleWeights Per-sample weight tensor of shape `[batchSize]`. * @returns Tensor of the same shape as`losses`. */ function computeWeightedLoss(losses, sampleWeights) { return mul$1(losses, sampleWeights); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // Default batch size used during tensor-based validation. const DEFAULT_VALIDATION_BATCH_SIZE = 32; /** * Standardize the output of a dataset iterator for use by * LayersModel.fitDataset(). * * @param model: A `tf.LayersModel` object. * @param iteratorOut The output of a dataset iterator. It is required to be * an object of the form `{xs: TensorOrArrayOrMap, ys: * TensorOrArrayOrMap}`, where `TensorOrArrayOrMap` is a single `tf.Tensor`, * a `tf.Tensor[]`, or a flat map from string names to `tf.Tensor`s. * @returns A flat array of `tf.Tensor` objects: the input `tf.Tensor`s * followed by the target `tf.Tensor`s. When `tf.Tensor`s are provided * as a map, the order in the resulting array is taken from the `inputNames` * and `outputNames` of the model. */ function standardizeDataIteratorOutput( // Type `model` as `any` here to avoid circular dependency w/ // training.ts. // tslint:disable-next-line:no-any model, iteratorOut) { let xs; let ys; const iteratorOutObj = iteratorOut; xs = iteratorOutObj['xs']; ys = iteratorOutObj['ys']; tfc.util.assert(xs != null && ys != null, () => 'A Dataset iterator for fitDataset() is expected to generate ' + 'objects of the form `{xs: xVal, ys: yVal}`, where the two ' + 'values may be `tf.Tensor`, an array of Tensors, or a map of ' + 'string to Tensor. The provided Dataset instead generates ' + `${iteratorOut}`); const flattenedXs = flattenTensorOrArrayOrMap('input', model.inputNames, xs); const flattenedYs = flattenTensorOrArrayOrMap('output', model.outputNames, ys); const batchSize = flattenedXs[0].shape[0]; tfc.util.assert(flattenedXs.length === model.inputs.length, () => `LayersModel has ${model.inputs.length} inputs, but the dataset ` + `provides ${flattenedXs.length} inputs. (Expected input keys: ` + `${JSON.stringify(model.inputNames)})`); tfc.util.assert(flattenedYs.length === model.outputs.length, () => `LayersModel has ${model.outputs.length} outputs, but the dataset ` + `provides ${flattenedYs.length} outputs. (Expected output keys: ` + `${JSON.stringify(model.outputNames)})`); for (let xIndex = 0; xIndex < flattenedXs.length; xIndex++) { tfc.util.assert(flattenedXs[xIndex].shape[0] === batchSize, () => `Batch size mismatch: input ` + `${model.inputNames[xIndex]} has ${flattenedXs[xIndex].shape[0]}; ` + `expected ${batchSize} based on input ${model.inputNames[0]}.`); } for (let yIndex = 0; yIndex < flattenedYs.length; yIndex++) { tfc.util.assert(flattenedYs[yIndex].shape[0] === batchSize, () => `Batch size mismatch: output ` + `${model.outputNames[yIndex]} has ${flattenedYs[yIndex].shape[0]}; ` + `expected ${batchSize} based on input ${model.inputNames[0]}.`); } return { xs: flattenedXs, ys: flattenedYs }; } function flattenTensorOrArrayOrMap(inputOrOutput, names, values) { if (values instanceof tfc.Tensor) { return [values]; } else if (Array.isArray(values)) { tfc.util.assert(values.length === names.length, () => `Received an array of ${values.length} Tensors, but expected ${names.length} to match the ${inputOrOutput} keys ${names}.`); return values; } else { const result = []; // Check that all the required keys are available. for (const name of names) { if (values[name] == null) { throw new ValueError(`The feature data generated by the dataset lacks the required ` + `${inputOrOutput} key '${name}'.`); } result.push(values[name]); } return result; } } function standardizeTensorValidationData(data) { if (data.length === 3) { throw new NotImplementedError('Validation with sample weights is not implemented yet.'); } return { xs: data[0], ys: data[1] }; } async function fitDataset( // Type `model` as `any` here to avoid circular dependency w/ // training.ts. // tslint:disable-next-line:no-any model, dataset, args) { const hasBatchesPerEpoch = args.batchesPerEpoch != null; tfc.util.assert(model.optimizer != null, () => 'You must compile a model before training/testing. Use ' + 'LayersModel.compile(modelCompileConfig).'); tfc.util.assert(args != null, () => `For fitDataset(), the 2nd argument (config) is required, ` + `but it is not provided in this call.`); tfc.util.assert(args.epochs != null && args.epochs > 0 && Number.isInteger(args.epochs), () => `For fitDataset(), config.epochs is expected to be a positive ` + `integer, but got ${args.epochs}`); tfc.util.assert(!hasBatchesPerEpoch || (args.batchesPerEpoch > 0 && Number.isInteger(args.batchesPerEpoch)), () => `For fitDataset(), config.batchesPerEpoch is expected to be a ` + `positive integer if specified, but got ${args.batchesPerEpoch}`); tfc.util.assert( // tslint:disable-next-line:no-any args['validationSplit'] == null, () => '`validationSplit` is not supported by `fitDataset()`. ' + 'Use validationData instead.'); if (model.isTraining) { throw new Error('Cannot start training because another fit() call is ongoing.'); } model.isTraining = true; try { const doValidation = args.validationData != null; let valXs; let valYs; if (doValidation) { if (isDatasetObject(args.validationData)) { tfc.util.assert(args.validationBatches == null || (args.validationBatches > 0 && Number.isInteger(args.validationBatches)), () => `For fitDataset() with dataset-based validation, ` + `config.validationBatches is expected not to be provided, ` + `or to be a positive integer, ` + `but got ${args.validationBatches}`); } else { const validationData = standardizeTensorValidationData(args.validationData); valXs = validationData.xs; valYs = validationData.ys; } } const trainFunction = model.makeTrainFunction(); const outLabels = model.getDedupedMetricsNames(); let callbackMetrics; if (doValidation) { callbackMetrics = outLabels.slice().concat(outLabels.map(n => 'val_' + n)); } else { callbackMetrics = outLabels.slice(); } const callbacks = standardizeCallbacks(args.callbacks, args.yieldEvery); const verbose = args.verbose == null ? 1 : args.verbose; const { callbackList, history } = configureCallbacks(callbacks, verbose, args.epochs, null, null, getStepsPerEpoch(dataset, args), null, // Batch size determined by the dataset itself. doValidation, callbackMetrics); callbackList.setModel(model); model.history = history; await callbackList.onTrainBegin(); model.stopTraining_ = false; let epoch = args.initialEpoch == null ? 0 : args.initialEpoch; let dataIterator = await dataset.iterator(); while (epoch < args.epochs) { const epochLogs = {}; await callbackList.onEpochBegin(epoch); let stepsDone = 0; let batchIndex = 0; if (!hasBatchesPerEpoch) { dataIterator = await dataset.iterator(); } while (hasBatchesPerEpoch ? stepsDone < args.batchesPerEpoch : true) { const iteratorOut = await dataIterator.next(); // If `batchesPerEpoch` is specified, the dataset should not be // exhausted until all epoches are done. if (hasBatchesPerEpoch && iteratorOut.done) { console.warn('You provided `batchesPerEpoch` as ' + `${args.batchesPerEpoch}, ` + 'but your dataset iterator ran out of data after ' + `${stepsDone} batches; ` + 'interrupting training. Make sure that your ' + 'dataset can generate at least `batchesPerEpoch * epochs` ' + 'batches (in this case, ' + `${args.batchesPerEpoch * args.epochs} batches). ` + 'You may need to use the repeat() function when building ' + 'your dataset.'); break; } if (iteratorOut.value != null) { const { xs, ys } = standardizeDataIteratorOutput(model, iteratorOut.value); const batchLogs = {}; batchLogs['batch'] = batchIndex; batchLogs['size'] = xs[0].shape[0]; await callbackList.onBatchBegin(batchIndex, batchLogs); const sampleWeights = []; if (args.classWeight != null) { const standardClassWeights = standardizeClassWeights(args.classWeight, model.outputNames); for (let i = 0; i < standardClassWeights.length; ++i) { sampleWeights.push(await standardizeWeights(ys[i], null, standardClassWeights[i])); } } // Train on batch. const ins = xs.concat(ys).concat(sampleWeights); const outs = trainFunction(ins); tfc.dispose(ins); for (let i = 0; i < outLabels.length; ++i) { const label = outLabels[i]; const out = outs[i]; batchLogs[label] = out; tfc.keep(out); } await callbackList.onBatchEnd(batchIndex, batchLogs); disposeTensorsInLogs(batchLogs); batchIndex++; stepsDone++; } if (hasBatchesPerEpoch ? stepsDone >= args.batchesPerEpoch : iteratorOut.done) { // Epoch finished. Perform validation. if (doValidation) { let valOuts; if (isDatasetObject(args.validationData)) { valOuts = toList(await model.evaluateDataset(args.validationData, { batches: args.validationBatches })); } else { valOuts = toList(model.evaluate(valXs, valYs, { batchSize: args.validationBatchSize == null ? DEFAULT_VALIDATION_BATCH_SIZE : args.validationBatchSize, verbose: 0 })); } for (let i = 0; i < model.metricsNames.length; ++i) { epochLogs[`val_${model.metricsNames[i]}`] = valOuts[i]; } } // Call `break` to exit one epoch lopp after validation is done. If // config.batchesPerEpoch is specified, an epoch while loop will // stop when `stepsDone >= config.batchesPerEpoch`. When // config.batchesPerEpoch is not provided, the following `break` is // required to exit the while lopp after dataset is exhausted. break; } if (model.stopTraining_) { break; } } await callbackList.onEpochEnd(epoch, epochLogs); epoch++; if (model.stopTraining_) { break; } } await callbackList.onTrainEnd(); await model.history.syncData(); return model.history; } finally { model.isTraining = false; } } /** Helper function that determines number of steps (batches) per epoch. */ function getStepsPerEpoch(dataset, args) { // Attempt to determine # of batches in an epoch. let stepsPerEpoch = null; if (args.batchesPerEpoch != null) { stepsPerEpoch = args.batchesPerEpoch; } else if (Number.isFinite(dataset.size)) { stepsPerEpoch = dataset.size; } return stepsPerEpoch; } // Check if provided object is a Dataset object by checking its .iterator // element. function isDatasetObject(dataset) { return (typeof dataset.iterator === 'function'); } // Check if provided object is a LazyIterator object by checking it's .next // element. function isLazyIteratorObject(iterator) { return (typeof iterator.next === 'function'); } async function evaluateDataset( // Type `model` as `any` here to avoid circular dependency w/ // training.ts. // tslint:disable-next-line:no-any model, dataset, args) { args = args || {}; const hasBatches = args.batches != null; const f = model.testFunction; let outs = []; if (args.verbose > 0) { throw new NotImplementedError('Verbose mode is not implemented yet.'); } tfc.util.assert(!hasBatches || (args.batches > 0 && Number.isInteger(args.batches)), () => 'Test loop expects `batches` to be a positive integer, but ' + `received ${JSON.stringify(args.batches)}`); const dataIterator = isLazyIteratorObject(dataset) ? dataset : await dataset.iterator(); // Keeps track of number of examples used in this evaluation. let numExamples = 0; let batch = 0; while (hasBatches ? batch < args.batches : true) { const iteratorOut = await dataIterator.next(); outs = tfc.tidy(() => { if (iteratorOut.value) { // TODO(cais): Once real dataset is available, use // `map(x => standardizeDataIteratorOutput(model, x).map(f)`. const { xs, ys } = standardizeDataIteratorOutput(model, iteratorOut.value); const xsAndYs = xs.concat(ys); const batchOuts = tfc.tidy(() => f(xsAndYs)); tfc.dispose(xsAndYs); if (batch === 0) { for (let i = 0; i < batchOuts.length; ++i) { outs.push(scalar$1(0)); } } const batchSize = xsAndYs[0].shape[0]; for (let i = 0; i < batchOuts.length; ++i) { const batchOut = batchOuts[i]; const oldScalar = outs[i]; outs[i] = tfc.tidy(() => tfc.add(outs[i], tfc.mul(batchSize, batchOut))); if (batch > 0) { tfc.dispose(oldScalar); } } tfc.dispose(batchOuts); numExamples += batchSize; ++batch; } return outs; }); if (iteratorOut.done) { if (hasBatches) { console.warn('Your dataset iterator ran out of data during evaluateDataset(). ' + 'Interrupting evalution. Make sure that your ' + 'dataset can generate at least `batches` ' + `batches (in this case, ${args.batches} batches). ` + 'You may need to use the repeat() function when building ' + 'your dataset.'); } break; } } for (let i = 0; i < outs.length; ++i) { const oldScalar = outs[i]; outs[i] = tfc.div(outs[i], numExamples); tfc.dispose(oldScalar); } return singletonOrArray(outs); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function checkBatchSize(batchSize) { tfc.util.assert(batchSize > 0 && Number.isInteger(batchSize), () => `batchSize is required to be a positive integer, but got ${batchSize}`); } /** * Slice a Tensor or an Array of Tensors, by start and stop indices. * * Porting Note: The `_slice_arrays` function in PyKeras is covered by this * function and `sliceArraysByIndices()` together. * * @param arrays: the input. * @param start: the starting index (inclusive). * @param stop: the stopping index (exclusive). * @returns The result of the slicing. If `arrays` is an `Array` of * `tf.Tensor`s, the slicing will be applied to all elements of the `Array` * in the same way. */ function sliceArrays(arrays, start, stop) { if (arrays == null) { return [null]; } else if (Array.isArray(arrays)) { return arrays.map(array => sliceAlongFirstAxis(array, start, stop - start)); } else { // Tensor. return sliceAlongFirstAxis(arrays, start, stop - start); } } /** * Slice a Tensor or an Array of Tensors, by random-order indices. * * Porting Note: The `_slice_arrays` function in PyKeras is covered by this * function and `sliceArrays()` together. * * @param arrays The input `tf.Tensor` or `Array` of `tf.Tensor`s to slice. * If an `Array` of `tf.Tensor`s, all `tf.Tensor`s will be sliced in the * same fashion. * @param indices The indices to use for slicing along the first (batch) * dimension. * @returns Result(s) of the slicing. */ function sliceArraysByIndices(arrays, indices) { return tfc.tidy(() => { if (arrays == null) { return null; } else if (Array.isArray(arrays)) { return arrays.map(array => sliceArraysByIndices(array, indices)); } else { // TODO(cais): indices should be a pre-constructed Tensor1D to avoid // tensor1d() calls. return gather$1(arrays, indices.dtype === 'int32' ? indices : tfc.cast(indices, 'int32')); } }); } /** * Returns a list of batch indices (tuples of indices). * @param size: Integer, total size of the data to slice into batches. * @param batchSize: Integer, batch size. * @returns An Array of [batchStart, batchEnd] tuples. batchStart is * inclusive; batchEnd is exclusive. I.e., each batch consists of indices x * that satisfy batchStart <= x < batchEnd. */ function makeBatches(size, batchSize) { const output = []; let batchStart = 0; let batchEnd = null; while (batchStart < size) { batchEnd = batchStart + batchSize; if (batchEnd >= size) { batchEnd = size; } output.push([batchStart, batchEnd]); batchStart = batchEnd; } return output; } /** * Ensure tensors all have a rank of at least 2. * * If a tensor has a rank of 1, it is dimension-expanded to rank 2. * If any tensor has a rank of 0 (i.e., is a scalar), an error will be thrown. */ function ensureTensorsRank2OrHigher(tensors) { const outs = []; if (tensors instanceof Tensor$1) { tensors = [tensors]; } // Make Tensors at least 2D. for (let i = 0; i < tensors.length; ++i) { const tensor = tensors[i]; if (tensor.rank === 1) { outs.push(expandDims$1(tensor, 1)); } else if (tensor.rank === 0) { throw new Error('Expected tensor to be at least 1D, but received a 0D tensor ' + '(scalar).'); } else { outs.push(tensor); } } return outs; } /** * Compare a set of tensors with a reference (old) set, discard the ones * in the new set that are not present in the reference set. * * This method is used for memory clenaup during calls such as * LayersModel.fit(). * * @param tensors New set which may contain Tensors not present in * `refTensors`. * @param refTensors Reference Tensor set. */ // TODO(cais, kangyizhang): Deduplicate with tfjs-data. function disposeNewTensors(tensors, refTensors) { if (tensors == null) { return; } const oldTensorIds = []; if (refTensors instanceof Tensor$1) { oldTensorIds.push(refTensors.id); } else if (Array.isArray(refTensors)) { refTensors.forEach(t => oldTensorIds.push(t.id)); } else if (refTensors != null) { // `oldTensors` is a map from string name to Tensor. for (const name in refTensors) { const oldTensor = refTensors[name]; oldTensorIds.push(oldTensor.id); } } const tensorsToDispose = []; if (tensors instanceof Tensor$1) { if (oldTensorIds.indexOf(tensors.id) === -1) { tensorsToDispose.push(tensors); } } else if (Array.isArray(tensors)) { tensors.forEach(t => { if (oldTensorIds.indexOf(t.id) === -1) { tensorsToDispose.push(t); } }); } else if (tensors != null) { // `oldTensors` is a map from string name to Tensor. for (const name in tensors) { const tensor = tensors[name]; if (oldTensorIds.indexOf(tensor.id) === -1) { tensorsToDispose.push(tensor); } } } tensorsToDispose.forEach(t => { if (!t.isDisposed) { t.dispose(); } }); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Helper function for polymorphic input data: 1. singleton Tensor. */ function isDataTensor(x) { return x instanceof Tensor$1; } /** * Helper function for polymorphic input data: 2. Array of Tensor. */ function isDataArray(x) { return Array.isArray(x); } /** * Helper function for polymorphic input data: 3. "dict" of Tensor. */ function isDataDict(x) { return !isDataTensor(x) && !isDataArray(x); } /** * Normalizes inputs and targets provided by users. * @param data User-provided input data (polymorphic). * @param names An Array of expected Tensor names. * @param shapes Optional Array of expected Tensor shapes. * @param checkBatchAxis Whether to check that the batch axis of the arrays * match the expected value found in `shapes`. * @param exceptionPrefix String prefix used for exception formatting. * @returns List of standardized input Tensors (one Tensor per model input). * @throws ValueError: in case of improperly formatted user data. */ function standardizeInputData(data, names, shapes, checkBatchAxis = true, exceptionPrefix = '') { if (names == null || names.length === 0) { // Check for the case where the model expected no data, but some data got // sent. if (data != null) { let gotUnexpectedData = false; if (isDataArray(data) && data.length > 0) { gotUnexpectedData = true; } else if (isDataDict(data)) { for (const key in data) { if (data.hasOwnProperty(key)) { gotUnexpectedData = true; break; } } } else { // `data` is a singleton Tensor in this case. gotUnexpectedData = true; } if (gotUnexpectedData) { throw new ValueError(`Error when checking model ${exceptionPrefix} expected no data, ` + `but got ${data}`); } } return []; } if (data == null) { return names.map(name => null); } let arrays; if (isDataDict(data)) { data = data; arrays = []; for (const name of names) { if (data[name] == null) { throw new ValueError(`No data provided for "${name}". Need data for each key in: ` + `${names}`); } arrays.push(data[name]); } } else if (isDataArray(data)) { data = data; if (data.length !== names.length) { throw new ValueError(`Error when checking model ${exceptionPrefix}: the Array of ` + `Tensors that you are passing to your model is not the size the ` + `model expected. Expected to see ${names.length} Tensor(s), but ` + `instead got the following list of Tensor(s): ${data}`); } arrays = data; } else { data = data; if (names.length > 1) { throw new ValueError(`The model ${exceptionPrefix} expects ${names.length} Tensor(s), ` + `but only received one Tensor. Found: Tensor with shape ${data.shape}`); } arrays = [data]; } arrays = ensureTensorsRank2OrHigher(arrays); // Check shape compatibility. if (shapes != null) { for (let i = 0; i < names.length; ++i) { if (shapes[i] == null) { continue; } const array = arrays[i]; if (array.shape.length !== shapes[i].length) { throw new ValueError(`Error when checking ${exceptionPrefix}: expected ${names[i]} ` + `to have ${shapes[i].length} dimension(s). but got array with ` + `shape ${array.shape}`); } for (let j = 0; j < shapes[i].length; ++j) { if (j === 0 && !checkBatchAxis) { // Skip the first (batch) axis. continue; } const dim = array.shape[j]; const refDim = shapes[i][j]; if (refDim != null && refDim >= 0 && dim !== refDim) { throw new ValueError(`${exceptionPrefix} expected a batch of elements where each ` + `example has shape [${shapes[i].slice(1, shapes[i].length)}] ` + `(i.e.,tensor shape [*,${shapes[i].slice(1, shapes[i].length)}])` + ` but the ${exceptionPrefix} received an input with ${array.shape[0]}` + ` examples, each with shape [${array.shape.slice(1, array.shape.length)}]` + ` (tensor shape [${array.shape}])`); } } } } return arrays; } /** * User input validation for Tensors. * @param inputs `Array` of `tf.Tensor`s for inputs. * @param targets `Array` of `tf.Tensor`s for targets. * @param weights Optional `Array` of `tf.Tensor`s for sample weights. * @throws ValueError: in case of incorrectly formatted data. */ function checkArrayLengths(inputs, targets, weights) { const setX = unique(inputs.map(input => input.shape[0])); setX.sort(); const setY = unique(targets.map(target => target.shape[0])); setY.sort(); // TODO(cais): Check `weights` as well. if (setX.length > 1) { throw new ValueError(`All input Tensors (x) should have the same number of samples. ` + `Got array shapes: ` + `${JSON.stringify(inputs.map(input => input.shape))}`); } if (setY.length > 1) { throw new ValueError(`All target Tensors (y) should have the same number of samples. ` + `Got array shapes: ` + `${JSON.stringify(targets.map(target => target.shape))}`); } if (setX.length > 0 && setY.length > 0 && !util.arraysEqual(setX, setY)) { throw new ValueError(`Input Tensors should have the same number of samples as target ` + `Tensors. Found ${setX[0]} input sample(s) and ${setY[0]} target ` + `sample(s).`); } } /** * Validation on the compatibility of targes and loss functions. * * This helps prevent users from using loss functions incorrectly. * * @param targets `Array` of `tf.Tensor`s of targets. * @param lossFns `Array` of loss functions. * @param outputShapes `Array` of shapes of model outputs. */ function checkLossAndTargetCompatibility(targets, lossFns, outputShapes) { // TODO(cais): Dedicated test coverage? const keyLosses = [ meanSquaredError$1, binaryCrossentropy$2, categoricalCrossentropy$2 ]; for (let i = 0; i < targets.length; ++i) { const y = targets[i]; const loss = lossFns[i]; const shape = outputShapes[i]; if (loss == null) { continue; } if (loss === categoricalCrossentropy$2) { if (y.shape[y.shape.length - 1] === 1) { throw new ValueError(`You are passing a target array of shape ${y.shape} while using ` + `a loss 'categorical_crossentropy'. 'categorical_crossentropy'` + `expects targets to be binary matrices (1s and 0s) of shape ` + `[samples, classes].`); // TODO(cais): Example code in error message. } } if (keyLosses.indexOf(loss) !== -1) { const slicedYShape = y.shape.slice(1); const slicedShape = shape.slice(1); for (let j = 0; j < slicedYShape.length; ++j) { const targetDim = slicedYShape[j]; const outDim = slicedShape[j]; if (outDim != null && targetDim !== outDim) { throw new ValueError(`A target Tensor with shape ${y.shape} was passed for an ` + `output of shape ${shape}, while using a loss function that ` + `expects targets to have the same shape as the output.`); } } } } } /** * Check inputs provided by the user. * * Porting Note: This corresponds to _standardize_input_data() in Python * Keras. Because of the strong typing in TF.js, we do not need to convert * the data. Specifically: * 1) in PyKeras, `data` can be `DataFrame` instances from pandas, for * example. We don't need to worry about that here because there is no * widely popular javascript/typesdcript equivalent of pandas (so far). * If one becomes available in the future, we can add support. * 2) in PyKeras, inputs can be Python dict. But here we are stipulating * that the data is either a single `tf.Tensor` or an Array of `tf.Tensor`s. We * may add support for `Object` data inputs in the future when the need * arises. * * Instead, we perform basic checks for number of parameters and shapes. * * @param data: The input data. * @param names: Name for the inputs, from the model. * @param shapes: Expected shapes for the input data, from the model. * @param checkBatchAxis: Whether the size along the batch axis (i.e., the * first dimension) will be checked for matching. * @param exceptionPrefix: Execption prefix message, used in generating error * messages. * @throws ValueError: on incorrect number of inputs or mismatches in shapes. */ function checkInputData(data, names, shapes, checkBatchAxis = true, exceptionPrefix = '') { let arrays; if (Array.isArray(data)) { if (data.length !== names.length) { throw new ValueError(`Error when checking model ${exceptionPrefix}: the Array of ` + `Tensors that you are passing to your model is not the size the ` + `the model expected. Expected to see ${names.length} Tensor(s),` + ` but instead got ${data.length} Tensors(s).`); } arrays = data; } else { if (names.length > 1) { throw new ValueError(`The model expects ${names.length} ${exceptionPrefix} Tensors, ` + `but only received one Tensor. Found: array with shape ` + `${JSON.stringify(data.shape)}.`); } arrays = [data]; } if (shapes != null) { for (let i = 0; i < names.length; ++i) { if (shapes[i] == null) { continue; } const array = arrays[i]; if (array.shape.length !== shapes[i].length) { throw new ValueError(`Error when checking ${exceptionPrefix}: expected ${names[i]} ` + `to have ${shapes[i].length} dimension(s), but got array with ` + `shape ${JSON.stringify(array.shape)}`); } for (let j = 0; j < shapes[i].length; ++j) { if (j === 0 && !checkBatchAxis) { continue; } const dim = array.shape[j]; const refDim = shapes[i][j]; if (refDim != null) { if (refDim !== dim) { throw new ValueError(`Error when checking ${exceptionPrefix}: expected ` + `${names[i]} to have shape ${JSON.stringify(shapes[i])} but ` + `got array with shape ${JSON.stringify(array.shape)}.`); } } } } } } /** * Maps metric functions to model outputs. * @param metrics An shortcut strings name, metric function, `Array` or dict * (`Object`) of metric functions. * @param outputNames An `Array` of the names of model outputs. * @returns An `Array` (one entry per model output) of `Array` of metric * functions. For instance, if the model has 2 outputs, and for the first * output we want to compute `binaryAccuracy` and `binaryCrossentropy`, * and just `binaryAccuracy` for the second output, the `Array` would look * like: * `[[binaryAccuracy, binaryCrossentropy], [binaryAccuracy]]` * @throws TypeError: incompatible metrics format. */ function collectMetrics(metrics, outputNames) { if (metrics == null || Array.isArray(metrics) && metrics.length === 0) { return outputNames.map(name => []); } let wrappedMetrics; if (typeof metrics === 'string' || typeof metrics === 'function') { wrappedMetrics = [metrics]; } else if (Array.isArray(metrics) || typeof metrics === 'object') { wrappedMetrics = metrics; } else { throw new TypeError('Type of metrics argument not understood. Expected an string,' + `function, Array, or Object, found: ${metrics}`); } if (Array.isArray(wrappedMetrics)) { // We then apply all metrics to all outputs. return outputNames.map(name => wrappedMetrics); } else { // In this case, metrics is a dict. const nestedMetrics = []; for (const name of outputNames) { let outputMetrics = wrappedMetrics.hasOwnProperty(name) ? wrappedMetrics[name] : []; if (!Array.isArray(outputMetrics)) { outputMetrics = [outputMetrics]; } nestedMetrics.push(outputMetrics); } return nestedMetrics; } } const LAYERS_MODEL_FORMAT_NAME = 'layers-model'; /** * A `tf.LayersModel` is a directed, acyclic graph of `tf.Layer`s plus methods * for training, evaluation, prediction and saving. * * `tf.LayersModel` is the basic unit of training, inference and evaluation in * TensorFlow.js. To create a `tf.LayersModel`, use `tf.LayersModel`. * * See also: * `tf.Sequential`, `tf.loadLayersModel`. * * @doc {heading: 'Models', subheading: 'Classes'} */ class LayersModel extends Container { constructor(args) { super(args); this.isTraining = false; } /** * Print a text summary of the model's layers. * * The summary includes * - Name and type of all layers that comprise the model. * - Output shape(s) of the layers * - Number of weight parameters of each layer * - If the model has non-sequential-like topology, the inputs each layer * receives * - The total number of trainable and non-trainable parameters of the model. * * ```js * const input1 = tf.input({shape: [10]}); * const input2 = tf.input({shape: [20]}); * const dense1 = tf.layers.dense({units: 4}).apply(input1); * const dense2 = tf.layers.dense({units: 8}).apply(input2); * const concat = tf.layers.concatenate().apply([dense1, dense2]); * const output = * tf.layers.dense({units: 3, activation: 'softmax'}).apply(concat); * * const model = tf.model({inputs: [input1, input2], outputs: output}); * model.summary(); * ``` * * @param lineLength Custom line length, in number of characters. * @param positions Custom widths of each of the columns, as either * fractions of `lineLength` (e.g., `[0.5, 0.75, 1]`) or absolute number * of characters (e.g., `[30, 50, 65]`). Each number corresponds to * right-most (i.e., ending) position of a column. * @param printFn Custom print function. Can be used to replace the default * `console.log`. For example, you can use `x => {}` to mute the printed * messages in the console. * * @doc {heading: 'Models', subheading: 'Classes'} */ summary(lineLength, positions, printFn = console.log) { if (!this.built) { throw new ValueError(`This model has never been called, thus its weights have not been ` + `created yet. So no summary can be displayed. Build the model ` + `first (e.g., by calling it on some test data).`); } printSummary(this, lineLength, positions, printFn); } /** * 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. * * @param args a `ModelCompileArgs` specifying the loss, optimizer, and * metrics to be used for fitting and evaluating this model. * * @doc {heading: 'Models', subheading: 'Classes'} */ compile(args) { if (args.loss == null) { args.loss = []; } this.loss = args.loss; if (typeof args.optimizer === 'string') { this.optimizer_ = getOptimizer(args.optimizer); this.isOptimizerOwned = true; } else { if (!(args.optimizer instanceof Optimizer)) { throw new ValueError(`User-defined optimizer must be an instance of tf.Optimizer.`); } this.optimizer_ = args.optimizer; this.isOptimizerOwned = false; } // TODO(cais): Add lossWeights. // TODO(cais): Add sampleWeightMode. // Prepare loss functions. let lossFunctions = []; if (!Array.isArray(args.loss) && typeof args.loss !== 'string' && typeof args.loss !== 'function') { args.loss = args.loss; for (const name in args.loss) { if (this.outputNames.indexOf(name) === -1) { throw new ValueError(`Unknown entry in loss dictionary: "${name}". ` + `Only expected the following keys: ${this.outputNames}`); } } for (const name of this.outputNames) { if (args.loss[name] == null) { console.warn(`Output "${name}" is missing from loss dictionary. We assume ` + `this was done on purpose, and we will not be expecting data ` + `to be passed to ${name} during training`); } lossFunctions.push(get$1(args.loss[name])); } } else if (Array.isArray(args.loss)) { if (args.loss.length !== this.outputs.length) { throw new ValueError(`When passing an Array as loss, it should have one entry per ` + `model output. The model has ${this.outputs.length} output(s), ` + `but you passed loss=${args.loss}.`); } const theLosses = args.loss; lossFunctions = theLosses.map(l => get$1(l)); } else { const lossFunction = get$1(args.loss); this.outputs.forEach(_ => { lossFunctions.push(lossFunction); }); } this.lossFunctions = lossFunctions; this.feedOutputNames = []; this.feedOutputShapes = []; this.feedLossFns = []; for (let i = 0; i < this.outputs.length; ++i) { // TODO(cais): Logic for skipping target(s). const shape = this.internalOutputShapes[i]; const name = this.outputNames[i]; this.feedOutputNames.push(name); this.feedOutputShapes.push(shape); this.feedLossFns.push(this.lossFunctions[i]); } // TODO(cais): Add logic for output masks. // TODO(cais): Add logic for sample weights. const skipTargetIndices = []; // Prepare metrics. this.metrics = args.metrics; // TODO(cais): Add weightedMetrics. this.metricsNames = ['loss']; this.metricsTensors = []; // Compute total loss. // Porting Note: In PyKeras, metrics_tensors are symbolic tensor objects. // Here, metricsTensors are TypeScript functions. This difference is due // to the difference in symbolic/imperative property of the backends. nameScope('loss', () => { for (let i = 0; i < this.outputs.length; ++i) { if (skipTargetIndices.indexOf(i) !== -1) { continue; } // TODO(cais): Add weightedLoss, sampleWeight and mask. // The following line should be weightedLoss const weightedLoss = this.lossFunctions[i]; if (this.outputs.length > 1) { this.metricsTensors.push([weightedLoss, i]); this.metricsNames.push(this.outputNames[i] + '_loss'); } } // Porting Note: Due to the imperative nature of the backend, we calculate // the regularizer penalties in the totalLossFunction, instead of here. }); const nestedMetrics = collectMetrics(args.metrics, this.outputNames); // TODO(cais): Add nestedWeightedMetrics. /** * Helper function used in loop below. */ const appendMetric = (outputIndex, metricName, metricTensor) => { if (this.outputNames.length > 1) { metricName = this.outputNames[outputIndex] + '_' + metricName; } this.metricsNames.push(metricName); this.metricsTensors.push([metricTensor, outputIndex]); }; nameScope('metric', () => { for (let i = 0; i < this.outputs.length; ++i) { if (skipTargetIndices.indexOf(i) !== -1) { continue; } const outputMetrics = nestedMetrics[i]; // TODO(cais): Add weights and outputWeightedMetrics. // TODO(cais): Add optional arg `weights` to the following function. const handleMetrics = (metrics) => { const metricNamePrefix = ''; let metricName; let accFn; let weightedMetricFn; // TODO(cais): Use 'weights_' for weighted metrics. for (const metric of metrics) { if (typeof metric === 'string' && ['accuracy', 'acc', 'crossentropy', 'ce'].indexOf(metric) !== -1) { const outputShape = this.internalOutputShapes[i]; if (outputShape[outputShape.length - 1] === 1 || this.lossFunctions[i] === binaryCrossentropy$2) { // case: binary accuracy/crossentropy. if (['accuracy', 'acc'].indexOf(metric) !== -1) { accFn = binaryAccuracy$1; } else if (['crossentropy', 'ce'].indexOf(metric) !== -1) { accFn = binaryCrossentropy$1; } } else if (this.lossFunctions[i] === sparseCategoricalCrossentropy$1) { // case: categorical accuracy / crossentropy with sparse // targets. if (['accuracy', 'acc'].indexOf(metric) !== -1) { accFn = sparseCategoricalAccuracy$1; } else if (['crossentropy', 'ce'].indexOf(metric) !== -1) { accFn = sparseCategoricalCrossentropy; } } else { // case: categorical accuracy / crossentropy. if (['accuracy', 'acc'].indexOf(metric) !== -1) { accFn = categoricalAccuracy$1; } else if (['crossentropy', 'ce'].indexOf(metric) !== -1) { accFn = categoricalCrossentropy$1; } } let suffix; if (['accuracy', 'acc'].indexOf(metric) !== -1) { suffix = 'acc'; } else if (['crossentropy', 'ce'].indexOf(metric) !== -1) { suffix = 'ce'; } // TODO(cais): Add weighting actually. weightedMetricFn = accFn; metricName = metricNamePrefix + suffix; } else { const metricFn = get(metric); // TODO(cais): Add weighting actually. weightedMetricFn = metricFn; metricName = metricNamePrefix + getLossOrMetricName(metric); } // TODO(cais): Add weighting and masking to metricResult. let metricResult; nameScope(metricName, () => { metricResult = weightedMetricFn; }); appendMetric(i, metricName, metricResult); } }; handleMetrics(outputMetrics); // TODO(cais): Call handleMetrics with weights. } }); // Porting Notes: Given the imperative backend of tfjs-core, // there is no need for constructing the symbolic graph and placeholders. this.collectedTrainableWeights = this.trainableWeights; } /** * Check trainable weights count consistency. * * This will raise a warning if `this.trainableWeights` and * `this.collectedTrainableWeights` are inconsistent (i.e., have different * numbers of parameters). * Inconsistency will typically arise when one modifies `model.trainable` * without calling `model.compile()` again. */ checkTrainableWeightsConsistency() { if (this.collectedTrainableWeights == null) { return; } if (this.trainableWeights.length !== this.collectedTrainableWeights.length) { console.warn('Discrepancy between trainableweights and collected trainable ' + 'weights. Did you set `model.trainable` without calling ' + '`model.compile()` afterwards?'); } } /** * 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. * * ```js * 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(); * ``` * * @param x `tf.Tensor` of test data, or an `Array` of `tf.Tensor`s if the * model has multiple inputs. * @param y `tf.Tensor` of target data, or an `Array` of `tf.Tensor`s if the * model has multiple outputs. * @param args A `ModelEvaluateArgs`, containing optional fields. * * @return `Scalar` test loss (if the model has a single output and no * metrics) or `Array` of `Scalar`s (if the model has multiple outputs * and/or metrics). The attribute `model.metricsNames` * will give you the display labels for the scalar outputs. * * @doc {heading: 'Models', subheading: 'Classes'} */ evaluate(x, y, args = {}) { const batchSize = args.batchSize == null ? 32 : args.batchSize; checkBatchSize(batchSize); // TODO(cais): Standardize `config.sampleWeights` as well. // Validate user data. const checkBatchAxis = true; const standardizedOuts = this.standardizeUserDataXY(x, y, checkBatchAxis, batchSize); try { // TODO(cais): If uses `useLearningPhase`, set the corresponding element // of the input to 0. const ins = standardizedOuts[0].concat(standardizedOuts[1]); this.makeTestFunction(); const f = this.testFunction; const testOuts = this.testLoop(f, ins, batchSize, args.verbose, args.steps); return singletonOrArray(testOuts); } finally { disposeNewTensors(standardizedOuts[0], x); disposeNewTensors(standardizedOuts[1], y); } } // TODO(cais): Add code snippet below once real dataset objects are // available. /** * Evaluate model using a dataset object. * * Note: Unlike `evaluate()`, this method is asynchronous (`async`). * * @param dataset A dataset object. Its `iterator()` method is expected * to generate a dataset iterator object, the `next()` method of which * is expected to produce data batches for evaluation. The return value * of the `next()` call ought to contain a boolean `done` field and a * `value` field. The `value` field is expected to be an array of two * `tf.Tensor`s or an array of two nested `tf.Tensor` structures. The former * case is for models with exactly one input and one output (e.g. * a sequential model). The latter case is for models with multiple * inputs and/or multiple outputs. Of the two items in the array, the * first is the input feature(s) and the second is the output target(s). * @param args A configuration object for the dataset-based evaluation. * @returns Loss and metric values as an Array of `Scalar` objects. * * @doc {heading: 'Models', subheading: 'Classes'} */ async evaluateDataset(dataset, args) { this.makeTestFunction(); return evaluateDataset(this, dataset, args); } /** * Get number of samples provided for training, evaluation or prediction. * * @param ins Input `tf.Tensor`. * @param batchSize Integer batch size, optional. * @param steps Total number of steps (batches of samples) before * declaring loop finished. Optional. * @param stepsName The public API's parameter name for `steps`. * @returns Number of samples provided. */ checkNumSamples(ins, batchSize, steps, stepsName = 'steps') { let numSamples; if (steps != null) { numSamples = null; if (batchSize != null) { throw new ValueError(`If ${stepsName} is set, batchSize must be null or undefined.` + `Got batchSize = ${batchSize}`); } } else if (ins != null) { if (Array.isArray(ins)) { numSamples = ins[0].shape[0]; } else { numSamples = ins.shape[0]; } } else { throw new ValueError(`Either the input data should have a defined shape, or ` + `${stepsName} shoud be specified.`); } return numSamples; } /** * Execute internal tensors of the model with input data feed. * @param inputs Input data feed. Must match the inputs of the model. * @param outputs Names of the output tensors to be fetched. Must match * names of the SymbolicTensors that belong to the graph. * @returns Fetched values for `outputs`. */ execute(inputs, outputs) { if (Array.isArray(outputs) && outputs.length === 0) { throw new ValueError('`outputs` is an empty Array, which is not allowed.'); } const outputsIsArray = Array.isArray(outputs); const outputNames = (outputsIsArray ? outputs : [outputs]); const outputSymbolicTensors = this.retrieveSymbolicTensors(outputNames); // Format the input into a FeedDict. const feedDict = new FeedDict(); if (inputs instanceof Tensor$1) { inputs = [inputs]; } if (Array.isArray(inputs)) { if (inputs.length !== this.inputs.length) { throw new ValueError(`The number of inputs provided (${inputs.length}) ` + `does not match the number of inputs of this model ` + `(${this.inputs.length}).`); } for (let i = 0; i < this.inputs.length; ++i) { feedDict.add(this.inputs[i], inputs[i]); } } else { for (const input of this.inputs) { const tensorValue = inputs[input.name]; if (tensorValue == null) { throw new ValueError(`No value is provided for the model's input ${input.name}`); } feedDict.add(input, tensorValue); } } // Run execution. const executeOutputs = execute(outputSymbolicTensors, feedDict); return outputsIsArray ? executeOutputs : executeOutputs[0]; } /** * Retrieve the model's internal symbolic tensors from symbolic-tensor names. */ retrieveSymbolicTensors(symbolicTensorNames) { const outputSymbolicTensors = pyListRepeat(null, symbolicTensorNames.length); let outputsRemaining = symbolicTensorNames.length; for (const layer of this.layers) { const layerOutputs = Array.isArray(layer.output) ? layer.output : [layer.output]; const layerOutputNames = layerOutputs.map(output => output.name); for (let i = 0; i < symbolicTensorNames.length; ++i) { const index = layerOutputNames.indexOf(symbolicTensorNames[i]); if (index !== -1) { outputSymbolicTensors[i] = layerOutputs[index]; outputsRemaining--; } if (outputsRemaining === 0) { break; } } if (outputsRemaining === 0) { break; } } if (outputsRemaining > 0) { const remainingNames = []; outputSymbolicTensors.forEach((tensor, i) => { if (tensor == null) { remainingNames.push(symbolicTensorNames[i]); } }); throw new ValueError(`Cannot find SymbolicTensors for output name(s): ` + `${JSON.stringify(remainingNames)}`); } return outputSymbolicTensors; } /** * Helper method to loop over some data in batches. * * Porting Note: Not using the functional approach in the Python equivalent * due to the imperative backend. * Porting Note: Does not support step mode currently. * * @param ins: input data * @param batchSize: integer batch size. * @param verbose: verbosity model * @returns: Predictions as `tf.Tensor` (if a single output) or an `Array` of * `tf.Tensor` (if multipe outputs). */ predictLoop(ins, batchSize = 32, verbose = false) { return tfc.tidy(() => { const numSamples = this.checkNumSamples(ins); if (verbose) { throw new NotImplementedError('Verbose predictLoop() is not implemented yet.'); } // Sample-based predictions. // Porting Note: Tensor currently does not support sliced assignments as // in numpy, e.g., x[1:3] = y. Therefore we use concatenation while // iterating over the batches. const batches = makeBatches(numSamples, batchSize); const outsBatches = this.outputs.map(output => []); // TODO(cais): Can the scope() be pushed down inside the for loop? for (let batchIndex = 0; batchIndex < batches.length; ++batchIndex) { const batchOuts = tfc.tidy(() => { const batchStart = batches[batchIndex][0]; const batchEnd = batches[batchIndex][1]; // TODO(cais): Take care of the case of the last element is a flag for // training/test. const insBatch = sliceArrays(ins, batchStart, batchEnd); // Construct the feeds for execute(); const feeds = []; if (Array.isArray(insBatch)) { for (let i = 0; i < insBatch.length; ++i) { feeds.push({ key: this.inputs[i], value: insBatch[i] }); } } else { feeds.push({ key: this.inputs[0], value: insBatch }); } const feedDict = new FeedDict(feeds); return execute(this.outputs, feedDict); }); batchOuts.forEach((batchOut, i) => outsBatches[i].push(batchOut)); } return singletonOrArray(outsBatches.map(batches => tfc.concat(batches, 0))); }); } /** * 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. * * ```js * const model = tf.sequential({ * layers: [tf.layers.dense({units: 1, inputShape: [10]})] * }); * model.predict(tf.ones([8, 10]), {batchSize: 4}).print(); * ``` * * @param x The input data, as a Tensor, or an `Array` of `tf.Tensor`s if * the model has multiple inputs. * @param args A `ModelPredictArgs` object containing optional fields. * * @return Prediction results as a `tf.Tensor`(s). * * @exception ValueError In case of mismatch between the provided input data * and the model's expectations, or in case a stateful model receives a * number of samples that is not a multiple of the batch size. * * @doc {heading: 'Models', subheading: 'Classes'} */ predict(x, args = {}) { const xsRank2OrHigher = ensureTensorsRank2OrHigher(x); checkInputData(xsRank2OrHigher, this.inputNames, this.feedInputShapes, false); try { // TODO(cais): Take care of stateful models. // if (this.stateful) ... // TODO(cais): Take care of the learning_phase boolean flag. // if (this.useLearningPhase) ... const batchSize = args.batchSize == null ? 32 : args.batchSize; checkBatchSize(batchSize); return this.predictLoop(xsRank2OrHigher, batchSize); } finally { disposeNewTensors(xsRank2OrHigher, x); } } /** * Returns predictions for a single batch of samples. * * ```js * const model = tf.sequential({ * layers: [tf.layers.dense({units: 1, inputShape: [10]})] * }); * model.predictOnBatch(tf.ones([8, 10])).print(); * ``` * @param x: Input samples, as a Tensor (for models with exactly one * input) or an array of Tensors (for models with more than one input). * @return Tensor(s) of predictions * * @doc {heading: 'Models', subheading: 'Classes'} */ predictOnBatch(x) { checkInputData(x, this.inputNames, this.feedInputShapes, true); // TODO(cais): Take care of the learning_phase boolean flag. // if (this.useLearningPhase) ... const batchSize = (Array.isArray(x) ? x[0] : x).shape[0]; return this.predictLoop(x, batchSize); } standardizeUserDataXY(x, y, checkBatchAxis = true, batchSize) { // TODO(cais): Add sampleWeight, classWeight if (this.optimizer_ == null) { throw new RuntimeError('You must compile a model before training/testing. Use ' + 'LayersModel.compile(modelCompileArgs).'); } const outputShapes = []; for (let i = 0; i < this.feedOutputShapes.length; ++i) { const outputShape = this.feedOutputShapes[i]; const lossFn = this.feedLossFns[i]; if (lossFn === sparseCategoricalCrossentropy$1) { outputShapes.push(outputShape.slice(0, outputShape.length - 1).concat([1])); } else { // Porting Note: Because of strong typing `lossFn` must be a function. outputShapes.push(outputShape); } } x = standardizeInputData(x, this.feedInputNames, this.feedInputShapes, false, 'input'); y = standardizeInputData(y, this.feedOutputNames, outputShapes, false, 'target'); // TODO(cais): Standardize sampleWeights & classWeights. checkArrayLengths(x, y); // TODO(cais): Check sampleWeights as well. checkLossAndTargetCompatibility(y, this.feedLossFns, this.feedOutputShapes); if (this.stateful && batchSize != null && batchSize > 0) { if (x[0].shape[0] % batchSize !== 0) { throw new ValueError(`In a stateful network, you should only pass inputs with a ` + `number of samples that is divisible by the batch size ` + `${batchSize}. Found: ${x[0].shape[0]} sample(s).`); } } return [x, y]; } async standardizeUserData(x, y, sampleWeight, classWeight, checkBatchAxis = true, batchSize) { const [standardXs, standardYs] = this.standardizeUserDataXY(x, y, checkBatchAxis, batchSize); // TODO(cais): Handle sampleWeights. if (sampleWeight != null) { throw new Error('sample weight is not supported yet.'); } let standardSampleWeights = null; if (classWeight != null) { const classWeights = standardizeClassWeights(classWeight, this.outputNames); standardSampleWeights = []; for (let i = 0; i < classWeights.length; ++i) { standardSampleWeights.push(await standardizeWeights(standardYs[i], null, classWeights[i])); } } // TODO(cais): Deal with the case of model.stateful == true. return [standardXs, standardYs, standardSampleWeights]; } /** * Loop over some test data in batches. * @param f A Function returning a list of tensors. * @param ins Array of tensors to be fed to `f`. * @param batchSize Integer batch size or `null` / `undefined`. * @param verbose verbosity mode. * @param steps Total number of steps (batches of samples) before * declaring test finished. Ignored with the default value of `null` / * `undefined`. * @returns Array of Scalars. */ testLoop(f, ins, batchSize, verbose = 0, steps) { return tfc.tidy(() => { const numSamples = this.checkNumSamples(ins, batchSize, steps, 'steps'); const outs = []; if (verbose > 0) { throw new NotImplementedError('Verbose mode is not implemented yet.'); } // TODO(cais): Use `indicesForConversionToDense' to prevent slow down. if (steps != null) { throw new NotImplementedError('steps mode in testLoop() is not implemented yet'); } else { const batches = makeBatches(numSamples, batchSize); const indexArray = tensor1d(range(0, numSamples)); for (let batchIndex = 0; batchIndex < batches.length; ++batchIndex) { const batchStart = batches[batchIndex][0]; const batchEnd = batches[batchIndex][1]; const batchIds = sliceAlongFirstAxis(indexArray, batchStart, batchEnd - batchStart); // TODO(cais): In ins, train flag can be a number, instead of an // Tensor? Do we need to handle this in tfjs-layers? const insBatch = sliceArraysByIndices(ins, batchIds); const batchOuts = f(insBatch); if (batchIndex === 0) { for (let i = 0; i < batchOuts.length; ++i) { outs.push(scalar$1(0)); } } for (let i = 0; i < batchOuts.length; ++i) { const batchOut = batchOuts[i]; outs[i] = tfc.add(outs[i], tfc.mul(batchEnd - batchStart, batchOut)); } } for (let i = 0; i < outs.length; ++i) { outs[i] = tfc.div(outs[i], numSamples); } } return outs; }); } getDedupedMetricsNames() { const outLabels = this.metricsNames; // Rename duplicated metrics names (can happen with an output layer // shared among multiple dataflows). const dedupedOutLabels = []; for (let i = 0; i < outLabels.length; ++i) { const label = outLabels[i]; let newLabel = label; if (count(outLabels, label) > 1) { const dupIndex = count(outLabels.slice(0, i), label); newLabel += `_${dupIndex}`; } dedupedOutLabels.push(newLabel); } return dedupedOutLabels; } /** * Creates a function that performs the following actions: * * 1. computes the losses * 2. sums them to get the total loss * 3. call the optimizer computes the gradients of the LayersModel's * trainable weights w.r.t. the total loss and update the variables * 4. calculates the metrics * 5. returns the values of the losses and metrics. */ makeTrainFunction() { return (data) => { const lossValues = []; const inputs = data.slice(0, this.inputs.length); const targets = data.slice(this.inputs.length, this.inputs.length + this.outputs.length); const sampleWeights = data.slice(this.inputs.length + this.outputs.length, this.inputs.length + this.outputs.length * 2); const metricsValues = []; // Create a function that computes the total loss based on the // inputs. This function is used for obtaining gradients through // backprop. const totalLossFunction = () => { const feeds = []; for (let i = 0; i < this.inputs.length; ++i) { feeds.push({ key: this.inputs[i], value: inputs[i] }); } const feedDict = new FeedDict(feeds); const outputs = execute(this.outputs, feedDict, { 'training': true }); // TODO(cais): Take care of the case of multiple outputs from a // single layer? let totalLoss; for (let i = 0; i < this.lossFunctions.length; ++i) { const lossFunction = this.lossFunctions[i]; let loss = lossFunction(targets[i], outputs[i]); if (sampleWeights[i] != null) { loss = computeWeightedLoss(loss, sampleWeights[i]); } // TODO(cais): push Scalar instead. const meanLoss = tfc.mean(loss); // TODO(cais): Use a scope() instead, to avoid ownership. lossValues.push(meanLoss); if (i === 0) { totalLoss = loss; } else { totalLoss = tfc.add(totalLoss, loss); } } // Compute the metrics. // TODO(cais): These should probably be calculated outside // totalLossFunction to benefit speed? for (let i = 0; i < this.metricsTensors.length; ++i) { let weightedMetric; if (this.outputs.length > 1 && i < this.outputs.length) { weightedMetric = lossValues[i]; } else { const metric = this.metricsTensors[i][0]; const outputIndex = this.metricsTensors[i][1]; weightedMetric = tfc.mean(metric(targets[outputIndex], outputs[outputIndex])); } tfc.keep(weightedMetric); // TODO(cais): Use a scope() instead, to avoid ownership. metricsValues.push(weightedMetric); } totalLoss = tfc.mean(totalLoss); // Add regularizer penalties. this.calculateLosses().forEach(regularizerLoss => { totalLoss = tfc.add(totalLoss, regularizerLoss); }); return totalLoss; }; const variables = this.collectedTrainableWeights.map(param => param.read()); const returnCost = true; const totalLossValue = this.optimizer_.minimize(totalLossFunction, returnCost, variables); return [totalLossValue].concat(metricsValues); }; } /** * Create a function which, when invoked with an array of `tf.Tensor`s as a * batch of inputs, returns the prespecified loss and metrics of the model * under the batch of input data. */ makeTestFunction() { this.testFunction = (data) => { return tfc.tidy(() => { const valOutputs = []; let totalLoss; const inputs = data.slice(0, this.inputs.length); const targets = data.slice(this.inputs.length, this.inputs.length + this.outputs.length); const feeds = []; for (let i = 0; i < this.inputs.length; ++i) { feeds.push({ key: this.inputs[i], value: inputs[i] }); } const feedDict = new FeedDict(feeds); const outputs = execute(this.outputs, feedDict); // Compute total loss. for (let i = 0; i < this.lossFunctions.length; ++i) { const lossFunction = this.lossFunctions[i]; // TODO(cais): Add sample weighting and replace the simple // averaging. const loss = tfc.mean(lossFunction(targets[i], outputs[i])); if (i === 0) { totalLoss = loss; } else { totalLoss = tfc.add(totalLoss, loss); } valOutputs.push(totalLoss); } // Compute the metrics. for (let i = 0; i < this.metricsTensors.length; ++i) { const metric = this.metricsTensors[i][0]; const outputIndex = this.metricsTensors[i][1]; // TODO(cais): Replace K.mean() with a proper weighting function. const meanMetric = tfc.mean(metric(targets[outputIndex], outputs[outputIndex])); valOutputs.push(meanMetric); } return valOutputs; }); }; } /** * Trains the model for a fixed number of epochs (iterations on a * dataset). * * ```js * 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]); * } * ``` * * @param x `tf.Tensor` of training data, or an array of `tf.Tensor`s 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.Tensor`s. * @param y `tf.Tensor` of target (label) data, or an array of `tf.Tensor`s 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.Tensor`s. * @param args A `ModelFitArgs`, containing optional fields. * * @return A `History` instance. Its `history` attribute contains all * information collected during training. * * @exception ValueError In case of mismatch between the provided input * data and what the model expects. * * @doc {heading: 'Models', subheading: 'Classes'} */ async fit(x, y, args = {}) { if (this.isTraining) { throw new Error('Cannot start training because another fit() call is ongoing.'); } this.isTraining = true; let inputs; let targets; let originalInputs; let originalTargets; let inputValX; let inputValY; let valX; let valY; let sampleWeights; try { const batchSize = args.batchSize == null ? 32 : args.batchSize; checkBatchSize(batchSize); // Validate user data. // TODO(cais): Support sampleWeight. const checkBatchAxis = false; const standardizedOuts = await this.standardizeUserData(x, y, args.sampleWeight, args.classWeight, checkBatchAxis, batchSize); inputs = standardizedOuts[0]; targets = standardizedOuts[1]; sampleWeights = standardizedOuts[2]; // Prepare validation data. let doValidation = false; let valIns; if (args.validationData != null && args.validationData.length > 0) { doValidation = true; if (args.validationData.length === 2) { // config.validationData consists of valX and valY. inputValX = args.validationData[0]; inputValY = args.validationData[1]; } else if (args.validationData.length === 3) { throw new NotImplementedError('validationData including sample weights is not supported yet.'); } else { throw new ValueError(`When passing validation data, it must contain 2 (valX, valY) ` + `or 3 (valX, valY, valSampleWeight) items; ` + `${args.validationData} is invalid.`); } const checkBatchAxis = true; const valStandardized = await this.standardizeUserData(inputValX, inputValY, null, /** Unused sample weights. */ null, /** Unused class weights. */ checkBatchAxis, batchSize); valX = valStandardized[0]; valY = valStandardized[1]; valIns = valX.concat(valY); // TODO(cais): Add useLearningPhase data properly. } else if (args.validationSplit != null && args.validationSplit > 0 && args.validationSplit < 1) { doValidation = true; // Porting Note: In tfjs-layers, inputs[0] is always a Tensor. const splitAt = Math.floor(inputs[0].shape[0] * (1 - args.validationSplit)); const originalBatchSize = inputs[0].shape[0]; valX = sliceArrays(inputs, splitAt, originalBatchSize); originalInputs = inputs; inputs = sliceArrays(inputs, 0, splitAt); valY = sliceArrays(targets, splitAt, originalBatchSize); originalTargets = targets; targets = sliceArrays(targets, 0, splitAt); // TODO(cais): Once sampleWeights becomes available, slice it to get // valSampleWeights. valIns = valX.concat(valY); // TODO(cais): Add useLearningPhase data properly. } else if (args.validationSteps != null) { doValidation = true; // TODO(cais): Add useLearningPhase. } const ins = inputs.concat(targets).concat(sampleWeights); this.checkTrainableWeightsConsistency(); // TODO(cais): Handle use_learning_phase and learning_phase? // Porting Note: Here we see a key deviation of tfjs-layers from // Keras. // Due to the imperative nature of tfjs-layers' backend (tfjs-core), // we do not construct symbolic computation graphs to embody the // training process. Instead, we define a function that performs the // training action. In PyKeras, the data (inputs and targets) are fed // through graph placeholders. In tfjs-layers, the data are fed as // function arguments. Since the function are defined below in the // scope, we don't have equivalents of PyKeras's // `_make_train_funciton`. const trainFunction = this.makeTrainFunction(); const outLabels = this.getDedupedMetricsNames(); let valFunction; let callbackMetrics; if (doValidation) { this.makeTestFunction(); valFunction = this.testFunction; callbackMetrics = outLabels.slice().concat(outLabels.map(n => 'val_' + n)); } else { valFunction = null; valIns = []; callbackMetrics = outLabels.slice(); } const callbacks = standardizeCallbacks(args.callbacks, args.yieldEvery); const out = await this.fitLoop(trainFunction, ins, outLabels, batchSize, args.epochs, args.verbose, callbacks, valFunction, valIns, args.shuffle, callbackMetrics, args.initialEpoch, null, null); return out; } finally { this.isTraining = false; // Memory clean up. disposeNewTensors(inputs, x); disposeNewTensors(targets, y); disposeNewTensors(originalInputs, x); disposeNewTensors(originalTargets, y); disposeNewTensors(valX, inputValX); disposeNewTensors(valY, inputValY); if (sampleWeights != null) { tfc.dispose(sampleWeights); } } // TODO(cais): Add value to outLabels. } /** * Abstract fit function for `f(ins)`. * @param f A Function returning a list of tensors. For training, this * function is expected to perform the updates to the variables. * @param ins List of tensors to be fed to `f`. * @param outLabels List of strings, display names of the outputs of `f`. * @param batchSize Integer batch size or `== null` if unknown. Default : 32. * @param epochs Number of times to iterate over the data. Default : 1. * @param verbose Verbosity mode: 0, 1, or 2. Default: 1. * @param callbacks List of callbacks to be called during training. * @param valF Function to call for validation. * @param valIns List of tensors to be fed to `valF`. * @param shuffle Whether to shuffle the data at the beginning of every * epoch. Default : true. * @param callbackMetrics List of strings, the display names of the metrics * passed to the callbacks. They should be the concatenation of the * display names of the outputs of `f` and the list of display names * of the outputs of `valF`. * @param initialEpoch Epoch at which to start training (useful for * resuming a previous training run). Default : 0. * @param stepsPerEpoch Total number of steps (batches on samples) before * declaring one epoch finished and starting the next epoch. Ignored with * the default value of `undefined` or `null`. * @param validationSteps Number of steps to run validation for (only if * doing validation from data tensors). Not applicable for tfjs-layers. * @returns A `History` object. */ async fitLoop(f, ins, outLabels, batchSize, epochs, verbose, callbacks, valF, valIns, shuffle, callbackMetrics, initialEpoch, stepsPerEpoch, validationSteps) { if (batchSize == null) { batchSize = 32; } if (epochs == null) { epochs = 1; } if (shuffle == null) { shuffle = true; } if (initialEpoch == null) { initialEpoch = 0; } // TODO(cais): Change const to let below when implementing validation. let doValidation = false; if (valF != null && valIns != null) { doValidation = true; // TODO(cais): verbose message. } if (validationSteps != null) { doValidation = true; if (stepsPerEpoch == null) { throw new ValueError('Can only use `validationSteps` when doing step-wise training, ' + 'i.e., `stepsPerEpoch` must be set.'); } } const numTrainSamples = this.checkNumSamples(ins, batchSize, stepsPerEpoch, 'steps_per_epoch'); let indexArray; if (numTrainSamples != null) { indexArray = range(0, numTrainSamples); } if (verbose == null) { verbose = 1; } const { callbackList, history } = configureCallbacks(callbacks, verbose, epochs, initialEpoch, numTrainSamples, stepsPerEpoch, batchSize, doValidation, callbackMetrics); callbackList.setModel(this); this.history = history; await callbackList.onTrainBegin(); this.stopTraining_ = false; // TODO(cais): Take care of callbacks.validation_data as in PyKeras. // TODO(cais): Pre-convert feeds for performance as in PyKeras. for (let epoch = initialEpoch; epoch < epochs; ++epoch) { await callbackList.onEpochBegin(epoch); const epochLogs = {}; if (stepsPerEpoch != null) { throw new NotImplementedError('stepsPerEpoch mode is not implemented yet.'); } else { if (shuffle === 'batch') { throw new NotImplementedError('batch shuffling is not implemneted' + ' yet'); } else if (shuffle) { util.shuffle(indexArray); } // Convert the potentially shuffled indices to Tensor1D, to avoid the // cost of repeated creation of Array1Ds later on. const epochIndexArray1D = tensor1d(indexArray); const batches = makeBatches(numTrainSamples, batchSize); for (let batchIndex = 0; batchIndex < batches.length; ++batchIndex) { const batchLogs = {}; await callbackList.onBatchBegin(batchIndex, batchLogs); tfc.tidy(() => { const batchStart = batches[batchIndex][0]; const batchEnd = batches[batchIndex][1]; const batchIds = sliceAlongFirstAxis(epochIndexArray1D, batchStart, batchEnd - batchStart); batchLogs['batch'] = batchIndex; batchLogs['size'] = batchEnd - batchStart; // TODO(cais): In ins, train flag can be a number, instead of an // Tensor? Do we need to handle this in tfjs-layers? const insBatch = sliceArraysByIndices(ins, batchIds); const outs = f(insBatch); for (let i = 0; i < outLabels.length; ++i) { const label = outLabels[i]; const out = outs[i]; batchLogs[label] = out; tfc.keep(out); // TODO(cais): Use scope() to avoid ownership. } if (batchIndex === batches.length - 1) { // Last batch. if (doValidation) { const valOuts = this.testLoop(valF, valIns, batchSize); // Porting Notes: In tfjs-layers, valOuts is always an Array. for (let i = 0; i < outLabels.length; ++i) { const label = outLabels[i]; const out = valOuts[i]; tfc.keep(out); // TODO(cais): Use scope() to avoid ownership. epochLogs['val_' + label] = out; } } } }); await callbackList.onBatchEnd(batchIndex, batchLogs); disposeTensorsInLogs(batchLogs); if (this.stopTraining_) { break; } // TODO(cais): return outs as list of Tensor. } epochIndexArray1D.dispose(); } // TODO(cais): Run validation at the end of the epoch. await callbackList.onEpochEnd(epoch, epochLogs); if (this.stopTraining_) { break; } } await callbackList.onTrainEnd(); await this.history.syncData(); return this.history; } // TODO(cais): Add code snippet below when it's possible to instantiate // actual dataset objects. /** * Trains the model using a dataset object. * * @param dataset A dataset object. Its `iterator()` method is expected * to generate a dataset iterator object, the `next()` method of which * is expected to produce data batches for training. The return value * of the `next()` call ought to contain a boolean `done` field and a * `value` field. The `value` field is expected to be an array of two * `tf.Tensor`s or an array of two nested `tf.Tensor` structures. The former * case is for models with exactly one input and one output (e.g. * a sequential model). The latter case is for models with multiple * inputs and/or multiple outputs. * Of the two items in the array, the first is the input feature(s) and * the second is the output target(s). * @param args A `ModelFitDatasetArgs`, containing optional fields. * * @return A `History` instance. Its `history` attribute contains all * information collected during training. * * @doc {heading: 'Models', subheading: 'Classes'} */ async fitDataset(dataset, args) { return fitDataset(this, dataset, args); } /** * Runs a single gradient update on a single batch of data. * * This method differs from `fit()` and `fitDataset()` in the following * regards: * - It operates on exactly one batch of data. * - It returns only the loss and metric values, instead of * returning the batch-by-batch loss and metric values. * - It doesn't support fine-grained options such as verbosity and * callbacks. * * @param x Input data. It could be one of the following: * - A `tf.Tensor`, or an Array of `tf.Tensor`s (in case the model has * multiple inputs). * - An Object mapping input names to corresponding `tf.Tensor` (if the * model has named inputs). * @param y Target data. It could be either a `tf.Tensor` or multiple * `tf.Tensor`s. It should be consistent with `x`. * @returns Training loss or losses (in case the model has * multiple outputs), along with metrics (if any), as numbers. * * @doc {heading: 'Models', subheading: 'Classes'} */ async trainOnBatch(x, y) { // TODO(cais): Support sampleWeight and classWeight. // TODO(cais): Support Dataset objects. const standardizeOut = await this.standardizeUserData(x, y); const inputs = standardizeOut[0]; const targets = standardizeOut[1]; const trainFunction = this.makeTrainFunction(); const losses = trainFunction(inputs.concat(targets)); const lossValues = []; for (const loss of losses) { const v = await loss.data(); lossValues.push(v[0]); } tfc.dispose(losses); disposeNewTensors(standardizeOut[0], x); disposeNewTensors(standardizeOut[1], y); return singletonOrArray(lossValues); } /** * Extract weight values of the model. * * @param config: An instance of `io.SaveConfig`, which specifies * model-saving options such as whether only trainable weights are to be * saved. * @returns A `NamedTensorMap` mapping original weight names (i.e., * non-uniqueified weight names) to their values. */ getNamedWeights(config) { const namedWeights = []; const trainableOnly = config != null && config.trainableOnly; const weights = trainableOnly ? this.trainableWeights : this.weights; const weightValues = this.getWeights(trainableOnly); for (let i = 0; i < weights.length; ++i) { if (trainableOnly && !weights[i].trainable) { // Optionally skip non-trainable weights. continue; } namedWeights.push({ name: weights[i].originalName, tensor: weightValues[i] }); } return namedWeights; } /** * Setter used for force stopping of LayersModel.fit() (i.e., training). * * Example: * * ```js * const input = tf.input({shape: [10]}); * const output = tf.layers.dense({units: 1}).apply(input); * const model = tf.model({inputs: [input], outputs: [output]}); * model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); * const xs = tf.ones([8, 10]); * const ys = tf.zeros([8, 1]); * * const history = await model.fit(xs, ys, { * epochs: 10, * callbacks: { * onEpochEnd: async (epoch, logs) => { * if (epoch === 2) { * model.stopTraining = true; * } * } * } * }); * * // There should be only 3 values in the loss array, instead of 10 * values, * // due to the stopping after 3 epochs. * console.log(history.history.loss); * ``` */ set stopTraining(stop) { this.stopTraining_ = stop; } get stopTraining() { return this.stopTraining_; } get optimizer() { return this.optimizer_; } set optimizer(optimizer) { if (this.optimizer_ !== optimizer) { this.optimizer_ = optimizer; this.isOptimizerOwned = false; } } dispose() { const result = super.dispose(); if (result.refCountAfterDispose === 0 && this.optimizer != null && this.isOptimizerOwned) { const numTensorsBeforeOptmizerDisposal = tfc.memory().numTensors; this.optimizer_.dispose(); result.numDisposedVariables += numTensorsBeforeOptmizerDisposal - tfc.memory().numTensors; } return result; } getLossIdentifiers() { let lossNames; if (typeof this.loss === 'string') { lossNames = toSnakeCase(this.loss); } else if (Array.isArray(this.loss)) { for (const loss of this.loss) { if (typeof loss !== 'string') { throw new Error('Serialization of non-string loss is not supported.'); } } lossNames = this.loss.map(name => toSnakeCase(name)); } else { const outputNames = Object.keys(this.loss); lossNames = {}; const losses = this.loss; for (const outputName of outputNames) { if (typeof losses[outputName] === 'string') { lossNames[outputName] = toSnakeCase(losses[outputName]); } else { throw new Error('Serialization of non-string loss is not supported.'); } } } return lossNames; } getMetricIdentifiers() { if (typeof this.metrics === 'string' || typeof this.metrics === 'function') { return [toSnakeCase(getLossOrMetricName(this.metrics))]; } else if (Array.isArray(this.metrics)) { return this.metrics.map(metric => toSnakeCase(getLossOrMetricName(metric))); } else { const metricsIdentifiers = {}; for (const key in this.metrics) { metricsIdentifiers[key] = toSnakeCase(getLossOrMetricName(this.metrics[key])); } return metricsIdentifiers; } } getTrainingConfig() { return { loss: this.getLossIdentifiers(), metrics: this.getMetricIdentifiers(), optimizer_config: { class_name: this.optimizer.getClassName(), config: this.optimizer.getConfig() } }; // TODO(cais): Add weight_metrics when they are supported. // TODO(cais): Add sample_weight_mode when it's supported. // TODO(cais): Add loss_weights when it's supported. } loadTrainingConfig(trainingConfig) { if (trainingConfig.weighted_metrics != null) { throw new Error('Loading weight_metrics is not supported yet.'); } if (trainingConfig.loss_weights != null) { throw new Error('Loading loss_weights is not supported yet.'); } if (trainingConfig.sample_weight_mode != null) { throw new Error('Loading sample_weight_mode is not supported yet.'); } const tsConfig = convertPythonicToTs(trainingConfig.optimizer_config); const optimizer = deserialize(tsConfig); let loss; if (typeof trainingConfig.loss === 'string') { loss = toCamelCase(trainingConfig.loss); } else if (Array.isArray(trainingConfig.loss)) { loss = trainingConfig.loss.map(lossEntry => toCamelCase(lossEntry)); } else if (trainingConfig.loss != null) { loss = {}; for (const key in trainingConfig.loss) { loss[key] = toCamelCase(trainingConfig.loss[key]); } } let metrics; if (Array.isArray(trainingConfig.metrics)) { metrics = trainingConfig.metrics.map(metric => toCamelCase(metric)); } else if (trainingConfig.metrics != null) { metrics = {}; for (const key in trainingConfig.metrics) { metrics[key] = toCamelCase(trainingConfig.metrics[key]); } } this.compile({ loss, metrics, optimizer }); } /** * Save the configuration and/or weights of the LayersModel. * * An `IOHandler` is an object that has a `save` method of the proper * signature defined. The `save` method manages the storing or * transmission of serialized data ("artifacts") that represent the * model's topology and weights onto or via a specific medium, such as * file downloads, local storage, IndexedDB in the web browser and HTTP * requests to a server. TensorFlow.js provides `IOHandler` * implementations for a number of frequently used saving mediums, such as * `tf.io.browserDownloads` and `tf.io.browserLocalStorage`. See `tf.io` * for more details. * * This method also allows you to refer to certain types of `IOHandler`s * as URL-like string shortcuts, such as 'localstorage://' and * 'indexeddb://'. * * Example 1: Save `model`'s topology and weights to browser [local * storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage); * then load it back. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * console.log('Prediction from original model:'); * model.predict(tf.ones([1, 3])).print(); * * const saveResults = await model.save('localstorage://my-model-1'); * * const loadedModel = await tf.loadLayersModel('localstorage://my-model-1'); * console.log('Prediction from loaded model:'); * loadedModel.predict(tf.ones([1, 3])).print(); * ``` * * Example 2. Saving `model`'s topology and weights to browser * [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API); * then load it back. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * console.log('Prediction from original model:'); * model.predict(tf.ones([1, 3])).print(); * * const saveResults = await model.save('indexeddb://my-model-1'); * * const loadedModel = await tf.loadLayersModel('indexeddb://my-model-1'); * console.log('Prediction from loaded model:'); * loadedModel.predict(tf.ones([1, 3])).print(); * ``` * * Example 3. Saving `model`'s topology and weights as two files * (`my-model-1.json` and `my-model-1.weights.bin`) downloaded from * browser. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * const saveResults = await model.save('downloads://my-model-1'); * ``` * * Example 4. Send `model`'s topology and weights to an HTTP server. * See the documentation of `tf.io.http` for more details * including specifying request parameters and implementation of the * server. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * const saveResults = await model.save('http://my-server/model/upload'); * ``` * * @param handlerOrURL An instance of `IOHandler` or a URL-like, * scheme-based string shortcut for `IOHandler`. * @param config Options for saving the model. * @returns A `Promise` of `SaveResult`, which summarizes the result of * the saving, such as byte sizes of the saved artifacts for the model's * topology and weight values. * * @doc {heading: 'Models', subheading: 'Classes', ignoreCI: true} */ async save(handlerOrURL, config) { if (typeof handlerOrURL === 'string') { const handlers = io.getSaveHandlers(handlerOrURL); if (handlers.length === 0) { throw new ValueError(`Cannot find any save handlers for URL '${handlerOrURL}'`); } else if (handlers.length > 1) { throw new ValueError(`Found more than one (${handlers.length}) save handlers for ` + `URL '${handlerOrURL}'`); } handlerOrURL = handlers[0]; } if (handlerOrURL.save == null) { throw new ValueError('LayersModel.save() cannot proceed because the IOHandler ' + 'provided does not have the `save` attribute defined.'); } const weightDataAndSpecs = await io.encodeWeights(this.getNamedWeights(config)); const returnString = false; const unusedArg = null; const modelConfig = this.toJSON(unusedArg, returnString); const modelArtifacts = { modelTopology: modelConfig, format: LAYERS_MODEL_FORMAT_NAME, generatedBy: `TensorFlow.js tfjs-layers v${version}`, convertedBy: null, }; const includeOptimizer = config == null ? false : config.includeOptimizer; if (includeOptimizer && this.optimizer != null) { modelArtifacts.trainingConfig = this.getTrainingConfig(); const weightType = 'optimizer'; const { data: optimizerWeightData, specs: optimizerWeightSpecs } = await io.encodeWeights(await this.optimizer.getWeights(), weightType); weightDataAndSpecs.specs.push(...optimizerWeightSpecs); weightDataAndSpecs.data = io.concatenateArrayBuffers([weightDataAndSpecs.data, optimizerWeightData]); } if (this.userDefinedMetadata != null) { // Check serialized size of user-defined metadata. const checkSize = true; checkUserDefinedMetadata(this.userDefinedMetadata, this.name, checkSize); modelArtifacts.userDefinedMetadata = this.userDefinedMetadata; } modelArtifacts.weightData = weightDataAndSpecs.data; modelArtifacts.weightSpecs = weightDataAndSpecs.specs; return handlerOrURL.save(modelArtifacts); } /** * Set user-defined metadata. * * The set metadata will be serialized together with the topology * and weights of the model during `save()` calls. * * @param setUserDefinedMetadata */ setUserDefinedMetadata(userDefinedMetadata) { checkUserDefinedMetadata(userDefinedMetadata, this.name); this.userDefinedMetadata = userDefinedMetadata; } /** * Get user-defined metadata. * * The metadata is supplied via one of the two routes: * 1. By calling `setUserDefinedMetadata()`. * 2. Loaded during model loading (if the model is constructed * via `tf.loadLayersModel()`.) * * If no user-defined metadata is available from either of the * two routes, this function will return `undefined`. */ getUserDefinedMetadata() { return this.userDefinedMetadata; } } // The class name is 'Model' rather than 'LayersModel' for backwards // compatibility since this class name shows up in the serialization format. /** @nocollapse */ LayersModel.className = 'Model'; serialization.registerClass(LayersModel); /** * A `tf.Functional` is an alias to `tf.LayersModel`. * * See also: * `tf.LayersModel`, `tf.Sequential`, `tf.loadLayersModel`. */ /** @doc {heading: 'Models', subheading: 'Classes'} */ class Functional extends LayersModel { } Functional.className = 'Functional'; serialization.registerClass(Functional); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Parses a JSON model configuration file and returns a model instance. * * ```js * // This example shows how to serialize a model using `toJSON()` and * // deserialize it as another model using `tf.models.modelFromJSON()`. * // Note: this example serializes and deserializes only the topology * // of the model; the weights of the loaded model will be different * // from those of the the original model, due to random weight * // initialization. * // To load the topology and weights of a model, use `tf.loadLayersModel()`. * const model1 = tf.sequential(); * model1.add(tf.layers.repeatVector({inputShape: [2], n: 4})); * // Serialize `model1` as a JSON object. * const model1JSON = model1.toJSON(null, false); * model1.summary(); * * const model2 = await tf.models.modelFromJSON(model1JSON); * model2.summary(); * ``` * * @param modelAndWeightsConfig JSON object or string encoding a model and * weights configuration. It can also be only the topology JSON of the * model, in which case the weights will not be loaded. * @param custom_objects Optional dictionary mapping names * (strings) to custom classes or functions to be * considered during deserialization. * @returns A TensorFlow.js Layers `tf.LayersModel` instance (uncompiled). */ async function modelFromJSON(modelAndWeightsConfig, customObjects) { if (!('modelTopology' in modelAndWeightsConfig)) { modelAndWeightsConfig = { modelTopology: modelAndWeightsConfig }; } modelAndWeightsConfig = modelAndWeightsConfig; let modelTopology = modelAndWeightsConfig.modelTopology; if (modelTopology['model_config'] != null) { // If the model-topology JSON contains a 'model_config' field, then it is // a full model JSON (e.g., from `keras.Model.save()`), which contains // not only the model's architecture in its 'model_config' field, but // additional information such as the model's optimizer. We use only the // 'model_config' field currently. modelTopology = modelTopology['model_config']; } const tsConfig = convertPythonicToTs(modelTopology); const model = deserialize(tsConfig, customObjects); if (modelAndWeightsConfig.weightsManifest != null) { // Load the weight values keyed by the original tensor names in the model // file that was loaded. These should match the keys of the weight // manifest. const weightValues = await io.loadWeights(modelAndWeightsConfig.weightsManifest, modelAndWeightsConfig.pathPrefix, model.weights.map(weight => weight.originalName)); // Map the weights to the unique tensor names generated during model loading const uniqueWeightValues = {}; for (const weight of model.weights) { uniqueWeightValues[weight.originalName] = weightValues[weight.originalName]; } model.loadWeights(uniqueWeightValues); // Dispose temporary weight values. dispose(weightValues); } return model; } /** * Load a model composed of Layer objects, including its topology and optionally * weights. See the Tutorial named "How to import a Keras Model" for usage * examples. * * This method is applicable to: * * 1. Models created with the `tf.layers.*`, `tf.sequential`, and * `tf.model` APIs of TensorFlow.js and later saved with the * `tf.LayersModel.save` method. * 2. Models converted from Keras or TensorFlow tf.keras using the * [tensorflowjs_converter](https://github.com/tensorflow/tfjs/tree/master/tfjs-converter). * * This mode is *not* applicable to TensorFlow `SavedModel`s or their converted * forms. For those models, use `tf.loadGraphModel`. * * Example 1. Load a model from an HTTP server. * * ```js * const model = await tf.loadLayersModel( * 'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json'); * model.summary(); * ``` * * Example 2: Save `model`'s topology and weights to browser [local * storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage); * then load it back. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * console.log('Prediction from original model:'); * model.predict(tf.ones([1, 3])).print(); * * const saveResults = await model.save('localstorage://my-model-1'); * * const loadedModel = await tf.loadLayersModel('localstorage://my-model-1'); * console.log('Prediction from loaded model:'); * loadedModel.predict(tf.ones([1, 3])).print(); * ``` * * Example 3. Saving `model`'s topology and weights to browser * [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API); * then load it back. * * ```js * const model = tf.sequential( * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]}); * console.log('Prediction from original model:'); * model.predict(tf.ones([1, 3])).print(); * * const saveResults = await model.save('indexeddb://my-model-1'); * * const loadedModel = await tf.loadLayersModel('indexeddb://my-model-1'); * console.log('Prediction from loaded model:'); * loadedModel.predict(tf.ones([1, 3])).print(); * ``` * * Example 4. Load a model from user-selected files from HTML * [file input * elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file). * * ```js * // Note: this code snippet will not work without the HTML elements in the * // page * const jsonUpload = document.getElementById('json-upload'); * const weightsUpload = document.getElementById('weights-upload'); * * const model = await tf.loadLayersModel( * tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]])); * ``` * * @param pathOrIOHandler Can be either of the two formats * 1. A string path to the `ModelAndWeightsConfig` JSON describing * the model in the canonical TensorFlow.js format. For file:// * (tfjs-node-only), http:// and https:// schemas, the path can be * either absolute or relative. 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. * 2. A `tf.io.IOHandler` object that loads model artifacts with its `load` * method. * @param options Optional configuration arguments for the model loading, * including: * - `strict`: Require that the provided weights exactly match those required * by the layers. Default true. Passing false means that both extra * weights and missing weights will be silently ignored. * - `onProgress`: A progress callback of the form: * `(fraction: number) => void`. This callback can be used to monitor the * model-loading process. * @returns A `Promise` of `tf.LayersModel`, with the topology and weights * loaded. * * @doc {heading: 'Models', subheading: 'Loading'} */ async function loadLayersModel(pathOrIOHandler, options) { if (options == null) { options = {}; } if (typeof pathOrIOHandler === 'string') { const handlers = io.getLoadHandlers(pathOrIOHandler, options); if (handlers.length === 0) { // For backward compatibility: if no load handler can be found, // assume it is a relative http path. // TODO(cais): Reformat the args into a single `LoadOptions` once the core // is refactored. handlers.push(io.browserHTTPRequest(pathOrIOHandler, options)); } else if (handlers.length > 1) { throw new ValueError(`Found more than one (${handlers.length}) load handlers for ` + `URL '${pathOrIOHandler}'`); } pathOrIOHandler = handlers[0]; } return loadLayersModelFromIOHandler(pathOrIOHandler, undefined, options); } /** * Load a model and optionally its weights, using an IOHandler object. * * @param handler The instance of `IOHandler` to be used during the model * loading. * @param customObjects Any optional custom objects to be used during model * loading. * @param strict Whether the weight loading will be done in strict mode. * Default: `true`. */ async function loadLayersModelFromIOHandler(handler, customObjects, options) { if (options == null) { options = {}; } if (handler.load == null) { throw new ValueError('Cannot proceed with model loading because the IOHandler provided ' + 'does not have the `load` method implemented.'); } const artifacts = await handler.load(); let modelTopology = artifacts.modelTopology; if (modelTopology['model_config'] != null) { modelTopology = modelTopology['model_config']; } const strict = options.strict == null ? true : options.strict; // If weights are provided and the weight-loading mode is strict, use // fast weight initialization. This skips costly initializers such as // 'orthogonal' and saves unnecessary computation in cases where // the initialized weight values will immediately be overwritten by // loaded weight values. const fastWeightInit = artifacts.weightData != null && artifacts.weightSpecs != null && strict; const model = deserialize(convertPythonicToTs(modelTopology), customObjects, fastWeightInit); const trainingConfig = artifacts.trainingConfig; if (trainingConfig != null) { model.loadTrainingConfig(trainingConfig); } if (artifacts.userDefinedMetadata != null) { model.setUserDefinedMetadata(artifacts.userDefinedMetadata); } // If weightData is present, load the weights into the model. if (artifacts.weightData != null) { // Loading weights requires weightSpecs. if (artifacts.weightSpecs == null) { throw new ValueError('LayersModel artifacts contains weight data, but not weight specs. ' + 'Therefore loading of weights cannot proceed.'); } const { modelWeights, optimizerWeights } = decodeModelAndOptimizerWeights(artifacts.weightData, artifacts.weightSpecs); model.loadWeights(modelWeights, strict); if (model.optimizer != null && optimizerWeights.length > 0) { await model.optimizer.setWeights(optimizerWeights); } // Dispose temporary weight values. dispose(modelWeights); dispose(optimizerWeights.map(w => w.tensor)); } return model; } function decodeModelAndOptimizerWeights(weightData, specs) { const name2Tensor = io.decodeWeights(weightData, specs); const modelWeights = {}; const optimizerWeights = []; specs.forEach(spec => { if (spec.group === 'optimizer') { optimizerWeights.push({ name: spec.name, tensor: name2Tensor[spec.name] }); } else { modelWeights[spec.name] = name2Tensor[spec.name]; } }); return { modelWeights, optimizerWeights }; } /** * 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`. * * ```js * // Define a model for linear regression. * const model = tf.sequential(); * model.add(tf.layers.dense({units: 1, inputShape: [1]})); * * // Prepare the model for training: Specify the loss and the optimizer. * model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); * * // Generate some synthetic data for training. * const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); * const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); * * // Train the model using the data then do inference on a data point the * // model hasn't seen: * await model.fit(xs, ys); * model.predict(tf.tensor2d([5], [1, 1])).print(); * ``` * * @doc {heading: 'Models', subheading: 'Classes'} */ class Sequential extends LayersModel { constructor(args) { super({ inputs: [], outputs: [] }); args = args || {}; this.trainable = true; this.built = false; // Set model name. this.name = (args.name != null) ? args.name : getUid('sequential_'); // Add to the model any layers passed to the constructor. if (args.layers != null) { for (const layer of args.layers) { this.add(layer); } } } // Helper function to Sequential.add Throws if the new output shape will be // invalid. checkShape(layer) { const shape = layer.inboundNodes[0].outputTensors[0].shape; if (shape.some(x => x < 0)) { throw new ValueError('Negative dimension size caused by adding layer ' + `${layer.name} with input shape [` + `${layer.inboundNodes[0].inputTensors[0].shape}]`); } } /** * Adds a layer instance on top of the layer stack. * * ```js * const model = tf.sequential(); * model.add(tf.layers.dense({units: 8, inputShape: [1]})); * model.add(tf.layers.dense({units: 4, activation: 'relu6'})); * model.add(tf.layers.dense({units: 1, activation: 'relu6'})); * // Note that the untrained model is random at this point. * model.predict(tf.randomNormal([10, 1])).print(); * ``` * @param layer Layer instance. * * @exception ValueError In case the `layer` argument does not know its * input shape. * @exception ValueError In case the `layer` argument has multiple output * tensors, or is already connected somewhere else (forbidden in * `Sequential` models). * * @doc {heading: 'Models', subheading: 'Classes'} */ add(layer) { const isLayerModelInstance = layer instanceof Sequential || layer instanceof LayersModel; let modelLayer; if (isLayerModelInstance) { modelLayer = layer; if (modelLayer.outputs.length !== 1) { throw new ValueError('All layers in a Sequential model ' + 'should have a single output tensor. ' + 'For multi-output layers, ' + 'use the functional API.'); } if (modelLayer.inputs.length !== 1) { throw new ValueError('All layers in a Sequential model ' + 'should have a single input tensor. ' + 'For multi-input layers, ' + 'use the functional API.'); } } if (this.outputs.length === 0) { // first layer in model: check that it is an input layer if (layer.inboundNodes.length === 0) { // create an input layer if (layer.batchInputShape == null) { throw new ValueError('The first layer in a Sequential model must ' + 'get an `inputShape` or `batchInputShape` argument.'); } // Instantiate the input layer. const x = Input({ batchShape: layer.batchInputShape, dtype: layer.dtype, name: layer.name + '_input' }); // This will build the current layer and create the node connecting // the current layer to the input layer we just created. layer.apply(x); } if (isLayerModelInstance) { this.outputs = modelLayer.outputs; this.inputs = modelLayer.inputs; } else { if (layer.inboundNodes.length !== 1) { throw new ValueError('A layer added to a Sequential model must not already be ' + `connected somewhere else. LayersModel received layer ${layer.name} ` + `which has ${layer.inboundNodes.length} pre-existing inbound ` + 'connections.'); } if (layer.inboundNodes[0].outputTensors.length !== 1) { throw new ValueError('All layers in a Sequential model ' + 'should have a single output tensor. ' + 'For multi-output layers, ' + 'use the functional API.'); } this.checkShape(layer); this.outputs = [layer.inboundNodes[0].outputTensors[0]]; this.inputs = getSourceInputs(this.outputs[0]); } this.inboundNodes = []; // We create an input node, which we will keep updated // as we add more layers. // (This call has side effects.) // tslint:disable-next-line:no-unused-expression new Node({ outboundLayer: this, inboundLayers: [], nodeIndices: [], tensorIndices: [], inputTensors: this.inputs, outputTensors: this.outputs, // no model-level masking for now inputMasks: pyListRepeat(null, this.inputs.length), outputMasks: [null], inputShapes: this.inputs.map(x => x.shape), outputShapes: this.outputs[0].shape }); } else { const outputTensor = layer.apply(this.outputs[0]); if (Array.isArray(outputTensor)) { throw new TypeError('All layers in a Sequential model ' + 'should have a single output tensor. ' + 'For multi-output layers, ' + 'use the functional API.'); } this.checkShape(layer); this.outputs = [outputTensor]; // update self.inbound_nodes this.inboundNodes[0].outputTensors = this.outputs; this.inboundNodes[0].outputShapes = [this.outputs[0].shape]; } this.layers.push(layer); this.built = false; } /** * Removes the last layer in the model. * * @exception TypeError if there are no layers in the model. */ pop() { if (this.layers.length === 0) { throw new TypeError('There are no layers in the model.'); } this.layers.pop(); if (this.layers.length === 0) { this.outputs = []; this.inboundNodes = []; this.outboundNodes = []; } else { const lastLayerIndex = this.layers.length - 1; this.layers[lastLayerIndex].outboundNodes = []; this.outputs = [this.layers[lastLayerIndex].output]; // update self.inbound_nodes this.inboundNodes[0].outputTensors = this.outputs; this.inboundNodes[0].outputShapes = [this.outputs[0].shape]; } } call(inputs, kwargs) { if (this.model == null) { this.build(); } return this.model.call(inputs, kwargs); } build(inputShape) { // Call `getExactlyOneShape` without using its return value, // to verify that exactly one input shape is provided. getExactlyOneShape(inputShape); if (this.inputs.length === 0 || this.outputs.length === 0) { throw new TypeError('Sequential model cannot be built: model is empty.' + ' Add some layers first.'); } // actually create the model this.model = new LayersModel({ inputs: this.inputs, outputs: this.outputs[0], name: this.name + '_model' }); this.model.trainable = this.trainable; // mirror model attributes this.supportsMasking = this.model.supportsMasking; // TODO(michaelterry): Add caches this.inputLayers = this.model.inputLayers; this.inputLayersNodeIndices = this.model.inputLayersNodeIndices; this.inputLayersTensorIndices = this.model.inputLayersTensorIndices; this.outputLayers = this.model.outputLayers; this.outputLayersNodeIndices = this.model.outputLayersNodeIndices; this.outputLayersTensorIndices = this.model.outputLayersTensorIndices; this.nodesByDepth = this.model.nodesByDepth; this.containerNodes = this.model.containerNodes; this.outputNames = this.model.outputNames; this.inputNames = this.model.inputNames; // TODO(michaelterry): Add feedInputNames, feedInputs, if needed. // TODO(michaelterry): Add callbackModel if needed. this.built = true; } countParams() { if (!this.built) { this.build(); } return super.countParams(); } /** * Print a text summary of the Sequential model's layers. * * The summary includes * - Name and type of all layers that comprise the model. * - Output shape(s) of the layers * - Number of weight parameters of each layer * - The total number of trainable and non-trainable parameters of the * model. * * ```js * const model = tf.sequential(); * model.add( * tf.layers.dense({units: 100, inputShape: [10], activation: 'relu'})); * model.add(tf.layers.dense({units: 1, activation: 'sigmoid'})); * * model.summary(); * ``` * * @param lineLength Custom line length, in number of characters. * @param positions Custom widths of each of the columns, as either * fractions of `lineLength` (e.g., `[0.5, 0.75, 1]`) or absolute number * of characters (e.g., `[30, 50, 65]`). Each number corresponds to * right-most (i.e., ending) position of a column. * @param printFn Custom print function. Can be used to replace the default * `console.log`. For example, you can use `x => {}` to mute the printed * messages in the console. * * @doc {heading: 'Models', subheading: 'Classes'} */ summary(lineLength, positions, printFn = console.log) { if (!this.built) { this.build(); } super.summary(lineLength, positions, printFn); } /** * Sets the weights of the model. * * @param weights Should be a list of Tensors with shapes and types matching * the output of `model.getWeights()`. */ setWeights(weights) { if (this.model == null) { this.build(); } this.model.setWeights(weights); } /** * 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. * * ```js * 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(); * ``` * * @param x `tf.Tensor` of test data, or an `Array` of `tf.Tensor`s if the * model has multiple inputs. * @param y `tf.Tensor` of target data, or an `Array` of `tf.Tensor`s if the * model has multiple outputs. * @param args A `ModelEvaluateConfig`, containing optional fields. * * @return `Scalar` test loss (if the model has a single output and no * metrics) or `Array` of `Scalar`s (if the model has multiple outputs * and/or metrics). The attribute `model.metricsNames` * will give you the display labels for the scalar outputs. * * @doc {heading: 'Models', subheading: 'Classes'} */ evaluate(x, y, args = {}) { if (!this.built) { throw new RuntimeError('The model needs to be compiled before being used.'); } return this.model.evaluate(x, y, args); } // TODO(cais): Add code snippet below once real dataset objects are // available. /** * Evaluate model using a dataset object. * * Note: Unlike `evaluate()`, this method is asynchronous (`async`). * * @param dataset A dataset object. Its `iterator()` method is expected * to generate a dataset iterator object, the `next()` method of which * is expected to produce data batches for evaluation. The return value * of the `next()` call ought to contain a boolean `done` field and a * `value` field. The `value` field is expected to be an array of two * `tf.Tensor`s or an array of two nested `tf.Tensor` structures. The former * case is for models with exactly one input and one output (e.g. * a sequential model). The latter case is for models with multiple * inputs and/or multiple outputs. Of the two items in the array, the * first is the input feature(s) and the second is the output target(s). * @param args A configuration object for the dataset-based evaluation. * @returns Loss and metric values as an Array of `Scalar` objects. * * @doc {heading: 'Models', subheading: 'Classes'} */ async evaluateDataset(dataset, args) { if (!this.built) { throw new RuntimeError('The model needs to be compiled before being used.'); } return this.model.evaluateDataset(dataset, args); } /** * 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. * * ```js * const model = tf.sequential({ * layers: [tf.layers.dense({units: 1, inputShape: [10]})] * }); * model.predict(tf.ones([2, 10])).print(); * ``` * * @param x The input data, as a Tensor, or an `Array` of `tf.Tensor`s if * the model has multiple inputs. * @param conifg A `ModelPredictConfig` object containing optional fields. * * @return `tf.Tensor`(s) of predictions. * * @exception ValueError In case of mismatch between the provided input data * and the model's expectations, or in case a stateful model receives a * number of samples that is not a multiple of the batch size. * * @doc {heading: 'Models', subheading: 'Classes'} */ predict(x, args = {}) { if (this.model == null) { this.build(); } return this.model.predict(x, args); } /** * Returns predictions for a single batch of samples. * * @param x: Input samples, as a Tensor, or list of Tensors (if the model * has multiple inputs). * @return Tensor(s) of predictions */ predictOnBatch(x) { if (this.model == null) { this.build(); } return this.model.predictOnBatch(x); } /** * See `LayersModel.compile`. * * @param args */ compile(args) { this.build(); this.model.compile(args); this.optimizer_ = this.model.optimizer; // tslint:disable-next-line:no-any this.isOptimizerOwned = this.model.isOptimizerOwned; this.loss = this.model.loss; this.metrics = this.model.metrics; // TODO(cais): Add this.lossWeights, this.sampleWeightMode, // this.weightedMetrics, this.targets. this.metricsTensors = this.model.metricsTensors; this.metricsNames = this.model.metricsNames; // TODO(cais): Add sampleWeights. } get optimizer() { return this.model == null ? undefined : this.model.optimizer; } set optimizer(optimizer) { this.model.optimizer = optimizer; } /** * Trains the model for a fixed number of epochs (iterations on a dataset). * * ```js * 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 * }); * console.log(history.history.loss[0]); * ``` * * @param x `tf.Tensor` of training data, or an array of `tf.Tensor`s 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.Tensor`s. * @param y `tf.Tensor` of target (label) data, or an array of `tf.Tensor`s 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.Tensor`s. * @param args A `ModelFitConfig`, containing optional fields. * * @return A `History` instance. Its `history` attribute contains all * information collected during training. * * @exception ValueError In case of mismatch between the provided input data * and what the model expects. * * @doc {heading: 'Models', subheading: 'Classes'} */ async fit(x, y, args = {}) { if (!this.built) { throw new RuntimeError('The model needs to be compiled before ' + 'being used.'); } return this.model.fit(x, y, args); } /** * Trains the model using a dataset object. * * ```js * const xArray = [ * [1, 1, 1, 1, 1, 1, 1, 1, 1], * [1, 1, 1, 1, 1, 1, 1, 1, 1], * [1, 1, 1, 1, 1, 1, 1, 1, 1], * [1, 1, 1, 1, 1, 1, 1, 1, 1], * ]; * const yArray = [1, 1, 1, 1]; * // Create a dataset from the JavaScript array. * const xDataset = tf.data.array(xArray); * const yDataset = tf.data.array(yArray); * // Zip combines the `x` and `y` Datasets into a single Dataset, the * // iterator of which will return an object containing of two tensors, * // corresponding to `x` and `y`. The call to `batch(4)` will bundle * // four such samples into a single object, with the same keys now pointing * // to tensors that hold 4 examples, organized along the batch dimension. * // The call to `shuffle(4)` causes each iteration through the dataset to * // happen in a different order. The size of the shuffle window is 4. * const xyDataset = tf.data.zip({xs: xDataset, ys: yDataset}) * .batch(4) * .shuffle(4); * const model = tf.sequential({ * layers: [tf.layers.dense({units: 1, inputShape: [9]})] * }); * model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); * const history = await model.fitDataset(xyDataset, { * epochs: 4, * callbacks: {onEpochEnd: (epoch, logs) => console.log(logs.loss)} * }); * ``` * * @param dataset A dataset object. Its `iterator()` method is expected to * generate a dataset iterator object, the `next()` method of which is * expected to produce data batches for evaluation. The return value of the * `next()` call ought to contain a boolean `done` field and a `value` * field. * * The `value` field is expected to be an object of with fields * `xs` and `ys`, which point to the feature tensor and the target tensor, * respectively. This case is for models with exactly one input and one * output (e.g. a sequential model). For example: * ```js * {value: {xs: xsTensor, ys: ysTensor}, done: false} * ``` * * If the model has multiple inputs, the `xs` field of `value` should * be an object mapping input names to their respective feature tensors. * For example: * ```js * { * value: { * xs: { * input_1: xsTensor1, * input_2: xsTensor2 * }, * ys: ysTensor * }, * done: false * } * ``` * If the model has multiple outputs, the `ys` field of `value` should * be an object mapping output names to their respective target tensors. * For example: * ```js * { * value: { * xs: xsTensor, * ys: { * output_1: ysTensor1, * output_2: ysTensor2 * }, * }, * done: false * } * ``` * @param args A `ModelFitDatasetArgs`, containing optional fields. * * @return A `History` instance. Its `history` attribute contains all * information collected during training. * * @doc {heading: 'Models', subheading: 'Classes', ignoreCI: true} */ async fitDataset(dataset, args) { if (!this.built) { throw new RuntimeError('The model needs to be compiled before ' + 'being used.'); } return this.model.fitDataset(dataset, args); } /** * Runs a single gradient update on a single batch of data. * * This method differs from `fit()` and `fitDataset()` in the following * regards: * - It operates on exactly one batch of data. * - It returns only the loss and metric values, instead of * returning the batch-by-batch loss and metric values. * - It doesn't support fine-grained options such as verbosity and * callbacks. * * @param x Input data. It could be one of the following: * - A `tf.Tensor`, or an Array of `tf.Tensor`s (in case the model has * multiple inputs). * - An Object mapping input names to corresponding `tf.Tensor` (if the * model has named inputs). * @param y Target data. It could be either a `tf.Tensor` or multiple * `tf.Tensor`s. It should be consistent with `x`. * @returns Training loss or losses (in case the model has * multiple outputs), along with metrics (if any), as numbers. * * @doc {heading: 'Models', subheading: 'Classes'} */ async trainOnBatch(x, y) { return this.model.trainOnBatch(x, y); } /* See parent class for JsDoc */ /** @nocollapse */ static fromConfig(cls, config, customObjects = {}, fastWeightInit = false) { let configArray; let extraModelConfig = {}; if (config instanceof Array) { if (!(config[0].className != null) || config[0]['className'] === 'Merge') { throw new ValueError('Legacy serialization format not supported yet.'); } configArray = config; } else { util.assert(config['layers'] != null, () => `When the config data for a Sequential model is not an Array, ` + `it must be an Object that contains the 'layers' field.`); configArray = config['layers']; delete config['layers']; extraModelConfig = config; } const model = new cls(extraModelConfig); if (!(model instanceof Sequential)) { throw new NotImplementedError(`Sequential.fromConfig called on non-Sequential input: ${model}`); } for (const conf of configArray) { const customObjects = undefined; const layer = deserialize(conf, customObjects, fastWeightInit); if (fastWeightInit) { layer.setFastWeightInitDuringBuild(true); } model.add(layer); } return model; } /** * Setter used for force stopping of LayersModel.fit() (i.e., training). * * Example: * * ```js * const model = tf.sequential(); * model.add(tf.layers.dense({units: 1, inputShape: [10]})); * model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); * const xs = tf.ones([8, 10]); * const ys = tf.zeros([8, 1]); * * const history = await model.fit(xs, ys, { * epochs: 10, * callbacks: { * onEpochEnd: async (epoch, logs) => { * if (epoch === 2) { * model.stopTraining = true; * } * } * } * }); * * // There should be only 3 values in the loss array, instead of 10 values, * // due to the stopping after 3 epochs. * console.log(history.history.loss); * ``` */ set stopTraining(stop) { // TODO(cais): When refactoring to remove the composition pattern happens, // remove this method overriding. if (this.model == null) { throw new ValueError('Cannot set the stopTraining property of a sequential model before ' + 'it is compiled.'); } this.model.stopTraining = stop; } get stopTraining() { if (this.model == null) { throw new ValueError('Cannot get the stopTraining property of a sequential model before ' + 'it is compiled.'); } return this.model.stopTraining; } // TODO(cais): Override get trainableWeights() here // tslint:disable-next-line:no-any getConfig() { // NOTE(cais): We override the return type of getConfig() to `any` here, // because the `Sequential` class is a special case among `Container` // subtypes in that its getConfig() method returns an Array (not a // dict). const layers = []; for (const layer of this.layers) { const dict = {}; dict['className'] = layer.getClassName(); dict['config'] = layer.getConfig(); layers.push(dict); } return { name: this.name, layers }; } } /** @nocollapse */ Sequential.className = 'Sequential'; serialization.registerClass(Sequential); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // TODO(cais): Add doc string to all the public static functions in this // class; include exectuable JavaScript code snippets where applicable // (b/74074458). // LayersModel and related factory methods. /** * 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.LayersModel`, 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. * * ```js * // 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: 4, 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.loadLayersModel`. * * @doc {heading: 'Models', subheading: 'Creation'} */ function model(args) { return new LayersModel(args); } /** * 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 `tf.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: * * ```js * 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: * * ```js * 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 `Layer`s to create * a `tf.Sequential` model: * * ```js * 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)); * ``` * * @doc {heading: 'Models', subheading: 'Creation'} */ function sequential(config) { return new Sequential(config); } /** * Used to instantiate an input to a model as a `tf.SymbolicTensor`. * * Users should call the `input` factory function for * consistency with other generator functions. * * Example: * * ```js * // 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: `input` is only necessary when using `model`. When using * `sequential`, specify `inputShape` for the first layer or use `inputLayer` * as the first layer. * * @doc {heading: 'Models', subheading: 'Inputs'} */ function input(config) { return Input(config); } function registerCallbackConstructor(verbosityLevel, callbackConstructor) { CallbackConstructorRegistry.registerCallbackConstructor(verbosityLevel, callbackConstructor); } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Base class for Activations. * * Special note: due to cross-language compatibility reasons, the * static readonly className field in this family of classes must be set to * the initialLowerCamelCase name of the activation. */ let Activation$1 = class Activation extends serialization.Serializable { getConfig() { return {}; } }; /** * Exponential linear unit (ELU). * Reference: https://arxiv.org/abs/1511.07289 */ class Elu extends Activation$1 { /** * Calculate the activation function. * * @param x: Input. * @param alpha: Scaling factor the negative section. * @return Output of the ELU activation. */ apply(x, alpha = 1) { return elu$1(x, alpha); } } /** @nocollapse */ Elu.className = 'elu'; serialization.registerClass(Elu); /** * Scaled Exponential Linear Unit. (Klambauer et al., 2017). * Reference: Self-Normalizing Neural Networks, https://arxiv.org/abs/1706.02515 * Notes: * - To be used together with the initialization "lecunNormal". * - To be used together with the dropout variant "AlphaDropout". */ class Selu extends Activation$1 { apply(x) { return tfc.selu(x); } } /** @nocollapse */ Selu.className = 'selu'; serialization.registerClass(Selu); /** * Rectified linear unit */ class Relu extends Activation$1 { apply(x) { return tfc.relu(x); } } /** @nocollapse */ Relu.className = 'relu'; serialization.registerClass(Relu); /** * Rectified linear unit activation maxing out at 6.0. */ class Relu6 extends Activation$1 { apply(x) { return tidy$1(() => tfc.minimum(6.0, tfc.relu(x))); } } /** @nocollapse */ Relu6.className = 'relu6'; serialization.registerClass(Relu6); //* Linear activation (no-op) */ class Linear extends Activation$1 { apply(x) { return x; } } /** @nocollapse */ Linear.className = 'linear'; serialization.registerClass(Linear); /** * Sigmoid activation function. */ class Sigmoid extends Activation$1 { apply(x) { return tfc.sigmoid(x); } } /** @nocollapse */ Sigmoid.className = 'sigmoid'; serialization.registerClass(Sigmoid); /** * Segment-wise linear approximation of sigmoid. */ class HardSigmoid extends Activation$1 { apply(x) { return hardSigmoid(x); } } /** @nocollapse */ HardSigmoid.className = 'hardSigmoid'; serialization.registerClass(HardSigmoid); /** * Softplus activation function. */ class Softplus extends Activation$1 { apply(x) { return tfc.softplus(x); } } /** @nocollapse */ Softplus.className = 'softplus'; serialization.registerClass(Softplus); /** * Softsign activation function. */ class Softsign extends Activation$1 { apply(x) { return softsign(x); } } /** @nocollapse */ Softsign.className = 'softsign'; serialization.registerClass(Softsign); /** * Hyperbolic tangent function. */ class Tanh extends Activation$1 { apply(x) { return tfc.tanh(x); } } /** @nocollapse */ Tanh.className = 'tanh'; serialization.registerClass(Tanh); /** * Softmax activation function */ let Softmax$1 = class Softmax extends Activation$1 { /** * Calculate the activation function. * * @param x Tensor. * @param axis Integer, axis along which the softmax normalization is applied. * Invalid if < 2, as softmax across 1 (the batch dimension) is assumed to be * an error. * * @returns a Tensor of the same shape as x * * @throws ValueError: In case `dim(x) < 2`. */ apply(x, axis = (-1)) { return tfc.softmax(x, axis); } }; /** @nocollapse */ Softmax$1.className = 'softmax'; serialization.registerClass(Softmax$1); /** * Log softmax activation function */ class LogSoftmax extends Activation$1 { /** * Calculate the activation function of log softmax: * log( exp(x_i) / sum(exp(x)) ) * * @param x Tensor. * @param axis Integer, axis along which the softmax normalization is applied. * Invalid if < 2, as softmax across 1 (the batch dimension) is assumed to be * an error. * * @returns a Tensor of the same shape as x * * @throws ValueError: In case `dim(x) < 2`. */ apply(x, axis = (-1)) { return tfc.logSoftmax(x, axis); } } /** @nocollapse */ LogSoftmax.className = 'logSoftmax'; serialization.registerClass(LogSoftmax); /** * Swish activation function */ class Swish extends Activation$1 { /** * Calculate the activation function. * * @param x Tensor. * @param alpha Scaling factor for the sigmoid function. * @returns a Tensor of the same shape as x */ apply(x, alpha = 1) { return tidy$1(() => tfc.mul(tfc.sigmoid(tfc.mul(x, alpha)), x)); } } /** @nocollapse */ Swish.className = 'swish'; serialization.registerClass(Swish); /** * Mish activation function */ class Mish extends Activation$1 { /** * Calculate the activation function. * * @param x Tensor. * @returns a Tensor of the same shape as x */ apply(x) { return tidy$1(() => tfc.mul(x, tfc.tanh(tfc.softplus(x)))); } } /** @nocollapse */ Mish.className = 'mish'; serialization.registerClass(Mish); function serializeActivation(activation) { return activation.getClassName(); } function deserializeActivation(config, customObjects = {}) { return deserializeKerasObject(config, serialization.SerializationMap.getMap().classNameMap, customObjects, 'activation'); } function getActivation(identifier) { if (identifier == null) { const config = {}; config['className'] = 'linear'; config['config'] = {}; return deserializeActivation(config); } if (typeof identifier === 'string') { const config = {}; config['className'] = identifier; config['config'] = {}; return deserializeActivation(config); } else if (identifier instanceof Activation$1) { return identifier; } else { return deserializeActivation(identifier); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function assertObjectArgs(args) { if (args != null && typeof args !== 'object') { throw new Error(`Argument to L1L2 regularizer's constructor is expected to be an ` + `object, but received: ${args}`); } } /** * Regularizer base class. */ class Regularizer extends serialization.Serializable { } class L1L2 extends Regularizer { constructor(args) { super(); assertObjectArgs(args); this.l1 = args == null || args.l1 == null ? 0.01 : args.l1; this.l2 = args == null || args.l2 == null ? 0.01 : args.l2; this.hasL1 = this.l1 !== 0; this.hasL2 = this.l2 !== 0; } /** * Porting note: Renamed from __call__. * @param x Variable of which to calculate the regularization score. */ apply(x) { return tidy$1(() => { let regularization = zeros$2([1]); if (this.hasL1) { regularization = add$3(regularization, sum$1(tfc.mul(this.l1, abs(x)))); } if (this.hasL2) { regularization = add$3(regularization, sum$1(tfc.mul(this.l2, square$1(x)))); } return tfc.reshape(regularization, []); }); } getConfig() { return { 'l1': this.l1, 'l2': this.l2 }; } /** @nocollapse */ static fromConfig(cls, config) { return new cls({ l1: config['l1'], l2: config['l2'] }); } } /** @nocollapse */ L1L2.className = 'L1L2'; serialization.registerClass(L1L2); function l1$1(args) { assertObjectArgs(args); return new L1L2({ l1: args != null ? args.l1 : null, l2: 0 }); } function l2$1(args) { assertObjectArgs(args); return new L1L2({ l2: args != null ? args.l2 : null, l1: 0 }); } // Maps the JavaScript-like identifier keys to the corresponding keras symbols. const REGULARIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP = { 'l1l2': 'L1L2' }; function serializeRegularizer(constraint) { return serializeKerasObject(constraint); } function deserializeRegularizer(config, customObjects = {}) { return deserializeKerasObject(config, serialization.SerializationMap.getMap().classNameMap, customObjects, 'regularizer'); } function getRegularizer(identifier) { if (identifier == null) { return null; } if (typeof identifier === 'string') { const className = identifier in REGULARIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP ? REGULARIZER_IDENTIFIER_REGISTRY_SYMBOL_MAP[identifier] : identifier; const config = { className, config: {} }; return deserializeRegularizer(config); } else if (identifier instanceof Regularizer) { return identifier; } else { return deserializeRegularizer(identifier); } } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class ReLU extends Layer { constructor(args) { super(args == null ? {} : args); this.supportsMasking = true; if (args != null) { this.maxValue = args.maxValue; } } call(inputs, kwargs) { inputs = getExactlyOneTensor(inputs); let output = relu(inputs); if (this.maxValue != null) { output = clipByValue(output, 0, this.maxValue); } return output; } computeOutputShape(inputShape) { return inputShape; } getConfig() { const config = { maxValue: this.maxValue }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ ReLU.className = 'ReLU'; serialization.registerClass(ReLU); class LeakyReLU extends Layer { constructor(args) { super(args == null ? {} : args); this.DEFAULT_ALPHA = 0.3; if (args == null) { args = {}; } this.alpha = args.alpha == null ? this.DEFAULT_ALPHA : args.alpha; } call(inputs, kwargs) { const x = getExactlyOneTensor(inputs); return leakyRelu(x, this.alpha); } computeOutputShape(inputShape) { return inputShape; } getConfig() { const config = { alpha: this.alpha }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ LeakyReLU.className = 'LeakyReLU'; serialization.registerClass(LeakyReLU); class PReLU extends Layer { constructor(args) { super(args == null ? {} : args); this.DEFAULT_ALPHA_INITIALIZER = 'zeros'; if (args == null) { args = {}; } this.supportsMasking = true; this.alphaInitializer = getInitializer(args.alphaInitializer || this.DEFAULT_ALPHA_INITIALIZER); this.alphaRegularizer = getRegularizer(args.alphaRegularizer); this.alphaConstraint = getConstraint(args.alphaConstraint); if (args.sharedAxes == null) { this.sharedAxes = null; } else if (Array.isArray(args.sharedAxes)) { this.sharedAxes = args.sharedAxes; } else if (typeof args.sharedAxes === 'number') { this.sharedAxes = [args.sharedAxes]; } else { throw new ValueError(`Expected sharedAxes to be a number or an array of numbers, ` + `but got ${args.sharedAxes}`); } } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const paramShape = inputShape.slice(1); if (this.sharedAxes != null) { for (const i of this.sharedAxes) { paramShape[i - 1] = 1; } } this.alpha = this.addWeight('alpha', paramShape, 'float32', this.alphaInitializer, this.alphaRegularizer, true, this.alphaConstraint); // Set input spec. const axes = {}; if (this.sharedAxes != null) { for (let i = 1; i < inputShape.length; ++i) { axes[i] = inputShape[i]; } } this.inputSpec = [new InputSpec({ ndim: inputShape.length, axes, })]; this.built = true; } call(inputs, kwargs) { inputs = getExactlyOneTensor(inputs); return prelu$1(inputs, this.alpha.read()); } getConfig() { const config = { alphaInitializer: serializeInitializer(this.alphaInitializer), alphaRegularizer: serializeRegularizer(this.alphaRegularizer), alphaConstraint: serializeConstraint(this.alphaConstraint), sharedAxes: this.sharedAxes }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ PReLU.className = 'PReLU'; serialization.registerClass(PReLU); class ELU extends Layer { constructor(args) { super(args == null ? {} : args); this.DEFAULT_ALPHA = 1.0; if (args == null) { args = {}; } if (args.alpha != null && args.alpha !== this.DEFAULT_ALPHA) { throw new NotImplementedError(`Non-default alpha value (${args.alpha}) is not supported by the ` + `ELU layer yet.`); } this.alpha = args.alpha == null ? this.DEFAULT_ALPHA : args.alpha; } call(inputs, kwargs) { const x = getExactlyOneTensor(inputs); return elu$2(x); } computeOutputShape(inputShape) { return inputShape; } getConfig() { const config = { alpha: this.alpha }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ ELU.className = 'ELU'; serialization.registerClass(ELU); class ThresholdedReLU extends Layer { constructor(args) { super(args == null ? {} : args); this.DEFAULT_THETA = 1.0; if (args == null) { args = {}; } this.theta = args.theta == null ? this.DEFAULT_THETA : args.theta; } call(inputs, kwargs) { const x = getExactlyOneTensor(inputs); return mul$1(x, cast$2(greater$2(x, this.theta), 'float32')); } computeOutputShape(inputShape) { return inputShape; } getConfig() { const config = { theta: this.theta }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ ThresholdedReLU.className = 'ThresholdedReLU'; serialization.registerClass(ThresholdedReLU); class Softmax extends Layer { constructor(args) { super(args == null ? {} : args); this.DEFAULT_AXIS = 1.0; if (args == null) { args = {}; } this.softmax = new Softmax$1().apply; this.axis = args.axis == null ? this.DEFAULT_AXIS : args.axis; } call(inputs, kwargs) { // TODO(pforderique): Add tests for when `this.axis` is a number[]. return tidy$1(() => { let x = getExactlyOneTensor(inputs); const mask = kwargs['mask']; if (mask != null) { // Since mask is 1.0 for positions we want to keep and 0.0 for masked // positions, this operation will create a tensor which is 0.0 for // positions we want to attend and -1e.9 for masked positions. const adder = mul$1(sub$1(ones$3(x.shape), cast$2(mask, x.dtype)), scalar$1(-1e9)); // Since we are adding it to the raw scores before the softmax, this // is effectively the same as removing these entirely. x = add$3(x, adder); } if (this.axis instanceof Array) { if (this.axis.length > 1) { return exp$1(sub$1(x, logSumExp(x, this.axis, true))); } else { return this.softmax(x, this.axis[0]); } } return this.softmax(x, this.axis); }); } computeOutputShape(inputShape) { return inputShape; } getConfig() { const config = { axis: this.axis }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Softmax.className = 'Softmax'; serialization.registerClass(Softmax); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Transforms a single number of array of numbers into an array of numbers. * @param value * @param n: The size of the tuple to be returned. * @param name: Name of the parameter, used for generating error messages. * @returns An array of numbers. */ function normalizeArray(value, n, name) { if (typeof value === 'number') { return pyListRepeat(value, n); } else { if (value.length !== n) { throw new ValueError(`The ${name} argument must be an integer or tuple of ${n} integers.` + ` Received: ${value.length} elements.`); } for (let i = 0; i < n; ++i) { const singleValue = value[i]; if (!isInteger(singleValue)) { throw new ValueError(`The ${name} argument must be an integer or tuple of ${n}` + ` integers. Received: ${JSON.stringify(value)} including a` + ` non-integer number ${singleValue}`); } } return value; } } /** * Determines output length of a convolution given input length. * @param inputLength * @param filterSize * @param padding * @param stride * @param dilation: dilation rate. */ function convOutputLength(inputLength, filterSize, padding, stride, dilation = 1) { if (inputLength == null) { return inputLength; } const dilatedFilterSize = filterSize + (filterSize - 1) * (dilation - 1); let outputLength; if (padding === 'same') { outputLength = inputLength; } else { // VALID outputLength = inputLength - dilatedFilterSize + 1; } return Math.floor((outputLength + stride - 1) / stride); } function deconvLength(dimSize, strideSize, kernelSize, padding) { if (dimSize == null) { return null; } if (padding === 'valid') { dimSize = dimSize * strideSize + max([kernelSize - strideSize, 0]); } else if (padding === 'same') { dimSize = dimSize * strideSize; } else { throw new ValueError(`Unsupport padding mode: ${padding}.`); } return dimSize; } /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Transpose and cast the input before the conv2d. * @param x Input image tensor. * @param dataFormat */ function preprocessConv2DInput(x, dataFormat) { // TODO(cais): Cast type to float32 if not. return tidy$1(() => { checkDataFormat(dataFormat); if (dataFormat === 'channelsFirst') { return tfc.transpose(x, [0, 2, 3, 1]); // NCHW -> NHWC. } else { return x; } }); } /** * Transpose and cast the input before the conv3d. * @param x Input image tensor. * @param dataFormat */ function preprocessConv3DInput(x, dataFormat) { return tidy$1(() => { checkDataFormat(dataFormat); if (dataFormat === 'channelsFirst') { return tfc.transpose(x, [0, 2, 3, 4, 1]); // NCDHW -> NDHWC. } else { return x; } }); } /** * 1D-convolution with bias added. * * Porting Note: This function does not exist in the Python Keras backend. * It is exactly the same as `conv2d`, except the added `bias`. * * @param x Input tensor, rank-3, of shape `[batchSize, width, inChannels]`. * @param kernel Kernel, rank-3, of shape `[filterWidth, inDepth, outDepth]`. * @param bias Bias, rank-3, of shape `[outDepth]`. * @param strides * @param padding Padding mode. * @param dataFormat Data format. * @param dilationRate * @returns The result of the 1D convolution. * @throws ValueError, if `x`, `kernel` or `bias` is not of the correct rank. */ function conv1dWithBias(x, kernel, bias, strides = 1, padding = 'valid', dataFormat, dilationRate = 1) { return tidy$1(() => { if (dataFormat == null) { dataFormat = imageDataFormat(); } checkDataFormat(dataFormat); // Check the ranks of x, kernel and bias. if (x.shape.length !== 3) { throw new ValueError(`The input of a conv1dWithBias operation should be 3, but is ` + `${x.shape.length} instead.`); } if (kernel.shape.length !== 3) { throw new ValueError(`The kernel for a conv1dWithBias operation should be 3, but is ` + `${kernel.shape.length} instead`); } if (bias != null && bias.shape.length !== 1) { throw new ValueError(`The bias for a conv1dWithBias operation should be 1, but is ` + `${kernel.shape.length} instead`); } // TODO(cais): Support CAUSAL padding mode. if (dataFormat === 'channelsFirst') { x = tfc.transpose(x, [0, 2, 1]); // NCW -> NWC. } if (padding === 'causal') { throw new NotImplementedError('The support for CAUSAL padding mode in conv1dWithBias is not ' + 'implemented yet.'); } let y = tfc.conv1d(x, kernel, strides, padding === 'same' ? 'same' : 'valid', 'NWC', dilationRate); if (bias != null) { y = biasAdd(y, bias); } return y; }); } /** * 2D Convolution with an added bias and optional activation. * Note: This function does not exist in the Python Keras Backend. This function * is exactly the same as `conv2d`, except the added `bias`. */ function conv2dWithBiasActivation(x, kernel, bias, strides = [1, 1], padding = 'valid', dataFormat, dilationRate, activation = null) { return tidy$1(() => { if (dataFormat == null) { dataFormat = imageDataFormat(); } checkDataFormat(dataFormat); if (x.rank !== 3 && x.rank !== 4) { throw new ValueError(`conv2dWithBiasActivation expects input to be of rank 3 or 4, ` + `but received ${x.rank}.`); } if (kernel.rank !== 3 && kernel.rank !== 4) { throw new ValueError(`conv2dWithBiasActivation expects kernel to be of rank 3 or 4, ` + `but received ${x.rank}.`); } let y = preprocessConv2DInput(x, dataFormat); if (padding === 'causal') { throw new NotImplementedError('The support for CAUSAL padding mode in conv1dWithBias is not ' + 'implemented yet.'); } y = tfc.fused.conv2d({ x: y, filter: kernel, strides: strides, pad: padding === 'same' ? 'same' : 'valid', dilations: dilationRate, dataFormat: 'NHWC', bias, activation }); if (dataFormat === 'channelsFirst') { y = tfc.transpose(y, [0, 3, 1, 2]); } return y; }); } /** * 3D Convolution with an added bias. * Note: This function does not exist in the Python Keras Backend. This function * is exactly the same as `conv3d`, except the added `bias`. */ function conv3dWithBias(x, kernel, bias, strides = [1, 1, 1], padding = 'valid', dataFormat, dilationRate) { return tidy$1(() => { if (dataFormat == null) { dataFormat = imageDataFormat(); } checkDataFormat(dataFormat); if (x.rank !== 4 && x.rank !== 5) { throw new ValueError(`conv3dWithBias expects input to be of rank 4 or 5, but received ` + `${x.rank}.`); } if (kernel.rank !== 4 && kernel.rank !== 5) { throw new ValueError(`conv3dWithBias expects kernel to be of rank 4 or 5, but received ` + `${x.rank}.`); } let y = preprocessConv3DInput(x, dataFormat); if (padding === 'causal') { throw new NotImplementedError('The support for CAUSAL padding mode in conv3dWithBias is not ' + 'implemented yet.'); } y = tfc.conv3d(y, kernel, strides, padding === 'same' ? 'same' : 'valid', 'NDHWC', dilationRate); if (bias != null) { y = biasAdd(y, bias); } if (dataFormat === 'channelsFirst') { y = tfc.transpose(y, [0, 4, 1, 2, 3]); } return y; }); } /** * Abstract convolution layer. */ class BaseConv extends Layer { constructor(rank, args) { super(args); this.bias = null; this.DEFAULT_KERNEL_INITIALIZER = 'glorotNormal'; this.DEFAULT_BIAS_INITIALIZER = 'zeros'; BaseConv.verifyArgs(args); this.rank = rank; assertPositiveInteger(this.rank, 'rank'); if (this.rank !== 1 && this.rank !== 2 && this.rank !== 3) { throw new NotImplementedError(`Convolution layer for rank other than 1, 2, or 3 (${this.rank}) is ` + `not implemented yet.`); } this.kernelSize = normalizeArray(args.kernelSize, rank, 'kernelSize'); this.strides = normalizeArray(args.strides == null ? 1 : args.strides, rank, 'strides'); this.padding = args.padding == null ? 'valid' : args.padding; checkPaddingMode(this.padding); this.dataFormat = args.dataFormat == null ? 'channelsLast' : args.dataFormat; checkDataFormat(this.dataFormat); this.activation = getActivation(args.activation); this.useBias = args.useBias == null ? true : args.useBias; this.biasInitializer = getInitializer(args.biasInitializer || this.DEFAULT_BIAS_INITIALIZER); this.biasConstraint = getConstraint(args.biasConstraint); this.biasRegularizer = getRegularizer(args.biasRegularizer); this.activityRegularizer = getRegularizer(args.activityRegularizer); this.dilationRate = normalizeArray(args.dilationRate == null ? 1 : args.dilationRate, rank, 'dilationRate'); if (this.rank === 1 && (Array.isArray(this.dilationRate) && this.dilationRate.length !== 1)) { throw new ValueError(`dilationRate must be a number or an array of a single number ` + `for 1D convolution, but received ` + `${JSON.stringify(this.dilationRate)}`); } else if (this.rank === 2) { if (typeof this.dilationRate === 'number') { this.dilationRate = [this.dilationRate, this.dilationRate]; } else if (this.dilationRate.length !== 2) { throw new ValueError(`dilationRate must be a number or array of two numbers for 2D ` + `convolution, but received ${JSON.stringify(this.dilationRate)}`); } } else if (this.rank === 3) { if (typeof this.dilationRate === 'number') { this.dilationRate = [this.dilationRate, this.dilationRate, this.dilationRate]; } else if (this.dilationRate.length !== 3) { throw new ValueError(`dilationRate must be a number or array of three numbers for 3D ` + `convolution, but received ${JSON.stringify(this.dilationRate)}`); } } } static verifyArgs(args) { // Check config.kernelSize type and shape. assert$1('kernelSize' in args, `required key 'kernelSize' not in config`); if (typeof args.kernelSize !== 'number' && !checkArrayTypeAndLength(args.kernelSize, 'number', 1, 3)) { throw new ValueError(`BaseConv expects config.kernelSize to be number or number[] with ` + `length 1, 2, or 3, but received ${JSON.stringify(args.kernelSize)}.`); } } getConfig() { const config = { kernelSize: this.kernelSize, strides: this.strides, padding: this.padding, dataFormat: this.dataFormat, dilationRate: this.dilationRate, activation: serializeActivation(this.activation), useBias: this.useBias, biasInitializer: serializeInitializer(this.biasInitializer), biasRegularizer: serializeRegularizer(this.biasRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), biasConstraint: serializeConstraint(this.biasConstraint) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** * Abstract nD convolution layer. Ancestor of convolution layers which reduce * across channels, i.e., Conv1D and Conv2D, but not DepthwiseConv2D. */ class Conv extends BaseConv { constructor(rank, args) { super(rank, args); this.kernel = null; Conv.verifyArgs(args); this.filters = args.filters; assertPositiveInteger(this.filters, 'filters'); this.kernelInitializer = getInitializer(args.kernelInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.kernelConstraint = getConstraint(args.kernelConstraint); this.kernelRegularizer = getRegularizer(args.kernelRegularizer); } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : inputShape.length - 1; if (inputShape[channelAxis] == null) { throw new ValueError(`The channel dimension of the input should be defined. ` + `Found ${inputShape[channelAxis]}`); } const inputDim = inputShape[channelAxis]; const kernelShape = this.kernelSize.concat([inputDim, this.filters]); this.kernel = this.addWeight('kernel', kernelShape, null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.filters], null, this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } this.inputSpec = [{ ndim: this.rank + 2, axes: { [channelAxis]: inputDim } }]; this.built = true; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); let outputs; const biasValue = this.bias == null ? null : this.bias.read(); const fusedActivationName = mapActivationToFusedKernel(this.activation.getClassName()); if (fusedActivationName != null && this.rank === 2) { outputs = conv2dWithBiasActivation(inputs, this.kernel.read(), biasValue, this.strides, this.padding, this.dataFormat, this.dilationRate, fusedActivationName); } else { if (this.rank === 1) { outputs = conv1dWithBias(inputs, this.kernel.read(), biasValue, this.strides[0], this.padding, this.dataFormat, this.dilationRate[0]); } else if (this.rank === 2) { // TODO(cais): Move up to constructor. outputs = conv2dWithBiasActivation(inputs, this.kernel.read(), biasValue, this.strides, this.padding, this.dataFormat, this.dilationRate); } else if (this.rank === 3) { outputs = conv3dWithBias(inputs, this.kernel.read(), biasValue, this.strides, this.padding, this.dataFormat, this.dilationRate); } else { throw new NotImplementedError('convolutions greater than 3D are not implemented yet.'); } if (this.activation != null) { outputs = this.activation.apply(outputs); } } return outputs; }); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const newSpace = []; const space = (this.dataFormat === 'channelsLast') ? inputShape.slice(1, inputShape.length - 1) : inputShape.slice(2); for (let i = 0; i < space.length; ++i) { const newDim = convOutputLength(space[i], this.kernelSize[i], this.padding, this.strides[i], typeof this.dilationRate === 'number' ? this.dilationRate : this.dilationRate[i]); newSpace.push(newDim); } let outputShape = [inputShape[0]]; if (this.dataFormat === 'channelsLast') { outputShape = outputShape.concat(newSpace); outputShape.push(this.filters); } else { outputShape.push(this.filters); outputShape = outputShape.concat(newSpace); } return outputShape; } getConfig() { const config = { filters: this.filters, kernelInitializer: serializeInitializer(this.kernelInitializer), kernelRegularizer: serializeRegularizer(this.kernelRegularizer), kernelConstraint: serializeConstraint(this.kernelConstraint) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } static verifyArgs(args) { // Check config.filters type, shape, and value. if (!('filters' in args) || typeof args.filters !== 'number' || args.filters < 1) { throw new ValueError(`Convolution layer expected config.filters to be a 'number' > 0 ` + `but got ${JSON.stringify(args.filters)}`); } } } class Conv2D extends Conv { constructor(args) { super(2, args); Conv2D.verifyArgs(args); } getConfig() { const config = super.getConfig(); delete config['rank']; return config; } static verifyArgs(args) { // config.kernelSize must be a number or array of numbers. if ((typeof args.kernelSize !== 'number') && !checkArrayTypeAndLength(args.kernelSize, 'number', 1, 2)) { throw new ValueError(`Conv2D expects config.kernelSize to be number or number[] with ` + `length 1 or 2, but received ${JSON.stringify(args.kernelSize)}.`); } } } /** @nocollapse */ Conv2D.className = 'Conv2D'; serialization.registerClass(Conv2D); class Conv3D extends Conv { constructor(args) { super(3, args); Conv3D.verifyArgs(args); } getConfig() { const config = super.getConfig(); delete config['rank']; return config; } static verifyArgs(args) { // config.kernelSize must be a number or array of numbers. if (typeof args.kernelSize !== 'number') { if (!(Array.isArray(args.kernelSize) && (args.kernelSize.length === 1 || args.kernelSize.length === 3))) { throw new ValueError(`Conv3D expects config.kernelSize to be number or` + ` [number, number, number], but received ${JSON.stringify(args.kernelSize)}.`); } } } } /** @nocollapse */ Conv3D.className = 'Conv3D'; serialization.registerClass(Conv3D); class Conv2DTranspose extends Conv2D { constructor(args) { super(args); this.inputSpec = [new InputSpec({ ndim: 4 })]; if (this.padding !== 'same' && this.padding !== 'valid') { throw new ValueError(`Conv2DTranspose currently supports only padding modes 'same' ` + `and 'valid', but received padding mode ${this.padding}`); } } build(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape.length !== 4) { throw new ValueError('Input should have rank 4; Received input shape: ' + JSON.stringify(inputShape)); } const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : inputShape.length - 1; if (inputShape[channelAxis] == null) { throw new ValueError('The channel dimension of the inputs should be defined. ' + 'Found `None`.'); } const inputDim = inputShape[channelAxis]; const kernelShape = this.kernelSize.concat([this.filters, inputDim]); this.kernel = this.addWeight('kernel', kernelShape, 'float32', this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.filters], 'float32', this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } // Set input spec. this.inputSpec = [new InputSpec({ ndim: 4, axes: { [channelAxis]: inputDim } })]; this.built = true; } call(inputs, kwargs) { return tfc.tidy(() => { let input = getExactlyOneTensor(inputs); if (input.shape.length !== 4) { throw new ValueError(`Conv2DTranspose.call() expects input tensor to be rank-4, but ` + `received a tensor of rank-${input.shape.length}`); } const inputShape = input.shape; const batchSize = inputShape[0]; let hAxis; let wAxis; if (this.dataFormat === 'channelsFirst') { hAxis = 2; wAxis = 3; } else { hAxis = 1; wAxis = 2; } const height = inputShape[hAxis]; const width = inputShape[wAxis]; const kernelH = this.kernelSize[0]; const kernelW = this.kernelSize[1]; const strideH = this.strides[0]; const strideW = this.strides[1]; // Infer the dynamic output shape. const outHeight = deconvLength(height, strideH, kernelH, this.padding); const outWidth = deconvLength(width, strideW, kernelW, this.padding); // Porting Note: We don't branch based on `this.dataFormat` here, // because // the tjfs-core function `conv2dTranspose` called below always // assumes channelsLast. const outputShape = [batchSize, outHeight, outWidth, this.filters]; if (this.dataFormat !== 'channelsLast') { input = tfc.transpose(input, [0, 2, 3, 1]); } let outputs = tfc.conv2dTranspose(input, this.kernel.read(), outputShape, this.strides, this.padding); if (this.dataFormat !== 'channelsLast') { outputs = tfc.transpose(outputs, [0, 3, 1, 2]); } if (this.bias != null) { outputs = biasAdd(outputs, this.bias.read(), this.dataFormat); } if (this.activation != null) { outputs = this.activation.apply(outputs); } return outputs; }); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const outputShape = inputShape.slice(); let channelAxis; let heightAxis; let widthAxis; if (this.dataFormat === 'channelsFirst') { channelAxis = 1; heightAxis = 2; widthAxis = 3; } else { channelAxis = 3; heightAxis = 1; widthAxis = 2; } const kernelH = this.kernelSize[0]; const kernelW = this.kernelSize[1]; const strideH = this.strides[0]; const strideW = this.strides[1]; outputShape[channelAxis] = this.filters; outputShape[heightAxis] = deconvLength(outputShape[heightAxis], strideH, kernelH, this.padding); outputShape[widthAxis] = deconvLength(outputShape[widthAxis], strideW, kernelW, this.padding); return outputShape; } getConfig() { const config = super.getConfig(); delete config['dilationRate']; return config; } } /** @nocollapse */ Conv2DTranspose.className = 'Conv2DTranspose'; serialization.registerClass(Conv2DTranspose); class Conv3DTranspose extends Conv3D { constructor(args) { super(args); this.inputSpec = [new InputSpec({ ndim: 5 })]; if (this.padding !== 'same' && this.padding !== 'valid') { throw new ValueError(`Conv3DTranspose currently supports only padding modes 'same' ` + `and 'valid', but received padding mode ${this.padding}`); } } build(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape.length !== 5) { throw new ValueError('Input should have rank 5; Received input shape: ' + JSON.stringify(inputShape)); } const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : inputShape.length - 1; if (inputShape[channelAxis] == null) { throw new ValueError('The channel dimension of the inputs should be defined. ' + 'Found `None`.'); } const inputDim = inputShape[channelAxis]; const kernelShape = this.kernelSize.concat([this.filters, inputDim]); this.kernel = this.addWeight('kernel', kernelShape, 'float32', this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.filters], 'float32', this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } // Set input spec. this.inputSpec = [new InputSpec({ ndim: 5, axes: { [channelAxis]: inputDim } })]; this.built = true; } call(inputs, kwargs) { return tfc.tidy(() => { let input = getExactlyOneTensor(inputs); if (input.shape.length !== 5) { throw new ValueError(`Conv3DTranspose.call() expects input tensor to be rank-4, but ` + `received a tensor of rank-${input.shape.length}`); } const inputShape = input.shape; const batchSize = inputShape[0]; let hAxis; let wAxis; let dAxis; if (this.dataFormat === 'channelsFirst') { dAxis = 2; hAxis = 3; wAxis = 4; } else { dAxis = 1; hAxis = 2; wAxis = 3; } const depth = inputShape[dAxis]; const height = inputShape[hAxis]; const width = inputShape[wAxis]; const kernelD = this.kernelSize[0]; const kernelH = this.kernelSize[1]; const kernelW = this.kernelSize[2]; const strideD = this.strides[0]; const strideH = this.strides[1]; const strideW = this.strides[2]; // Infer the dynamic output shape. const outDepth = deconvLength(depth, strideD, kernelD, this.padding); const outHeight = deconvLength(height, strideH, kernelH, this.padding); const outWidth = deconvLength(width, strideW, kernelW, this.padding); // Same as `conv2dTranspose`. We always assumes channelsLast. const outputShape = [batchSize, outDepth, outHeight, outWidth, this.filters]; if (this.dataFormat !== 'channelsLast') { input = tfc.transpose(input, [0, 2, 3, 4, 1]); } let outputs = tfc.conv3dTranspose(input, this.kernel.read(), outputShape, this.strides, this.padding); if (this.dataFormat !== 'channelsLast') { outputs = tfc.transpose(outputs, [0, 4, 1, 2, 3]); } if (this.bias !== null) { outputs = biasAdd(outputs, this.bias.read(), this.dataFormat); } if (this.activation !== null) { outputs = this.activation.apply(outputs); } return outputs; }); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const outputShape = inputShape.slice(); let channelAxis; let depthAxis; let heightAxis; let widthAxis; if (this.dataFormat === 'channelsFirst') { channelAxis = 1; depthAxis = 2; heightAxis = 3; widthAxis = 4; } else { channelAxis = 4; depthAxis = 1; heightAxis = 2; widthAxis = 3; } const kernelD = this.kernelSize[0]; const kernelH = this.kernelSize[1]; const kernelW = this.kernelSize[2]; const strideD = this.strides[0]; const strideH = this.strides[1]; const strideW = this.strides[2]; outputShape[channelAxis] = this.filters; outputShape[depthAxis] = deconvLength(outputShape[depthAxis], strideD, kernelD, this.padding); outputShape[heightAxis] = deconvLength(outputShape[heightAxis], strideH, kernelH, this.padding); outputShape[widthAxis] = deconvLength(outputShape[widthAxis], strideW, kernelW, this.padding); return outputShape; } getConfig() { const config = super.getConfig(); delete config['dilationRate']; return config; } } /** @nocollapse */ Conv3DTranspose.className = 'Conv3DTranspose'; serialization.registerClass(Conv3DTranspose); class SeparableConv extends Conv { constructor(rank, config) { super(rank, config); this.DEFAULT_DEPTHWISE_INITIALIZER = 'glorotUniform'; this.DEFAULT_POINTWISE_INITIALIZER = 'glorotUniform'; this.depthwiseKernel = null; this.pointwiseKernel = null; if (config.filters == null) { throw new ValueError('The `filters` configuration field is required by SeparableConv, ' + 'but is unspecified.'); } if (config.kernelInitializer != null || config.kernelRegularizer != null || config.kernelConstraint != null) { throw new ValueError('Fields kernelInitializer, kernelRegularizer and kernelConstraint ' + 'are invalid for SeparableConv2D. Use depthwiseInitializer, ' + 'depthwiseRegularizer, depthwiseConstraint, pointwiseInitializer, ' + 'pointwiseRegularizer and pointwiseConstraint instead.'); } if (config.padding != null && config.padding !== 'same' && config.padding !== 'valid') { throw new ValueError(`SeparableConv${this.rank}D supports only padding modes: ` + `'same' and 'valid', but received ${JSON.stringify(config.padding)}`); } this.depthMultiplier = config.depthMultiplier == null ? 1 : config.depthMultiplier; this.depthwiseInitializer = getInitializer(config.depthwiseInitializer || this.DEFAULT_DEPTHWISE_INITIALIZER); this.depthwiseRegularizer = getRegularizer(config.depthwiseRegularizer); this.depthwiseConstraint = getConstraint(config.depthwiseConstraint); this.pointwiseInitializer = getInitializer(config.depthwiseInitializer || this.DEFAULT_POINTWISE_INITIALIZER); this.pointwiseRegularizer = getRegularizer(config.pointwiseRegularizer); this.pointwiseConstraint = getConstraint(config.pointwiseConstraint); } build(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape.length < this.rank + 2) { throw new ValueError(`Inputs to SeparableConv${this.rank}D should have rank ` + `${this.rank + 2}, but received input shape: ` + `${JSON.stringify(inputShape)}`); } const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : inputShape.length - 1; if (inputShape[channelAxis] == null || inputShape[channelAxis] < 0) { throw new ValueError(`The channel dimension of the inputs should be defined, ` + `but found ${JSON.stringify(inputShape[channelAxis])}`); } const inputDim = inputShape[channelAxis]; const depthwiseKernelShape = this.kernelSize.concat([inputDim, this.depthMultiplier]); const pointwiseKernelShape = []; for (let i = 0; i < this.rank; ++i) { pointwiseKernelShape.push(1); } pointwiseKernelShape.push(inputDim * this.depthMultiplier, this.filters); const trainable = true; this.depthwiseKernel = this.addWeight('depthwise_kernel', depthwiseKernelShape, 'float32', this.depthwiseInitializer, this.depthwiseRegularizer, trainable, this.depthwiseConstraint); this.pointwiseKernel = this.addWeight('pointwise_kernel', pointwiseKernelShape, 'float32', this.pointwiseInitializer, this.pointwiseRegularizer, trainable, this.pointwiseConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.filters], 'float32', this.biasInitializer, this.biasRegularizer, trainable, this.biasConstraint); } else { this.bias = null; } this.inputSpec = [new InputSpec({ ndim: this.rank + 2, axes: { [channelAxis]: inputDim } })]; this.built = true; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); let output; if (this.rank === 1) { throw new NotImplementedError('1D separable convolution is not implemented yet.'); } else if (this.rank === 2) { if (this.dataFormat === 'channelsFirst') { inputs = tfc.transpose(inputs, [0, 2, 3, 1]); // NCHW -> NHWC. } output = tfc.separableConv2d(inputs, this.depthwiseKernel.read(), this.pointwiseKernel.read(), this.strides, this.padding, this.dilationRate, 'NHWC'); } if (this.useBias) { output = biasAdd(output, this.bias.read(), this.dataFormat); } if (this.activation != null) { output = this.activation.apply(output); } if (this.dataFormat === 'channelsFirst') { output = tfc.transpose(output, [0, 3, 1, 2]); // NHWC -> NCHW. } return output; }); } getConfig() { const config = super.getConfig(); delete config['rank']; delete config['kernelInitializer']; delete config['kernelRegularizer']; delete config['kernelConstraint']; config['depthwiseInitializer'] = serializeInitializer(this.depthwiseInitializer); config['pointwiseInitializer'] = serializeInitializer(this.pointwiseInitializer); config['depthwiseRegularizer'] = serializeRegularizer(this.depthwiseRegularizer); config['pointwiseRegularizer'] = serializeRegularizer(this.pointwiseRegularizer); config['depthwiseConstraint'] = serializeConstraint(this.depthwiseConstraint); config['pointwiseConstraint'] = serializeConstraint(this.pointwiseConstraint); return config; } } /** @nocollapse */ SeparableConv.className = 'SeparableConv'; class SeparableConv2D extends SeparableConv { constructor(args) { super(2, args); } } /** @nocollapse */ SeparableConv2D.className = 'SeparableConv2D'; serialization.registerClass(SeparableConv2D); class Conv1D extends Conv { constructor(args) { super(1, args); Conv1D.verifyArgs(args); this.inputSpec = [{ ndim: 3 }]; } getConfig() { const config = super.getConfig(); delete config['rank']; delete config['dataFormat']; return config; } static verifyArgs(args) { // config.kernelSize must be a number or array of numbers. if (typeof args.kernelSize !== 'number' && !checkArrayTypeAndLength(args.kernelSize, 'number', 1, 1)) { throw new ValueError(`Conv1D expects config.kernelSize to be number or number[] with ` + `length 1, but received ${JSON.stringify(args.kernelSize)}.`); } } } /** @nocollapse */ Conv1D.className = 'Conv1D'; serialization.registerClass(Conv1D); class Cropping2D extends Layer { constructor(args) { super(args); if (typeof args.cropping === 'number') { this.cropping = [[args.cropping, args.cropping], [args.cropping, args.cropping]]; } else if (typeof args.cropping[0] === 'number') { this.cropping = [ [args.cropping[0], args.cropping[0]], [args.cropping[1], args.cropping[1]] ]; } else { this.cropping = args.cropping; } this.dataFormat = args.dataFormat === undefined ? 'channelsLast' : args.dataFormat; this.inputSpec = [{ ndim: 4 }]; } computeOutputShape(inputShape) { if (this.dataFormat === 'channelsFirst') { return [ inputShape[0], inputShape[1], inputShape[2] - this.cropping[0][0] - this.cropping[0][1], inputShape[3] - this.cropping[1][0] - this.cropping[1][1] ]; } else { return [ inputShape[0], inputShape[1] - this.cropping[0][0] - this.cropping[0][1], inputShape[2] - this.cropping[1][0] - this.cropping[1][1], inputShape[3] ]; } } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); if (this.dataFormat === 'channelsLast') { const hSliced = sliceAlongAxis(inputs, this.cropping[0][0], inputs.shape[1] - this.cropping[0][0] - this.cropping[0][1], 2); return sliceAlongAxis(hSliced, this.cropping[1][0], inputs.shape[2] - this.cropping[1][1] - this.cropping[1][0], 3); } else { const hSliced = sliceAlongAxis(inputs, this.cropping[0][0], inputs.shape[2] - this.cropping[0][0] - this.cropping[0][1], 3); return sliceAlongAxis(hSliced, this.cropping[1][0], inputs.shape[3] - this.cropping[1][1] - this.cropping[1][0], 4); } }); } getConfig() { const config = { cropping: this.cropping, dataFormat: this.dataFormat }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Cropping2D.className = 'Cropping2D'; serialization.registerClass(Cropping2D); class UpSampling2D extends Layer { constructor(args) { super(args); this.DEFAULT_SIZE = [2, 2]; this.inputSpec = [{ ndim: 4 }]; this.size = args.size == null ? this.DEFAULT_SIZE : args.size; this.dataFormat = args.dataFormat == null ? 'channelsLast' : args.dataFormat; checkDataFormat(this.dataFormat); this.interpolation = args.interpolation == null ? 'nearest' : args.interpolation; checkInterpolationFormat(this.interpolation); } computeOutputShape(inputShape) { if (this.dataFormat === 'channelsFirst') { const height = inputShape[2] == null ? null : this.size[0] * inputShape[2]; const width = inputShape[3] == null ? null : this.size[1] * inputShape[3]; return [inputShape[0], inputShape[1], height, width]; } else { const height = inputShape[1] == null ? null : this.size[0] * inputShape[1]; const width = inputShape[2] == null ? null : this.size[1] * inputShape[2]; return [inputShape[0], height, width, inputShape[3]]; } } call(inputs, kwargs) { return tfc.tidy(() => { let input = getExactlyOneTensor(inputs); const inputShape = input.shape; if (this.dataFormat === 'channelsFirst') { input = tfc.transpose(input, [0, 2, 3, 1]); const height = this.size[0] * inputShape[2]; const width = this.size[1] * inputShape[3]; const resized = this.interpolation === 'nearest' ? tfc.image.resizeNearestNeighbor(input, [height, width]) : tfc.image.resizeBilinear(input, [height, width]); return tfc.transpose(resized, [0, 3, 1, 2]); } else { const height = this.size[0] * inputShape[1]; const width = this.size[1] * inputShape[2]; return this.interpolation === 'nearest' ? tfc.image.resizeNearestNeighbor(input, [height, width]) : tfc.image.resizeBilinear(input, [height, width]); } }); } getConfig() { const config = { size: this.size, dataFormat: this.dataFormat, interpolation: this.interpolation }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ UpSampling2D.className = 'UpSampling2D'; serialization.registerClass(UpSampling2D); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * 2D convolution with separable filters. * @param x Input tensor. * @param depthwiseKernel Convolution kernel for depthwise convolution. * @param strides Strides (Array of two integers). * @param padding Padding model. * @param dataFormat Data format. * @param dilationRate Array of two integers, dilation rates for the separable * convolution. * @returns Output tensor. * @throws ValueError If depthwiseKernel is not a 4D array. */ function depthwiseConv2d$1(x, depthwiseKernel, strides = [1, 1], padding = 'valid', dataFormat, dilationRate) { return tidy$1(() => { if (dataFormat == null) { dataFormat = imageDataFormat(); } checkDataFormat(dataFormat); let y = preprocessConv2DInput(x, dataFormat); if (x.rank !== 4) { throw new ValueError(`Input for depthwiseConv2d is required to be 4-D, but is instead ` + `${x.rank}-D`); } if (depthwiseKernel.rank !== 4) { throw new ValueError(`depthwiseKernel is required to be 4-D, but is instead ` + `${depthwiseKernel.rank}-D`); } y = tfc.depthwiseConv2d(y, depthwiseKernel, strides, padding === 'same' ? 'same' : 'valid', 'NHWC', dilationRate); if (dataFormat === 'channelsFirst') { y = tfc.transpose(y, [0, 3, 1, 2]); } return y; }); } class DepthwiseConv2D extends BaseConv { constructor(args) { super(2, args); this.depthwiseKernel = null; this.depthMultiplier = args.depthMultiplier == null ? 1 : args.depthMultiplier; this.depthwiseInitializer = getInitializer(args.depthwiseInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.depthwiseConstraint = getConstraint(args.depthwiseConstraint); this.depthwiseRegularizer = getRegularizer(args.depthwiseRegularizer); } build(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape.length < 4) { throw new ValueError(`Inputs to DepthwiseConv2D should have rank 4. ` + `Received input shape: ${JSON.stringify(inputShape)}.`); } const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : 3; if (inputShape[channelAxis] == null || inputShape[channelAxis] < 0) { throw new ValueError('The channel dimension of the inputs to DepthwiseConv2D should ' + `be defined, but is not (${inputShape[channelAxis]}).`); } const inputDim = inputShape[channelAxis]; const depthwiseKernelShape = [ this.kernelSize[0], this.kernelSize[1], inputDim, this.depthMultiplier ]; this.depthwiseKernel = this.addWeight('depthwise_kernel', depthwiseKernelShape, null, this.depthwiseInitializer, this.depthwiseRegularizer, true, this.depthwiseConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [inputDim * this.depthMultiplier], null, this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } else { this.bias = null; } this.built = true; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); let outputs = depthwiseConv2d$1(inputs, this.depthwiseKernel.read(), this.strides, this.padding, this.dataFormat, null); // TODO(cais): Add support for dilation. if (this.useBias) { outputs = biasAdd(outputs, this.bias.read(), this.dataFormat); } if (this.activation != null) { outputs = this.activation.apply(outputs); } return outputs; }); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const rows = this.dataFormat === 'channelsFirst' ? inputShape[2] : inputShape[1]; const cols = this.dataFormat === 'channelsFirst' ? inputShape[3] : inputShape[2]; const outFilters = this.dataFormat === 'channelsFirst' ? inputShape[1] * this.depthMultiplier : inputShape[3] * this.depthMultiplier; const outRows = convOutputLength(rows, this.kernelSize[0], this.padding, this.strides[0]); const outCols = convOutputLength(cols, this.kernelSize[1], this.padding, this.strides[1]); if (this.dataFormat === 'channelsFirst') { return [inputShape[0], outFilters, outRows, outCols]; } else { // In this case, assume 'channelsLast'. return [inputShape[0], outRows, outCols, outFilters]; } } getConfig() { const config = super.getConfig(); config['depthMultiplier'] = this.depthMultiplier; config['depthwiseInitializer'] = serializeInitializer(this.depthwiseInitializer); config['depthwiseRegularizer'] = serializeRegularizer(this.depthwiseRegularizer); config['depthwiseConstraint'] = serializeConstraint(this.depthwiseRegularizer); return config; } } /** @nocollapse */ DepthwiseConv2D.className = 'DepthwiseConv2D'; serialization.registerClass(DepthwiseConv2D); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Standardize `apply()` args to a single list of tensor inputs. * * When running a model loaded from file, the input tensors `initialState` and * `constants` are passed to `RNN.apply()` as part of `inputs` instead of the * dedicated kwargs fields. `inputs` consists of * `[inputs, initialState0, initialState1, ..., constant0, constant1]` in this * case. * This method makes sure that arguments are * separated and that `initialState` and `constants` are `Array`s of tensors * (or None). * * @param inputs Tensor or `Array` of tensors. * @param initialState Tensor or `Array` of tensors or `null`/`undefined`. * @param constants Tensor or `Array` of tensors or `null`/`undefined`. * @returns An object consisting of * inputs: A tensor. * initialState: `Array` of tensors or `null`. * constants: `Array` of tensors or `null`. * @throws ValueError, if `inputs` is an `Array` but either `initialState` or * `constants` is provided. */ function standardizeArgs(inputs, initialState, constants, numConstants) { if (Array.isArray(inputs)) { if (initialState != null || constants != null) { throw new ValueError('When inputs is an array, neither initialState or constants ' + 'should be provided'); } if (numConstants != null) { constants = inputs.slice(inputs.length - numConstants, inputs.length); inputs = inputs.slice(0, inputs.length - numConstants); } if (inputs.length > 1) { initialState = inputs.slice(1, inputs.length); } inputs = inputs[0]; } function toListOrNull(x) { if (x == null || Array.isArray(x)) { return x; } else { return [x]; } } initialState = toListOrNull(initialState); constants = toListOrNull(constants); return { inputs, initialState, constants }; } /** * Iterates over the time dimension of a tensor. * * @param stepFunction RNN step function. * Parameters: * inputs: tensor with shape `[samples, ...]` (no time dimension), * representing input for the batch of samples at a certain time step. * states: an Array of tensors. * Returns: * outputs: tensor with shape `[samples, outputDim]` (no time dimension). * newStates: list of tensors, same length and shapes as `states`. The first * state in the list must be the output tensor at the previous timestep. * @param inputs Tensor of temporal data of shape `[samples, time, ...]` (at * least 3D). * @param initialStates Tensor with shape `[samples, outputDim]` (no time * dimension), containing the initial values of the states used in the step * function. * @param goBackwards If `true`, do the iteration over the time dimension in * reverse order and return the reversed sequence. * @param mask Binary tensor with shape `[sample, time, 1]`, with a zero for * every element that is masked. * @param constants An Array of constant values passed at each step. * @param unroll Whether to unroll the RNN or to use a symbolic loop. *Not* * applicable to this imperative deeplearn.js backend. Its value is ignored. * @param needPerStepOutputs Whether the per-step outputs are to be * concatenated into a single tensor and returned (as the second return * value). Default: `false`. This arg is included so that the relatively * expensive concatenation of the stepwise outputs can be omitted unless * the stepwise outputs need to be kept (e.g., for an LSTM layer of which * `returnSequence` is `true`.) * @returns An Array: `[lastOutput, outputs, newStates]`. * lastOutput: the lastest output of the RNN, of shape `[samples, ...]`. * outputs: tensor with shape `[samples, time, ...]` where each entry * `output[s, t]` is the output of the step function at time `t` for sample * `s`. This return value is provided if and only if the * `needPerStepOutputs` is set as `true`. If it is set as `false`, this * return value will be `undefined`. * newStates: Array of tensors, latest states returned by the step function, * of shape `(samples, ...)`. * @throws ValueError If input dimension is less than 3. * * TODO(nielsene): This needs to be tidy-ed. */ function rnn$1(stepFunction, inputs, initialStates, goBackwards = false, mask, constants, unroll = false, needPerStepOutputs = false) { return tfc.tidy(() => { const ndim = inputs.shape.length; if (ndim < 3) { throw new ValueError(`Input should be at least 3D, but is ${ndim}D.`); } // Transpose to time-major, i.e., from [batch, time, ...] to [time, batch, // ...]. const axes = [1, 0].concat(range(2, ndim)); inputs = tfc.transpose(inputs, axes); if (constants != null) { throw new NotImplementedError('The rnn() functoin of the deeplearn.js backend does not support ' + 'constants yet.'); } // Porting Note: the unroll option is ignored by the imperative backend. if (unroll) { console.warn('Backend rnn(): the unroll = true option is not applicable to the ' + 'imperative deeplearn.js backend.'); } if (mask != null) { mask = tfc.cast(tfc.cast(mask, 'bool'), 'float32'); if (mask.rank === ndim - 1) { mask = tfc.expandDims(mask, -1); } mask = tfc.transpose(mask, axes); } if (goBackwards) { inputs = tfc.reverse(inputs, 0); if (mask != null) { mask = tfc.reverse(mask, 0); } } // Porting Note: PyKeras with TensorFlow backend uses a symbolic loop // (tf.while_loop). But for the imperative deeplearn.js backend, we just // use the usual TypeScript control flow to iterate over the time steps in // the inputs. // Porting Note: PyKeras patches a "_use_learning_phase" attribute to // outputs. // This is not idiomatic in TypeScript. The info regarding whether we are // in a learning (i.e., training) phase for RNN is passed in a different // way. const perStepOutputs = []; let lastOutput; let states = initialStates; const timeSteps = inputs.shape[0]; const perStepInputs = tfc.unstack(inputs); let perStepMasks; if (mask != null) { perStepMasks = tfc.unstack(mask); } for (let t = 0; t < timeSteps; ++t) { const currentInput = perStepInputs[t]; const stepOutputs = tfc.tidy(() => stepFunction(currentInput, states)); if (mask == null) { lastOutput = stepOutputs[0]; states = stepOutputs[1]; } else { const maskedOutputs = tfc.tidy(() => { const stepMask = perStepMasks[t]; const negStepMask = tfc.sub(tfc.onesLike(stepMask), stepMask); // TODO(cais): Would tfc.where() be better for performance? const output = tfc.add(tfc.mul(stepOutputs[0], stepMask), tfc.mul(states[0], negStepMask)); const newStates = states.map((state, i) => { return tfc.add(tfc.mul(stepOutputs[1][i], stepMask), tfc.mul(state, negStepMask)); }); return { output, newStates }; }); lastOutput = maskedOutputs.output; states = maskedOutputs.newStates; } if (needPerStepOutputs) { perStepOutputs.push(lastOutput); } } let outputs; if (needPerStepOutputs) { const axis = 1; outputs = tfc.stack(perStepOutputs, axis); } return [lastOutput, outputs, states]; }); } class RNN extends Layer { constructor(args) { super(args); let cell; if (args.cell == null) { throw new ValueError('cell property is missing for the constructor of RNN.'); } else if (Array.isArray(args.cell)) { cell = new StackedRNNCells({ cells: args.cell }); } else { cell = args.cell; } if (cell.stateSize == null) { throw new ValueError('The RNN cell should have an attribute `stateSize` (tuple of ' + 'integers, one integer per RNN state).'); } this.cell = cell; this.returnSequences = args.returnSequences == null ? false : args.returnSequences; this.returnState = args.returnState == null ? false : args.returnState; this.goBackwards = args.goBackwards == null ? false : args.goBackwards; this._stateful = args.stateful == null ? false : args.stateful; this.unroll = args.unroll == null ? false : args.unroll; this.supportsMasking = true; this.inputSpec = [new InputSpec({ ndim: 3 })]; this.stateSpec = null; this.states_ = null; // TODO(cais): Add constantsSpec and numConstants. this.numConstants = null; // TODO(cais): Look into the use of initial_state in the kwargs of the // constructor. this.keptStates = []; } // Porting Note: This is the equivalent of `RNN.states` property getter in // PyKeras. getStates() { if (this.states_ == null) { const numStates = Array.isArray(this.cell.stateSize) ? this.cell.stateSize.length : 1; return range(0, numStates).map(x => null); } else { return this.states_; } } // Porting Note: This is the equivalent of the `RNN.states` property setter in // PyKeras. setStates(states) { this.states_ = states; } computeOutputShape(inputShape) { if (isArrayOfShapes(inputShape)) { inputShape = inputShape[0]; } inputShape = inputShape; // TODO(cais): Remove the casting once stacked RNN cells become supported. let stateSize = this.cell.stateSize; if (!Array.isArray(stateSize)) { stateSize = [stateSize]; } const outputDim = stateSize[0]; let outputShape; if (this.returnSequences) { outputShape = [inputShape[0], inputShape[1], outputDim]; } else { outputShape = [inputShape[0], outputDim]; } if (this.returnState) { const stateShape = []; for (const dim of stateSize) { stateShape.push([inputShape[0], dim]); } return [outputShape].concat(stateShape); } else { return outputShape; } } computeMask(inputs, mask) { return tfc.tidy(() => { if (Array.isArray(mask)) { mask = mask[0]; } const outputMask = this.returnSequences ? mask : null; if (this.returnState) { const stateMask = this.states.map(s => null); return [outputMask].concat(stateMask); } else { return outputMask; } }); } /** * Get the current state tensors of the RNN. * * If the state hasn't been set, return an array of `null`s of the correct * length. */ get states() { if (this.states_ == null) { const numStates = Array.isArray(this.cell.stateSize) ? this.cell.stateSize.length : 1; const output = []; for (let i = 0; i < numStates; ++i) { output.push(null); } return output; } else { return this.states_; } } set states(s) { this.states_ = s; } build(inputShape) { if (this.numConstants != null) { throw new NotImplementedError('Constants support is not implemented in RNN yet.'); } if (isArrayOfShapes(inputShape)) { inputShape = inputShape[0]; } inputShape = inputShape; const batchSize = this.stateful ? inputShape[0] : null; const inputDim = inputShape.slice(2); this.inputSpec[0] = new InputSpec({ shape: [batchSize, null, ...inputDim] }); // Allow cell (if RNNCell Layer) to build before we set or validate // stateSpec. const stepInputShape = [inputShape[0]].concat(inputShape.slice(2)); { this.cell.build(stepInputShape); } // Set or validate stateSpec. let stateSize; if (Array.isArray(this.cell.stateSize)) { stateSize = this.cell.stateSize; } else { stateSize = [this.cell.stateSize]; } if (this.stateSpec != null) { if (!util.arraysEqual(this.stateSpec.map(spec => spec.shape[spec.shape.length - 1]), stateSize)) { throw new ValueError(`An initialState was passed that is not compatible with ` + `cell.stateSize. Received stateSpec=${this.stateSpec}; ` + `However cell.stateSize is ${this.cell.stateSize}`); } } else { this.stateSpec = stateSize.map(dim => new InputSpec({ shape: [null, dim] })); } if (this.stateful) { this.resetStates(); } } /** * Reset the state tensors of the RNN. * * If the `states` argument is `undefined` or `null`, will set the * state tensor(s) of the RNN to all-zero tensors of the appropriate * shape(s). * * If `states` is provided, will set the state tensors of the RNN to its * value. * * @param states Optional externally-provided initial states. * @param training Whether this call is done during training. For stateful * RNNs, this affects whether the old states are kept or discarded. In * particular, if `training` is `true`, the old states will be kept so * that subsequent backpropgataion through time (BPTT) may work properly. * Else, the old states will be discarded. */ resetStates(states, training = false) { tidy$1(() => { if (!this.stateful) { throw new AttributeError('Cannot call resetStates() on an RNN Layer that is not stateful.'); } const batchSize = this.inputSpec[0].shape[0]; if (batchSize == null) { throw new ValueError('If an RNN is stateful, it needs to know its batch size. Specify ' + 'the batch size of your input tensors: \n' + '- If using a Sequential model, specify the batch size by ' + 'passing a `batchInputShape` option to your first layer.\n' + '- If using the functional API, specify the batch size by ' + 'passing a `batchShape` option to your Input layer.'); } // Initialize state if null. if (this.states_ == null) { if (Array.isArray(this.cell.stateSize)) { this.states_ = this.cell.stateSize.map(dim => tfc.zeros([batchSize, dim])); } else { this.states_ = [tfc.zeros([batchSize, this.cell.stateSize])]; } } else if (states == null) { // Dispose old state tensors. tfc.dispose(this.states_); // For stateful RNNs, fully dispose kept old states. if (this.keptStates != null) { tfc.dispose(this.keptStates); this.keptStates = []; } if (Array.isArray(this.cell.stateSize)) { this.states_ = this.cell.stateSize.map(dim => tfc.zeros([batchSize, dim])); } else { this.states_[0] = tfc.zeros([batchSize, this.cell.stateSize]); } } else { if (!Array.isArray(states)) { states = [states]; } if (states.length !== this.states_.length) { throw new ValueError(`Layer ${this.name} expects ${this.states_.length} state(s), ` + `but it received ${states.length} state value(s). Input ` + `received: ${states}`); } if (training === true) { // Store old state tensors for complete disposal later, i.e., during // the next no-arg call to this method. We do not dispose the old // states immediately because that BPTT (among other things) require // them. this.keptStates.push(this.states_.slice()); } else { tfc.dispose(this.states_); } for (let index = 0; index < this.states_.length; ++index) { const value = states[index]; const dim = Array.isArray(this.cell.stateSize) ? this.cell.stateSize[index] : this.cell.stateSize; const expectedShape = [batchSize, dim]; if (!util.arraysEqual(value.shape, expectedShape)) { throw new ValueError(`State ${index} is incompatible with layer ${this.name}: ` + `expected shape=${expectedShape}, received shape=${value.shape}`); } this.states_[index] = value; } } this.states_ = this.states_.map(state => tfc.keep(state.clone())); }); } apply(inputs, kwargs) { // TODO(cais): Figure out whether initialState is in kwargs or inputs. let initialState = kwargs == null ? null : kwargs['initialState']; let constants = kwargs == null ? null : kwargs['constants']; if (kwargs == null) { kwargs = {}; } const standardized = standardizeArgs(inputs, initialState, constants, this.numConstants); inputs = standardized.inputs; initialState = standardized.initialState; constants = standardized.constants; // If any of `initial_state` or `constants` are specified and are // `tf.SymbolicTensor`s, then add them to the inputs and temporarily modify // the input_spec to include them. let additionalInputs = []; let additionalSpecs = []; if (initialState != null) { kwargs['initialState'] = initialState; additionalInputs = additionalInputs.concat(initialState); this.stateSpec = []; for (const state of initialState) { this.stateSpec.push(new InputSpec({ shape: state.shape })); } // TODO(cais): Use the following instead. // this.stateSpec = initialState.map(state => new InputSpec({shape: // state.shape})); additionalSpecs = additionalSpecs.concat(this.stateSpec); } if (constants != null) { kwargs['constants'] = constants; additionalInputs = additionalInputs.concat(constants); // TODO(cais): Add this.constantsSpec. this.numConstants = constants.length; } const isTensor = additionalInputs[0] instanceof SymbolicTensor; if (isTensor) { // Compute full input spec, including state and constants. const fullInput = [inputs].concat(additionalInputs); const fullInputSpec = this.inputSpec.concat(additionalSpecs); // Perform the call with temporarily replaced inputSpec. const originalInputSpec = this.inputSpec; this.inputSpec = fullInputSpec; const output = super.apply(fullInput, kwargs); this.inputSpec = originalInputSpec; return output; } else { return super.apply(inputs, kwargs); } } // tslint:disable-next-line:no-any call(inputs, kwargs) { // Input shape: `[samples, time (padded with zeros), input_dim]`. // Note that the .build() method of subclasses **must** define // this.inputSpec and this.stateSpec owith complete input shapes. return tidy$1(() => { const mask = kwargs == null ? null : kwargs['mask']; const training = kwargs == null ? null : kwargs['training']; let initialState = kwargs == null ? null : kwargs['initialState']; inputs = getExactlyOneTensor(inputs); if (initialState == null) { if (this.stateful) { initialState = this.states_; } else { initialState = this.getInitialState(inputs); } } const numStates = Array.isArray(this.cell.stateSize) ? this.cell.stateSize.length : 1; if (initialState.length !== numStates) { throw new ValueError(`RNN Layer has ${numStates} state(s) but was passed ` + `${initialState.length} initial state(s).`); } if (this.unroll) { console.warn('Ignoring unroll = true for RNN layer, due to imperative backend.'); } const cellCallKwargs = { training }; // TODO(cais): Add support for constants. const step = (inputs, states) => { // `inputs` and `states` are concatenated to form a single `Array` of // `tf.Tensor`s as the input to `cell.call()`. const outputs = this.cell.call([inputs].concat(states), cellCallKwargs); // Marshall the return value into output and new states. return [outputs[0], outputs.slice(1)]; }; // TODO(cais): Add support for constants. const rnnOutputs = rnn$1(step, inputs, initialState, this.goBackwards, mask, null, this.unroll, this.returnSequences); const lastOutput = rnnOutputs[0]; const outputs = rnnOutputs[1]; const states = rnnOutputs[2]; if (this.stateful) { this.resetStates(states, training); } const output = this.returnSequences ? outputs : lastOutput; // TODO(cais): Porperty set learning phase flag. if (this.returnState) { return [output].concat(states); } else { return output; } }); } getInitialState(inputs) { return tidy$1(() => { // Build an all-zero tensor of shape [samples, outputDim]. // [Samples, timeSteps, inputDim]. let initialState = tfc.zeros(inputs.shape); // [Samples]. initialState = tfc.sum(initialState, [1, 2]); initialState = expandDims$1(initialState); // [Samples, 1]. if (Array.isArray(this.cell.stateSize)) { return this.cell.stateSize.map(dim => dim > 1 ? tile$1(initialState, [1, dim]) : initialState); } else { return this.cell.stateSize > 1 ? [tile$1(initialState, [1, this.cell.stateSize])] : [initialState]; } }); } get trainableWeights() { if (!this.trainable) { return []; } // Porting Note: In TypeScript, `this` is always an instance of `Layer`. return this.cell.trainableWeights; } get nonTrainableWeights() { // Porting Note: In TypeScript, `this` is always an instance of `Layer`. if (!this.trainable) { return this.cell.weights; } return this.cell.nonTrainableWeights; } setFastWeightInitDuringBuild(value) { super.setFastWeightInitDuringBuild(value); if (this.cell != null) { this.cell.setFastWeightInitDuringBuild(value); } } getConfig() { const baseConfig = super.getConfig(); const config = { returnSequences: this.returnSequences, returnState: this.returnState, goBackwards: this.goBackwards, stateful: this.stateful, unroll: this.unroll, }; if (this.numConstants != null) { config['numConstants'] = this.numConstants; } const cellConfig = this.cell.getConfig(); if (this.getClassName() === RNN.className) { config['cell'] = { 'className': this.cell.getClassName(), 'config': cellConfig, }; } // this order is necessary, to prevent cell name from replacing layer name return Object.assign(Object.assign(Object.assign({}, cellConfig), baseConfig), config); } /** @nocollapse */ static fromConfig(cls, config, customObjects = {}) { const cellConfig = config['cell']; const cell = deserialize(cellConfig, customObjects); return new cls(Object.assign(config, { cell })); } } /** @nocollapse */ RNN.className = 'RNN'; serialization.registerClass(RNN); // Porting Note: This is a common parent class for RNN cells. There is no // equivalent of this in PyKeras. Having a common parent class forgoes the // need for `has_attr(cell, ...)` checks or its TypeScript equivalent. /** * An RNNCell layer. * * @doc {heading: 'Layers', subheading: 'Classes'} */ class RNNCell extends Layer { } class SimpleRNNCell extends RNNCell { constructor(args) { super(args); this.DEFAULT_ACTIVATION = 'tanh'; this.DEFAULT_KERNEL_INITIALIZER = 'glorotNormal'; this.DEFAULT_RECURRENT_INITIALIZER = 'orthogonal'; this.DEFAULT_BIAS_INITIALIZER = 'zeros'; this.units = args.units; assertPositiveInteger(this.units, `units`); this.activation = getActivation(args.activation == null ? this.DEFAULT_ACTIVATION : args.activation); this.useBias = args.useBias == null ? true : args.useBias; this.kernelInitializer = getInitializer(args.kernelInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.recurrentInitializer = getInitializer(args.recurrentInitializer || this.DEFAULT_RECURRENT_INITIALIZER); this.biasInitializer = getInitializer(args.biasInitializer || this.DEFAULT_BIAS_INITIALIZER); this.kernelRegularizer = getRegularizer(args.kernelRegularizer); this.recurrentRegularizer = getRegularizer(args.recurrentRegularizer); this.biasRegularizer = getRegularizer(args.biasRegularizer); this.kernelConstraint = getConstraint(args.kernelConstraint); this.recurrentConstraint = getConstraint(args.recurrentConstraint); this.biasConstraint = getConstraint(args.biasConstraint); this.dropout = min([1, max([0, args.dropout == null ? 0 : args.dropout])]); this.recurrentDropout = min([ 1, max([0, args.recurrentDropout == null ? 0 : args.recurrentDropout]) ]); this.dropoutFunc = args.dropoutFunc; this.stateSize = this.units; this.dropoutMask = null; this.recurrentDropoutMask = null; } build(inputShape) { inputShape = getExactlyOneShape(inputShape); // TODO(cais): Use regularizer. this.kernel = this.addWeight('kernel', [inputShape[inputShape.length - 1], this.units], null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); this.recurrentKernel = this.addWeight('recurrent_kernel', [this.units, this.units], null, this.recurrentInitializer, this.recurrentRegularizer, true, this.recurrentConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.units], null, this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } else { this.bias = null; } this.built = true; } // Porting Note: PyKeras' equivalent of this method takes two tensor inputs: // `inputs` and `states`. Here, the two tensors are combined into an // `Tensor[]` Array as the first input argument. // Similarly, PyKeras' equivalent of this method returns two values: // `output` and `[output]`. Here the two are combined into one length-2 // `Tensor[]`, consisting of `output` repeated. call(inputs, kwargs) { return tidy$1(() => { inputs = inputs; if (inputs.length !== 2) { throw new ValueError(`SimpleRNNCell expects 2 input Tensors, got ${inputs.length}.`); } let prevOutput = inputs[1]; inputs = inputs[0]; const training = kwargs['training'] == null ? false : kwargs['training']; if (0 < this.dropout && this.dropout < 1 && this.dropoutMask == null) { this.dropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(inputs), rate: this.dropout, training, dropoutFunc: this.dropoutFunc, }); } if (0 < this.recurrentDropout && this.recurrentDropout < 1 && this.recurrentDropoutMask == null) { this.recurrentDropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(prevOutput), rate: this.recurrentDropout, training, dropoutFunc: this.dropoutFunc, }); } let h; const dpMask = this.dropoutMask; const recDpMask = this.recurrentDropoutMask; if (dpMask != null) { h = dot$1(tfc.mul(inputs, dpMask), this.kernel.read()); } else { h = dot$1(inputs, this.kernel.read()); } if (this.bias != null) { h = biasAdd(h, this.bias.read()); } if (recDpMask != null) { prevOutput = tfc.mul(prevOutput, recDpMask); } let output = tfc.add(h, dot$1(prevOutput, this.recurrentKernel.read())); if (this.activation != null) { output = this.activation.apply(output); } // TODO(cais): Properly set learning phase on output tensor? return [output, output]; }); } getConfig() { const baseConfig = super.getConfig(); const config = { units: this.units, activation: serializeActivation(this.activation), useBias: this.useBias, kernelInitializer: serializeInitializer(this.kernelInitializer), recurrentInitializer: serializeInitializer(this.recurrentInitializer), biasInitializer: serializeInitializer(this.biasInitializer), kernelRegularizer: serializeRegularizer(this.kernelRegularizer), recurrentRegularizer: serializeRegularizer(this.recurrentRegularizer), biasRegularizer: serializeRegularizer(this.biasRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), kernelConstraint: serializeConstraint(this.kernelConstraint), recurrentConstraint: serializeConstraint(this.recurrentConstraint), biasConstraint: serializeConstraint(this.biasConstraint), dropout: this.dropout, recurrentDropout: this.recurrentDropout, }; return Object.assign(Object.assign({}, baseConfig), config); } } /** @nocollapse */ SimpleRNNCell.className = 'SimpleRNNCell'; serialization.registerClass(SimpleRNNCell); class SimpleRNN extends RNN { constructor(args) { args.cell = new SimpleRNNCell(args); super(args); // TODO(cais): Add activityRegularizer. } call(inputs, kwargs) { return tidy$1(() => { if (this.cell.dropoutMask != null) { tfc.dispose(this.cell.dropoutMask); this.cell.dropoutMask = null; } if (this.cell.recurrentDropoutMask != null) { tfc.dispose(this.cell.recurrentDropoutMask); this.cell.recurrentDropoutMask = null; } const mask = kwargs == null ? null : kwargs['mask']; const training = kwargs == null ? null : kwargs['training']; const initialState = kwargs == null ? null : kwargs['initialState']; return super.call(inputs, { mask, training, initialState }); }); } /** @nocollapse */ static fromConfig(cls, config) { return new cls(config); } } /** @nocollapse */ SimpleRNN.className = 'SimpleRNN'; serialization.registerClass(SimpleRNN); class GRUCell extends RNNCell { constructor(args) { super(args); this.DEFAULT_ACTIVATION = 'tanh'; this.DEFAULT_RECURRENT_ACTIVATION = 'hardSigmoid'; this.DEFAULT_KERNEL_INITIALIZER = 'glorotNormal'; this.DEFAULT_RECURRENT_INITIALIZER = 'orthogonal'; this.DEFAULT_BIAS_INITIALIZER = 'zeros'; if (args.resetAfter) { throw new ValueError(`GRUCell does not support reset_after parameter set to true.`); } this.units = args.units; assertPositiveInteger(this.units, 'units'); this.activation = getActivation(args.activation === undefined ? this.DEFAULT_ACTIVATION : args.activation); this.recurrentActivation = getActivation(args.recurrentActivation === undefined ? this.DEFAULT_RECURRENT_ACTIVATION : args.recurrentActivation); this.useBias = args.useBias == null ? true : args.useBias; this.kernelInitializer = getInitializer(args.kernelInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.recurrentInitializer = getInitializer(args.recurrentInitializer || this.DEFAULT_RECURRENT_INITIALIZER); this.biasInitializer = getInitializer(args.biasInitializer || this.DEFAULT_BIAS_INITIALIZER); this.kernelRegularizer = getRegularizer(args.kernelRegularizer); this.recurrentRegularizer = getRegularizer(args.recurrentRegularizer); this.biasRegularizer = getRegularizer(args.biasRegularizer); this.kernelConstraint = getConstraint(args.kernelConstraint); this.recurrentConstraint = getConstraint(args.recurrentConstraint); this.biasConstraint = getConstraint(args.biasConstraint); this.dropout = min([1, max([0, args.dropout == null ? 0 : args.dropout])]); this.recurrentDropout = min([ 1, max([0, args.recurrentDropout == null ? 0 : args.recurrentDropout]) ]); this.dropoutFunc = args.dropoutFunc; this.implementation = args.implementation; this.stateSize = this.units; this.dropoutMask = null; this.recurrentDropoutMask = null; } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const inputDim = inputShape[inputShape.length - 1]; this.kernel = this.addWeight('kernel', [inputDim, this.units * 3], null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); this.recurrentKernel = this.addWeight('recurrent_kernel', [this.units, this.units * 3], null, this.recurrentInitializer, this.recurrentRegularizer, true, this.recurrentConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.units * 3], null, this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } else { this.bias = null; } // Porting Notes: Unlike the PyKeras implementation, we perform slicing // of the weights and bias in the call() method, at execution time. this.built = true; } call(inputs, kwargs) { return tidy$1(() => { inputs = inputs; if (inputs.length !== 2) { throw new ValueError(`GRUCell expects 2 input Tensors (inputs, h, c), got ` + `${inputs.length}.`); } const training = kwargs['training'] == null ? false : kwargs['training']; let hTMinus1 = inputs[1]; // Previous memory state. inputs = inputs[0]; // Note: For superior performance, TensorFlow.js always uses // implementation 2, regardless of the actual value of // config.implementation. if (0 < this.dropout && this.dropout < 1 && this.dropoutMask == null) { this.dropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(inputs), rate: this.dropout, training, count: 3, dropoutFunc: this.dropoutFunc, }); } if (0 < this.recurrentDropout && this.recurrentDropout < 1 && this.recurrentDropoutMask == null) { this.recurrentDropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(hTMinus1), rate: this.recurrentDropout, training, count: 3, dropoutFunc: this.dropoutFunc, }); } const dpMask = this.dropoutMask; const recDpMask = this.recurrentDropoutMask; let z; let r; let hh; if (0 < this.dropout && this.dropout < 1) { inputs = tfc.mul(inputs, dpMask[0]); } let matrixX = dot$1(inputs, this.kernel.read()); if (this.useBias) { matrixX = biasAdd(matrixX, this.bias.read()); } if (0 < this.recurrentDropout && this.recurrentDropout < 1) { hTMinus1 = tfc.mul(hTMinus1, recDpMask[0]); } const recurrentKernelValue = this.recurrentKernel.read(); const [rk1, rk2] = tfc.split(recurrentKernelValue, [2 * this.units, this.units], recurrentKernelValue.rank - 1); const matrixInner = dot$1(hTMinus1, rk1); const [xZ, xR, xH] = tfc.split(matrixX, 3, matrixX.rank - 1); const [recurrentZ, recurrentR] = tfc.split(matrixInner, 2, matrixInner.rank - 1); z = this.recurrentActivation.apply(tfc.add(xZ, recurrentZ)); r = this.recurrentActivation.apply(tfc.add(xR, recurrentR)); const recurrentH = dot$1(tfc.mul(r, hTMinus1), rk2); hh = this.activation.apply(tfc.add(xH, recurrentH)); const h = tfc.add(tfc.mul(z, hTMinus1), tfc.mul(tfc.add(1, tfc.neg(z)), hh)); // TODO(cais): Add use_learning_phase flag properly. return [h, h]; }); } getConfig() { const baseConfig = super.getConfig(); const config = { units: this.units, activation: serializeActivation(this.activation), recurrentActivation: serializeActivation(this.recurrentActivation), useBias: this.useBias, kernelInitializer: serializeInitializer(this.kernelInitializer), recurrentInitializer: serializeInitializer(this.recurrentInitializer), biasInitializer: serializeInitializer(this.biasInitializer), kernelRegularizer: serializeRegularizer(this.kernelRegularizer), recurrentRegularizer: serializeRegularizer(this.recurrentRegularizer), biasRegularizer: serializeRegularizer(this.biasRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), kernelConstraint: serializeConstraint(this.kernelConstraint), recurrentConstraint: serializeConstraint(this.recurrentConstraint), biasConstraint: serializeConstraint(this.biasConstraint), dropout: this.dropout, recurrentDropout: this.recurrentDropout, implementation: this.implementation, resetAfter: false }; return Object.assign(Object.assign({}, baseConfig), config); } } /** @nocollapse */ GRUCell.className = 'GRUCell'; serialization.registerClass(GRUCell); class GRU extends RNN { constructor(args) { if (args.implementation === 0) { console.warn('`implementation=0` has been deprecated, and now defaults to ' + '`implementation=1`. Please update your layer call.'); } args.cell = new GRUCell(args); super(args); // TODO(cais): Add activityRegularizer. } call(inputs, kwargs) { return tidy$1(() => { if (this.cell.dropoutMask != null) { tfc.dispose(this.cell.dropoutMask); this.cell.dropoutMask = null; } if (this.cell.recurrentDropoutMask != null) { tfc.dispose(this.cell.recurrentDropoutMask); this.cell.recurrentDropoutMask = null; } const mask = kwargs == null ? null : kwargs['mask']; const training = kwargs == null ? null : kwargs['training']; const initialState = kwargs == null ? null : kwargs['initialState']; return super.call(inputs, { mask, training, initialState }); }); } /** @nocollapse */ static fromConfig(cls, config) { if (config['implmentation'] === 0) { config['implementation'] = 1; } return new cls(config); } } /** @nocollapse */ GRU.className = 'GRU'; serialization.registerClass(GRU); class LSTMCell extends RNNCell { constructor(args) { super(args); this.DEFAULT_ACTIVATION = 'tanh'; this.DEFAULT_RECURRENT_ACTIVATION = 'hardSigmoid'; this.DEFAULT_KERNEL_INITIALIZER = 'glorotNormal'; this.DEFAULT_RECURRENT_INITIALIZER = 'orthogonal'; this.DEFAULT_BIAS_INITIALIZER = 'zeros'; this.units = args.units; assertPositiveInteger(this.units, 'units'); this.activation = getActivation(args.activation === undefined ? this.DEFAULT_ACTIVATION : args.activation); this.recurrentActivation = getActivation(args.recurrentActivation === undefined ? this.DEFAULT_RECURRENT_ACTIVATION : args.recurrentActivation); this.useBias = args.useBias == null ? true : args.useBias; this.kernelInitializer = getInitializer(args.kernelInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.recurrentInitializer = getInitializer(args.recurrentInitializer || this.DEFAULT_RECURRENT_INITIALIZER); this.biasInitializer = getInitializer(args.biasInitializer || this.DEFAULT_BIAS_INITIALIZER); this.unitForgetBias = args.unitForgetBias; this.kernelRegularizer = getRegularizer(args.kernelRegularizer); this.recurrentRegularizer = getRegularizer(args.recurrentRegularizer); this.biasRegularizer = getRegularizer(args.biasRegularizer); this.kernelConstraint = getConstraint(args.kernelConstraint); this.recurrentConstraint = getConstraint(args.recurrentConstraint); this.biasConstraint = getConstraint(args.biasConstraint); this.dropout = min([1, max([0, args.dropout == null ? 0 : args.dropout])]); this.recurrentDropout = min([ 1, max([0, args.recurrentDropout == null ? 0 : args.recurrentDropout]) ]); this.dropoutFunc = args.dropoutFunc; this.implementation = args.implementation; this.stateSize = [this.units, this.units]; this.dropoutMask = null; this.recurrentDropoutMask = null; } build(inputShape) { var _a; inputShape = getExactlyOneShape(inputShape); const inputDim = inputShape[inputShape.length - 1]; this.kernel = this.addWeight('kernel', [inputDim, this.units * 4], null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); this.recurrentKernel = this.addWeight('recurrent_kernel', [this.units, this.units * 4], null, this.recurrentInitializer, this.recurrentRegularizer, true, this.recurrentConstraint); let biasInitializer; if (this.useBias) { if (this.unitForgetBias) { const capturedBiasInit = this.biasInitializer; const capturedUnits = this.units; biasInitializer = new (_a = class CustomInit extends Initializer { apply(shape, dtype) { // TODO(cais): More informative variable names? const bI = capturedBiasInit.apply([capturedUnits]); const bF = (new Ones()).apply([capturedUnits]); const bCAndH = capturedBiasInit.apply([capturedUnits * 2]); return concatAlongFirstAxis(concatAlongFirstAxis(bI, bF), bCAndH); } }, /** @nocollapse */ _a.className = 'CustomInit', _a)(); } else { biasInitializer = this.biasInitializer; } this.bias = this.addWeight('bias', [this.units * 4], null, biasInitializer, this.biasRegularizer, true, this.biasConstraint); } else { this.bias = null; } // Porting Notes: Unlike the PyKeras implementation, we perform slicing // of the weights and bias in the call() method, at execution time. this.built = true; } call(inputs, kwargs) { return tidy$1(() => { const training = kwargs['training'] == null ? false : kwargs['training']; inputs = inputs; if (inputs.length !== 3) { throw new ValueError(`LSTMCell expects 3 input Tensors (inputs, h, c), got ` + `${inputs.length}.`); } let hTMinus1 = inputs[1]; // Previous memory state. const cTMinus1 = inputs[2]; // Previous carry state. inputs = inputs[0]; if (0 < this.dropout && this.dropout < 1 && this.dropoutMask == null) { this.dropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(inputs), rate: this.dropout, training, count: 4, dropoutFunc: this.dropoutFunc }); } if (0 < this.recurrentDropout && this.recurrentDropout < 1 && this.recurrentDropoutMask == null) { this.recurrentDropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(hTMinus1), rate: this.recurrentDropout, training, count: 4, dropoutFunc: this.dropoutFunc }); } const dpMask = this.dropoutMask; const recDpMask = this.recurrentDropoutMask; // Note: For superior performance, TensorFlow.js always uses // implementation 2 regardless of the actual value of // config.implementation. let i; let f; let c; let o; if (0 < this.dropout && this.dropout < 1) { inputs = tfc.mul(inputs, dpMask[0]); } let z = dot$1(inputs, this.kernel.read()); if (0 < this.recurrentDropout && this.recurrentDropout < 1) { hTMinus1 = tfc.mul(hTMinus1, recDpMask[0]); } z = tfc.add(z, dot$1(hTMinus1, this.recurrentKernel.read())); if (this.useBias) { z = biasAdd(z, this.bias.read()); } const [z0, z1, z2, z3] = tfc.split(z, 4, z.rank - 1); i = this.recurrentActivation.apply(z0); f = this.recurrentActivation.apply(z1); c = tfc.add(tfc.mul(f, cTMinus1), tfc.mul(i, this.activation.apply(z2))); o = this.recurrentActivation.apply(z3); const h = tfc.mul(o, this.activation.apply(c)); // TODO(cais): Add use_learning_phase flag properly. return [h, h, c]; }); } getConfig() { const baseConfig = super.getConfig(); const config = { units: this.units, activation: serializeActivation(this.activation), recurrentActivation: serializeActivation(this.recurrentActivation), useBias: this.useBias, kernelInitializer: serializeInitializer(this.kernelInitializer), recurrentInitializer: serializeInitializer(this.recurrentInitializer), biasInitializer: serializeInitializer(this.biasInitializer), unitForgetBias: this.unitForgetBias, kernelRegularizer: serializeRegularizer(this.kernelRegularizer), recurrentRegularizer: serializeRegularizer(this.recurrentRegularizer), biasRegularizer: serializeRegularizer(this.biasRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), kernelConstraint: serializeConstraint(this.kernelConstraint), recurrentConstraint: serializeConstraint(this.recurrentConstraint), biasConstraint: serializeConstraint(this.biasConstraint), dropout: this.dropout, recurrentDropout: this.recurrentDropout, implementation: this.implementation, }; return Object.assign(Object.assign({}, baseConfig), config); } } /** @nocollapse */ LSTMCell.className = 'LSTMCell'; serialization.registerClass(LSTMCell); class LSTM extends RNN { constructor(args) { if (args.implementation === 0) { console.warn('`implementation=0` has been deprecated, and now defaults to ' + '`implementation=1`. Please update your layer call.'); } args.cell = new LSTMCell(args); super(args); // TODO(cais): Add activityRegularizer. } call(inputs, kwargs) { return tidy$1(() => { if (this.cell.dropoutMask != null) { tfc.dispose(this.cell.dropoutMask); this.cell.dropoutMask = null; } if (this.cell.recurrentDropoutMask != null) { tfc.dispose(this.cell.recurrentDropoutMask); this.cell.recurrentDropoutMask = null; } const mask = kwargs == null ? null : kwargs['mask']; const training = kwargs == null ? null : kwargs['training']; const initialState = kwargs == null ? null : kwargs['initialState']; return super.call(inputs, { mask, training, initialState }); }); } /** @nocollapse */ static fromConfig(cls, config) { if (config['implmentation'] === 0) { config['implementation'] = 1; } return new cls(config); } } /** @nocollapse */ LSTM.className = 'LSTM'; serialization.registerClass(LSTM); class StackedRNNCells extends RNNCell { constructor(args) { super(args); this.cells = args.cells; } get stateSize() { // States are a flat list in reverse order of the cell stack. // This allows perserving the requirement `stack.statesize[0] === // outputDim`. E.g., states of a 2-layer LSTM would be `[h2, c2, h1, c1]`, // assuming one LSTM has states `[h, c]`. const stateSize = []; for (const cell of this.cells.slice().reverse()) { if (Array.isArray(cell.stateSize)) { stateSize.push(...cell.stateSize); } else { stateSize.push(cell.stateSize); } } return stateSize; } call(inputs, kwargs) { return tidy$1(() => { inputs = inputs; let states = inputs.slice(1); // Recover per-cell states. const nestedStates = []; for (const cell of this.cells.slice().reverse()) { if (Array.isArray(cell.stateSize)) { nestedStates.push(states.splice(0, cell.stateSize.length)); } else { nestedStates.push(states.splice(0, 1)); } } nestedStates.reverse(); // Call the cells in order and store the returned states. const newNestedStates = []; let callInputs; for (let i = 0; i < this.cells.length; ++i) { const cell = this.cells[i]; states = nestedStates[i]; // TODO(cais): Take care of constants. if (i === 0) { callInputs = [inputs[0]].concat(states); } else { callInputs = [callInputs[0]].concat(states); } callInputs = cell.call(callInputs, kwargs); newNestedStates.push(callInputs.slice(1)); } // Format the new states as a flat list in reverse cell order. states = []; for (const cellStates of newNestedStates.slice().reverse()) { states.push(...cellStates); } return [callInputs[0]].concat(states); }); } build(inputShape) { if (isArrayOfShapes(inputShape)) { // TODO(cais): Take care of input constants. // const constantShape = inputShape.slice(1); inputShape = inputShape[0]; } inputShape = inputShape; let outputDim; this.cells.forEach((cell, i) => { nameScope(`RNNCell_${i}`, () => { // TODO(cais): Take care of input constants. cell.build(inputShape); if (Array.isArray(cell.stateSize)) { outputDim = cell.stateSize[0]; } else { outputDim = cell.stateSize; } inputShape = [inputShape[0], outputDim]; }); }); this.built = true; } getConfig() { const baseConfig = super.getConfig(); const getCellConfig = (cell) => { return { 'className': cell.getClassName(), 'config': cell.getConfig(), }; }; const cellConfigs = this.cells.map(getCellConfig); const config = { 'cells': cellConfigs }; return Object.assign(Object.assign({}, baseConfig), config); } /** @nocollapse */ static fromConfig(cls, config, customObjects = {}) { const cells = []; for (const cellConfig of config['cells']) { cells.push(deserialize(cellConfig, customObjects)); } return new cls({ cells }); } get trainableWeights() { if (!this.trainable) { return []; } const weights = []; for (const cell of this.cells) { weights.push(...cell.trainableWeights); } return weights; } get nonTrainableWeights() { const weights = []; for (const cell of this.cells) { weights.push(...cell.nonTrainableWeights); } if (!this.trainable) { const trainableWeights = []; for (const cell of this.cells) { trainableWeights.push(...cell.trainableWeights); } return trainableWeights.concat(weights); } return weights; } /** * Retrieve the weights of a the model. * * @returns A flat `Array` of `tf.Tensor`s. */ getWeights() { const weights = []; for (const cell of this.cells) { weights.push(...cell.weights); } return batchGetValue(weights); } /** * Set the weights of the model. * * @param weights An `Array` of `tf.Tensor`s with shapes and types matching * the output of `getWeights()`. */ setWeights(weights) { const tuples = []; for (const cell of this.cells) { const numParams = cell.weights.length; const inputWeights = weights.splice(numParams); for (let i = 0; i < cell.weights.length; ++i) { tuples.push([cell.weights[i], inputWeights[i]]); } } batchSetValue(tuples); } } /** @nocollapse */ StackedRNNCells.className = 'StackedRNNCells'; serialization.registerClass(StackedRNNCells); function generateDropoutMask(args) { const { ones, rate, training = false, count = 1, dropoutFunc } = args; const droppedInputs = () => dropoutFunc != null ? dropoutFunc(ones(), rate) : dropout$1(ones(), rate); const createMask = () => inTrainPhase(droppedInputs, ones, training); // just in case count is provided with null or undefined if (!count || count <= 1) { return tfc.keep(createMask().clone()); } const masks = Array(count).fill(undefined).map(createMask); return masks.map(m => tfc.keep(m.clone())); } /** * @license * Copyright 2020 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ var __rest = (undefined && undefined.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; /** * Base class for convolutional-recurrent layers. */ class ConvRNN2D extends RNN { constructor(args) { if (args.unroll) { throw new NotImplementedError('Unrolling is not possible with convolutional RNNs.'); } if (Array.isArray(args.cell)) { throw new NotImplementedError('It is not possible at the moment to stack convolutional cells.'); } super(args); this.inputSpec = [new InputSpec({ ndim: 5 })]; } call(inputs, kwargs) { return tfc.tidy(() => { if (this.cell.dropoutMask != null) { tfc.dispose(this.cell.dropoutMask); this.cell.dropoutMask = null; } if (this.cell.recurrentDropoutMask != null) { tfc.dispose(this.cell.recurrentDropoutMask); this.cell.recurrentDropoutMask = null; } if (kwargs && kwargs['constants']) { throw new ValueError('ConvRNN2D cell does not support constants'); } const mask = kwargs == null ? null : kwargs['mask']; const training = kwargs == null ? null : kwargs['training']; const initialState = kwargs == null ? null : kwargs['initialState']; return super.call(inputs, { mask, training, initialState }); }); } computeOutputShape(inputShape) { let outShape = this.computeSingleOutputShape(inputShape); if (!this.returnSequences) { outShape = [outShape[0], ...outShape.slice(2)]; } if (this.returnState) { outShape = [outShape, ...Array(2).fill([inputShape[0], ...outShape.slice(-3)])]; } return outShape; } getInitialState(inputs) { return tfc.tidy(() => { const { stateSize } = this.cell; const inputShape = inputs.shape; const outputShape = this.computeSingleOutputShape(inputShape); const stateShape = [outputShape[0], ...outputShape.slice(2)]; const initialState = tfc.zeros(stateShape); if (Array.isArray(stateSize)) { return Array(stateSize.length).fill(initialState); } return [initialState]; }); } resetStates(states, training = false) { tfc.tidy(() => { if (!this.stateful) { throw new AttributeError('Cannot call resetStates() on an RNN Layer that is not stateful.'); } const inputShape = this.inputSpec[0].shape; const outputShape = this.computeSingleOutputShape(inputShape); const stateShape = [outputShape[0], ...outputShape.slice(2)]; const batchSize = inputShape[0]; if (batchSize == null) { throw new ValueError('If an RNN is stateful, it needs to know its batch size. Specify ' + 'the batch size of your input tensors: \n' + '- If using a Sequential model, specify the batch size by ' + 'passing a `batchInputShape` option to your first layer.\n' + '- If using the functional API, specify the batch size by ' + 'passing a `batchShape` option to your Input layer.'); } // Initialize state if null. if (this.getStates() == null) { if (Array.isArray(this.cell.stateSize)) { this.states_ = this.cell.stateSize.map(() => tfc.zeros(stateShape)); } else { this.states_ = [tfc.zeros(stateShape)]; } } else if (states == null) { // Dispose old state tensors. tfc.dispose(this.states_); // For stateful RNNs, fully dispose kept old states. if (this.keptStates != null) { tfc.dispose(this.keptStates); this.keptStates = []; } if (Array.isArray(this.cell.stateSize)) { this.states_ = this.cell.stateSize.map(() => tfc.zeros(stateShape)); } else { this.states_[0] = tfc.zeros(stateShape); } } else { if (!Array.isArray(states)) { states = [states]; } if (states.length !== this.states_.length) { throw new ValueError(`Layer ${this.name} expects ${this.states_.length} state(s), ` + `but it received ${states.length} state value(s). Input ` + `received: ${states}`); } if (training) { // Store old state tensors for complete disposal later, i.e., during // the next no-arg call to this method. We do not dispose the old // states immediately because that BPTT (among other things) require // them. this.keptStates.push(this.states_.slice()); } else { tfc.dispose(this.states_); } for (let index = 0; index < this.states_.length; ++index) { const value = states[index]; const expectedShape = stateShape; if (!util.arraysEqual(value.shape, expectedShape)) { throw new ValueError(`State ${index} is incompatible with layer ${this.name}: ` + `expected shape=${expectedShape}, received shape=${value.shape}`); } this.states_[index] = value; } } this.states_ = this.states_.map(state => tfc.keep(state.clone())); }); } computeSingleOutputShape(inputShape) { const { dataFormat, filters, kernelSize, padding, strides, dilationRate } = this.cell; const isChannelsFirst = dataFormat === 'channelsFirst'; const h = inputShape[isChannelsFirst ? 3 : 2]; const w = inputShape[isChannelsFirst ? 4 : 3]; const hOut = convOutputLength(h, kernelSize[0], padding, strides[0], dilationRate[0]); const wOut = convOutputLength(w, kernelSize[1], padding, strides[1], dilationRate[1]); const outShape = [ ...inputShape.slice(0, 2), ...(isChannelsFirst ? [filters, hOut, wOut] : [hOut, wOut, filters]) ]; return outShape; } } /** @nocollapse */ ConvRNN2D.className = 'ConvRNN2D'; class ConvLSTM2DCell extends LSTMCell { constructor(args) { const { filters, kernelSize, strides, padding, dataFormat, dilationRate, } = args; super(Object.assign(Object.assign({}, args), { units: filters })); this.filters = filters; assertPositiveInteger(this.filters, 'filters'); this.kernelSize = normalizeArray(kernelSize, 2, 'kernelSize'); this.kernelSize.forEach(size => assertPositiveInteger(size, 'kernelSize')); this.strides = normalizeArray(strides || 1, 2, 'strides'); this.strides.forEach(stride => assertPositiveInteger(stride, 'strides')); this.padding = padding || 'valid'; checkPaddingMode(this.padding); this.dataFormat = dataFormat || 'channelsLast'; checkDataFormat(this.dataFormat); this.dilationRate = normalizeArray(dilationRate || 1, 2, 'dilationRate'); this.dilationRate.forEach(rate => assertPositiveInteger(rate, 'dilationRate')); } build(inputShape) { var _a; inputShape = getExactlyOneShape(inputShape); const channelAxis = this.dataFormat === 'channelsFirst' ? 1 : inputShape.length - 1; if (inputShape[channelAxis] == null) { throw new ValueError(`The channel dimension of the input should be defined. ` + `Found ${inputShape[channelAxis]}`); } const inputDim = inputShape[channelAxis]; const numOfKernels = 4; const kernelShape = this.kernelSize.concat([inputDim, this.filters * numOfKernels]); this.kernel = this.addWeight('kernel', kernelShape, null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); const recurrentKernelShape = this.kernelSize.concat([this.filters, this.filters * numOfKernels]); this.recurrentKernel = this.addWeight('recurrent_kernel', recurrentKernelShape, null, this.recurrentInitializer, this.recurrentRegularizer, true, this.recurrentConstraint); if (this.useBias) { let biasInitializer; if (this.unitForgetBias) { const init = this.biasInitializer; const filters = this.filters; biasInitializer = new (_a = class CustomInit extends Initializer { apply(shape, dtype) { const biasI = init.apply([filters]); const biasF = tfc.ones([filters]); const biasCAndO = init.apply([filters * 2]); return concatenate$1([biasI, biasF, biasCAndO]); } }, /** @nocollapse */ _a.className = 'CustomInit', _a)(); } else { biasInitializer = this.biasInitializer; } this.bias = this.addWeight('bias', [this.filters * numOfKernels], null, biasInitializer, this.biasRegularizer, true, this.biasConstraint); } this.built = true; } call(inputs, kwargs) { return tfc.tidy(() => { if (inputs.length !== 3) { throw new ValueError(`ConvLSTM2DCell expects 3 input Tensors (inputs, h, c), got ` + `${inputs.length}.`); } const training = kwargs['training'] || false; const x = inputs[0]; // Current input const hTMinus1 = inputs[1]; // Previous memory state. const cTMinus1 = inputs[2]; // Previous carry state. const numOfKernels = 4; if (0 < this.dropout && this.dropout < 1 && this.dropoutMask == null) { this.dropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(x), rate: this.dropout, training, count: numOfKernels, dropoutFunc: this.dropoutFunc }); } const dropoutMask = this.dropoutMask; const applyDropout = (x, mask, index) => { if (!mask || !mask[index]) { return x; } return tfc.mul(mask[index], x); }; let xI = applyDropout(x, dropoutMask, 0); let xF = applyDropout(x, dropoutMask, 1); let xC = applyDropout(x, dropoutMask, 2); let xO = applyDropout(x, dropoutMask, 3); if (0 < this.recurrentDropout && this.recurrentDropout < 1 && this.recurrentDropoutMask == null) { this.recurrentDropoutMask = generateDropoutMask({ ones: () => tfc.onesLike(hTMinus1), rate: this.recurrentDropout, training, count: numOfKernels, dropoutFunc: this.dropoutFunc }); } const recDropoutMask = this.recurrentDropoutMask; let hI = applyDropout(hTMinus1, recDropoutMask, 0); let hF = applyDropout(hTMinus1, recDropoutMask, 1); let hC = applyDropout(hTMinus1, recDropoutMask, 2); let hO = applyDropout(hTMinus1, recDropoutMask, 3); const kernelChannelAxis = 3; const [kernelI, kernelF, kernelC, kernelO] = tfc.split(this.kernel.read(), numOfKernels, kernelChannelAxis); const [biasI, biasF, biasC, biasO] = this.useBias ? tfc.split(this.bias.read(), numOfKernels) : [null, null, null, null]; xI = this.inputConv(xI, kernelI, biasI, this.padding); xF = this.inputConv(xF, kernelF, biasF, this.padding); xC = this.inputConv(xC, kernelC, biasC, this.padding); xO = this.inputConv(xO, kernelO, biasO, this.padding); const [recKernelI, recKernelF, recKernelC, recKernelO] = tfc.split(this.recurrentKernel.read(), numOfKernels, kernelChannelAxis); hI = this.recurrentConv(hI, recKernelI); hF = this.recurrentConv(hF, recKernelF); hC = this.recurrentConv(hC, recKernelC); hO = this.recurrentConv(hO, recKernelO); const i = this.recurrentActivation.apply(tfc.add(xI, hI)); const f = this.recurrentActivation.apply(tfc.add(xF, hF)); const c = tfc.add(tfc.mul(f, cTMinus1), tfc.mul(i, this.activation.apply(tfc.add(xC, hC)))); const h = tfc.mul(this.recurrentActivation.apply(tfc.add(xO, hO)), this.activation.apply(c)); return [h, h, c]; }); } getConfig() { const _a = super.getConfig(), baseConfig = __rest(_a, ['units']); const config = { filters: this.filters, kernelSize: this.kernelSize, padding: this.padding, dataFormat: this.dataFormat, dilationRate: this.dilationRate, strides: this.strides, }; return Object.assign(Object.assign({}, baseConfig), config); } inputConv(x, w, b, padding) { const out = tfc.conv2d(x, w, this.strides, (padding || 'valid'), this.dataFormat === 'channelsFirst' ? 'NCHW' : 'NHWC', this.dilationRate); if (b) { return biasAdd(out, b, this.dataFormat); } return out; } recurrentConv(x, w) { const strides = 1; return tfc.conv2d(x, w, strides, 'same', this.dataFormat === 'channelsFirst' ? 'NCHW' : 'NHWC'); } } /** @nocollapse */ ConvLSTM2DCell.className = 'ConvLSTM2DCell'; tfc.serialization.registerClass(ConvLSTM2DCell); class ConvLSTM2D extends ConvRNN2D { constructor(args) { const cell = new ConvLSTM2DCell(args); super(Object.assign(Object.assign({}, args), { cell })); } /** @nocollapse */ static fromConfig(cls, config) { return new cls(config); } } /** @nocollapse */ ConvLSTM2D.className = 'ConvLSTM2D'; tfc.serialization.registerClass(ConvLSTM2D); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class Dropout extends Layer { constructor(args) { super(args); this.rate = Math.max(Math.min(args.rate, 1), 0); // So that the scalar doesn't get tidied up between executions. this.noiseShape = args.noiseShape; this.seed = args.seed; this.supportsMasking = true; } getNoiseShape(input) { if (this.noiseShape == null) { return this.noiseShape; } const inputShape = input.shape; const noiseShape = []; for (let i = 0; i < this.noiseShape.length; ++i) { noiseShape.push(this.noiseShape[i] == null ? inputShape[i] : this.noiseShape[i]); } return noiseShape; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); if (0 < this.rate && this.rate < 1) { const training = kwargs['training'] == null ? false : kwargs['training']; const noiseShape = this.getNoiseShape(input); const output = inTrainPhase(() => dropout$1(input, this.rate, noiseShape, this.seed), () => input, training); return output; } return inputs; }); } getConfig() { const config = { rate: this.rate, noiseShape: this.noiseShape, seed: this.seed, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } dispose() { return super.dispose(); } } /** @nocollapse */ Dropout.className = 'Dropout'; serialization.registerClass(Dropout); class SpatialDropout1D extends Dropout { constructor(args) { super(args); this.inputSpec = [{ ndim: 3 }]; } getNoiseShape(input) { const inputShape = input.shape; return [inputShape[0], 1, inputShape[2]]; } } /** @nocollapse */ SpatialDropout1D.className = 'SpatialDropout1D'; serialization.registerClass(SpatialDropout1D); class Dense extends Layer { constructor(args) { super(args); // Default activation: Linear (none). this.activation = null; this.useBias = true; this.kernel = null; this.bias = null; this.DEFAULT_KERNEL_INITIALIZER = 'glorotNormal'; this.DEFAULT_BIAS_INITIALIZER = 'zeros'; if (args.batchInputShape == null && args.inputShape == null && args.inputDim != null) { // This logic is copied from Layer's constructor, since we can't // do exactly what the Python constructor does for Dense(). let batchSize = null; if (args.batchSize != null) { batchSize = args.batchSize; } this.batchInputShape = [batchSize, args.inputDim]; } this.units = args.units; assertPositiveInteger(this.units, 'units'); this.activation = getActivation(args.activation); if (args.useBias != null) { this.useBias = args.useBias; } this.kernelInitializer = getInitializer(args.kernelInitializer || this.DEFAULT_KERNEL_INITIALIZER); this.biasInitializer = getInitializer(args.biasInitializer || this.DEFAULT_BIAS_INITIALIZER); this.kernelConstraint = getConstraint(args.kernelConstraint); this.biasConstraint = getConstraint(args.biasConstraint); this.kernelRegularizer = getRegularizer(args.kernelRegularizer); this.biasRegularizer = getRegularizer(args.biasRegularizer); this.activityRegularizer = getRegularizer(args.activityRegularizer); this.supportsMasking = true; this.inputSpec = [{ minNDim: 2 }]; } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const inputLastDim = inputShape[inputShape.length - 1]; if (this.kernel == null) { this.kernel = this.addWeight('kernel', [inputLastDim, this.units], null, this.kernelInitializer, this.kernelRegularizer, true, this.kernelConstraint); if (this.useBias) { this.bias = this.addWeight('bias', [this.units], null, this.biasInitializer, this.biasRegularizer, true, this.biasConstraint); } } this.inputSpec = [{ minNDim: 2, axes: { [-1]: inputLastDim } }]; this.built = true; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const outputShape = inputShape.slice(); outputShape[outputShape.length - 1] = this.units; return outputShape; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); // Dense layer accepts only a single input. const input = getExactlyOneTensor(inputs); const fusedActivationName = mapActivationToFusedKernel(this.activation.getClassName()); let output; if (fusedActivationName != null) { output = dot$1(input, this.kernel.read(), fusedActivationName, this.bias ? this.bias.read() : null); } else { output = dot$1(input, this.kernel.read()); if (this.bias != null) { output = biasAdd(output, this.bias.read()); } if (this.activation != null) { output = this.activation.apply(output); } } return output; }); } getConfig() { const config = { units: this.units, activation: serializeActivation(this.activation), useBias: this.useBias, kernelInitializer: serializeInitializer(this.kernelInitializer), biasInitializer: serializeInitializer(this.biasInitializer), kernelRegularizer: serializeRegularizer(this.kernelRegularizer), biasRegularizer: serializeRegularizer(this.biasRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), kernelConstraint: serializeConstraint(this.kernelConstraint), biasConstraint: serializeConstraint(this.biasConstraint) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Dense.className = 'Dense'; serialization.registerClass(Dense); class Flatten extends Layer { constructor(args) { args = args || {}; super(args); this.inputSpec = [{ minNDim: 3 }]; this.dataFormat = args.dataFormat; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); for (const dim of inputShape.slice(1)) { if (dim == null) { throw new ValueError(`The shape of the input to "Flatten" is not fully defined ` + `(got ${inputShape.slice(1)}). Make sure to pass a complete ` + `"input_shape" or "batch_input_shape" argument to the first ` + `layer in your model.`); } } return [inputShape[0], arrayProd(inputShape, 1)]; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); let input = getExactlyOneTensor(inputs); if (this.dataFormat === 'channelsFirst' && input.rank > 1) { const permutation = [0]; for (let i = 2; i < input.rank; ++i) { permutation.push(i); } permutation.push(1); input = transpose$1(input, permutation); } return batchFlatten(input); }); } getConfig() { const config = {}; if (this.dataFormat != null) { config['dataFormat'] = this.dataFormat; } const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Flatten.className = 'Flatten'; serialization.registerClass(Flatten); class Activation extends Layer { constructor(args) { super(args); this.supportsMasking = true; this.activation = getActivation(args.activation); } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); return this.activation.apply(input); }); } getConfig() { const config = { activation: serializeActivation(this.activation) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Activation.className = 'Activation'; serialization.registerClass(Activation); class RepeatVector extends Layer { constructor(args) { super(args); this.n = args.n; this.inputSpec = [{ ndim: 2 }]; } computeOutputShape(inputShape) { return [inputShape[0], this.n, inputShape[1]]; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); return repeat(inputs, this.n); }); } getConfig() { const config = { n: this.n, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ RepeatVector.className = 'RepeatVector'; serialization.registerClass(RepeatVector); class Reshape extends Layer { constructor(args) { super(args); this.targetShape = args.targetShape; // Make sure that all unknown dimensions are represented as `null`. for (let i = 0; i < this.targetShape.length; ++i) { if (this.isUnknown(this.targetShape[i])) { this.targetShape[i] = null; } } } isUnknown(dim) { return dim < 0 || dim == null; } /** * Finds and replaces a missing dimension in output shape. * * This is a near direct port of the internal Numpy function * `_fix_unknown_dimension` in `numpy/core/src/multiarray/shape.c`. * * @param inputShape: Original shape of array begin reshape. * @param outputShape: Target shape of the array, with at most a single * `null` or negative number, which indicates an underdetermined dimension * that should be derived from `inputShape` and the known dimensions of * `outputShape`. * @returns: The output shape with `null` replaced with its computed value. * @throws: ValueError: If `inputShape` and `outputShape` do not match. */ fixUnknownDimension(inputShape, outputShape) { const errorMsg = 'Total size of new array must be unchanged.'; const finalShape = outputShape.slice(); let known = 1; let unknown = null; for (let i = 0; i < finalShape.length; ++i) { const dim = finalShape[i]; if (this.isUnknown(dim)) { if (unknown === null) { unknown = i; } else { throw new ValueError('Can only specifiy one unknown dimension.'); } } else { known *= dim; } } const originalSize = arrayProd(inputShape); if (unknown !== null) { if (known === 0 || originalSize % known !== 0) { throw new ValueError(errorMsg); } finalShape[unknown] = originalSize / known; } else if (originalSize !== known) { throw new ValueError(errorMsg); } return finalShape; } computeOutputShape(inputShape) { let anyUnknownDims = false; for (let i = 0; i < inputShape.length; ++i) { if (this.isUnknown(inputShape[i])) { anyUnknownDims = true; break; } } if (anyUnknownDims) { return inputShape.slice(0, 1).concat(this.targetShape); } else { return inputShape.slice(0, 1).concat(this.fixUnknownDimension(inputShape.slice(1), this.targetShape)); } } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); const inputShape = input.shape; const outputShape = inputShape.slice(0, 1).concat(this.fixUnknownDimension(inputShape.slice(1), this.targetShape)); return reshape$2(input, outputShape); }); } getConfig() { const config = { targetShape: this.targetShape, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Reshape.className = 'Reshape'; serialization.registerClass(Reshape); class Permute extends Layer { constructor(args) { super(args); if (args.dims == null) { throw new Error('Required configuration field `dims` is missing during Permute ' + 'constructor call.'); } if (!Array.isArray(args.dims)) { throw new Error('Permute constructor requires `dims` to be an Array, but received ' + `${args.dims} instead.`); } // Check the validity of the permutation indices. const expectedSortedIndices = range(1, args.dims.length + 1); if (!util.arraysEqual(args.dims.slice().sort(), expectedSortedIndices)) { throw new Error('Invalid permutation `dims`: ' + JSON.stringify(args.dims) + ' `dims` must contain consecutive integers starting from 1.'); } this.dims = args.dims; this.dimsIncludingBatch = [0].concat(this.dims); this.inputSpec = [new InputSpec({ ndim: this.dims.length + 1 })]; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const outputShape = inputShape.slice(); this.dims.forEach((dim, i) => { outputShape[i + 1] = inputShape[dim]; }); return outputShape; } call(inputs, kwargs) { return transpose$1(getExactlyOneTensor(inputs), this.dimsIncludingBatch); } getConfig() { const config = { dims: this.dims, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Permute.className = 'Permute'; serialization.registerClass(Permute); class Masking extends Layer { constructor(args) { super(args == null ? {} : args); this.supportsMasking = true; if (args != null) { this.maskValue = args.maskValue == null ? 0 : args.maskValue; } else { this.maskValue = 0; } } computeOutputShape(inputShape) { return inputShape; } getConfig() { const baseConfig = super.getConfig(); const config = { maskValue: this.maskValue }; Object.assign(config, baseConfig); return config; } computeMask(inputs, mask) { const input = getExactlyOneTensor(inputs); const axis = -1; return any(notEqual(input, this.maskValue), axis); } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); const axis = -1; const keepDims = true; const booleanMask = any(notEqual(input, this.maskValue), axis, keepDims); const output = mul$1(input, cast$2(booleanMask, input.dtype)); return output; }); } } /** @nocollapse */ Masking.className = 'Masking'; serialization.registerClass(Masking); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class Embedding extends Layer { constructor(args) { super(args); this.embeddings = null; this.DEFAULT_EMBEDDINGS_INITIALIZER = 'randomUniform'; if (args.batchInputShape == null && args.inputShape == null) { // Porting Note: This logic is copied from Layer's constructor, since we // can't do exactly what the Python constructor does for Embedding(). // Specifically, the super constructor can not be called after the // mutation of the `config` argument. let batchSize = null; if (args.batchSize != null) { batchSize = args.batchSize; } if (args.inputLength == null) { // Fix super-constructor to what it would have done if // 'config.inputShape' were (None, ) this.batchInputShape = [batchSize, null]; } else { // Fix super-constructor to what it would have done if // 'config.inputShape' were (config.inputLength, ) this.batchInputShape = [batchSize].concat(toList(args.inputLength)); } } this.inputDim = args.inputDim; assertPositiveInteger(this.inputDim, 'inputDim'); this.outputDim = args.outputDim; assertPositiveInteger(this.outputDim, 'outputDim'); this.embeddingsInitializer = getInitializer(args.embeddingsInitializer || this.DEFAULT_EMBEDDINGS_INITIALIZER); this.embeddingsRegularizer = getRegularizer(args.embeddingsRegularizer); this.activityRegularizer = getRegularizer(args.activityRegularizer); this.embeddingsConstraint = getConstraint(args.embeddingsConstraint); this.maskZero = args.maskZero; this.supportsMasking = args.maskZero; this.inputLength = args.inputLength; } build(inputShape) { this.embeddings = this.addWeight('embeddings', [this.inputDim, this.outputDim], this.dtype, this.embeddingsInitializer, this.embeddingsRegularizer, true, this.embeddingsConstraint); this.built = true; } // Override warnOnIncompatibleInputShape because an embedding layer allows // the input to have varying ranks. warnOnIncompatibleInputShape(inputShape) { } computeMask(inputs, mask) { return tidy$1(() => { if (!this.maskZero) { return null; } else { inputs = getExactlyOneTensor(inputs); return notEqual(inputs, zerosLike$1(inputs)); } }); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); if (this.inputLength == null) { return [...inputShape, this.outputDim]; } // inputLength can be an array if input is 3D or higher. const inLens = toList(this.inputLength); if (inLens.length !== inputShape.length - 1) { throw new ValueError(`"inputLength" is ${this.inputLength}, but received ` + `input shape has shape ${inputShape}`); } else { let i = 0; for (let k = 0; k < inLens.length; ++k) { const s1 = inLens[k]; const s2 = inputShape[k + 1]; if ((s1 != null) && (s2 != null) && (s1 !== s2)) { throw new ValueError(`"inputLength" is ${this.inputLength}, but received ` + `input shape has shape ${inputShape}`); } else if (s1 == null) { inLens[i] = s2; } i++; } } return [inputShape[0], ...inLens, this.outputDim]; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); // Embedding layer accepts only a single input. let input = getExactlyOneTensor(inputs); if (input.dtype !== 'int32') { input = cast$1(input, 'int32'); } const output = gather$1(this.embeddings.read(), reshape$2(input, [input.size])); return reshape$2(output, getExactlyOneShape(this.computeOutputShape(input.shape))); }); } getConfig() { const config = { inputDim: this.inputDim, outputDim: this.outputDim, embeddingsInitializer: serializeInitializer(this.embeddingsInitializer), embeddingsRegularizer: serializeRegularizer(this.embeddingsRegularizer), activityRegularizer: serializeRegularizer(this.activityRegularizer), embeddingsConstraint: serializeConstraint(this.embeddingsConstraint), maskZero: this.maskZero, inputLength: this.inputLength }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Embedding.className = 'Embedding'; serialization.registerClass(Embedding); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Generic Merge layer for element-wise merge functions. * * Used to implement `Sum`, `Average`, `Concatenate`, etc. */ class Merge extends Layer { constructor(args) { super(args || {}); this.supportsMasking = true; } /** * Logic for merging multiple tensors, to be overridden by subclasses. * @param inputs */ mergeFunction(inputs) { throw new NotImplementedError(); } /** * Computes the shape of the result of an elementwise operation. * * @param shape1: Shape of the first tensor. * @param shape2: Shape of the second tensor. * @returns Expected output shape when an elementwise operation is carried * out on 2 tensors with shapes `shape1` and `shape2`. * @throws ValueError: If `shape1` and `shape2` are not compatible for * element-wise operations. */ computeElementwiseOpOutputShape(shape1, shape2) { if (shape1 == null || shape2 == null) { return null; } else if (shape1.length < shape2.length) { return this.computeElementwiseOpOutputShape(shape2, shape1); } else if (shape2.length === 0) { return shape1; } const outputShape = shape1.slice(0, shape1.length - shape2.length); for (let k = 0; k < shape2.length; ++k) { const i = shape1[shape1.length - shape2.length + k]; const j = shape2[k]; if (i == null || j == null || i < 0 || j < 0) { outputShape.push(null); } else if (i === 1) { outputShape.push(j); } else if (j === 1) { outputShape.push(i); } else { if (i !== j) { throw new ValueError('Operands could not be broadcast together with shapes ' + JSON.stringify(shape1) + ' ' + JSON.stringify(shape2)); } outputShape.push(i); } } return outputShape; } build(inputShape) { // Used purely for shape validation. if (Array.isArray(inputShape) && !Array.isArray(inputShape[0])) { // Make sure that inputShape is an Array of shape. inputShape = [getExactlyOneShape(inputShape)]; } inputShape = inputShape; if (inputShape.length < 2) { throw new ValueError('A merge layer should be called on an Array of at least 2 inputs.' + ` Got ${inputShape.length} input(s).`); } // Make sure that there is at most one unique batch size among the input // shapes. let batchSizes = []; for (const shape of inputShape) { if (shape != null && shape[0] !== null) { batchSizes.push(shape[0]); } } batchSizes = unique(batchSizes); if (batchSizes.length > 1) { throw new ValueError(`Can not merge tensors with different batch sizes. ` + `Got tensors with shapes: ${JSON.stringify(inputShape)}.`); } let outputShape = inputShape[0] == null ? null : inputShape[0].slice(1); for (let i = 1; i < inputShape.length; ++i) { const shape = inputShape[i] == null ? null : inputShape[i].slice(1); outputShape = this.computeElementwiseOpOutputShape(outputShape, shape); } // If the inputs have different ranks, we have to reshape them to make them // broadcastable. const allRanks = inputShape.map(shape => shape.length); if (inputShape.indexOf(null) === -1 && unique(allRanks).length === 1) { this.reshapeRequired = false; } else { this.reshapeRequired = true; } } call(inputs, kwargs) { return tidy$1(() => { inputs = inputs; if (this.reshapeRequired) { const reshapedInputs = []; const inputDims = inputs.map(input => input.rank); if (inputDims.indexOf(null) === -1) { // If ranks of all inputs are available, we simply expand each of them // at axis=1 until all of them have the same rank. const maxNDim = max(inputDims); for (let x of inputs) { const xNDim = x.rank; for (let k = 0; k < maxNDim - xNDim; ++k) { x = expandDims$1(x, 1); } reshapedInputs.push(x); } return this.mergeFunction(reshapedInputs); } else { // Transpose all inputs so that batch size is the last dimension. // [batchSize, dim1, dim2, ...] -> [dim1, dim2, ..., batchSize] let transposed = false; for (const x of inputs) { const xNDim = x.rank; if (xNDim == null) { const xShape = x.shape; const batchSize = xShape[0]; const newShape = xShape.slice(1).concat([batchSize]); let xTransposed = tfc.reshape(x, [batchSize].concat(arrayProd(xShape.slice(1)))); xTransposed = tfc.transpose(xTransposed, [1, 0]); xTransposed = tfc.reshape(xTransposed, newShape); reshapedInputs.push(xTransposed); transposed = true; } else if (xNDim > 1) { const dims = range(1, xNDim).concat([0]); reshapedInputs.push(tfc.transpose(x, dims)); transposed = true; } else { // We don't transpose inputs if they are 1D vectors or scalars. reshapedInputs.push(x); } } let y = this.mergeFunction(reshapedInputs); const yNDim = y.rank; if (transposed) { // If inputs have been transposed, we have to transpose the output // too. if (yNDim == null) { const yShape = y.shape; const yNDim = yShape.length; const batchSize = yShape[yNDim - 1]; const newShape = [batchSize].concat(yShape.slice(0, yShape.length - 1)); y = tfc.reshape(tfc.transpose(tfc.reshape(y, [-1, batchSize]), [1, 0]), newShape); } else if (yNDim > 1) { const dims = [yNDim - 1].concat(range(0, yNDim - 1)); y = tfc.transpose(y, dims); } } return y; } } else { return this.mergeFunction(inputs); } }); } computeOutputShape(inputShape) { inputShape = inputShape; let outputShape; if (inputShape[0] == null) { outputShape = null; } else { outputShape = inputShape[0].slice(1); } for (let i = 1; i < inputShape.length; ++i) { const shape = inputShape[i] == null ? null : inputShape[i].slice(1); outputShape = this.computeElementwiseOpOutputShape(outputShape, shape); } let batchSizes = []; for (const shape of inputShape) { if (shape != null && shape[0] !== null) { batchSizes.push(shape[0]); } } batchSizes = unique(batchSizes); if (batchSizes.length === 1) { outputShape = batchSizes.concat(outputShape); } else { outputShape = [null].concat(outputShape); } return outputShape; } computeMask(inputs, mask) { return tfc.tidy(() => { if (mask == null) { return null; } if (!Array.isArray(mask)) { throw new ValueError('`mask` should be an Array'); } if (!Array.isArray(inputs)) { throw new ValueError('`inputs` should be an Array'); } if (mask.length !== inputs.length) { throw new ValueError(`The Array 'inputs' and 'mask' are expected to have the same ` + `length, but have different lengths ` + `(${inputs.length} vs ${mask.length})`); } if (mask.every(m => m == null)) { return null; } mask = mask.map(m => m == null ? m : tfc.expandDims(m, 0)); let output = mask[0]; for (let i = 1; i < mask.length - 1; ++i) { output = tfc.logicalAnd(output, mask[i]); } return output; }); } } class Add extends Merge { constructor(args) { super(args); } mergeFunction(inputs) { return tidy$1(() => { let output = inputs[0].clone(); for (let i = 1; i < inputs.length; ++i) { output = tfc.add(output, inputs[i]); } return output; }); } } /** @nocollapse */ Add.className = 'Add'; serialization.registerClass(Add); class Multiply extends Merge { constructor(args) { super(args); } mergeFunction(inputs) { return tidy$1(() => { let output = inputs[0].clone(); for (let i = 1; i < inputs.length; ++i) { output = tfc.mul(output, inputs[i]); } return output; }); } } /** @nocollapse */ Multiply.className = 'Multiply'; serialization.registerClass(Multiply); class Average extends Merge { constructor(args) { super(args); } mergeFunction(inputs) { return tidy$1(() => { let output = inputs[0].clone(); for (let i = 1; i < inputs.length; ++i) { output = tfc.add(output, inputs[i]); } return tfc.mul(1 / inputs.length, output); }); } } /** @nocollapse */ Average.className = 'Average'; serialization.registerClass(Average); class Maximum extends Merge { constructor(args) { super(args); } mergeFunction(inputs) { return tidy$1(() => { let output = inputs[0]; for (let i = 1; i < inputs.length; ++i) { output = tfc.maximum(output, inputs[i]); } return output; }); } } /** @nocollapse */ Maximum.className = 'Maximum'; serialization.registerClass(Maximum); class Minimum extends Merge { constructor(args) { super(args); } mergeFunction(inputs) { return tidy$1(() => { let output = inputs[0]; for (let i = 1; i < inputs.length; ++i) { output = tfc.minimum(output, inputs[i]); } return output; }); } } /** @nocollapse */ Minimum.className = 'Minimum'; serialization.registerClass(Minimum); class Concatenate extends Merge { constructor(args) { super(args); this.DEFAULT_AXIS = -1; if (args == null) { args = {}; } this.axis = args.axis == null ? this.DEFAULT_AXIS : args.axis; this.supportsMasking = true; this.reshapeRequired = false; } build(inputShape) { // Used purely for shape validation.] if (!(Array.isArray(inputShape) && Array.isArray(inputShape[0])) || inputShape.length === 1) { throw new ValueError('A `Concatenate` layer should be called on a list of at least 2 ' + 'inputs'); } inputShape = inputShape; let allNoneShape = true; for (const shape of inputShape) { if (shape != null) { allNoneShape = false; break; } } if (allNoneShape) { return; } const shapeSet = []; for (let i = 0; i < inputShape.length; ++i) { const shapeWithoutConcatAxis = inputShape[i].slice(); shapeWithoutConcatAxis.splice(this.axis, 1); let exists = false; for (const shape of shapeSet) { if (util.arraysEqual(shape, shapeWithoutConcatAxis)) { exists = true; break; } } if (!exists) { shapeSet.push(shapeWithoutConcatAxis); } } if (shapeSet.length > 1) { throw new ValueError('A `Concatenate` layer requires inputs with matching shapes ' + 'except for the concat axis. Got input shapes: ' + JSON.stringify(inputShape)); } } mergeFunction(inputs) { return tidy$1(() => { return concatenate$1(inputs, this.axis); }); } computeOutputShape(inputShape) { if (!(Array.isArray(inputShape) && Array.isArray(inputShape[0]))) { throw new ValueError('A `Concatenate` layer should be called on a list of inputs.'); } const inputShapes = inputShape; const outputShape = inputShapes[0].slice(); const axis = this.axis < 0 ? outputShape.length + this.axis : this.axis; // Porting Note: the line above is because TypeScript doesn't support // negative indices. for (const shape of inputShapes.slice(1)) { if (outputShape[axis] == null || shape[axis] == null) { outputShape[axis] = null; break; } outputShape[axis] += shape[axis]; } return outputShape; } computeMask(inputs, mask) { if (mask == null) { return null; } if (!Array.isArray(mask)) { throw new ValueError('`mask` should be an array for Concatenate'); } if (!Array.isArray(inputs)) { throw new ValueError('`inputs` should be an array for Concatenate'); } if (mask.length !== inputs.length) { throw new ValueError(`Mismatch in the length of mask (${mask.length}) ` + `and the legnth of inputs (${inputs.length})`); } return tfc.tidy(() => { let allNullMasks = true; mask.forEach(m => { if (m != null) { allNullMasks = false; return; } }); if (allNullMasks) { return null; } const outputMasks = []; for (let i = 0; i < inputs.length; ++i) { if (mask[i] == null) { // Input is unmasked. Append all 1's to masks. outputMasks.push(tfc.cast(tfc.onesLike(inputs[i]), 'bool')); } else if (mask[i].rank < inputs[i].rank) { // Mask is smaller than the input, expand it. outputMasks.push(tfc.expandDims(mask[i], -1)); } else { outputMasks.push(mask[i]); } } const concatenatedMasks = tfc.concat(outputMasks, this.axis); return tfc.all(concatenatedMasks, -1, false); }); } getConfig() { const config = { 'axis': this.axis, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Concatenate.className = 'Concatenate'; serialization.registerClass(Concatenate); /** * Interpretable potentially negative axis index. * * For example, given axis = -1, and dim = 3, this function will return 2. * * @param axis The axis index, may be a positive, zero or negative integer. * @param dim Total number of dimensions, a positive integer. * @returns A non-negative axis index equivalent to the input `axis`. */ function interpretAxis(axis, dim) { while (axis < 0) { axis += dim; } return axis; } function batchDot(x, y, axes) { if (x.shape.length > 3 || y.shape.length > 3) { throw new NotImplementedError('batchDot is not implemented for tensors of 4D or higher rank yet'); } tfc.util.assert(x.shape.length >= 2, () => `batchDot requires the rank of x to be >= 2, ` + `but got ${x.shape.length}`); tfc.util.assert(x.shape.length >= 2, () => `batchDot requires the rank of y to be >= 2, ` + `but got ${y.shape.length}`); if (typeof axes === 'number') { axes = [axes, axes]; } if (x.dtype === 'complex64' || y.dtype === 'complex64') { throw new NotImplementedError('batchDot is not implemented for complex64-type Tensors yet.'); } const xNDim = x.shape.length; const yNDim = y.shape.length; if (axes == null) { // Behave like batchMatmul by default. axes = [xNDim - 1, yNDim - 2]; } const axesArray = axes; return tfc.tidy(() => { let diff; if (xNDim > yNDim) { diff = xNDim - yNDim; const diffShape = []; for (let i = 0; i < diff; ++i) { diffShape.push(1); } y = tfc.reshape(y, y.shape.concat(diffShape)); } else if (yNDim > xNDim) { diff = yNDim - xNDim; const diffShape = []; for (let i = 0; i < diff; ++i) { diffShape.push(1); } x = tfc.reshape(x, x.shape.concat(diffShape)); } else { diff = 0; } let out; if (x.shape.length === 2 && y.shape.length === 2) { if (axesArray[0] === axesArray[1]) { out = tfc.sum(tfc.mul(x, y), axesArray[0]); } else { out = tfc.sum(tfc.mul(tfc.transpose(x, [1, 0]), y), axesArray[1]); } } else { const adjX = axesArray[0] !== x.shape.length - 1; const adjY = axesArray[1] === y.shape.length - 1; out = tfc.matMul(x, y, adjX, adjY); } if (diff > 0) { let idx; if (xNDim > yNDim) { idx = xNDim + yNDim - 3; } else { idx = xNDim - 1; } const squeezeAxes = []; for (let i = idx; i < idx + diff; ++i) { squeezeAxes.push(i); } out = tfc.squeeze(out, squeezeAxes); } if (out.shape.length === 1) { out = tfc.expandDims(out, 1); } return out; }); } class Dot extends Merge { constructor(args) { super(args); this.axes = args.axes; this.normalize = args.normalize == null ? false : args.normalize; this.supportsMasking = true; this.reshapeRequired = false; } build(inputShape) { tfc.util.assert(Array.isArray(inputShape) && inputShape.length === 2 && Array.isArray(inputShape[0]) && Array.isArray(inputShape[1]), () => 'A `Dot` layer should be called on a list of exactly 2 inputs.'); const shape1 = inputShape[0]; const shape2 = inputShape[1]; if (shape1.length > 3 || shape2.length > 3) { throw new NotImplementedError('Dot layer does not support tensors of 4D or higher rank yet.'); } const axes = this.interpretAxes(shape1, shape2); if (shape1[axes[0]] !== shape2[axes[1]]) { throw new ValueError(`Dimension incompatibility: ` + `${shape1[axes[0]]} !== ${shape2[axes[1]]}`); } } mergeFunction(inputs) { if (inputs.length !== 2) { throw new ValueError('A `Dot` layer must be called on exactly 2 inputs, ' + `but received ${inputs.length} input(s).`); } let x1 = inputs[0]; let x2 = inputs[1]; let axes; if (!Array.isArray(this.axes)) { axes = [ interpretAxis(this.axes, x1.shape.length), interpretAxis(this.axes, x2.shape.length) ]; } else { axes = this.axes.map((axis, i) => interpretAxis(axis, inputs[i].shape.length)); } if (this.normalize) { x1 = l2Normalize(x1, axes[0]); x2 = l2Normalize(x2, axes[1]); } return batchDot(x1, x2, axes); } interpretAxes(shape1, shape2) { let axes; if (!Array.isArray(this.axes)) { // `this.axes` is a single integer. axes = [ interpretAxis(this.axes, shape1.length), interpretAxis(this.axes, shape2.length) ]; } else { // `this.axes` is an Array of integers. axes = this.axes; } return axes; } computeOutputShape(inputShape) { tfc.util.assert(Array.isArray(inputShape) && inputShape.length === 2 && Array.isArray(inputShape[0]) && Array.isArray(inputShape[1]), () => 'A `Dot` layer should be called on a list of exactly 2 inputs.'); const shape1 = inputShape[0].slice(); const shape2 = inputShape[1].slice(); if (shape1.length > 3 || shape2.length > 3) { throw new NotImplementedError('Dot layer does not support tensors of 4D or higher rank yet.'); } const axes = this.interpretAxes(shape1, shape2); shape1.splice(axes[0], 1); shape2.splice(axes[1], 1); shape2.splice(0, 1); const outputShape = shape1.concat(shape2); if (outputShape.length === 1) { outputShape.push(1); } return outputShape; } computeMask(inputs, mask) { return null; } getConfig() { const config = { 'axes': this.axes, 'normalize': this.normalize }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ Dot.className = 'Dot'; serialization.registerClass(Dot); // TODO(cais): Add functional interfaces for the merge layers. /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class GaussianNoise extends Layer { constructor(args) { super(args); this.supportsMasking = true; this.stddev = args.stddev; } computeOutputShape(inputShape) { return inputShape; } getConfig() { const baseConfig = super.getConfig(); const config = { stddev: this.stddev }; Object.assign(config, baseConfig); return config; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); const noised = () => add$3(randomNormal$1(input.shape, 0, this.stddev), input); const output = inTrainPhase(noised, () => input, kwargs['training'] || false); return output; }); } } /** @nocollapse */ GaussianNoise.className = 'GaussianNoise'; serialization.registerClass(GaussianNoise); class GaussianDropout extends Layer { constructor(args) { super(args); this.supportsMasking = true; this.rate = args.rate; } computeOutputShape(inputShape) { return inputShape; } getConfig() { const baseConfig = super.getConfig(); const config = { rate: this.rate }; Object.assign(config, baseConfig); return config; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); const input = getExactlyOneTensor(inputs); if (this.rate > 0 && this.rate < 1) { const noised = () => { const stddev = Math.sqrt(this.rate / (1 - this.rate)); return mul$1(input, randomNormal$1(input.shape, 1, stddev)); }; return inTrainPhase(noised, () => input, kwargs['training'] || false); } return input; }); } } /** @nocollapse */ GaussianDropout.className = 'GaussianDropout'; serialization.registerClass(GaussianDropout); /** * Applies Alpha Dropout to the input. * * As it is a regularization layer, it is only active at training time. * * Alpha Dropout is a `Dropout` that keeps mean and variance of inputs * to their original values, in order to ensure the self-normalizing property * even after this dropout. * Alpha Dropout fits well to Scaled Exponential Linear Units * by randomly setting activations to the negative saturation value. * * Arguments: * - `rate`: float, drop probability (as with `Dropout`). * The multiplicative noise will have * standard deviation `sqrt(rate / (1 - rate))`. * - `noise_shape`: A 1-D `Tensor` of type `int32`, representing the * shape for randomly generated keep/drop flags. * * Input shape: * Arbitrary. Use the keyword argument `inputShape` * (tuple of integers, does not include the samples axis) * when using this layer as the first layer in a model. * * Output shape: * Same shape as input. * * References: * - [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515) */ class AlphaDropout extends Layer { constructor(args) { super(args); this.supportsMasking = true; this.rate = args.rate; this.noiseShape = args.noiseShape; } _getNoiseShape(inputs) { return this.noiseShape || getExactlyOneTensor(inputs).shape; } computeOutputShape(inputShape) { return inputShape; } getConfig() { const baseConfig = super.getConfig(); const config = { rate: this.rate }; Object.assign(config, baseConfig); return config; } call(inputs, kwargs) { return tidy$1(() => { if (this.rate < 1 && this.rate > 0) { const noiseShape = this._getNoiseShape(inputs); const droppedInputs = () => { const input = getExactlyOneTensor(inputs); const alpha = 1.6732632423543772848170429916717; const scale = 1.0507009873554804934193349852946; const alphaP = -alpha * scale; let keptIdx = greaterEqual$1(randomUniform$1(noiseShape), this.rate); keptIdx = cast$1(keptIdx, 'float32'); // get default dtype. // Get affine transformation params. const a = ((1 - this.rate) * (1 + this.rate * alphaP ** 2)) ** -0.5; const b = -a * alphaP * this.rate; // Apply mask. const x = add$3(mul$1(input, keptIdx), mul$1(add$3(keptIdx, -1), alphaP)); return add$3(mul$1(x, a), b); }; return inTrainPhase(droppedInputs, () => getExactlyOneTensor(inputs), kwargs['training'] || false); } return inputs; }); } } /** @nocollapse */ AlphaDropout.className = 'AlphaDropout'; serialization.registerClass(AlphaDropout); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Applies batch normalization on x given mean, var, beta and gamma. * * I.e. returns: * `output = (x - mean) / (sqrt(var) + epsilon) * gamma + beta` * * @param x Input tensor. * @param mean Mean of batch. * @param variance Variance of batch. * @param beta Tensor with which to center the input. * @param gamma Tensor by which to scale the input. * @param epsilon Fuzz factor. * @returns The result of the batch normalization. */ function batchNormalization$1(x, mean, variance, beta, gamma, epsilon = 1e-3) { let out; if (x.rank === 2) { out = tfc.batchNorm2d(x, mean, variance, beta, gamma, epsilon); } else if (x.rank === 3) { // TODO(cais): Check rank; give proper error message. out = tfc.batchNorm3d(x, mean, variance, beta, gamma, epsilon); } else if (x.rank === 4) { out = tfc.batchNorm4d(x, mean, variance, beta, gamma, epsilon); } else { throw new NotImplementedError(`batchNormalization is not implemented for array of rank ${x.rank} ` + `yet`); } return out; } /** * Non-broadcasting batch normalization for use in training (not inference). * * The input is normalized to zero mean and unit variance along the * `reductionAxes`, followed by scaling with `gamma` and shifted by `beta`. * The result of that is returned as the first element * of the returned `Array`. The other two elements are the mean and variance, * respectively. * * @param x Input tensor to be normalized. * @param gamma Tensor by which to scale the input. * @param beta Tensor by which to center the input. * @param reductionAxes Axes over which to normalize. * @param epsilon Fuzz factor. * @returns An `Array` of three `Tensors`: * [normalized tensor, mean of input, variance of input]. */ function regularNormalizeBatchInTraining(x, gamma, beta, reductionAxes, epsilon = 1e-3) { return tidy$1(() => { const meanAndVariance = tfc.moments(x, reductionAxes); const mean = meanAndVariance.mean; const variance = meanAndVariance.variance; const normed = batchNormalization$1(x, mean, variance, beta, gamma, epsilon); return [normed, mean, variance]; }); } /** * Broadcasting batch normalization for use in training (not inference). * * The input is normalized to zero mean and unit variance along the * `reductionAxes`, followed by scaling with `gamma` and shifted by `beta`. * The result of that is returned as the first element * of the returned `Array`. The other two elements are the mean and variance, * respectively. * * @param x Input tensor to be normalized. * @param gamma Tensor by which to scale the input. * @param beta Tensor by which to center the input. * @param reductionAxes Axes over which to normalize. * @param epsilon Fuzz factor. * @returns An `Array` of three `Tensors`: * [normalized tensor, mean of input, variance of input]. */ function broadcastNormalizeBatchInTraining(x, gamma, beta, reductionAxes, epsilon = 1e-3) { return tidy$1(() => { const meanAndVariance = tfc.moments(x, reductionAxes); const mean = meanAndVariance.mean; const variance = meanAndVariance.variance; const targetShape = []; for (const axis of range(0, x.rank)) { if (reductionAxes.indexOf(axis) !== -1) { targetShape.push(1); } else { targetShape.push(x.shape[axis]); } } const broadcastMean = reshape$2(mean, targetShape); const broadcastVariance = reshape$2(variance, targetShape); const broadcastGamma = gamma == null ? null : reshape$2(gamma, targetShape); const broadcastBeta = beta == null ? null : reshape$2(beta, targetShape); const normed = batchNormalization$1(x, broadcastMean, broadcastVariance, broadcastBeta, broadcastGamma, epsilon); return [normed, mean, variance]; }); } /** * Batch normalization for use in training (not inference). * * @param x Input tensor to be normalized. * @param gamma Tensor by which to scale the input. * @param beta Tensor by which to center the input. * @param reductionAxes Axes over which to normalize. * @param epsilon Fuzz factor. * @returns An `Array` of three `Tensors`: * [normalized tensor, mean of input, variance of input]. */ function normalizeBatchInTraining(x, gamma, beta, reductionAxes, epsilon = 1e-3) { if (util.arraysEqual(reductionAxes.slice().sort(), range(0, x.rank - 1))) { return regularNormalizeBatchInTraining(x, gamma, beta, reductionAxes, epsilon); } else { return broadcastNormalizeBatchInTraining(x, gamma, beta, reductionAxes, epsilon); } } class BatchNormalization extends Layer { constructor(args) { if (args == null) { args = {}; } super(args); this.supportsMasking = true; this.axis = args.axis == null ? -1 : args.axis; this.momentum = args.momentum == null ? 0.99 : args.momentum; this.epsilon = args.epsilon == null ? 1e-3 : args.epsilon; this.center = args.center == null ? true : args.center; this.scale = args.scale == null ? true : args.scale; this.betaInitializer = getInitializer(args.betaInitializer || 'zeros'); this.gammaInitializer = getInitializer(args.gammaInitializer || 'ones'); this.movingMeanInitializer = getInitializer(args.movingMeanInitializer || 'zeros'); this.movingVarianceInitializer = getInitializer(args.movingVarianceInitializer || 'ones'); this.betaConstraint = getConstraint(args.betaConstraint); this.gammaConstraint = getConstraint(args.gammaConstraint); this.betaRegularizer = getRegularizer(args.betaRegularizer); this.gammaRegularizer = getRegularizer(args.gammaRegularizer); } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const axis = this.axis >= 0 ? this.axis : (this.axis + inputShape.length); const dim = inputShape[axis]; if (dim == null) { throw new ValueError(`Axis ${axis} of input tensor should have a defined dimension but ` + `the layer received an input with shape ` + `${JSON.stringify(inputShape)}.`); } this.inputSpec = [new InputSpec({ ndim: inputShape.length, axes: { [axis]: dim } })]; const shape = [dim]; if (this.scale) { this.gamma = this.addWeight('gamma', shape, null, this.gammaInitializer, this.gammaRegularizer, true, this.gammaConstraint); } if (this.center) { this.beta = this.addWeight('beta', shape, null, this.betaInitializer, this.betaRegularizer, true, this.betaConstraint); } this.movingMean = this.addWeight('moving_mean', shape, null, this.movingMeanInitializer, null, false); this.movingVariance = this.addWeight('moving_variance', shape, null, this.movingVarianceInitializer, null, false); this.built = true; } call(inputs, kwargs) { return tidy$1(() => { const training = kwargs['training'] == null ? false : kwargs['training']; const input = getExactlyOneTensor(inputs); const inputShape = input.shape; const ndim = inputShape.length; const reductionAxes = range(0, ndim); const axis = this.axis >= 0 ? this.axis : (this.axis + ndim); reductionAxes.splice(axis, 1); const broadcastShape = pyListRepeat(1, ndim); broadcastShape[axis] = inputShape[axis]; const sortedReductionAxes = reductionAxes.slice(); sortedReductionAxes.sort(); const needsBroadcasting = !util.arraysEqual(sortedReductionAxes, range(0, ndim).slice(0, ndim - 1)); const normalizeInference = () => { if (needsBroadcasting) { const broadcastMovingMean = reshape$2(this.movingMean.read(), broadcastShape); const broadcastMovingVariance = reshape$2(this.movingVariance.read(), broadcastShape); const broadcastBeta = this.center ? reshape$2(this.beta.read(), broadcastShape) : null; const broadcastGamma = this.scale ? reshape$2(this.gamma.read(), broadcastShape) : null; return batchNormalization$1(input, broadcastMovingMean, broadcastMovingVariance, broadcastBeta, broadcastGamma, this.epsilon); } else { return batchNormalization$1(input, this.movingMean.read(), this.movingVariance.read(), this.beta == null ? null : this.beta.read(), this.gamma == null ? null : this.gamma.read(), this.epsilon); } }; if (!training) { return normalizeInference(); } const [normedTraining, mean, variance] = normalizeBatchInTraining(input, this.gamma.read(), this.beta.read(), reductionAxes, this.epsilon); const doMovingAverage = (variable, value, momentum) => { tfc.tidy(() => { const decay = 1 - momentum; const origValue = variable.read(); const updateDelta = tfc.mul(tfc.sub(origValue, value), decay); variable.write(tfc.sub(origValue, updateDelta)); }); }; // Perform updates to moving mean and moving variance for training. // Porting Note: In PyKeras, these updates to `movingMean` and // `movingAverage` are done as a deferred Graph, added to the `Layer`'s // `update`s using the `add_update()` method. Here we do it imperatively // and encapsulate the updates in a function that is invoked // immediately. const updateMovingMeanAndVariance = () => { doMovingAverage(this.movingMean, mean, this.momentum); doMovingAverage(this.movingVariance, variance, this.momentum); }; updateMovingMeanAndVariance(); return normedTraining; }); } getConfig() { const config = { axis: this.axis, momentum: this.momentum, epsilon: this.epsilon, center: this.center, scale: this.scale, betaInitializer: serializeInitializer(this.betaInitializer), gammaInitializer: serializeInitializer(this.gammaInitializer), movingMeanInitializer: serializeInitializer(this.movingMeanInitializer), movingVarianceInitializer: serializeInitializer(this.movingVarianceInitializer), betaRegularizer: serializeRegularizer(this.betaRegularizer), gammaRegularizer: serializeRegularizer(this.gammaRegularizer), betaConstraint: serializeConstraint(this.betaConstraint), gammaConstraint: serializeConstraint(this.gammaConstraint) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ BatchNormalization.className = 'BatchNormalization'; serialization.registerClass(BatchNormalization); class LayerNormalization extends Layer { constructor(args) { if (args == null) { args = {}; } super(args); this.axis = args.axis == null ? -1 : args.axis; if (typeof this.axis === 'number') { if (!Number.isInteger(this.axis)) { throw new Error(`Expected axis to be an integer, but received ${this.axis}`); } } else if (Array.isArray(this.axis)) { for (const axis of this.axis) { if (!Number.isInteger(axis)) { throw new Error(`Expected axis to be an array of integers, ` + `but received ${JSON.stringify(this.axis)}`); } } } else { throw new Error(`Expected axis to be an integer or an array of integers, ` + `but received ${JSON.stringify(this.axis)}`); } this.epsilon = args.epsilon == null ? 1e-3 : args.epsilon; this.center = args.center == null ? true : args.center; this.scale = args.scale == null ? true : args.scale; this.betaInitializer = getInitializer(args.betaInitializer || 'zeros'); this.gammaInitializer = getInitializer(args.gammaInitializer || 'ones'); this.betaRegularizer = getRegularizer(args.betaRegularizer); this.gammaRegularizer = getRegularizer(args.gammaRegularizer); this.supportsMasking = true; } build(inputShape) { inputShape = getExactlyOneShape(inputShape); const nDims = inputShape.length; // Convert axis to array and resolve negatives. if (typeof this.axis === 'number') { this.axis = [this.axis]; } for (let i = 0; i < this.axis.length; ++i) { if (this.axis[i] < 0) { this.axis[i] += nDims; } } // Further validate axes. for (const axis of this.axis) { if (axis < 0 || axis >= nDims) { throw new Error(`Invalid axis: ${axis}`); } } if (this.axis.length !== unique(this.axis).length) { throw new Error(`Found duplicate axes in: ${this.axis}`); } const paramShape = this.axis.map(axis => inputShape[axis]); const trainable = true; if (this.scale) { this.gamma = this.addWeight('gamma', paramShape, 'float32', this.gammaInitializer, this.gammaRegularizer, trainable); } else { this.gamma = null; } if (this.center) { this.beta = this.addWeight('beta', paramShape, 'float32', this.betaInitializer, this.betaRegularizer, trainable); } else { this.beta = null; } this.built = true; } call(inputs, kwargs) { const input = getExactlyOneTensor(inputs); const inputShape = input.shape; const nDims = inputShape.length; return tidy$1(() => { const keepDims = true; let { mean, variance } = moments(input, this.axis, keepDims); const broadcastShape = pyListRepeat(1, nDims); for (const dim of this.axis) { broadcastShape[dim] = inputShape[dim]; } const broadcast = (v) => { if (v != null && v.shape.length !== nDims) { return tfc.reshape(v, broadcastShape); } else { return v; } }; let scale = this.scale ? broadcast(this.gamma.read()) : null; let offset = this.center ? broadcast(this.beta.read()) : null; // TODO(https://github.com/tensorflow/tfjs/issues/2120): The tiling below // is a workaround for the limitation of core's batchNormalization?d don't // support broadcasting in their gradients. In addition, the tiling is // necessary to ensure correctness on the browser CPU backend regardless // of forward or backward computation. Remove this workaround once the // limitation is addressed. See . const momentsTiling = []; const scaleOffsetTiling = []; for (let i = 0; i < nDims; ++i) { if (this.axis.indexOf(i) !== -1) { momentsTiling.push(inputShape[i]); scaleOffsetTiling.push(1); } else { momentsTiling.push(1); scaleOffsetTiling.push(inputShape[i]); } } mean = tfc.tile(mean, momentsTiling); variance = tfc.tile(variance, momentsTiling); if (scale != null) { scale = tfc.tile(scale, scaleOffsetTiling); } if (offset != null) { offset = tfc.tile(offset, scaleOffsetTiling); } return batchNormalization$1(input, mean, variance, offset, scale, this.epsilon); }); } getConfig() { const config = { axis: this.axis, epsilon: this.epsilon, center: this.center, scale: this.scale, betaInitializer: serializeInitializer(this.betaInitializer), gammaInitializer: serializeInitializer(this.gammaInitializer), betaRegularizer: serializeRegularizer(this.betaRegularizer), gammaRegularizer: serializeRegularizer(this.gammaRegularizer) }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ LayerNormalization.className = 'LayerNormalization'; serialization.registerClass(LayerNormalization); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Pads the 2nd and 3rd dimensions of a 4D tensor. * * @param x Input `tf.Tensor` to be padded. * @param padding `Array` of two `Array`s, each of which is an `Array` of two * integers. The amount of padding at the beginning and end of the 2nd and 3rd * dimensions, respectively. * @param dataFormat 'channelsLast' (default) or 'channelsFirst'. * @return Padded 4D `tf.Tensor`. */ function spatial2dPadding(x, padding, dataFormat) { return tidy$1(() => { if (x.rank !== 4) { throw new ValueError(`temporalPadding expects input tensor to be 4-D, but received a ` + `${x.rank}-D tensor.`); } if (padding == null) { padding = [[1, 1], [1, 1]]; } if (padding.length !== 2 || padding[0].length !== 2 || padding[1].length !== 2) { throw new ValueError('spatial2dPadding expects `padding` to be an Array of two Arrays, ' + 'each of which is an Array of two integers.'); } if (dataFormat == null) { dataFormat = imageDataFormat(); } if (dataFormat !== 'channelsLast' && dataFormat !== 'channelsFirst') { throw new ValueError(`Unknown data format: ${dataFormat}. ` + `Supported data formats are 'channelsLast' and 'channelsFirst.`); } let pattern; if (dataFormat === 'channelsFirst') { pattern = [[0, 0], [0, 0], padding[0], padding[1]]; } else { pattern = [[0, 0], padding[0], padding[1], [0, 0]]; } return tfc.pad(x, pattern); }); } class ZeroPadding2D extends Layer { constructor(args) { if (args == null) { args = {}; } super(args); this.dataFormat = args.dataFormat == null ? imageDataFormat() : args.dataFormat; // TODO(cais): Maybe refactor the following logic surrounding `padding` // into a helper method. if (args.padding == null) { this.padding = [[1, 1], [1, 1]]; } else if (typeof args.padding === 'number') { this.padding = [[args.padding, args.padding], [args.padding, args.padding]]; } else { args.padding = args.padding; if (args.padding.length !== 2) { throw new ValueError(`ZeroPadding2D expects padding to be a length-2 array, but ` + `received a length-${args.padding.length} array.`); } let heightPadding; let widthPadding; if (typeof args.padding[0] === 'number') { heightPadding = [args.padding[0], args.padding[0]]; widthPadding = [args.padding[1], args.padding[1]]; } else { args.padding = args.padding; if (args.padding[0].length !== 2) { throw new ValueError(`ZeroPadding2D expects height padding to be a length-2 array, ` + `but received a length-${args.padding[0].length} array.`); } heightPadding = args.padding[0]; if (args.padding[1].length !== 2) { throw new ValueError(`ZeroPadding2D expects width padding to be a length-2 array, ` + `but received a length-${args.padding[1].length} array.`); } widthPadding = args.padding[1]; } this.padding = [heightPadding, widthPadding]; } this.inputSpec = [new InputSpec({ ndim: 4 })]; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); let rows; let cols; if (this.dataFormat === 'channelsFirst') { if (inputShape[2] != null && inputShape[2] >= 0) { rows = inputShape[2] + this.padding[0][0] + this.padding[0][1]; } else { rows = null; } if (inputShape[3] != null && inputShape[3] >= 0) { cols = inputShape[3] + this.padding[1][0] + this.padding[1][1]; } else { cols = null; } return [inputShape[0], inputShape[1], rows, cols]; } else { if (inputShape[1] != null && inputShape[1] >= 0) { rows = inputShape[1] + this.padding[0][0] + this.padding[0][1]; } else { rows = null; } if (inputShape[2] != null && inputShape[2] >= 0) { cols = inputShape[2] + this.padding[1][0] + this.padding[1][1]; } else { cols = null; } return [inputShape[0], rows, cols, inputShape[3]]; } } call(inputs, kwargs) { return tidy$1(() => spatial2dPadding(getExactlyOneTensor(inputs), this.padding, this.dataFormat)); } getConfig() { const config = { padding: this.padding, dataFormat: this.dataFormat, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } /** @nocollapse */ ZeroPadding2D.className = 'ZeroPadding2D'; serialization.registerClass(ZeroPadding2D); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * 2D pooling. * @param x * @param poolSize * @param strides strides. Defaults to [1, 1]. * @param padding padding. Defaults to 'valid'. * @param dataFormat data format. Defaults to 'channelsLast'. * @param poolMode Mode of pooling. Defaults to 'max'. * @returns Result of the 2D pooling. */ function pool2d(x, poolSize, strides, padding, dataFormat, poolMode) { return tidy$1(() => { checkDataFormat(dataFormat); checkPoolMode(poolMode); checkPaddingMode(padding); if (strides == null) { strides = [1, 1]; } if (padding == null) { padding = 'valid'; } if (dataFormat == null) { dataFormat = imageDataFormat(); } if (poolMode == null) { poolMode = 'max'; } // TODO(cais): Remove the preprocessing step once deeplearn.js supports // dataFormat as an input argument. x = preprocessConv2DInput(x, dataFormat); // x is NHWC after preprocessing. let y; const paddingString = (padding === 'same') ? 'same' : 'valid'; if (poolMode === 'max') { // TODO(cais): Rank check? y = tfc.maxPool(x, poolSize, strides, paddingString); } else { // 'avg' // TODO(cais): Check the dtype and rank of x and give clear error message // if those are incorrect. y = tfc.avgPool( // TODO(cais): Rank check? x, poolSize, strides, paddingString); } if (dataFormat === 'channelsFirst') { y = tfc.transpose(y, [0, 3, 1, 2]); // NHWC -> NCHW. } return y; }); } /** * 3D pooling. * @param x * @param poolSize. Default to [1, 1, 1]. * @param strides strides. Defaults to [1, 1, 1]. * @param padding padding. Defaults to 'valid'. * @param dataFormat data format. Defaults to 'channelsLast'. * @param poolMode Mode of pooling. Defaults to 'max'. * @returns Result of the 3D pooling. */ function pool3d(x, poolSize, strides, padding, dataFormat, poolMode) { return tidy$1(() => { checkDataFormat(dataFormat); checkPoolMode(poolMode); checkPaddingMode(padding); if (strides == null) { strides = [1, 1, 1]; } if (padding == null) { padding = 'valid'; } if (dataFormat == null) { dataFormat = imageDataFormat(); } if (poolMode == null) { poolMode = 'max'; } // x is NDHWC after preprocessing. x = preprocessConv3DInput(x, dataFormat); let y; const paddingString = (padding === 'same') ? 'same' : 'valid'; if (poolMode === 'max') { y = tfc.maxPool3d(x, poolSize, strides, paddingString); } else { // 'avg' y = tfc.avgPool3d(x, poolSize, strides, paddingString); } if (dataFormat === 'channelsFirst') { y = tfc.transpose(y, [0, 4, 1, 2, 3]); // NDHWC -> NCDHW. } return y; }); } /** * Abstract class for different pooling 1D layers. */ class Pooling1D extends Layer { /** * * @param args Parameters for the Pooling layer. * * config.poolSize defaults to 2. */ constructor(args) { if (args.poolSize == null) { args.poolSize = 2; } super(args); if (typeof args.poolSize === 'number') { this.poolSize = [args.poolSize]; } else if (Array.isArray(args.poolSize) && args.poolSize.length === 1 && typeof args.poolSize[0] === 'number') { this.poolSize = args.poolSize; } else { throw new ValueError(`poolSize for 1D convolutional layer must be a number or an ` + `Array of a single number, but received ` + `${JSON.stringify(args.poolSize)}`); } assertPositiveInteger(this.poolSize, 'poolSize'); if (args.strides == null) { this.strides = this.poolSize; } else { if (typeof args.strides === 'number') { this.strides = [args.strides]; } else if (Array.isArray(args.strides) && args.strides.length === 1 && typeof args.strides[0] === 'number') { this.strides = args.strides; } else { throw new ValueError(`strides for 1D convolutional layer must be a number or an ` + `Array of a single number, but received ` + `${JSON.stringify(args.strides)}`); } } assertPositiveInteger(this.strides, 'strides'); this.padding = args.padding == null ? 'valid' : args.padding; checkPaddingMode(this.padding); this.inputSpec = [new InputSpec({ ndim: 3 })]; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const length = convOutputLength(inputShape[1], this.poolSize[0], this.padding, this.strides[0]); return [inputShape[0], length, inputShape[2]]; } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); // Add dummy last dimension. inputs = expandDims$1(getExactlyOneTensor(inputs), 2); const output = this.poolingFunction(getExactlyOneTensor(inputs), [this.poolSize[0], 1], [this.strides[0], 1], this.padding, 'channelsLast'); // Remove dummy last dimension. return tfc.squeeze(output, [2]); }); } getConfig() { const config = { poolSize: this.poolSize, padding: this.padding, strides: this.strides, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } class MaxPooling1D extends Pooling1D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool2d(inputs, poolSize, strides, padding, dataFormat, 'max'); } } /** @nocollapse */ MaxPooling1D.className = 'MaxPooling1D'; serialization.registerClass(MaxPooling1D); class AveragePooling1D extends Pooling1D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool2d(inputs, poolSize, strides, padding, dataFormat, 'avg'); } } /** @nocollapse */ AveragePooling1D.className = 'AveragePooling1D'; serialization.registerClass(AveragePooling1D); /** * Abstract class for different pooling 2D layers. */ class Pooling2D extends Layer { constructor(args) { if (args.poolSize == null) { args.poolSize = [2, 2]; } super(args); this.poolSize = Array.isArray(args.poolSize) ? args.poolSize : [args.poolSize, args.poolSize]; if (args.strides == null) { this.strides = this.poolSize; } else if (Array.isArray(args.strides)) { if (args.strides.length !== 2) { throw new ValueError(`If the strides property of a 2D pooling layer is an Array, ` + `it is expected to have a length of 2, but received length ` + `${args.strides.length}.`); } this.strides = args.strides; } else { // `config.strides` is a number. this.strides = [args.strides, args.strides]; } assertPositiveInteger(this.poolSize, 'poolSize'); assertPositiveInteger(this.strides, 'strides'); this.padding = args.padding == null ? 'valid' : args.padding; this.dataFormat = args.dataFormat == null ? 'channelsLast' : args.dataFormat; checkDataFormat(this.dataFormat); checkPaddingMode(this.padding); this.inputSpec = [new InputSpec({ ndim: 4 })]; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); let rows = this.dataFormat === 'channelsFirst' ? inputShape[2] : inputShape[1]; let cols = this.dataFormat === 'channelsFirst' ? inputShape[3] : inputShape[2]; rows = convOutputLength(rows, this.poolSize[0], this.padding, this.strides[0]); cols = convOutputLength(cols, this.poolSize[1], this.padding, this.strides[1]); if (this.dataFormat === 'channelsFirst') { return [inputShape[0], inputShape[1], rows, cols]; } else { return [inputShape[0], rows, cols, inputShape[3]]; } } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); return this.poolingFunction(getExactlyOneTensor(inputs), this.poolSize, this.strides, this.padding, this.dataFormat); }); } getConfig() { const config = { poolSize: this.poolSize, padding: this.padding, strides: this.strides, dataFormat: this.dataFormat }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } class MaxPooling2D extends Pooling2D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool2d(inputs, poolSize, strides, padding, dataFormat, 'max'); } } /** @nocollapse */ MaxPooling2D.className = 'MaxPooling2D'; serialization.registerClass(MaxPooling2D); class AveragePooling2D extends Pooling2D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool2d(inputs, poolSize, strides, padding, dataFormat, 'avg'); } } /** @nocollapse */ AveragePooling2D.className = 'AveragePooling2D'; serialization.registerClass(AveragePooling2D); /** * Abstract class for different pooling 3D layers. */ class Pooling3D extends Layer { constructor(args) { if (args.poolSize == null) { args.poolSize = [2, 2, 2]; } super(args); this.poolSize = Array.isArray(args.poolSize) ? args.poolSize : [args.poolSize, args.poolSize, args.poolSize]; if (args.strides == null) { this.strides = this.poolSize; } else if (Array.isArray(args.strides)) { if (args.strides.length !== 3) { throw new ValueError(`If the strides property of a 3D pooling layer is an Array, ` + `it is expected to have a length of 3, but received length ` + `${args.strides.length}.`); } this.strides = args.strides; } else { // `config.strides` is a number. this.strides = [args.strides, args.strides, args.strides]; } assertPositiveInteger(this.poolSize, 'poolSize'); assertPositiveInteger(this.strides, 'strides'); this.padding = args.padding == null ? 'valid' : args.padding; this.dataFormat = args.dataFormat == null ? 'channelsLast' : args.dataFormat; checkDataFormat(this.dataFormat); checkPaddingMode(this.padding); this.inputSpec = [new InputSpec({ ndim: 5 })]; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); let depths = this.dataFormat === 'channelsFirst' ? inputShape[2] : inputShape[1]; let rows = this.dataFormat === 'channelsFirst' ? inputShape[3] : inputShape[2]; let cols = this.dataFormat === 'channelsFirst' ? inputShape[4] : inputShape[3]; depths = convOutputLength(depths, this.poolSize[0], this.padding, this.strides[0]); rows = convOutputLength(rows, this.poolSize[1], this.padding, this.strides[1]); cols = convOutputLength(cols, this.poolSize[2], this.padding, this.strides[2]); if (this.dataFormat === 'channelsFirst') { return [inputShape[0], inputShape[1], depths, rows, cols]; } else { return [inputShape[0], depths, rows, cols, inputShape[4]]; } } call(inputs, kwargs) { return tidy$1(() => { this.invokeCallHook(inputs, kwargs); return this.poolingFunction(getExactlyOneTensor(inputs), this.poolSize, this.strides, this.padding, this.dataFormat); }); } getConfig() { const config = { poolSize: this.poolSize, padding: this.padding, strides: this.strides, dataFormat: this.dataFormat }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } class MaxPooling3D extends Pooling3D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool3d(inputs, poolSize, strides, padding, dataFormat, 'max'); } } /** @nocollapse */ MaxPooling3D.className = 'MaxPooling3D'; serialization.registerClass(MaxPooling3D); class AveragePooling3D extends Pooling3D { constructor(args) { super(args); } poolingFunction(inputs, poolSize, strides, padding, dataFormat) { checkDataFormat(dataFormat); checkPaddingMode(padding); return pool3d(inputs, poolSize, strides, padding, dataFormat, 'avg'); } } /** @nocollapse */ AveragePooling3D.className = 'AveragePooling3D'; serialization.registerClass(AveragePooling3D); /** * Abstract class for different global pooling 1D layers. */ class GlobalPooling1D extends Layer { constructor(args) { super(args); this.inputSpec = [new InputSpec({ ndim: 3 })]; } computeOutputShape(inputShape) { return [inputShape[0], inputShape[2]]; } call(inputs, kwargs) { throw new NotImplementedError(); } } class GlobalAveragePooling1D extends GlobalPooling1D { constructor(args) { super(args || {}); } call(inputs, kwargs) { return tidy$1(() => { const input = getExactlyOneTensor(inputs); return tfc.mean(input, 1); }); } } /** @nocollapse */ GlobalAveragePooling1D.className = 'GlobalAveragePooling1D'; serialization.registerClass(GlobalAveragePooling1D); class GlobalMaxPooling1D extends GlobalPooling1D { constructor(args) { super(args || {}); } call(inputs, kwargs) { return tidy$1(() => { const input = getExactlyOneTensor(inputs); return tfc.max(input, 1); }); } } /** @nocollapse */ GlobalMaxPooling1D.className = 'GlobalMaxPooling1D'; serialization.registerClass(GlobalMaxPooling1D); /** * Abstract class for different global pooling 2D layers. */ class GlobalPooling2D extends Layer { constructor(args) { super(args); this.dataFormat = args.dataFormat == null ? 'channelsLast' : args.dataFormat; checkDataFormat(this.dataFormat); this.inputSpec = [new InputSpec({ ndim: 4 })]; } computeOutputShape(inputShape) { inputShape = inputShape; if (this.dataFormat === 'channelsLast') { return [inputShape[0], inputShape[3]]; } else { return [inputShape[0], inputShape[1]]; } } call(inputs, kwargs) { throw new NotImplementedError(); } getConfig() { const config = { dataFormat: this.dataFormat }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } class GlobalAveragePooling2D extends GlobalPooling2D { call(inputs, kwargs) { return tidy$1(() => { const input = getExactlyOneTensor(inputs); if (this.dataFormat === 'channelsLast') { return tfc.mean(input, [1, 2]); } else { return tfc.mean(input, [2, 3]); } }); } } /** @nocollapse */ GlobalAveragePooling2D.className = 'GlobalAveragePooling2D'; serialization.registerClass(GlobalAveragePooling2D); class GlobalMaxPooling2D extends GlobalPooling2D { call(inputs, kwargs) { return tidy$1(() => { const input = getExactlyOneTensor(inputs); if (this.dataFormat === 'channelsLast') { return tfc.max(input, [1, 2]); } else { return tfc.max(input, [2, 3]); } }); } } /** @nocollapse */ GlobalMaxPooling2D.className = 'GlobalMaxPooling2D'; serialization.registerClass(GlobalMaxPooling2D); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Abstract wrapper base class. * * Wrappers take another layer and augment it in various ways. * Do not use this class as a layer, it is only an abstract base class. * Two usable wrappers are the `TimeDistributed` and `Bidirectional` wrappers. */ class Wrapper extends Layer { constructor(args) { // Porting Note: In PyKeras, `self.layer` is set prior to the calling // `super()`. But we can't do that here due to TypeScript's restriction. // See: https://github.com/Microsoft/TypeScript/issues/8277 // As a result, we have to add checks in `get trainable()` and // `set trainable()` below in order to prevent using `this.layer` when // its value is `undefined`. The super constructor does use the getter // and the setter of `this.layer`. super(args); this.layer = args.layer; } build(inputShape) { this.built = true; } // TODO(cais): Implement activityRegularizer getter. get trainable() { // Porting Note: the check of `this.layer` here is necessary due to the // way the `constructor` of this class is written (see Porting Note // above). if (this.layer != null) { return this.layer.trainable; } else { return false; } } set trainable(value) { // Porting Note: the check of `this.layer` here is necessary due to the // way the `constructor` of this class is written (see Porting Note // above). if (this.layer != null) { this.layer.trainable = value; } } get trainableWeights() { return this.layer.trainableWeights; } // TODO(cais): Implement setter for trainableWeights. get nonTrainableWeights() { return this.layer.nonTrainableWeights; } // TODO(cais): Implement setter for nonTrainableWeights. get updates() { // tslint:disable-next-line:no-any return this.layer._updates; } // TODO(cais): Implement getUpdatesFor(). get losses() { return this.layer.losses; } // TODO(cais): Implement getLossesFor(). getWeights() { return this.layer.getWeights(); } setWeights(weights) { this.layer.setWeights(weights); } getConfig() { const config = { 'layer': { 'className': this.layer.getClassName(), 'config': this.layer.getConfig(), } }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } setFastWeightInitDuringBuild(value) { super.setFastWeightInitDuringBuild(value); if (this.layer != null) { this.layer.setFastWeightInitDuringBuild(value); } } /** @nocollapse */ static fromConfig(cls, config, customObjects = {}) { const layerConfig = config['layer']; const layer = deserialize(layerConfig, customObjects); delete config['layer']; const newConfig = { layer }; Object.assign(newConfig, config); return new cls(newConfig); } } class TimeDistributed extends Wrapper { constructor(args) { super(args); this.supportsMasking = true; } build(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape.length < 3) { throw new ValueError(`TimeDistributed layer expects an input shape >= 3D, but received ` + `input shape ${JSON.stringify(inputShape)}`); } this.inputSpec = [{ shape: inputShape }]; const childInputShape = [inputShape[0]].concat(inputShape.slice(2)); if (!this.layer.built) { this.layer.build(childInputShape); this.layer.built = true; } super.build(inputShape); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const childInputShape = [inputShape[0]].concat(inputShape.slice(2)); const childOutputShape = this.layer.computeOutputShape(childInputShape); const timesteps = inputShape[1]; return [childOutputShape[0], timesteps].concat(childOutputShape.slice(1)); } call(inputs, kwargs) { return tidy$1(() => { // TODO(cais): Add 'training' and 'useLearningPhase' to kwargs. inputs = getExactlyOneTensor(inputs); // Porting Note: In tfjs-layers, `inputs` are always concrete tensor // values. Hence the inputs can't have an undetermined first (batch) // dimension, which is why we always use the K.rnn approach here. const step = (inputs, states) => { // TODO(cais): Add useLearningPhase. // NOTE(cais): `layer.call` may return a length-1 array of Tensor in // some cases (e.g., `layer` is a `Sequential` instance), which is // why `getExactlyOneTensor` is used below. const output = getExactlyOneTensor(this.layer.call(inputs, kwargs)); return [output, []]; }; const rnnOutputs = rnn$1(step, inputs, [], false /* goBackwards */, null /* mask */, null /* constants */, false /* unroll */, true /* needPerStepOutputs */); const y = rnnOutputs[1]; // TODO(cais): Add activity regularization. // TODO(cais): Add useLearningPhase. return y; }); } } /** @nocollapse */ TimeDistributed.className = 'TimeDistributed'; serialization.registerClass(TimeDistributed); function checkBidirectionalMergeMode(value) { checkStringTypeUnionValue(VALID_BIDIRECTIONAL_MERGE_MODES, 'BidirectionalMergeMode', value); } const DEFAULT_BIDIRECTIONAL_MERGE_MODE = 'concat'; class Bidirectional extends Wrapper { constructor(args) { super(args); // Note: When creating `this.forwardLayer`, the original Layer object // (`config.layer`) ought to be cloned. This is why we call // `getConfig()` followed by `deserialize()`. Without this cloning, // the layer names saved during serialization will incorrectly contain // the 'forward_' prefix. In Python Keras, this is done using // `copy.copy` (shallow copy), which does not have a simple equivalent // in JavaScript. JavaScript's `Object.assign()` does not copy // methods. const layerConfig = args.layer.getConfig(); const forwDict = {}; forwDict['className'] = args.layer.getClassName(); forwDict['config'] = layerConfig; this.forwardLayer = deserialize(forwDict); layerConfig['goBackwards'] = layerConfig['goBackwards'] === true ? false : true; const backDict = {}; backDict['className'] = args.layer.getClassName(); backDict['config'] = layerConfig; this.backwardLayer = deserialize(backDict); this.forwardLayer.name = 'forward_' + this.forwardLayer.name; this.backwardLayer.name = 'backward_' + this.backwardLayer.name; this.mergeMode = args.mergeMode === undefined ? DEFAULT_BIDIRECTIONAL_MERGE_MODE : args.mergeMode; checkBidirectionalMergeMode(this.mergeMode); if (args.weights) { throw new NotImplementedError('weights support is not implemented for Bidirectional layer yet.'); } this._stateful = args.layer.stateful; this.returnSequences = args.layer.returnSequences; this.returnState = args.layer.returnState; this.supportsMasking = true; this._trainable = true; this.inputSpec = args.layer.inputSpec; this.numConstants = null; } get trainable() { return this._trainable; } set trainable(value) { // Porting Note: the check of `this.layer` here is necessary due to the // way the `constructor` of this class is written (see Porting Note // above). this._trainable = value; if (this.forwardLayer != null) { this.forwardLayer.trainable = value; } if (this.backwardLayer != null) { this.backwardLayer.trainable = value; } } getWeights() { return this.forwardLayer.getWeights().concat(this.backwardLayer.getWeights()); } setWeights(weights) { const numWeights = weights.length; const numeightsOver2 = Math.floor(numWeights / 2); this.forwardLayer.setWeights(weights.slice(0, numeightsOver2)); this.backwardLayer.setWeights(weights.slice(numeightsOver2)); } computeOutputShape(inputShape) { let layerShapes = this.forwardLayer.computeOutputShape(inputShape); if (!(Array.isArray(layerShapes) && Array.isArray(layerShapes[0]))) { layerShapes = [layerShapes]; } layerShapes = layerShapes; let outputShape; let outputShapes; let stateShape; if (this.returnState) { stateShape = layerShapes.slice(1); outputShape = layerShapes[0]; } else { outputShape = layerShapes[0]; } outputShape = outputShape; if (this.mergeMode === 'concat') { outputShape[outputShape.length - 1] *= 2; outputShapes = [outputShape]; } else if (this.mergeMode == null) { outputShapes = [outputShape, outputShape.slice()]; } else { outputShapes = [outputShape]; } if (this.returnState) { if (this.mergeMode == null) { return outputShapes.concat(stateShape).concat(stateShape.slice()); } return [outputShape].concat(stateShape).concat(stateShape.slice()); } return singletonOrArray(outputShapes); } apply(inputs, kwargs) { let initialState = kwargs == null ? null : kwargs['initialState']; let constants = kwargs == null ? null : kwargs['constants']; if (kwargs == null) { kwargs = {}; } const standardized = standardizeArgs(inputs, initialState, constants, this.numConstants); inputs = standardized.inputs; initialState = standardized.initialState; constants = standardized.constants; if (Array.isArray(inputs)) { initialState = inputs.slice(1); inputs = inputs[0]; } if ((initialState == null || initialState.length === 0) && constants == null) { return super.apply(inputs, kwargs); } const additionalInputs = []; const additionalSpecs = []; if (initialState != null) { const numStates = initialState.length; if (numStates % 2 > 0) { throw new ValueError('When passing `initialState` to a Bidrectional RNN, ' + 'the state should be an Array containing the states of ' + 'the underlying RNNs.'); } kwargs['initialState'] = initialState; additionalInputs.push(...initialState); const stateSpecs = initialState .map(state => new InputSpec({ shape: state.shape })); this.forwardLayer.stateSpec = stateSpecs.slice(0, numStates / 2); this.backwardLayer.stateSpec = stateSpecs.slice(numStates / 2); additionalSpecs.push(...stateSpecs); } if (constants != null) { throw new NotImplementedError('Support for constants in Bidirectional layers is not ' + 'implemented yet.'); } const isSymbolicTensor = additionalInputs[0] instanceof SymbolicTensor; for (const tensor of additionalInputs) { if (tensor instanceof SymbolicTensor !== isSymbolicTensor) { throw new ValueError('The initial state of a Bidirectional layer cannot be ' + 'specified as a mix of symbolic and non-symbolic tensors'); } } if (isSymbolicTensor) { // Compute the full input and specs, including the states. const fullInput = [inputs].concat(additionalInputs); const fullInputSpec = this.inputSpec.concat(additionalSpecs); // Perform the call temporarily and replace inputSpec. // Note: with initial states symbolic calls and non-symbolic calls to // this method differ in how the initial states are passed. For // symbolic calls, the initial states are passed in the first arg, as // an Array of SymbolicTensors; for non-symbolic calls, they are // passed in the second arg as a part of the kwargs. Hence the need to // temporarily modify inputSpec here. // TODO(cais): Make refactoring so that this hacky code below is no // longer needed. const originalInputSpec = this.inputSpec; this.inputSpec = fullInputSpec; const output = super.apply(fullInput, kwargs); this.inputSpec = originalInputSpec; return output; } else { return super.apply(inputs, kwargs); } } call(inputs, kwargs) { return tidy$1(() => { const initialState = kwargs['initialState']; let y; let yRev; if (initialState == null) { y = this.forwardLayer.call(inputs, kwargs); yRev = this.backwardLayer.call(inputs, kwargs); } else { const forwardState = initialState.slice(0, initialState.length / 2); const backwardState = initialState.slice(initialState.length / 2); y = this.forwardLayer.call(inputs, Object.assign(kwargs, { initialState: forwardState })); yRev = this.backwardLayer.call(inputs, Object.assign(kwargs, { initialState: backwardState })); } let states; if (this.returnState) { if (Array.isArray(y)) { states = y.slice(1).concat(yRev.slice(1)); } y = y[0]; yRev = yRev[0]; } if (this.returnSequences) { yRev = tfc.reverse(yRev, 1); } let output; if (this.mergeMode === 'concat') { output = concatenate$1([y, yRev]); } else if (this.mergeMode === 'sum') { output = tfc.add(y, yRev); } else if (this.mergeMode === 'ave') { output = tfc.mul(.5, tfc.add(y, yRev)); } else if (this.mergeMode === 'mul') { output = tfc.mul(y, yRev); } else if (this.mergeMode == null) { output = [y, yRev]; } // TODO(cais): Properly set learning phase. if (this.returnState) { if (this.mergeMode == null) { return output.concat(states); } return [output].concat(states); } return output; }); } resetStates(states) { this.forwardLayer.resetStates(); this.backwardLayer.resetStates(); } build(inputShape) { nameScope(this.forwardLayer.name, () => { this.forwardLayer.build(inputShape); }); nameScope(this.backwardLayer.name, () => { this.backwardLayer.build(inputShape); }); this.built = true; } computeMask(inputs, mask) { if (Array.isArray(mask)) { mask = mask[0]; } let outputMask; if (this.returnSequences) { if (this.mergeMode == null) { outputMask = [mask, mask]; } else { outputMask = mask; } } else { if (this.mergeMode == null) { outputMask = [null, null]; } else { outputMask = null; } } if (this.returnState) { const states = this.forwardLayer.states; const stateMask = states.map(state => null); if (Array.isArray(outputMask)) { return outputMask.concat(stateMask).concat(stateMask); } else { return [outputMask].concat(stateMask).concat(stateMask); } } else { return outputMask; } } get trainableWeights() { return this.forwardLayer.trainableWeights.concat(this.backwardLayer.trainableWeights); } get nonTrainableWeights() { return this.forwardLayer.nonTrainableWeights.concat(this.backwardLayer.nonTrainableWeights); } // TODO(cais): Implement constraints(). setFastWeightInitDuringBuild(value) { super.setFastWeightInitDuringBuild(value); if (this.forwardLayer != null) { this.forwardLayer.setFastWeightInitDuringBuild(value); } if (this.backwardLayer != null) { this.backwardLayer.setFastWeightInitDuringBuild(value); } } getConfig() { const config = { 'mergeMode': this.mergeMode, }; // TODO(cais): Add logic for `numConstants` once the property is added. const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } /** @nocollapse */ static fromConfig(cls, config) { const rnnLayer = deserialize(config['layer']); delete config['layer']; // TODO(cais): Add logic for `numConstants` once the property is added. if (config['numConstants'] != null) { throw new NotImplementedError(`Deserialization of a Bidirectional layer with numConstants ` + `present is not supported yet.`); } // tslint:disable-next-line:no-any const newConfig = config; newConfig['layer'] = rnnLayer; return new cls(newConfig); } } /** @nocollapse */ Bidirectional.className = 'Bidirectional'; serialization.registerClass(Bidirectional); /** * @license * Copyright 2022 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Preprocessing Rescaling Layer * * This rescales images by a scaling and offset factor */ class Rescaling extends Layer { constructor(args) { super(args); this.scale = args.scale; if (args.offset) { this.offset = args.offset; } else { this.offset = 0; } } getConfig() { const config = { 'scale': this.scale, 'offset': this.offset }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); if (inputs.dtype !== 'float32') { inputs = cast$1(inputs, 'float32'); } return add$3(mul$1(inputs, this.scale), this.offset); }); } } /** @nocollapse */ Rescaling.className = 'Rescaling'; serialization.registerClass(Rescaling); /** * @license * Copyright 2022 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ const { resizeBilinear, cropAndResize } = image; class CenterCrop extends Layer { constructor(args) { super(args); this.height = args.height; this.width = args.width; } centerCrop(inputs, hBuffer, wBuffer, height, width, inputHeight, inputWidth, dtype) { return tidy$1(() => { let input; let isRank3 = false; const top = hBuffer / inputHeight; const left = wBuffer / inputWidth; const bottom = ((height) + hBuffer) / inputHeight; const right = ((width) + wBuffer) / inputWidth; const bound = [top, left, bottom, right]; const boxesArr = []; if (inputs.rank === 3) { isRank3 = true; input = stack$1([inputs]); } else { input = inputs; } for (let i = 0; i < input.shape[0]; i++) { boxesArr.push(bound); } const boxes = tensor(boxesArr, [boxesArr.length, 4]); const boxInd = range$1(0, boxesArr.length, 1, 'int32'); const cropSize = [height, width]; const cropped = cropAndResize(input, boxes, boxInd, cropSize, 'nearest'); if (isRank3) { return cast$1(getExactlyOneTensor(unstack$1(cropped)), dtype); } return cast$1(cropped, dtype); }); } upsize(inputs, height, width, dtype) { return tidy$1(() => { const outputs = resizeBilinear(inputs, [height, width]); return cast$1(outputs, dtype); }); } call(inputs, kwargs) { return tidy$1(() => { const rankedInputs = getExactlyOneTensor(inputs); const dtype = rankedInputs.dtype; const inputShape = rankedInputs.shape; const inputHeight = inputShape[inputShape.length - 3]; const inputWidth = inputShape[inputShape.length - 2]; let hBuffer = 0; if (inputHeight !== this.height) { hBuffer = Math.floor((inputHeight - this.height) / 2); } let wBuffer = 0; if (inputWidth !== this.width) { wBuffer = Math.floor((inputWidth - this.width) / 2); if (wBuffer === 0) { wBuffer = 1; } } if (hBuffer >= 0 && wBuffer >= 0) { return this.centerCrop(rankedInputs, hBuffer, wBuffer, this.height, this.width, inputHeight, inputWidth, dtype); } else { return this.upsize(inputs, this.height, this.width, dtype); } }); } getConfig() { const config = { 'height': this.height, 'width': this.width }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const hAxis = inputShape.length - 3; const wAxis = inputShape.length - 2; inputShape[hAxis] = this.height; inputShape[wAxis] = this.width; return inputShape; } } /** @nocollapse */ CenterCrop.className = 'CenterCrop'; serialization.registerClass(CenterCrop); /** * @license * Copyright 2022 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ function encodeCategoricalInputs(inputs, outputMode, depth, weights) { let input = getExactlyOneTensor(inputs); if (input.dtype !== 'int32') { input = cast$1(input, 'int32'); } if (outputMode === 'int') { return input; } const originalShape = input.shape; if (input.rank === 0) { input = expandDims$2(input, -1); } if (outputMode === 'oneHot') { if (input.shape[input.shape.length - 1] !== 1) { input = expandDims$2(input, -1); } } if (input.rank > 2) { throw new ValueError(`When outputMode is not int, maximum output rank is 2` + ` Received outputMode ${outputMode} and input shape ${originalShape}` + ` which would result in output rank ${input.rank}.`); } const binaryOutput = ['multiHot', 'oneHot'].includes(outputMode); const denseBincountInput = input; let binCounts; if ((typeof weights) !== 'undefined' && outputMode === 'count') { binCounts = denseBincount(denseBincountInput, weights, depth, binaryOutput); } else { binCounts = denseBincount(denseBincountInput, [], depth, binaryOutput); } if (outputMode !== 'tfIdf') { return binCounts; } if (weights) { return mul$1(binCounts, weights); } else { throw new ValueError(`When outputMode is 'tfIdf', weights must be provided.`); } } /** * @license * Copyright 2022 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class CategoryEncoding extends Layer { constructor(args) { super(args); this.numTokens = args.numTokens; if (args.outputMode) { this.outputMode = args.outputMode; } else { this.outputMode = 'multiHot'; } } getConfig() { const config = { 'numTokens': this.numTokens, 'outputMode': this.outputMode, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); if (inputShape == null) { return [this.numTokens]; } if (this.outputMode === 'oneHot' && inputShape[inputShape.length - 1] !== 1) { inputShape.push(this.numTokens); return inputShape; } inputShape[inputShape.length - 1] = this.numTokens; return inputShape; } call(inputs, kwargs) { return tidy$1(() => { inputs = getExactlyOneTensor(inputs); if (inputs.dtype !== 'int32') { inputs = cast$1(inputs, 'int32'); } let countWeights; if ((typeof kwargs['countWeights']) !== 'undefined') { if (this.outputMode !== 'count') { throw new ValueError(`countWeights is not used when outputMode !== count. Received countWeights=${kwargs['countWeights']}`); } countWeights = getExactlyOneTensor(kwargs['countWeights']); } const maxValue = max$1(inputs); const minValue = min$1(inputs); const greaterEqualMax = greater$2(this.numTokens, maxValue) .bufferSync().get(0); const greaterMin = greaterEqual$1(minValue, 0).bufferSync().get(0); if (!(greaterEqualMax && greaterMin)) { throw new ValueError('Input values must be between 0 < values <=' + ` numTokens with numTokens=${this.numTokens}`); } return encodeCategoricalInputs(inputs, this.outputMode, this.numTokens, countWeights); }); } } /** @nocollapse */ CategoryEncoding.className = 'CategoryEncoding'; serialization.registerClass(CategoryEncoding); /** * @license * Copyright 2022 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // tf methods unimplemented in tfjs: 'bicubic', 'area', 'lanczos3', 'lanczos5', // 'gaussian', 'mitchellcubic' const INTERPOLATION_KEYS$1 = ['bilinear', 'nearest']; const INTERPOLATION_METHODS$1 = new Set(INTERPOLATION_KEYS$1); /** * Preprocessing Resizing Layer * * This resizes images by a scaling and offset factor */ class Resizing extends Layer { constructor(args) { super(args); this.height = args.height; this.width = args.width; if (args.interpolation) { if (INTERPOLATION_METHODS$1.has(args.interpolation)) { this.interpolation = args.interpolation; } else { throw new ValueError(`Invalid interpolation parameter: ${args.interpolation} is not implemented`); } } else { this.interpolation = 'bilinear'; } this.cropToAspectRatio = Boolean(args.cropToAspectRatio); } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const numChannels = inputShape[2]; return [this.height, this.width, numChannels]; } getConfig() { const config = { 'height': this.height, 'width': this.width, 'interpolation': this.interpolation, 'cropToAspectRatio': this.cropToAspectRatio }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } call(inputs, kwargs) { return tidy$1(() => { const size = [this.height, this.width]; if (this.interpolation === 'bilinear') { return image.resizeBilinear(inputs, size, !this.cropToAspectRatio); } else if (this.interpolation === 'nearest') { return image.resizeNearestNeighbor(inputs, size, !this.cropToAspectRatio); } else { throw new Error(`Interpolation is ${this.interpolation} but only ${[...INTERPOLATION_METHODS$1]} are supported`); } }); } } /** @nocollapse */ Resizing.className = 'Resizing'; serialization.registerClass(Resizing); /** * @license * Copyright 2023 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * Keeps track of seed and handles pseudorandomness * Instance created in BaseRandomLayer class * Utilized for random preprocessing layers */ class RandomSeed { constructor(seed) { this.seed = seed; } next() { if (this.seed === undefined) { return undefined; } return this.seed++; } } RandomSeed.className = 'RandomSeed'; /** * @license * Copyright 2023 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class BaseRandomLayer extends Layer { constructor(args) { super(args); this.randomGenerator = new RandomSeed(args.seed); } getConfig() { const config = { 'seed': this.randomGenerator.seed }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } } // A layer handle the random number creation and savemodel behavior. /** @nocollapse */ BaseRandomLayer.className = 'BaseRandomLayer'; /** * @license * Copyright 2023 CodeSmith LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ const INTERPOLATION_KEYS = ['bilinear', 'nearest']; const INTERPOLATION_METHODS = new Set(INTERPOLATION_KEYS); /** * Preprocessing Layer with randomly varies image during training * * This layer randomly adjusts the width of a batch of images of a * batch of images by a random factor. * * The input should be a 3D (unbatched) or * 4D (batched) tensor in the `"channels_last"` image data format. Input pixel * values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and of interger * or floating point dtype. By default, the layer will output floats. * * tf methods implemented in tfjs: 'bilinear', 'nearest', * tf methods unimplemented in tfjs: 'bicubic', 'area', 'lanczos3', 'lanczos5', * 'gaussian', 'mitchellcubic' * */ class RandomWidth extends BaseRandomLayer { constructor(args) { super(args); const { factor, interpolation = 'bilinear' } = args; this.factor = factor; if (Array.isArray(this.factor) && this.factor.length === 2) { this.widthLower = this.factor[0]; this.widthUpper = this.factor[1]; } else if (!Array.isArray(this.factor) && this.factor > 0) { this.widthLower = -this.factor; this.widthUpper = this.factor; } else { throw new ValueError(`Invalid factor: ${this.factor}. Must be positive number or tuple of 2 numbers`); } if (this.widthLower < -1.0 || this.widthUpper < -1.0) { throw new ValueError(`factor must have values larger than -1. Got: ${this.factor}`); } if (this.widthUpper < this.widthLower) { throw new ValueError(`factor cannot have upper bound less than lower bound. Got upper bound: ${this.widthUpper}. Got lower bound: ${this.widthLower} `); } if (interpolation) { if (INTERPOLATION_METHODS.has(interpolation)) { this.interpolation = interpolation; } else { throw new ValueError(`Invalid interpolation parameter: ${interpolation} is not implemented`); } } } getConfig() { const config = { 'factor': this.factor, 'interpolation': this.interpolation, }; const baseConfig = super.getConfig(); Object.assign(config, baseConfig); return config; } computeOutputShape(inputShape) { inputShape = getExactlyOneShape(inputShape); const numChannels = inputShape[2]; return [this.imgHeight, -1, numChannels]; } call(inputs, kwargs) { return tidy$1(() => { const input = getExactlyOneTensor(inputs); this.imgHeight = input.shape[input.shape.length - 3]; const imgWidth = input.shape[input.shape.length - 2]; this.widthFactor = randomUniform$1([1], (1.0 + this.widthLower), (1.0 + this.widthUpper), 'float32', this.randomGenerator.next()); let adjustedWidth = this.widthFactor.dataSync()[0] * imgWidth; adjustedWidth = Math.round(adjustedWidth); const size = [this.imgHeight, adjustedWidth]; switch (this.interpolation) { case 'bilinear': return image.resizeBilinear(inputs, size); case 'nearest': return image.resizeNearestNeighbor(inputs, size); default: throw new Error(`Interpolation is ${this.interpolation} but only ${[...INTERPOLATION_METHODS]} are supported`); } }); } } /** @nocollapse */ RandomWidth.className = 'RandomWidth'; serialization.registerClass(RandomWidth); /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ // TODO(cais): Add doc string to all the public static functions in this // class; include exectuable JavaScript code snippets where applicable // (b/74074458). // Input Layer. /** * An input layer is an entry point into a `tf.LayersModel`. * * `InputLayer` is generated automatically for `tf.Sequential` models by * specifying the `inputshape` or `batchInputShape` for the first layer. It * should not be specified explicitly. However, it can be useful sometimes, * e.g., when constructing a sequential model from a subset of another * sequential model's layers. Like the code snippet below shows. * * ```js * // Define a model which simply adds two inputs. * const model1 = tf.sequential(); * model1.add(tf.layers.dense({inputShape: [4], units: 3, activation: 'relu'})); * model1.add(tf.layers.dense({units: 1, activation: 'sigmoid'})); * model1.summary(); * model1.predict(tf.zeros([1, 4])).print(); * * // Construct another model, reusing the second layer of `model1` while * // not using the first layer of `model1`. Note that you cannot add the second * // layer of `model` directly as the first layer of the new sequential model, * // because doing so will lead to an error related to the fact that the layer * // is not an input layer. Instead, you need to create an `inputLayer` and add * // it to the new sequential model before adding the reused layer. * const model2 = tf.sequential(); * // Use an inputShape that matches the input shape of `model1`'s second * // layer. * model2.add(tf.layers.inputLayer({inputShape: [3]})); * model2.add(model1.layers[1]); * model2.summary(); * model2.predict(tf.zeros([1, 3])).print(); * ``` * * @doc {heading: 'Layers', subheading: 'Inputs', namespace: 'layers'} */ function inputLayer(args) { return new InputLayer(args); } // Advanced Activation Layers. /** * Exponential 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: * - [Fast and Accurate Deep Network Learning by Exponential Linear Units * (ELUs)](https://arxiv.org/abs/1511.07289v1) * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function elu(args) { return new ELU(args); } /** * Rectified Linear Unit activation function. * * Input shape: * Arbitrary. Use the config field `inputShape` (Array of integers, does * not include the sample axis) when using this layer as the first layer * in a model. * * Output shape: * Same shape as the input. * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function reLU(args) { return new ReLU(args); } /** * 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. * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function leakyReLU(args) { return new LeakyReLU(args); } /** * Parameterized version of a leaky rectified linear unit. * * It follows * `f(x) = alpha * x for x < 0.` * `f(x) = x for x >= 0.` * wherein `alpha` is a trainable weight. * * 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. * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function prelu(args) { return new PReLU(args); } /** * 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. * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function softmax(args) { return new Softmax(args); } /** * 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: * - [Zero-Bias Autoencoders and the Benefits of Co-Adapting * Features](http://arxiv.org/abs/1402.3337) * * @doc { * heading: 'Layers', * subheading: 'Advanced Activation', * namespace: 'layers' * } */ function thresholdedReLU(args) { return new ThresholdedReLU(args); } // Convolutional Layers. /** * 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. * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function conv1d(args) { return new Conv1D(args); } /** * 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'`. * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function conv2d(args) { return new Conv2D(args); } /** * 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: * - [A guide to convolution arithmetic for deep * learning](https://arxiv.org/abs/1603.07285v1) * - [Deconvolutional * Networks](http://www.matthewzeiler.com/pubs/cvpr2010/cvpr2010.pdf) * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function conv2dTranspose(args) { return new Conv2DTranspose(args); } /** * 3D convolution layer (e.g. spatial convolution over volumes). * * 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, 128, 1]` for 128x128x128 grayscale volumes * in `dataFormat='channelsLast'`. * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function conv3d(args) { return new Conv3D(args); } function conv3dTranspose(args) { return new Conv3DTranspose(args); } /** * 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. * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function separableConv2d(args) { return new SeparableConv2D(args); } /** * Cropping layer for 2D input (e.g., image). * * This layer can crop an input * 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, croppedRows, croppedCols, channels]` * - If `dataFormat` is `"channelsFirst"`: * `[batch, channels, croppedRows, croppedCols]`. * * Examples * ```js * * const model = tf.sequential(); * model.add(tf.layers.cropping2D({cropping:[[2, 2], [2, 2]], * inputShape: [128, 128, 3]})); * //now output shape is [batch, 124, 124, 3] * ``` * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function cropping2D(args) { return new Cropping2D(args); } /** * Upsampling layer for 2D inputs. * * Repeats the rows and columns of the data * by size[0] and size[1] respectively. * * * Input shape: * 4D tensor with shape: * - If `dataFormat` is `"channelsLast"`: * `[batch, rows, cols, channels]` * - If `dataFormat` is `"channelsFirst"`: * `[batch, channels, rows, cols]` * * Output shape: * 4D tensor with shape: * - If `dataFormat` is `"channelsLast"`: * `[batch, upsampledRows, upsampledCols, channels]` * - If `dataFormat` is `"channelsFirst"`: * `[batch, channels, upsampledRows, upsampledCols]` * * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function upSampling2d(args) { return new UpSampling2D(args); } // Convolutional(depthwise) Layers. /** * 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 `depthMultiplier` argument controls how many output channels * are generated per input channel in the depthwise step. * * @doc {heading: 'Layers', subheading: 'Convolutional', namespace: 'layers'} */ function depthwiseConv2d(args) { return new DepthwiseConv2D(args); } // Basic Layers. /** * Applies an activation function to an output. * * This layer applies element-wise activation function. Other layers, notably * `dense` can also apply activation functions. Use this isolated activation * function to extract the values before and after the * activation. For instance: * * ```js * const input = tf.input({shape: [5]}); * const denseLayer = tf.layers.dense({units: 1}); * const activationLayer = tf.layers.activation({activation: 'relu6'}); * * // Obtain the output symbolic tensors by applying the layers in order. * const denseOutput = denseLayer.apply(input); * const activationOutput = activationLayer.apply(denseOutput); * * // Create the model based on the inputs. * const model = tf.model({ * inputs: input, * outputs: [denseOutput, activationOutput] * }); * * // Collect both outputs and print separately. * const [denseOut, activationOut] = model.predict(tf.randomNormal([6, 5])); * denseOut.print(); * activationOut.print(); * ``` * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function activation(args) { return new Activation(args); } /** * 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. * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function dense(args) { return new Dense(args); } /** * Applies * [dropout](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf) 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. * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function dropout(args) { return new Dropout(args); } /** * Spatial 1D version of Dropout. * * This Layer type performs the same function as the Dropout layer, but it drops * entire 1D feature maps instead of individual elements. For example, if an * input example consists of 3 timesteps and the feature map for each timestep * has a size of 4, a `spatialDropout1d` layer may zero out the feature maps * of the 1st timesteps and 2nd timesteps completely while sparing all feature * elements of the 3rd timestep. * * If adjacent frames (timesteps) are strongly correlated (as is normally the * case in early convolution layers), regular dropout will not regularize the * activation and will otherwise just result in merely an effective learning * rate decrease. In this case, `spatialDropout1d` will help promote * independence among feature maps and should be used instead. * * **Arguments:** * rate: A floating-point number >=0 and <=1. Fraction of the input elements * to drop. * * **Input shape:** * 3D tensor with shape `(samples, timesteps, channels)`. * * **Output shape:** * Same as the input shape. * * References: * - [Efficient Object Localization Using Convolutional * Networks](https://arxiv.org/abs/1411.4280) * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function spatialDropout1d(args) { return new SpatialDropout1D(args); } /** * 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: * * ```js * 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)); * ``` * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function flatten(args) { return new Flatten(args); } /** * Repeats the input n times in a new dimension. * * ```js * const model = tf.sequential(); * model.add(tf.layers.repeatVector({n: 4, inputShape: [2]})); * const x = tf.tensor2d([[10, 20]]); * // Use the model to do inference on a data point the model hasn't seen * model.predict(x).print(); * // output shape is now [batch, 2, 4] * ``` * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function repeatVector(args) { return new RepeatVector(args); } /** * Reshapes an input to a certain shape. * * ```js * const input = tf.input({shape: [4, 3]}); * const reshapeLayer = tf.layers.reshape({targetShape: [2, 6]}); * // Inspect the inferred output shape of the Reshape layer, which * // equals `[null, 2, 6]`. (The 1st dimension is the undermined batch size.) * console.log(JSON.stringify(reshapeLayer.apply(input).shape)); * ``` * * Input shape: * Arbitrary, although all dimensions in the input shape must be fixed. * Use the configuration `inputShape` when using this layer as the * first layer in a model. * * * Output shape: * [batchSize, targetShape[0], targetShape[1], ..., * targetShape[targetShape.length - 1]]. * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function reshape(args) { return new Reshape(args); } /** * Permutes the dimensions of the input according to a given pattern. * * Useful for, e.g., connecting RNNs and convnets together. * * Example: * * ```js * const model = tf.sequential(); * model.add(tf.layers.permute({ * dims: [2, 1], * inputShape: [10, 64] * })); * console.log(model.outputShape); * // Now model's output shape is [null, 64, 10], where null is the * // unpermuted sample (batch) dimension. * ``` * * Input shape: * Arbitrary. Use the configuration field `inputShape` when using this * layer as the first layer in a model. * * Output shape: * Same rank as the input shape, but with the dimensions re-ordered (i.e., * permuted) according to the `dims` configuration of this layer. * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function permute(args) { return new Permute(args); } /** * Maps positive integers (indices) into dense vectors of fixed size. * E.g. [[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]`. * * @doc {heading: 'Layers', subheading: 'Basic', namespace: 'layers'} */ function embedding(args) { return new Embedding(args); } // Merge Layers. /** * Layer that performs element-wise addition on an `Array` of inputs. * * It takes as input a list of tensors, all of the same shape, and returns a * single tensor (also of the same shape). The inputs are specified as an * `Array` when the `apply` method of the `Add` layer instance is called. For * example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const addLayer = tf.layers.add(); * const sum = addLayer.apply([input1, input2]); * console.log(JSON.stringify(sum.shape)); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function add(args) { return new Add(args); } /** * Layer that performs element-wise averaging on an `Array` of inputs. * * It takes as input a list of tensors, all of the same shape, and returns a * single tensor (also of the same shape). For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 2]}); * const averageLayer = tf.layers.average(); * const average = averageLayer.apply([input1, input2]); * console.log(JSON.stringify(average.shape)); * // You get [null, 2, 2], with the first dimension as the undetermined batch * // dimension. * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function average(args) { return new Average(args); } /** * Layer that concatenates an `Array` of inputs. * * It takes a list of tensors, all of the same shape except for the * concatenation axis, and returns a single tensor, the concatenation * of all inputs. For example: * * ```js * const input1 = tf.input({shape: [2, 2]}); * const input2 = tf.input({shape: [2, 3]}); * const concatLayer = tf.layers.concatenate(); * const output = concatLayer.apply([input1, input2]); * console.log(JSON.stringify(output.shape)); * // You get [null, 2, 5], with the first dimension as the undetermined batch * // dimension. The last dimension (5) is the result of concatenating the * // last dimensions of the inputs (2 and 3). * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function concatenate(args) { return new Concatenate(args); } /** * Layer that computes the element-wise maximum 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: * * ```js * 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. * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function maximum(args) { return new Maximum(args); } /** * 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: * * ```js * 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. * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function minimum(args) { return new Minimum(args); } /** * 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: * * ```js * 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. * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function multiply(args) { return new Multiply(args); } /** * Layer that computes a dot product between samples in two tensors. * * E.g., if applied to a list of two tensors `a` and `b` both of shape * `[batchSize, n]`, the output will be a tensor of shape `[batchSize, 1]`, * where each entry at index `[i, 0]` will be the dot product between * `a[i, :]` and `b[i, :]`. * * Example: * * ```js * const dotLayer = tf.layers.dot({axes: -1}); * const x1 = tf.tensor2d([[10, 20], [30, 40]]); * const x2 = tf.tensor2d([[-1, -2], [-3, -4]]); * * // Invoke the layer's apply() method in eager (imperative) mode. * const y = dotLayer.apply([x1, x2]); * y.print(); * ``` * * @doc {heading: 'Layers', subheading: 'Merge', namespace: 'layers'} */ function dot(args) { return new Dot(args); } // Normalization Layers. /** * Batch normalization layer (Ioffe and Szegedy, 2014). * * Normalize the activations of the previous layer at each batch, * i.e. applies a transformation that maintains the mean activation * close to 0 and the activation standard deviation close to 1. * * Input shape: * Arbitrary. Use the keyword argument `inputShape` (Array of integers, does * not include the sample axis) when calling the constructor of this class, * if this layer is used as a first layer in a model. * * Output shape: * Same shape as input. * * References: * - [Batch Normalization: Accelerating Deep Network Training by Reducing * Internal Covariate Shift](https://arxiv.org/abs/1502.03167) * * @doc {heading: 'Layers', subheading: 'Normalization', namespace: 'layers'} */ function batchNormalization(args) { return new BatchNormalization(args); } /** * Layer-normalization layer (Ba et al., 2016). * * Normalizes the activations of the previous layer for each given example in a * batch independently, instead of across a batch like in `batchNormalization`. * In other words, this layer applies a transformation that maintains the mean * activation within each example close to 0 and activation variance close to 1. * * Input shape: * Arbitrary. Use the argument `inputShape` when using this layer as the first * layer in a model. * * Output shape: * Same as input. * * References: * - [Layer Normalization](https://arxiv.org/abs/1607.06450) * * @doc {heading: 'Layers', subheading: 'Normalization', namespace: 'layers'} */ function layerNormalization(args) { return new LayerNormalization(args); } // Padding Layers. /** * 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]`. * * @doc {heading: 'Layers', subheading: 'Padding', namespace: 'layers'} */ function zeroPadding2d(args) { return new ZeroPadding2D(args); } // Pooling Layers. /** * Average pooling operation for spatial data. * * Input shape: `[batchSize, inLength, channels]` * * Output shape: `[batchSize, pooledLength, channels]` * * `tf.avgPool1d` is an alias. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function averagePooling1d(args) { return new AveragePooling1D(args); } function avgPool1d(args) { return averagePooling1d(args); } // For backwards compatibility. // See https://github.com/tensorflow/tfjs/issues/152 function avgPooling1d(args) { return averagePooling1d(args); } /** * Average pooling operation for spatial data. * * Input shape: * - If `dataFormat === CHANNEL_LAST`: * 4D tensor with shape: * `[batchSize, rows, cols, channels]` * - If `dataFormat === CHANNEL_FIRST`: * 4D tensor with shape: * `[batchSize, channels, rows, cols]` * * Output shape * - If `dataFormat === CHANNEL_LAST`: * 4D tensor with shape: * `[batchSize, pooledRows, pooledCols, channels]` * - If `dataFormat === CHANNEL_FIRST`: * 4D tensor with shape: * `[batchSize, channels, pooledRows, pooledCols]` * * `tf.avgPool2d` is an alias. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function averagePooling2d(args) { return new AveragePooling2D(args); } function avgPool2d(args) { return averagePooling2d(args); } // For backwards compatibility. // See https://github.com/tensorflow/tfjs/issues/152 function avgPooling2d(args) { return averagePooling2d(args); } /** * Average pooling operation for 3D data. * * Input shape * - If `dataFormat === channelsLast`: * 5D tensor with shape: * `[batchSize, depths, rows, cols, channels]` * - If `dataFormat === channelsFirst`: * 4D tensor with shape: * `[batchSize, channels, depths, rows, cols]` * * Output shape * - If `dataFormat=channelsLast`: * 5D tensor with shape: * `[batchSize, pooledDepths, pooledRows, pooledCols, channels]` * - If `dataFormat=channelsFirst`: * 5D tensor with shape: * `[batchSize, channels, pooledDepths, pooledRows, pooledCols]` * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function averagePooling3d(args) { return new AveragePooling3D(args); } function avgPool3d(args) { return averagePooling3d(args); } // For backwards compatibility. // See https://github.com/tensorflow/tfjs/issues/152 function avgPooling3d(args) { return averagePooling3d(args); } /** * Global average pooling operation for temporal data. * * Input Shape: 3D tensor with shape: `[batchSize, steps, features]`. * * Output Shape: 2D tensor with shape: `[batchSize, features]`. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function globalAveragePooling1d(args) { return new GlobalAveragePooling1D(args); } /** * 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]`. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function globalAveragePooling2d(args) { return new GlobalAveragePooling2D(args); } /** * Global max pooling operation for temporal data. * * Input Shape: 3D tensor with shape: `[batchSize, steps, features]`. * * Output Shape: 2D tensor with shape: `[batchSize, features]`. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function globalMaxPooling1d(args) { return new GlobalMaxPooling1D(args); } /** * 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]`. * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function globalMaxPooling2d(args) { return new GlobalMaxPooling2D(args); } /** * Max pooling operation for temporal data. * * Input shape: `[batchSize, inLength, channels]` * * Output shape: `[batchSize, pooledLength, channels]` * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function maxPooling1d(args) { return new MaxPooling1D(args); } /** * 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, pooledRows, pooledCols, channels]` * - If `dataFormat=CHANNEL_FIRST`: * 4D tensor with shape: * `[batchSize, channels, pooledRows, pooledCols]` * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function maxPooling2d(args) { return new MaxPooling2D(args); } /** * Max pooling operation for 3D data. * * Input shape * - If `dataFormat === channelsLast`: * 5D tensor with shape: * `[batchSize, depths, rows, cols, channels]` * - If `dataFormat === channelsFirst`: * 5D tensor with shape: * `[batchSize, channels, depths, rows, cols]` * * Output shape * - If `dataFormat=channelsLast`: * 5D tensor with shape: * `[batchSize, pooledDepths, pooledRows, pooledCols, channels]` * - If `dataFormat=channelsFirst`: * 5D tensor with shape: * `[batchSize, channels, pooledDepths, pooledRows, pooledCols]` * * @doc {heading: 'Layers', subheading: 'Pooling', namespace: 'layers'} */ function maxPooling3d(args) { return new MaxPooling3D(args); } // Recurrent Layers. /** * 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: * * ```js * 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 `input`, due to `returnSequences`: `true`; * // 3rd dimension is the `GRUCell`'s number of units. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function gru(args) { return new GRU(args); } /** * 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: * * ```js * 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: * * ```js * 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 `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`. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function gruCell(args) { return new GRUCell(args); } /** * 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: * * ```js * 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 `input`, due to `returnSequences`: `true`; * // 3rd dimension is the `LSTMCell`'s number of units. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function lstm(args) { return new LSTM(args); } /** * 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: * * ```js * 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: * * ```js * 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 `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`. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function lstmCell(args) { return new LSTMCell(args); } /** * 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: * * ```js * 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 `input`, due to `returnSequences`: `true`; * // 3rd dimension is the `SimpleRNNCell`'s number of units. * ``` * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function simpleRNN(args) { return new SimpleRNN(args); } /** * 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: * * ```js * 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: * * ```js * 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 `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`. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function simpleRNNCell(args) { return new SimpleRNNCell(args); } /** * Convolutional LSTM layer - Xingjian Shi 2015. * * This is a `ConvRNN2D` layer consisting of one `ConvLSTM2DCell`. However, * unlike the underlying `ConvLSTM2DCell`, the `apply` method of `ConvLSTM2D` * operates on a sequence of inputs. The shape of the input (not including the * first, batch dimension) needs to be 4-D, with the first dimension being time * steps. For example: * * ```js * const filters = 3; * const kernelSize = 3; * * const batchSize = 4; * const sequenceLength = 2; * const size = 5; * const channels = 3; * * const inputShape = [batchSize, sequenceLength, size, size, channels]; * const input = tf.ones(inputShape); * * const layer = tf.layers.convLstm2d({filters, kernelSize}); * * const output = layer.apply(input); * ``` */ /** @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function convLstm2d(args) { return new ConvLSTM2D(args); } /** * Cell class for `ConvLSTM2D`. * * `ConvLSTM2DCell` is distinct from the `ConvRNN2D` subclass `ConvLSTM2D` in * that its `call` method takes the input data of only a single time step and * returns the cell's output at the time step, while `ConvLSTM2D` takes the * input data over a number of time steps. For example: * * ```js * const filters = 3; * const kernelSize = 3; * * const sequenceLength = 1; * const size = 5; * const channels = 3; * * const inputShape = [sequenceLength, size, size, channels]; * const input = tf.ones(inputShape); * * const cell = tf.layers.convLstm2dCell({filters, kernelSize}); * * cell.build(input.shape); * * const outputSize = size - kernelSize + 1; * const outShape = [sequenceLength, outputSize, outputSize, filters]; * * const initialH = tf.zeros(outShape); * const initialC = tf.zeros(outShape); * * const [o, h, c] = cell.call([input, initialH, initialC], {}); * ``` */ /** @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function convLstm2dCell(args) { return new ConvLSTM2DCell(args); } /** * 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.Tensor`s). 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 `.resetStates()` 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 condition the cell transformation on additional static * inputs (not changing over time), a.k.a. an attention mechanism. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function rnn(args) { return new RNN(args); } /** * Wrapper allowing a stack of RNN cells to behave as a single cell. * * Used to implement efficient stacked RNNs. * * @doc {heading: 'Layers', subheading: 'Recurrent', namespace: 'layers'} */ function stackedRNNCells(args) { return new StackedRNNCells(args); } // Wrapper Layers. /** @doc {heading: 'Layers', subheading: 'Wrapper', namespace: 'layers'} */ function bidirectional(args) { return new Bidirectional(args); } /** * 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: * * ```js * 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. * * ```js * 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)); * ``` * * @doc {heading: 'Layers', subheading: 'Wrapper', namespace: 'layers'} */ function timeDistributed(args) { return new TimeDistributed(args); } // Aliases for pooling. const globalMaxPool1d = globalMaxPooling1d; const globalMaxPool2d = globalMaxPooling2d; const maxPool1d = maxPooling1d; const maxPool2d = maxPooling2d; /** * Apply additive zero-centered Gaussian noise. * * As it is a regularization layer, it is only active at training time. * * This is useful to mitigate overfitting * (you could see it as a form of random data augmentation). * Gaussian Noise (GS) is a natural choice as corruption process * for real valued inputs. * * # Arguments * stddev: float, standard deviation of the noise distribution. * * # Input shape * Arbitrary. Use the keyword argument `input_shape` * (tuple of integers, does not include the samples axis) * when using this layer as the first layer in a model. * * # Output shape * Same shape as input. * * @doc {heading: 'Layers', subheading: 'Noise', namespace: 'layers'} */ function gaussianNoise(args) { return new GaussianNoise(args); } /** * Apply multiplicative 1-centered Gaussian noise. * * As it is a regularization layer, it is only active at training time. * * Arguments: * - `rate`: float, drop probability (as with `Dropout`). * The multiplicative noise will have * standard deviation `sqrt(rate / (1 - rate))`. * * Input shape: * Arbitrary. Use the keyword argument `inputShape` * (tuple of integers, does not include the samples axis) * when using this layer as the first layer in a model. * * Output shape: * Same shape as input. * * References: * - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting]( * http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf) * * @doc {heading: 'Layers', subheading: 'Noise', namespace: 'layers'} */ function gaussianDropout(args) { return new GaussianDropout(args); } /** * Applies Alpha Dropout to the input. * * As it is a regularization layer, it is only active at training time. * * Alpha Dropout is a `Dropout` that keeps mean and variance of inputs * to their original values, in order to ensure the self-normalizing property * even after this dropout. * Alpha Dropout fits well to Scaled Exponential Linear Units * by randomly setting activations to the negative saturation value. * * Arguments: * - `rate`: float, drop probability (as with `Dropout`). * The multiplicative noise will have * standard deviation `sqrt(rate / (1 - rate))`. * - `noise_shape`: A 1-D `Tensor` of type `int32`, representing the * shape for randomly generated keep/drop flags. * * Input shape: * Arbitrary. Use the keyword argument `inputShape` * (tuple of integers, does not include the samples axis) * when using this layer as the first layer in a model. * * Output shape: * Same shape as input. * * References: * - [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515) * * @doc {heading: 'Layers', subheading: 'Noise', namespace: 'layers'} */ function alphaDropout(args) { return new AlphaDropout(args); } /** * Masks a sequence by using a mask value to skip timesteps. * * If all features for a given sample timestep are equal to `mask_value`, * then the sample timestep will be masked (skipped) in all downstream layers * (as long as they support masking). * * If any downstream layer does not support masking yet receives such * an input mask, an exception will be raised. * * Arguments: * - `maskValue`: Either None or mask value to skip. * * Input shape: * Arbitrary. Use the keyword argument `inputShape` * (tuple of integers, does not include the samples axis) * when using this layer as the first layer in a model. * * Output shape: * Same shape as input. * * @doc {heading: 'Layers', subheading: 'Mask', namespace: 'layers'} */ function masking(args) { return new Masking(args); } /** * A preprocessing layer which rescales input values to a new range. * * This layer rescales every value of an input (often an image) by multiplying * by `scale` and adding `offset`. * * For instance: * 1. To rescale an input in the ``[0, 255]`` range * to be in the `[0, 1]` range, you would pass `scale=1/255`. * 2. To rescale an input in the ``[0, 255]`` range to be in the `[-1, 1]` * range, you would pass `scale=1./127.5, offset=-1`. * The rescaling is applied both during training and inference. Inputs can be * of integer or floating point dtype, and by default the layer will output * floats. * * Arguments: * - `scale`: Float, the scale to apply to the inputs. * - `offset`: Float, the offset to apply to the inputs. * * Input shape: * Arbitrary. * * Output shape: * Same as input. * * @doc {heading: 'Layers', subheading: 'Rescaling', namespace: 'layers'} */ function rescaling(args) { return new Rescaling(args); } /** * A preprocessing layer which center crops images. * * This layers crops the central portion of the images to a target size. If an * image is smaller than the target size, it will be resized and cropped so as * to return the largest possible window in the image that matches the target * aspect ratio. * * Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and * of integer or floating point dtype. * * If the input height/width is even and the target height/width is odd (or * inversely), the input image is left-padded by 1 pixel. * * Arguments: * `height`: Integer, the height of the output shape. * `width`: Integer, the width of the output shape. * * Input shape: * 3D (unbatched) or 4D (batched) tensor with shape: * `(..., height, width, channels)`, in `channelsLast` format. * * Output shape: * 3D (unbatched) or 4D (batched) tensor with shape: * `(..., targetHeight, targetWidth, channels)`. * * * @doc {heading: 'Layers', subheading: 'CenterCrop', namespace: 'layers'} */ function centerCrop(args) { return new CenterCrop(args); } /** * A preprocessing layer which resizes images. * This layer resizes an image input to a target height and width. The input * should be a 4D (batched) or 3D (unbatched) tensor in `"channels_last"` * format. Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, * 255]`) and of interger or floating point dtype. By default, the layer will * output floats. * * Arguments: * - `height`: number, the height for the output tensor. * - `width`: number, the width for the output tensor. * - `interpolation`: string, the method for image resizing interpolation. * - `cropToAspectRatio`: boolean, whether to keep image aspect ratio. * * Input shape: * Arbitrary. * * Output shape: * height, width, num channels. * * @doc {heading: 'Layers', subheading: 'Resizing', namespace: 'layers'} */ function resizing(args) { return new Resizing(args); } /** * A preprocessing layer which encodes integer features. * * This layer provides options for condensing data into a categorical encoding * when the total number of tokens are known in advance. It accepts integer * values as inputs, and it outputs a dense representation of those * inputs. * * Arguments: * * numTokens: The total number of tokens the layer should support. All * inputs to the layer must integers in the range `0 <= value < * numTokens`, or an error will be thrown. * * outputMode: Specification for the output of the layer. * Defaults to `multiHot`. Values can be `oneHot`, `multiHot` or * `count`, configuring the layer as follows: * * oneHot: Encodes each individual element in the input into an * array of `numTokens` size, containing a 1 at the element index. If * the last dimension is size 1, will encode on that dimension. If the * last dimension is not size 1, will append a new dimension for the * encoded output. * * multiHot: Encodes each sample in the input into a single array * of `numTokens` size, containing a 1 for each vocabulary term * present in the sample. Treats the last dimension as the sample * dimension, if input shape is `(..., sampleLength)`, output shape * will be `(..., numTokens)`. * * count: Like `multiHot`, but the int array contains a count of * the number of times the token at that index appeared in the sample. * * For all output modes, currently only output up to rank 2 is supported. * Call arguments: * inputs: A 1D or 2D tensor of integer inputs. * countWeights: A tensor in the same shape as `inputs` indicating the * weight for each sample value when summing up in `count` mode. Not used * in `multiHot` or `oneHot` modes. * * * @doc {heading: 'Layers', subheading: 'CategoryEncoding', namespace: 'layers'} */ function categoryEncoding(args) { return new CategoryEncoding(args); } /** * A preprocessing layer which randomly varies image width during training. * * This layer will randomly adjusts the width of a batch of images of a batch * of images by a random factor. * * The input should be a 3D (unbatched) or 4D (batched) tensor in * the `"channels_last"` image data format. Input pixel values can be of any * range (e.g. `[0., 1.)` or `[0, 255]`) and of integer or floating point * dtype. By default, the layer will output floats. By default, this layer is * inactive during inference. For an overview and full list of preprocessing * layers, see the preprocessing [guide] * (https://www.tensorflow.org/guide/keras/preprocessing_layers). * * Arguments: * * factor: * A positive float (fraction of original width), or a tuple of size 2 * representing lower and upper bound for resizing vertically. * When represented as a single float, this value is used for both the upper * and lower bound. For instance, `factor=(0.2, 0.3)` results in an output * with width changed by a random amount in the range `[20%, 30%]`. * `factor=(-0.2, 0.3)` results in an output with width changed by a random * amount in the range `[-20%, +30%]`. `factor=0.2` results in an output * with width changed by a random amount in the range `[-20%, +20%]`. * interpolation: * String, the interpolation method. * Defaults to `bilinear`. * Supports `"bilinear"`, `"nearest"`. * The tf methods `"bicubic"`, `"area"`, `"lanczos3"`, `"lanczos5"`, * `"gaussian"`, `"mitchellcubic"` are unimplemented in tfjs. * seed: * Integer. Used to create a random seed. * * Input shape: * 3D (unbatched) or 4D (batched) tensor with shape: * `(..., height, width, channels)`, in `"channels_last"` format. * Output shape: * 3D (unbatched) or 4D (batched) tensor with shape: * `(..., height, random_width, channels)`. * * * @doc {heading: 'Layers', subheading: 'RandomWidth', namespace: 'layers'} */ function randomWidth(args) { return new RandomWidth(args); } var exports_layers = { __proto__: null, Layer: Layer, RNN: RNN, RNNCell: RNNCell, activation: activation, add: add, alphaDropout: alphaDropout, average: average, averagePooling1d: averagePooling1d, averagePooling2d: averagePooling2d, averagePooling3d: averagePooling3d, avgPool1d: avgPool1d, avgPool2d: avgPool2d, avgPool3d: avgPool3d, avgPooling1d: avgPooling1d, avgPooling2d: avgPooling2d, avgPooling3d: avgPooling3d, batchNormalization: batchNormalization, bidirectional: bidirectional, categoryEncoding: categoryEncoding, centerCrop: centerCrop, concatenate: concatenate, conv1d: conv1d, conv2d: conv2d, conv2dTranspose: conv2dTranspose, conv3d: conv3d, conv3dTranspose: conv3dTranspose, convLstm2d: convLstm2d, convLstm2dCell: convLstm2dCell, cropping2D: cropping2D, dense: dense, depthwiseConv2d: depthwiseConv2d, dot: dot, dropout: dropout, elu: elu, embedding: embedding, flatten: flatten, gaussianDropout: gaussianDropout, gaussianNoise: gaussianNoise, globalAveragePooling1d: globalAveragePooling1d, globalAveragePooling2d: globalAveragePooling2d, globalMaxPool1d: globalMaxPool1d, globalMaxPool2d: globalMaxPool2d, globalMaxPooling1d: globalMaxPooling1d, globalMaxPooling2d: globalMaxPooling2d, gru: gru, gruCell: gruCell, input: input, inputLayer: inputLayer, layerNormalization: layerNormalization, leakyReLU: leakyReLU, lstm: lstm, lstmCell: lstmCell, masking: masking, maxPool1d: maxPool1d, maxPool2d: maxPool2d, maxPooling1d: maxPooling1d, maxPooling2d: maxPooling2d, maxPooling3d: maxPooling3d, maximum: maximum, minimum: minimum, multiply: multiply, permute: permute, prelu: prelu, randomWidth: randomWidth, reLU: reLU, repeatVector: repeatVector, rescaling: rescaling, reshape: reshape, resizing: resizing, rnn: rnn, separableConv2d: separableConv2d, simpleRNN: simpleRNN, simpleRNNCell: simpleRNNCell, softmax: softmax, spatialDropout1d: spatialDropout1d, stackedRNNCells: stackedRNNCells, thresholdedReLU: thresholdedReLU, timeDistributed: timeDistributed, upSampling2d: upSampling2d, zeroPadding2d: zeroPadding2d }; /** * Binary accuracy metric function. * * `yTrue` and `yPred` can have 0-1 values. Example: * ```js * const x = tf.tensor2d([[1, 1, 1, 1], [0, 0, 0, 0]], [2, 4]); * const y = tf.tensor2d([[1, 0, 1, 0], [0, 0, 0, 1]], [2, 4]); * const accuracy = tf.metrics.binaryAccuracy(x, y); * accuracy.print(); * ``` * * `yTrue` and `yPred` can also have floating-number values between 0 and 1, in * which case the values will be thresholded at 0.5 to yield 0-1 values (i.e., * a value >= 0.5 and <= 1.0 is interpreted as 1). * * Example: * ```js * const x = tf.tensor1d([1, 1, 1, 1, 0, 0, 0, 0]); * const y = tf.tensor1d([0.2, 0.4, 0.6, 0.8, 0.2, 0.3, 0.4, 0.7]); * const accuracy = tf.metrics.binaryAccuracy(x, y); * accuracy.print(); * ``` * * @param yTrue Binary Tensor of truth. * @param yPred Binary Tensor of prediction. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function binaryAccuracy(yTrue, yPred) { return binaryAccuracy$1(yTrue, yPred); } /** * Binary crossentropy metric function. * * Example: * ```js * const x = tf.tensor2d([[0], [1], [1], [1]]); * const y = tf.tensor2d([[0], [0], [0.5], [1]]); * const crossentropy = tf.metrics.binaryCrossentropy(x, y); * crossentropy.print(); * ``` * * @param yTrue Binary Tensor of truth. * @param yPred Binary Tensor of prediction, probabilities for the `1` case. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function binaryCrossentropy(yTrue, yPred) { return binaryCrossentropy$1(yTrue, yPred); } /** * Sparse categorical accuracy metric function. * * Example: * ```js * * const yTrue = tf.tensor1d([1, 1, 2, 2, 0]); * const yPred = tf.tensor2d( * [[0, 1, 0], [1, 0, 0], [0, 0.4, 0.6], [0, 0.6, 0.4], [0.7, 0.3, 0]]); * const crossentropy = tf.metrics.sparseCategoricalAccuracy(yTrue, yPred); * crossentropy.print(); * ``` * * @param yTrue True labels: indices. * @param yPred Predicted probabilities or logits. * @returns Accuracy tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function sparseCategoricalAccuracy(yTrue, yPred) { return sparseCategoricalAccuracy$1(yTrue, yPred); } /** * Categorical accuracy metric function. * * Example: * ```js * const x = tf.tensor2d([[0, 0, 0, 1], [0, 0, 0, 1]]); * const y = tf.tensor2d([[0.1, 0.8, 0.05, 0.05], [0.1, 0.05, 0.05, 0.8]]); * const accuracy = tf.metrics.categoricalAccuracy(x, y); * accuracy.print(); * ``` * * @param yTrue Binary Tensor of truth: one-hot encoding of categories. * @param yPred Binary Tensor of prediction: probabilities or logits for the * same categories as in `yTrue`. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function categoricalAccuracy(yTrue, yPred) { return categoricalAccuracy$1(yTrue, yPred); } /** * Categorical crossentropy between an output tensor and a target tensor. * * @param target A tensor of the same shape as `output`. * @param output A tensor resulting from a softmax (unless `fromLogits` is * `true`, in which case `output` is expected to be the logits). * @param fromLogits Boolean, whether `output` is the result of a softmax, or is * a tensor of logits. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function categoricalCrossentropy(yTrue, yPred) { return categoricalCrossentropy$1(yTrue, yPred); } /** * Computes the precision of the predictions with respect to the labels. * * Example: * ```js * const x = tf.tensor2d( * [ * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 0, 0, 1], * [1, 0, 0, 0], * [0, 0, 1, 0] * ] * ); * * const y = tf.tensor2d( * [ * [0, 0, 1, 0], * [0, 1, 0, 0], * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 1, 0, 0] * ] * ); * * const precision = tf.metrics.precision(x, y); * precision.print(); * ``` * * @param yTrue The ground truth values. Expected to contain only 0-1 values. * @param yPred The predicted values. Expected to contain only 0-1 values. * @return Precision Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function precision(yTrue, yPred) { return precision$1(yTrue, yPred); } /** * Computes the recall of the predictions with respect to the labels. * * Example: * ```js * const x = tf.tensor2d( * [ * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 0, 0, 1], * [1, 0, 0, 0], * [0, 0, 1, 0] * ] * ); * * const y = tf.tensor2d( * [ * [0, 0, 1, 0], * [0, 1, 0, 0], * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 1, 0, 0] * ] * ); * * const recall = tf.metrics.recall(x, y); * recall.print(); * ``` * * @param yTrue The ground truth values. Expected to contain only 0-1 values. * @param yPred The predicted values. Expected to contain only 0-1 values. * @return Recall Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function recall(yTrue, yPred) { return recall$1(yTrue, yPred); } /** * Loss or metric function: Cosine proximity. * * Mathematically, cosine proximity is defined as: * `-sum(l2Normalize(yTrue) * l2Normalize(yPred))`, * wherein `l2Normalize()` normalizes the L2 norm of the input to 1 and `*` * represents element-wise multiplication. * * ```js * const yTrue = tf.tensor2d([[1, 0], [1, 0]]); * const yPred = tf.tensor2d([[1 / Math.sqrt(2), 1 / Math.sqrt(2)], [0, 1]]); * const proximity = tf.metrics.cosineProximity(yTrue, yPred); * proximity.print(); * ``` * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Cosine proximity Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function cosineProximity(yTrue, yPred) { return cosineProximity$1(yTrue, yPred); } /** * Loss or metric function: Mean absolute error. * * Mathematically, mean absolute error is defined as: * `mean(abs(yPred - yTrue))`, * wherein the `mean` is applied over feature dimensions. * * ```js * const yTrue = tf.tensor2d([[0, 1], [0, 0], [2, 3]]); * const yPred = tf.tensor2d([[0, 1], [0, 1], [-2, -3]]); * const mse = tf.metrics.meanAbsoluteError(yTrue, yPred); * mse.print(); * ``` * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean absolute error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function meanAbsoluteError(yTrue, yPred) { return meanAbsoluteError$1(yTrue, yPred); } /** * Loss or metric function: Mean absolute percentage error. * * ```js * const yTrue = tf.tensor2d([[0, 1], [10, 20]]); * const yPred = tf.tensor2d([[0, 1], [11, 24]]); * const mse = tf.metrics.meanAbsolutePercentageError(yTrue, yPred); * mse.print(); * ``` * * Aliases: `tf.metrics.MAPE`, `tf.metrics.mape`. * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean absolute percentage error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function meanAbsolutePercentageError(yTrue, yPred) { return meanAbsolutePercentageError$1(yTrue, yPred); } function MAPE(yTrue, yPred) { return meanAbsolutePercentageError$1(yTrue, yPred); } function mape(yTrue, yPred) { return meanAbsolutePercentageError$1(yTrue, yPred); } /** * Loss or metric function: Mean squared error. * * ```js * const yTrue = tf.tensor2d([[0, 1], [3, 4]]); * const yPred = tf.tensor2d([[0, 1], [-3, -4]]); * const mse = tf.metrics.meanSquaredError(yTrue, yPred); * mse.print(); * ``` * * Aliases: `tf.metrics.MSE`, `tf.metrics.mse`. * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean squared error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ function meanSquaredError(yTrue, yPred) { return meanSquaredError$1(yTrue, yPred); } function MSE(yTrue, yPred) { return meanSquaredError$1(yTrue, yPred); } function mse(yTrue, yPred) { return meanSquaredError$1(yTrue, yPred); } var exports_metrics = { __proto__: null, MAPE: MAPE, MSE: MSE, binaryAccuracy: binaryAccuracy, binaryCrossentropy: binaryCrossentropy, categoricalAccuracy: categoricalAccuracy, categoricalCrossentropy: categoricalCrossentropy, cosineProximity: cosineProximity, mape: mape, meanAbsoluteError: meanAbsoluteError, meanAbsolutePercentageError: meanAbsolutePercentageError, meanSquaredError: meanSquaredError, mse: mse, precision: precision, recall: recall, sparseCategoricalAccuracy: sparseCategoricalAccuracy }; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ var exports_models = { __proto__: null, modelFromJSON: modelFromJSON }; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ /** * 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) * * @doc {heading: 'Regularizers', namespace: 'regularizers'} */ function l1l2(config) { return new L1L2(config); } /** * Regularizer for L1 regularization. * * Adds a term to the loss to penalize large weights: * loss += sum(l1 * abs(x)) * @param args l1 config. * * @doc {heading: 'Regularizers', namespace: 'regularizers'} */ function l1(config) { return l1$1(config); } /** * Regularizer for L2 regularization. * * Adds a term to the loss to penalize large weights: * loss += sum(l2 * x^2) * @param args l2 config. * * @doc {heading: 'Regularizers', namespace: 'regularizers'} */ function l2(config) { return l2$1(config); } var exports_regularizers = { __proto__: null, l1: l1, l1l2: l1l2, l2: l2 }; /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ class Callback extends BaseCallback { constructor() { super(...arguments); /** Instance of `keras.models.Model`. Reference of the model being trained. */ this.model = null; } setModel(model) { if (!(model instanceof LayersModel)) { throw new Error('model must be a LayersModel, not some other Container'); } this.model = model; } } function less(currVal, prevVal) { return currVal < prevVal; } function greater(currVal, prevVal) { return currVal > prevVal; } /** * A Callback that stops training when a monitored quantity has stopped * improving. */ class EarlyStopping extends Callback { constructor(args) { super(); if (args == null) { args = {}; } if (args.restoreBestWeights) { throw new NotImplementedError('restoreBestWeights = True is not implemented in EarlyStopping yet.'); } this.monitor = args.monitor || 'val_loss'; this.minDelta = Math.abs(args.minDelta || 0); this.patience = args.patience || 0; this.verbose = args.verbose || 0; this.mode = args.mode || 'auto'; this.baseline = args.baseline; if (['auto', 'min', 'max'].indexOf(this.mode) === -1) { console.warn(`EarlyStopping mode '${this.mode}' is invalid. ` + `Falling back to mode 'auto'.`); this.mode = 'auto'; } if (this.mode === 'min') { this.monitorFunc = less; } else if (this.mode === 'max') { this.monitorFunc = greater; } else { // For mode === 'auto'. if (this.monitor.indexOf('acc') !== -1) { this.monitorFunc = greater; } else { this.monitorFunc = less; } } if (this.monitorFunc === less) { this.minDelta *= -1; } } async onTrainBegin(logs) { this.wait = 0; this.stoppedEpoch = 0; if (this.baseline != null) { this.best = this.baseline; } else { this.best = this.monitorFunc === less ? Infinity : -Infinity; } } async onEpochEnd(epoch, logs) { await resolveScalarsInLogs(logs); const current = this.getMonitorValue(logs); if (current == null) { return; } if (this.monitorFunc(current - this.minDelta, this.best)) { this.best = current; this.wait = 0; // TODO(cais): Logic for restoreBestWeights. } else { this.wait++; if (this.wait >= this.patience) { this.stoppedEpoch = epoch; this.model.stopTraining = true; } // TODO(cais): Logic for restoreBestWeights. } } async onTrainEnd(logs) { if (this.stoppedEpoch > 0 && this.verbose) { console.log(`Epoch ${this.stoppedEpoch}: early stopping.`); } } getMonitorValue(logs) { if (logs == null) { logs = {}; } const monitorValue = logs[this.monitor]; if (monitorValue == null) { console.warn(`Metric for EarlyStopping ${this.monitor} is not available. ` + `Available metrics are: ${Object.keys(logs)}`); } return monitorValue; } } /** * Factory function for a Callback that stops training when a monitored * quantity has stopped improving. * * Early stopping is a type of regularization, and protects model against * overfitting. * * The following example based on fake data illustrates how this callback * can be used during `tf.LayersModel.fit()`: * * ```js * const model = tf.sequential(); * model.add(tf.layers.dense({ * units: 3, * activation: 'softmax', * kernelInitializer: 'ones', * inputShape: [2] * })); * const xs = tf.tensor2d([1, 2, 3, 4], [2, 2]); * const ys = tf.tensor2d([[1, 0, 0], [0, 1, 0]], [2, 3]); * const xsVal = tf.tensor2d([4, 3, 2, 1], [2, 2]); * const ysVal = tf.tensor2d([[0, 0, 1], [0, 1, 0]], [2, 3]); * model.compile( * {loss: 'categoricalCrossentropy', optimizer: 'sgd', metrics: ['acc']}); * * // Without the EarlyStopping callback, the val_acc value would be: * // 0.5, 0.5, 0.5, 0.5, ... * // With val_acc being monitored, training should stop after the 2nd epoch. * const history = await model.fit(xs, ys, { * epochs: 10, * validationData: [xsVal, ysVal], * callbacks: tf.callbacks.earlyStopping({monitor: 'val_acc'}) * }); * * // Expect to see a length-2 array. * console.log(history.history.val_acc); * ``` * * @doc { * heading: 'Callbacks', * namespace: 'callbacks' * } */ function earlyStopping(args) { return new EarlyStopping(args); } const callbacks = { earlyStopping }; export { Callback, CallbackList, CustomCallback, EarlyStopping, History, InputSpec, LayerVariable, LayersModel, RNN, Sequential, SymbolicTensor, callbacks, exports_constraints as constraints, exports_initializers as initializers, input, exports_layers as layers, loadLayersModel, exports_metrics as metrics, model, exports_models as models, registerCallbackConstructor, exports_regularizers as regularizers, sequential, version as version_layers }; //# sourceMappingURL=tf-layers.fesm.js.map