32602 lines
1.2 MiB
Executable File
32602 lines
1.2 MiB
Executable File
/**
|
|
* @license
|
|
* Copyright 2024 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 strict';
|
|
|
|
var tfc = require('@tensorflow/tfjs-core');
|
|
|
|
function _interopNamespaceDefault(e) {
|
|
var n = Object.create(null);
|
|
if (e) {
|
|
Object.keys(e).forEach(function (k) {
|
|
if (k !== 'default') {
|
|
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
Object.defineProperty(n, k, d.get ? d : {
|
|
enumerable: true,
|
|
get: function () { return e[k]; }
|
|
});
|
|
}
|
|
});
|
|
}
|
|
n.default = e;
|
|
return n;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
var tfc__namespace = /*#__PURE__*/_interopNamespaceDefault(tfc);
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var ENV$1 = tfc.env();
|
|
/** Whether to keep intermediate tensors. */
|
|
ENV$1.registerFlag('KEEP_INTERMEDIATE_TENSORS', function () { return false; }, function (debugValue) {
|
|
if (debugValue) {
|
|
console.warn('Keep intermediate tensors is ON. This will print the values of all ' +
|
|
'intermediate tensors during model inference. Not all models ' +
|
|
'support this mode. For details, check e2e/benchmarks/ ' +
|
|
'model_config.js. This significantly impacts performance.');
|
|
}
|
|
});
|
|
|
|
/******************************************************************************
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
***************************************************************************** */
|
|
/* global Reflect, Promise */
|
|
var extendStatics = function (d, b) {
|
|
extendStatics = Object.setPrototypeOf ||
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
function (d, b) { for (var p in b)
|
|
if (Object.prototype.hasOwnProperty.call(b, p))
|
|
d[p] = b[p]; };
|
|
return extendStatics(d, b);
|
|
};
|
|
function __extends(d, b) {
|
|
if (typeof b !== "function" && b !== null)
|
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
extendStatics(d, b);
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
}
|
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try {
|
|
step(generator.next(value));
|
|
}
|
|
catch (e) {
|
|
reject(e);
|
|
} }
|
|
function rejected(value) { try {
|
|
step(generator["throw"](value));
|
|
}
|
|
catch (e) {
|
|
reject(e);
|
|
} }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
}
|
|
function __generator(thisArg, body) {
|
|
var _ = { label: 0, sent: function () { if (t[0] & 1)
|
|
throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
|
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
function step(op) {
|
|
if (f)
|
|
throw new TypeError("Generator is already executing.");
|
|
while (_)
|
|
try {
|
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
|
|
return t;
|
|
if (y = 0, t)
|
|
op = [op[0] & 2, t.value];
|
|
switch (op[0]) {
|
|
case 0:
|
|
case 1:
|
|
t = op;
|
|
break;
|
|
case 4:
|
|
_.label++;
|
|
return { value: op[1], done: false };
|
|
case 5:
|
|
_.label++;
|
|
y = op[1];
|
|
op = [0];
|
|
continue;
|
|
case 7:
|
|
op = _.ops.pop();
|
|
_.trys.pop();
|
|
continue;
|
|
default:
|
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
_ = 0;
|
|
continue;
|
|
}
|
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
_.label = op[1];
|
|
break;
|
|
}
|
|
if (op[0] === 6 && _.label < t[1]) {
|
|
_.label = t[1];
|
|
t = op;
|
|
break;
|
|
}
|
|
if (t && _.label < t[2]) {
|
|
_.label = t[2];
|
|
_.ops.push(op);
|
|
break;
|
|
}
|
|
if (t[2])
|
|
_.ops.pop();
|
|
_.trys.pop();
|
|
continue;
|
|
}
|
|
op = body.call(thisArg, _);
|
|
}
|
|
catch (e) {
|
|
op = [6, e];
|
|
y = 0;
|
|
}
|
|
finally {
|
|
f = t = 0;
|
|
}
|
|
if (op[0] & 5)
|
|
throw op[1];
|
|
return { value: op[0] ? op[1] : void 0, done: true };
|
|
}
|
|
}
|
|
function __values(o) {
|
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
if (m)
|
|
return m.call(o);
|
|
if (o && typeof o.length === "number")
|
|
return {
|
|
next: function () {
|
|
if (o && i >= o.length)
|
|
o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
}
|
|
function __read(o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m)
|
|
return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
|
|
ar.push(r.value);
|
|
}
|
|
catch (error) {
|
|
e = { error: error };
|
|
}
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"]))
|
|
m.call(i);
|
|
}
|
|
finally {
|
|
if (e)
|
|
throw e.error;
|
|
}
|
|
}
|
|
return ar;
|
|
}
|
|
function __spreadArray(to, from, pack) {
|
|
if (pack || arguments.length === 2)
|
|
for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
if (ar || !(i in from)) {
|
|
if (!ar)
|
|
ar = Array.prototype.slice.call(from, 0, i);
|
|
ar[i] = from[i];
|
|
}
|
|
}
|
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
*
|
|
* =============================================================================
|
|
*/
|
|
/** DataType enum. */
|
|
var DataType;
|
|
(function (DataType) {
|
|
// These properties must be quoted since they are used by parseDtypeParam
|
|
// in tfjs-converter/src/operations/operation_mapper.ts to look up dtypes
|
|
// by string name. If they are not quoted, Closure will mangle their names.
|
|
// Not a legal value for DataType. Used to indicate a DataType field
|
|
// has not been set.
|
|
DataType[DataType["DT_INVALID"] = 0] = "DT_INVALID";
|
|
// Data types that all computation devices are expected to be
|
|
// capable to support.
|
|
DataType[DataType["DT_FLOAT"] = 1] = "DT_FLOAT";
|
|
DataType[DataType["DT_DOUBLE"] = 2] = "DT_DOUBLE";
|
|
DataType[DataType["DT_INT32"] = 3] = "DT_INT32";
|
|
DataType[DataType["DT_UINT8"] = 4] = "DT_UINT8";
|
|
DataType[DataType["DT_INT16"] = 5] = "DT_INT16";
|
|
DataType[DataType["DT_INT8"] = 6] = "DT_INT8";
|
|
DataType[DataType["DT_STRING"] = 7] = "DT_STRING";
|
|
DataType[DataType["DT_COMPLEX64"] = 8] = "DT_COMPLEX64";
|
|
DataType[DataType["DT_INT64"] = 9] = "DT_INT64";
|
|
DataType[DataType["DT_BOOL"] = 10] = "DT_BOOL";
|
|
DataType[DataType["DT_QINT8"] = 11] = "DT_QINT8";
|
|
DataType[DataType["DT_QUINT8"] = 12] = "DT_QUINT8";
|
|
DataType[DataType["DT_QINT32"] = 13] = "DT_QINT32";
|
|
DataType[DataType["DT_BFLOAT16"] = 14] = "DT_BFLOAT16";
|
|
DataType[DataType["DT_QINT16"] = 15] = "DT_QINT16";
|
|
DataType[DataType["DT_QUINT16"] = 16] = "DT_QUINT16";
|
|
DataType[DataType["DT_UINT16"] = 17] = "DT_UINT16";
|
|
DataType[DataType["DT_COMPLEX128"] = 18] = "DT_COMPLEX128";
|
|
DataType[DataType["DT_HALF"] = 19] = "DT_HALF";
|
|
DataType[DataType["DT_RESOURCE"] = 20] = "DT_RESOURCE";
|
|
DataType[DataType["DT_VARIANT"] = 21] = "DT_VARIANT";
|
|
DataType[DataType["DT_UINT32"] = 22] = "DT_UINT32";
|
|
DataType[DataType["DT_UINT64"] = 23] = "DT_UINT64";
|
|
// Do not use! These are only for parameters. Every enum above
|
|
// should have a corresponding value below (verified by types_test).
|
|
DataType[DataType["DT_FLOAT_REF"] = 101] = "DT_FLOAT_REF";
|
|
DataType[DataType["DT_DOUBLE_REF"] = 102] = "DT_DOUBLE_REF";
|
|
DataType[DataType["DT_INT32_REF"] = 103] = "DT_INT32_REF";
|
|
DataType[DataType["DT_UINT8_REF"] = 104] = "DT_UINT8_REF";
|
|
DataType[DataType["DT_INT16_REF"] = 105] = "DT_INT16_REF";
|
|
DataType[DataType["DT_INT8_REF"] = 106] = "DT_INT8_REF";
|
|
DataType[DataType["DT_STRING_REF"] = 107] = "DT_STRING_REF";
|
|
DataType[DataType["DT_COMPLEX64_REF"] = 108] = "DT_COMPLEX64_REF";
|
|
DataType[DataType["DT_INT64_REF"] = 109] = "DT_INT64_REF";
|
|
DataType[DataType["DT_BOOL_REF"] = 110] = "DT_BOOL_REF";
|
|
DataType[DataType["DT_QINT8_REF"] = 111] = "DT_QINT8_REF";
|
|
DataType[DataType["DT_QUINT8_REF"] = 112] = "DT_QUINT8_REF";
|
|
DataType[DataType["DT_QINT32_REF"] = 113] = "DT_QINT32_REF";
|
|
DataType[DataType["DT_BFLOAT16_REF"] = 114] = "DT_BFLOAT16_REF";
|
|
DataType[DataType["DT_QINT16_REF"] = 115] = "DT_QINT16_REF";
|
|
DataType[DataType["DT_QUINT16_REF"] = 116] = "DT_QUINT16_REF";
|
|
DataType[DataType["DT_UINT16_REF"] = 117] = "DT_UINT16_REF";
|
|
DataType[DataType["DT_COMPLEX128_REF"] = 118] = "DT_COMPLEX128_REF";
|
|
DataType[DataType["DT_HALF_REF"] = 119] = "DT_HALF_REF";
|
|
DataType[DataType["DT_RESOURCE_REF"] = 120] = "DT_RESOURCE_REF";
|
|
DataType[DataType["DT_VARIANT_REF"] = 121] = "DT_VARIANT_REF";
|
|
DataType[DataType["DT_UINT32_REF"] = 122] = "DT_UINT32_REF";
|
|
DataType[DataType["DT_UINT64_REF"] = 123] = "DT_UINT64_REF";
|
|
})(DataType || (DataType = {}));
|
|
var SaverDef;
|
|
(function (SaverDef) {
|
|
(function (CheckpointFormatVersion) {
|
|
CheckpointFormatVersion[CheckpointFormatVersion["LEGACY"] = 0] = "LEGACY";
|
|
CheckpointFormatVersion[CheckpointFormatVersion["V1"] = 1] = "V1";
|
|
CheckpointFormatVersion[CheckpointFormatVersion["V2"] = 2] = "V2";
|
|
})(SaverDef.CheckpointFormatVersion || (SaverDef.CheckpointFormatVersion = {}));
|
|
})(SaverDef || (SaverDef = {}));
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var CUSTOM_OPS = {};
|
|
/**
|
|
* Register an Op for graph model executor. This allows you to register
|
|
* TensorFlow custom op or override existing op.
|
|
*
|
|
* Here is an example of registering a new MatMul Op.
|
|
* ```js
|
|
* const customMatmul = (node) =>
|
|
* tf.matMul(
|
|
* node.inputs[0], node.inputs[1],
|
|
* node.attrs['transpose_a'], node.attrs['transpose_b']);
|
|
*
|
|
* tf.registerOp('MatMul', customMatmul);
|
|
* ```
|
|
* The inputs and attrs of the node object are based on the TensorFlow op
|
|
* registry.
|
|
*
|
|
* @param name The Tensorflow Op name.
|
|
* @param opFunc An op function which is called with the current graph node
|
|
* during execution and needs to return a tensor or a list of tensors. The node
|
|
* has the following attributes:
|
|
* - attr: A map from attribute name to its value
|
|
* - inputs: A list of input tensors
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Op Registry'}
|
|
*/
|
|
function registerOp(name, opFunc) {
|
|
var opMapper = {
|
|
tfOpName: name,
|
|
category: 'custom',
|
|
inputs: [],
|
|
attrs: [],
|
|
customExecutor: opFunc
|
|
};
|
|
CUSTOM_OPS[name] = opMapper;
|
|
}
|
|
/**
|
|
* Retrieve the OpMapper object for the registered op.
|
|
*
|
|
* @param name The Tensorflow Op name.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Op Registry'}
|
|
*/
|
|
function getRegisteredOp(name) {
|
|
return CUSTOM_OPS[name];
|
|
}
|
|
/**
|
|
* Deregister the Op for graph model executor.
|
|
*
|
|
* @param name The Tensorflow Op name.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Op Registry'}
|
|
*/
|
|
function deregisterOp(name) {
|
|
delete CUSTOM_OPS[name];
|
|
}
|
|
|
|
function getParamValue(paramName, node, tensorMap, context, resourceManager) {
|
|
var inputParam = node.inputParams[paramName];
|
|
if (inputParam && inputParam.inputIndexStart !== undefined) {
|
|
var start = inputParam.inputIndexStart;
|
|
var end = inputParam.inputIndexEnd === 0 ?
|
|
undefined :
|
|
(inputParam.inputIndexEnd === undefined ? start + 1 :
|
|
inputParam.inputIndexEnd);
|
|
var shiftedStart = start < 0 ? node.inputNames.length + start : start;
|
|
if (inputParam.type === 'tensor') {
|
|
return getTensor(node.inputNames[shiftedStart], tensorMap, context, resourceManager);
|
|
}
|
|
if (inputParam.type === 'tensors') {
|
|
// TODO(mattSoulanille): This filters out NoOp nodes during execution, but
|
|
// these should really never be in the execution graph in the first place.
|
|
// They're necessary for ordering the graph, but should not be visible
|
|
// during execution. Perhaps have different sets of children, one for
|
|
// control dependencies and another for real dependencies.
|
|
var inputs_1 = node.inputs.slice(start, end);
|
|
var inputNames = node.inputNames.slice(start, end)
|
|
.filter(function (_name, index) { var _a; return ((_a = inputs_1[index]) === null || _a === void 0 ? void 0 : _a.op) !== 'NoOp'; });
|
|
return inputNames.map(function (name) { return getTensor(name, tensorMap, context, resourceManager); });
|
|
}
|
|
var tensor = getTensor(node.inputNames[shiftedStart], tensorMap, context, resourceManager);
|
|
var data = tensor.dataSync();
|
|
return inputParam.type === 'number' ?
|
|
data[0] :
|
|
tfc.util.toNestedArray(tensor.shape, data);
|
|
}
|
|
var attrParam = node.attrParams[paramName];
|
|
return attrParam && attrParam.value;
|
|
}
|
|
/**
|
|
* Retrieve the tensor from tensorsMap based on input name.
|
|
* @param name Node input name
|
|
* @param tensorsMap Tensors map keyed by the node
|
|
* @param context contains tensors and information for running the current node.
|
|
* @param resourceManager Optional. Contains global resources of the model.
|
|
*/
|
|
function getTensor(name, tensorsMap, context, resourceManager) {
|
|
var _b = __read(parseNodeName(name, context), 2), nodeName = _b[0], index = _b[1];
|
|
if (resourceManager != null) {
|
|
var tensor = resourceManager.getHashTableHandleByName(nodeName);
|
|
if (tensor != null) {
|
|
return tensor;
|
|
}
|
|
}
|
|
var contextId = context.currentContextIds.find(function (contextId) {
|
|
return !!tensorsMap[getNodeNameWithContextId(nodeName, contextId)];
|
|
});
|
|
return contextId !== undefined ?
|
|
tensorsMap[getNodeNameWithContextId(nodeName, contextId)][index] :
|
|
undefined;
|
|
}
|
|
/**
|
|
* Retrieve the tensors based on input name for current context.
|
|
* @param name Node input name
|
|
* @param tensorsMap Tensors map keyed by the node
|
|
*/
|
|
function getTensorsForCurrentContext(name, tensorsMap, context) {
|
|
return tensorsMap[getNodeNameWithContextId(name, context.currentContextId)];
|
|
}
|
|
/**
|
|
* Returns the node name, outputName and index from the Node input name.
|
|
* @param inputName The input name of the node, in format of
|
|
* node_name:output_index, i.e. MatMul:0, if the output_index is not set, it is
|
|
* default to 0.
|
|
* If the input name contains output name i.e. StringSplit:indices:0, it will
|
|
* return ['StringSplit', 0, 'indices'].
|
|
*/
|
|
function getNodeNameAndIndex(inputName, context) {
|
|
var _b = __read(parseNodeName(inputName, context), 3), nodeName = _b[0], index = _b[1], outputName = _b[2];
|
|
return [
|
|
getNodeNameWithContextId(nodeName, context && context.currentContextId),
|
|
index, outputName
|
|
];
|
|
}
|
|
function getNodeNameWithContextId(name, contextId) {
|
|
return !!contextId ? "".concat(name, "-").concat(contextId) : name;
|
|
}
|
|
function parseNodeName(name, context) {
|
|
if (name === '') {
|
|
return ['', 0, undefined];
|
|
}
|
|
var isCacheEnabled = context != null && context.parseNodeNameCache != null;
|
|
if (isCacheEnabled) {
|
|
var cachedResult = context.parseNodeNameCache.get(name);
|
|
if (cachedResult != null) {
|
|
return cachedResult;
|
|
}
|
|
}
|
|
var parts = name.split(':');
|
|
var result;
|
|
if (parts.length === 1) {
|
|
result = [name, 0, undefined];
|
|
}
|
|
else {
|
|
var nodeName = parts[0];
|
|
var outputName = parts.length === 3 ? parts[1] : undefined;
|
|
var index = Number(parts[parts.length - 1]);
|
|
result = [nodeName, index, outputName];
|
|
}
|
|
if (isCacheEnabled) {
|
|
context.parseNodeNameCache.set(name, result);
|
|
}
|
|
return result;
|
|
}
|
|
function getPadding(node, tensorMap, context) {
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
if (pad === 'explicit') {
|
|
// This is 1d array, we need to convert it to 2d array
|
|
pad = getParamValue('explicitPaddings', node, tensorMap, context);
|
|
var explicitPadding = [[0, 0], [0, 0], [0, 0], [0, 0]];
|
|
for (var i = 0; i < 4; i++) {
|
|
explicitPadding[i][0] = pad[i * 2];
|
|
explicitPadding[i][1] = pad[i * 2 + 1];
|
|
}
|
|
return explicitPadding;
|
|
}
|
|
return pad;
|
|
}
|
|
/**
|
|
* Reuse the tensor if it is marked as keep, otherwise clone the tensor to
|
|
* avoid disposal. This is important for TensorArray and TensorList ops, since
|
|
* internally they use a tensor as the id for TensorArray and TensorList, and
|
|
* to simplify lookup, they also use Tensor.id as the key to the internal map.
|
|
* These id tensors have been marked as kept in the backend, we need avoid clone
|
|
* them in order to create new Tensor.id.
|
|
* @param tensor
|
|
*/
|
|
function cloneTensor(tensor) {
|
|
return tensor.kept ? tensor : tfc.clone(tensor);
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$i = [
|
|
{
|
|
'tfOpName': 'Add',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'AddV2',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'AddN',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BiasAdd',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sub',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'RealDiv',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Div',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'DivNoNan',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FloorDiv',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Mul',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Maximum',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Minimum',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Pow',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SquaredDifference',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Mod',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FloorMod',
|
|
'category': 'arithmetic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var arithmetic = {
|
|
__proto__: null,
|
|
json: json$i
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$h = [
|
|
{
|
|
'tfOpName': 'Abs',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Acos',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Asin',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Atan',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Atan2',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'y',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Ceil',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ClipByValue',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'clipValueMin',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'clipValueMax',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Complex',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'real',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'imag',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ComplexAbs',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Cos',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Cosh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Elu',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Exp',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Floor',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Log',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Imag',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'outputType',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Neg',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Real',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'outputType',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Prelu',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'alpha',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Relu',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Relu6',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Selu',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sigmoid',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sin',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sinh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sqrt',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Rsqrt',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Square',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Tan',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Tanh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sign',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Round',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Expm1',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Log1p',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Reciprocal',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Softplus',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Asinh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Acosh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Atanh',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Erf',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LeakyRelu',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'alpha',
|
|
'name': 'alpha',
|
|
'type': 'number',
|
|
'defaultValue': 0.2
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IsNan',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IsFinite',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IsInf',
|
|
'category': 'basic_math',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var basicMath = {
|
|
__proto__: null,
|
|
json: json$h
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$g = [
|
|
{
|
|
'tfOpName': 'EmptyTensorList',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'maxNumElements',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LoopCond',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'pred',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Switch',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'data',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'pred',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Merge',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Enter',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'frame_name',
|
|
'name': 'frameName',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'is_constant',
|
|
'name': 'isConstant',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Exit',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NextIteration',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'size',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'element_shape',
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'tfName': 'dynamic_size',
|
|
'name': 'dynamicSize',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'clear_after_read',
|
|
'name': 'clearAfterRead',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'identical_element_shapes',
|
|
'name': 'identicalElementShapes',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'tensor_array_name',
|
|
'name': 'name',
|
|
'type': 'string'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayWriteV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'index',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayReadV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'index',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayGatherV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'element_shape',
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayScatterV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayConcatV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'element_shape_except0',
|
|
'name': 'elementShapeExcept0',
|
|
'type': 'shape',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArraySplitV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'lengths',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArraySizeV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'flowIn',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorArrayCloseV3',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorArrayId',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StatelessIf',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'cond',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'then_branch',
|
|
'name': 'thenBranch',
|
|
'type': 'func'
|
|
},
|
|
{
|
|
'tfName': 'else_branch',
|
|
'name': 'elseBranch',
|
|
'type': 'func'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'If',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'cond',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'then_branch',
|
|
'name': 'thenBranch',
|
|
'type': 'func'
|
|
},
|
|
{
|
|
'tfName': 'else_branch',
|
|
'name': 'elseBranch',
|
|
'type': 'func'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StatelessWhile',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'cond',
|
|
'name': 'cond',
|
|
'type': 'func'
|
|
},
|
|
{
|
|
'tfName': 'body',
|
|
'name': 'body',
|
|
'type': 'func'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'While',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'cond',
|
|
'name': 'cond',
|
|
'type': 'func'
|
|
},
|
|
{
|
|
'tfName': 'body',
|
|
'name': 'body',
|
|
'type': 'func'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListScatter',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListScatterV2',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'numElements',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListGather',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListGetItem',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'index',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListSetItem',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'index',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListReserve',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'numElements',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListFromTensor',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListStack',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'num_elements',
|
|
'name': 'numElements',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListSplit',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'lengths',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListConcat',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_shape',
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListConcatV2',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_shape',
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListPopBack',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'elementShape',
|
|
'type': 'shape'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListPushBack',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'element_dtype',
|
|
'name': 'elementDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListLength',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorListResize',
|
|
'category': 'control',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensorListId',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'size',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var control = {
|
|
__proto__: null,
|
|
json: json$g
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$f = [
|
|
{
|
|
'tfOpName': 'AvgPool',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'ksize',
|
|
'name': 'kernelSize',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MaxPool',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'ksize',
|
|
'name': 'kernelSize',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': [],
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MaxPoolWithArgmax',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'ksize',
|
|
'name': 'kernelSize',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'include_batch_in_index',
|
|
'name': 'includeBatchInIndex',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'AvgPool3D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'ksize',
|
|
'name': 'kernelSize',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MaxPool3D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'ksize',
|
|
'name': 'kernelSize',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Conv1D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'stride',
|
|
'name': 'stride',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NWC'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'dilation',
|
|
'name': 'dilation',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Conv2D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'useCudnnOnGpu',
|
|
'name': 'useCudnnOnGpu',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': '_FusedConv2D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'num_args',
|
|
'name': 'numArgs',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'use_cudnn_on_gpu',
|
|
'name': 'useCudnnOnGpu',
|
|
'type': 'bool',
|
|
'defaultValue': true
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]',
|
|
'defaultValue': [
|
|
1,
|
|
1,
|
|
1,
|
|
1
|
|
]
|
|
},
|
|
{
|
|
'tfName': 'fused_ops',
|
|
'name': 'fusedOps',
|
|
'type': 'string[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'epsilon',
|
|
'name': 'epsilon',
|
|
'type': 'number',
|
|
'defaultValue': 0.0001
|
|
},
|
|
{
|
|
'tfName': 'leakyrelu_alpha',
|
|
'name': 'leakyreluAlpha',
|
|
'type': 'number',
|
|
'defaultValue': 0.2
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Conv2DBackpropInput',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 2,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 0,
|
|
'name': 'outputShape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'DepthwiseConv2d',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'input',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'DepthwiseConv2dNative',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'input',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FusedDepthwiseConv2dNative',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'num_args',
|
|
'name': 'numArgs',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]',
|
|
'defaultValue': [
|
|
1,
|
|
1,
|
|
1,
|
|
1
|
|
]
|
|
},
|
|
{
|
|
'tfName': 'fused_ops',
|
|
'name': 'fusedOps',
|
|
'type': 'string[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'explicit_paddings',
|
|
'name': 'explicitPaddings',
|
|
'type': 'number[]',
|
|
'defaultValue': []
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Conv3D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'defaultValue': 'NHWC'
|
|
},
|
|
{
|
|
'tfName': 'dilations',
|
|
'name': 'dilations',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Dilation2D',
|
|
'category': 'convolution',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'filter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'strides',
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'rates',
|
|
'name': 'dilations',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'padding',
|
|
'name': 'pad',
|
|
'type': 'string'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var convolution = {
|
|
__proto__: null,
|
|
json: json$f
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$e = [
|
|
{
|
|
'tfOpName': 'Fill',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'value',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LinSpace',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'start',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'stop',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'num',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'OneHot',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'depth',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'onValue',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'offValue',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'axis',
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Ones',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'OnesLike',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'RandomStandardNormal',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'seed',
|
|
'name': 'seed',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'seed2',
|
|
'name': 'seed2',
|
|
'type': 'number',
|
|
'defaultValue': 0,
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'T',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'RandomUniform',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'minval',
|
|
'name': 'minval',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'maxval',
|
|
'name': 'maxval',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
},
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'seed',
|
|
'name': 'seed',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'seed2',
|
|
'name': 'seed2',
|
|
'type': 'number',
|
|
'defaultValue': 0,
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'T',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'RandomUniformInt',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'minval',
|
|
'name': 'minval',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'maxval',
|
|
'name': 'maxval',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'seed',
|
|
'name': 'seed',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'seed2',
|
|
'name': 'seed2',
|
|
'type': 'number',
|
|
'defaultValue': 0,
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Range',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'start',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'stop',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'step',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'Tidx',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TruncatedNormal',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'means',
|
|
'name': 'mean',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'stddev',
|
|
'name': 'stdDev',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
},
|
|
{
|
|
'tfName': 'seed',
|
|
'name': 'seed',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'seed2',
|
|
'name': 'seed2',
|
|
'type': 'number',
|
|
'defaultValue': 0,
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'T',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Zeros',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ZerosLike',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Multinomial',
|
|
'category': 'creation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'logits',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'numSamples',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'seed',
|
|
'name': 'seed',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'seed2',
|
|
'name': 'seed2',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'output_dtype',
|
|
'name': 'output_dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var creation = {
|
|
__proto__: null,
|
|
json: json$e
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$d = [
|
|
{
|
|
'tfOpName': 'NonMaxSuppressionV2',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'boxes',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scores',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'maxOutputSize',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'iouThreshold',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NonMaxSuppressionV3',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'boxes',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scores',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'maxOutputSize',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'iouThreshold',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'scoreThreshold',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NonMaxSuppressionV4',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'boxes',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scores',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'maxOutputSize',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'iouThreshold',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'scoreThreshold',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'T_threshold',
|
|
'name': 'threshold',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'pad_to_max_output_size',
|
|
'name': 'padToMaxOutputSize',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NonMaxSuppressionV5',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'boxes',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scores',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'maxOutputSize',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'iouThreshold',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'scoreThreshold',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 5,
|
|
'name': 'softNmsSigma',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Where',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'condition',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ListDiff',
|
|
'category': 'dynamic',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'y',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var dynamic = {
|
|
__proto__: null,
|
|
json: json$d
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$c = [
|
|
{
|
|
'tfOpName': 'LowerBound',
|
|
'category': 'evaluation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'sortedSequence',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TopKV2',
|
|
'category': 'evaluation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'k',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'sorted',
|
|
'name': 'sorted',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'UpperBound',
|
|
'category': 'evaluation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'sortedSequence',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Unique',
|
|
'category': 'evaluation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'UniqueV2',
|
|
'category': 'evaluation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var evaluation = {
|
|
__proto__: null,
|
|
json: json$c
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$b = [
|
|
{
|
|
'tfOpName': 'PlaceholderWithDefault',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'default',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'shape',
|
|
'name': 'shape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Placeholder',
|
|
'category': 'graph',
|
|
'attrs': [
|
|
{
|
|
'tfName': 'shape',
|
|
'name': 'shape',
|
|
'type': 'shape'
|
|
},
|
|
{
|
|
'tfName': 'dtype',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Const',
|
|
'category': 'graph'
|
|
},
|
|
{
|
|
'tfOpName': 'Identity',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IdentityN',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'x',
|
|
'type': 'tensors'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Snapshot',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Rank',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Size',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Shape',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ShapeN',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'x',
|
|
'type': 'tensors'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Print',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'data',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'message',
|
|
'name': 'message',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'first_n',
|
|
'name': 'firstN',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'summarize',
|
|
'name': 'summarize',
|
|
'type': 'number',
|
|
'defaultValue': 3
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NoOp',
|
|
'category': 'graph',
|
|
'inputs': []
|
|
},
|
|
{
|
|
'tfOpName': 'StopGradient',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FakeQuantWithMinMaxVars',
|
|
'category': 'graph',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'min',
|
|
'name': 'min',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'max',
|
|
'name': 'max',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var graph = {
|
|
__proto__: null,
|
|
json: json$b
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$a = [
|
|
{
|
|
'tfOpName': 'HashTable',
|
|
'category': 'hash_table',
|
|
'inputs': [],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'shared_name',
|
|
'name': 'sharedName',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'use_node_name_sharing',
|
|
'name': 'useNodeNameSharing',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'key_dtype',
|
|
'name': 'keyDType',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'value_dtype',
|
|
'name': 'valueDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'HashTableV2',
|
|
'category': 'hash_table',
|
|
'inputs': [],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'shared_name',
|
|
'name': 'sharedName',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'use_node_name_sharing',
|
|
'name': 'useNodeNameSharing',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'key_dtype',
|
|
'name': 'keyDType',
|
|
'type': 'dtype'
|
|
},
|
|
{
|
|
'tfName': 'value_dtype',
|
|
'name': 'valueDType',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableImport',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'Tin',
|
|
'name': 'tIn',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'tOut',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableImportV2',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'Tin',
|
|
'name': 'tIn',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'tOut',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableFind',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'defaultValue',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'Tin',
|
|
'name': 'tIn',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'tOut',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableFindV2',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'defaultValue',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'Tin',
|
|
'name': 'tIn',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'Tout',
|
|
'name': 'tOut',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableSize',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LookupTableSizeV2',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'InitializeTable',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'InitializeTableV2',
|
|
'category': 'hash_table',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tableHandle',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'keys',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var hashTable = {
|
|
__proto__: null,
|
|
json: json$a
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$9 = [
|
|
{
|
|
'tfOpName': 'ResizeBilinear',
|
|
'category': 'image',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'images',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'size',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'align_corners',
|
|
'name': 'alignCorners',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'half_pixel_centers',
|
|
'name': 'halfPixelCenters',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ResizeNearestNeighbor',
|
|
'category': 'image',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'images',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'size',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'align_corners',
|
|
'name': 'alignCorners',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'half_pixel_centers',
|
|
'name': 'halfPixelCenters',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'CropAndResize',
|
|
'category': 'image',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'image',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'boxes',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'boxInd',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'cropSize',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'method',
|
|
'name': 'method',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'extrapolation_value',
|
|
'name': 'extrapolationValue',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ImageProjectiveTransformV3',
|
|
'category': 'image',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'images',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'transforms',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'outputShape',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'fillValue',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'interpolation',
|
|
'name': 'interpolation',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'fill_mode',
|
|
'name': 'fillMode',
|
|
'type': 'string'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var image$1 = {
|
|
__proto__: null,
|
|
json: json$9
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$8 = [
|
|
{
|
|
'tfOpName': 'Equal',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'NotEqual',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Greater',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'GreaterEqual',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Less',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LessEqual',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LogicalAnd',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LogicalNot',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LogicalOr',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Select',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'condition',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SelectV2',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'condition',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BitwiseAnd',
|
|
'category': 'logical',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'y',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var logical = {
|
|
__proto__: null,
|
|
json: json$8
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$7 = [
|
|
{
|
|
'tfOpName': '_FusedMatMul',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'end': 0,
|
|
'name': 'args',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'num_args',
|
|
'name': 'numArgs',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'fused_ops',
|
|
'name': 'fusedOps',
|
|
'type': 'string[]',
|
|
'defaultValue': []
|
|
},
|
|
{
|
|
'tfName': 'epsilon',
|
|
'name': 'epsilon',
|
|
'type': 'number',
|
|
'defaultValue': 0.0001
|
|
},
|
|
{
|
|
'tfName': 'transpose_a',
|
|
'name': 'transposeA',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'transpose_b',
|
|
'name': 'transposeB',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'leakyrelu_alpha',
|
|
'name': 'leakyreluAlpha',
|
|
'type': 'number',
|
|
'defaultValue': 0.2
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MatMul',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'transpose_a',
|
|
'name': 'transposeA',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'transpose_b',
|
|
'name': 'transposeB',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BatchMatMul',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'adj_x',
|
|
'name': 'transposeA',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'adj_y',
|
|
'name': 'transposeB',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BatchMatMulV2',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'b',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'adj_x',
|
|
'name': 'transposeA',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'adj_y',
|
|
'name': 'transposeB',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Transpose',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'perm',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Einsum',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'equation',
|
|
'name': 'equation',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'N',
|
|
'name': 'n',
|
|
'type': 'number',
|
|
'defaultValue': 2
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MatrixBandPart',
|
|
'category': 'matrices',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'a',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'numLower',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'numUpper',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var matrices = {
|
|
__proto__: null,
|
|
json: json$7
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$6 = [
|
|
{
|
|
'tfOpName': 'EuclideanNorm',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool',
|
|
'defaultValue': false
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FusedBatchNorm',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scale',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'offset',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'mean',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'variance',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'epsilon',
|
|
'name': 'epsilon',
|
|
'type': 'number',
|
|
'defaultValue': 0.001
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FusedBatchNormV2',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scale',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'offset',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'mean',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'variance',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'epsilon',
|
|
'name': 'epsilon',
|
|
'type': 'number',
|
|
'defaultValue': 0.001
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'FusedBatchNormV3',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'scale',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'offset',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'mean',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 4,
|
|
'name': 'variance',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'epsilon',
|
|
'name': 'epsilon',
|
|
'type': 'number',
|
|
'defaultValue': 0.001
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LRN',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'depth_radius',
|
|
'name': 'radius',
|
|
'type': 'number',
|
|
'defaultValue': 5
|
|
},
|
|
{
|
|
'tfName': 'bias',
|
|
'name': 'bias',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
},
|
|
{
|
|
'tfName': 'alpha',
|
|
'name': 'alpha',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
},
|
|
{
|
|
'tfName': 'beta',
|
|
'name': 'beta',
|
|
'type': 'number',
|
|
'defaultValue': 0.5
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Softmax',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'LogSoftmax',
|
|
'category': 'normalization',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var normalization = {
|
|
__proto__: null,
|
|
json: json$6
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$5 = [
|
|
{
|
|
'tfOpName': 'Bincount',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'size',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'weights',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'DenseBincount',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'size',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'weights',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'binary_output',
|
|
'name': 'binaryOutput',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Max',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Mean',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Min',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Sum',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'All',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Any',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ArgMax',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ArgMin',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Prod',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'keep_dims',
|
|
'name': 'keepDims',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Cumprod',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'exclusive',
|
|
'name': 'exclusive',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'reverse',
|
|
'name': 'reverse',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Cumsum',
|
|
'category': 'reduction',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'exclusive',
|
|
'name': 'exclusive',
|
|
'type': 'bool'
|
|
},
|
|
{
|
|
'tfName': 'reverse',
|
|
'name': 'reverse',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var reduction = {
|
|
__proto__: null,
|
|
json: json$5
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$4 = [
|
|
{
|
|
'tfOpName': 'ConcatV2',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': -1,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
},
|
|
{
|
|
'start': -1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'N',
|
|
'name': 'n',
|
|
'type': 'number',
|
|
'defaultValue': 2
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Concat',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 1,
|
|
'end': 0,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
},
|
|
{
|
|
'start': 0,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'N',
|
|
'name': 'n',
|
|
'type': 'number',
|
|
'defaultValue': 2
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'GatherV2',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'batch_dims',
|
|
'name': 'batchDims',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Gather',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'validate_indices',
|
|
'name': 'validateIndices',
|
|
'type': 'bool',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Reverse',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'dims',
|
|
'type': 'bool[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ReverseV2',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Slice',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'begin',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'size',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StridedSlice',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'begin',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'end',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'strides',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'begin_mask',
|
|
'name': 'beginMask',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'end_mask',
|
|
'name': 'endMask',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'new_axis_mask',
|
|
'name': 'newAxisMask',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'ellipsis_mask',
|
|
'name': 'ellipsisMask',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'shrink_axis_mask',
|
|
'name': 'shrinkAxisMask',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Pack',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'end': 0,
|
|
'name': 'tensors',
|
|
'type': 'tensors'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'axis',
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Unpack',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'axis',
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'tfName': 'num',
|
|
'name': 'num',
|
|
'type': 'number',
|
|
'defaultValue': 0,
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Tile',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'reps',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Split',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'num_split',
|
|
'name': 'numOrSizeSplits',
|
|
'type': 'number',
|
|
'defaultValue': 1
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SplitV',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'numOrSizeSplits',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'axis',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ScatterNd',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'GatherNd',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SparseToDense',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'sparseIndices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'outputShape',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'sparseValues',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'defaultValue',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'validate_indices',
|
|
'name': 'validateIndices',
|
|
'type': 'bool',
|
|
'defaultValue': false,
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'TensorScatterUpdate',
|
|
'category': 'slice_join',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'tensor',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var sliceJoin = {
|
|
__proto__: null,
|
|
json: json$4
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$3 = [
|
|
{
|
|
'tfOpName': 'SparseFillEmptyRows',
|
|
'category': 'sparse',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'values',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'denseShape',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 3,
|
|
'name': 'defaultValue',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SparseReshape',
|
|
'category': 'sparse',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'inputIndices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'inputShape',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'newShape',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'T',
|
|
'name': 'dtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SparseSegmentMean',
|
|
'category': 'sparse',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'data',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'segmentIds',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SparseSegmentSum',
|
|
'category': 'sparse',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'data',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'indices',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'segmentIds',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var sparse$1 = {
|
|
__proto__: null,
|
|
json: json$3
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$2 = [
|
|
{
|
|
'tfOpName': 'FFT',
|
|
'category': 'spectral',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IFFT',
|
|
'category': 'spectral',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'RFFT',
|
|
'category': 'spectral',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'fft_length',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'IRFFT',
|
|
'category': 'spectral',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'fft_length',
|
|
'type': 'number',
|
|
'notSupported': true
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var spectral$1 = {
|
|
__proto__: null,
|
|
json: json$2
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json$1 = [
|
|
{
|
|
'tfOpName': 'StaticRegexReplace',
|
|
'category': 'string',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'input',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'pattern',
|
|
'name': 'pattern',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'rewrite',
|
|
'name': 'rewrite',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'replace_global',
|
|
'name': 'replaceGlobal',
|
|
'type': 'bool'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StringNGrams',
|
|
'category': 'string',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'data',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'dataSplits',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'separator',
|
|
'name': 'separator',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'ngram_widths',
|
|
'name': 'nGramWidths',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'tfName': 'left_pad',
|
|
'name': 'leftPad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'right_pad',
|
|
'name': 'rightPad',
|
|
'type': 'string'
|
|
},
|
|
{
|
|
'tfName': 'pad_width',
|
|
'name': 'padWidth',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'preserve_short_sequences',
|
|
'name': 'preserveShortSequences',
|
|
'type': 'bool'
|
|
}
|
|
],
|
|
'outputs': [
|
|
'ngrams',
|
|
'ngrams_splits'
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StringSplit',
|
|
'category': 'string',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'input',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'delimiter',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'skip_empty',
|
|
'name': 'skipEmpty',
|
|
'type': 'bool'
|
|
}
|
|
],
|
|
'outputs': [
|
|
'indices',
|
|
'values',
|
|
'shape'
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'StringToHashBucketFast',
|
|
'category': 'string',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'input',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'num_buckets',
|
|
'name': 'numBuckets',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
var string$1 = {
|
|
__proto__: null,
|
|
json: json$1
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var json = [
|
|
{
|
|
'tfOpName': 'Cast',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'SrcT',
|
|
'name': 'sdtype',
|
|
'type': 'dtype',
|
|
'notSupported': true
|
|
},
|
|
{
|
|
'tfName': 'DstT',
|
|
'name': 'dtype',
|
|
'type': 'dtype'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'ExpandDims',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'axis',
|
|
'type': 'number'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'MirrorPad',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'padding',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'mode',
|
|
'name': 'mode',
|
|
'type': 'string'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Pad',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'padding',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'constant_value',
|
|
'name': 'constantValue',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'PadV2',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'padding',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'constantValue',
|
|
'type': 'number',
|
|
'defaultValue': 0
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Reshape',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'EnsureShape',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'Squeeze',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'axis',
|
|
'tfDeprecatedName': 'squeeze_dims',
|
|
'name': 'axis',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'SpaceToBatchND',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'blockShape',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'paddings',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BatchToSpaceND',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'blockShape',
|
|
'type': 'number[]'
|
|
},
|
|
{
|
|
'start': 2,
|
|
'name': 'crops',
|
|
'type': 'number[]'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'DepthToSpace',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': [
|
|
{
|
|
'tfName': 'block_size',
|
|
'name': 'blockSize',
|
|
'type': 'number'
|
|
},
|
|
{
|
|
'tfName': 'data_format',
|
|
'name': 'dataFormat',
|
|
'type': 'string'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'tfOpName': 'BroadcastTo',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 'x',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 'shape',
|
|
'type': 'number[]'
|
|
}
|
|
],
|
|
'attrs': []
|
|
},
|
|
{
|
|
'tfOpName': 'BroadcastArgs',
|
|
'category': 'transformation',
|
|
'inputs': [
|
|
{
|
|
'start': 0,
|
|
'name': 's0',
|
|
'type': 'tensor'
|
|
},
|
|
{
|
|
'start': 1,
|
|
'name': 's1',
|
|
'type': 'tensor'
|
|
}
|
|
],
|
|
'attrs': []
|
|
}
|
|
];
|
|
|
|
var transformation = {
|
|
__proto__: null,
|
|
json: json
|
|
};
|
|
|
|
var OperationMapper = /** @class */ (function () {
|
|
// Loads the op mapping from the JSON file.
|
|
function OperationMapper() {
|
|
var ops = [
|
|
arithmetic, basicMath, control, convolution, creation, dynamic,
|
|
evaluation, graph, hashTable, image$1, logical, matrices, normalization,
|
|
reduction, sliceJoin, sparse$1, spectral$1, string$1, transformation
|
|
];
|
|
var mappersJson = [].concat.apply([], __spreadArray([], __read(ops.map(function (op) { return op.json; })), false));
|
|
this.opMappers = mappersJson.reduce(function (map, mapper) {
|
|
map[mapper.tfOpName] = mapper;
|
|
return map;
|
|
}, {});
|
|
}
|
|
Object.defineProperty(OperationMapper, "Instance", {
|
|
// Singleton instance for the mapper
|
|
get: function () {
|
|
return this._instance || (this._instance = new this());
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
// Converts the model inference graph from Tensorflow GraphDef to local
|
|
// representation for TensorFlow.js API
|
|
OperationMapper.prototype.transformGraph = function (graph, signature) {
|
|
var _this = this;
|
|
if (signature === void 0) { signature = {}; }
|
|
var tfNodes = graph.node;
|
|
var placeholders = [];
|
|
var weights = [];
|
|
var initNodes = [];
|
|
var nodes = tfNodes.reduce(function (map, node) {
|
|
map[node.name] = _this.mapNode(node);
|
|
if (node.op.startsWith('Placeholder')) {
|
|
placeholders.push(map[node.name]);
|
|
}
|
|
else if (node.op === 'Const') {
|
|
weights.push(map[node.name]);
|
|
}
|
|
else if (node.input == null || node.input.length === 0) {
|
|
initNodes.push(map[node.name]);
|
|
}
|
|
return map;
|
|
}, {});
|
|
var inputs = [];
|
|
var outputs = [];
|
|
var inputNodeNameToKey = {};
|
|
var outputNodeNameToKey = {};
|
|
if (signature != null) {
|
|
inputNodeNameToKey = this.mapSignatureEntries(signature.inputs);
|
|
outputNodeNameToKey = this.mapSignatureEntries(signature.outputs);
|
|
}
|
|
var allNodes = Object.keys(nodes);
|
|
allNodes.forEach(function (key) {
|
|
var node = nodes[key];
|
|
node.inputNames.forEach(function (name, index) {
|
|
var _a = __read(getNodeNameAndIndex(name), 3), nodeName = _a[0], outputName = _a[2];
|
|
var inputNode = nodes[nodeName];
|
|
if (inputNode.outputs != null) {
|
|
var outputIndex = inputNode.outputs.indexOf(outputName);
|
|
if (outputIndex !== -1) {
|
|
var inputName = "".concat(nodeName, ":").concat(outputIndex);
|
|
// update the input name to use the mapped output index directly.
|
|
node.inputNames[index] = inputName;
|
|
}
|
|
}
|
|
node.inputs.push(inputNode);
|
|
inputNode.children.push(node);
|
|
});
|
|
});
|
|
// if signature has not outputs set, add any node that does not have
|
|
// outputs.
|
|
if (Object.keys(outputNodeNameToKey).length === 0) {
|
|
allNodes.forEach(function (key) {
|
|
var node = nodes[key];
|
|
if (node.children.length === 0) {
|
|
outputs.push(node);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
Object.keys(outputNodeNameToKey).forEach(function (name) {
|
|
var _a = __read(getNodeNameAndIndex(name), 1), nodeName = _a[0];
|
|
var node = nodes[nodeName];
|
|
if (node != null) {
|
|
node.signatureKey = outputNodeNameToKey[name];
|
|
outputs.push(node);
|
|
}
|
|
});
|
|
}
|
|
if (Object.keys(inputNodeNameToKey).length > 0) {
|
|
Object.keys(inputNodeNameToKey).forEach(function (name) {
|
|
var _a = __read(getNodeNameAndIndex(name), 1), nodeName = _a[0];
|
|
var node = nodes[nodeName];
|
|
if (node) {
|
|
node.signatureKey = inputNodeNameToKey[name];
|
|
inputs.push(node);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
inputs = placeholders;
|
|
}
|
|
var functions = {};
|
|
if (graph.library != null && graph.library.function != null) {
|
|
functions = graph.library.function.reduce(function (functions, func) {
|
|
functions[func.signature.name] = _this.mapFunction(func);
|
|
return functions;
|
|
}, {});
|
|
}
|
|
var result = { nodes: nodes, inputs: inputs, outputs: outputs, weights: weights, placeholders: placeholders, signature: signature, functions: functions };
|
|
if (initNodes.length > 0) {
|
|
result.initNodes = initNodes;
|
|
}
|
|
return result;
|
|
};
|
|
OperationMapper.prototype.mapSignatureEntries = function (entries) {
|
|
return Object.keys(entries || {})
|
|
.reduce(function (prev, curr) {
|
|
prev[entries[curr].name] = curr;
|
|
return prev;
|
|
}, {});
|
|
};
|
|
OperationMapper.prototype.mapNode = function (node) {
|
|
// Unsupported ops will cause an error at run-time (not parse time), since
|
|
// they may not be used by the actual execution subgraph.
|
|
var mapper = getRegisteredOp(node.op) || this.opMappers[node.op] || {};
|
|
if (node.attr == null) {
|
|
node.attr = {};
|
|
}
|
|
var newNode = {
|
|
name: node.name,
|
|
op: node.op,
|
|
category: mapper.category,
|
|
inputNames: (node.input ||
|
|
[]).map(function (input) { return input.startsWith('^') ? input.slice(1) : input; }),
|
|
inputs: [],
|
|
children: [],
|
|
inputParams: {},
|
|
attrParams: {},
|
|
rawAttrs: node.attr,
|
|
outputs: mapper.outputs
|
|
};
|
|
if (mapper.inputs != null) {
|
|
newNode.inputParams =
|
|
mapper.inputs.reduce(function (map, param) {
|
|
map[param.name] = {
|
|
type: param.type,
|
|
inputIndexStart: param.start,
|
|
inputIndexEnd: param.end
|
|
};
|
|
return map;
|
|
}, {});
|
|
}
|
|
if (mapper.attrs != null) {
|
|
newNode.attrParams =
|
|
mapper.attrs.reduce(function (map, param) {
|
|
var type = param.type;
|
|
var value = undefined;
|
|
switch (param.type) {
|
|
case 'string':
|
|
value = getStringParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getStringParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'string[]':
|
|
value = getStringArrayParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getStringArrayParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'number':
|
|
value = getNumberParam(node.attr, param.tfName, (param.defaultValue || 0));
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getNumberParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'number[]':
|
|
value = getNumericArrayParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getNumericArrayParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'bool':
|
|
value = getBoolParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getBoolParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'bool[]':
|
|
value = getBoolArrayParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getBoolArrayParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'shape':
|
|
value = getTensorShapeParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getTensorShapeParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'shape[]':
|
|
value = getTensorShapeArrayParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getTensorShapeArrayParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'dtype':
|
|
value = getDtypeParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getDtypeParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'dtype[]':
|
|
value = getDtypeArrayParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getDtypeArrayParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'func':
|
|
value = getFuncParam(node.attr, param.tfName, param.defaultValue);
|
|
if (value === undefined && !!param.tfDeprecatedName) {
|
|
value = getFuncParam(node.attr, param.tfDeprecatedName, param.defaultValue);
|
|
}
|
|
break;
|
|
case 'tensor':
|
|
case 'tensors':
|
|
break;
|
|
default:
|
|
throw new Error("Unsupported param type: ".concat(param.type, " for op: ").concat(node.op));
|
|
}
|
|
map[param.name] = { value: value, type: type };
|
|
return map;
|
|
}, {});
|
|
}
|
|
return newNode;
|
|
};
|
|
// map the TFunctionDef to TFJS graph object
|
|
OperationMapper.prototype.mapFunction = function (functionDef) {
|
|
var _this = this;
|
|
var tfNodes = functionDef.nodeDef;
|
|
var placeholders = [];
|
|
var weights = [];
|
|
var nodes = {};
|
|
if (tfNodes != null) {
|
|
nodes = tfNodes.reduce(function (map, node) {
|
|
map[node.name] = _this.mapNode(node);
|
|
if (node.op === 'Const') {
|
|
weights.push(map[node.name]);
|
|
}
|
|
return map;
|
|
}, {});
|
|
}
|
|
var inputs = [];
|
|
var outputs = [];
|
|
functionDef.signature.inputArg.forEach(function (arg) {
|
|
var _a = __read(getNodeNameAndIndex(arg.name), 1), nodeName = _a[0];
|
|
var node = {
|
|
name: nodeName,
|
|
op: 'Placeholder',
|
|
inputs: [],
|
|
inputNames: [],
|
|
category: 'graph',
|
|
inputParams: {},
|
|
attrParams: { dtype: { value: parseDtypeParam(arg.type), type: 'dtype' } },
|
|
children: []
|
|
};
|
|
node.signatureKey = arg.name;
|
|
inputs.push(node);
|
|
nodes[nodeName] = node;
|
|
});
|
|
var allNodes = Object.keys(nodes);
|
|
allNodes.forEach(function (key) {
|
|
var node = nodes[key];
|
|
node.inputNames.forEach(function (name, index) {
|
|
var _a = __read(getNodeNameAndIndex(name), 3), nodeName = _a[0], outputName = _a[2];
|
|
var inputNode = nodes[nodeName];
|
|
if (inputNode.outputs != null) {
|
|
var outputIndex = inputNode.outputs.indexOf(outputName);
|
|
if (outputIndex !== -1) {
|
|
var inputName = "".concat(nodeName, ":").concat(outputIndex);
|
|
// update the input name to use the mapped output index directly.
|
|
node.inputNames[index] = inputName;
|
|
}
|
|
}
|
|
node.inputs.push(inputNode);
|
|
inputNode.children.push(node);
|
|
});
|
|
});
|
|
var returnNodeMap = functionDef.ret;
|
|
functionDef.signature.outputArg.forEach(function (output) {
|
|
var _a = __read(getNodeNameAndIndex(returnNodeMap[output.name]), 2), nodeName = _a[0], index = _a[1];
|
|
var node = nodes[nodeName];
|
|
if (node != null) {
|
|
node.defaultOutput = index;
|
|
outputs.push(node);
|
|
}
|
|
});
|
|
var signature = this.mapArgsToSignature(functionDef);
|
|
return { nodes: nodes, inputs: inputs, outputs: outputs, weights: weights, placeholders: placeholders, signature: signature };
|
|
};
|
|
OperationMapper.prototype.mapArgsToSignature = function (functionDef) {
|
|
var _this = this;
|
|
return {
|
|
methodName: functionDef.signature.name,
|
|
inputs: functionDef.signature.inputArg.reduce(function (map, arg) {
|
|
map[arg.name] = _this.mapArgToTensorInfo(arg);
|
|
return map;
|
|
}, {}),
|
|
outputs: functionDef.signature.outputArg.reduce(function (map, arg) {
|
|
map[arg.name] = _this.mapArgToTensorInfo(arg, functionDef.ret);
|
|
return map;
|
|
}, {}),
|
|
};
|
|
};
|
|
OperationMapper.prototype.mapArgToTensorInfo = function (arg, nameMap) {
|
|
var name = arg.name;
|
|
if (nameMap != null) {
|
|
name = nameMap[name];
|
|
}
|
|
return { name: name, dtype: arg.type };
|
|
};
|
|
return OperationMapper;
|
|
}());
|
|
function decodeBase64(text) {
|
|
var global = tfc.env().global;
|
|
if (typeof global.atob !== 'undefined') {
|
|
return global.atob(text);
|
|
}
|
|
else if (typeof Buffer !== 'undefined') {
|
|
return new Buffer(text, 'base64').toString();
|
|
}
|
|
else {
|
|
throw new Error('Unable to decode base64 in this environment. ' +
|
|
'Missing built-in atob() or Buffer()');
|
|
}
|
|
}
|
|
function parseStringParam(s, keepCase) {
|
|
var value = Array.isArray(s) ? String.fromCharCode.apply(null, s) : decodeBase64(s);
|
|
return keepCase ? value : value.toLowerCase();
|
|
}
|
|
function getStringParam(attrs, name, def, keepCase) {
|
|
if (keepCase === void 0) { keepCase = false; }
|
|
var param = attrs[name];
|
|
if (param != null) {
|
|
return parseStringParam(param.s, keepCase);
|
|
}
|
|
return def;
|
|
}
|
|
function getBoolParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
return param ? param.b : def;
|
|
}
|
|
function getNumberParam(attrs, name, def) {
|
|
var param = attrs[name] || {};
|
|
var value = param['i'] != null ? param['i'] : (param['f'] != null ? param['f'] : def);
|
|
return (typeof value === 'number') ? value : parseInt(value, 10);
|
|
}
|
|
function parseDtypeParam(value) {
|
|
if (typeof (value) === 'string') {
|
|
// tslint:disable-next-line:no-any
|
|
value = DataType[value];
|
|
}
|
|
switch (value) {
|
|
case DataType.DT_FLOAT:
|
|
case DataType.DT_HALF:
|
|
return 'float32';
|
|
case DataType.DT_INT32:
|
|
case DataType.DT_INT64:
|
|
case DataType.DT_INT8:
|
|
case DataType.DT_UINT8:
|
|
return 'int32';
|
|
case DataType.DT_BOOL:
|
|
return 'bool';
|
|
case DataType.DT_DOUBLE:
|
|
return 'float32';
|
|
case DataType.DT_STRING:
|
|
return 'string';
|
|
case DataType.DT_COMPLEX64:
|
|
case DataType.DT_COMPLEX128:
|
|
return 'complex64';
|
|
default:
|
|
// Unknown dtype error will happen at runtime (instead of parse time),
|
|
// since these nodes might not be used by the actual subgraph execution.
|
|
return null;
|
|
}
|
|
}
|
|
function getFuncParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.func) {
|
|
return param.func.name;
|
|
}
|
|
return def;
|
|
}
|
|
function getDtypeParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.type) {
|
|
return parseDtypeParam(param.type);
|
|
}
|
|
return def;
|
|
}
|
|
function getDtypeArrayParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.list && param.list.type) {
|
|
return param.list.type.map(function (v) { return parseDtypeParam(v); });
|
|
}
|
|
return def;
|
|
}
|
|
function parseTensorShapeParam(shape) {
|
|
if (shape.unknownRank) {
|
|
return undefined;
|
|
}
|
|
if (shape.dim != null) {
|
|
return shape.dim.map(function (dim) { return (typeof dim.size === 'number') ? dim.size : parseInt(dim.size, 10); });
|
|
}
|
|
return [];
|
|
}
|
|
function getTensorShapeParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.shape) {
|
|
return parseTensorShapeParam(param.shape);
|
|
}
|
|
return def;
|
|
}
|
|
function getNumericArrayParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param) {
|
|
return ((param.list.f && param.list.f.length ? param.list.f :
|
|
param.list.i) ||
|
|
[])
|
|
.map(function (v) { return (typeof v === 'number') ? v : parseInt(v, 10); });
|
|
}
|
|
return def;
|
|
}
|
|
function getStringArrayParam(attrs, name, def, keepCase) {
|
|
if (keepCase === void 0) { keepCase = false; }
|
|
var param = attrs[name];
|
|
if (param && param.list && param.list.s) {
|
|
return param.list.s.map(function (v) {
|
|
return parseStringParam(v, keepCase);
|
|
});
|
|
}
|
|
return def;
|
|
}
|
|
function getTensorShapeArrayParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.list && param.list.shape) {
|
|
return param.list.shape.map(function (v) {
|
|
return parseTensorShapeParam(v);
|
|
});
|
|
}
|
|
return def;
|
|
}
|
|
function getBoolArrayParam(attrs, name, def) {
|
|
var param = attrs[name];
|
|
if (param && param.list && param.list.b) {
|
|
return param.list.b;
|
|
}
|
|
return def;
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Helper class for lookup inputs and params for nodes in the model graph.
|
|
*/
|
|
var NodeValueImpl = /** @class */ (function () {
|
|
function NodeValueImpl(node, tensorMap, context) {
|
|
var _this = this;
|
|
this.node = node;
|
|
this.tensorMap = tensorMap;
|
|
this.context = context;
|
|
this.inputs = [];
|
|
this.attrs = {};
|
|
this.inputs = node.inputNames.map(function (name) { return _this.getInput(name); });
|
|
if (node.rawAttrs != null) {
|
|
this.attrs = Object.keys(node.rawAttrs)
|
|
.reduce(function (attrs, key) {
|
|
attrs[key] = _this.getAttr(key);
|
|
return attrs;
|
|
}, {});
|
|
}
|
|
}
|
|
/**
|
|
* Return the value of the attribute or input param.
|
|
* @param name String: name of attribute or input param.
|
|
*/
|
|
NodeValueImpl.prototype.getInput = function (name) {
|
|
return getTensor(name, this.tensorMap, this.context);
|
|
};
|
|
/**
|
|
* Return the value of the attribute or input param.
|
|
* @param name String: name of attribute or input param.
|
|
*/
|
|
NodeValueImpl.prototype.getAttr = function (name, defaultValue) {
|
|
var value = this.node.rawAttrs[name];
|
|
if (value.tensor != null) {
|
|
return getTensor(name, this.tensorMap, this.context);
|
|
}
|
|
if (value.i != null || value.f != null) {
|
|
return getNumberParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.s != null) {
|
|
return getStringParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.b != null) {
|
|
return getBoolParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.shape != null) {
|
|
return getTensorShapeParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.type != null) {
|
|
return getDtypeParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.list != null) {
|
|
if (value.list.i != null || value.list.f != null) {
|
|
return getNumericArrayParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.list.s != null) {
|
|
return getStringArrayParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.list.shape != null) {
|
|
return getTensorShapeArrayParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.list.b != null) {
|
|
return getBoolArrayParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
if (value.list.type != null) {
|
|
return getDtypeArrayParam(this.node.rawAttrs, name, defaultValue);
|
|
}
|
|
}
|
|
return defaultValue;
|
|
};
|
|
return NodeValueImpl;
|
|
}());
|
|
|
|
/**
|
|
* @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 EPSILON_FLOAT32 = 1e-7;
|
|
var 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).
|
|
*/
|
|
var KernelBackend = /** @class */ (function () {
|
|
function KernelBackend() {
|
|
}
|
|
KernelBackend.prototype.refCount = function (dataId) {
|
|
return notYetImplemented('refCount');
|
|
};
|
|
KernelBackend.prototype.incRef = function (dataId) {
|
|
return notYetImplemented('incRef');
|
|
};
|
|
KernelBackend.prototype.timerAvailable = function () {
|
|
return true;
|
|
};
|
|
KernelBackend.prototype.time = function (f) {
|
|
return notYetImplemented('time');
|
|
};
|
|
KernelBackend.prototype.read = function (dataId) {
|
|
return notYetImplemented('read');
|
|
};
|
|
KernelBackend.prototype.readSync = function (dataId) {
|
|
return notYetImplemented('readSync');
|
|
};
|
|
KernelBackend.prototype.readToGPU = function (dataId, options) {
|
|
return notYetImplemented('readToGPU');
|
|
};
|
|
KernelBackend.prototype.numDataIds = function () {
|
|
return notYetImplemented('numDataIds');
|
|
};
|
|
KernelBackend.prototype.disposeData = function (dataId, force) {
|
|
return notYetImplemented('disposeData');
|
|
};
|
|
KernelBackend.prototype.write = function (values, shape, dtype) {
|
|
return notYetImplemented('write');
|
|
};
|
|
KernelBackend.prototype.move = function (dataId, values, shape, dtype, refCount) {
|
|
return notYetImplemented('move');
|
|
};
|
|
KernelBackend.prototype.createTensorFromGPUData = function (values, shape, dtype) {
|
|
return notYetImplemented('createTensorFromGPUData');
|
|
};
|
|
KernelBackend.prototype.memory = function () {
|
|
return notYetImplemented('memory');
|
|
};
|
|
/** Returns the highest precision for floats in bits (e.g. 16 or 32) */
|
|
KernelBackend.prototype.floatPrecision = function () {
|
|
return notYetImplemented('floatPrecision');
|
|
};
|
|
/** Returns the smallest representable number. */
|
|
KernelBackend.prototype.epsilon = function () {
|
|
return this.floatPrecision() === 32 ? EPSILON_FLOAT32 : EPSILON_FLOAT16;
|
|
};
|
|
KernelBackend.prototype.dispose = function () {
|
|
return notYetImplemented('dispose');
|
|
};
|
|
return KernelBackend;
|
|
}());
|
|
function notYetImplemented(kernelName) {
|
|
throw new Error("'".concat(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) {
|
|
if (errorMessagePrefix === void 0) { errorMessagePrefix = ''; }
|
|
assert(arraysEqual(shapeA, shapeB), function () { return errorMessagePrefix + " Shapes ".concat(shapeA, " and ").concat(shapeB, " must match"); });
|
|
}
|
|
function assertNonNull(a) {
|
|
assert(a != null, function () { return "The input to the tensor constructor must be a non-null value."; });
|
|
}
|
|
/**
|
|
* 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;
|
|
}
|
|
var size = shape[0];
|
|
for (var i = 1; i < shape.length; i++) {
|
|
size *= shape[i];
|
|
}
|
|
return size;
|
|
}
|
|
function arraysEqualWithNull(n1, n2) {
|
|
if (n1 === n2) {
|
|
return true;
|
|
}
|
|
if (n1 == null || n2 == null) {
|
|
return false;
|
|
}
|
|
if (n1.length !== n2.length) {
|
|
return false;
|
|
}
|
|
for (var i = 0; i < n1.length; i++) {
|
|
if (n1[i] !== null && n2[i] !== null && n1[i] !== n2[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
function arraysEqual(n1, n2) {
|
|
if (n1 === n2) {
|
|
return true;
|
|
}
|
|
if (n1 == null || n2 == null) {
|
|
return false;
|
|
}
|
|
if (n1.length !== n2.length) {
|
|
return false;
|
|
}
|
|
for (var 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) {
|
|
var rank = shape.length;
|
|
// Normalize input
|
|
axis = axis == null ? shape.map(function (s, i) { return i; }) : [].concat(axis);
|
|
// Check for valid range
|
|
assert(axis.every(function (ax) { return ax >= -rank && ax < rank; }), function () { return "All values in axis param must be in range [-".concat(rank, ", ").concat(rank, ") but ") +
|
|
"got axis ".concat(axis); });
|
|
// Check for only integers
|
|
assert(axis.every(function (ax) { return isInt(ax); }), function () { return "All values in axis param must be integers but " +
|
|
"got axis ".concat(axis); });
|
|
// Handle negative axis.
|
|
return axis.map(function (a) { return a < 0 ? rank + a : a; });
|
|
}
|
|
/** Reduces the shape by removing all dimensions of shape 1. */
|
|
function squeezeShape(shape, axis) {
|
|
var newShape = [];
|
|
var keptDims = [];
|
|
var isEmptyArray = axis != null && Array.isArray(axis) && axis.length === 0;
|
|
var axes = (axis == null || isEmptyArray) ?
|
|
null :
|
|
parseAxisParam(axis, shape).sort();
|
|
var j = 0;
|
|
for (var i = 0; i < shape.length; ++i) {
|
|
if (axes != null) {
|
|
if (axes[j] === i && shape[i] !== 1) {
|
|
throw new Error("Can't squeeze axis ".concat(i, " since its dim '").concat(shape[i], "' is not 1"));
|
|
}
|
|
if ((axes[j] == null || axes[j] > i) && shape[i] === 1) {
|
|
newShape.push(shape[i]);
|
|
keptDims.push(i);
|
|
}
|
|
if (axes[j] <= i) {
|
|
j++;
|
|
}
|
|
}
|
|
if (shape[i] !== 1) {
|
|
newShape.push(shape[i]);
|
|
keptDims.push(i);
|
|
}
|
|
}
|
|
return { newShape: newShape, keptDims: keptDims };
|
|
}
|
|
function getTypedArrayFromDType(dtype, size) {
|
|
return getArrayFromDType(dtype, size);
|
|
}
|
|
function getArrayFromDType(dtype, size) {
|
|
var values = null;
|
|
if (dtype == null || dtype === 'float32') {
|
|
values = new Float32Array(size);
|
|
}
|
|
else if (dtype === 'int32') {
|
|
values = new Int32Array(size);
|
|
}
|
|
else if (dtype === 'bool') {
|
|
values = new Uint8Array(size);
|
|
}
|
|
else if (dtype === 'string') {
|
|
values = new Array(size);
|
|
}
|
|
else {
|
|
throw new Error("Unknown data type ".concat(dtype));
|
|
}
|
|
return values;
|
|
}
|
|
function checkConversionForErrors(vals, dtype) {
|
|
for (var i = 0; i < vals.length; i++) {
|
|
var num = vals[i];
|
|
if (isNaN(num) || !isFinite(num)) {
|
|
throw Error("A tensor of type ".concat(dtype, " being uploaded contains ").concat(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 ".concat(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;
|
|
}
|
|
var bytes = 0;
|
|
arr.forEach(function (x) { return 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) {
|
|
var rank = shape.length;
|
|
if (rank < 2) {
|
|
return [];
|
|
}
|
|
// Last dimension has implicit stride of 1, thus having D-1 (instead of D)
|
|
// strides.
|
|
var strides = new Array(rank - 1);
|
|
strides[rank - 2] = shape[rank - 1];
|
|
for (var i = rank - 3; i >= 0; --i) {
|
|
strides[i] = strides[i + 1] * shape[i + 1];
|
|
}
|
|
return strides;
|
|
}
|
|
function createNestedArray(offset, shape, a, isComplex) {
|
|
if (isComplex === void 0) { isComplex = false; }
|
|
var ret = new Array();
|
|
if (shape.length === 1) {
|
|
var d = shape[0] * (isComplex ? 2 : 1);
|
|
for (var i = 0; i < d; i++) {
|
|
ret[i] = a[offset + i];
|
|
}
|
|
}
|
|
else {
|
|
var d = shape[0];
|
|
var rest = shape.slice(1);
|
|
var len = rest.reduce(function (acc, c) { return acc * c; }) * (isComplex ? 2 : 1);
|
|
for (var 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) {
|
|
if (isComplex === void 0) { isComplex = false; }
|
|
if (shape.length === 0) {
|
|
// Scalar type should return a single number.
|
|
return a[0];
|
|
}
|
|
var size = shape.reduce(function (acc, c) { return 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("[".concat(shape, "] does not match the input size ").concat(a.length).concat(isComplex ? ' for a complex tensor' : '', "."));
|
|
}
|
|
return createNestedArray(0, shape, a, isComplex);
|
|
}
|
|
function makeOnesTypedArray(size, dtype) {
|
|
var array = makeZerosTypedArray(size, dtype);
|
|
for (var 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 ".concat(dtype));
|
|
}
|
|
}
|
|
function assertNonNegativeIntegerDimensions(shape) {
|
|
shape.forEach(function (dimSize) {
|
|
assert(Number.isInteger(dimSize) && dimSize >= 0, function () { return "Tensor must have a shape comprised of positive integers but got " +
|
|
"shape [".concat(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';
|
|
}
|
|
|
|
// Expects flags from URL in the format ?tfjsflags=FLAG1:1,FLAG2:true.
|
|
var 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'}
|
|
*/
|
|
var Environment = /** @class */ (function () {
|
|
// tslint:disable-next-line: no-any
|
|
function Environment(global) {
|
|
this.global = global;
|
|
this.flags = {};
|
|
this.flagRegistry = {};
|
|
this.urlFlags = {};
|
|
// Jasmine spies on this in 'environment_test.ts'
|
|
this.getQueryParams = getQueryParams;
|
|
this.populateURLFlags();
|
|
}
|
|
Environment.prototype.setPlatform = function (platformName, platform) {
|
|
if (this.platform != null) {
|
|
if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) {
|
|
console.warn("Platform ".concat(this.platformName, " has already been set. ") +
|
|
"Overwriting the platform with ".concat(platformName, "."));
|
|
}
|
|
}
|
|
this.platformName = platformName;
|
|
this.platform = platform;
|
|
};
|
|
Environment.prototype.registerFlag = function (flagName, evaluationFn, setHook) {
|
|
this.flagRegistry[flagName] = { evaluationFn: evaluationFn, setHook: 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) {
|
|
var flagValue = this.urlFlags[flagName];
|
|
if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) {
|
|
console.warn("Setting feature override from URL ".concat(flagName, ": ").concat(flagValue, "."));
|
|
}
|
|
this.set(flagName, flagValue);
|
|
}
|
|
};
|
|
Environment.prototype.getAsync = function (flagName) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var _a, _b;
|
|
return __generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
if (flagName in this.flags) {
|
|
return [2 /*return*/, this.flags[flagName]];
|
|
}
|
|
_a = this.flags;
|
|
_b = flagName;
|
|
return [4 /*yield*/, this.evaluateFlag(flagName)];
|
|
case 1:
|
|
_a[_b] = _c.sent();
|
|
return [2 /*return*/, this.flags[flagName]];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
Environment.prototype.get = function (flagName) {
|
|
if (flagName in this.flags) {
|
|
return this.flags[flagName];
|
|
}
|
|
var flagValue = this.evaluateFlag(flagName);
|
|
if (isPromise(flagValue)) {
|
|
throw new Error("Flag ".concat(flagName, " cannot be synchronously evaluated. ") +
|
|
"Please use getAsync() instead.");
|
|
}
|
|
this.flags[flagName] = flagValue;
|
|
return this.flags[flagName];
|
|
};
|
|
Environment.prototype.getNumber = function (flagName) {
|
|
return this.get(flagName);
|
|
};
|
|
Environment.prototype.getBool = function (flagName) {
|
|
return this.get(flagName);
|
|
};
|
|
Environment.prototype.getString = function (flagName) {
|
|
return this.get(flagName);
|
|
};
|
|
Environment.prototype.getFlags = function () {
|
|
return this.flags;
|
|
};
|
|
Object.defineProperty(Environment.prototype, "features", {
|
|
// For backwards compatibility.
|
|
get: function () {
|
|
return this.flags;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Environment.prototype.set = function (flagName, value) {
|
|
if (this.flagRegistry[flagName] == null) {
|
|
throw new Error("Cannot set flag ".concat(flagName, " as it has not been registered."));
|
|
}
|
|
this.flags[flagName] = value;
|
|
if (this.flagRegistry[flagName].setHook != null) {
|
|
this.flagRegistry[flagName].setHook(value);
|
|
}
|
|
};
|
|
Environment.prototype.evaluateFlag = function (flagName) {
|
|
if (this.flagRegistry[flagName] == null) {
|
|
throw new Error("Cannot evaluate flag '".concat(flagName, "': no evaluation function found."));
|
|
}
|
|
return this.flagRegistry[flagName].evaluationFn();
|
|
};
|
|
Environment.prototype.setFlags = function (flags) {
|
|
this.flags = Object.assign({}, flags);
|
|
};
|
|
Environment.prototype.reset = function () {
|
|
this.flags = {};
|
|
this.urlFlags = {};
|
|
this.populateURLFlags();
|
|
};
|
|
Environment.prototype.populateURLFlags = function () {
|
|
var _this = this;
|
|
if (typeof this.global === 'undefined' ||
|
|
typeof this.global.location === 'undefined' ||
|
|
typeof this.global.location.search === 'undefined') {
|
|
return;
|
|
}
|
|
var urlParams = this.getQueryParams(this.global.location.search);
|
|
if (TENSORFLOWJS_FLAGS_PREFIX in urlParams) {
|
|
var keyValues = urlParams[TENSORFLOWJS_FLAGS_PREFIX].split(',');
|
|
keyValues.forEach(function (keyValue) {
|
|
var _a = __read(keyValue.split(':'), 2), key = _a[0], value = _a[1];
|
|
_this.urlFlags[key] = parseValue(key, value);
|
|
});
|
|
}
|
|
};
|
|
return Environment;
|
|
}());
|
|
function getQueryParams(queryString) {
|
|
var params = {};
|
|
queryString.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g, function (s) {
|
|
var t = [];
|
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
t[_i - 1] = arguments[_i];
|
|
}
|
|
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) {
|
|
var lowerCaseValue = value.toLowerCase();
|
|
if (lowerCaseValue === 'true' || lowerCaseValue === 'false') {
|
|
return lowerCaseValue === 'true';
|
|
}
|
|
else if ("".concat(+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;
|
|
}
|
|
var ENV = null;
|
|
function setEnvironmentGlobal(environment) {
|
|
ENV = 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
|
|
var globalNameSpace;
|
|
// tslint:disable-next-line:no-any
|
|
function getGlobalNamespace() {
|
|
if (globalNameSpace == null) {
|
|
// tslint:disable-next-line:no-any
|
|
var ns = void 0;
|
|
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() {
|
|
var 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) {
|
|
var globalMap = getGlobalMap();
|
|
if (globalMap.has(key)) {
|
|
return globalMap.get(key);
|
|
}
|
|
else {
|
|
var singleton = init();
|
|
globalMap.set(key, singleton);
|
|
return globalMap.get(key);
|
|
}
|
|
}
|
|
|
|
var Abs = 'Abs';
|
|
var Acos = 'Acos';
|
|
var Acosh = 'Acosh';
|
|
var Add = 'Add';
|
|
var AddN = 'AddN';
|
|
var All = 'All';
|
|
var Any = 'Any';
|
|
var ArgMax = 'ArgMax';
|
|
var ArgMin = 'ArgMin';
|
|
var Asin = 'Asin';
|
|
var Asinh = 'Asinh';
|
|
var Atan = 'Atan';
|
|
var Atanh = 'Atanh';
|
|
var Atan2 = 'Atan2';
|
|
var AvgPool = 'AvgPool';
|
|
var AvgPool3D = 'AvgPool3D';
|
|
var BatchMatMul = 'BatchMatMul';
|
|
var BatchToSpaceND = 'BatchToSpaceND';
|
|
var Bincount = 'Bincount';
|
|
var BitwiseAnd = 'BitwiseAnd';
|
|
var BroadcastArgs = 'BroadcastArgs';
|
|
var Cast = 'Cast';
|
|
var Ceil = 'Ceil';
|
|
var ClipByValue = 'ClipByValue';
|
|
var Complex = 'Complex';
|
|
var ComplexAbs = 'ComplexAbs';
|
|
var Concat = 'Concat';
|
|
var Conv2D = 'Conv2D';
|
|
var Conv2DBackpropFilter = 'Conv2DBackpropFilter';
|
|
var Conv2DBackpropInput = 'Conv2DBackpropInput';
|
|
var Conv3D = 'Conv3D';
|
|
var Conv3DBackpropInputV2 = 'Conv3DBackpropInputV2';
|
|
var Cos = 'Cos';
|
|
var Cosh = 'Cosh';
|
|
var Cumprod = 'Cumprod';
|
|
var Cumsum = 'Cumsum';
|
|
var CropAndResize = 'CropAndResize';
|
|
var DenseBincount = 'DenseBincount';
|
|
var DepthToSpace = 'DepthToSpace';
|
|
var DepthwiseConv2dNative = 'DepthwiseConv2dNative';
|
|
var DepthwiseConv2dNativeBackpropFilter = 'DepthwiseConv2dNativeBackpropFilter';
|
|
var DepthwiseConv2dNativeBackpropInput = 'DepthwiseConv2dNativeBackpropInput';
|
|
var Diag = 'Diag';
|
|
var Dilation2D = 'Dilation2D';
|
|
var RealDiv = 'RealDiv';
|
|
var Einsum = 'Einsum';
|
|
var Elu = 'Elu';
|
|
var Erf = 'Erf';
|
|
var Equal = 'Equal';
|
|
var Exp = 'Exp';
|
|
var ExpandDims = 'ExpandDims';
|
|
var Expm1 = 'Expm1';
|
|
var FFT = 'FFT';
|
|
var Fill = 'Fill';
|
|
var FlipLeftRight = 'FlipLeftRight';
|
|
var Floor = 'Floor';
|
|
var FloorDiv = 'FloorDiv';
|
|
var FusedBatchNorm = 'FusedBatchNorm';
|
|
var GatherV2 = 'GatherV2';
|
|
var GatherNd = 'GatherNd';
|
|
var Greater = 'Greater';
|
|
var GreaterEqual = 'GreaterEqual';
|
|
var Identity = 'Identity';
|
|
var IFFT = 'IFFT';
|
|
var Imag = 'Imag';
|
|
var IsFinite = 'IsFinite';
|
|
var IsInf = 'IsInf';
|
|
var IsNan = 'IsNan';
|
|
var LeakyRelu = 'LeakyRelu';
|
|
var Less = 'Less';
|
|
var LessEqual = 'LessEqual';
|
|
var LinSpace = 'LinSpace';
|
|
var Log = 'Log';
|
|
var Log1p = 'Log1p';
|
|
var LogicalAnd = 'LogicalAnd';
|
|
var LogicalNot = 'LogicalNot';
|
|
var LogicalOr = 'LogicalOr';
|
|
var LRN = 'LRN';
|
|
var Max = 'Max';
|
|
var Maximum = 'Maximum';
|
|
var MaxPool = 'MaxPool';
|
|
var MaxPool3D = 'MaxPool3D';
|
|
var MaxPoolWithArgmax = 'MaxPoolWithArgmax';
|
|
var Mean = 'Mean';
|
|
var Min = 'Min';
|
|
var Minimum = 'Minimum';
|
|
var MirrorPad = 'MirrorPad';
|
|
var Mod = 'Mod';
|
|
var Multinomial = 'Multinomial';
|
|
var Multiply = 'Multiply';
|
|
var Neg = 'Neg';
|
|
var NotEqual = 'NotEqual';
|
|
var NonMaxSuppressionV3 = 'NonMaxSuppressionV3';
|
|
var NonMaxSuppressionV4 = 'NonMaxSuppressionV4';
|
|
var NonMaxSuppressionV5 = 'NonMaxSuppressionV5';
|
|
var OnesLike = 'OnesLike';
|
|
var OneHot = 'OneHot';
|
|
var Pack = 'Pack';
|
|
var PadV2 = 'PadV2';
|
|
var Pow = 'Pow';
|
|
var Prelu = 'Prelu';
|
|
var Prod = 'Prod';
|
|
var RaggedGather = 'RaggedGather';
|
|
var RaggedRange = 'RaggedRange';
|
|
var RaggedTensorToTensor = 'RaggedTensorToTensor';
|
|
var Range = 'Range';
|
|
var Real = 'Real';
|
|
var Reciprocal = 'Reciprocal';
|
|
var Relu = 'Relu';
|
|
var Reshape = 'Reshape';
|
|
var ResizeNearestNeighbor = 'ResizeNearestNeighbor';
|
|
var ResizeBilinear = 'ResizeBilinear';
|
|
var Relu6 = 'Relu6';
|
|
var Reverse = 'Reverse';
|
|
var Round = 'Round';
|
|
var Rsqrt = 'Rsqrt';
|
|
var ScatterNd = 'ScatterNd';
|
|
var TensorScatterUpdate = 'TensorScatterUpdate';
|
|
var SearchSorted = 'SearchSorted';
|
|
var Select = 'Select';
|
|
var Selu = 'Selu';
|
|
var Slice = 'Slice';
|
|
var Sin = 'Sin';
|
|
var Sinh = 'Sinh';
|
|
var Sign = 'Sign';
|
|
var Sigmoid = 'Sigmoid';
|
|
var Softplus = 'Softplus';
|
|
var Sqrt = 'Sqrt';
|
|
var Sum = 'Sum';
|
|
var SpaceToBatchND = 'SpaceToBatchND';
|
|
var SplitV = 'SplitV';
|
|
var Softmax = 'Softmax';
|
|
var SparseFillEmptyRows = 'SparseFillEmptyRows';
|
|
var SparseReshape = 'SparseReshape';
|
|
var SparseSegmentMean = 'SparseSegmentMean';
|
|
var SparseSegmentSum = 'SparseSegmentSum';
|
|
var SparseToDense = 'SparseToDense';
|
|
var SquaredDifference = 'SquaredDifference';
|
|
var StaticRegexReplace = 'StaticRegexReplace';
|
|
var StridedSlice = 'StridedSlice';
|
|
var StringNGrams = 'StringNGrams';
|
|
var StringSplit = 'StringSplit';
|
|
var StringToHashBucketFast = 'StringToHashBucketFast';
|
|
var Sub = 'Sub';
|
|
var Tan = 'Tan';
|
|
var Tanh = 'Tanh';
|
|
var Tile = 'Tile';
|
|
var TopK = 'TopK';
|
|
var Transform = 'Transform';
|
|
var Transpose = 'Transpose';
|
|
var Unique = 'Unique';
|
|
var Unpack = 'Unpack';
|
|
var UnsortedSegmentSum = 'UnsortedSegmentSum';
|
|
var ZerosLike = 'ZerosLike';
|
|
/**
|
|
* TensorFlow.js-only kernels
|
|
*/
|
|
var Step = 'Step';
|
|
var RotateWithOffset = 'RotateWithOffset';
|
|
var _FusedMatMul = '_FusedMatMul';
|
|
var FusedConv2D = 'FusedConv2D';
|
|
var FusedDepthwiseConv2D = 'FusedDepthwiseConv2D';
|
|
|
|
function warn() {
|
|
var msg = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
msg[_i] = arguments[_i];
|
|
}
|
|
if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) {
|
|
console.warn.apply(console, __spreadArray([], __read(msg), false));
|
|
}
|
|
}
|
|
|
|
var kernelRegistry = getGlobal('kernelRegistry', function () { return new Map(); });
|
|
var gradRegistry = getGlobal('gradRegistry', function () { return 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) {
|
|
var 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) {
|
|
var it = kernelRegistry.entries();
|
|
var result = [];
|
|
while (true) {
|
|
var _a = it.next(), done = _a.done, value = _a.value;
|
|
if (done) {
|
|
break;
|
|
}
|
|
var _b = __read(value, 2), key = _b[0], config = _b[1];
|
|
var _c = __read(key.split('_'), 1), backend = _c[0];
|
|
if (backend === backendName) {
|
|
result.push(config);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
function makeKey(kernelName, backendName) {
|
|
return "".concat(backendName, "_").concat(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.<number>} Byte representation
|
|
*/
|
|
LongPrototype.toBytes = function toBytes(le) {
|
|
return le ? this.toBytesLE() : this.toBytesBE();
|
|
};
|
|
/**
|
|
* Converts this Long to its little endian byte representation.
|
|
* @returns {!Array.<number>} 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.<number>} 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.<number>} 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.<number>} 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.<number>} 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]);
|
|
|
|
// tslint:disable-next-line
|
|
var 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');
|
|
|
|
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(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') {
|
|
var bool = new Uint8Array(a.length);
|
|
for (var i = 0; i < bool.length; ++i) {
|
|
if (Math.round(a[i]) !== 0) {
|
|
bool[i] = 1;
|
|
}
|
|
}
|
|
return bool;
|
|
}
|
|
else {
|
|
throw new Error("Unknown data type ".concat(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) {
|
|
if (encoding === void 0) { 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) {
|
|
if (encoding === void 0) { 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<number>().
|
|
/**
|
|
* 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(arr, result, skipTypedArray) {
|
|
var e_1, _a;
|
|
if (result === void 0) { result = []; }
|
|
if (skipTypedArray === void 0) { 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 (var i = 0; i < arr.length; ++i) {
|
|
flatten(arr[i], result, skipTypedArray);
|
|
}
|
|
}
|
|
else {
|
|
var maxIndex = -1;
|
|
try {
|
|
for (var _b = __values(Object.keys(arr)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var key = _c.value;
|
|
// 0 or positive integer.
|
|
if (/^([1-9]+[0-9]*|0)$/.test(key)) {
|
|
maxIndex = Math.max(maxIndex, Number(key));
|
|
}
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
for (var i = 0; i <= maxIndex; i++) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
flatten(arr[i], result, skipTypedArray);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
var Profiler = /** @class */ (function () {
|
|
function Profiler(backendTimer, logger) {
|
|
this.backendTimer = backendTimer;
|
|
this.logger = logger;
|
|
if (logger == null) {
|
|
this.logger = new Logger();
|
|
}
|
|
}
|
|
Profiler.prototype.profileKernel = function (kernelName, inputs, f) {
|
|
var e_1, _a;
|
|
var outputs;
|
|
var holdResultWrapperFn = function () {
|
|
outputs = f();
|
|
};
|
|
var timer;
|
|
var start = now();
|
|
if (this.backendTimer.timerAvailable()) {
|
|
timer = this.backendTimer.time(holdResultWrapperFn);
|
|
}
|
|
else {
|
|
holdResultWrapperFn();
|
|
try {
|
|
for (var outputs_1 = __values(outputs), outputs_1_1 = outputs_1.next(); !outputs_1_1.done; outputs_1_1 = outputs_1.next()) {
|
|
var output = outputs_1_1.value;
|
|
output.dataSync();
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (outputs_1_1 && !outputs_1_1.done && (_a = outputs_1.return)) _a.call(outputs_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
timer = Promise.resolve({ kernelMs: now() - start });
|
|
}
|
|
if (env().getBool('CHECK_COMPUTATION_FOR_ERRORS')) {
|
|
var _loop_1 = function (i) {
|
|
var output = outputs[i];
|
|
// Dangling promise here because we don't want to propagate up
|
|
// asynchronicity.
|
|
output.data().then(function (tensorVals) {
|
|
checkComputationForErrors(tensorVals, output.dtype, kernelName);
|
|
});
|
|
};
|
|
for (var i = 0; i < outputs.length; i++) {
|
|
_loop_1(i);
|
|
}
|
|
}
|
|
var kernelProfile = {
|
|
kernelName: kernelName,
|
|
outputs: outputs,
|
|
inputs: inputs,
|
|
timeMs: timer.then(function (timing) { return timing.kernelMs; }),
|
|
extraInfo: timer.then(function (timing) { return timing.getExtraProfileInfo != null ?
|
|
timing.getExtraProfileInfo() :
|
|
''; })
|
|
};
|
|
return kernelProfile;
|
|
};
|
|
Profiler.prototype.logKernelProfile = function (kernelProfile) {
|
|
var _this = this;
|
|
var kernelName = kernelProfile.kernelName, outputs = kernelProfile.outputs, timeMs = kernelProfile.timeMs, inputs = kernelProfile.inputs, extraInfo = kernelProfile.extraInfo;
|
|
outputs.forEach(function (result) {
|
|
Promise.all([result.data(), timeMs, extraInfo]).then(function (valueContainer) {
|
|
_this.logger.logKernelProfile(kernelName, result, valueContainer[0], valueContainer[1], inputs, valueContainer[2]);
|
|
});
|
|
});
|
|
};
|
|
return Profiler;
|
|
}());
|
|
function checkComputationForErrors(vals, dtype, kernelName) {
|
|
if (dtype !== 'float32') {
|
|
// Only floating point computations will generate NaN values
|
|
return false;
|
|
}
|
|
for (var i = 0; i < vals.length; i++) {
|
|
var num = vals[i];
|
|
if (isNaN(num) || !isFinite(num)) {
|
|
// Throwing custom exception so behavior is testable.
|
|
console.warn("Found ".concat(num, " in the result of '").concat(kernelName, "'"));
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
var Logger = /** @class */ (function () {
|
|
function Logger() {
|
|
}
|
|
Logger.prototype.logKernelProfile = function (name, result, vals, timeMs, inputs, extraInfo) {
|
|
var time = typeof timeMs === 'number' ? rightPad("".concat(timeMs, "ms"), 9) :
|
|
timeMs['error'];
|
|
var paddedName = rightPad(name, 25);
|
|
var rank = result.rank;
|
|
var size = result.size;
|
|
var shape = rightPad(result.shape.toString(), 14);
|
|
var inputShapesDescription = '';
|
|
for (var name_1 in inputs) {
|
|
var input = inputs[name_1];
|
|
if (input != null) {
|
|
// The input might be a non-tensor (e.g HTMLImageElement), in which case
|
|
// we claim the output shape as input shape.
|
|
var inputShape = input.shape || result.shape;
|
|
var inputRank = inputShape.length;
|
|
inputShapesDescription +=
|
|
"".concat(name_1, ": ").concat(inputRank, "D ").concat(inputRank > 0 ? inputShape : '', " ");
|
|
}
|
|
}
|
|
console.log("%c".concat(paddedName, "\t%c").concat(time, "\t%c").concat(rank, "D ").concat(shape, "\t%c").concat(size, "\t%c").concat(inputShapesDescription, "\t%c").concat(extraInfo), 'font-weight:bold', 'color:red', 'color:blue', 'color: orange', 'color: green', 'color: steelblue');
|
|
};
|
|
return Logger;
|
|
}());
|
|
|
|
/**
|
|
* @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.
|
|
var tensorsFromX = {};
|
|
var nodesFromX = {};
|
|
for (var i = 0; i < xs.length; i++) {
|
|
tensorsFromX[xs[i].id] = true;
|
|
}
|
|
for (var i = 0; i < tape.length; i++) {
|
|
var node = tape[i];
|
|
var nodeInputs = node.inputs;
|
|
for (var inputName in nodeInputs) {
|
|
var input = nodeInputs[inputName];
|
|
var anyInputFromX = false;
|
|
for (var j = 0; j < xs.length; j++) {
|
|
if (tensorsFromX[input.id]) {
|
|
node.outputs.forEach(function (output) { return 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.
|
|
var tensorsLeadToY = {};
|
|
tensorsLeadToY[y.id] = true;
|
|
var nodesToY = {};
|
|
for (var i = tape.length - 1; i >= 0; i--) {
|
|
var node = tape[i];
|
|
var nodeInputs = node.inputs;
|
|
// If any of the outputs lead to y, mark all of the inputs as leading to y.
|
|
for (var j = 0; j < node.outputs.length; j++) {
|
|
if (tensorsLeadToY[node.outputs[j].id]) {
|
|
for (var inputName in nodeInputs) {
|
|
tensorsLeadToY[nodeInputs[inputName].id] = true;
|
|
nodesToY[node.id] = true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// Return the paths that come from x and lead to y.
|
|
var filteredTape = [];
|
|
for (var i = 0; i < tape.length; i++) {
|
|
var node = tape[i];
|
|
if (nodesFromX[node.id] && nodesToY[node.id]) {
|
|
// Prune the inputs from the node that aren't a function of x.
|
|
var prunedInputs = {};
|
|
for (var inputName in node.inputs) {
|
|
var nodeInput = node.inputs[inputName];
|
|
if (tensorsFromX[nodeInput.id]) {
|
|
prunedInputs[inputName] = nodeInput;
|
|
}
|
|
}
|
|
// Copy the node and overwrite inputsAndArgs to the pruned version.
|
|
var 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) {
|
|
var _loop_1 = function (i) {
|
|
var node = filteredTape[i];
|
|
var dys = [];
|
|
node.outputs.forEach(function (o) {
|
|
var 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 ".concat(node.kernelName, "."));
|
|
}
|
|
// Backprop dy through this node and accumulate gradients over the inputs.
|
|
var inputGradients = node.gradient(dys);
|
|
var _loop_2 = function (inputName) {
|
|
if (!(inputName in inputGradients)) {
|
|
throw new Error("Cannot backprop through input ".concat(inputName, ". ") +
|
|
"Available gradients found: ".concat(Object.keys(inputGradients), "."));
|
|
}
|
|
// Call the gradient function.
|
|
var dx = tidy(function () { return inputGradients[inputName](); });
|
|
if (dx.dtype !== 'float32') {
|
|
throw new Error("Error in gradient for op ".concat(node.kernelName, ". The gradient of input ") +
|
|
"".concat(inputName, " must have 'float32' dtype, but has '").concat(dx.dtype, "'"));
|
|
}
|
|
var x = node.inputs[inputName];
|
|
if (!arraysEqual(dx.shape, x.shape)) {
|
|
throw new Error("Error in gradient for op ".concat(node.kernelName, ". The gradient of input ") +
|
|
"'".concat(inputName, "' has shape '").concat(dx.shape, "', which does not match ") +
|
|
"the shape of the input '".concat(x.shape, "'"));
|
|
}
|
|
if (tensorAccumulatedGradientMap[x.id] == null) {
|
|
tensorAccumulatedGradientMap[x.id] = dx;
|
|
}
|
|
else {
|
|
var curGradient = tensorAccumulatedGradientMap[x.id];
|
|
tensorAccumulatedGradientMap[x.id] = add(curGradient, dx);
|
|
curGradient.dispose();
|
|
}
|
|
};
|
|
for (var inputName in node.inputs) {
|
|
_loop_2(inputName);
|
|
}
|
|
};
|
|
// Walk the tape backward and keep a map of Tensor to its gradient.
|
|
for (var i = filteredTape.length - 1; i >= 0; i--) {
|
|
_loop_1(i);
|
|
}
|
|
}
|
|
|
|
// Maximum number of values before we decide to show ellipsis.
|
|
var FORMAT_LIMIT_NUM_VALS = 20;
|
|
// Number of first and last values to show when displaying a, b,...,y, z.
|
|
var FORMAT_NUM_FIRST_LAST_VALS = 3;
|
|
// Number of significant digits to show.
|
|
var FORMAT_NUM_SIG_DIGITS = 7;
|
|
function tensorToString(vals, shape, dtype, verbose) {
|
|
var strides = computeStrides(shape);
|
|
var padPerCol = computeMaxSizePerColumn(vals, shape, dtype, strides);
|
|
var rank = shape.length;
|
|
var valsLines = subTensorToString(vals, shape, dtype, strides, padPerCol);
|
|
var lines = ['Tensor'];
|
|
if (verbose) {
|
|
lines.push(" dtype: ".concat(dtype));
|
|
lines.push(" rank: ".concat(rank));
|
|
lines.push(" shape: [".concat(shape, "]"));
|
|
lines.push(" values:");
|
|
}
|
|
lines.push(valsLines.map(function (l) { return ' ' + l; }).join('\n'));
|
|
return lines.join('\n');
|
|
}
|
|
function computeMaxSizePerColumn(vals, shape, dtype, strides) {
|
|
var n = sizeFromShape(shape);
|
|
var numCols = strides[strides.length - 1];
|
|
var padPerCol = new Array(numCols).fill(0);
|
|
var rank = shape.length;
|
|
var valuesOrTuples = dtype === 'complex64' ? createComplexTuples(vals) : vals;
|
|
if (rank > 1) {
|
|
for (var row = 0; row < n / numCols; row++) {
|
|
var offset = row * numCols;
|
|
for (var 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) {
|
|
var valStr;
|
|
if (Array.isArray(val)) {
|
|
valStr = "".concat(parseFloat(val[0].toFixed(FORMAT_NUM_SIG_DIGITS)), " + ") +
|
|
"".concat(parseFloat(val[1].toFixed(FORMAT_NUM_SIG_DIGITS)), "j");
|
|
}
|
|
else if (isString(val)) {
|
|
valStr = "'".concat(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) {
|
|
if (isLast === void 0) { isLast = true; }
|
|
var storagePerElement = dtype === 'complex64' ? 2 : 1;
|
|
var size = shape[0];
|
|
var rank = shape.length;
|
|
if (rank === 0) {
|
|
if (dtype === 'complex64') {
|
|
var 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) {
|
|
var firstValsSize = FORMAT_NUM_FIRST_LAST_VALS * storagePerElement;
|
|
var firstVals = Array.from(vals.slice(0, firstValsSize));
|
|
var 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(function (x, i) { return valToString(x, padPerCol[i], dtype); })
|
|
.join(', ') +
|
|
', ..., ' +
|
|
lastVals
|
|
.map(function (x, i) { return valToString(x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i], dtype); })
|
|
.join(', ') +
|
|
']'
|
|
];
|
|
}
|
|
var displayVals = dtype === 'complex64' ? createComplexTuples(vals) :
|
|
Array.from(vals);
|
|
return [
|
|
'[' +
|
|
displayVals.map(function (x, i) { return valToString(x, padPerCol[i], dtype); })
|
|
.join(', ') +
|
|
']'
|
|
];
|
|
}
|
|
// The array is rank 2 or more.
|
|
var subshape = shape.slice(1);
|
|
var substrides = strides.slice(1);
|
|
var stride = strides[0] * storagePerElement;
|
|
var lines = [];
|
|
if (size > FORMAT_LIMIT_NUM_VALS) {
|
|
for (var i = 0; i < FORMAT_NUM_FIRST_LAST_VALS; i++) {
|
|
var start = i * stride;
|
|
var end = start + stride;
|
|
lines.push.apply(lines, __spreadArray([], __read(subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, false /* isLast */)), false));
|
|
}
|
|
lines.push('...');
|
|
for (var i = size - FORMAT_NUM_FIRST_LAST_VALS; i < size; i++) {
|
|
var start = i * stride;
|
|
var end = start + stride;
|
|
lines.push.apply(lines, __spreadArray([], __read(subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, i === size - 1 /* isLast */)), false));
|
|
}
|
|
}
|
|
else {
|
|
for (var i = 0; i < size; i++) {
|
|
var start = i * stride;
|
|
var end = start + stride;
|
|
lines.push.apply(lines, __spreadArray([], __read(subTensorToString(vals.slice(start, end), subshape, dtype, substrides, padPerCol, i === size - 1 /* isLast */)), false));
|
|
}
|
|
}
|
|
var sep = rank === 2 ? ',' : '';
|
|
lines[0] = '[' + (size > 0 ? lines[0] + sep : '');
|
|
for (var i = 1; i < lines.length - 1; i++) {
|
|
lines[i] = ' ' + lines[i] + sep;
|
|
}
|
|
var newLineSep = ',\n';
|
|
for (var i = 2; i < rank; i++) {
|
|
newLineSep += '\n';
|
|
}
|
|
lines[lines.length - 1] =
|
|
' ' + lines[lines.length - 1] + ']' + (isLast ? '' : newLineSep);
|
|
return lines;
|
|
}
|
|
function createComplexTuples(vals) {
|
|
var complexTuples = [];
|
|
for (var i = 0; i < vals.length; i += 2) {
|
|
complexTuples.push([vals[i], vals[i + 1]]);
|
|
}
|
|
return complexTuples;
|
|
}
|
|
|
|
/**
|
|
* A mutable object, similar to `tf.Tensor`, that allows users to set values
|
|
* at locations before converting to an immutable `tf.Tensor`.
|
|
*
|
|
* See `tf.buffer` for creating a tensor buffer.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
var TensorBuffer = /** @class */ (function () {
|
|
function TensorBuffer(shape, dtype, values) {
|
|
var _this = this;
|
|
this.dtype = dtype;
|
|
this.shape = shape.slice();
|
|
this.size = sizeFromShape(shape);
|
|
if (values != null) {
|
|
var n_1 = values.length;
|
|
assert(n_1 === this.size, function () { return "Length of values '".concat(n_1, "' does not match the size ") +
|
|
"inferred by the shape '".concat(_this.size, "'."); });
|
|
}
|
|
if (dtype === 'complex64') {
|
|
throw new Error("complex64 dtype TensorBuffers are not supported. Please create " +
|
|
"a TensorBuffer for the real and imaginary parts separately and " +
|
|
"call tf.complex(real, imag).");
|
|
}
|
|
this.values = values || getArrayFromDType(dtype, this.size);
|
|
this.strides = computeStrides(shape);
|
|
}
|
|
/**
|
|
* Sets a value in the buffer at a given location.
|
|
*
|
|
* @param value The value to set.
|
|
* @param locs The location indices.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
TensorBuffer.prototype.set = function (value) {
|
|
var _this = this;
|
|
var locs = [];
|
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
locs[_i - 1] = arguments[_i];
|
|
}
|
|
if (locs.length === 0) {
|
|
locs = [0];
|
|
}
|
|
assert(locs.length === this.rank, function () { return "The number of provided coordinates (".concat(locs.length, ") must ") +
|
|
"match the rank (".concat(_this.rank, ")"); });
|
|
var index = this.locToIndex(locs);
|
|
this.values[index] = value;
|
|
};
|
|
/**
|
|
* Returns the value in the buffer at the provided location.
|
|
*
|
|
* @param locs The location indices.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
TensorBuffer.prototype.get = function () {
|
|
var e_1, _b;
|
|
var locs = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
locs[_i] = arguments[_i];
|
|
}
|
|
if (locs.length === 0) {
|
|
locs = [0];
|
|
}
|
|
var i = 0;
|
|
try {
|
|
for (var locs_1 = __values(locs), locs_1_1 = locs_1.next(); !locs_1_1.done; locs_1_1 = locs_1.next()) {
|
|
var loc = locs_1_1.value;
|
|
if (loc < 0 || loc >= this.shape[i]) {
|
|
var msg = "Requested out of range element at ".concat(locs, ". ") +
|
|
" Buffer shape=".concat(this.shape);
|
|
throw new Error(msg);
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (locs_1_1 && !locs_1_1.done && (_b = locs_1.return)) _b.call(locs_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
var index = locs[locs.length - 1];
|
|
for (var i_1 = 0; i_1 < locs.length - 1; ++i_1) {
|
|
index += this.strides[i_1] * locs[i_1];
|
|
}
|
|
return this.values[index];
|
|
};
|
|
TensorBuffer.prototype.locToIndex = function (locs) {
|
|
if (this.rank === 0) {
|
|
return 0;
|
|
}
|
|
else if (this.rank === 1) {
|
|
return locs[0];
|
|
}
|
|
var index = locs[locs.length - 1];
|
|
for (var i = 0; i < locs.length - 1; ++i) {
|
|
index += this.strides[i] * locs[i];
|
|
}
|
|
return index;
|
|
};
|
|
TensorBuffer.prototype.indexToLoc = function (index) {
|
|
if (this.rank === 0) {
|
|
return [];
|
|
}
|
|
else if (this.rank === 1) {
|
|
return [index];
|
|
}
|
|
var locs = new Array(this.shape.length);
|
|
for (var i = 0; i < locs.length - 1; ++i) {
|
|
locs[i] = Math.floor(index / this.strides[i]);
|
|
index -= locs[i] * this.strides[i];
|
|
}
|
|
locs[locs.length - 1] = index;
|
|
return locs;
|
|
};
|
|
Object.defineProperty(TensorBuffer.prototype, "rank", {
|
|
get: function () {
|
|
return this.shape.length;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Creates an immutable `tf.Tensor` object from the buffer.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
TensorBuffer.prototype.toTensor = function () {
|
|
return trackerFn().makeTensor(this.values, this.shape, this.dtype);
|
|
};
|
|
return TensorBuffer;
|
|
}());
|
|
// For tracking tensor creation and disposal.
|
|
var trackerFn = null;
|
|
// Used by chaining methods to call into ops.
|
|
var 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'}
|
|
*/
|
|
var Tensor = /** @class */ (function () {
|
|
function Tensor(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');
|
|
}
|
|
Object.defineProperty(Tensor.prototype, "rank", {
|
|
get: function () {
|
|
return this.shape.length;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Returns a promise of `tf.TensorBuffer` that holds the underlying data.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
Tensor.prototype.buffer = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var vals;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0: return [4 /*yield*/, this.data()];
|
|
case 1:
|
|
vals = _b.sent();
|
|
return [2 /*return*/, opHandler.buffer(this.shape, this.dtype, vals)];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* Returns a `tf.TensorBuffer` that holds the underlying data.
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
Tensor.prototype.bufferSync = function () {
|
|
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'}
|
|
*/
|
|
Tensor.prototype.array = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var vals;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0: return [4 /*yield*/, this.data()];
|
|
case 1:
|
|
vals = _b.sent();
|
|
return [2 /*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'}
|
|
*/
|
|
Tensor.prototype.arraySync = function () {
|
|
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'}
|
|
*/
|
|
Tensor.prototype.data = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var data, bytes;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
this.throwIfDisposed();
|
|
data = trackerFn().read(this.dataId);
|
|
if (!(this.dtype === 'string')) return [3 /*break*/, 2];
|
|
return [4 /*yield*/, data];
|
|
case 1:
|
|
bytes = _b.sent();
|
|
try {
|
|
return [2 /*return*/, bytes.map(function (b) { return decodeString(b); })];
|
|
}
|
|
catch (_a) {
|
|
throw new Error('Failed to decode the string bytes into utf-8. ' +
|
|
'To get the original bytes, call tensor.bytes().');
|
|
}
|
|
_b.label = 2;
|
|
case 2: return [2 /*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'}
|
|
*/
|
|
Tensor.prototype.dataToGPU = function (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'}
|
|
*/
|
|
Tensor.prototype.dataSync = function () {
|
|
this.throwIfDisposed();
|
|
var data = trackerFn().readSync(this.dataId);
|
|
if (this.dtype === 'string') {
|
|
try {
|
|
return data.map(function (b) { return 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. */
|
|
Tensor.prototype.bytes = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var data;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
this.throwIfDisposed();
|
|
return [4 /*yield*/, trackerFn().read(this.dataId)];
|
|
case 1:
|
|
data = _b.sent();
|
|
if (this.dtype === 'string') {
|
|
return [2 /*return*/, data];
|
|
}
|
|
else {
|
|
return [2 /*return*/, new Uint8Array(data.buffer)];
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* Disposes `tf.Tensor` from memory.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
Tensor.prototype.dispose = function () {
|
|
if (this.isDisposed) {
|
|
return;
|
|
}
|
|
if (this.kerasMask) {
|
|
this.kerasMask.dispose();
|
|
}
|
|
trackerFn().disposeTensor(this);
|
|
this.isDisposedInternal = true;
|
|
};
|
|
Object.defineProperty(Tensor.prototype, "isDisposed", {
|
|
get: function () {
|
|
return this.isDisposedInternal;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Tensor.prototype.throwIfDisposed = function () {
|
|
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'}
|
|
*/
|
|
Tensor.prototype.print = function (verbose) {
|
|
if (verbose === void 0) { verbose = false; }
|
|
return opHandler.print(this, verbose);
|
|
};
|
|
/**
|
|
* Returns a copy of the tensor. See `tf.clone` for details.
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
Tensor.prototype.clone = function () {
|
|
this.throwIfDisposed();
|
|
return opHandler.clone(this);
|
|
};
|
|
/**
|
|
* Returns a human-readable description of the tensor. Useful for logging.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
|
*/
|
|
Tensor.prototype.toString = function (verbose) {
|
|
if (verbose === void 0) { verbose = false; }
|
|
var vals = this.dataSync();
|
|
return tensorToString(vals, this.shape, this.dtype, verbose);
|
|
};
|
|
Tensor.prototype.cast = function (dtype) {
|
|
this.throwIfDisposed();
|
|
return opHandler.cast(this, dtype);
|
|
};
|
|
Tensor.prototype.variable = function (trainable, name, dtype) {
|
|
if (trainable === void 0) { trainable = true; }
|
|
this.throwIfDisposed();
|
|
return trackerFn().makeVariable(this, trainable, name, dtype);
|
|
};
|
|
return Tensor;
|
|
}());
|
|
Object.defineProperty(Tensor, Symbol.hasInstance, {
|
|
value: function (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', function () {
|
|
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'}
|
|
*/
|
|
var Variable = /** @class */ (function (_super) {
|
|
__extends(Variable, _super);
|
|
function Variable(initialValue, trainable, name, tensorId) {
|
|
var _this = _super.call(this, initialValue.shape, initialValue.dtype, initialValue.dataId, tensorId) || this;
|
|
_this.trainable = trainable;
|
|
_this.name = name;
|
|
return _this;
|
|
}
|
|
/**
|
|
* 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'}
|
|
*/
|
|
Variable.prototype.assign = function (newValue) {
|
|
if (newValue.dtype !== this.dtype) {
|
|
throw new Error("dtype of the new value (".concat(newValue.dtype, ") and ") +
|
|
"previous value (".concat(this.dtype, ") must match"));
|
|
}
|
|
if (!arraysEqual(newValue.shape, this.shape)) {
|
|
throw new Error("shape of the new value (".concat(newValue.shape, ") and ") +
|
|
"previous value (".concat(this.shape, ") must match"));
|
|
}
|
|
trackerFn().disposeTensor(this);
|
|
this.dataId = newValue.dataId;
|
|
trackerFn().incRef(this, null /* backend */);
|
|
};
|
|
Variable.prototype.dispose = function () {
|
|
trackerFn().disposeVariable(this);
|
|
this.isDisposedInternal = true;
|
|
};
|
|
return Variable;
|
|
}(Tensor));
|
|
Object.defineProperty(Variable, Symbol.hasInstance, {
|
|
value: function (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 = {}));
|
|
var 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 ".concat(typeA, " with ").concat(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];
|
|
}
|
|
var dtype = upcastType(a.dtype, b.dtype);
|
|
return [a.cast(dtype), b.cast(dtype)];
|
|
}
|
|
function assertTypesMatch(a, b) {
|
|
assert(a.dtype === b.dtype, function () { return "The dtypes of the first(".concat(a.dtype, ") and") +
|
|
" second(".concat(b.dtype, ") input must match"); });
|
|
}
|
|
/**
|
|
* 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) {
|
|
var list = [];
|
|
var 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.
|
|
var iterable = container;
|
|
for (var k in iterable) {
|
|
var 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';
|
|
}
|
|
|
|
function isRegisteredKernelInvocation(kernelInvocation) {
|
|
return kernelInvocation.kernelName != null;
|
|
}
|
|
var EngineState = /** @class */ (function () {
|
|
function EngineState() {
|
|
// 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(function (k) { return k.name; })));
|
|
}
|
|
};
|
|
}
|
|
EngineState.prototype.dispose = function () {
|
|
for (var variableName in this.registeredVariables) {
|
|
this.registeredVariables[variableName].dispose();
|
|
}
|
|
};
|
|
return EngineState;
|
|
}());
|
|
var Engine = /** @class */ (function () {
|
|
function Engine(ENV) {
|
|
this.ENV = ENV;
|
|
this.registry = {};
|
|
this.registryFactory = {};
|
|
this.pendingBackendInitId = 0;
|
|
this.state = new EngineState();
|
|
}
|
|
Engine.prototype.ready = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var sortedBackends, i, backendName, success;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
if (this.pendingBackendInit != null) {
|
|
return [2 /*return*/, this.pendingBackendInit.then(function () { })];
|
|
}
|
|
if (this.backendInstance != null) {
|
|
return [2 /*return*/];
|
|
}
|
|
sortedBackends = this.getSortedBackends();
|
|
i = 0;
|
|
_a.label = 1;
|
|
case 1:
|
|
if (!(i < sortedBackends.length)) return [3 /*break*/, 5];
|
|
backendName = sortedBackends[i];
|
|
return [4 /*yield*/, this.initializeBackend(backendName).success];
|
|
case 2:
|
|
success = _a.sent();
|
|
if (!success) return [3 /*break*/, 4];
|
|
return [4 /*yield*/, this.setBackend(backendName)];
|
|
case 3:
|
|
_a.sent();
|
|
return [2 /*return*/];
|
|
case 4:
|
|
i++;
|
|
return [3 /*break*/, 1];
|
|
case 5: throw new Error("Could not initialize any backends, all backend initializations " +
|
|
"failed.");
|
|
}
|
|
});
|
|
});
|
|
};
|
|
Object.defineProperty(Engine.prototype, "backend", {
|
|
get: function () {
|
|
if (this.pendingBackendInit != null) {
|
|
throw new Error("Backend '".concat(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) {
|
|
var _a = this.initializeBackendsAndReturnBest(), name = _a.name, asyncInit = _a.asyncInit;
|
|
if (asyncInit) {
|
|
throw new Error("The highest priority backend '".concat(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;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Engine.prototype.backendNames = function () {
|
|
return Object.keys(this.registryFactory);
|
|
};
|
|
Engine.prototype.findBackend = function (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) {
|
|
var asyncInit = this.initializeBackend(backendName).asyncInit;
|
|
if (asyncInit) {
|
|
// Backend is not ready yet.
|
|
return null;
|
|
}
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
return this.registry[backendName];
|
|
};
|
|
Engine.prototype.findBackendFactory = function (backendName) {
|
|
if (!(backendName in this.registryFactory)) {
|
|
return null;
|
|
}
|
|
return this.registryFactory[backendName].factory;
|
|
};
|
|
Engine.prototype.registerBackend = function (backendName, factory, priority) {
|
|
if (priority === void 0) { priority = 1; }
|
|
if (backendName in this.registryFactory) {
|
|
warn("".concat(backendName, " backend was already registered. ") +
|
|
"Reusing existing backend factory.");
|
|
return false;
|
|
}
|
|
this.registryFactory[backendName] = { factory: factory, priority: priority };
|
|
return true;
|
|
};
|
|
Engine.prototype.setBackend = function (backendName) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var _a, success, asyncInit, result, _b;
|
|
return __generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
if (this.registryFactory[backendName] == null) {
|
|
throw new Error("Backend name '".concat(backendName, "' not found in registry"));
|
|
}
|
|
this.backendName = backendName;
|
|
if (!(this.registry[backendName] == null)) return [3 /*break*/, 4];
|
|
this.backendInstance = null;
|
|
_a = this.initializeBackend(backendName), success = _a.success, asyncInit = _a.asyncInit;
|
|
if (!asyncInit) return [3 /*break*/, 2];
|
|
return [4 /*yield*/, success];
|
|
case 1:
|
|
_b = _c.sent();
|
|
return [3 /*break*/, 3];
|
|
case 2:
|
|
_b = success;
|
|
_c.label = 3;
|
|
case 3:
|
|
result = _b;
|
|
if (!result) {
|
|
return [2 /*return*/, false];
|
|
}
|
|
_c.label = 4;
|
|
case 4:
|
|
this.backendInstance = this.registry[backendName];
|
|
this.setupRegisteredKernels();
|
|
// Reset the profiler.
|
|
this.profiler = new Profiler(this.backendInstance);
|
|
return [2 /*return*/, true];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
Engine.prototype.setupRegisteredKernels = function () {
|
|
var _this = this;
|
|
var kernels = getKernelsForBackend(this.backendName);
|
|
kernels.forEach(function (kernel) {
|
|
if (kernel.setupFunc != null) {
|
|
kernel.setupFunc(_this.backendInstance);
|
|
}
|
|
});
|
|
};
|
|
Engine.prototype.disposeRegisteredKernels = function (backendName) {
|
|
var _this = this;
|
|
var kernels = getKernelsForBackend(backendName);
|
|
kernels.forEach(function (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.
|
|
*/
|
|
Engine.prototype.initializeBackend = function (backendName) {
|
|
var _this = this;
|
|
var registryFactoryEntry = this.registryFactory[backendName];
|
|
if (registryFactoryEntry == null) {
|
|
throw new Error("Cannot initialize backend ".concat(backendName, ", no registration found."));
|
|
}
|
|
try {
|
|
var 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') {
|
|
var promiseId_1 = ++this.pendingBackendInitId;
|
|
var success = backend
|
|
.then(function (backendInstance) {
|
|
// Outdated promise. Another backend was set in the meantime.
|
|
if (promiseId_1 < _this.pendingBackendInitId) {
|
|
return false;
|
|
}
|
|
_this.registry[backendName] = backendInstance;
|
|
_this.pendingBackendInit = null;
|
|
return true;
|
|
})
|
|
.catch(function (err) {
|
|
// Outdated promise. Another backend was set in the meantime.
|
|
if (promiseId_1 < _this.pendingBackendInitId) {
|
|
return false;
|
|
}
|
|
_this.pendingBackendInit = null;
|
|
warn("Initialization of backend ".concat(backendName, " failed"));
|
|
warn(err.stack || err.message);
|
|
return false;
|
|
});
|
|
this.pendingBackendInit = success;
|
|
return { success: success, asyncInit: true };
|
|
}
|
|
else {
|
|
this.registry[backendName] = backend;
|
|
return { success: true, asyncInit: false };
|
|
}
|
|
}
|
|
catch (err) {
|
|
warn("Initialization of backend ".concat(backendName, " failed"));
|
|
warn(err.stack || err.message);
|
|
return { success: false, asyncInit: false };
|
|
}
|
|
};
|
|
Engine.prototype.removeBackend = function (backendName) {
|
|
if (!(backendName in this.registryFactory)) {
|
|
throw new Error("".concat(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;
|
|
}
|
|
};
|
|
Engine.prototype.getSortedBackends = function () {
|
|
var _this = this;
|
|
if (Object.keys(this.registryFactory).length === 0) {
|
|
throw new Error('No backend found in registry.');
|
|
}
|
|
return Object.keys(this.registryFactory).sort(function (a, b) {
|
|
// Highest priority comes first.
|
|
return _this.registryFactory[b].priority -
|
|
_this.registryFactory[a].priority;
|
|
});
|
|
};
|
|
Engine.prototype.initializeBackendsAndReturnBest = function () {
|
|
var sortedBackends = this.getSortedBackends();
|
|
for (var i = 0; i < sortedBackends.length; i++) {
|
|
var backendName = sortedBackends[i];
|
|
var _a = this.initializeBackend(backendName), success = _a.success, asyncInit = _a.asyncInit;
|
|
if (asyncInit || success) {
|
|
return { name: backendName, asyncInit: asyncInit };
|
|
}
|
|
}
|
|
throw new Error("Could not initialize any backends, all backend initializations " +
|
|
"failed.");
|
|
};
|
|
Engine.prototype.moveData = function (backend, dataId) {
|
|
var info = this.state.tensorInfo.get(dataId);
|
|
var srcBackend = info.backend;
|
|
var values = this.readSync(dataId);
|
|
var 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]++;
|
|
}
|
|
};
|
|
Engine.prototype.tidy = function (nameOrFn, fn) {
|
|
var _this = this;
|
|
var 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.
|
|
}
|
|
var result;
|
|
return this.scopedRun(function () { return _this.startScope(name); }, function () { return _this.endScope(result); }, function () {
|
|
result = fn();
|
|
if (result instanceof Promise) {
|
|
console.error('Cannot return a Promise inside of tidy.');
|
|
}
|
|
return result;
|
|
});
|
|
};
|
|
Engine.prototype.scopedRun = function (start, end, f) {
|
|
start();
|
|
try {
|
|
var res = f();
|
|
end();
|
|
return res;
|
|
}
|
|
catch (ex) {
|
|
end();
|
|
throw ex;
|
|
}
|
|
};
|
|
Engine.prototype.nextTensorId = function () {
|
|
return Engine.nextTensorId++;
|
|
};
|
|
Engine.prototype.nextVariableId = function () {
|
|
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.
|
|
*/
|
|
Engine.prototype.clone = function (x) {
|
|
var y = ENGINE.runKernel(Identity, { x: x });
|
|
var inputs = { x: x };
|
|
var grad = function (dy) { return ({
|
|
x: function () {
|
|
var dtype = 'float32';
|
|
var gradInputs = { x: dy };
|
|
var attrs = { dtype: dtype };
|
|
return ENGINE.runKernel(Cast, gradInputs,
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
attrs);
|
|
}
|
|
}); };
|
|
var 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.
|
|
*/
|
|
Engine.prototype.runKernel = function (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;
|
|
}
|
|
var hasKernel = getKernel(kernelName, this.backendName) != null;
|
|
if (!hasKernel) {
|
|
throw new Error("Kernel '".concat(kernelName, "' not registered for backend '").concat(this.backendName, "'"));
|
|
}
|
|
return this.runKernelFunc({ kernelName: kernelName, inputs: inputs, attrs: attrs });
|
|
};
|
|
Engine.prototype.shouldCheckForMemLeaks = function () {
|
|
return this.ENV.getBool('IS_TEST');
|
|
};
|
|
Engine.prototype.checkKernelForMemLeak = function (kernelName, numDataIdsBefore, outInfos) {
|
|
var numDataIdsAfter = this.backend.numDataIds();
|
|
// Count the number of data ids associated with the result of the kernel.
|
|
var numOutputDataIds = 0;
|
|
outInfos.forEach(function (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.
|
|
var numMoves = this.state.numDataMovesStack[this.state.numDataMovesStack.length - 1];
|
|
var dataIdsLeaked = numDataIdsAfter - numDataIdsBefore - numOutputDataIds - numMoves;
|
|
if (dataIdsLeaked > 0) {
|
|
throw new Error("Backend '".concat(this.backendName, "' has an internal memory leak ") +
|
|
"(".concat(dataIdsLeaked, " data ids) after running '").concat(kernelName, "'"));
|
|
}
|
|
};
|
|
/**
|
|
* Internal helper method to execute a kernel Func
|
|
*
|
|
* Use `runKernel` to execute kernels from outside of engine.
|
|
*/
|
|
Engine.prototype.runKernelFunc = function (kernelParams) {
|
|
var _this = this;
|
|
var outputs;
|
|
var saved = [];
|
|
var isTapeOn = this.isTapeOn();
|
|
var startingBytecount = this.state.numBytes;
|
|
var startingNumTensors = this.state.numTensors;
|
|
if (this.shouldCheckForMemLeaks()) {
|
|
this.state.numDataMovesStack.push(0);
|
|
}
|
|
var 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;
|
|
}
|
|
var out;
|
|
var 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)) {
|
|
var kernelName_1 = kernelParams.kernelName, inputs_1 = kernelParams.inputs, attrs_1 = kernelParams.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;
|
|
}
|
|
var kernel_1 = getKernel(kernelName_1, this.backendName);
|
|
assert(kernel_1 != null, function () { return "Cannot find registered kernel '".concat(kernelName_1, "' for backend '").concat(_this.backendName, "'"); });
|
|
kernelFunc = function () {
|
|
var numDataIdsBefore = _this.backend.numDataIds();
|
|
out = kernel_1.kernelFunc({ inputs: inputs_1, attrs: attrs_1, backend: _this.backend });
|
|
var outInfos = Array.isArray(out) ? out : [out];
|
|
if (_this.shouldCheckForMemLeaks()) {
|
|
_this.checkKernelForMemLeak(kernelName_1, numDataIdsBefore, outInfos);
|
|
}
|
|
var outTensors = outInfos.map(function (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) {
|
|
var tensorsToSave = _this.getTensorsForGradient(kernelName_1, inputs_1, outTensors);
|
|
saved = _this.saveTensorsForBackwardMode(tensorsToSave);
|
|
}
|
|
return outTensors;
|
|
};
|
|
}
|
|
else {
|
|
var forwardFunc_1 = kernelParams.forwardFunc;
|
|
// Running a customGrad op.
|
|
var saveFunc_1 = function (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(function (tensor) { return _this.keep(_this.clone(tensor)); });
|
|
};
|
|
kernelFunc = function () {
|
|
var numDataIdsBefore = _this.backend.numDataIds();
|
|
out = _this.tidy(function () { return forwardFunc_1(_this.backend, saveFunc_1); });
|
|
var 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.
|
|
//
|
|
var inputs = kernelParams.inputs, attrs = kernelParams.attrs;
|
|
var backwardsFunc = isRegisteredKernelInvocation(kernelParams) ?
|
|
null :
|
|
kernelParams.backwardsFunc;
|
|
var kernelProfile;
|
|
this.scopedRun(
|
|
// Stop recording to a tape when running a kernel.
|
|
function () { return _this.state.kernelDepth++; }, function () { return _this.state.kernelDepth--; }, function () {
|
|
if (!_this.ENV.getBool('DEBUG') && !_this.state.profiling) {
|
|
outputs = kernelFunc();
|
|
}
|
|
else {
|
|
kernelProfile = _this.profiler.profileKernel(kernelOrScopeName, inputs, function () { return 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(function (key) { return inputs[key] != null ? inputs[key].shape : null; }),
|
|
outputShapes: outputs.map(function (item) { return 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.
|
|
*/
|
|
Engine.prototype.saveTensorsForBackwardMode = function (tensors) {
|
|
var _this = this;
|
|
var saved = tensors.map(function (tensor) { return _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.
|
|
*/
|
|
Engine.prototype.getTensorsForGradient = function (kernelName, inputs, outputs) {
|
|
var gradConfig = getGradient(kernelName);
|
|
if (gradConfig != null) {
|
|
var inputsToSave = gradConfig.inputsToSave || [];
|
|
var outputsToSave_1 = gradConfig.outputsToSave || [];
|
|
// If saveAllInputs is true, all inputs will be saved. Otherwise, inputs
|
|
// specified in inputsToSave will be saved.
|
|
var inputTensorsToSave = void 0;
|
|
if (gradConfig.saveAllInputs) {
|
|
assert(Array.isArray(inputs), function () { return 'saveAllInputs is true, expected inputs to be an array.'; });
|
|
inputTensorsToSave = Object.keys(inputs).map(function (key) { return inputs[key]; });
|
|
}
|
|
else {
|
|
inputTensorsToSave = inputsToSave.map(function (inputName) { return inputs[inputName]; });
|
|
}
|
|
var outputTensorsToSave = outputs.filter(function (_, i) { return outputsToSave_1[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.
|
|
*/
|
|
Engine.prototype.makeTensor = function (values, shape, dtype, backend) {
|
|
if (values == null) {
|
|
throw new Error('Values passed to engine.makeTensor() are null');
|
|
}
|
|
dtype = dtype || 'float32';
|
|
backend = backend || this.backend;
|
|
var backendVals = values;
|
|
if (dtype === 'string' && isString(values[0])) {
|
|
backendVals = values.map(function (d) { return encodeString(d); });
|
|
}
|
|
var dataId = backend.write(backendVals, shape, dtype);
|
|
var t = new Tensor(shape, dtype, dataId, this.nextTensorId());
|
|
this.trackTensor(t, backend);
|
|
// Count bytes for string tensors.
|
|
if (dtype === 'string') {
|
|
var info = this.state.tensorInfo.get(dataId);
|
|
var 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
|
|
*/
|
|
Engine.prototype.makeTensorFromDataId = function (dataId, shape, dtype, backend) {
|
|
dtype = dtype || 'float32';
|
|
var tensorInfo = { dataId: dataId, shape: shape, dtype: 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.
|
|
*/
|
|
Engine.prototype.makeTensorFromTensorInfo = function (tensorInfo, backend) {
|
|
var dataId = tensorInfo.dataId, shape = tensorInfo.shape, dtype = tensorInfo.dtype;
|
|
var t = new Tensor(shape, dtype, dataId, this.nextTensorId());
|
|
this.trackTensor(t, backend);
|
|
return t;
|
|
};
|
|
Engine.prototype.makeVariable = function (initialValue, trainable, name, dtype) {
|
|
if (trainable === void 0) { trainable = true; }
|
|
name = name || this.nextVariableId().toString();
|
|
if (dtype != null && dtype !== initialValue.dtype) {
|
|
initialValue = initialValue.cast(dtype);
|
|
}
|
|
var v = new Variable(initialValue, trainable, name, this.nextTensorId());
|
|
if (this.state.registeredVariables[v.name] != null) {
|
|
throw new Error("Variable with name ".concat(v.name, " was already registered"));
|
|
}
|
|
this.state.registeredVariables[v.name] = v;
|
|
this.incRef(v, this.backend);
|
|
return v;
|
|
};
|
|
Engine.prototype.trackTensor = function (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.
|
|
var 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: 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.
|
|
Engine.prototype.incRef = function (a, backend) {
|
|
this.trackTensor(a, backend);
|
|
this.backend.incRef(a.dataId);
|
|
};
|
|
Engine.prototype.removeDataId = function (dataId, backend) {
|
|
if (this.state.tensorInfo.has(dataId) &&
|
|
this.state.tensorInfo.get(dataId).backend === backend) {
|
|
this.state.tensorInfo.delete(dataId);
|
|
this.state.numDataBuffers--;
|
|
}
|
|
};
|
|
Engine.prototype.disposeTensor = function (a) {
|
|
if (!this.state.tensorInfo.has(a.dataId)) {
|
|
return;
|
|
}
|
|
var 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') {
|
|
var 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.
|
|
};
|
|
Engine.prototype.disposeVariables = function () {
|
|
for (var varName in this.state.registeredVariables) {
|
|
var v = this.state.registeredVariables[varName];
|
|
this.disposeVariable(v);
|
|
}
|
|
};
|
|
Engine.prototype.disposeVariable = function (v) {
|
|
this.disposeTensor(v);
|
|
if (this.state.registeredVariables[v.name] != null) {
|
|
delete this.state.registeredVariables[v.name];
|
|
}
|
|
};
|
|
Engine.prototype.memory = function () {
|
|
var 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;
|
|
};
|
|
Engine.prototype.profile = function (query) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var startBytes, startNumTensors, _a, _b, _c, kernel, _d, _e, e_1_1;
|
|
var e_1, _f;
|
|
return __generator(this, function (_g) {
|
|
switch (_g.label) {
|
|
case 0:
|
|
this.state.profiling = true;
|
|
startBytes = this.state.numBytes;
|
|
startNumTensors = this.state.numTensors;
|
|
this.state.activeProfile.kernels = [];
|
|
_a = this.state.activeProfile;
|
|
return [4 /*yield*/, query()];
|
|
case 1:
|
|
_a.result = _g.sent();
|
|
this.state.profiling = false;
|
|
this.state.activeProfile.peakBytes = Math.max.apply(Math, __spreadArray([], __read(this.state.activeProfile.kernels.map(function (d) { return d.totalBytesSnapshot; })), false));
|
|
this.state.activeProfile.newBytes = this.state.numBytes - startBytes;
|
|
this.state.activeProfile.newTensors =
|
|
this.state.numTensors - startNumTensors;
|
|
_g.label = 2;
|
|
case 2:
|
|
_g.trys.push([2, 8, 9, 10]);
|
|
_b = __values(this.state.activeProfile.kernels), _c = _b.next();
|
|
_g.label = 3;
|
|
case 3:
|
|
if (!!_c.done) return [3 /*break*/, 7];
|
|
kernel = _c.value;
|
|
_d = kernel;
|
|
return [4 /*yield*/, kernel.kernelTimeMs];
|
|
case 4:
|
|
_d.kernelTimeMs = _g.sent();
|
|
_e = kernel;
|
|
return [4 /*yield*/, kernel.extraInfo];
|
|
case 5:
|
|
_e.extraInfo = _g.sent();
|
|
_g.label = 6;
|
|
case 6:
|
|
_c = _b.next();
|
|
return [3 /*break*/, 3];
|
|
case 7: return [3 /*break*/, 10];
|
|
case 8:
|
|
e_1_1 = _g.sent();
|
|
e_1 = { error: e_1_1 };
|
|
return [3 /*break*/, 10];
|
|
case 9:
|
|
try {
|
|
if (_c && !_c.done && (_f = _b.return)) _f.call(_b);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
return [7 /*endfinally*/];
|
|
case 10: return [2 /*return*/, this.state.activeProfile];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
Engine.prototype.isTapeOn = function () {
|
|
return this.state.gradientDepth > 0 && this.state.kernelDepth === 0;
|
|
};
|
|
Engine.prototype.addTapeNode = function (kernelName, inputs, outputs, gradientsFunc, saved, attrs) {
|
|
var _this = this;
|
|
var tapeNode = { id: this.state.nextTapeNodeId++, kernelName: kernelName, inputs: inputs, outputs: outputs, saved: saved };
|
|
var gradConfig = getGradient(kernelName);
|
|
if (gradConfig != null) {
|
|
gradientsFunc = gradConfig.gradFunc;
|
|
}
|
|
if (gradientsFunc != null) {
|
|
tapeNode.gradient = function (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(function (dy, i) {
|
|
if (dy == null) {
|
|
var output = outputs[i];
|
|
var 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);
|
|
};
|
|
Engine.prototype.keep = function (result) {
|
|
result.kept = true;
|
|
return result;
|
|
};
|
|
Engine.prototype.startTape = function () {
|
|
if (this.state.gradientDepth === 0) {
|
|
this.state.activeTape = [];
|
|
}
|
|
this.state.gradientDepth++;
|
|
};
|
|
Engine.prototype.endTape = function () {
|
|
this.state.gradientDepth--;
|
|
};
|
|
/**
|
|
* Start a scope. Use this with endScope() to achieve the same functionality
|
|
* as scope() without the need for a function closure.
|
|
*/
|
|
Engine.prototype.startScope = function (name) {
|
|
var 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.
|
|
*/
|
|
Engine.prototype.endScope = function (result) {
|
|
var _this = this;
|
|
var tensorsToTrackInParent = getTensorsInContainer(result);
|
|
var tensorsToTrackInParentSet = new Set(tensorsToTrackInParent.map(function (t) { return t.id; }));
|
|
// Dispose the arrays tracked in this scope.
|
|
for (var i = 0; i < this.state.activeScope.track.length; i++) {
|
|
var tensor = this.state.activeScope.track[i];
|
|
if (!tensor.kept && !tensorsToTrackInParentSet.has(tensor.id)) {
|
|
tensor.dispose();
|
|
}
|
|
}
|
|
var 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(function (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`.
|
|
*/
|
|
Engine.prototype.gradients = function (f, xs, dy, allowNoGradients) {
|
|
var _this = this;
|
|
if (allowNoGradients === void 0) { allowNoGradients = false; }
|
|
assert(xs.length > 0, function () { return 'gradients() received an empty list of xs.'; });
|
|
if (dy != null && dy.dtype !== 'float32') {
|
|
throw new Error("dy must have 'float32' dtype, but has '".concat(dy.dtype, "'"));
|
|
}
|
|
var y = this.scopedRun(function () { return _this.startTape(); }, function () { return _this.endTape(); }, function () { return _this.tidy('forward', f); });
|
|
assert(y instanceof Tensor, function () { return 'The result y returned by f() must be a tensor.'; });
|
|
// Filter out the nodes that don't connect x => y.
|
|
var 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', function () {
|
|
var accumulatedGradientMap = {};
|
|
accumulatedGradientMap[y.id] = (dy == null) ? ones$1(y.shape) : dy;
|
|
// Backprop gradients through the filtered nodes.
|
|
backpropagateGradients(accumulatedGradientMap, filteredTape,
|
|
// Pass the tidy function to avoid circular dep with `tape.ts`.
|
|
function (f) { return _this.tidy(f); },
|
|
// Pass an add function to avoide a circular dep with `tape.ts`.
|
|
add$1);
|
|
var grads = xs.map(function (x) { return 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(function (node) {
|
|
var e_2, _a;
|
|
try {
|
|
for (var _b = __values(node.saved), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var tensor = _c.value;
|
|
tensor.dispose();
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
});
|
|
_this.state.activeTape = null;
|
|
}
|
|
return { value: y, grads: grads };
|
|
});
|
|
};
|
|
Engine.prototype.customGrad = function (f) {
|
|
var _this = this;
|
|
assert(isFunction(f), function () { return 'The f passed in customGrad(f) must be a function.'; });
|
|
return function () {
|
|
var inputs = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
inputs[_i] = arguments[_i];
|
|
}
|
|
assert(inputs.every(function (t) { return t instanceof Tensor; }), function () { return 'The args passed in customGrad(f)(x1, x2,...) must all be ' +
|
|
'tensors'; });
|
|
var res;
|
|
var inputMap = {};
|
|
inputs.forEach(function (input, i) {
|
|
inputMap[i] = input;
|
|
});
|
|
var forwardFunc = function (_, save) {
|
|
res = f.apply(void 0, __spreadArray([], __read(__spreadArray(__spreadArray([], __read(inputs), false), [save], false)), false));
|
|
assert(res.value instanceof Tensor, function () { return 'The function f passed in customGrad(f) must return an ' +
|
|
'object where `obj.value` is a tensor'; });
|
|
assert(isFunction(res.gradFunc), function () { return 'The function f passed in customGrad(f) must return an ' +
|
|
'object where `obj.gradFunc` is a function.'; });
|
|
return res.value;
|
|
};
|
|
var backwardsFunc = function (dy, saved) {
|
|
var gradRes = res.gradFunc(dy, saved);
|
|
var grads = Array.isArray(gradRes) ? gradRes : [gradRes];
|
|
assert(grads.length === inputs.length, function () { return '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(function (t) { return t instanceof Tensor; }), function () { return 'The function f passed in customGrad(f) must return an ' +
|
|
'object where `obj.gradFunc` is a function that returns ' +
|
|
'a list of only tensors.'; });
|
|
var gradMap = {};
|
|
grads.forEach(function (grad, i) {
|
|
gradMap[i] = function () { return grad; };
|
|
});
|
|
return gradMap;
|
|
};
|
|
return _this.runKernelFunc({
|
|
forwardFunc: forwardFunc,
|
|
backwardsFunc: backwardsFunc,
|
|
inputs: inputMap,
|
|
});
|
|
};
|
|
};
|
|
Engine.prototype.readSync = function (dataId) {
|
|
// Route the read to the correct backend.
|
|
var info = this.state.tensorInfo.get(dataId);
|
|
return info.backend.readSync(dataId);
|
|
};
|
|
Engine.prototype.read = function (dataId) {
|
|
// Route the read to the correct backend.
|
|
var info = this.state.tensorInfo.get(dataId);
|
|
return info.backend.read(dataId);
|
|
};
|
|
Engine.prototype.readToGPU = function (dataId, options) {
|
|
// Route the read to the correct backend.
|
|
var info = this.state.tensorInfo.get(dataId);
|
|
return info.backend.readToGPU(dataId, options);
|
|
};
|
|
Engine.prototype.time = function (query) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var start, timingInfo;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
start = now();
|
|
return [4 /*yield*/, this.backend.time(query)];
|
|
case 1:
|
|
timingInfo = _a.sent();
|
|
timingInfo.wallMs = now() - start;
|
|
return [2 /*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.
|
|
*/
|
|
Engine.prototype.track = function (result) {
|
|
if (this.state.activeScope != null) {
|
|
result.scopeId = this.state.activeScope.id;
|
|
this.state.activeScope.track.push(result);
|
|
}
|
|
return result;
|
|
};
|
|
Object.defineProperty(Engine.prototype, "registeredVariables", {
|
|
get: function () {
|
|
return this.state.registeredVariables;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Resets the engine state. Removes all backends but does not remove
|
|
* registered backend factories.
|
|
*/
|
|
Engine.prototype.reset = function () {
|
|
// Make any pending promise obsolete.
|
|
this.pendingBackendInitId++;
|
|
this.state.dispose();
|
|
this.ENV.reset();
|
|
this.state = new EngineState();
|
|
for (var backendName in this.registry) {
|
|
this.disposeRegisteredKernels(backendName);
|
|
this.registry[backendName].dispose();
|
|
delete this.registry[backendName];
|
|
}
|
|
this.backendName = null;
|
|
this.backendInstance = null;
|
|
this.pendingBackendInit = null;
|
|
};
|
|
return Engine;
|
|
}());
|
|
Engine.nextTensorId = 0;
|
|
Engine.nextVariableId = 0;
|
|
function ones$1(shape) {
|
|
var values = makeOnesTypedArray(sizeFromShape(shape), 'float32');
|
|
return ENGINE.makeTensor(values, shape, 'float32');
|
|
}
|
|
function getOrMakeEngine() {
|
|
var ns = getGlobalNamespace();
|
|
if (ns._tfengine == null) {
|
|
var 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(function () { return ns._tfengine; });
|
|
return ns._tfengine;
|
|
}
|
|
var 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$1(a, b) {
|
|
// We duplicate Add here to avoid a circular dependency with add.ts.
|
|
var inputs = { a: a, b: b };
|
|
return ENGINE.runKernel(Add, 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) {
|
|
var firstElem = val;
|
|
if (isTypedArray(val)) {
|
|
return dtype === 'string' ? [] : [val.length];
|
|
}
|
|
if (isWebGLData(val)) {
|
|
var 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.
|
|
}
|
|
var 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, function () { return "Element arr[".concat(indices.join(']['), "] is a primitive, ") +
|
|
"but should be an array/TypedArray of ".concat(shape[0], " elements"); });
|
|
return;
|
|
}
|
|
assert(shape.length > 0, function () { return "Element arr[".concat(indices.join(']['), "] should be a primitive, ") +
|
|
"but is an array of ".concat(val.length, " elements"); });
|
|
assert(val.length === shape[0], function () { return "Element arr[".concat(indices.join(']['), "] should have ").concat(shape[0], " ") +
|
|
"elements, but has ".concat(val.length, " elements"); });
|
|
var subShape = shape.slice(1);
|
|
for (var 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 '".concat(argName, "' passed to '").concat(functionName, "' must ") +
|
|
"be ".concat(expectedDtype, " tensor, but got ").concat(actualDType, " tensor"));
|
|
}
|
|
}
|
|
function convertToTensor(x, argName, functionName, parseAsDtype) {
|
|
if (parseAsDtype === void 0) { parseAsDtype = 'numeric'; }
|
|
if (x instanceof getGlobalTensorClass()) {
|
|
assertDtype(parseAsDtype, x.dtype, argName, functionName);
|
|
return x;
|
|
}
|
|
var 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')) {
|
|
var type = x == null ? 'null' : x.constructor.name;
|
|
throw new Error("Argument '".concat(argName, "' passed to '").concat(functionName, "' must be a ") +
|
|
"Tensor or TensorLike, but got '".concat(type, "'"));
|
|
}
|
|
var inferredShape = inferShape(x, inferredDtype);
|
|
if (!isTypedArray(x) && !Array.isArray(x)) {
|
|
x = [x];
|
|
}
|
|
var skipTypedArray = true;
|
|
var values = inferredDtype !== 'string' ?
|
|
toTypedArray(x, inferredDtype) :
|
|
flatten(x, [], skipTypedArray);
|
|
return ENGINE.makeTensor(values, inferredShape, inferredDtype);
|
|
}
|
|
function convertToTensorArray(arg, argName, functionName, parseAsDtype) {
|
|
if (parseAsDtype === void 0) { parseAsDtype = 'numeric'; }
|
|
if (!Array.isArray(arg)) {
|
|
throw new Error("Argument ".concat(argName, " passed to ").concat(functionName, " must be a ") +
|
|
'`Tensor[]` or `TensorLike[]`');
|
|
}
|
|
var tensors = arg;
|
|
return tensors.map(function (t, i) { return convertToTensor(t, "".concat(argName, "[").concat(i, "]"), functionName, parseAsDtype); });
|
|
}
|
|
|
|
var 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) {
|
|
var 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 " +
|
|
"".concat(keys.length, " keys."));
|
|
}
|
|
var opName = keys[0];
|
|
var 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
|
|
var f2 = function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
ENGINE.startScope(opName);
|
|
try {
|
|
var result = fn.apply(void 0, __spreadArray([], __read(args), false));
|
|
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 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 absolute value element-wise: `abs(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
*
|
|
* x.abs().print(); // or tf.abs(x)
|
|
* ```
|
|
* @param x The input `tf.Tensor`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function abs_(x) {
|
|
var $x = convertToTensor(x, 'x', 'abs');
|
|
if ($x.dtype === 'complex64') {
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(ComplexAbs, inputs);
|
|
}
|
|
else {
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Abs, inputs);
|
|
}
|
|
}
|
|
var abs = /* @__PURE__ */ op({ abs_: abs_ });
|
|
|
|
/**
|
|
* @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 acos of the input `tf.Tensor` element-wise: `acos(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.acos().print(); // or tf.acos(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function acos_(x) {
|
|
var $x = convertToTensor(x, 'x', 'acos');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Acos, inputs);
|
|
}
|
|
var acos = /* @__PURE__ */ op({ acos_: acos_ });
|
|
|
|
/**
|
|
* @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 inverse hyperbolic cos of the input `tf.Tensor` element-wise:
|
|
* `acosh(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([10, 1, 3, 5.7]);
|
|
*
|
|
* x.acosh().print(); // or tf.acosh(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function acosh_(x) {
|
|
var $x = convertToTensor(x, 'x', 'acosh');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Acosh, inputs);
|
|
}
|
|
var acosh = /* @__PURE__ */ op({ acosh_: acosh_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'add');
|
|
var $b = convertToTensor(b, 'b', 'add');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Add, inputs);
|
|
}
|
|
var add = /* @__PURE__ */ op({ add_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Adds a list of `tf.Tensor`s element-wise, each with the same shape and dtype.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2]);
|
|
* const b = tf.tensor1d([3, 4]);
|
|
* const c = tf.tensor1d([5, 6]);
|
|
*
|
|
* tf.addN([a, b, c]).print();
|
|
* ```
|
|
* @param tensors A list of tensors with the same shape and dtype.
|
|
* @doc {heading: 'Operations', subheading: 'Arithmetic'}
|
|
*/
|
|
function addN_(tensors) {
|
|
assert(Array.isArray(tensors), function () { return 'The argument passed to tf.addN() must be a list of tensors'; });
|
|
assert(tensors.length >= 1, function () { return "Must pass at least one tensor to tf.addN(), but got " +
|
|
"".concat(tensors.length); });
|
|
var $tensors = tensors.map(function (t, i) { return convertToTensor(t, "tensors".concat(i), 'addN'); });
|
|
var firstTensor = $tensors[0];
|
|
$tensors.forEach(function (t) {
|
|
if (t.dtype !== firstTensor.dtype) {
|
|
throw new Error('All tensors passed to tf.addN() must have the same dtype');
|
|
}
|
|
});
|
|
$tensors.forEach(function (t) {
|
|
if (!arraysEqual(t.shape, firstTensor.shape)) {
|
|
throw new Error('All tensors passed to tf.addN() must have the same shape');
|
|
}
|
|
});
|
|
var inputs = $tensors;
|
|
return ENGINE.runKernel(AddN, inputs);
|
|
}
|
|
var addN = /* @__PURE__ */ op({ addN_: addN_ });
|
|
|
|
/**
|
|
* @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 logical and 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, 1, 1], 'bool');
|
|
*
|
|
* x.all().print(); // or tf.all(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
|
|
*
|
|
* const axis = 1;
|
|
* x.all(axis).print(); // or tf.all(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor. Must be of dtype bool.
|
|
* @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 all_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'all', 'bool');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(All, inputs, attrs);
|
|
}
|
|
var all = /* @__PURE__ */ op({ all_: all_ });
|
|
|
|
/**
|
|
* @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 logical or 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, 1, 1], 'bool');
|
|
*
|
|
* x.any().print(); // or tf.any(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
|
|
*
|
|
* const axis = 1;
|
|
* x.any(axis).print(); // or tf.any(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor. Must be of dtype bool.
|
|
* @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 any_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'any', 'bool');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(Any, inputs, attrs);
|
|
}
|
|
// tslint:disable-next-line:variable-name
|
|
var any = /* @__PURE__ */ op({ any_: any_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns the indices of the maximum values along an `axis`.
|
|
*
|
|
* The result has the same shape as `input` with the dimension along `axis`
|
|
* removed.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.argMax().print(); // or tf.argMax(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 4, 3], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.argMax(axis).print(); // or tf.argMax(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The dimension to reduce. Defaults to 0 (outer-most dimension).
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Reduction'}
|
|
*/
|
|
function argMax_(x, axis) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'argMax');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis };
|
|
return ENGINE.runKernel(ArgMax, inputs, attrs);
|
|
}
|
|
var argMax = /* @__PURE__ */ op({ argMax_: argMax_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns the indices of the minimum values along an `axis`.
|
|
*
|
|
* The result has the same shape as `input` with the dimension along `axis`
|
|
* removed.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.argMin().print(); // or tf.argMin(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 4, 3], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.argMin(axis).print(); // or tf.argMin(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The dimension to reduce. Defaults to 0 (outer-most dimension).
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Reduction'}
|
|
*/
|
|
function argMin_(x, axis) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'argMin');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis };
|
|
return ENGINE.runKernel(ArgMin, inputs, attrs);
|
|
}
|
|
var argMin = /* @__PURE__ */ op({ argMin_: argMin_ });
|
|
|
|
/**
|
|
* @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 asin of the input `tf.Tensor` element-wise: `asin(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.asin().print(); // or tf.asin(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function asin_(x) {
|
|
var $x = convertToTensor(x, 'x', 'asin');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Asin, inputs);
|
|
}
|
|
var asin = /* @__PURE__ */ op({ asin_: asin_ });
|
|
|
|
/**
|
|
* @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 inverse hyperbolic sin of the input `tf.Tensor` element-wise:
|
|
* `asinh(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.asinh().print(); // or tf.asinh(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function asinh_(x) {
|
|
var $x = convertToTensor(x, 'x', 'asinh');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Asinh, inputs);
|
|
}
|
|
var asinh = /* @__PURE__ */ op({ asinh_: asinh_ });
|
|
|
|
/**
|
|
* @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 atan of the input `tf.Tensor` element-wise: `atan(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.atan().print(); // or tf.atan(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function atan_(x) {
|
|
var $x = convertToTensor(x, 'x', 'atan');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Atan, inputs);
|
|
}
|
|
var atan = /* @__PURE__ */ op({ atan_: atan_ });
|
|
|
|
/**
|
|
* Computes arctangent of `tf.Tensor`s a / b element-wise: `atan2(a, b)`.
|
|
* Supports broadcasting.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1.0, 1.0, -1.0, .7]);
|
|
* const b = tf.tensor1d([2.0, 13.0, 3.5, .21]);
|
|
*
|
|
* tf.atan2(a, b).print()
|
|
* ```
|
|
*
|
|
* @param a The first tensor.
|
|
* @param b The second tensor. Must have the same dtype as `a`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function atan2_(a, b) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'atan2');
|
|
var $b = convertToTensor(b, 'b', 'atan2');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Atan2, inputs);
|
|
}
|
|
var atan2 = /* @__PURE__ */ op({ atan2_: atan2_ });
|
|
|
|
/**
|
|
* @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 inverse hyperbolic tan of the input `tf.Tensor` element-wise:
|
|
* `atanh(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, .1, -.1, .7]);
|
|
*
|
|
* x.atanh().print(); // or tf.atanh(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function atanh_(x) {
|
|
var $x = convertToTensor(x, 'x', 'atanh');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Atanh, inputs);
|
|
}
|
|
var atanh = /* @__PURE__ */ op({ atanh_: atanh_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'cast');
|
|
// Sanity checks.
|
|
if (!isValidDtype(dtype)) {
|
|
throw new Error("Failed to cast to unknown dtype ".concat(dtype));
|
|
}
|
|
if (dtype === 'string' && $x.dtype !== 'string' ||
|
|
dtype !== 'string' && $x.dtype === 'string') {
|
|
throw new Error('Only strings can be casted to strings');
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { dtype: dtype };
|
|
return ENGINE.runKernel(Cast, inputs, attrs);
|
|
}
|
|
var cast = /* @__PURE__ */ op({ cast_: cast_ });
|
|
|
|
function computePool2DInfo(inShape, filterSize, strides, dilations, pad, roundingMode, dataFormat) {
|
|
if (dataFormat === void 0) { dataFormat = 'channelsLast'; }
|
|
var _a = __read(parseTupleParam(filterSize), 2), filterHeight = _a[0], filterWidth = _a[1];
|
|
var filterShape;
|
|
if (dataFormat === 'channelsLast') {
|
|
filterShape = [filterHeight, filterWidth, inShape[3], inShape[3]];
|
|
}
|
|
else if (dataFormat === 'channelsFirst') {
|
|
filterShape = [filterHeight, filterWidth, inShape[1], inShape[1]];
|
|
}
|
|
else {
|
|
throw new Error("Unknown dataFormat ".concat(dataFormat));
|
|
}
|
|
return computeConv2DInfo(inShape, filterShape, strides, dilations, pad, roundingMode, false, dataFormat);
|
|
}
|
|
/**
|
|
* Computes the information for a forward pass of a convolution/pooling
|
|
* operation.
|
|
*/
|
|
function computeConv2DInfo(inShape, filterShape, strides, dilations, pad, roundingMode, depthwise, dataFormat) {
|
|
var _a, _b;
|
|
if (depthwise === void 0) { depthwise = false; }
|
|
if (dataFormat === void 0) { dataFormat = 'channelsLast'; }
|
|
var _c = __read([-1, -1, -1, -1], 4), batchSize = _c[0], inHeight = _c[1], inWidth = _c[2], inChannels = _c[3];
|
|
if (dataFormat === 'channelsLast') {
|
|
_a = __read(inShape, 4), batchSize = _a[0], inHeight = _a[1], inWidth = _a[2], inChannels = _a[3];
|
|
}
|
|
else if (dataFormat === 'channelsFirst') {
|
|
_b = __read(inShape, 4), batchSize = _b[0], inChannels = _b[1], inHeight = _b[2], inWidth = _b[3];
|
|
}
|
|
else {
|
|
throw new Error("Unknown dataFormat ".concat(dataFormat));
|
|
}
|
|
var _d = __read(filterShape, 4), filterHeight = _d[0], filterWidth = _d[1], filterChannels = _d[3];
|
|
var _e = __read(parseTupleParam(strides), 2), strideHeight = _e[0], strideWidth = _e[1];
|
|
var _f = __read(parseTupleParam(dilations), 2), dilationHeight = _f[0], dilationWidth = _f[1];
|
|
var effectiveFilterHeight = getEffectiveFilterSize(filterHeight, dilationHeight);
|
|
var effectiveFilterWidth = getEffectiveFilterSize(filterWidth, dilationWidth);
|
|
var _g = getPadAndOutInfo(pad, inHeight, inWidth, strideHeight, strideWidth, effectiveFilterHeight, effectiveFilterWidth, roundingMode, dataFormat), padInfo = _g.padInfo, outHeight = _g.outHeight, outWidth = _g.outWidth;
|
|
var outChannels = depthwise ? filterChannels * inChannels : filterChannels;
|
|
var outShape;
|
|
if (dataFormat === 'channelsFirst') {
|
|
outShape = [batchSize, outChannels, outHeight, outWidth];
|
|
}
|
|
else if (dataFormat === 'channelsLast') {
|
|
outShape = [batchSize, outHeight, outWidth, outChannels];
|
|
}
|
|
return {
|
|
batchSize: batchSize,
|
|
dataFormat: dataFormat,
|
|
inHeight: inHeight,
|
|
inWidth: inWidth,
|
|
inChannels: inChannels,
|
|
outHeight: outHeight,
|
|
outWidth: outWidth,
|
|
outChannels: outChannels,
|
|
padInfo: padInfo,
|
|
strideHeight: strideHeight,
|
|
strideWidth: strideWidth,
|
|
filterHeight: filterHeight,
|
|
filterWidth: filterWidth,
|
|
effectiveFilterHeight: effectiveFilterHeight,
|
|
effectiveFilterWidth: effectiveFilterWidth,
|
|
dilationHeight: dilationHeight,
|
|
dilationWidth: dilationWidth,
|
|
inShape: inShape,
|
|
outShape: outShape,
|
|
filterShape: filterShape
|
|
};
|
|
}
|
|
function computeOutputShape2D(inShape, fieldSize, stride, zeroPad, roundingMode) {
|
|
if (zeroPad == null) {
|
|
zeroPad = computeDefaultPad(inShape, fieldSize, stride);
|
|
}
|
|
var inputRows = inShape[0];
|
|
var inputCols = inShape[1];
|
|
var outputRows = round$1((inputRows - fieldSize + 2 * zeroPad) / stride + 1, roundingMode);
|
|
var outputCols = round$1((inputCols - fieldSize + 2 * zeroPad) / stride + 1, roundingMode);
|
|
return [outputRows, outputCols];
|
|
}
|
|
function computeDefaultPad(inputShape, fieldSize, stride, dilation) {
|
|
if (dilation === void 0) { dilation = 1; }
|
|
var effectiveFieldSize = getEffectiveFilterSize(fieldSize, dilation);
|
|
return Math.floor((inputShape[0] * (stride - 1) - stride + effectiveFieldSize) / 2);
|
|
}
|
|
function parseTupleParam(param) {
|
|
if (typeof param === 'number') {
|
|
return [param, param, param];
|
|
}
|
|
if (param.length === 2) {
|
|
return [param[0], param[1], 1];
|
|
}
|
|
return param;
|
|
}
|
|
/* See https://www.tensorflow.org/api_docs/python/tf/nn/atrous_conv2d
|
|
* Atrous convolution is equivalent to standard convolution with upsampled
|
|
* filters with effective_filter_height =
|
|
* filter_height + (filter_height - 1) * (dilation - 1)
|
|
* and effective_filter_width =
|
|
* filter_width + (filter_width - 1) * (dilation - 1),
|
|
* produced by inserting dilation - 1 zeros along consecutive elements across
|
|
* the filters' spatial dimensions.
|
|
* When there is a dilation, this converts a filter dimension to the
|
|
* effective filter dimension, so it can be used in a standard convolution.
|
|
*/
|
|
function getEffectiveFilterSize(filterSize, dilation) {
|
|
if (dilation <= 1) {
|
|
return filterSize;
|
|
}
|
|
return filterSize + (filterSize - 1) * (dilation - 1);
|
|
}
|
|
function getPadAndOutInfo(pad, inHeight, inWidth, strideHeight, strideWidth, filterHeight, filterWidth, roundingMode, dataFormat) {
|
|
var padInfo;
|
|
var outHeight;
|
|
var outWidth;
|
|
if (typeof pad === 'number') {
|
|
var padType = (pad === 0) ? 'VALID' : 'NUMBER';
|
|
padInfo = { top: pad, bottom: pad, left: pad, right: pad, type: padType };
|
|
var outShape = computeOutputShape2D([inHeight, inWidth], filterHeight, strideHeight, pad, roundingMode);
|
|
outHeight = outShape[0];
|
|
outWidth = outShape[1];
|
|
}
|
|
else if (pad === 'same') {
|
|
outHeight = Math.ceil(inHeight / strideHeight);
|
|
outWidth = Math.ceil(inWidth / strideWidth);
|
|
var padAlongHeight = Math.max(0, (outHeight - 1) * strideHeight + filterHeight - inHeight);
|
|
var padAlongWidth = Math.max(0, (outWidth - 1) * strideWidth + filterWidth - inWidth);
|
|
var top = Math.floor(padAlongHeight / 2);
|
|
var bottom = padAlongHeight - top;
|
|
var left = Math.floor(padAlongWidth / 2);
|
|
var right = padAlongWidth - left;
|
|
padInfo = { top: top, bottom: bottom, left: left, right: right, type: 'SAME' };
|
|
}
|
|
else if (pad === 'valid') {
|
|
padInfo = { top: 0, bottom: 0, left: 0, right: 0, type: 'VALID' };
|
|
outHeight = Math.ceil((inHeight - filterHeight + 1) / strideHeight);
|
|
outWidth = Math.ceil((inWidth - filterWidth + 1) / strideWidth);
|
|
}
|
|
else if (typeof pad === 'object') {
|
|
var top = dataFormat === 'channelsLast' ? pad[1][0] : pad[2][0];
|
|
var bottom = dataFormat === 'channelsLast' ? pad[1][1] : pad[2][1];
|
|
var left = dataFormat === 'channelsLast' ? pad[2][0] : pad[3][0];
|
|
var right = dataFormat === 'channelsLast' ? pad[2][1] : pad[3][1];
|
|
var padType = (top === 0 && bottom === 0 && left === 0 && right === 0) ?
|
|
'VALID' :
|
|
'EXPLICIT';
|
|
padInfo = { top: top, bottom: bottom, left: left, right: right, type: padType };
|
|
outHeight = round$1((inHeight - filterHeight + top + bottom) / strideHeight + 1, roundingMode);
|
|
outWidth = round$1((inWidth - filterWidth + left + right) / strideWidth + 1, roundingMode);
|
|
}
|
|
else {
|
|
throw Error("Unknown padding parameter: ".concat(pad));
|
|
}
|
|
return { padInfo: padInfo, outHeight: outHeight, outWidth: outWidth };
|
|
}
|
|
/**
|
|
* Rounds a value depending on the rounding mode
|
|
* @param value
|
|
* @param roundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
*/
|
|
function round$1(value, roundingMode) {
|
|
if (!roundingMode) {
|
|
return Math.trunc(value);
|
|
}
|
|
switch (roundingMode) {
|
|
case 'round':
|
|
// used for Caffe Conv
|
|
return Math.round(value);
|
|
case 'ceil':
|
|
// used for Caffe Pool
|
|
return Math.ceil(value);
|
|
case 'floor':
|
|
return Math.floor(value);
|
|
default:
|
|
throw new Error("Unknown roundingMode ".concat(roundingMode));
|
|
}
|
|
}
|
|
function tupleValuesAreOne(param) {
|
|
var _a = __read(parseTupleParam(param), 3), dimA = _a[0], dimB = _a[1], dimC = _a[2];
|
|
return dimA === 1 && dimB === 1 && dimC === 1;
|
|
}
|
|
function eitherStridesOrDilationsAreOne(strides, dilations) {
|
|
return tupleValuesAreOne(strides) || tupleValuesAreOne(dilations);
|
|
}
|
|
function stridesOrDilationsArePositive(values) {
|
|
return parseTupleParam(values).every(function (value) { return 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 ".concat(opDesc, ": pad must be an integer when using ") +
|
|
"dimRoundingMode ".concat(dimRoundingMode, " but got pad ").concat(pad, "."));
|
|
}
|
|
else if (typeof pad === 'number') {
|
|
assert(isInt(pad), function () { return "Error in ".concat(opDesc, ": pad must be an integer when using ") +
|
|
"dimRoundingMode ".concat(dimRoundingMode, " but got pad ").concat(pad, "."); });
|
|
}
|
|
else if (typeof pad === 'object') {
|
|
pad.forEach(function (p) {
|
|
p.forEach(function (v) {
|
|
assert(isInt(v), function () { return "Error in ".concat(opDesc, ": pad must be an integer when using ") +
|
|
"dimRoundingMode ".concat(dimRoundingMode, " but got pad ").concat(v, "."); });
|
|
});
|
|
});
|
|
}
|
|
else {
|
|
throw Error("Error in ".concat(opDesc, ": Unknown padding parameter: ").concat(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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
var $x = convertToTensor(x, 'x', 'reshape', 'string_or_numeric');
|
|
var inputs = { x: $x };
|
|
var attrs = { shape: shape };
|
|
return ENGINE.runKernel(Reshape, inputs, attrs);
|
|
}
|
|
var reshape = /* @__PURE__ */ op({ reshape_: reshape_ });
|
|
|
|
/**
|
|
* @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 2D average pooling of an image.
|
|
*
|
|
* @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 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:
|
|
* - `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.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function avgPool_(x, filterSize, strides, pad, dimRoundingMode) {
|
|
var $x = convertToTensor(x, 'x', 'avgPool', 'float32');
|
|
var dilations = 1;
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in avgPool: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in avgPool: x must be rank 4 but got rank ".concat(x4D.rank, "."); });
|
|
checkPadOnDimRoundingMode('avgPool', pad, dimRoundingMode);
|
|
var inputs = { x: x4D };
|
|
var attrs = { filterSize: filterSize, strides: strides, pad: pad, dimRoundingMode: dimRoundingMode };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(AvgPool, inputs, attrs);
|
|
res = cast(res, $x.dtype);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var avgPool = /* @__PURE__ */ op({ avgPool_: avgPool_ });
|
|
|
|
/**
|
|
* @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 3D average pooling.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor5d([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 2, 2, 1]);
|
|
* const result = tf.avgPool3d(x, 2, 1, 'valid');
|
|
* result.print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor, of rank 5 or rank 4 of shape
|
|
* `[batch, depth, height, width, inChannels]`.
|
|
* @param filterSize The filter size:
|
|
* `[filterDepth, filterHeight, filterWidth]`.
|
|
* If `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 `strideDepth == 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 1*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.
|
|
* @param dataFormat An optional string from: "NDHWC", "NCDHW". Defaults to
|
|
* "NDHWC". Specify the data format of the input and output data. With the
|
|
* default format "NDHWC", the data is stored in the order of: [batch,
|
|
* depth, height, width, channels]. Only "NDHWC" is currently supported.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function avgPool3d_(x, filterSize, strides, pad, dimRoundingMode, dataFormat) {
|
|
if (dataFormat === void 0) { dataFormat = 'NDHWC'; }
|
|
var $x = convertToTensor(x, 'x', 'avgPool3d', 'float32');
|
|
var x5D = $x;
|
|
var reshapedTo5D = false;
|
|
if ($x.rank === 4) {
|
|
reshapedTo5D = true;
|
|
x5D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2], $x.shape[3]]);
|
|
}
|
|
assert(x5D.rank === 5, function () { return "Error in avgPool3d: x must be rank 5 but got rank ".concat(x5D.rank, "."); });
|
|
assert(dataFormat === 'NDHWC', function () { return "Error in avgPool3d: Only NDHWC is currently supported, " +
|
|
"but got dataFormat of ".concat(dataFormat); });
|
|
assert((typeof strides === 'number' && strides > 0) ||
|
|
(Array.isArray(strides) && strides[0] > 0 && strides[1] > 0 &&
|
|
strides[2] > 0), function () { return "Error in avgPool3d: Stride must be > 0, but got '".concat(strides, "'"); });
|
|
checkPadOnDimRoundingMode('avgPool3d', pad, dimRoundingMode);
|
|
var inputs = { x: x5D };
|
|
var attrs = { filterSize: filterSize, strides: strides, pad: pad, dimRoundingMode: dimRoundingMode, dataFormat: dataFormat };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(AvgPool3D, inputs, attrs);
|
|
res = cast(res, x5D.dtype);
|
|
if (reshapedTo5D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]);
|
|
}
|
|
return res;
|
|
}
|
|
var avgPool3d = /* @__PURE__ */ op({ avgPool3d_: avgPool3d_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'clone', 'string_or_numeric');
|
|
var inputs = { x: $x };
|
|
// Note this op is called tf.identity in python. Hence the kernel name used
|
|
// here.
|
|
return ENGINE.runKernel(Identity, inputs);
|
|
}
|
|
var clone = /* @__PURE__ */ op({ clone_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
if (axis === void 0) { axis = 0; }
|
|
assert(tensors.length >= 1, function () { return 'Pass at least one tensor to concat'; });
|
|
var $tensors = convertToTensorArray(tensors, 'tensors', 'concat', 'string_or_numeric');
|
|
if ($tensors[0].dtype === 'complex64') {
|
|
$tensors.forEach(function (tensor) {
|
|
if (tensor.dtype !== 'complex64') {
|
|
throw new Error("Cannot concatenate complex64 tensors with a tensor\n with dtype ".concat(tensor.dtype, ". "));
|
|
}
|
|
});
|
|
}
|
|
if ($tensors.length === 1) {
|
|
return clone($tensors[0]);
|
|
}
|
|
var inputs = $tensors;
|
|
var attr = { axis: axis };
|
|
return ENGINE.runKernel(Concat, inputs, attr);
|
|
}
|
|
var concat = /* @__PURE__ */ op({ concat_: concat_ });
|
|
|
|
/**
|
|
* 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, transposeB) {
|
|
var _a;
|
|
if (transposeA === void 0) { transposeA = false; }
|
|
if (transposeB === void 0) { transposeB = false; }
|
|
var $a = convertToTensor(a, 'a', 'matMul');
|
|
var $b = convertToTensor(b, 'b', 'matMul');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
var attrs = { transposeA: transposeA, transposeB: transposeB };
|
|
return ENGINE.runKernel(BatchMatMul, inputs, attrs);
|
|
}
|
|
var matMul$1 = /* @__PURE__ */ op({ matMul_: matMul_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'mul');
|
|
var $b = convertToTensor(b, 'b', 'mul');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Multiply, inputs);
|
|
}
|
|
var mul = /* @__PURE__ */ op({ mul_: 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 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) {
|
|
var $x = convertToTensor(x, 'x', 'sigmoid', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Sigmoid, inputs);
|
|
}
|
|
var sigmoid = /* @__PURE__ */ op({ sigmoid_: sigmoid_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'slice', 'string_or_numeric');
|
|
if ($x.rank === 0) {
|
|
throw new Error('Slicing scalar is not possible');
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { begin: begin, size: size };
|
|
return ENGINE.runKernel(Slice, inputs, attrs);
|
|
}
|
|
var slice = /* @__PURE__ */ op({ slice_: slice_ });
|
|
|
|
/**
|
|
* @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 tangent of the input `tf.Tensor` element-wise: `tanh(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, 70]);
|
|
*
|
|
* x.tanh().print(); // or tf.tanh(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function tanh_(x) {
|
|
var $x = convertToTensor(x, 'x', 'tanh', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Tanh, inputs);
|
|
}
|
|
var tanh = /* @__PURE__ */ op({ tanh_: tanh_ });
|
|
|
|
/**
|
|
* @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 next state and output of a BasicLSTMCell.
|
|
*
|
|
* Returns `[newC, newH]`.
|
|
*
|
|
* Derived from tf.contrib.rnn.BasicLSTMCell.
|
|
*
|
|
* @param forgetBias Forget bias for the cell.
|
|
* @param lstmKernel The weights for the cell.
|
|
* @param lstmBias The bias for the cell.
|
|
* @param data The input to the cell.
|
|
* @param c Previous cell state.
|
|
* @param h Previous cell output.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'RNN'}
|
|
*/
|
|
function basicLSTMCell_(forgetBias, lstmKernel, lstmBias, data, c, h) {
|
|
var $forgetBias = convertToTensor(forgetBias, 'forgetBias', 'basicLSTMCell');
|
|
var $lstmKernel = convertToTensor(lstmKernel, 'lstmKernel', 'basicLSTMCell');
|
|
var $lstmBias = convertToTensor(lstmBias, 'lstmBias', 'basicLSTMCell');
|
|
var $data = convertToTensor(data, 'data', 'basicLSTMCell');
|
|
var $c = convertToTensor(c, 'c', 'basicLSTMCell');
|
|
var $h = convertToTensor(h, 'h', 'basicLSTMCell');
|
|
var combined = concat([$data, $h], 1);
|
|
var weighted = matMul$1(combined, $lstmKernel);
|
|
var res = add(weighted, $lstmBias);
|
|
// i = input_gate, j = new_input, f = forget_gate, o = output_gate
|
|
var batchSize = res.shape[0];
|
|
var sliceCols = res.shape[1] / 4;
|
|
var sliceSize = [batchSize, sliceCols];
|
|
var i = slice(res, [0, 0], sliceSize);
|
|
var j = slice(res, [0, sliceCols], sliceSize);
|
|
var f = slice(res, [0, sliceCols * 2], sliceSize);
|
|
var o = slice(res, [0, sliceCols * 3], sliceSize);
|
|
var newC = add(mul(sigmoid(i), tanh(j)), mul($c, sigmoid(add($forgetBias, f))));
|
|
var newH = mul(tanh(newC), sigmoid(o));
|
|
return [newC, newH];
|
|
}
|
|
var basicLSTMCell = /* @__PURE__ */ op({ basicLSTMCell_: basicLSTMCell_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'batchToSpaceND');
|
|
var prod = blockShape.reduce(function (a, b) { return a * b; });
|
|
assert($x.rank >= 1 + blockShape.length, function () { return "input rank is ".concat($x.rank, " but should be > than blockShape.length ").concat(blockShape.length); });
|
|
assert(crops.length === blockShape.length, function () { return "crops.length is ".concat(crops.length, " but should be equal to blockShape.length ").concat(blockShape.length); });
|
|
assert($x.shape[0] % prod === 0, function () { return "input tensor batch is ".concat($x.shape[0], " but is not divisible by the product of ") +
|
|
"the elements of blockShape ".concat(blockShape.join(' * '), " === ").concat(prod); });
|
|
var inputs = { x: $x };
|
|
var attrs = { blockShape: blockShape, crops: crops };
|
|
return ENGINE.runKernel(BatchToSpaceND, inputs, attrs);
|
|
}
|
|
var batchToSpaceND = /* @__PURE__ */ op({ batchToSpaceND_: batchToSpaceND_ });
|
|
|
|
function xAs4D(x) {
|
|
var x4D;
|
|
if (x.rank === 0 || x.rank === 1) {
|
|
x4D = reshape(x, [1, 1, 1, x.size]);
|
|
}
|
|
else if (x.rank === 2) {
|
|
x4D = reshape(x, [1, 1, x.shape[0], x.shape[1]]);
|
|
}
|
|
else if (x.rank === 3) {
|
|
x4D = reshape(x, [1, x.shape[0], x.shape[1], x.shape[2]]);
|
|
}
|
|
else {
|
|
x4D = x;
|
|
}
|
|
return x4D;
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Batch normalization.
|
|
*
|
|
* As described in
|
|
* [http://arxiv.org/abs/1502.03167](http://arxiv.org/abs/1502.03167).
|
|
*
|
|
* Mean, variance, scale, and offset can be of two shapes:
|
|
* - The same shape as the input.
|
|
* - In the common case, the depth dimension is the last dimension of x, so
|
|
* the values would be a `tf.Tensor1D` of shape [depth].
|
|
*
|
|
* Also available are stricter rank-specific methods with the same signature
|
|
* as this method that assert that parameters passed are of given rank
|
|
* - `tf.batchNorm2d`
|
|
* - `tf.batchNorm3d`
|
|
* - `tf.batchNorm4d`
|
|
*
|
|
* @param x The input Tensor.
|
|
* @param mean A mean Tensor.
|
|
* @param variance A variance Tensor.
|
|
* @param offset An offset Tensor.
|
|
* @param scale A scale Tensor.
|
|
* @param varianceEpsilon A small float number to avoid dividing by 0.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function batchNorm_(x, mean, variance, offset, scale, varianceEpsilon) {
|
|
if (varianceEpsilon == null) {
|
|
varianceEpsilon = 0.001;
|
|
}
|
|
var $x = convertToTensor(x, 'x', 'batchNorm');
|
|
var $mean = convertToTensor(mean, 'mean', 'batchNorm');
|
|
var $variance = convertToTensor(variance, 'variance', 'batchNorm');
|
|
var $scale;
|
|
if (scale != null) {
|
|
$scale = convertToTensor(scale, 'scale', 'batchNorm');
|
|
}
|
|
var $offset;
|
|
if (offset != null) {
|
|
$offset = convertToTensor(offset, 'offset', 'batchNorm');
|
|
}
|
|
assert($mean.rank === $variance.rank, function () { return 'Batch normalization gradient requires mean and variance to have ' +
|
|
'equal ranks.'; });
|
|
assert($offset == null || $mean.rank === $offset.rank, function () { return 'Batch normalization gradient requires mean and offset to have ' +
|
|
'equal ranks.'; });
|
|
assert($scale == null || $mean.rank === $scale.rank, function () { return 'Batch normalization gradient requires mean and scale to have ' +
|
|
'equal ranks.'; });
|
|
var x4D = xAs4D($x);
|
|
var inputs = {
|
|
x: x4D,
|
|
scale: $scale,
|
|
offset: $offset,
|
|
mean: $mean,
|
|
variance: $variance
|
|
};
|
|
var attrs = { varianceEpsilon: varianceEpsilon };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(FusedBatchNorm, inputs, attrs);
|
|
return reshape(res, $x.shape);
|
|
}
|
|
var batchNorm = /* @__PURE__ */ op({ batchNorm_: batchNorm_ });
|
|
|
|
/**
|
|
* Batch normalization, strictly for 2D. For the more relaxed version, see
|
|
* `tf.batchNorm`.
|
|
*
|
|
* @param x The input Tensor.
|
|
* @param mean A mean Tensor.
|
|
* @param variance A variance Tensor.
|
|
* @param offset An offset Tensor.
|
|
* @param scale A scale Tensor.
|
|
* @param varianceEpsilon A small float number to avoid dividing by 0.
|
|
*/
|
|
function batchNorm2d_(x, mean, variance, offset, scale, varianceEpsilon) {
|
|
var $x = convertToTensor(x, 'x', 'batchNorm');
|
|
var $mean = convertToTensor(mean, 'mean', 'batchNorm');
|
|
var $variance = convertToTensor(variance, 'variance', 'batchNorm');
|
|
var $scale;
|
|
if (scale != null) {
|
|
$scale = convertToTensor(scale, 'scale', 'batchNorm');
|
|
}
|
|
var $offset;
|
|
if (offset != null) {
|
|
$offset = convertToTensor(offset, 'offset', 'batchNorm');
|
|
}
|
|
assert($x.rank === 2, function () { return "Error in batchNorm2D: x must be rank 2 but got rank " +
|
|
"".concat($x.rank, "."); });
|
|
assert($mean.rank === 2 || $mean.rank === 1, function () { return "Error in batchNorm2D: mean must be rank 2 or rank 1 but " +
|
|
"got rank ".concat($mean.rank, "."); });
|
|
assert($variance.rank === 2 || $variance.rank === 1, function () { return "Error in batchNorm2D: variance must be rank 2 or rank 1 " +
|
|
"but got rank ".concat($variance.rank, "."); });
|
|
if ($scale != null) {
|
|
assert($scale.rank === 2 || $scale.rank === 1, function () { return "Error in batchNorm2D: scale must be rank 2 or rank 1 " +
|
|
"but got rank ".concat($scale.rank, "."); });
|
|
}
|
|
if ($offset != null) {
|
|
assert($offset.rank === 2 || $offset.rank === 1, function () { return "Error in batchNorm2D: offset must be rank 2 or rank 1 " +
|
|
"but got rank ".concat($offset.rank, "."); });
|
|
}
|
|
return batchNorm($x, $mean, $variance, $offset, $scale, varianceEpsilon);
|
|
}
|
|
var batchNorm2d = /* @__PURE__ */ op({ batchNorm2d_: batchNorm2d_ });
|
|
|
|
/**
|
|
* Batch normalization, strictly for 3D. For the more relaxed version, see
|
|
* `tf.batchNorm`.
|
|
*
|
|
* @param x The input Tensor.
|
|
* @param mean A mean Tensor.
|
|
* @param variance A variance Tensor.
|
|
* @param offset An offset Tensor.
|
|
* @param scale A scale Tensor.
|
|
* @param varianceEpsilon A small float number to avoid dividing by 0.
|
|
*/
|
|
function batchNorm3d_(x, mean, variance, offset, scale, varianceEpsilon) {
|
|
var $x = convertToTensor(x, 'x', 'batchNorm');
|
|
var $mean = convertToTensor(mean, 'mean', 'batchNorm');
|
|
var $variance = convertToTensor(variance, 'variance', 'batchNorm');
|
|
var $scale;
|
|
if (scale != null) {
|
|
$scale = convertToTensor(scale, 'scale', 'batchNorm');
|
|
}
|
|
var $offset;
|
|
if (offset != null) {
|
|
$offset = convertToTensor(offset, 'offset', 'batchNorm');
|
|
}
|
|
assert($x.rank === 3, function () { return "Error in batchNorm3D: x must be rank 3 but got rank " +
|
|
"".concat($x.rank, "."); });
|
|
assert($mean.rank === 3 || $mean.rank === 1, function () { return "Error in batchNorm3D: mean must be rank 3 or rank 1 but " +
|
|
"got rank ".concat($mean.rank, "."); });
|
|
assert($variance.rank === 3 || $variance.rank === 1, function () { return "Error in batchNorm3D: variance must be rank 3 or rank 1 " +
|
|
"but got rank ".concat($variance.rank, "."); });
|
|
if ($scale != null) {
|
|
assert($scale.rank === 3 || $scale.rank === 1, function () { return "Error in batchNorm3D: scale must be rank 3 or rank 1 " +
|
|
"but got rank ".concat($scale.rank, "."); });
|
|
}
|
|
if ($offset != null) {
|
|
assert($offset.rank === 3 || $offset.rank === 1, function () { return "Error in batchNorm3D: offset must be rank 3 or rank 1 " +
|
|
"but got rank ".concat($offset.rank, "."); });
|
|
}
|
|
return batchNorm($x, $mean, $variance, $offset, $scale, varianceEpsilon);
|
|
}
|
|
var batchNorm3d = /* @__PURE__ */ op({ batchNorm3d_: batchNorm3d_ });
|
|
|
|
/**
|
|
* Batch normalization, strictly for 4D. For the more relaxed version, see
|
|
* `tf.batchNorm`.
|
|
*
|
|
* @param x The input Tensor.
|
|
* @param mean A mean Tensor.
|
|
* @param variance A variance Tensor.
|
|
* @param offset An offset Tensor.
|
|
* @param scale A scale Tensor.
|
|
* @param varianceEpsilon A small float number to avoid dividing by 0.
|
|
*/
|
|
function batchNorm4d_(x, mean, variance, offset, scale, varianceEpsilon) {
|
|
var $x = convertToTensor(x, 'x', 'batchNorm');
|
|
var $mean = convertToTensor(mean, 'mean', 'batchNorm');
|
|
var $variance = convertToTensor(variance, 'variance', 'batchNorm');
|
|
var $scale;
|
|
if (scale != null) {
|
|
$scale = convertToTensor(scale, 'scale', 'batchNorm');
|
|
}
|
|
var $offset;
|
|
if (offset != null) {
|
|
$offset = convertToTensor(offset, 'offset', 'batchNorm');
|
|
}
|
|
assert($x.rank === 4, function () { return "Error in batchNorm4D: x must be rank 4 but got rank " +
|
|
"".concat($x.rank, "."); });
|
|
assert($mean.rank === 4 || $mean.rank === 1, function () { return "Error in batchNorm4D: mean must be rank 4 or rank 1 but " +
|
|
"got rank ".concat($mean.rank, "."); });
|
|
assert($variance.rank === 4 || $variance.rank === 1, function () { return "Error in batchNorm4D: variance must be rank 4 or rank 1 " +
|
|
"but got rank ".concat($variance.rank, "."); });
|
|
if ($scale != null) {
|
|
assert($scale.rank === 4 || $scale.rank === 1, function () { return "Error in batchNorm4D: scale must be rank 4 or rank 1 " +
|
|
"but got rank ".concat($scale.rank, "."); });
|
|
}
|
|
if ($offset != null) {
|
|
assert($offset.rank === 4 || $offset.rank === 1, function () { return "Error in batchNorm4D: offset must be rank 4 or rank 1 " +
|
|
"but got rank ".concat($offset.rank, "."); });
|
|
}
|
|
return batchNorm($x, $mean, $variance, $offset, $scale, varianceEpsilon);
|
|
}
|
|
var batchNorm4d = /* @__PURE__ */ op({ batchNorm4d_: batchNorm4d_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Outputs a vector with length `size` and the same dtype as `weights`.
|
|
*
|
|
* If `weights` are empty, then index `i` stores the number of times the value
|
|
* `i` is counted in `x`. If `weights` are non-empty, then index `i` stores the
|
|
* sum of the value in `weights` at each index where the corresponding value in
|
|
* `x` is `i`.
|
|
*
|
|
* Values in `x` outside of the range [0, size) are ignored.
|
|
*
|
|
* @param x The input int tensor, rank 1.
|
|
* @param weights The weights tensor, must have the same shape as x, or a
|
|
* length-0 Tensor, in which case it acts as all weights equal to 1.
|
|
* @param size Non-negative integer.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Reduction'}
|
|
*/
|
|
function bincount_(x, weights, size) {
|
|
var $x = convertToTensor(x, 'x', 'bincount');
|
|
var $weights = convertToTensor(weights, 'weights', 'bincount');
|
|
assert($x.dtype === 'int32', function () { return "Error in bincount: input " +
|
|
"dtype must be int32, but got ".concat($x.dtype); });
|
|
assert(size >= 0, function () { return "size must be non-negative, but got ".concat(size, "."); });
|
|
assert($weights.size === $x.size || $weights.size === 0, function () { return "Error in bincount: weights must have the same size as input or" +
|
|
"0-length, but got input shape: ".concat($x.shape, ", weights shape: ") +
|
|
"".concat($weights.shape, "."); });
|
|
var inputs = { x: $x, weights: $weights };
|
|
var attrs = { size: size };
|
|
return ENGINE.runKernel(Bincount, inputs, attrs);
|
|
}
|
|
var bincount = /* @__PURE__ */ op({ bincount_: bincount_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Bitwise `AND` operation for input tensors.
|
|
*
|
|
* Given two input tensors, returns a new tensor
|
|
* with the `AND` calculated values.
|
|
*
|
|
* The method supports int32 values
|
|
*
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 5, 3, 14], 'int32');
|
|
* const y = tf.tensor1d([5, 0, 7, 11], 'int32');
|
|
* tf.bitwiseAnd(x, y).print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor to be calculated.
|
|
* @param y The input tensor to be calculated.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Logical'}
|
|
*/
|
|
function bitwiseAnd_(x, y) {
|
|
var $x = convertToTensor(x, 'x', 'bitwiseAnd');
|
|
var $y = convertToTensor(y, 'y', 'bitwiseAnd');
|
|
if (!arraysEqual($x.shape, $y.shape)) {
|
|
throw new Error("BitwiseAnd: Tensors must have the same shape. x: ".concat($x.shape, ", y: ").concat($y.shape));
|
|
}
|
|
if ($x.dtype !== 'int32' || $y.dtype !== 'int32') {
|
|
throw new Error("BitwiseAnd: Only supports 'int32' values in tensor, found type of x: ".concat($x.dtype, " and type of y: ").concat($y.dtype));
|
|
}
|
|
var inputs = { a: $x, b: $y };
|
|
return ENGINE.runKernel(BitwiseAnd, inputs);
|
|
}
|
|
var bitwiseAnd = /* @__PURE__ */ op({ bitwiseAnd_: bitwiseAnd_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Return the shape of s0 op s1 with broadcast.
|
|
*
|
|
* compute r0, the broadcasted shape as a tensor.
|
|
* s0, s1 and r0 are all integer vectors.
|
|
*
|
|
* This function returns the shape of the result of an operation between
|
|
* two tensors of size s0 and s1 performed with broadcast.
|
|
*
|
|
* @param s0 A tensor representing a shape
|
|
* @param s1 A tensor representing a shape
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Transformations'}
|
|
*/
|
|
function broadcastArgs_(s0, s1) {
|
|
var shape1Input = convertToTensor(s0, 's0', 'broadcastArgs', 'int32');
|
|
var shape2Input = convertToTensor(s1, 's1', 'broadcastArgs', 'int32');
|
|
if (shape1Input.rank !== 1) {
|
|
throw new Error('broadcastArgs(): first input must be a vector (rank=1). ' +
|
|
"Has rank ".concat(shape1Input.rank));
|
|
}
|
|
if (shape2Input.rank !== 1) {
|
|
throw new Error('broadcastArgs(): second input must be a vector (rank=1). ' +
|
|
"Has rank ".concat(shape2Input.rank));
|
|
}
|
|
var inputs = { s0: shape1Input, s1: shape2Input };
|
|
return ENGINE.runKernel(BroadcastArgs, inputs);
|
|
}
|
|
var broadcastArgs = /* @__PURE__ */ op({ broadcastArgs_: broadcastArgs_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var input = convertToTensor(x, 'broadcastTo', 'x');
|
|
var xShape = input.shape;
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (shape.length < input.rank) {
|
|
throw new Error("broadcastTo(): shape.length=".concat(shape.length, " < input.rank=").concat(input.rank, "."));
|
|
}
|
|
if (shape.length > input.rank) {
|
|
var newShape = input.shape.slice();
|
|
while (newShape.length < shape.length) {
|
|
newShape.unshift(1);
|
|
}
|
|
input = reshape(input, newShape);
|
|
}
|
|
var inputShape = input.shape;
|
|
var reps = Array.from(shape);
|
|
for (var 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(): [".concat(xShape, "] cannot be broadcast to [").concat(shape, "]."));
|
|
}
|
|
}
|
|
var axes = reps.map(function (n, i) { return n > 1 ? i : -1; }).filter(function (i) { return i >= 0; });
|
|
if (axes.length === 0) {
|
|
return clone(input);
|
|
}
|
|
// TODO call broadcastTo kernel directly once backends implement broadcstTo
|
|
var inputs = { x: input };
|
|
var attrs = { reps: reps };
|
|
return ENGINE.runKernel(Tile, inputs, attrs);
|
|
}
|
|
var broadcastTo = /* @__PURE__ */ op({ broadcastTo_: broadcastTo_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates an empty `tf.TensorBuffer` with the specified `shape` and `dtype`.
|
|
*
|
|
* The values are stored in CPU as `TypedArray`. Fill the buffer using
|
|
* `buffer.set()`, or by modifying directly `buffer.values`.
|
|
*
|
|
* When done, call `buffer.toTensor()` to get an immutable `tf.Tensor` with
|
|
* those values.
|
|
*
|
|
* ```js
|
|
* // Create a buffer and set values at particular indices.
|
|
* const buffer = tf.buffer([2, 2]);
|
|
* buffer.set(3, 0, 0);
|
|
* buffer.set(5, 1, 0);
|
|
*
|
|
* // Convert the buffer back to a tensor.
|
|
* buffer.toTensor().print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param dtype The dtype of the buffer. Defaults to 'float32'.
|
|
* @param values The values of the buffer as `TypedArray`. Defaults to
|
|
* zeros.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function buffer(shape, dtype, values) {
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
dtype = dtype || 'float32';
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
return new TensorBuffer(shape, dtype, values);
|
|
}
|
|
|
|
/**
|
|
* @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 ceiling of input `tf.Tensor` element-wise: `ceil(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([.6, 1.1, -3.3]);
|
|
*
|
|
* x.ceil().print(); // or tf.ceil(x)
|
|
* ```
|
|
* @param x The input Tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function ceil_(x) {
|
|
var $x = convertToTensor(x, 'x', 'ceil', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Ceil, inputs);
|
|
}
|
|
var ceil = /* @__PURE__ */ op({ ceil_: ceil_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` filled with a scalar value.
|
|
*
|
|
* ```js
|
|
* tf.fill([2, 2], 4).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param value The scalar value to fill the tensor with.
|
|
* @param dtype The type of an element in the resulting tensor. Defaults to
|
|
* 'float32' if the given param value is a number, otherwise 'string'.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function fill(shape, value, dtype) {
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
dtype = dtype || inferDtype(value);
|
|
var attrs = { shape: shape, value: value, dtype: dtype };
|
|
return ENGINE.runKernel(Fill, {}, attrs);
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Clips values element-wise. `max(min(x, clipValueMax), clipValueMin)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
*
|
|
* x.clipByValue(-2, 3).print(); // or tf.clipByValue(x, -2, 3)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @param clipValueMin Lower bound of range to be clipped to.
|
|
* @param clipValueMax Upper bound of range to be clipped to.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function clipByValue_(x, clipValueMin, clipValueMax) {
|
|
var $x = convertToTensor(x, 'x', 'clipByValue');
|
|
assert((clipValueMin <= clipValueMax), function () { return "Error in clip: min (".concat(clipValueMin, ") must be ") +
|
|
"less than or equal to max (".concat(clipValueMax, ")."); });
|
|
if (clipValueMin === clipValueMax) {
|
|
return fill($x.shape, clipValueMin, $x.dtype);
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { clipValueMin: clipValueMin, clipValueMax: clipValueMax };
|
|
return ENGINE.runKernel(ClipByValue, inputs, attrs);
|
|
}
|
|
var clipByValue = /* @__PURE__ */ op({ clipByValue_: clipByValue_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $real = convertToTensor(real, 'real', 'complex');
|
|
var $imag = convertToTensor(imag, 'imag', 'complex');
|
|
assertShapesMatch($real.shape, $imag.shape, "real and imag shapes, ".concat($real.shape, " and ").concat($imag.shape, ", ") +
|
|
"must match in call to tf.complex().");
|
|
var inputs = { real: $real, imag: $imag };
|
|
return ENGINE.runKernel(Complex, inputs);
|
|
}
|
|
var complex = /* @__PURE__ */ op({ complex_: complex_ });
|
|
|
|
/**
|
|
* Concatenates a list of`tf.Tensor1D`s along an axis. See `concat` for details.
|
|
*
|
|
* For example, if:
|
|
* A: shape(3) = |r1, g1, b1|
|
|
* B: shape(2) = |r2, g2|
|
|
* C = tf.concat1d([A, B]) == |r1, g1, b1, r2, g2|
|
|
*
|
|
* @param tensors A list of`tf.Tensor`s to concatenate.
|
|
* @return The concatenated array.
|
|
*/
|
|
function concat1d_(tensors) {
|
|
return concat(tensors, 0 /* axis */);
|
|
}
|
|
var concat1d = /* @__PURE__ */ op({ concat1d_: concat1d_ });
|
|
|
|
/**
|
|
* Concatenates a list of`tf.Tensor2D`s along an axis. See `concat` for details.
|
|
*
|
|
* For example, if:
|
|
* A: shape(2, 3) = | r1, g1, b1 |
|
|
* | r2, g2, b2 |
|
|
*
|
|
* B: shape(2, 3) = | r3, g3, b3 |
|
|
* | r4, g4, b4 |
|
|
*
|
|
* C = tf.concat2d([A, B], axis)
|
|
*
|
|
* if axis = 0:
|
|
* C: shape(4, 3) = | r1, g1, b1 |
|
|
* | r2, g2, b2 |
|
|
* | r3, g3, b3 |
|
|
* | r4, g4, b4 |
|
|
*
|
|
* if axis = 1:
|
|
* C = shape(2, 6) = | r1, g1, b1, r3, g3, b3 |
|
|
* | r2, g2, b2, r4, g4, b4 |
|
|
*
|
|
*
|
|
* @param tensors A list of `tf.Tensor`s to concatenate.
|
|
* @param axis The axis to concatenate along.
|
|
* @return The concatenated array.
|
|
*/
|
|
function concat2d_(tensors, axis) {
|
|
return concat(tensors, axis);
|
|
}
|
|
var concat2d = /* @__PURE__ */ op({ concat2d_: concat2d_ });
|
|
|
|
/**
|
|
* Concatenates a list of `tf.Tensor3D`s along an axis.
|
|
* See `concat` for details.
|
|
*
|
|
* For example, if:
|
|
* A: shape(2, 1, 3) = | r1, g1, b1 |
|
|
* | r2, g2, b2 |
|
|
*
|
|
* B: shape(2, 1, 3) = | r3, g3, b3 |
|
|
* | r4, g4, b4 |
|
|
*
|
|
* C = tf.concat3d([A, B], axis)
|
|
*
|
|
* if axis = 0:
|
|
* C: shape(4, 1, 3) = | r1, g1, b1 |
|
|
* | r2, g2, b2 |
|
|
* | r3, g3, b3 |
|
|
* | r4, g4, b4 |
|
|
*
|
|
* if axis = 1:
|
|
* C: shape(2, 2, 3) = | r1, g1, b1, r3, g3, b3 |
|
|
* | r2, g2, b2, r4, g4, b4 |
|
|
*
|
|
* if axis = 2:
|
|
* C = shape(2, 1, 6) = | r1, g1, b1, r3, g3, b3 |
|
|
* | r2, g2, b2, r4, g4, b4 |
|
|
*
|
|
* @param tensors A list of`tf.Tensor`s to concatenate.
|
|
* @param axis The axis to concate along.
|
|
* @return The concatenated array.
|
|
*/
|
|
function concat3d_(tensors, axis) {
|
|
return concat(tensors, axis);
|
|
}
|
|
var concat3d = /* @__PURE__ */ op({ concat3d_: concat3d_ });
|
|
|
|
/**
|
|
* Concatenates a list of `tf.Tensor4D`s along an axis.
|
|
* See `concat` for details.
|
|
*
|
|
* @param tensors A list of `tf.Tensor`s to concatenate.
|
|
* @param axis The axis to concate along.
|
|
* @return The concatenated array.
|
|
*/
|
|
function concat4d_(tensors, axis) {
|
|
return concat(tensors, axis);
|
|
}
|
|
var concat4d = /* @__PURE__ */ op({ concat4d_: concat4d_ });
|
|
|
|
/**
|
|
* @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, dilations, dimRoundingMode) {
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
if (dilations === void 0) { dilations = [1, 1]; }
|
|
var $x = convertToTensor(x, 'x', 'conv2d', 'float32');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv2d', 'float32');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in conv2d: input must be rank 4, but got rank ".concat(x4D.rank, "."); });
|
|
assert($filter.rank === 4, function () { return "Error in conv2d: filter must be rank 4, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
checkPadOnDimRoundingMode('conv2d', pad, dimRoundingMode);
|
|
var inDepth = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1];
|
|
assert(inDepth === $filter.shape[2], function () { return "Error in conv2d: depth of input (".concat(inDepth, ") must match ") +
|
|
"input depth for filter ".concat($filter.shape[2], "."); });
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in conv2D: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
assert(stridesOrDilationsArePositive(dilations), function () { return 'Error in conv2D: Dilated rates should be larger than 0.'; });
|
|
assert(stridesOrDilationsArePositive(strides), function () { return 'Error in conv2D: Strides should be larger than 0.'; });
|
|
var inputs = { x: x4D, filter: $filter };
|
|
var attrs = { strides: strides, pad: pad, dataFormat: dataFormat, dilations: dilations, dimRoundingMode: dimRoundingMode };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Conv2D, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var conv2d$1 = /* @__PURE__ */ op({ conv2d_: conv2d_ });
|
|
|
|
/**
|
|
* Computes a 1D convolution over the input x.
|
|
*
|
|
* @param x The input tensor, of rank 3 or rank 2, of shape
|
|
* `[batch, width, inChannels]`. If rank 2, batch of 1 is assumed.
|
|
* @param filter The filter, rank 3, of shape
|
|
* `[filterWidth, inDepth, outDepth]`.
|
|
* @param stride The number of entries by which the filter is moved right at
|
|
* each step.
|
|
* @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 "NWC", "NCW". Defaults to "NWC",
|
|
* the data is stored in the order of [batch, in_width, in_channels]. Only
|
|
* "NWC" is currently supported.
|
|
* @param dilation The dilation rate in which we sample input values in
|
|
* atrous convolution. Defaults to `1`. If it is greater than 1, then
|
|
* stride must be `1`.
|
|
* @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function conv1d_(x, filter, stride, pad, dataFormat, dilation, dimRoundingMode) {
|
|
if (dataFormat === void 0) { dataFormat = 'NWC'; }
|
|
if (dilation === void 0) { dilation = 1; }
|
|
var $x = convertToTensor(x, 'x', 'conv1d');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv1d');
|
|
var x3D = $x;
|
|
var reshapedTo3D = false;
|
|
if ($x.rank === 2) {
|
|
reshapedTo3D = true;
|
|
x3D = reshape($x, [1, $x.shape[0], $x.shape[1]]);
|
|
}
|
|
assert(x3D.rank === 3, function () { return "Error in conv1d: input must be rank 3, but got rank ".concat(x3D.rank, "."); });
|
|
assert($filter.rank === 3, function () { return "Error in conv1d: filter must be rank 3, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
checkPadOnDimRoundingMode('conv1d', pad, dimRoundingMode);
|
|
assert(x3D.shape[2] === $filter.shape[1], function () { return "Error in conv1d: depth of input (".concat(x3D.shape[2], ") must match ") +
|
|
"input depth for filter ".concat($filter.shape[1], "."); });
|
|
assert(eitherStridesOrDilationsAreOne(stride, dilation), function () { return 'Error in conv1D: Either stride or dilation must be 1. ' +
|
|
"Got stride ".concat(stride, " and dilation '").concat(dilation, "'"); });
|
|
assert(stridesOrDilationsArePositive(dilation), function () { return 'Error in conv1D: Dilated rates should be larger than 0.'; });
|
|
assert(stridesOrDilationsArePositive(stride), function () { return 'Error in conv1D: Stride should be larger than 0.'; });
|
|
assert(dataFormat === 'NWC', function () { return "Error in conv1d: got dataFormat of ".concat(dataFormat, " but only NWC is currently supported."); });
|
|
var filter4D = reshape($filter, [1, $filter.shape[0], $filter.shape[1], $filter.shape[2]]);
|
|
var input4D = reshape(x3D, [x3D.shape[0], 1, x3D.shape[1], x3D.shape[2]]);
|
|
var strides = [1, stride];
|
|
var dilations = [1, dilation];
|
|
var conv2dDataFormat = 'NHWC';
|
|
var res = conv2d$1(input4D, filter4D, strides, pad, conv2dDataFormat, dilations, dimRoundingMode);
|
|
if (reshapedTo3D) {
|
|
return reshape(res, [res.shape[2], res.shape[3]]);
|
|
}
|
|
return reshape(res, [res.shape[0], res.shape[2], res.shape[3]]);
|
|
}
|
|
var conv1d = /* @__PURE__ */ op({ conv1d_: conv1d_ });
|
|
|
|
/**
|
|
* @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, dimRoundingMode) {
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
assert(xShape.length === dy.rank, function () { return "Length of inShape " +
|
|
"(".concat(xShape.length, ") and rank of dy (").concat(dy.rank, ") must match"); });
|
|
var xShape4D = xShape;
|
|
var dy4D = dy;
|
|
var reshapedTo4D = false;
|
|
if (dy.rank === 3) {
|
|
reshapedTo4D = true;
|
|
dy4D = reshape(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]);
|
|
xShape4D = [1, xShape[0], xShape[1], xShape[2]];
|
|
}
|
|
assert(xShape4D.length === 4, function () { return "Error in conv2dDerInput: inShape must be length 4, but got length " +
|
|
"".concat(xShape4D.length, "."); });
|
|
assert(dy4D.rank === 4, function () { return "Error in conv2dDerInput: dy must be rank 4, but got " +
|
|
"rank ".concat(dy4D.rank); });
|
|
assert(filter.rank === 4, function () { return "Error in conv2dDerInput: filter must be rank 4, but got " +
|
|
"rank ".concat(filter.rank); });
|
|
var inDepth = dataFormat === 'NHWC' ? xShape4D[3] : xShape4D[1];
|
|
var outDepth = dataFormat === 'NHWC' ? dy4D.shape[3] : dy4D.shape[1];
|
|
assert(inDepth === filter.shape[2], function () { return "Error in conv2dDerInput: depth of input (".concat(inDepth, ") must ") +
|
|
"match input depth for filter ".concat(filter.shape[2], "."); });
|
|
assert(outDepth === filter.shape[3], function () { return "Error in conv2dDerInput: depth of output (".concat(outDepth, ") must ") +
|
|
"match output depth for filter ".concat(filter.shape[3], "."); });
|
|
checkPadOnDimRoundingMode('conv2dDerInput', pad, dimRoundingMode);
|
|
var inputs = { dy: dy4D, filter: filter };
|
|
var attrs = { strides: strides, pad: pad, dataFormat: dataFormat, dimRoundingMode: dimRoundingMode, inputShape: xShape4D };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Conv2DBackpropInput, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var conv2DBackpropInput = /* @__PURE__ */ op({ conv2DBackpropInput_: conv2DBackpropInput_ });
|
|
|
|
/**
|
|
* Computes the transposed 2D convolution of an image, also known as a
|
|
* deconvolution.
|
|
*
|
|
* @param x The input image, of rank 4 or rank 3, of shape
|
|
* `[batch, height, width, inDepth]`. If rank 3, batch of 1 is assumed.
|
|
* @param filter The filter, rank 4, of shape
|
|
* `[filterHeight, filterWidth, outDepth, inDepth]`.
|
|
* `inDepth` must match `inDepth` in `x`.
|
|
* @param outputShape Output shape, of rank 4 or rank 3:
|
|
* `[batch, height, width, outDepth]`. If rank 3, batch of 1 is assumed.
|
|
* @param strides The strides of the original convolution:
|
|
* `[strideHeight, strideWidth]`.
|
|
* @param pad The type of padding algorithm used in the non-transpose version
|
|
* of the op.
|
|
* @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function conv2dTranspose_(x, filter, outputShape, strides, pad, dimRoundingMode) {
|
|
var $x = convertToTensor(x, 'x', 'conv2dTranspose');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv2dTranspose');
|
|
return conv2DBackpropInput(outputShape, $x, $filter, strides, pad, 'NHWC', dimRoundingMode);
|
|
}
|
|
var conv2dTranspose = /* @__PURE__ */ op({ conv2dTranspose_: conv2dTranspose_ });
|
|
|
|
/**
|
|
* @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 3D convolution over the input x.
|
|
*
|
|
* @param x The input tensor, of rank 5 or rank 4, of shape
|
|
* `[batch, depth, height, width, channels]`. If rank 4,
|
|
* batch of 1 is assumed.
|
|
* @param filter The filter, rank 5, of shape
|
|
* `[filterDepth, filterHeight, filterWidth, inChannels, outChannels]`.
|
|
* inChannels must match between input and filter.
|
|
* @param strides The strides of the convolution: `[strideDepth, 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: "NDHWC", "NCDHW". Defaults to
|
|
* "NDHWC". Specify the data format of the input and output data. With the
|
|
* default format "NDHWC", the data is stored in the order of: [batch,
|
|
* depth, height, width, channels]. Only "NDHWC" is currently supported.
|
|
* @param dilations The dilation rates: `[dilationDepth, dilationHeight,
|
|
* dilationWidth]` in which we sample input values across the height
|
|
* and width dimensions in atrous convolution. Defaults to `[1, 1, 1]`.
|
|
* If `dilations` is a single number, then
|
|
* `dilationDepth == dilationHeight == dilationWidth`. If it is greater
|
|
* than 1, then all values of `strides` must be 1.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function conv3d_(x, filter, strides, pad, dataFormat, dilations) {
|
|
if (dataFormat === void 0) { dataFormat = 'NDHWC'; }
|
|
if (dilations === void 0) { dilations = [1, 1, 1]; }
|
|
var $x = convertToTensor(x, 'x', 'conv3d');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv3d');
|
|
var x5D = $x;
|
|
var reshapedTo5D = false;
|
|
if ($x.rank === 4) {
|
|
reshapedTo5D = true;
|
|
x5D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2], $x.shape[3]]);
|
|
}
|
|
assert(x5D.rank === 5, function () { return "Error in conv3d: input must be rank 5, but got rank ".concat(x5D.rank, "."); });
|
|
assert($filter.rank === 5, function () { return "Error in conv3d: filter must be rank 5, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
assert(x5D.shape[4] === $filter.shape[3], function () { return "Error in conv3d: depth of input (".concat(x5D.shape[4], ") must match ") +
|
|
"input depth for filter ".concat($filter.shape[3], "."); });
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in conv3D: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
assert(dataFormat === 'NDHWC', function () { return "Error in conv3d: got dataFormat of ".concat(dataFormat, " but only NDHWC is currently supported."); });
|
|
assert(stridesOrDilationsArePositive(dilations), function () { return 'Error in conv3D: Dilated rates should be larger than 0.'; });
|
|
assert(stridesOrDilationsArePositive(strides), function () { return 'Error in conv3D: Strides should be larger than 0.'; });
|
|
var inputs = { x: x5D, filter: $filter };
|
|
var attrs = { strides: strides, pad: pad, dataFormat: dataFormat, dilations: dilations };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Conv3D, inputs, attrs);
|
|
if (reshapedTo5D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]);
|
|
}
|
|
return res;
|
|
}
|
|
var conv3d = /* @__PURE__ */ op({ conv3d_: conv3d_ });
|
|
|
|
/**
|
|
* @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, function () { return "Length of inShape " +
|
|
"(".concat(xShape.length, ") and rank of dy (").concat(dy.rank, ") must match"); });
|
|
var xShape5D = xShape;
|
|
var dy5D = dy;
|
|
var reshapedTo5D = false;
|
|
if (dy.rank === 4) {
|
|
reshapedTo5D = true;
|
|
dy5D = reshape(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2], dy.shape[3]]);
|
|
xShape5D = [1, xShape[0], xShape[1], xShape[2], xShape[3]];
|
|
}
|
|
var inDepth = xShape5D[4];
|
|
var outDepth = dy5D.shape[4];
|
|
assert(xShape5D.length === 5, function () { return "Error in conv3dDerInput: inShape must be length 5, but got length " +
|
|
"".concat(xShape5D.length, "."); });
|
|
assert(dy5D.rank === 5, function () { return "Error in conv3dDerInput: dy must be rank 5, but got " +
|
|
"rank ".concat(dy5D.rank); });
|
|
assert(filter.rank === 5, function () { return "Error in conv3dDerInput: filter must be rank 5, but got " +
|
|
"rank ".concat(filter.rank); });
|
|
assert(inDepth === filter.shape[3], function () { return "Error in conv3dDerInput: depth of input (".concat(inDepth, ") must ") +
|
|
"match input depth for filter ".concat(filter.shape[3], "."); });
|
|
assert(outDepth === filter.shape[4], function () { return "Error in conv3dDerInput: depth of output (".concat(outDepth, ") must ") +
|
|
"match output depth for filter ".concat(filter.shape[4], "."); });
|
|
var inputs = { dy: dy5D, filter: filter };
|
|
var attrs = { pad: pad, strides: strides, inputShape: xShape5D };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Conv3DBackpropInputV2, inputs, attrs);
|
|
if (reshapedTo5D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]);
|
|
}
|
|
return res;
|
|
}
|
|
var conv3DBackpropInput = /* @__PURE__ */ op({ conv3DBackpropInput_: conv3DBackpropInput_ });
|
|
|
|
/**
|
|
* Computes the transposed 3D convolution of a volume, also known as a
|
|
* deconvolution.
|
|
*
|
|
* @param x The input image, of rank 5 or rank 4, of shape
|
|
* `[batch, depth, height, width, inDepth]`. If rank 4, batch of 1 is assumed.
|
|
* @param filter The filter, rank 4, of shape
|
|
* `[depth, filterHeight, filterWidth, outDepth, inDepth]`.
|
|
* `inDepth` must match `inDepth` in `x`.
|
|
* @param outputShape Output shape, of rank 5 or rank 4:
|
|
* `[batch, depth, height, width, outDepth]`. If rank 3, batch of 1 is
|
|
* assumed.
|
|
* @param strides The strides of the original convolution:
|
|
* `[strideDepth, strideHeight, strideWidth]`.
|
|
* @param pad The type of padding algorithm used in the non-transpose version
|
|
* of the op.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function conv3dTranspose_(x, filter, outputShape, strides, pad) {
|
|
var $x = convertToTensor(x, 'x', 'conv3dTranspose');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv3dTranspose');
|
|
return conv3DBackpropInput(outputShape, $x, $filter, strides, pad);
|
|
}
|
|
var conv3dTranspose = /* @__PURE__ */ op({ conv3dTranspose_: conv3dTranspose_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'cos', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Cos, inputs);
|
|
}
|
|
var cos = /* @__PURE__ */ op({ cos_: 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) {
|
|
var $x = convertToTensor(x, 'x', 'cosh', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Cosh, inputs);
|
|
}
|
|
var cosh = /* @__PURE__ */ op({ cosh_: 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, exclusive, reverse) {
|
|
if (axis === void 0) { axis = 0; }
|
|
if (exclusive === void 0) { exclusive = false; }
|
|
if (reverse === void 0) { reverse = false; }
|
|
var $x = convertToTensor(x, 'x', 'cumprod');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, exclusive: exclusive, reverse: reverse };
|
|
return ENGINE.runKernel(Cumprod, inputs, attrs);
|
|
}
|
|
var cumprod = /* @__PURE__ */ op({ cumprod_: cumprod_ });
|
|
|
|
/**
|
|
* @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, exclusive, reverse) {
|
|
if (axis === void 0) { axis = 0; }
|
|
if (exclusive === void 0) { exclusive = false; }
|
|
if (reverse === void 0) { reverse = false; }
|
|
var $x = convertToTensor(x, 'x', 'cumsum');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, exclusive: exclusive, reverse: reverse };
|
|
return ENGINE.runKernel(Cumsum, inputs, attrs);
|
|
}
|
|
var cumsum = /* @__PURE__ */ op({ cumsum_: cumsum_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Outputs a vector with length `size` and the same dtype as `weights`.
|
|
*
|
|
* If `weights` are empty, then index `i` stores the number of times the value
|
|
* `i` is counted in `x`. If `weights` are non-empty, then index `i` stores the
|
|
* sum of the value in `weights` at each index where the corresponding value in
|
|
* `x` is `i`.
|
|
*
|
|
* Values in `x` outside of the range [0, size) are ignored.
|
|
*
|
|
* @param x The input int tensor, rank 1 or rank 2.
|
|
* @param weights The weights tensor, must have the same shape as x, or a
|
|
* length-0 Tensor, in which case it acts as all weights equal to 1.
|
|
* @param size Non-negative integer.
|
|
* @param binaryOutput Optional. Whether the kernel should count the appearance
|
|
* or number of occurrences. Defaults to False.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Reduction'}
|
|
*/
|
|
function denseBincount_(x, weights, size, binaryOutput) {
|
|
if (binaryOutput === void 0) { binaryOutput = false; }
|
|
var $x = convertToTensor(x, 'x', 'denseBincount');
|
|
var $weights = convertToTensor(weights, 'weights', 'denseBincount');
|
|
assert($x.dtype === 'int32', function () { return "Error in denseBincount: input " +
|
|
"dtype must be int32, but got ".concat($x.dtype); });
|
|
assert($x.rank <= 2, function () { return "Error in denseBincount: input must be at most rank 2, but got " +
|
|
"rank ".concat($x.rank, "."); });
|
|
assert(size >= 0, function () { return "size must be non-negative, but got ".concat(size, "."); });
|
|
assert($weights.size === $x.size || $weights.size === 0, function () { return "Error in denseBincount: weights must have the same shape as x or " +
|
|
"0-length, but got x shape: ".concat($x.shape, ", weights shape: ") +
|
|
"".concat($weights.shape, "."); });
|
|
var inputs = { x: $x, weights: $weights };
|
|
var attrs = { size: size, binaryOutput: binaryOutput };
|
|
return ENGINE.runKernel(DenseBincount, inputs, attrs);
|
|
}
|
|
var denseBincount = /* @__PURE__ */ op({ denseBincount_: denseBincount_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Rearranges data from depth into blocks of spatial data. More specifically,
|
|
* this op outputs a copy of the input tensor where values from the `depth`
|
|
* dimension are moved in spatial blocks to the `height` and `width` dimensions.
|
|
* The attr `blockSize` indicates the input block size and how the data is
|
|
* moved.
|
|
*
|
|
* - Chunks of data of size `blockSize * blockSize` from depth are rearranged
|
|
* into non-overlapping blocks of size `blockSize x blockSize`
|
|
*
|
|
* - The width the output tensor is `inputWidth * blockSize`, whereas the
|
|
* height is `inputHeight * blockSize`
|
|
*
|
|
* - The Y, X coordinates within each block of the output image are determined
|
|
* by the high order component of the input channel index
|
|
*
|
|
* - The depth of the input tensor must be divisible by `blockSize *
|
|
* blockSize`
|
|
*
|
|
* The `dataFormat` attr specifies the layout of the input and output tensors
|
|
* with the following options: "NHWC": [ `batch, height, width, channels` ]
|
|
* "NCHW": [ `batch, channels, height, width` ]
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor4d([1, 2, 3, 4], [1, 1, 1, 4]);
|
|
* const blockSize = 2;
|
|
* const dataFormat = "NHWC";
|
|
*
|
|
* tf.depthToSpace(x, blockSize, dataFormat).print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor of rank 4
|
|
* @param blockSIze An `int` that is `>= 2`. The size of the spatial block
|
|
* @param dataFormat An optional string from: "NHWC", "NCHW". Defaults to "NHWC"
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Transformations'}
|
|
*/
|
|
function depthToSpace_(x, blockSize, dataFormat) {
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
var $x = convertToTensor(x, 'x', 'depthToSpace', 'float32');
|
|
var inputHeight = (dataFormat === 'NHWC') ? $x.shape[1] : $x.shape[2];
|
|
var inputWidth = (dataFormat === 'NHWC') ? $x.shape[2] : $x.shape[3];
|
|
var inputDepth = (dataFormat === 'NHWC') ? $x.shape[3] : $x.shape[1];
|
|
assert(blockSize > 1, function () { return "blockSize should be > 1 for depthToSpace, but was: ".concat(blockSize); });
|
|
assert(inputHeight * blockSize >= 0, function () { return "Negative dimension size caused by overflow when multiplying\n ".concat(inputHeight, " and ").concat(blockSize, " for depthToSpace with input shape\n ").concat($x.shape); });
|
|
assert(inputWidth * blockSize >= 0, function () { return "Negative dimension size caused by overflow when multiplying\n ".concat(inputWidth, " and ").concat(blockSize, " for depthToSpace with input shape\n ").concat($x.shape); });
|
|
assert((inputDepth % (blockSize * blockSize) === 0), function () { return "Dimension size must be evenly divisible by ".concat(blockSize * blockSize, " but is ").concat(inputDepth, " for depthToSpace with input shape ").concat($x.shape); });
|
|
var inputs = { x: $x };
|
|
var attrs = { blockSize: blockSize, dataFormat: dataFormat };
|
|
return ENGINE.runKernel(DepthToSpace, inputs, attrs);
|
|
}
|
|
var depthToSpace = /* @__PURE__ */ op({ depthToSpace_: depthToSpace_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Depthwise 2D convolution.
|
|
*
|
|
* Given a 4D `input` array and a `filter` array of shape
|
|
* `[filterHeight, filterWidth, inChannels, channelMultiplier]` containing
|
|
* `inChannels` convolutional filters of depth 1, this op applies a
|
|
* different filter to each input channel (expanding from 1 channel to
|
|
* `channelMultiplier` channels for each), then concatenates the results
|
|
* together. The output has `inChannels * channelMultiplier` channels.
|
|
*
|
|
* See
|
|
* [https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d](
|
|
* https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d)
|
|
* for more details.
|
|
*
|
|
* @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 tensor, rank 4, of shape
|
|
* `[filterHeight, filterWidth, inChannels, channelMultiplier]`.
|
|
* @param strides The strides of the convolution: `[strideHeight,
|
|
* strideWidth]`. If strides is a single number, then `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 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 `rate` is a single
|
|
* number, then `dilationHeight == dilationWidth`. If it is greater than
|
|
* 1, then all values of `strides` must be 1.
|
|
* @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]. Only "NHWC" is currently supported.
|
|
* @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function depthwiseConv2d_(x, filter, strides, pad, dataFormat, dilations, dimRoundingMode) {
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
if (dilations === void 0) { dilations = [1, 1]; }
|
|
var $x = convertToTensor(x, 'x', 'depthwiseConv2d', 'float32');
|
|
var $filter = convertToTensor(filter, 'filter', 'depthwiseConv2d', 'float32');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in depthwiseConv2d: input must be rank 4, but got " +
|
|
"rank ".concat(x4D.rank, "."); });
|
|
assert($filter.rank === 4, function () { return "Error in depthwiseConv2d: filter must be rank 4, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
var inChannels = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1];
|
|
assert(inChannels === $filter.shape[2], function () { return "Error in depthwiseConv2d: number of input channels " +
|
|
"(".concat(inChannels, ") must match the inChannels dimension in ") +
|
|
"filter ".concat($filter.shape[2], "."); });
|
|
checkPadOnDimRoundingMode('depthwiseConv2d', pad, dimRoundingMode);
|
|
var inputs = { x: x4D, filter: $filter };
|
|
var attrs = { strides: strides, pad: pad, dataFormat: dataFormat, dilations: dilations, dimRoundingMode: dimRoundingMode };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(DepthwiseConv2dNative, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var depthwiseConv2d$1 = /* @__PURE__ */ op({ depthwiseConv2d_: depthwiseConv2d_ });
|
|
|
|
/**
|
|
* @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 diagonal tensor with given diagonal values.
|
|
*
|
|
* Given a diagonal, this operation returns a tensor with the diagonal and
|
|
* everything else padded with zeros.
|
|
*
|
|
* Assume the input has dimensions `[D1,..., Dk]`, then the output is a tensor
|
|
* of rank 2k with dimensions `[D1,..., Dk, D1,..., Dk]`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3, 4]);
|
|
*
|
|
* tf.diag(x).print()
|
|
* ```
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4, 5, 6, 7, 8], [4, 2])
|
|
*
|
|
* tf.diag(x).print()
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function diag_(x) {
|
|
var $x = convertToTensor(x, 'x', 'diag');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Diag, inputs);
|
|
}
|
|
var diag = /* @__PURE__ */ op({ diag_: diag_ });
|
|
|
|
/**
|
|
* @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 grayscale dilation over the input `x`.
|
|
*
|
|
* @param x The input tensor, rank 3 or rank 4 of shape
|
|
* `[batch, height, width, depth]`. If rank 3, batch of 1 is assumed.
|
|
* @param filter The filter tensor, rank 3, of shape
|
|
* `[filterHeight, filterWidth, depth]`.
|
|
* @param strides The strides of the sliding window for each dimension of the
|
|
* input tensor: `[strideHeight, strideWidth]`.
|
|
* If `strides` is a single number,
|
|
* then `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 1*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 Specify the data format of the input and output data.
|
|
* Defaults to 'NHWC'. Only 'NHWC' is currently supported. 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
|
|
* for atrous morphological dilation. 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.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function dilation2d_(x, filter, strides, pad, dilations, dataFormat) {
|
|
if (dilations === void 0) { dilations = [1, 1]; }
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
var $x = convertToTensor(x, 'x', 'dilation2d');
|
|
var $filter = convertToTensor(filter, 'filter', 'dilation2d');
|
|
assert($x.rank === 3 || $x.rank === 4, function () { return "Error in dilation2d: input must be rank 3 or 4, but got rank " +
|
|
"".concat($x.rank, "."); });
|
|
assert($filter.rank === 3, function () { return "Error in dilation2d: filter must be rank 3, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
assert(dataFormat === 'NHWC', function () { return "Error in dilation2d: Only NHWC is currently supported, " +
|
|
"but got dataFormat of ".concat(dataFormat); });
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
reshapedTo4D = true;
|
|
}
|
|
assert(x4D.shape[3] === $filter.shape[2], function () { return "Error in dilation2d: input and filter must have the same depth: ".concat(x4D.shape[3], " vs ").concat($filter.shape[2]); });
|
|
var inputs = { x: x4D, filter: $filter };
|
|
var attrs = { strides: strides, pad: pad, dilations: dilations };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Dilation2D, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var dilation2d = /* @__PURE__ */ op({ dilation2d_: dilation2d_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'floorDiv');
|
|
var $b = convertToTensor(b, 'b', 'floorDiv');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(FloorDiv, inputs);
|
|
}
|
|
var floorDiv = /* @__PURE__ */ op({ floorDiv_: floorDiv_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'div');
|
|
var $b = convertToTensor(b, 'b', 'div');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
if ($a.dtype === 'int32' && $b.dtype === 'int32') {
|
|
return floorDiv($a, $b);
|
|
}
|
|
var inputs = { a: $a, b: $b };
|
|
var attrs = {};
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(RealDiv, inputs, attrs);
|
|
}
|
|
var div = /* @__PURE__ */ op({ div_: div_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var result = [];
|
|
for (var i = 0; i < outShape.length; i++) {
|
|
var inDim = inShape[inShape.length - i - 1];
|
|
var outAxis = outShape.length - i - 1;
|
|
var outDim = outShape[outAxis];
|
|
if (inDim == null || (inDim === 1 && outDim > 1)) {
|
|
result.unshift(outAxis);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
function assertAndGetBroadcastShape(shapeA, shapeB) {
|
|
var l = Math.max(shapeA.length, shapeB.length);
|
|
var result = new Array(l);
|
|
for (var i = 0; i < l; i++) {
|
|
var a = shapeA[shapeA.length - i - 1];
|
|
if (a == null) {
|
|
a = 1;
|
|
}
|
|
var 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) {
|
|
var errMsg = "Operands could not be broadcast together with shapes " +
|
|
"".concat(shapeA, " and ").concat(shapeB, ".");
|
|
throw Error(errMsg);
|
|
}
|
|
else {
|
|
result[l - i - 1] = a;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'equal', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'equal', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Equal, inputs);
|
|
}
|
|
var equal = /* @__PURE__ */ op({ equal_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
var $a = convertToTensor(a, 'a', 'where');
|
|
var $b = convertToTensor(b, 'b', 'where');
|
|
var $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.
|
|
var broadcastShape = assertAndGetBroadcastShape(assertAndGetBroadcastShape($condition.shape, $a.shape), $b.shape);
|
|
var $broadcastedCondition = broadcastTo($condition, broadcastShape);
|
|
var $broadcastedA = broadcastTo($a, broadcastShape);
|
|
var $broadcastedB = broadcastTo($b, broadcastShape);
|
|
var inputs = {
|
|
condition: $broadcastedCondition,
|
|
t: $broadcastedA,
|
|
e: $broadcastedB
|
|
};
|
|
return ENGINE.runKernel(Select, inputs);
|
|
}
|
|
var where = /* @__PURE__ */ op({ where_: where_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'zerosLike');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(ZerosLike, inputs);
|
|
}
|
|
var zerosLike = /* @__PURE__ */ op({ zerosLike_: zerosLike_ });
|
|
|
|
/**
|
|
* Divides two `tf.Tensor`s element-wise, A / B. Supports broadcasting. Return 0
|
|
* if denominator is 0.
|
|
*
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 4, 9, 16]);
|
|
* const b = tf.tensor1d([1, 2, 3, 4]);
|
|
* const c = tf.tensor1d([0, 0, 0, 0]);
|
|
*
|
|
* a.divNoNan(b).print(); // or tf.divNoNan(a, b)
|
|
* a.divNoNan(c).print(); // or tf.divNoNan(a, c)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Broadcast div a with b.
|
|
* const a = tf.tensor1d([2, 4, 6, 8]);
|
|
* const b = tf.scalar(2);
|
|
* const c = tf.scalar(0);
|
|
*
|
|
* a.divNoNan(b).print(); // or tf.divNoNan(a, b)
|
|
* a.divNoNan(c).print(); // or tf.divNoNan(a, c)
|
|
* ```
|
|
*
|
|
* @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 divNoNan_(a, b) {
|
|
var _a;
|
|
// TODO: Make this into its own kernel.
|
|
var $a = convertToTensor(a, 'a', 'div');
|
|
var $b = convertToTensor(b, 'b', 'div');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var divResult = div($a, $b);
|
|
var zeros = zerosLike(divResult);
|
|
var bEqualsZero = equal($b, zeros);
|
|
return where(bEqualsZero, zeros, divResult);
|
|
}
|
|
var divNoNan = /* @__PURE__ */ op({ divNoNan_: divNoNan_ });
|
|
|
|
/**
|
|
* @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 and/or vectors, `t1` and `t2`.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2]);
|
|
* const b = tf.tensor2d([[1, 2], [3, 4]]);
|
|
* const c = tf.tensor2d([[1, 2, 3], [4, 5, 6]]);
|
|
*
|
|
* a.dot(b).print(); // or tf.dot(a, b)
|
|
* b.dot(a).print();
|
|
* b.dot(c).print();
|
|
* ```
|
|
* @param t1 The first tensor in the dot operation.
|
|
* @param t2 The second tensor in the dot operation.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Matrices'}
|
|
*/
|
|
function dot_(t1, t2) {
|
|
var $t1 = convertToTensor(t1, 't1', 'dot');
|
|
var $t2 = convertToTensor(t2, 't2', 'dot');
|
|
assert(($t1.rank === 1 || $t1.rank === 2) && ($t2.rank === 1 || $t2.rank === 2), function () { return "Error in dot: inputs must all be rank 1 or 2, but got ranks " +
|
|
"".concat($t1.rank, " and ").concat($t2.rank, "."); });
|
|
var t1Inner = ($t1.rank === 1 ? $t1.size : $t1.shape[1]);
|
|
var t2Inner = ($t2.rank === 1 ? $t2.size : $t2.shape[0]);
|
|
assert(t1Inner === t2Inner, function () { return "Error in dot: inner dimensions of inputs must match, but got " +
|
|
"".concat(t1Inner, " and ").concat(t2Inner, "."); });
|
|
if ($t1.rank === 1 && $t2.rank === 1) {
|
|
var t12D = reshape($t1, [1, -1]);
|
|
var t22D = reshape($t2, [-1, 1]);
|
|
var t1t2 = matMul$1(t12D, t22D);
|
|
return reshape(t1t2, []);
|
|
}
|
|
else if ($t1.rank === 1 && $t2.rank === 2) {
|
|
var t12D = reshape($t1, [1, -1]);
|
|
var t22D = reshape($t2, [$t2.shape[0], $t2.shape[1]]);
|
|
var t1t2 = matMul$1(t12D, t22D);
|
|
return reshape(t1t2, [t1t2.size]);
|
|
}
|
|
else if ($t1.rank === 2 && $t2.rank === 1) {
|
|
var t22D = reshape($t2, [-1, 1]);
|
|
var t1t2 = matMul$1($t1, t22D);
|
|
return reshape(t1t2, [t1t2.size]);
|
|
}
|
|
else {
|
|
var t22D = reshape($t2, [$t2.shape[0], $t2.shape[1]]);
|
|
var t1t2 = matMul$1($t1, t22D);
|
|
return t1t2;
|
|
}
|
|
}
|
|
var dot = /* @__PURE__ */ op({ dot_: dot_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Tensor contraction over specified indices and outer product.
|
|
*
|
|
* `einsum` allows defining Tensors by defining their element-wise computation.
|
|
* This computation is based on
|
|
* [Einstein summation](https://en.wikipedia.org/wiki/Einstein_notation).
|
|
*
|
|
* Some special cases include:
|
|
*
|
|
* Matrix multiplication:
|
|
* ```js
|
|
* const x = tf.tensor2d([[1, 2, 3], [4, 5, 6]]);
|
|
* const y = tf.tensor2d([[0, 1], [2, 3], [4, 5]]);
|
|
* x.print();
|
|
* y.print();
|
|
* tf.einsum('ij,jk->ik', x, y).print();
|
|
* ```
|
|
*
|
|
* Dot product:
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
* const y = tf.tensor1d([0, 1, 2]);
|
|
* x.print();
|
|
* y.print();
|
|
* tf.einsum('i,i->', x, y).print();
|
|
* ```
|
|
*
|
|
* Batch dot product:
|
|
* ```js
|
|
* const x = tf.tensor2d([[1, 2, 3], [4, 5, 6]]);
|
|
* const y = tf.tensor2d([[0, 1, 2], [3, 4, 5]]);
|
|
* x.print();
|
|
* y.print();
|
|
* tf.einsum('bi,bi->b', x, y).print();
|
|
* ```
|
|
*
|
|
* Outer prouduct:
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 3, 5]);
|
|
* const y = tf.tensor1d([2, 4, 6]);
|
|
* x.print();
|
|
* y.print();
|
|
* tf.einsum('i,j->ij', x, y).print();
|
|
* ```
|
|
*
|
|
* Matrix transpose:
|
|
* ```js
|
|
* const x = tf.tensor2d([[1, 2], [3, 4]]);
|
|
* x.print();
|
|
* tf.einsum('ij->ji', x).print();
|
|
* ```
|
|
*
|
|
* Batch matrix transpose:
|
|
* ```js
|
|
* const x = tf.tensor3d([[[1, 2], [3, 4]], [[-1, -2], [-3, -4]]]);
|
|
* x.print();
|
|
* tf.einsum('bij->bji', x).print();
|
|
* ```
|
|
*
|
|
* Limitations:
|
|
*
|
|
* This implementation of einsum has the following limitations:
|
|
*
|
|
* - Does not support >2 input tensors.
|
|
* - Does not support duplicate axes for any given input tensor. E.g., equation
|
|
* 'ii->' is not supported.
|
|
* - The `...` notation is not supported.
|
|
*
|
|
* @param equation a string describing the contraction, in the same format as
|
|
* [numpy.einsum](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html).
|
|
* @param tensors the input(s) to contract (each one a Tensor), whose shapes
|
|
* should be consistent with equation.
|
|
* @returns The output tensor.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Matrices'}
|
|
*/
|
|
function einsum_(equation) {
|
|
var tensors = [];
|
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
tensors[_i - 1] = arguments[_i];
|
|
}
|
|
var $tensors = tensors.map(function (t, i) { return convertToTensor(t, "tensors".concat(i), 'einsum'); });
|
|
var attrs = { equation: equation };
|
|
return ENGINE.runKernel(Einsum, $tensors, attrs);
|
|
}
|
|
var einsum = /* @__PURE__ */ op({ einsum_: einsum_ });
|
|
|
|
/**
|
|
* @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 exponential linear element-wise: `x > 0 ? x : (e ^ x) - 1`.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 1, -3, 2]);
|
|
*
|
|
* x.elu().print(); // or tf.elu(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function elu_(x) {
|
|
var $x = convertToTensor(x, 'x', 'elu', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Elu, inputs);
|
|
}
|
|
var elu = /* @__PURE__ */ op({ elu_: elu_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Checks the input tensor mathes the given shape.
|
|
*
|
|
* Given an input tensor, returns a new tensor with the same values as the
|
|
* input tensor with shape `shape`.
|
|
*
|
|
* The method supports the null value in tensor. It will still check the shapes,
|
|
* and null is a placeholder.
|
|
*
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3, 4]);
|
|
* const y = tf.tensor1d([1, null, 3, 4]);
|
|
* const z = tf.tensor2d([1, 2, 3, 4], [2,2]);
|
|
* tf.ensureShape(x, [4]).print();
|
|
* tf.ensureShape(y, [4]).print();
|
|
* tf.ensureShape(z, [null, 2]).print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor to be ensured.
|
|
* @param shape A TensorShape representing the shape of this tensor, an array
|
|
* or null.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Transformations'}
|
|
*/
|
|
function ensureShape_(x, shape) {
|
|
var $x = convertToTensor(x, 'x', 'ensureShape', 'string_or_numeric');
|
|
if (!arraysEqualWithNull($x.shape, shape)) {
|
|
throw new Error("EnsureShape: Shape of tensor ".concat($x.shape, " is not compatible with expected shape ").concat(shape));
|
|
}
|
|
return x;
|
|
}
|
|
var ensureShape = /* @__PURE__ */ op({ ensureShape_: ensureShape_ });
|
|
|
|
/**
|
|
* @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 Gauss error function of the input `tf.Tensor` element-wise:
|
|
* `erf(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, .1, -.1, .7]);
|
|
*
|
|
* x.erf().print(); // or tf.erf(x);
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function erf_(x) {
|
|
var $x = convertToTensor(x, 'x', 'erf');
|
|
assert($x.dtype === 'int32' || $x.dtype === 'float32', function () { return 'Input dtype must be `int32` or `float32`.'; });
|
|
if ($x.dtype === 'int32') {
|
|
$x = cast($x, 'float32');
|
|
}
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Erf, inputs);
|
|
}
|
|
var erf = /* @__PURE__ */ op({ erf_: erf_ });
|
|
|
|
/**
|
|
* @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 combineLocations(outputLoc, reduceLoc, axes) {
|
|
var rank = outputLoc.length + reduceLoc.length;
|
|
var loc = [];
|
|
var outIdx = 0;
|
|
var reduceIdx = 0;
|
|
for (var dim = 0; dim < rank; dim++) {
|
|
if (axes.indexOf(dim) === -1) {
|
|
loc.push(outputLoc[outIdx++]);
|
|
}
|
|
else {
|
|
loc.push(reduceLoc[reduceIdx++]);
|
|
}
|
|
}
|
|
return loc;
|
|
}
|
|
function expandShapeToKeepDim(shape, axes) {
|
|
var reduceSubShape = axes.map(function (x) { return 1; });
|
|
return combineLocations(shape, reduceSubShape, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes the maximum of elements across dimensions of a `tf.Tensor`.
|
|
*
|
|
* Reduces the input along the dimensions given in `axes`. Unless `keepDims`
|
|
* is true, the rank of the `tf.Tensor` is reduced by 1 for each entry in
|
|
* `axes`. If `keepDims` is true, the reduced dimensions are retained with
|
|
* length 1. If `axes` has no entries, all dimensions are reduced, and a
|
|
* `tf.Tensor` with a single element is returned.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.max().print(); // or tf.max(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.max(axis).print(); // or tf.max(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor.
|
|
* @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 max_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'max');
|
|
var inputs = { x: $x };
|
|
var attrs = { reductionIndices: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(Max, inputs, attrs);
|
|
}
|
|
var max = /* @__PURE__ */ op({ max_: max_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes the minimum value from the input.
|
|
*
|
|
* Reduces the input along the dimensions given in `axes`. Unless `keepDims`
|
|
* is true, the rank of the array is reduced by 1 for each entry in `axes`.
|
|
* If `keepDims` is true, the reduced dimensions are retained with length 1.
|
|
* If `axes` has no entries, all dimensions are reduced, and an array with a
|
|
* single element is returned.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.min().print(); // or tf.min(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.min(axis).print(); // or tf.min(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input Tensor.
|
|
* @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 min_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'min');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(Min, inputs, attrs);
|
|
}
|
|
var min = /* @__PURE__ */ op({ min_: min_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $base = convertToTensor(base, 'base', 'pow');
|
|
var $exp = convertToTensor(exp, 'exp', 'pow');
|
|
_a = __read(makeTypesMatch($base, $exp), 2), $base = _a[0], $exp = _a[1];
|
|
var inputs = { a: $base, b: $exp };
|
|
return ENGINE.runKernel(Pow, inputs);
|
|
}
|
|
var pow = /* @__PURE__ */ op({ pow_: pow_ });
|
|
|
|
/**
|
|
* @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 ".concat(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);
|
|
var providedSize_1 = sizeFromShape(shape);
|
|
var inferredSize_1 = sizeFromShape(inferredShape);
|
|
assert(providedSize_1 === inferredSize_1, function () { return "Based on the provided shape, [".concat(shape, "], the tensor should have ") +
|
|
"".concat(providedSize_1, " values but has ").concat(inferredSize_1); });
|
|
for (var i = 0; i < inferredShape.length; ++i) {
|
|
var inferred = inferredShape[i];
|
|
var flatDimsDontMatch = i === inferredShape.length - 1 ?
|
|
inferred !== sizeFromShape(shape.slice(i)) :
|
|
true;
|
|
assert(inferredShape[i] === shape[i] || !flatDimsDontMatch, function () { return "Error creating a new Tensor. Inferred shape " +
|
|
"(".concat(inferredShape, ") does not match the provided ") +
|
|
"shape (".concat(shape, "). "); });
|
|
}
|
|
}
|
|
if (!isTypedArray(values) && !Array.isArray(values)) {
|
|
values = [values];
|
|
}
|
|
shape = shape || inferredShape;
|
|
values = dtype !== 'string' ?
|
|
toTypedArray(values, dtype) :
|
|
flatten(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`.');
|
|
}
|
|
var shape = [];
|
|
var 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) {
|
|
var $x = convertToTensor(x, 'x', 'sqrt', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Sqrt, inputs);
|
|
}
|
|
var sqrt = /* @__PURE__ */ op({ sqrt_: 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) {
|
|
var $x = convertToTensor(x, 'x', 'square');
|
|
var attrs = {};
|
|
return ENGINE.runKernel('Square', { x: $x }, attrs);
|
|
}
|
|
var square = /* @__PURE__ */ op({ square_: square_ });
|
|
|
|
/**
|
|
* @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, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'sum');
|
|
if ($x.dtype === 'bool') {
|
|
$x = cast($x, 'int32');
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(Sum, inputs, attrs);
|
|
}
|
|
var sum = /* @__PURE__ */ op({ sum_: sum_ });
|
|
|
|
/**
|
|
* @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 norm of scalar, vectors, and matrices.
|
|
* This function can compute several different vector norms (the 1-norm, the
|
|
* Euclidean or 2-norm, the inf-norm, and in general the p-norm for p > 0)
|
|
* and matrix norms (Frobenius, 1-norm, and inf-norm).
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3, 4]);
|
|
*
|
|
* x.norm().print(); // or tf.norm(x)
|
|
* ```
|
|
*
|
|
* @param x The input array.
|
|
* @param ord Optional. Order of the norm. Supported norm types are
|
|
* following:
|
|
*
|
|
* | ord | norm for matrices | norm for vectors
|
|
* |------------|---------------------------|---------------------
|
|
* |'euclidean' |Frobenius norm |2-norm
|
|
* |'fro' |Frobenius norm |
|
|
* |Infinity |max(sum(abs(x), axis=1)) |max(abs(x))
|
|
* |-Infinity |min(sum(abs(x), axis=1)) |min(abs(x))
|
|
* |1 |max(sum(abs(x), axis=0)) |sum(abs(x))
|
|
* |2 | |sum(abs(x)^2)^(1/2)
|
|
*
|
|
* @param axis Optional. If axis is null (the default), the input is
|
|
* considered a vector and a single vector norm is computed over the entire
|
|
* set of values in the Tensor, i.e. norm(x, ord) is equivalent
|
|
* to norm(x.reshape([-1]), ord). If axis is an integer, the input
|
|
* is considered a batch of vectors, and axis determines the axis in x
|
|
* over which to compute vector norms. If axis is a 2-tuple of integer it is
|
|
* considered a batch of matrices and axis determines the axes in NDArray
|
|
* over which to compute a matrix norm.
|
|
* @param keepDims Optional. If true, the norm has the same dimensionality
|
|
* as the input.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Matrices'}
|
|
*/
|
|
function norm_(x, ord, axis, keepDims) {
|
|
if (ord === void 0) { ord = 'euclidean'; }
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
x = convertToTensor(x, 'x', 'norm');
|
|
var norm = normImpl(x, ord, axis);
|
|
var keepDimsShape = norm.shape;
|
|
if (keepDims) {
|
|
var axes = parseAxisParam(axis, x.shape);
|
|
keepDimsShape = expandShapeToKeepDim(norm.shape, axes);
|
|
}
|
|
return reshape(norm, keepDimsShape);
|
|
}
|
|
function normImpl(x, p, axis) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (x.rank === 0) {
|
|
return abs(x);
|
|
}
|
|
// consider vector when no axis is specified
|
|
if (x.rank !== 1 && axis === null) {
|
|
return normImpl(reshape(x, [-1]), p, axis);
|
|
}
|
|
// vector
|
|
if (x.rank === 1 || typeof axis === 'number' ||
|
|
Array.isArray(axis) && axis.length === 1) {
|
|
if (p === 1) {
|
|
return sum(abs(x), axis);
|
|
}
|
|
if (p === Infinity) {
|
|
return max(abs(x), axis);
|
|
}
|
|
if (p === -Infinity) {
|
|
return min(abs(x), axis);
|
|
}
|
|
if (p === 'euclidean' || p === 2) {
|
|
// norm(x, 2) = sum(abs(xi) ^ 2) ^ 1/2
|
|
return sqrt(sum(pow(abs(x), scalar(2, 'int32')), axis));
|
|
}
|
|
throw new Error("Error in norm: invalid ord value: ".concat(p));
|
|
}
|
|
// matrix (assumption axis[0] < axis[1])
|
|
if (Array.isArray(axis) && axis.length === 2) {
|
|
if (p === 1) {
|
|
return max(sum(abs(x), axis[0]), axis[1] - 1);
|
|
}
|
|
if (p === Infinity) {
|
|
return max(sum(abs(x), axis[1]), axis[0]);
|
|
}
|
|
if (p === -Infinity) {
|
|
return min(sum(abs(x), axis[1]), axis[0]);
|
|
}
|
|
if (p === 'fro' || p === 'euclidean') {
|
|
// norm(x) = sqrt(sum(pow(x, 2)))
|
|
return sqrt(sum(square(x), axis));
|
|
}
|
|
throw new Error("Error in norm: invalid ord value: ".concat(p));
|
|
}
|
|
throw new Error("Error in norm: invalid axis: ".concat(axis));
|
|
}
|
|
var norm = /* @__PURE__ */ op({ norm_: norm_ });
|
|
|
|
/**
|
|
* @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 Euclidean norm of scalar, vectors, and matrices.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3, 4]);
|
|
*
|
|
* x.euclideanNorm().print(); // or tf.euclideanNorm(x)
|
|
* ```
|
|
*
|
|
* @param x The input array.
|
|
* @param axis Optional. If axis is null (the default), the input is
|
|
* considered a vector and a single vector norm is computed over the entire
|
|
* set of values in the Tensor, i.e. euclideanNorm(x) is equivalent
|
|
* to euclideanNorm(x.reshape([-1])). If axis is an integer, the input
|
|
* is considered a batch of vectors, and axis determines the axis in x
|
|
* over which to compute vector norms. If axis is a 2-tuple of integer it is
|
|
* considered a batch of matrices and axis determines the axes in NDArray
|
|
* over which to compute a matrix norm.
|
|
* @param keepDims Optional. If true, the norm has the same dimensionality
|
|
* as the input.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Matrices'}
|
|
*/
|
|
function euclideanNorm_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
return norm(x, 'euclidean', axis, keepDims);
|
|
}
|
|
var euclideanNorm = /* @__PURE__ */ op({ euclideanNorm_: euclideanNorm_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'exp');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Exp, inputs);
|
|
}
|
|
var exp = /* @__PURE__ */ op({ exp_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'expandDims', 'string_or_numeric');
|
|
assert(axis <= $x.rank, function () { return 'Axis must be <= rank of the tensor'; });
|
|
var inputs = { input: $x };
|
|
var attrs = { dim: axis };
|
|
return ENGINE.runKernel(ExpandDims, inputs, attrs);
|
|
}
|
|
var expandDims = /* @__PURE__ */ op({ expandDims_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes exponential of the input `tf.Tensor` minus one element-wise.
|
|
* `e ^ x - 1`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, -3]);
|
|
*
|
|
* x.expm1().print(); // or tf.expm1(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function expm1_(x) {
|
|
var $x = convertToTensor(x, 'x', 'expm1');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Expm1, inputs);
|
|
}
|
|
var expm1 = /* @__PURE__ */ op({ expm1_: expm1_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'tile', 'string_or_numeric');
|
|
assert($x.rank === reps.length, function () { return "Error in transpose: rank of input ".concat($x.rank, " ") +
|
|
"must match length of reps ".concat(reps, "."); });
|
|
var inputs = { x: $x };
|
|
var attrs = { reps: reps };
|
|
return ENGINE.runKernel(Tile, inputs, attrs);
|
|
}
|
|
var tile = /* @__PURE__ */ op({ tile_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Create an identity matrix.
|
|
*
|
|
* @param numRows Number of rows.
|
|
* @param numColumns Number of columns. Defaults to `numRows`.
|
|
* @param batchShape If provided, will add the batch shape to the beginning
|
|
* of the shape of the returned `tf.Tensor` by repeating the identity
|
|
* matrix.
|
|
* @param dtype Data type.
|
|
* @returns Identity matrix of the specified size and data type, possibly
|
|
* with batch repetition if `batchShape` is specified.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function eye_(numRows, numColumns, batchShape, dtype) {
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
if (numColumns == null) {
|
|
numColumns = numRows;
|
|
}
|
|
var buff = buffer([numRows, numColumns], dtype);
|
|
var n = numRows <= numColumns ? numRows : numColumns;
|
|
for (var i = 0; i < n; ++i) {
|
|
buff.set(1, i, i);
|
|
}
|
|
var out = reshape(buff.toTensor(), [numRows, numColumns]);
|
|
if (batchShape == null) {
|
|
return out;
|
|
}
|
|
else {
|
|
if (batchShape.length === 1) {
|
|
return tile(expandDims(out, 0), [batchShape[0], 1, 1]);
|
|
}
|
|
else if (batchShape.length === 2) {
|
|
// tslint:disable-next-line:no-unnecessary-type-assertion
|
|
return tile(expandDims(expandDims(out, 0), 0), [batchShape[0], batchShape[1], 1, 1]);
|
|
}
|
|
else if (batchShape.length === 3) {
|
|
// tslint:disable-next-line:no-unnecessary-type-assertion
|
|
return tile(expandDims(expandDims(expandDims(out, 0), 0), 0), [
|
|
batchShape[0], batchShape[1], batchShape[2], 1, 1
|
|
]);
|
|
}
|
|
else {
|
|
throw new Error("eye() currently supports only 1D and 2D " +
|
|
// tslint:disable-next-line:no-any
|
|
"batchShapes, but received ".concat(batchShape.length, "D."));
|
|
}
|
|
}
|
|
}
|
|
var eye = /* @__PURE__ */ op({ eye_: eye_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'floor', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Floor, inputs);
|
|
}
|
|
var floor = /* @__PURE__ */ op({ floor_: floor_ });
|
|
|
|
/**
|
|
* @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, batchDims) {
|
|
if (axis === void 0) { axis = 0; }
|
|
if (batchDims === void 0) { batchDims = 0; }
|
|
var $x = convertToTensor(x, 'x', 'gather');
|
|
var $indices = convertToTensor(indices, 'indices', 'gather', 'int32');
|
|
var inputs = { x: $x, indices: $indices };
|
|
var attrs = { axis: axis, batchDims: batchDims };
|
|
return ENGINE.runKernel(GatherV2, inputs, attrs);
|
|
}
|
|
var gather = /* @__PURE__ */ op({ gather_: gather_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'greater', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'greater', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Greater, inputs);
|
|
}
|
|
var greater = /* @__PURE__ */ op({ greater_: greater_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'greaterEqual', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'greaterEqual', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(GreaterEqual, inputs);
|
|
}
|
|
var greaterEqual = /* @__PURE__ */ op({ greaterEqual_: 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 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) {
|
|
var $input = convertToTensor(input, 'input', 'imag');
|
|
var inputs = { input: $input };
|
|
return ENGINE.runKernel(Imag, inputs);
|
|
}
|
|
var imag = /* @__PURE__ */ op({ imag_: imag_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns which elements of x are finite.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([NaN, Infinity, -Infinity, 0, 1]);
|
|
*
|
|
* x.isFinite().print(); // or tf.isNaN(x)
|
|
* ```
|
|
* @param x The input Tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function isFinite_(x) {
|
|
var $x = convertToTensor(x, 'x', 'isFinite');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(IsFinite, inputs);
|
|
}
|
|
var isFinite$1 = /* @__PURE__ */ op({ isFinite_: isFinite_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns which elements of x are Infinity or -Infinity.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([NaN, Infinity, -Infinity, 0, 1]);
|
|
*
|
|
* x.isInf().print(); // or tf.isNaN(x)
|
|
* ```
|
|
* @param x The input Tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function isInf_(x) {
|
|
var $x = convertToTensor(x, 'x', 'isInf');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(IsInf, inputs);
|
|
}
|
|
var isInf = /* @__PURE__ */ op({ isInf_: isInf_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns which elements of x are NaN.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([NaN, Infinity, -Infinity, 0, 1]);
|
|
*
|
|
* x.isNaN().print(); // or tf.isNaN(x)
|
|
* ```
|
|
* @param x The input Tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function isNaN_(x) {
|
|
var $x = convertToTensor(x, 'x', 'isNaN');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(IsNan, inputs);
|
|
}
|
|
var isNaN$1 = /* @__PURE__ */ op({ isNaN_: isNaN_ });
|
|
|
|
/**
|
|
* @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 leaky rectified linear element-wise.
|
|
*
|
|
* See
|
|
* [http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf](
|
|
* http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf)
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
*
|
|
* x.leakyRelu(0.1).print(); // or tf.leakyRelu(x, 0.1)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @param alpha The scaling factor for negative values, defaults to 0.2.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function leakyRelu_(x, alpha) {
|
|
if (alpha === void 0) { alpha = 0.2; }
|
|
var $x = convertToTensor(x, 'x', 'leakyRelu');
|
|
var inputs = { x: $x };
|
|
var attrs = { alpha: alpha };
|
|
return ENGINE.runKernel(LeakyRelu, inputs, attrs);
|
|
}
|
|
var leakyRelu = /* @__PURE__ */ op({ leakyRelu_: leakyRelu_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'less', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'less', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Less, inputs);
|
|
}
|
|
var less = /* @__PURE__ */ op({ less_: less_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'lessEqual', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'lessEqual', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(LessEqual, inputs);
|
|
}
|
|
var lessEqual = /* @__PURE__ */ op({ lessEqual_: lessEqual_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Return an evenly spaced sequence of numbers over the given interval.
|
|
*
|
|
* ```js
|
|
* tf.linspace(0, 9, 10).print();
|
|
* ```
|
|
* @param start The start value of the sequence.
|
|
* @param stop The end value of the sequence.
|
|
* @param num The number of values to generate.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function linspace(start, stop, num) {
|
|
if (num <= 0) {
|
|
throw new Error('The number of values should be positive.');
|
|
}
|
|
var attrs = { start: start, stop: stop, num: num };
|
|
return ENGINE.runKernel(LinSpace, {}, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Normalizes the activation of a local neighborhood across or within
|
|
* channels.
|
|
*
|
|
* @param x The input tensor. The 4-D input tensor is treated as a 3-D array
|
|
* of 1D vectors (along the last dimension), and each vector is
|
|
* normalized independently.
|
|
* @param depthRadius The number of adjacent channels in the 1D normalization
|
|
* window.
|
|
* @param bias A constant bias term for the basis.
|
|
* @param alpha A scale factor, usually positive.
|
|
* @param beta An exponent.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function localResponseNormalization_(x, depthRadius, bias, alpha, beta) {
|
|
if (depthRadius === void 0) { depthRadius = 5; }
|
|
if (bias === void 0) { bias = 1; }
|
|
if (alpha === void 0) { alpha = 1; }
|
|
if (beta === void 0) { beta = 0.5; }
|
|
var $x = convertToTensor(x, 'x', 'localResponseNormalization');
|
|
assert($x.rank === 4 || $x.rank === 3, function () { return "Error in localResponseNormalization: x must be rank 3 or 4 but got\n rank ".concat($x.rank, "."); });
|
|
assert(isInt(depthRadius), function () { return "Error in localResponseNormalization: depthRadius must be an " +
|
|
"integer but got depthRadius ".concat(depthRadius, "."); });
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
var inputs = { x: x4D };
|
|
var attrs = { depthRadius: depthRadius, bias: bias, alpha: alpha, beta: beta };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(LRN, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
else {
|
|
return res;
|
|
}
|
|
}
|
|
var localResponseNormalization = /* @__PURE__ */ op({ localResponseNormalization_: localResponseNormalization_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'log', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Log, inputs);
|
|
}
|
|
var log = /* @__PURE__ */ op({ log_: log_ });
|
|
|
|
/**
|
|
* @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` plus one
|
|
* element-wise: `ln(1 + x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, Math.E - 1]);
|
|
*
|
|
* x.log1p().print(); // or tf.log1p(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function log1p_(x) {
|
|
var $x = convertToTensor(x, 'x', 'log1p');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Log1p, inputs);
|
|
}
|
|
var log1p = /* @__PURE__ */ op({ log1p_: log1p_ });
|
|
|
|
/**
|
|
* Overrides the gradient computation of a function `f`.
|
|
*
|
|
* Takes a function
|
|
* `f(...inputs, save) => {value: Tensor, gradFunc: (dy, saved) => Tensor[]}`
|
|
* and returns another function `g(...inputs)` which takes the same inputs as
|
|
* `f`. When called, `g` returns `f().value`. In backward mode, custom gradients
|
|
* with respect to each input of `f` are computed using `f().gradFunc`.
|
|
*
|
|
* The `save` function passed to `f` should be used for saving tensors needed
|
|
* in the gradient. And the `saved` passed to the `gradFunc` is a
|
|
* `NamedTensorMap`, which contains those saved tensors.
|
|
*
|
|
* ```js
|
|
* const customOp = tf.customGrad((x, save) => {
|
|
* // Save x to make sure it's available later for the gradient.
|
|
* save([x]);
|
|
* // Override gradient of our custom x ^ 2 op to be dy * abs(x);
|
|
* return {
|
|
* value: x.square(),
|
|
* // Note `saved.x` which points to the `x` we saved earlier.
|
|
* gradFunc: (dy, saved) => [dy.mul(saved[0].abs())]
|
|
* };
|
|
* });
|
|
*
|
|
* const x = tf.tensor1d([-1, -2, 3]);
|
|
* const dx = tf.grad(x => customOp(x));
|
|
*
|
|
* console.log(`f(x):`);
|
|
* customOp(x).print();
|
|
* console.log(`f'(x):`);
|
|
* dx(x).print();
|
|
* ```
|
|
*
|
|
* @param f The function to evaluate in forward mode, which should return
|
|
* `{value: Tensor, gradFunc: (dy, saved) => Tensor[]}`, where `gradFunc`
|
|
* returns the custom gradients of `f` with respect to its inputs.
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Gradients'}
|
|
*/
|
|
function customGrad(f) {
|
|
return ENGINE.customGrad(f);
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'neg');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Neg, inputs);
|
|
}
|
|
var neg = /* @__PURE__ */ op({ neg_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes softplus of the input `tf.Tensor` element-wise: `log(exp(x) + 1)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.softplus().print(); // or tf.softplus(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function softplus_(x) {
|
|
var $x = convertToTensor(x, 'x', 'softplus');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Softplus, inputs);
|
|
}
|
|
var softplus = /* @__PURE__ */ op({ softplus_: softplus_ });
|
|
|
|
/**
|
|
* @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 log sigmoid of the input `tf.Tensor` element-wise:
|
|
* `logSigmoid(x)`. For numerical stability, we use `-tf.softplus(-x)`.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, -1, .7]);
|
|
*
|
|
* x.logSigmoid().print(); // or tf.logSigmoid(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function logSigmoid_(x) {
|
|
var $x = convertToTensor(x, 'x', 'logSigmoid');
|
|
// Use a custom gradient to maintain previous implementation.
|
|
// There is no LogSigmoid kernel in TF so we can't use engine.runKernel
|
|
// directly
|
|
var customOp = customGrad(function (x) {
|
|
// TODO(yassogba) we can remove the chained softplus call here only
|
|
// after backends have modualrized softplus at which point we can call
|
|
// engine runKernel(..., Sotfplus, ...) directly.
|
|
var value = neg(softplus(neg(x)));
|
|
var gradFunc = function (dy) {
|
|
var derX = mul(dy, sigmoid(neg(x)));
|
|
return derX;
|
|
};
|
|
return { value: value, gradFunc: gradFunc };
|
|
});
|
|
return customOp($x);
|
|
}
|
|
var logSigmoid = /* @__PURE__ */ op({ logSigmoid_: logSigmoid_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'sub');
|
|
var $b = convertToTensor(b, 'b', 'sub');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Sub, inputs);
|
|
}
|
|
var sub = /* @__PURE__ */ op({ sub_: sub_ });
|
|
|
|
/**
|
|
* Computes the log softmax.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* a.logSoftmax().print(); // or tf.logSoftmax(a)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor2d([2, 4, 6, 1, 2, 3], [2, 3]);
|
|
*
|
|
* a.logSoftmax().print(); // or tf.logSoftmax(a)
|
|
* ```
|
|
*
|
|
* @param logits The logits array.
|
|
* @param axis The dimension softmax would be performed on. Defaults to `-1`
|
|
* which indicates the last dimension.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function logSoftmax_(logits, axis) {
|
|
if (axis === void 0) { axis = -1; }
|
|
var $logits = convertToTensor(logits, 'logits', 'logSoftmax');
|
|
if (axis === -1) {
|
|
axis = $logits.rank - 1;
|
|
}
|
|
if (axis !== $logits.rank - 1) {
|
|
throw Error('Log Softmax along a non-last dimension is not yet supported. ' +
|
|
"Logits was rank ".concat($logits.rank, " and axis was ").concat(axis));
|
|
}
|
|
// const forward: ForwardFunc<Tensor> = (backend, save) => {
|
|
// const keepDims = true;
|
|
// const xMax = max(logits, axis, true);
|
|
// const shifted = sub(logits, xMax);
|
|
// const value =
|
|
// sub(cast(shifted, 'float32'), log(sum(exp(shifted), axis,
|
|
// keepDims)));
|
|
// save([value]);
|
|
// return value;
|
|
// };
|
|
// Use a custom gradient for numerical stability.
|
|
var customOp = customGrad(function (logits, save) {
|
|
var keepDims = true;
|
|
var xMax = max(logits, axis, true);
|
|
var shifted = sub(logits, xMax);
|
|
var value = sub(cast(shifted, 'float32'), log(sum(exp(shifted), axis, keepDims)));
|
|
save([value]);
|
|
var gradFunc = function (dy, saved) {
|
|
var _a = __read(saved, 1), value = _a[0];
|
|
var keepDims = true;
|
|
var softmax = exp(value);
|
|
return sub(dy, mul(sum(dy, axis, keepDims), softmax));
|
|
};
|
|
return { value: value, gradFunc: gradFunc };
|
|
});
|
|
return customOp($logits);
|
|
// TODO Use Engine.runKernel when CPU/WebGL/WASM backends implement this.
|
|
// const inputs: LogSoftmaxInputs = {logits: $logits};
|
|
// const attrs: LogSoftmaxAttrs = {axis};
|
|
// return ENGINE.runKernel(
|
|
// LogSoftmax, inputs as unknown as NamedTensorMap,
|
|
// attrs as unknown as NamedAttrMap);
|
|
}
|
|
var logSoftmax = /* @__PURE__ */ op({ logSoftmax_: logSoftmax_ });
|
|
|
|
/**
|
|
* @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 log(sum(exp(elements across the reduction dimensions))).
|
|
*
|
|
* Reduces the input along the dimensions given in `axis`. Unless `keepDims`
|
|
* is true, the rank of the array is reduced by 1 for each entry in `axis`.
|
|
* If `keepDims` is true, the reduced dimensions are retained with length 1.
|
|
* If `axis` has no entries, all dimensions are reduced, and an array with a
|
|
* single element is returned.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.logSumExp().print(); // or tf.logSumExp(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.logSumExp(axis).print(); // or tf.logSumExp(a, axis)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @param axis The dimension(s) to reduce. If null (the default),
|
|
* reduces all dimensions.
|
|
* @param keepDims If true, retains reduced dimensions with length
|
|
* of 1. Defaults to false.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Reduction'}
|
|
*/
|
|
function logSumExp_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'logSumExp');
|
|
var axes = parseAxisParam(axis, $x.shape);
|
|
var xMax = max($x, axes, true /* keepDims */);
|
|
var a = sub($x, xMax);
|
|
var b = exp(a);
|
|
var c = sum(b, axes);
|
|
var d = log(c);
|
|
var res = add(reshape(xMax, d.shape), d);
|
|
if (keepDims) {
|
|
var newShape = expandShapeToKeepDim(res.shape, axes);
|
|
return reshape(res, newShape);
|
|
}
|
|
return res;
|
|
}
|
|
var logSumExp = /* @__PURE__ */ op({ logSumExp_: logSumExp_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $a = convertToTensor(a, 'a', 'logicalAnd', 'bool');
|
|
var $b = convertToTensor(b, 'b', 'logicalAnd', 'bool');
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(LogicalAnd, inputs);
|
|
}
|
|
var logicalAnd = /* @__PURE__ */ op({ logicalAnd_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
var $x = convertToTensor(x, 'x', 'logicalNot', 'bool');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(LogicalNot, inputs);
|
|
}
|
|
var logicalNot = /* @__PURE__ */ op({ logicalNot_: 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 truth value of `a OR 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.logicalOr(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 logicalOr_(a, b) {
|
|
var $a = convertToTensor(a, 'a', 'logicalOr', 'bool');
|
|
var $b = convertToTensor(b, 'b', 'logicalOr', 'bool');
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(LogicalOr, inputs);
|
|
}
|
|
var logicalOr = /* @__PURE__ */ op({ logicalOr_: logicalOr_ });
|
|
|
|
/**
|
|
* @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 XOR 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.logicalXor(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 logicalXor_(a, b) {
|
|
var $a = convertToTensor(a, 'a', 'logicalXor', 'bool');
|
|
var $b = convertToTensor(b, 'b', 'logicalXor', 'bool');
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
// x ^ y = (x | y) & ~(x & y)
|
|
return logicalAnd(logicalOr(a, b), logicalNot(logicalAnd(a, b)));
|
|
}
|
|
var logicalXor = /* @__PURE__ */ op({ logicalXor_: logicalXor_ });
|
|
|
|
/**
|
|
* @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 INT32_MAX = 2147483648;
|
|
/**
|
|
* Searches for where a value would go in a sorted sequence.
|
|
*
|
|
* This is not a method for checking containment (like javascript in).
|
|
*
|
|
* The typical use case for this operation is "binning", "bucketing", or
|
|
* "discretizing". The values are assigned to bucket-indices based on the edges
|
|
* listed in 'sortedSequence'. This operation returns the bucket-index for each
|
|
* value.
|
|
*
|
|
* The side argument controls which index is returned if a value lands exactly
|
|
* on an edge.
|
|
*
|
|
* The axis is not settable for this operation. It always operates on the
|
|
* innermost dimension (axis=-1). The operation will accept any number of outer
|
|
* dimensions.
|
|
*
|
|
* Note: This operation assumes that 'sortedSequence' is sorted along the
|
|
* innermost axis, maybe using 'sort(..., axis=-1)'. If the sequence is not
|
|
* sorted no error is raised and the content of the returned tensor is not well
|
|
* defined.
|
|
*
|
|
* ```js
|
|
* const edges = tf.tensor1d([-1, 3.3, 9.1, 10.0]);
|
|
* let values = tf.tensor1d([0.0, 4.1, 12.0]);
|
|
* const result1 = tf.searchSorted(edges, values, 'left');
|
|
* result1.print(); // [1, 2, 4]
|
|
*
|
|
* const seq = tf.tensor1d([0, 3, 9, 10, 10]);
|
|
* values = tf.tensor1d([0, 4, 10]);
|
|
* const result2 = tf.searchSorted(seq, values, 'left');
|
|
* result2.print(); // [0, 2, 3]
|
|
* const result3 = tf.searchSorted(seq, values, 'right');
|
|
* result3.print(); // [1, 2, 5]
|
|
*
|
|
* const sortedSequence = tf.tensor2d([[0., 3., 8., 9., 10.],
|
|
* [1., 2., 3., 4., 5.]]);
|
|
* values = tf.tensor2d([[9.8, 2.1, 4.3],
|
|
* [0.1, 6.6, 4.5, ]]);
|
|
* const result4 = tf.searchSorted(sortedSequence, values, 'left');
|
|
* result4.print(); // [[4, 1, 2], [0, 5, 4]]
|
|
* ```
|
|
* @param sortedSequence: N-D. Sorted sequence.
|
|
* @param values: N-D. Search values.
|
|
* @param side: 'left'|'right'. Defaults to 'left'. 'left' corresponds to lower
|
|
* bound and 'right' to upper bound.
|
|
* @return An N-D int32 tensor the size of values containing the result of
|
|
* applying either lower bound or upper bound (depending on side) to each
|
|
* value. The result is not a global index to the entire Tensor, but the
|
|
* index in the last dimension.
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function searchSorted_(sortedSequence, values, side) {
|
|
if (side === void 0) { side = 'left'; }
|
|
var $sortedSequence = convertToTensor(sortedSequence, 'sortedSequence', 'searchSorted');
|
|
var $values = convertToTensor(values, 'values', 'searchSorted');
|
|
var sequenceSize = $sortedSequence.shape[$sortedSequence.shape.length - 1];
|
|
var valuesSize = $values.shape[$values.shape.length - 1];
|
|
var $sortedSequence2D = reshape($sortedSequence, [-1, sequenceSize]);
|
|
var $values2D = reshape($values, [-1, valuesSize]);
|
|
if ($sortedSequence2D.rank < 2) {
|
|
throw new Error("Sorted input argument must be at least 2-dimensional");
|
|
}
|
|
if ($sortedSequence2D.shape[0] !== $values2D.shape[0]) {
|
|
throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");
|
|
}
|
|
if (sizeFromShape($values2D.shape) >= INT32_MAX) {
|
|
throw new Error("values tensor size must less than ".concat(INT32_MAX));
|
|
}
|
|
if ($sortedSequence2D.shape[1] >= INT32_MAX) {
|
|
throw new Error("trailing dim_size must less than ".concat(INT32_MAX, " for int32 output type, was ").concat($sortedSequence2D.shape[1]));
|
|
}
|
|
var inputs = {
|
|
sortedSequence: $sortedSequence2D,
|
|
values: $values2D,
|
|
};
|
|
var attrs = { side: side };
|
|
return ENGINE.runKernel(SearchSorted, inputs, attrs);
|
|
}
|
|
var searchSorted = /* @__PURE__ */ op({ searchSorted_: searchSorted_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Searches for where a value would go in a sorted sequence.
|
|
*
|
|
* This is not a method for checking containment (like javascript in).
|
|
*
|
|
* The typical use case for this operation is "binning", "bucketing", or
|
|
* "discretizing". The values are assigned to bucket-indices based on the edges
|
|
* listed in 'sortedSequence'. This operation returns the bucket-index for each
|
|
* value.
|
|
*
|
|
* The index returned corresponds to the first edge greater than or equal to the
|
|
* value.
|
|
*
|
|
* The axis is not settable for this operation. It always operates on the
|
|
* innermost dimension (axis=-1). The operation will accept any number of outer
|
|
* dimensions.
|
|
*
|
|
* Note: This operation assumes that 'lowerBound' is sorted along the
|
|
* innermost axis, maybe using 'sort(..., axis=-1)'. If the sequence is not
|
|
* sorted no error is raised and the content of the returned tensor is not well
|
|
* defined.
|
|
*
|
|
* ```js
|
|
* const edges = tf.tensor1d([-1, 3.3, 9.1, 10.0]);
|
|
* let values = tf.tensor1d([0.0, 4.1, 12.0]);
|
|
* const result1 = tf.lowerBound(edges, values);
|
|
* result1.print(); // [1, 2, 4]
|
|
*
|
|
* const seq = tf.tensor1d([0, 3, 9, 10, 10]);
|
|
* values = tf.tensor1d([0, 4, 10]);
|
|
* const result2 = tf.lowerBound(seq, values);
|
|
* result2.print(); // [0, 2, 3]
|
|
*
|
|
* const sortedSequence = tf.tensor2d([[0., 3., 8., 9., 10.],
|
|
* [1., 2., 3., 4., 5.]]);
|
|
* values = tf.tensor2d([[9.8, 2.1, 4.3],
|
|
* [0.1, 6.6, 4.5, ]]);
|
|
* const result3 = tf.lowerBound(sortedSequence, values);
|
|
* result3.print(); // [[4, 1, 2], [0, 5, 4]]
|
|
* ```
|
|
* @param sortedSequence: N-D. Sorted sequence.
|
|
* @param values: N-D. Search values.
|
|
* @return An N-D int32 tensor the size of values containing the result of
|
|
* applying lower bound to each value. The result is not a global index to
|
|
* the entire Tensor, but the index in the last dimension.
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function lowerBound(sortedSequence, values) {
|
|
return searchSorted(sortedSequence, values, 'left');
|
|
}
|
|
|
|
/**
|
|
* @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 2D max pooling of an image.
|
|
*
|
|
* @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 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 dilations The dilation rates: `[dilationHeight, dilationWidth]`
|
|
* in which we sample input values across the height and width dimensions
|
|
* in dilated pooling. 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 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.
|
|
*/
|
|
function maxPool_(x, filterSize, strides, pad, dimRoundingMode) {
|
|
var $x = convertToTensor(x, 'x', 'maxPool');
|
|
var dilations = 1;
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in maxPool: input must be rank 4 but got rank ".concat(x4D.rank, "."); });
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in maxPool: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
checkPadOnDimRoundingMode('maxPool', pad, dimRoundingMode);
|
|
var inputs = { x: x4D };
|
|
var attrs = { filterSize: filterSize, strides: strides, pad: pad, dimRoundingMode: dimRoundingMode };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(MaxPool, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var maxPool = /* @__PURE__ */ op({ maxPool_: maxPool_ });
|
|
|
|
/**
|
|
* @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 3D max pooling.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor5d([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 2, 2, 1]);
|
|
* const result = tf.maxPool3d(x, 2, 1, 'valid');
|
|
* result.print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor, of rank 5 or rank 4 of shape
|
|
* `[batch, depth, height, width, inChannels]`.
|
|
* @param filterSize The filter size:
|
|
* `[filterDepth, filterHeight, filterWidth]`.
|
|
* If `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 `strideDepth == 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 1*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.
|
|
* @param dataFormat An optional string from: "NDHWC", "NCDHW". Defaults to
|
|
* "NDHWC". Specify the data format of the input and output data. With the
|
|
* default format "NDHWC", the data is stored in the order of: [batch,
|
|
* depth, height, width, channels]. Only "NDHWC" is currently supported.
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function maxPool3d_(x, filterSize, strides, pad, dimRoundingMode, dataFormat) {
|
|
if (filterSize === void 0) { filterSize = [1, 1, 1]; }
|
|
if (dataFormat === void 0) { dataFormat = 'NDHWC'; }
|
|
var $x = convertToTensor(x, 'x', 'maxPool3d');
|
|
var x5D = $x;
|
|
var reshapedTo5D = false;
|
|
if ($x.rank === 4) {
|
|
reshapedTo5D = true;
|
|
x5D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2], $x.shape[3]]);
|
|
}
|
|
assert(x5D.rank === 5, function () { return "Error in maxPool3d: x must be rank 5 but got rank ".concat(x5D.rank, "."); });
|
|
assert(dataFormat === 'NDHWC', function () { return "Error in maxPool3d: Only NDHWC is currently supported, " +
|
|
"but got dataFormat of ".concat(dataFormat); });
|
|
checkPadOnDimRoundingMode('maxPool3d', pad, dimRoundingMode);
|
|
var inputs = { x: x5D };
|
|
var attrs = { filterSize: filterSize, strides: strides, pad: pad, dimRoundingMode: dimRoundingMode, dataFormat: dataFormat };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(MaxPool3D, inputs, attrs);
|
|
if (reshapedTo5D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3], res.shape[4]]);
|
|
}
|
|
return res;
|
|
}
|
|
var maxPool3d = /* @__PURE__ */ op({ maxPool3d_: maxPool3d_ });
|
|
|
|
/**
|
|
* @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 2D max pooling of an image with Argmax index.
|
|
* The indices in argmax are flattened, so that a maximum value at position `[b,
|
|
* y, x, c]` becomes flattened index: `(y * width + x) * channels + c` if
|
|
* include_batch_in_index is False; `((b * height + y) * width + x) * channels
|
|
* +c` if include_batch_in_index is True.
|
|
*
|
|
* The indices returned are always in `[0, height) x [0, width)` before
|
|
* flattening.
|
|
*
|
|
* @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 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 dataFormat An optional string from: "NDHWC", "NCDHW". Defaults to
|
|
* "NDHWC". Specify the data format of the input and output data. With the
|
|
* default format "NDHWC", the data is stored in the order of: [batch,
|
|
* depth, height, width, channels]. Only "NDHWC" is currently supported.
|
|
* @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 includeBatchIndex Defaults to False. Whether to include batch
|
|
* dimension in flattened index of argmax.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function maxPoolWithArgmax_(x, filterSize, strides, pad, includeBatchInIndex) {
|
|
if (includeBatchInIndex === void 0) { includeBatchInIndex = false; }
|
|
var $x = convertToTensor(x, 'x', 'maxPoolWithArgmax');
|
|
var inputs = { x: $x };
|
|
var attrs = { filterSize: filterSize, strides: strides, pad: pad, includeBatchInIndex: includeBatchInIndex };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var result = ENGINE.runKernel(MaxPoolWithArgmax, inputs, attrs);
|
|
return { result: result[0], indexes: result[1] };
|
|
}
|
|
var maxPoolWithArgmax = /* @__PURE__ */ op({ maxPoolWithArgmax_: maxPoolWithArgmax_ });
|
|
|
|
/**
|
|
* 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) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'maximum');
|
|
var $b = convertToTensor(b, 'b', 'maximum');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
if ($a.dtype === 'bool') {
|
|
$a = cast($a, 'int32');
|
|
$b = cast($b, 'int32');
|
|
}
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Maximum, inputs);
|
|
}
|
|
var maximum = /* @__PURE__ */ op({ maximum_: maximum_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes the mean of elements across dimensions of a `tf.Tensor`.
|
|
*
|
|
* Reduces `x` along the dimensions given in `axis`. Unless `keepDims` is
|
|
* true, the rank of the `tf.Tensor` is reduced by 1 for each entry in `axis`.
|
|
* If `keepDims` is true, the reduced dimensions are retained with length 1.
|
|
* If `axis` has no entries, all dimensions are reduced, and a `tf.Tensor` with
|
|
* a single element is returned.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* x.mean().print(); // or tf.mean(a)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.mean(axis).print(); // or tf.mean(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor.
|
|
* @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 mean_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'mean');
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(Mean, inputs, attrs);
|
|
}
|
|
var mean = /* @__PURE__ */ op({ mean_: mean_ });
|
|
|
|
/**
|
|
* @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(shape, dtype) {
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (dtype === 'complex64') {
|
|
var real = zeros(shape, 'float32');
|
|
var imag = zeros(shape, 'float32');
|
|
return complex(real, imag);
|
|
}
|
|
var 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(shape, dtype) {
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (dtype === 'complex64') {
|
|
var real = ones(shape, 'float32');
|
|
var imag = zeros(shape, 'float32');
|
|
return complex(real, imag);
|
|
}
|
|
var values = makeOnesTypedArray(sizeFromShape(shape), dtype);
|
|
return ENGINE.makeTensor(values, shape, dtype);
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Broadcasts parameters for evaluation on an N-D grid.
|
|
*
|
|
* Given N one-dimensional coordinate arrays `*args`, returns a list `outputs`
|
|
* of N-D coordinate arrays for evaluating expressions on an N-D grid.
|
|
*
|
|
* Notes:
|
|
* `meshgrid` supports cartesian ('xy') and matrix ('ij') indexing conventions.
|
|
* When the `indexing` argument is set to 'xy' (the default), the broadcasting
|
|
* instructions for the first two dimensions are swapped.
|
|
* Examples:
|
|
* Calling `const [X, Y] = meshgrid(x, y)` with the tensors
|
|
*
|
|
* ```javascript
|
|
* const x = [1, 2, 3];
|
|
* const y = [4, 5, 6];
|
|
* const [X, Y] = tf.meshgrid(x, y);
|
|
* // X = [[1, 2, 3],
|
|
* // [1, 2, 3],
|
|
* // [1, 2, 3]]
|
|
* // Y = [[4, 4, 4],
|
|
* // [5, 5, 5],
|
|
* // [6, 6, 6]]
|
|
* ```
|
|
*
|
|
* @param x Tensor with rank geq 1.
|
|
* @param y Tensor with rank geq 1.
|
|
* @param indexing
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Slicing and Joining'}
|
|
*/
|
|
function meshgrid(x, y, _a) {
|
|
var _b = _a === void 0 ? {} : _a, _c = _b.indexing, indexing = _c === void 0 ? 'xy' : _c;
|
|
if (indexing !== 'xy' && indexing !== 'ij') {
|
|
throw new TypeError("".concat(indexing, " is not a valid third argument to meshgrid"));
|
|
}
|
|
if (x === undefined) {
|
|
return [];
|
|
}
|
|
var $x = convertToTensor(x, 'x', 'meshgrid', x instanceof Tensor ? x.dtype : 'float32');
|
|
if (y === undefined) {
|
|
return [$x];
|
|
}
|
|
var $y = convertToTensor(y, 'y', 'meshgrid', y instanceof Tensor ? y.dtype : 'float32');
|
|
var w = sizeFromShape($x.shape);
|
|
var h = sizeFromShape($y.shape);
|
|
if (indexing === 'xy') {
|
|
$x = reshape($x, [1, -1]);
|
|
$y = reshape($y, [-1, 1]);
|
|
return [
|
|
matMul$1(ones([h, 1], $x.dtype), $x),
|
|
matMul$1($y, ones([1, w], $y.dtype)),
|
|
];
|
|
}
|
|
$x = reshape($x, [-1, 1]);
|
|
$y = reshape($y, [1, -1]);
|
|
return [
|
|
matMul$1($x, ones([1, h], $x.dtype)),
|
|
matMul$1(ones([w, 1], $y.dtype), $y),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Returns the min of a and b (`a < b ? a : b`) element-wise.
|
|
* Supports broadcasting.
|
|
*
|
|
* We also expose `minimumStrict` which has the same signature as this op and
|
|
* asserts that `a` and `b` are the same shape (does not broadcast).
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 4, 3, 16]);
|
|
* const b = tf.tensor1d([1, 2, 9, 4]);
|
|
*
|
|
* a.minimum(b).print(); // or tf.minimum(a, b)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Broadcast minimum a with b.
|
|
* const a = tf.tensor1d([2, 4, 6, 8]);
|
|
* const b = tf.scalar(5);
|
|
*
|
|
* a.minimum(b).print(); // or tf.minimum(a, b)
|
|
* ```
|
|
*
|
|
* @param a The first tensor.
|
|
* @param b The second tensor. Must have the same type as `a`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Arithmetic'}
|
|
*/
|
|
function minimum_(a, b) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'minimum');
|
|
var $b = convertToTensor(b, 'b', 'minimum');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
if ($a.dtype === 'bool') {
|
|
$a = cast($a, 'int32');
|
|
$b = cast($b, 'int32');
|
|
}
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Minimum, inputs);
|
|
}
|
|
var minimum = /* @__PURE__ */ op({ minimum_: minimum_ });
|
|
|
|
/**
|
|
* @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` using mirror padding.
|
|
*
|
|
* This operation implements the `REFLECT` and `SYMMETRIC` modes of pad.
|
|
*
|
|
* ```js
|
|
* const x = tf.range(0, 9).reshape([1, 1, 3, 3]);
|
|
* x.mirrorPad([[0, 0], [0, 0], [2, 2], [2, 2]], 'reflect').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.
|
|
* In "reflect" mode, the padded regions do not include the borders,
|
|
* while in "symmetric" mode the padded regions do include the borders.
|
|
* For example, if the input is `[1, 2, 3]` and paddings is `[0, 2]`,
|
|
* then the output is `[1, 2, 3, 2, 1]` in "reflect" mode, and
|
|
* `[1, 2, 3, 3, 2]` in "symmetric" mode.
|
|
* If `mode` is "reflect" then both `paddings[D, 0]` and `paddings[D, 1]`
|
|
* must be no greater than `x.shape[D] - 1`. If mode is "symmetric"
|
|
* then both `paddings[D, 0]` and `paddings[D, 1]` must be no greater than
|
|
* `x.shape[D]`
|
|
* @param mode String to specify padding mode. Can be `'reflect' | 'symmetric'`
|
|
*/
|
|
/** @doc {heading: 'Tensors', subheading: 'Transformations'} */
|
|
function mirrorPad_(x, paddings, mode) {
|
|
assert(mode === 'reflect' || mode === 'symmetric', function () { return "Invalid mode. Mode must be either reflect or symmetric. " +
|
|
"Got ".concat(mode, "."); });
|
|
var $x = convertToTensor(x, 'x', 'mirrorPad');
|
|
if ($x.rank === 0) {
|
|
throw new Error('mirrorPad(scalar) is not defined. ' +
|
|
'Pass non-scalar to mirrorPad');
|
|
}
|
|
assert(paddings.length === $x.rank, function () { return "Padding doesn't match input. Must be ".concat($x.rank, ". ") +
|
|
"Got ".concat(paddings.length, "."); });
|
|
var shapeOffset = mode === 'reflect' ? 1 : 0;
|
|
var _loop_1 = function (i) {
|
|
assert(paddings[i].length === 2, function () { return "Invalid number of paddings. Must be length of 2 each."; });
|
|
assert(paddings[i][0] >= 0 && paddings[i][0] <= $x.shape[i] - shapeOffset &&
|
|
paddings[i][1] >= 0 && paddings[i][1] <= $x.shape[i] - shapeOffset, function () { return "Padding in dimension ".concat(i, " cannot be greater than or equal ") +
|
|
"to ".concat($x.shape[i] - shapeOffset, " or less than 0 for input of ") +
|
|
"shape ".concat($x.shape); });
|
|
};
|
|
for (var i = 0; i < $x.rank; i++) {
|
|
_loop_1(i);
|
|
}
|
|
var attrs = { paddings: paddings, mode: mode };
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(MirrorPad, inputs, attrs);
|
|
}
|
|
var mirrorPad = /* @__PURE__ */ op({ mirrorPad_: mirrorPad_ });
|
|
|
|
/**
|
|
* Returns the mod of a and b element-wise.
|
|
* `floor(x / y) * y + mod(x, y) = x`
|
|
* Supports broadcasting.
|
|
*
|
|
* We also expose `tf.modStrict` 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.mod(b).print(); // or tf.mod(a, b)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Broadcast a mod b.
|
|
* const a = tf.tensor1d([2, 4, 6, 8]);
|
|
* const b = tf.scalar(5);
|
|
*
|
|
* a.mod(b).print(); // or tf.mod(a, b)
|
|
* ```
|
|
*
|
|
* @param a The first tensor.
|
|
* @param b The second tensor. Must have the same type as `a`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Arithmetic'}
|
|
*/
|
|
function mod_(a, b) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'mod');
|
|
var $b = convertToTensor(b, 'b', 'mod');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(Mod, inputs);
|
|
}
|
|
var mod = /* @__PURE__ */ op({ mod_: mod_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Calculates the mean and variance of `x`. The mean and variance are
|
|
* calculated by aggregating the contents of `x` across `axes`. If `x` is
|
|
* 1-D and `axes = [0]` this is just the mean and variance of a vector.
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The dimension(s) along with to compute mean and
|
|
* variance. By default it reduces all dimensions.
|
|
* @param keepDims If true, the moments have the same dimensionality as the
|
|
* input.
|
|
* @return An object with two keys: `mean` and `variance`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function moments_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
x = convertToTensor(x, 'x', 'moments');
|
|
var axes = parseAxisParam(axis, x.shape);
|
|
var xMean = mean(x, axes, keepDims);
|
|
var keepDimsShape = xMean.shape;
|
|
if (!keepDims) {
|
|
keepDimsShape = expandShapeToKeepDim(xMean.shape, axes);
|
|
}
|
|
var devSquared = square(sub(cast(x, 'float32'), reshape(xMean, keepDimsShape)));
|
|
var variance = mean(devSquared, axes, keepDims);
|
|
return { mean: xMean, variance: variance };
|
|
}
|
|
var moments = /* @__PURE__ */ op({ moments_: moments_ });
|
|
|
|
/**
|
|
* Computes the next states and outputs of a stack of LSTMCells.
|
|
*
|
|
* Each cell output is used as input to the next cell.
|
|
*
|
|
* Returns `[cellState, cellOutput]`.
|
|
*
|
|
* Derived from tf.contrib.rn.MultiRNNCell.
|
|
*
|
|
* @param lstmCells Array of LSTMCell functions.
|
|
* @param data The input to the cell.
|
|
* @param c Array of previous cell states.
|
|
* @param h Array of previous cell outputs.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'RNN'}
|
|
*/
|
|
function multiRNNCell_(lstmCells, data, c, h) {
|
|
var $data = convertToTensor(data, 'data', 'multiRNNCell');
|
|
var $c = convertToTensorArray(c, 'c', 'multiRNNCell');
|
|
var $h = convertToTensorArray(h, 'h', 'multiRNNCell');
|
|
var input = $data;
|
|
var newStates = [];
|
|
for (var i = 0; i < lstmCells.length; i++) {
|
|
var output = lstmCells[i](input, $c[i], $h[i]);
|
|
newStates.push(output[0]);
|
|
newStates.push(output[1]);
|
|
input = output[1];
|
|
}
|
|
var newC = [];
|
|
var newH = [];
|
|
for (var i = 0; i < newStates.length; i += 2) {
|
|
newC.push(newStates[i]);
|
|
newH.push(newStates[i + 1]);
|
|
}
|
|
return [newC, newH];
|
|
}
|
|
var multiRNNCell = /* @__PURE__ */ op({ multiRNNCell_: multiRNNCell_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values drawn from a multinomial distribution.
|
|
*
|
|
* ```js
|
|
* const probs = tf.tensor([.75, .25]);
|
|
* tf.multinomial(probs, 3).print();
|
|
* ```
|
|
*
|
|
* @param logits 1D array with unnormalized log-probabilities, or
|
|
* 2D array of shape `[batchSize, numOutcomes]`. See the `normalized`
|
|
* parameter.
|
|
* @param numSamples Number of samples to draw for each row slice.
|
|
* @param seed The seed number.
|
|
* @param normalized Whether the provided `logits` are normalized true
|
|
* probabilities (sum to 1). Defaults to false.
|
|
* @return 1D array of shape `[numSamples]`, or 2D array of shape
|
|
* `[batchSize, numSamples]`, depending on the rank of the input.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function multinomial_(logits, numSamples, seed, normalized) {
|
|
if (normalized === void 0) { normalized = false; }
|
|
var $logits = convertToTensor(logits, 'logits', 'multinomial');
|
|
var numOutcomes = $logits.size;
|
|
var origRank = $logits.rank;
|
|
if (numOutcomes < 2) {
|
|
throw new Error("Error in multinomial: you need at least 2 outcomes, but got " +
|
|
"".concat(numOutcomes, "."));
|
|
}
|
|
if (origRank > 2) {
|
|
throw new Error("Rank of probabilities must be 1 or 2, but is ".concat(origRank));
|
|
}
|
|
// TODO(lina128): Investigate correct seed behavior. The code seems not allow
|
|
// setting see to 0.
|
|
seed = seed || Math.random();
|
|
// The kernel only accepts (and returns) rank 2 tensors.
|
|
var logits2D = origRank === 1 ? reshape($logits, [1, -1]) : $logits;
|
|
var inputs = { logits: logits2D };
|
|
var attrs = { numSamples: numSamples, seed: seed, normalized: normalized };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(Multinomial, inputs, attrs);
|
|
// tslint:disable-next-line:no-unnecessary-type-assertion
|
|
return origRank === 1 ? reshape(res, [res.size]) : res;
|
|
}
|
|
var multinomial = /* @__PURE__ */ op({ multinomial_: multinomial_ });
|
|
|
|
/**
|
|
* Returns the truth value of (a != b) element-wise. Supports broadcasting.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2, 3]);
|
|
* const b = tf.tensor1d([0, 2, 3]);
|
|
*
|
|
* a.notEqual(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 notEqual_(a, b) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'notEqual', 'string_or_numeric');
|
|
var $b = convertToTensor(b, 'b', 'notEqual', 'string_or_numeric');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
return ENGINE.runKernel(NotEqual, inputs);
|
|
}
|
|
var notEqual = /* @__PURE__ */ op({ notEqual_: notEqual_ });
|
|
|
|
/**
|
|
* @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 one-hot `tf.Tensor`. The locations represented by `indices` take
|
|
* value `onValue` (defaults to 1), while all other locations take value
|
|
* `offValue` (defaults to 0). If `indices` is rank `R`, the output has rank
|
|
* `R+1` with the last axis of size `depth`.
|
|
* `indices` used to encode prediction class must start from 0. For example,
|
|
* if you have 3 classes of data, class 1 should be encoded as 0, class 2
|
|
* should be 1, and class 3 should be 2.
|
|
*
|
|
* ```js
|
|
* tf.oneHot(tf.tensor1d([0, 1], 'int32'), 3).print();
|
|
* ```
|
|
*
|
|
* @param indices `tf.Tensor` of indices with dtype `int32`. Indices must
|
|
* start from 0.
|
|
* @param depth The depth of the one hot dimension.
|
|
* @param onValue A number used to fill in the output when the index matches
|
|
* the location.
|
|
* @param offValue A number used to fill in the output when the index does
|
|
* not match the location.
|
|
* @param dtype The dtype of the output tensor, default to 'int32'.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function oneHot_(indices, depth, onValue, offValue, dtype) {
|
|
if (onValue === void 0) { onValue = 1; }
|
|
if (offValue === void 0) { offValue = 0; }
|
|
if (dtype === void 0) { dtype = 'int32'; }
|
|
if (depth < 2) {
|
|
throw new Error("Error in oneHot: depth must be >=2, but it is ".concat(depth));
|
|
}
|
|
var $indices = convertToTensor(indices, 'indices', 'oneHot', 'int32');
|
|
var inputs = { indices: $indices };
|
|
var attrs = { dtype: dtype, depth: depth, onValue: onValue, offValue: offValue };
|
|
return ENGINE.runKernel(OneHot, inputs, attrs);
|
|
}
|
|
var oneHot = /* @__PURE__ */ op({ oneHot_: oneHot_ });
|
|
|
|
/**
|
|
* @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 with the same shape as the
|
|
* given tensor.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor([1, 2]);
|
|
* tf.onesLike(x).print();
|
|
* ```
|
|
* @param x A tensor.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function onesLike_(x) {
|
|
var $x = convertToTensor(x, 'x', 'onesLike');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(OnesLike, inputs);
|
|
}
|
|
var onesLike = /* @__PURE__ */ op({ onesLike_: onesLike_ });
|
|
|
|
/**
|
|
* Computes the outer product of two vectors, `v1` and `v2`.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2, 3]);
|
|
* const b = tf.tensor1d([3, 4, 5]);
|
|
*
|
|
* tf.outerProduct(a, b).print();
|
|
* ```
|
|
* @param v1 The first vector in the outer product operation.
|
|
* @param v2 The second vector in the outer product operation.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Matrices'}
|
|
*/
|
|
function outerProduct_(v1, v2) {
|
|
var $v1 = convertToTensor(v1, 'v1', 'outerProduct');
|
|
var $v2 = convertToTensor(v2, 'v2', 'outerProduct');
|
|
assert($v1.rank === 1 && $v2.rank === 1, function () { return "Error in outerProduct: inputs must be rank 1, but got ranks " +
|
|
"".concat($v1.rank, " and ").concat($v2.rank, "."); });
|
|
var v12D = reshape($v1, [-1, 1]);
|
|
var v22D = reshape($v2, [1, -1]);
|
|
return matMul$1(v12D, v22D);
|
|
}
|
|
var outerProduct = /* @__PURE__ */ op({ outerProduct_: outerProduct_ });
|
|
|
|
/**
|
|
* @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) {
|
|
if (constantValue === void 0) { constantValue = 0; }
|
|
var $x = convertToTensor(x, 'x', 'pad');
|
|
if ($x.rank === 0) {
|
|
throw new Error('pad(scalar) is not defined. Pass non-scalar to pad');
|
|
}
|
|
var attrs = { paddings: paddings, constantValue: constantValue };
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(PadV2, inputs, attrs);
|
|
}
|
|
var pad = /* @__PURE__ */ op({ pad_: pad_ });
|
|
|
|
/**
|
|
* Pads a `tf.Tensor1D` with a given value and paddings. See `pad` for details.
|
|
*/
|
|
function pad1d_(x, paddings, constantValue) {
|
|
if (constantValue === void 0) { constantValue = 0; }
|
|
assert(paddings.length === 2, function () { return 'Invalid number of paddings. Must be length of 2.'; });
|
|
return pad(x, [paddings], constantValue);
|
|
}
|
|
var pad1d = /* @__PURE__ */ op({ pad1d_: pad1d_ });
|
|
|
|
/**
|
|
* Pads a `tf.Tensor2D` with a given value and paddings. See `pad` for details.
|
|
*/
|
|
function pad2d_(x, paddings, constantValue) {
|
|
if (constantValue === void 0) { constantValue = 0; }
|
|
assert(paddings.length === 2 && paddings[0].length === 2 &&
|
|
paddings[1].length === 2, function () { return 'Invalid number of paddings. Must be length of 2 each.'; });
|
|
return pad(x, paddings, constantValue);
|
|
}
|
|
var pad2d = /* @__PURE__ */ op({ pad2d_: pad2d_ });
|
|
|
|
/**
|
|
* Pads a `tf.Tensor3D` with a given value and paddings. See `pad` for details.
|
|
*/
|
|
function pad3d_(x, paddings, constantValue) {
|
|
if (constantValue === void 0) { constantValue = 0; }
|
|
assert(paddings.length === 3 && paddings[0].length === 2 &&
|
|
paddings[1].length === 2 && paddings[2].length === 2, function () { return 'Invalid number of paddings. Must be length of 2 each.'; });
|
|
return pad(x, paddings, constantValue);
|
|
}
|
|
var pad3d = /* @__PURE__ */ op({ pad3d_: pad3d_ });
|
|
|
|
/**
|
|
* Pads a `tf.Tensor4D` with a given value and paddings. See `pad` for details.
|
|
*/
|
|
function pad4d_(x, paddings, constantValue) {
|
|
if (constantValue === void 0) { constantValue = 0; }
|
|
assert(paddings.length === 4 && paddings[0].length === 2 &&
|
|
paddings[1].length === 2 && paddings[2].length === 2 &&
|
|
paddings[3].length === 2, function () { return 'Invalid number of paddings. Must be length of 2 each.'; });
|
|
return pad(x, paddings, constantValue);
|
|
}
|
|
var pad4d = /* @__PURE__ */ op({ pad4d_: pad4d_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'spaceToBatchND');
|
|
assert($x.rank >= 1 + blockShape.length, function () { return "input rank ".concat($x.rank, " should be > than [blockShape] ").concat(blockShape.length); });
|
|
assert(paddings.length === blockShape.length, function () { return "paddings.shape[0] ".concat(paddings.length, " must be equal to [blockShape] ").concat(blockShape.length); });
|
|
assert($x.shape.reduce(function (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), function () { return "input spatial dimensions ".concat($x.shape.slice(1), " with paddings ").concat(paddings.toString(), " must be divisible by blockShapes ").concat(blockShape.toString()); });
|
|
var inputs = { x: $x };
|
|
var attrs = { blockShape: blockShape, paddings: paddings };
|
|
return ENGINE.runKernel(SpaceToBatchND, inputs, attrs);
|
|
}
|
|
var spaceToBatchND = /* @__PURE__ */ op({ spaceToBatchND_: spaceToBatchND_ });
|
|
|
|
/**
|
|
* Performs an N-D pooling operation
|
|
*
|
|
* @param input The input tensor, of rank 4 or rank 3 of shape
|
|
* `[batch, height, width, inChannels]`. If rank 3, batch of 1 is assumed.
|
|
* @param windowShape The filter size: `[filterHeight, filterWidth]`. If
|
|
* `filterSize` is a single number, then `filterHeight == filterWidth`.
|
|
* @param poolingType The type of pooling, either 'max' or 'avg'.
|
|
* @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_guides/python/nn#Convolution](
|
|
* https://www.tensorflow.org/api_guides/python/nn#Convolution)
|
|
* @param dilations The dilation rates: `[dilationHeight, dilationWidth]`
|
|
* in which we sample input values across the height and width dimensions
|
|
* in dilated pooling. Defaults to `[1, 1]`. If `dilationRate` is a single
|
|
* number, then `dilationHeight == dilationWidth`. If it is greater than
|
|
* 1, then all values of `strides` must be 1.
|
|
* @param strides The strides of the pooling: `[strideHeight, strideWidth]`. If
|
|
* `strides` is a single number, then `strideHeight == strideWidth`.
|
|
* @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function pool_(input, windowShape, poolingType, pad, dilations, strides, dimRoundingMode) {
|
|
if (dilations == null) {
|
|
dilations = [1, 1];
|
|
}
|
|
if (strides == null) {
|
|
strides = 1;
|
|
}
|
|
if (pad === 0) {
|
|
pad = 'valid';
|
|
}
|
|
var $x = convertToTensor(input, 'x', 'maxPool');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in pool: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
var convInfo = computePool2DInfo(x4D.shape, windowShape, strides, dilations, pad);
|
|
var dilation = [convInfo.dilationHeight, convInfo.dilationWidth];
|
|
// The following implementation does batchToSpace(pool(spaceToBatch(x)))
|
|
// whenever dilation > 1 since the TF kernels do not support dilation > 1.
|
|
// tslint:disable-next-line:max-line-length
|
|
// https://github.com/tensorflow/tensorflow/blob/50f6bb67dc98c9b74630b6047aae7a4f8a40fd02/tensorflow/python/ops/nn_ops.py#L1037
|
|
var basePadding;
|
|
if (pad === 'same') {
|
|
basePadding = withSpaceToBatchBasePaddings([convInfo.filterHeight, convInfo.filterWidth], dilation);
|
|
}
|
|
else {
|
|
basePadding = [[0, 0], [0, 0]];
|
|
}
|
|
var isDilationOne = dilation[0] === 1 && dilation[1] === 1;
|
|
var _a = __read(requiredSpaceToBatchPaddings([convInfo.inHeight, convInfo.inWidth], dilation, basePadding), 2), adjustedPadding = _a[0], adjustedCrops = _a[1];
|
|
var convertedPad = isDilationOne ? pad : 'valid';
|
|
var convertedX = isDilationOne ? x4D : spaceToBatchND(x4D, dilation, adjustedPadding);
|
|
var forwardOp = poolingType === 'avg' ?
|
|
function () { return avgPool(convertedX, windowShape, strides, convertedPad, dimRoundingMode); } :
|
|
function () { return maxPool(convertedX, windowShape, strides, convertedPad, dimRoundingMode); };
|
|
var y = forwardOp();
|
|
var res = isDilationOne ? y : batchToSpaceND(y, dilation, adjustedCrops);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
// Helper function to compute crops and paddings for pool with dilation > 1.
|
|
// tslint:disable-next-line:max-line-length
|
|
// https://github.com/tensorflow/tensorflow/blob/50f6bb67dc98c9b74630b6047aae7a4f8a40fd02/tensorflow/python/ops/array_ops.py#L2184
|
|
function requiredSpaceToBatchPaddings(inputShape, blockShape, basePadding) {
|
|
var padStart = basePadding.map(function (b) { return b[0]; });
|
|
var origPadEnd = basePadding.map(function (b) { return b[1]; });
|
|
var fullInputShape = inputShape.concat(padStart, origPadEnd);
|
|
var padEndExtra = blockShape.map(function (b, i) { return (b - fullInputShape[i] % b) % b; });
|
|
var padEnd = origPadEnd.map(function (s, i) { return s + padEndExtra[i]; });
|
|
var paddings = blockShape.map(function (_, i) { return [padStart[i], padEnd[i]]; });
|
|
var crops = blockShape.map(function (_, i) { return [0, padEndExtra[i]]; });
|
|
return [paddings, crops];
|
|
}
|
|
// Helper function to compute base paddings for pool with dilation > 1.
|
|
// tslint:disable-next-line:max-line-length
|
|
// https://github.com/tensorflow/tensorflow/blob/50f6bb67dc98c9b74630b6047aae7a4f8a40fd02/tensorflow/python/ops/nn_ops.py#L524
|
|
function withSpaceToBatchBasePaddings(filterShape, dilation) {
|
|
// Spatial dimensions of the filters and the upsampled filters in which we
|
|
// introduce (rate - 1) zeros between consecutive filter values.
|
|
var dilatedFilterShape = filterShape.map(function (s, i) {
|
|
return s + (s - 1) * (dilation[i] - 1);
|
|
});
|
|
var padExtraShape = dilatedFilterShape.map(function (s) { return s - 1; });
|
|
// When padding is odd, we pad more at end, following the same
|
|
// convention as conv2d.
|
|
var padExtraStart = padExtraShape.map(function (s) { return Math.floor(s / 2); });
|
|
var padExtraEnd = padExtraShape.map(function (s, i) { return s - padExtraStart[i]; });
|
|
return padExtraShape.map(function (_, i) {
|
|
return [padExtraStart[i], padExtraEnd[i]];
|
|
});
|
|
}
|
|
var pool = /* @__PURE__ */ op({ pool_: pool_ });
|
|
|
|
/**
|
|
* @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 leaky rectified linear element-wise with parametric alphas.
|
|
*
|
|
* `x < 0 ? alpha * x : f(x) = x`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
* const alpha = tf.scalar(0.1);
|
|
*
|
|
* x.prelu(alpha).print(); // or tf.prelu(x, alpha)
|
|
* ```
|
|
* @param x The input tensor.
|
|
* @param alpha Scaling factor for negative values.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function prelu_(x, alpha) {
|
|
var $x = convertToTensor(x, 'x', 'prelu');
|
|
var $alpha = convertToTensor(alpha, 'alpha', 'prelu');
|
|
var inputs = { x: $x, alpha: $alpha };
|
|
return ENGINE.runKernel(Prelu, inputs);
|
|
}
|
|
var prelu = /* @__PURE__ */ op({ prelu_: prelu_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Prints information about the `tf.Tensor` including its data.
|
|
*
|
|
* ```js
|
|
* const verbose = true;
|
|
* tf.tensor2d([1, 2, 3, 4], [2, 2]).print(verbose);
|
|
* ```
|
|
* @param x The tensor to be printed.
|
|
* @param verbose Whether to print verbose information about the ` Tensor`,
|
|
* including dtype and size.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function print(x, verbose) {
|
|
if (verbose === void 0) { verbose = false; }
|
|
console.log(x.toString(verbose));
|
|
}
|
|
|
|
/**
|
|
* @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 product 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.prod().print(); // or tf.prod(x)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
*
|
|
* const axis = 1;
|
|
* x.prod(axis).print(); // or tf.prod(x, axis)
|
|
* ```
|
|
*
|
|
* @param x The input tensor to compute the product 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 prod_(x, axis, keepDims) {
|
|
if (axis === void 0) { axis = null; }
|
|
if (keepDims === void 0) { keepDims = false; }
|
|
var $x = convertToTensor(x, 'x', 'prod');
|
|
if ($x.dtype === 'bool') {
|
|
// bool is not an allowed type for the underlying kernel.
|
|
$x = cast($x, 'int32');
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis, keepDims: keepDims };
|
|
return ENGINE.runKernel(Prod, inputs, attrs);
|
|
}
|
|
var prod = /* @__PURE__ */ op({ prod_: prod_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
function raggedGather_(paramsNestedSplits, paramsDenseValues, indices, outputRaggedRank) {
|
|
var $paramsNestedSplits = paramsNestedSplits.map(function (t, i) { return convertToTensor(t, "tensors".concat(i), 'raggedGather', 'int32'); });
|
|
var $paramsDenseValues = convertToTensor(paramsDenseValues, 'paramsDenseValues', 'raggedGather');
|
|
var $indices = convertToTensor(indices, 'indices', 'raggedGather', 'int32');
|
|
var inputs = {
|
|
paramsNestedSplits: $paramsNestedSplits,
|
|
paramsDenseValues: $paramsDenseValues,
|
|
indices: $indices,
|
|
};
|
|
var attrs = { outputRaggedRank: outputRaggedRank };
|
|
var result = ENGINE.runKernel(RaggedGather, inputs, attrs);
|
|
return {
|
|
outputNestedSplits: result.slice(0, result.length - 1),
|
|
outputDenseValues: result[result.length - 1],
|
|
};
|
|
}
|
|
var raggedGather = /* @__PURE__ */ op({ raggedGather_: raggedGather_ });
|
|
|
|
/**
|
|
* @license
|
|
* Copyright 2022 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns a RaggedTensor result composed from rtDenseValues and rtNestedSplits,
|
|
* such that result[i] = [starts[i], starts[i] + deltas[i], ..., limits[i]]).
|
|
*
|
|
* @param starts: A Tensor. Must be one of the following types:
|
|
* 'float32', 'int32'. The starts of each range.
|
|
* @param limits: A Tensor. Must have the same type as starts. The limits of
|
|
* each range.
|
|
* @param deltas: A Tensor. Must have the same type as starts. The deltas of
|
|
* each range.
|
|
* @return A map with the following properties:
|
|
* - rtNestedSplits: A Tensor of type 'int32'.
|
|
* - rtDenseValues: A Tensor. Has the same type as starts.
|
|
*/
|
|
function raggedRange_(starts, limits, deltas) {
|
|
var $starts = convertToTensor(starts, 'starts', 'raggedRange');
|
|
var $limits = convertToTensor(limits, 'limits', 'raggedRange', $starts.dtype);
|
|
var $deltas = convertToTensor(deltas, 'deltas', 'raggedRange', $starts.dtype);
|
|
var inputs = {
|
|
starts: $starts,
|
|
limits: $limits,
|
|
deltas: $deltas,
|
|
};
|
|
var result = ENGINE.runKernel(RaggedRange, inputs);
|
|
return {
|
|
rtNestedSplits: result[0],
|
|
rtDenseValues: result[1],
|
|
};
|
|
}
|
|
var raggedRange = /* @__PURE__ */ op({ raggedRange_: raggedRange_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Create a dense tensor from a ragged tensor, possibly altering its shape.
|
|
*
|
|
* The raggedTensorToTensor op creates a dense tensor from am array of row
|
|
* partition tensors, a value vector, and default values. If the shape is
|
|
* unspecified, the minimal shape required to contain all the elements in the
|
|
* ragged tensor (the natural shape) will be used. If some dimensions are left
|
|
* unspecified, then the size of the natural shape is used in that dimension.
|
|
*
|
|
* The defaultValue will be broadcast to the output shape. After that, the
|
|
* values from the ragged tensor overwrite the default values. Note that the
|
|
* defaultValue must have less dimensions than the value.
|
|
*
|
|
* The row partition tensors are in the order of the dimensions. At present, the
|
|
* types can be: "ROW_SPLITS": the row_splits tensor from the ragged tensor.
|
|
* "VALUE_ROWIDS": the value_rowids tensor from the ragged tensor.
|
|
* "FIRST_DIM_SIZE": if value_rowids is used for the first dimension, then it
|
|
* is preceded by "FIRST_DIM_SIZE".
|
|
* ```
|
|
* @param shape: A Tensor. Must be one of the following types: 'int32'. The
|
|
* desired shape of the output tensor. If left unspecified (empty), the
|
|
* minimal shape required to contain all the elements in the ragged tensor
|
|
* (the natural shape) will be used. If some dimensions are left
|
|
* unspecified, then the size of the natural shape is used in that
|
|
* dimension.
|
|
*
|
|
* Note that dense dimensions cannot be modified by the shape argument.
|
|
* Trying to change the size of a dense dimension will cause the op to fail.
|
|
* Examples: natural shape: [4, 5, 6] shape: -1 output shape: [4, 5, 6]
|
|
*
|
|
* natural shape: [4, 5, 6] shape: [3, -1, 2] output shape: [3, 5, 2]
|
|
*
|
|
* natural shape: [4, 5, 6] shape: [3, 7, 2] output shape: [3, 7, 2]
|
|
* @param values: A Tensor. A 1D tensor representing the values of the ragged
|
|
* tensor.
|
|
* @param defaultValue: A Tensor. Must have the same type as values. The
|
|
* defaultValue when the shape is larger than the ragged tensor. The
|
|
* defaultValue is broadcast until it is the shape of the output tensor,
|
|
* and then overwritten by values in the ragged tensor. The default value
|
|
* must be compatible with this broadcast operation, and must have fewer
|
|
* dimensions than the value tensor.
|
|
* @param rowPartitionTensors: A list of at least 1 Tensor objects with the same
|
|
* type in: 'int32'.
|
|
* @param rowPartitionTypes: A list of strings. The types of the row partition
|
|
* tensors. At present, these can be:
|
|
* "ROW_SPLITS": the row_splits tensor from the ragged tensor.
|
|
* "VALUE_ROWIDS": the value_rowids tensor from the ragged tensor.
|
|
* "FIRST_DIM_SIZE": if value_rowids is used for the first dimension, then
|
|
* it is preceeded by "FIRST_DIM_SIZE". The tensors are in the order of
|
|
* the dimensions.
|
|
* @return A Tensor. Has the same type as values.
|
|
* @doc {heading: 'Operations', subheading: 'Ragged'}
|
|
*/
|
|
function raggedTensorToTensor_(shape, values, defaultValue, rowPartitionTensors, rowPartitionTypes) {
|
|
var $shape = convertToTensor(shape, 'shape', 'raggedTensorToTensor', 'int32');
|
|
var $values = convertToTensor(values, 'values', 'raggedTensorToTensor');
|
|
var $defaultValue = convertToTensor(defaultValue, 'defaultValue', 'raggedTensorToTensor', $values.dtype);
|
|
var $rowPartitionTensors = rowPartitionTensors.map(function (t, i) { return convertToTensor(t, "tensors".concat(i), 'raggedTensorToTensor', 'int32'); });
|
|
var inputs = {
|
|
shape: $shape,
|
|
values: $values,
|
|
defaultValue: $defaultValue,
|
|
rowPartitionTensors: $rowPartitionTensors
|
|
};
|
|
var attrs = { rowPartitionTypes: rowPartitionTypes };
|
|
return ENGINE.runKernel(RaggedTensorToTensor, inputs, attrs);
|
|
}
|
|
var raggedTensorToTensor = /* @__PURE__ */ op({ raggedTensorToTensor_: raggedTensorToTensor_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a random number generator
|
|
* function defined by the user.
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param randFunction A random number generator function which is called
|
|
* for each element in the output tensor.
|
|
* @param dtype The data type of the output tensor. Defaults to 'float32'.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function rand_(shape, randFunction, dtype) {
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
var size = sizeFromShape(shape);
|
|
var values = null;
|
|
if (dtype == null || dtype === 'float32') {
|
|
values = new Float32Array(size);
|
|
}
|
|
else if (dtype === 'int32') {
|
|
values = new Int32Array(size);
|
|
}
|
|
else if (dtype === 'bool') {
|
|
values = new Uint8Array(size);
|
|
}
|
|
else {
|
|
throw new Error("Unknown data type ".concat(dtype));
|
|
}
|
|
for (var i = 0; i < size; i++) {
|
|
values[i] = randFunction();
|
|
}
|
|
return ENGINE.makeTensor(values, shape, dtype);
|
|
}
|
|
var rand = /* @__PURE__ */ op({ rand_: rand_ });
|
|
|
|
var alea$1 = { exports: {} };
|
|
|
|
(function (module) {
|
|
// A port of an algorithm by Johannes Baagøe <baagoe@baagoe.com>, 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 <baagoe@baagoe.org>
|
|
//
|
|
// 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$1 = { 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$1));
|
|
var seedrandomExports = seedrandom$1.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;
|
|
var seedrandom = sr;
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
// https://en.wikipedia.org/wiki/Marsaglia_polar_method
|
|
var MPRandGauss = /** @class */ (function () {
|
|
function MPRandGauss(mean, stdDeviation, dtype, truncated, seed) {
|
|
this.mean = mean;
|
|
this.stdDev = stdDeviation;
|
|
this.dtype = dtype;
|
|
this.nextVal = NaN;
|
|
this.truncated = truncated;
|
|
if (this.truncated) {
|
|
this.upper = this.mean + this.stdDev * 2;
|
|
this.lower = this.mean - this.stdDev * 2;
|
|
}
|
|
var seedValue = seed ? seed : Math.random();
|
|
this.random = seedrandom.alea(seedValue.toString());
|
|
}
|
|
/** Returns next sample from a Gaussian distribution. */
|
|
MPRandGauss.prototype.nextValue = function () {
|
|
if (!isNaN(this.nextVal)) {
|
|
var value = this.nextVal;
|
|
this.nextVal = NaN;
|
|
return value;
|
|
}
|
|
var resultX, resultY;
|
|
var isValid = false;
|
|
while (!isValid) {
|
|
var v1 = void 0, v2 = void 0, s = void 0;
|
|
do {
|
|
v1 = 2 * this.random() - 1;
|
|
v2 = 2 * this.random() - 1;
|
|
s = v1 * v1 + v2 * v2;
|
|
} while (s >= 1 || s === 0);
|
|
var mul = Math.sqrt(-2.0 * Math.log(s) / s);
|
|
resultX = this.mean + this.stdDev * v1 * mul;
|
|
resultY = this.mean + this.stdDev * v2 * mul;
|
|
if (!this.truncated || this.isValidTruncated(resultX)) {
|
|
isValid = true;
|
|
}
|
|
}
|
|
if (!this.truncated || this.isValidTruncated(resultY)) {
|
|
this.nextVal = this.convertValue(resultY);
|
|
}
|
|
return this.convertValue(resultX);
|
|
};
|
|
/** Handles proper rounding for non-floating-point numbers. */
|
|
MPRandGauss.prototype.convertValue = function (value) {
|
|
if (this.dtype == null || this.dtype === 'float32') {
|
|
return value;
|
|
}
|
|
return Math.round(value);
|
|
};
|
|
/** Returns true if less than 2-standard-deviations from the mean. */
|
|
MPRandGauss.prototype.isValidTruncated = function (value) {
|
|
return value <= this.upper && value >= this.lower;
|
|
};
|
|
return MPRandGauss;
|
|
}());
|
|
// Marsaglia, George, and Wai Wan Tsang. 2000. "A Simple Method for Generating
|
|
// Gamma Variables."
|
|
var RandGamma = /** @class */ (function () {
|
|
function RandGamma(alpha, beta, dtype, seed) {
|
|
this.alpha = alpha;
|
|
this.beta = 1 / beta; // convert rate to scale parameter
|
|
this.dtype = dtype;
|
|
var seedValue = seed ? seed : Math.random();
|
|
this.randu = seedrandom.alea(seedValue.toString());
|
|
this.randn = new MPRandGauss(0, 1, dtype, false, this.randu());
|
|
if (alpha < 1) {
|
|
this.d = alpha + (2 / 3);
|
|
}
|
|
else {
|
|
this.d = alpha - (1 / 3);
|
|
}
|
|
this.c = 1 / Math.sqrt(9 * this.d);
|
|
}
|
|
/** Returns next sample from a gamma distribution. */
|
|
RandGamma.prototype.nextValue = function () {
|
|
var x2, v0, v1, x, u, v;
|
|
while (true) {
|
|
do {
|
|
x = this.randn.nextValue();
|
|
v = 1 + (this.c * x);
|
|
} while (v <= 0);
|
|
v *= v * v;
|
|
x2 = x * x;
|
|
v0 = 1 - (0.331 * x2 * x2);
|
|
v1 = (0.5 * x2) + (this.d * (1 - v + Math.log(v)));
|
|
u = this.randu();
|
|
if (u < v0 || Math.log(u) < v1) {
|
|
break;
|
|
}
|
|
}
|
|
v = (1 / this.beta) * this.d * v;
|
|
if (this.alpha < 1) {
|
|
v *= Math.pow(this.randu(), 1 / this.alpha);
|
|
}
|
|
return this.convertValue(v);
|
|
};
|
|
/** Handles proper rounding for non-floating-point numbers. */
|
|
RandGamma.prototype.convertValue = function (value) {
|
|
if (this.dtype === 'float32') {
|
|
return value;
|
|
}
|
|
return Math.round(value);
|
|
};
|
|
return RandGamma;
|
|
}());
|
|
var UniformRandom = /** @class */ (function () {
|
|
function UniformRandom(min, max, dtype, seed) {
|
|
if (min === void 0) { min = 0; }
|
|
if (max === void 0) { max = 1; }
|
|
var _this = this;
|
|
/** Handles proper rounding for non floating point numbers. */
|
|
this.canReturnFloat = function () { return (_this.dtype == null || _this.dtype === 'float32'); };
|
|
this.min = min;
|
|
this.range = max - min;
|
|
this.dtype = dtype;
|
|
if (seed == null) {
|
|
seed = Math.random();
|
|
}
|
|
if (typeof seed === 'number') {
|
|
seed = seed.toString();
|
|
}
|
|
if (!this.canReturnFloat() && this.range <= 1) {
|
|
throw new Error("The difference between ".concat(min, " - ").concat(max, " <= 1 and dtype is not float"));
|
|
}
|
|
this.random = seedrandom.alea(seed);
|
|
}
|
|
UniformRandom.prototype.convertValue = function (value) {
|
|
if (this.canReturnFloat()) {
|
|
return value;
|
|
}
|
|
return Math.round(value);
|
|
};
|
|
UniformRandom.prototype.nextValue = function () {
|
|
return this.convertValue(this.min + this.range * this.random());
|
|
};
|
|
return UniformRandom;
|
|
}());
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a gamma distribution.
|
|
*
|
|
* ```js
|
|
* tf.randomGamma([2, 2], 1).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param alpha The shape parameter of the gamma distribution.
|
|
* @param beta The inverse scale parameter of the gamma distribution. Defaults
|
|
* to 1.
|
|
* @param dtype The data type of the output. Defaults to float32.
|
|
* @param seed The seed for the random number generator.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function randomGamma_(shape, alpha, beta, dtype, seed) {
|
|
if (beta === void 0) { beta = 1; }
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (beta == null) {
|
|
beta = 1;
|
|
}
|
|
if (dtype == null) {
|
|
dtype = 'float32';
|
|
}
|
|
if (dtype !== 'float32' && dtype !== 'int32') {
|
|
throw new Error("Unsupported data type ".concat(dtype));
|
|
}
|
|
var rgamma = new RandGamma(alpha, beta, dtype, seed);
|
|
var res = buffer(shape, dtype);
|
|
for (var i = 0; i < res.values.length; i++) {
|
|
res.values[i] = rgamma.nextValue();
|
|
}
|
|
return res.toTensor();
|
|
}
|
|
var randomGamma = /* @__PURE__ */ op({ randomGamma_: randomGamma_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a normal distribution.
|
|
*
|
|
* ```js
|
|
* tf.randomNormal([2, 2]).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param mean The mean of the normal distribution.
|
|
* @param stdDev The standard deviation of the normal distribution.
|
|
* @param dtype The data type of the output.
|
|
* @param seed The seed for the random number generator.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function randomNormal_(shape, mean, stdDev, dtype, seed) {
|
|
if (mean === void 0) { mean = 0; }
|
|
if (stdDev === void 0) { stdDev = 1; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (dtype != null && dtype === 'bool') {
|
|
throw new Error("Unsupported data type ".concat(dtype));
|
|
}
|
|
var randGauss = new MPRandGauss(mean, stdDev, dtype, false /* truncated */, seed);
|
|
var res = buffer(shape, dtype);
|
|
for (var i = 0; i < res.values.length; i++) {
|
|
res.values[i] = randGauss.nextValue();
|
|
}
|
|
return res.toTensor();
|
|
}
|
|
var randomNormal = /* @__PURE__ */ op({ randomNormal_: randomNormal_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a normal distribution.
|
|
*
|
|
* The generated values will have mean 0 and standard deviation 1.
|
|
*
|
|
* ```js
|
|
* tf.randomStandardNormal([2, 2]).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param dtype The data type of the output.
|
|
* @param seed The seed for the random number generator.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function randomStandardNormal_(shape, dtype, seed) {
|
|
if (dtype != null && dtype === 'bool') {
|
|
throw new Error("Unsupported data type ".concat(dtype));
|
|
}
|
|
return randomNormal(shape, 0, 1, dtype, seed);
|
|
}
|
|
var randomStandardNormal = /* @__PURE__ */ op({ randomStandardNormal_: randomStandardNormal_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a uniform distribution.
|
|
*
|
|
* The generated values follow a uniform distribution in the range [minval,
|
|
* maxval). The lower bound minval is included in the range, while the upper
|
|
* bound maxval is excluded.
|
|
*
|
|
* ```js
|
|
* tf.randomUniform([2, 2]).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param minval The lower bound on the range of random values to generate.
|
|
* Defaults to 0.
|
|
* @param maxval The upper bound on the range of random values to generate.
|
|
* Defaults to 1.
|
|
* @param dtype The data type of the output tensor. Defaults to 'float32'.
|
|
* @param seed An optional int. Defaults to 0. If seed is set to be non-zero,
|
|
* the random number generator is seeded by the given seed. Otherwise, it is
|
|
* seeded by a random seed.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function randomUniform_(shape, minval, maxval, dtype, seed) {
|
|
if (minval === void 0) { minval = 0; }
|
|
if (maxval === void 0) { maxval = 1; }
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
var res = buffer(shape, dtype);
|
|
var random = new UniformRandom(minval, maxval, null, seed);
|
|
for (var i = 0; i < res.values.length; i++) {
|
|
res.values[i] = random.nextValue();
|
|
}
|
|
return res.toTensor();
|
|
}
|
|
var randomUniform = /* @__PURE__ */ op({ randomUniform_: randomUniform_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates a `tf.Tensor` with integers sampled from a uniform distribution.
|
|
*
|
|
* The generated values are uniform integers in the range [minval, maxval). The
|
|
* lower bound minval is included in the range, while the upper bound maxval is
|
|
* excluded.
|
|
*
|
|
* ```js
|
|
* tf.randomUniformInt([2, 2], 0, 10).print();
|
|
* ```
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param minval Inclusive lower bound on the generated integers.
|
|
* @param maxval Exclusive upper bound on the generated integers.
|
|
* @param seed An optional int. Defaults to 0. If seed is set to be non-zero,
|
|
* the random number generator is seeded by the given seed. Otherwise, it is
|
|
* seeded by a random seed.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Random'}
|
|
*/
|
|
function randomUniformInt_(shape, minval, maxval, seed) {
|
|
// TODO(mattsoulanille): Handle optional seed2 input.
|
|
return randomUniform(shape, minval, maxval, 'int32', seed);
|
|
}
|
|
var randomUniformInt = /* @__PURE__ */ op({ randomUniformInt_: randomUniformInt_ });
|
|
|
|
/**
|
|
* @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 new `tf.Tensor1D` filled with the numbers in the range provided.
|
|
*
|
|
* The tensor is a half-open interval meaning it includes start, but
|
|
* excludes stop. Decrementing ranges and negative step values are also
|
|
* supported.
|
|
*
|
|
*
|
|
* ```js
|
|
* tf.range(0, 9, 2).print();
|
|
* ```
|
|
*
|
|
* @param start An integer start value
|
|
* @param stop An integer stop value
|
|
* @param step An integer increment (will default to 1 or -1)
|
|
* @param dtype The data type of the output tensor. Defaults to 'float32'.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function range(start, stop, step, dtype) {
|
|
if (step === void 0) { step = 1; }
|
|
if (dtype === void 0) { dtype = 'float32'; }
|
|
if (step === 0) {
|
|
throw new Error('Cannot have a step of zero');
|
|
}
|
|
var attrs = { start: start, stop: stop, step: step, dtype: dtype };
|
|
return ENGINE.runKernel(Range, {} /* inputs */, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
var $input = convertToTensor(input, 'input', 'real');
|
|
var inputs = { input: $input };
|
|
return ENGINE.runKernel(Real, inputs);
|
|
}
|
|
var real = /* @__PURE__ */ op({ real_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes reciprocal of x element-wise: `1 / x`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, 1, 2]);
|
|
*
|
|
* x.reciprocal().print(); // or tf.reciprocal(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function reciprocal_(x) {
|
|
var $x = convertToTensor(x, 'x', 'reciprocal');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Reciprocal, inputs);
|
|
}
|
|
var reciprocal = /* @__PURE__ */ op({ reciprocal_: reciprocal_ });
|
|
|
|
/**
|
|
* @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 rectified linear element-wise: `max(x, 0)`.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
*
|
|
* x.relu().print(); // or tf.relu(x)
|
|
* ```
|
|
* @param x The input tensor. If the dtype is `bool`, the output dtype will be
|
|
* `int32`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function relu_(x) {
|
|
var $x = convertToTensor(x, 'x', 'relu');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Relu, inputs);
|
|
}
|
|
var relu = /* @__PURE__ */ op({ relu_: relu_ });
|
|
|
|
/**
|
|
* @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 rectified linear 6 element-wise: `min(max(x, 0), 6)`.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 8]);
|
|
*
|
|
* x.relu6().print(); // or tf.relu6(x)
|
|
* ```
|
|
* @param x The input tensor. If the dtype is `bool`, the output dtype will be
|
|
* `int32`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function relu6_(x) {
|
|
var $x = convertToTensor(x, 'x', 'relu6');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Relu6, inputs);
|
|
}
|
|
var relu6 = /* @__PURE__ */ op({ relu6_: relu6_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'reverse');
|
|
var inputs = { x: $x };
|
|
var attrs = { dims: axis };
|
|
return ENGINE.runKernel(Reverse, inputs, attrs);
|
|
}
|
|
var reverse = /* @__PURE__ */ op({ reverse_: reverse_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Reverses a `tf.Tensor1D`.
|
|
*
|
|
* @param x The input tensor.
|
|
*/
|
|
function reverse1d_(x) {
|
|
var $x = convertToTensor(x, 'x', 'reverse');
|
|
assert($x.rank === 1, function () { return "Error in reverse1D: x must be rank 1 but got rank ".concat($x.rank, "."); });
|
|
return reverse($x, 0);
|
|
}
|
|
var reverse1d = /* @__PURE__ */ op({ reverse1d_: reverse1d_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Reverses a `tf.Tensor2D` along a specified axis.
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The set of dimensions to reverse. Must be in the
|
|
* range [-rank(x), rank(x)). Defaults to all axes.
|
|
*/
|
|
function reverse2d_(x, axis) {
|
|
var $x = convertToTensor(x, 'x', 'reverse');
|
|
assert($x.rank === 2, function () { return "Error in reverse2D: x must be rank 2 but got rank ".concat($x.rank, "."); });
|
|
return reverse($x, axis);
|
|
}
|
|
var reverse2d = /* @__PURE__ */ op({ reverse2d_: reverse2d_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Reverses a `tf.Tensor3D` along a specified axis.
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The set of dimensions to reverse. Must be in the
|
|
* range [-rank(x), rank(x)). Defaults to all axes.
|
|
*/
|
|
function reverse3d_(x, axis) {
|
|
var $x = convertToTensor(x, 'x', 'reverse');
|
|
assert($x.rank === 3, function () { return "Error in reverse3D: x must be rank 3 but got rank ".concat($x.rank, "."); });
|
|
return reverse($x, axis);
|
|
}
|
|
var reverse3d = /* @__PURE__ */ op({ reverse3d_: reverse3d_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Reverses a `tf.Tensor4D` along a specified axis.
|
|
*
|
|
* @param x The input tensor.
|
|
* @param axis The set of dimensions to reverse. Must be in the
|
|
* range [-rank(x), rank(x)). Defaults to all axes.
|
|
*/
|
|
function reverse4d_(x, axis) {
|
|
var $x = convertToTensor(x, 'x', 'reverse');
|
|
assert($x.rank === 4, function () { return "Error in reverse4D: x must be rank 4 but got rank ".concat($x.rank, "."); });
|
|
return reverse($x, axis);
|
|
}
|
|
var reverse4d = /* @__PURE__ */ op({ reverse4d_: reverse4d_ });
|
|
|
|
/**
|
|
* @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 round of input `tf.Tensor` element-wise: `round(x)`.
|
|
* It implements banker's rounding.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([.6, 1.1, -3.3]);
|
|
*
|
|
* x.round().print(); // or tf.round(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function round_(x) {
|
|
var $x = convertToTensor(x, 'x', 'round');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Round, inputs);
|
|
}
|
|
var round = /* @__PURE__ */ op({ round_: round_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'rsqrt', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Rsqrt, inputs);
|
|
}
|
|
var rsqrt = /* @__PURE__ */ op({ rsqrt_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes scaled exponential linear element-wise.
|
|
*
|
|
* `x < 0 ? scale * alpha * (exp(x) - 1) : scale * x`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([-1, 2, -3, 4]);
|
|
*
|
|
* x.selu().print(); // or tf.selu(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function selu_(x) {
|
|
var $x = convertToTensor(x, 'x', 'selu');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Selu, inputs);
|
|
}
|
|
var selu = /* @__PURE__ */ op({ selu_: selu_ });
|
|
|
|
/**
|
|
* 2-D convolution with separable filters.
|
|
*
|
|
* Performs a depthwise convolution that acts separately on channels followed
|
|
* by a pointwise convolution that mixes channels. Note that this is
|
|
* separability between dimensions [1, 2] and 3, not spatial separability
|
|
* between dimensions 1 and 2.
|
|
*
|
|
* See
|
|
* [https://www.tensorflow.org/api_docs/python/tf/nn/separable_conv2d](
|
|
* https://www.tensorflow.org/api_docs/python/tf/nn/separable_conv2d)
|
|
* for more details.
|
|
*
|
|
* @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 depthwiseFilter The depthwise filter tensor, rank 4, of shape
|
|
* `[filterHeight, filterWidth, inChannels, channelMultiplier]`. This is
|
|
* the filter used in the first step.
|
|
* @param pointwiseFilter The pointwise filter tensor, rank 4, of shape
|
|
* `[1, 1, inChannels * channelMultiplier, outChannels]`. This is
|
|
* the filter used in the second step.
|
|
* @param strides The strides of the convolution: `[strideHeight,
|
|
* strideWidth]`. If strides is a single number, then `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 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 `rate` is a single
|
|
* number, then `dilationHeight == dilationWidth`. If it is greater than
|
|
* 1, then all values of `strides` must be 1.
|
|
* @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]. Only "NHWC" is currently supported.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Convolution'}
|
|
*/
|
|
function separableConv2d_(x, depthwiseFilter, pointwiseFilter, strides, pad, dilation, dataFormat) {
|
|
if (dilation === void 0) { dilation = [1, 1]; }
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
var $x = convertToTensor(x, 'x', 'separableConv2d');
|
|
var $depthwiseFilter = convertToTensor(depthwiseFilter, 'depthwiseFilter', 'separableConv2d');
|
|
var $pointwiseFilter = convertToTensor(pointwiseFilter, 'pointwiseFilter', 'separableConv2d');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
if (dataFormat === 'NCHW') {
|
|
throw new Error('separableConv2d currently does not support dataFormat NCHW; only ' +
|
|
'NHWC is supported');
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in separableConv2d: input must be rank 4, but got " +
|
|
"rank ".concat(x4D.rank, "."); });
|
|
assert($depthwiseFilter.rank === 4, function () { return "Error in separableConv2d: depthwise filter must be rank 4, but " +
|
|
"got rank ".concat($depthwiseFilter.rank, "."); });
|
|
assert($pointwiseFilter.rank === 4, function () { return "Error in separableConv2d: pointwise filter must be rank 4, but " +
|
|
"got rank ".concat($depthwiseFilter.rank, "."); });
|
|
assert($pointwiseFilter.shape[0] === 1, function () { return "Error in separableConv2d: the first dimension of pointwise filter " +
|
|
" must be 1, but got ".concat($pointwiseFilter.shape[0], "."); });
|
|
assert($pointwiseFilter.shape[1] === 1, function () { return "Error in separableConv2d: the second dimension of pointwise " +
|
|
"filter must be 1, but got ".concat($pointwiseFilter.shape[1], "."); });
|
|
var inChannels = $depthwiseFilter.shape[2];
|
|
var channelMultiplier = $depthwiseFilter.shape[3];
|
|
assert($pointwiseFilter.shape[2] === inChannels * channelMultiplier, function () { return "Error in separableConv2d: the third dimension of pointwise filter " +
|
|
"must be ".concat(inChannels * channelMultiplier, ", ") +
|
|
"but got ".concat($pointwiseFilter.shape[2], "."); });
|
|
var depthwise = depthwiseConv2d$1(x4D, $depthwiseFilter, strides, pad, dataFormat, dilation);
|
|
var pointwiseStride = 1;
|
|
var res = conv2d$1(depthwise, $pointwiseFilter, pointwiseStride, 'valid', dataFormat);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var separableConv2d = /* @__PURE__ */ op({ separableConv2d_: separableConv2d_ });
|
|
|
|
/**
|
|
* Computes the difference between two lists of numbers.
|
|
*
|
|
* Given a Tensor `x` and a Tensor `y`, this operation returns a Tensor `out`
|
|
* that represents all values that are in `x` but not in `y`. The returned
|
|
* Tensor `out` is sorted in the same order that the numbers appear in `x`
|
|
* (duplicates are preserved). This operation also returns a Tensor indices that
|
|
* represents the position of each out element in `x`. In other words:
|
|
*
|
|
* `out[i] = x[idx[i]] for i in [0, 1, ..., out.length - 1]`
|
|
*
|
|
* ```js
|
|
* const x = [1, 2, 3, 4, 5, 6];
|
|
* const y = [1, 3, 5];
|
|
*
|
|
* const [out, indices] = await tf.setdiff1dAsync(x, y);
|
|
* out.print(); // [2, 4, 6]
|
|
* indices.print(); // [1, 3, 5]
|
|
* ```
|
|
*
|
|
* @param x 1-D Tensor. Values to keep.
|
|
* @param y 1-D Tensor. Must have the same type as x. Values to exclude in the
|
|
* output.
|
|
* @returns Promise of Tensor tuple [out, indices].
|
|
* out: Tensor with the same type as x.
|
|
* indices: A Tensor of type int32.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Transformations'}
|
|
*/
|
|
function setdiff1dAsync_(x, y) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $x, $y, xVals, yVals, ySet, outputSize, i, buffer, indices, i, p;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
$x = convertToTensor(x, 'x', 'setdiff1d');
|
|
$y = convertToTensor(y, 'y', 'setdiff1d');
|
|
assert($x.dtype === $y.dtype, function () { return "x and y should have the same dtype, but got x (".concat($x.dtype, ") and y (").concat($y.dtype, ")."); });
|
|
assert($x.rank === 1, function () { return "x should be 1D tensor, but got x (".concat($x.shape, ")."); });
|
|
assert($y.rank === 1, function () { return "y should be 1D tensor, but got y (".concat($y.shape, ")."); });
|
|
return [4 /*yield*/, $x.data()];
|
|
case 1:
|
|
xVals = _a.sent();
|
|
return [4 /*yield*/, $y.data()];
|
|
case 2:
|
|
yVals = _a.sent();
|
|
ySet = new Set(yVals);
|
|
outputSize = 0;
|
|
for (i = 0; i < xVals.length; i++) {
|
|
if (!ySet.has(xVals[i])) {
|
|
outputSize++;
|
|
}
|
|
}
|
|
buffer = new TensorBuffer([outputSize], $x.dtype);
|
|
indices = new TensorBuffer([outputSize], 'int32');
|
|
for (i = 0, p = 0; i < xVals.length; i++) {
|
|
if (!ySet.has(xVals[i])) {
|
|
buffer.values[p] = xVals[i];
|
|
indices.values[p] = i;
|
|
p++;
|
|
}
|
|
}
|
|
return [2 /*return*/, [buffer.toTensor(), indices.toTensor()]];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var setdiff1dAsync = setdiff1dAsync_;
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Returns an element-wise indication of the sign of a number.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([.6, 1.1, -3.3, NaN, 0]);
|
|
*
|
|
* x.sign().print(); // or tf.sign(x)
|
|
* ```
|
|
* @param x The input Tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function sign_(x) {
|
|
var $x = convertToTensor(x, 'x', 'sign');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Sign, inputs);
|
|
}
|
|
var sign = /* @__PURE__ */ op({ sign_: sign_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'sin', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Sin, inputs);
|
|
}
|
|
var sin = /* @__PURE__ */ op({ sin_: sin_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'sinh');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Sinh, inputs);
|
|
}
|
|
var sinh = /* @__PURE__ */ op({ sinh_: sinh_ });
|
|
|
|
/**
|
|
* @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 1D slice from 1D array starting at coordinates `begin` and is
|
|
* of length `size`. See `slice` for details.
|
|
*/
|
|
function slice1d_(x, begin, size) {
|
|
var $x = convertToTensor(x, 'x', 'slice1d');
|
|
assert($x.rank === 1, function () { return "slice1d expects a rank-1 tensor, but got a rank-".concat($x.rank, " tensor"); });
|
|
return slice($x, [begin], [size]);
|
|
}
|
|
var slice1d = /* @__PURE__ */ op({ slice1d_: slice1d_ });
|
|
|
|
/**
|
|
* @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 2D slice from a 2D array starting at coordinates `begin` and
|
|
* is of size `size`. See `slice` for details.
|
|
*/
|
|
function slice2d_(x, begin, size) {
|
|
var $x = convertToTensor(x, 'x', 'slice2d');
|
|
assert($x.rank === 2, function () { return "slice2d expects a rank-2 tensor, but got a rank-".concat($x.rank, " tensor"); });
|
|
return slice($x, begin, size);
|
|
}
|
|
var slice2d = /* @__PURE__ */ op({ slice2d_: slice2d_ });
|
|
|
|
/**
|
|
* @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 3D slice from a 3D array starting at coordinates `begin` and
|
|
* is of size `size`. See `slice` for details.
|
|
*/
|
|
function slice3d_(x, begin, size) {
|
|
var $x = convertToTensor(x, 'x', 'slice3d');
|
|
assert($x.rank === 3, function () { return "slice3d expects a rank-3 tensor, but got a rank-".concat($x.rank, " tensor"); });
|
|
return slice($x, begin, size);
|
|
}
|
|
var slice3d = /* @__PURE__ */ op({ slice3d_: slice3d_ });
|
|
|
|
/**
|
|
* @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 4D slice from a 4D array starting at coordinates `begin` and
|
|
* is of size `size`. See `slice` for details.
|
|
*/
|
|
function slice4d_(x, begin, size) {
|
|
var $x = convertToTensor(x, 'x', 'slice4d');
|
|
assert($x.rank === 4, function () { return "slice4d expects a rank-4 tensor, but got a rank-".concat($x.rank, " tensor"); });
|
|
return slice($x, begin, size);
|
|
}
|
|
var slice4d = /* @__PURE__ */ op({ slice4d_: slice4d_ });
|
|
|
|
/**
|
|
* @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 softmax normalized vector given the logits.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* a.softmax().print(); // or tf.softmax(a)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor2d([2, 4, 6, 1, 2, 3], [2, 3]);
|
|
*
|
|
* a.softmax().print(); // or tf.softmax(a)
|
|
* ```
|
|
*
|
|
* @param logits The logits array.
|
|
* @param dim The dimension softmax would be performed on. Defaults to `-1`
|
|
* which indicates the last dimension.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function softmax_(logits, dim) {
|
|
if (dim === void 0) { dim = -1; }
|
|
var $logits = convertToTensor(logits, 'logits', 'softmax', 'float32');
|
|
if (dim === -1) {
|
|
dim = $logits.rank - 1;
|
|
}
|
|
if (dim !== $logits.rank - 1) {
|
|
throw Error('Softmax along a non-last dimension is not yet supported. ' +
|
|
"Logits was rank ".concat($logits.rank, " and dim was ").concat(dim));
|
|
}
|
|
var inputs = { logits: $logits };
|
|
var attrs = { dim: dim };
|
|
return ENGINE.runKernel(Softmax, inputs, attrs);
|
|
}
|
|
var softmax = /* @__PURE__ */ op({ softmax_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Fast Fourier transform.
|
|
*
|
|
* Computes the 1-dimensional discrete Fourier transform over the inner-most
|
|
* dimension of input.
|
|
*
|
|
* ```js
|
|
* const real = tf.tensor1d([1, 2, 3]);
|
|
* const imag = tf.tensor1d([1, 2, 3]);
|
|
* const x = tf.complex(real, imag);
|
|
*
|
|
* x.fft().print(); // tf.spectral.fft(x).print();
|
|
* ```
|
|
* @param input The complex input to compute an fft over.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
|
|
*/
|
|
function fft_(input) {
|
|
assert(input.dtype === 'complex64', function () { return "The dtype for tf.spectral.fft() must be complex64 " +
|
|
"but got ".concat(input.dtype, "."); });
|
|
var inputs = { input: input };
|
|
return ENGINE.runKernel(FFT, inputs);
|
|
}
|
|
var fft = /* @__PURE__ */ op({ fft_: fft_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Inverse fast Fourier transform.
|
|
*
|
|
* Computes the inverse 1-dimensional discrete Fourier transform over the
|
|
* inner-most dimension of input.
|
|
*
|
|
* ```js
|
|
* const real = tf.tensor1d([1, 2, 3]);
|
|
* const imag = tf.tensor1d([1, 2, 3]);
|
|
* const x = tf.complex(real, imag);
|
|
*
|
|
* x.ifft().print(); // tf.spectral.ifft(x).print();
|
|
* ```
|
|
* @param input The complex input to compute an ifft over.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
|
|
*/
|
|
function ifft_(input) {
|
|
assert(input.dtype === 'complex64', function () { return "The dtype for tf.spectral.ifft() must be complex64 " +
|
|
"but got ".concat(input.dtype, "."); });
|
|
var inputs = { input: input };
|
|
return ENGINE.runKernel(IFFT, inputs);
|
|
}
|
|
var ifft = /* @__PURE__ */ op({ ifft_: ifft_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Inversed real value input fast Fourier transform.
|
|
*
|
|
* Computes the 1-dimensional inversed discrete Fourier transform over the
|
|
* inner-most dimension of the real input.
|
|
*
|
|
* ```js
|
|
* const real = tf.tensor1d([1, 2, 3]);
|
|
* const imag = tf.tensor1d([0, 0, 0]);
|
|
* const x = tf.complex(real, imag);
|
|
*
|
|
* x.irfft().print();
|
|
* ```
|
|
* @param input The real value input to compute an irfft over.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
|
|
*/
|
|
function irfft_(input) {
|
|
var innerDimensionSize = input.shape[input.shape.length - 1];
|
|
var batch = input.size / innerDimensionSize;
|
|
var ret;
|
|
if (innerDimensionSize <= 2) {
|
|
var complexInput = reshape(input, [batch, innerDimensionSize]);
|
|
ret = ifft(complexInput);
|
|
}
|
|
else {
|
|
// The length of unique components of the DFT of a real-valued signal
|
|
// is 2 * (input_len - 1)
|
|
var outputShape = [batch, 2 * (innerDimensionSize - 1)];
|
|
var realInput = reshape(real(input), [batch, innerDimensionSize]);
|
|
var imagInput = reshape(imag(input), [batch, innerDimensionSize]);
|
|
var realConjugate = reverse(slice(realInput, [0, 1], [batch, innerDimensionSize - 2]), 1);
|
|
var imagConjugate = mul(reverse(slice(imagInput, [0, 1], [batch, innerDimensionSize - 2]), 1), scalar(-1));
|
|
var r = concat([realInput, realConjugate], 1);
|
|
var i = concat([imagInput, imagConjugate], 1);
|
|
var complexInput = reshape(complex(r, i), [outputShape[0], outputShape[1]]);
|
|
ret = ifft(complexInput);
|
|
}
|
|
ret = real(ret);
|
|
// reshape the result if the input is 3D tensor.
|
|
if (input.rank === 3 && input.shape[0] !== 0) {
|
|
var temp = ret;
|
|
var batch_1 = input.shape[0];
|
|
ret = reshape(ret, [batch_1, ret.shape[0] / batch_1, ret.shape[1]]);
|
|
temp.dispose();
|
|
}
|
|
return ret;
|
|
}
|
|
var irfft = /* @__PURE__ */ op({ irfft_: irfft_ });
|
|
|
|
/**
|
|
* @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) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'split');
|
|
var inputs = { x: $x };
|
|
var attr = { numOrSizeSplits: numOrSizeSplits, axis: axis };
|
|
return ENGINE.runKernel(SplitV, inputs, attr);
|
|
}
|
|
var split$1 = /* @__PURE__ */ op({ split_: split_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Real value input fast Fourier transform.
|
|
*
|
|
* Computes the 1-dimensional discrete Fourier transform over the
|
|
* inner-most dimension of the real input.
|
|
*
|
|
* ```js
|
|
* const real = tf.tensor1d([1, 2, 3]);
|
|
*
|
|
* real.rfft().print();
|
|
* ```
|
|
* @param input The real value input to compute an rfft over.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
|
|
*/
|
|
function rfft_(input, fftLength) {
|
|
assert(input.dtype === 'float32', function () { return "The dtype for rfft() must be real value but got ".concat(input.dtype); });
|
|
var innerDimensionSize = input.shape[input.shape.length - 1];
|
|
var batch = input.size / innerDimensionSize;
|
|
var adjustedInput;
|
|
if (fftLength != null && fftLength < innerDimensionSize) {
|
|
// Need to crop
|
|
var begin = input.shape.map(function (v) { return 0; });
|
|
var size = input.shape.map(function (v) { return v; });
|
|
size[input.shape.length - 1] = fftLength;
|
|
adjustedInput = slice(input, begin, size);
|
|
innerDimensionSize = fftLength;
|
|
}
|
|
else if (fftLength != null && fftLength > innerDimensionSize) {
|
|
// Need to pad with zeros
|
|
var zerosShape = input.shape.map(function (v) { return v; });
|
|
zerosShape[input.shape.length - 1] = fftLength - innerDimensionSize;
|
|
adjustedInput = concat([input, zeros(zerosShape)], input.shape.length - 1);
|
|
innerDimensionSize = fftLength;
|
|
}
|
|
else {
|
|
adjustedInput = input;
|
|
}
|
|
// Complement the input with zero imaginary numbers.
|
|
var zerosInput = zerosLike(adjustedInput);
|
|
var complexInput = reshape(complex(adjustedInput, zerosInput), [batch, innerDimensionSize]);
|
|
var ret = fft(complexInput);
|
|
// Exclude complex conjugations. These conjugations are put symmetrically.
|
|
var half = Math.floor(innerDimensionSize / 2) + 1;
|
|
var realValues = real(ret);
|
|
var imagValues = imag(ret);
|
|
var realComplexConjugate = split$1(realValues, [half, innerDimensionSize - half], realValues.shape.length - 1);
|
|
var imagComplexConjugate = split$1(imagValues, [half, innerDimensionSize - half], imagValues.shape.length - 1);
|
|
var outputShape = adjustedInput.shape.slice();
|
|
outputShape[adjustedInput.shape.length - 1] = half;
|
|
return reshape(complex(realComplexConjugate[0], imagComplexConjugate[0]), outputShape);
|
|
}
|
|
var rfft = /* @__PURE__ */ op({ rfft_: rfft_ });
|
|
|
|
/**
|
|
* Returns (a - b) * (a - b) element-wise.
|
|
* Supports broadcasting.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor1d([1, 4, 3, 16]);
|
|
* const b = tf.tensor1d([1, 2, 9, 4]);
|
|
*
|
|
* a.squaredDifference(b).print(); // or tf.squaredDifference(a, b)
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Broadcast squared difference a with b.
|
|
* const a = tf.tensor1d([2, 4, 6, 8]);
|
|
* const b = tf.scalar(5);
|
|
*
|
|
* a.squaredDifference(b).print(); // or tf.squaredDifference(a, b)
|
|
* ```
|
|
*
|
|
* @param a The first tensor.
|
|
* @param b The second tensor. Must have the same type as `a`.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Arithmetic'}
|
|
*/
|
|
function squaredDifference_(a, b) {
|
|
var _a;
|
|
var $a = convertToTensor(a, 'a', 'squaredDifference');
|
|
var $b = convertToTensor(b, 'b', 'squaredDifference');
|
|
_a = __read(makeTypesMatch($a, $b), 2), $a = _a[0], $b = _a[1];
|
|
assertAndGetBroadcastShape($a.shape, $b.shape);
|
|
var inputs = { a: $a, b: $b };
|
|
var attrs = {};
|
|
return ENGINE.runKernel(SquaredDifference, inputs, attrs);
|
|
}
|
|
var squaredDifference = /* @__PURE__ */ op({ squaredDifference_: squaredDifference_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Removes dimensions of size 1 from the shape of a `tf.Tensor`.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor([1, 2, 3, 4], [1, 1, 4]);
|
|
* x.squeeze().print();
|
|
* ```
|
|
*
|
|
* @param x The input tensor to be squeezed.
|
|
* @param axis An optional list of numbers. If specified, only
|
|
* squeezes the dimensions listed. The dimension index starts at 0. It
|
|
* is an error to squeeze a dimension that is not 1.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Transformations'}
|
|
*/
|
|
function squeeze_(x, axis) {
|
|
var $x = convertToTensor(x, 'x', 'squeeze', 'string_or_numeric');
|
|
return reshape($x, squeezeShape($x.shape, axis).newShape);
|
|
}
|
|
var squeeze = /* @__PURE__ */ op({ squeeze_: squeeze_ });
|
|
|
|
/**
|
|
* @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) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $tensors = convertToTensorArray(tensors, 'tensors', 'stack', 'string_or_numeric');
|
|
assert($tensors.length >= 1, function () { return 'Pass at least one tensor to tf.stack'; });
|
|
if ($tensors.length > 0) {
|
|
assert(axis <= $tensors[0].rank, function () { return 'Axis must be <= rank of the tensor'; });
|
|
}
|
|
var inputs = $tensors;
|
|
var attrs = { axis: axis };
|
|
return ENGINE.runKernel(Pack, inputs, attrs);
|
|
}
|
|
var stack = /* @__PURE__ */ op({ stack_: stack_ });
|
|
|
|
/**
|
|
* @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) {
|
|
if (alpha === void 0) { alpha = 0.0; }
|
|
var $x = convertToTensor(x, 'x', 'step');
|
|
var inputs = { x: $x };
|
|
var attrs = { alpha: alpha };
|
|
return ENGINE.runKernel(Step, inputs, attrs);
|
|
}
|
|
var step = /* @__PURE__ */ op({ step_: step_ });
|
|
|
|
/**
|
|
* @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 strided slice of a tensor.
|
|
*
|
|
* Roughly speaking, this op extracts a slice of size (end-begin)/stride from
|
|
* the given input tensor (x). Starting at the location specified by begin the
|
|
* slice continues by adding stride to the index until all dimensions are not
|
|
* less than end. Note that a stride can be negative, which causes a reverse
|
|
* slice.
|
|
*
|
|
* ```js
|
|
* const t = tf.tensor3d([1, 1, 1 ,2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],
|
|
* [3, 2, 3]);
|
|
* t.stridedSlice([1, 0, 0], [2, 1, 3], [1, 1, 1]).print() // [[[3, 3, 3]]]
|
|
* t.stridedSlice([1, 0, 0], [2, 2, 3], [1, 1, 1]).print() // [[[3, 3, 3],
|
|
* // [4, 4, 4]]]
|
|
* t.stridedSlice([1, -1, 0], [2, -3, 3], [1, -1, 1]).print() // [[[4, 4, 4],
|
|
* // [3, 3, 3]]]
|
|
* ```
|
|
*
|
|
* @param x The tensor to stride slice.
|
|
* @param begin The coordinates to start the slice from.
|
|
* @param end: The coordinates to end the slice at.
|
|
* @param strides: The size of the slice.
|
|
* @param beginMask: If the ith bit of beginMask is set, begin[i] is ignored
|
|
* and the fullest possible range in that dimension is used instead.
|
|
* @param endMask: If the ith bit of endMask is set, end[i] is ignored
|
|
* and the fullest possible range in that dimension is used instead.
|
|
* @param shrinkAxisMask: a bitmask where bit i implies that
|
|
* the ith specification should shrink the dimensionality. begin and end must
|
|
* imply a slice of size 1 in the dimension.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Slicing and Joining'}
|
|
*/
|
|
function stridedSlice_(x, begin, end, strides, beginMask, endMask, ellipsisMask, newAxisMask, shrinkAxisMask) {
|
|
if (beginMask === void 0) { beginMask = 0; }
|
|
if (endMask === void 0) { endMask = 0; }
|
|
if (ellipsisMask === void 0) { ellipsisMask = 0; }
|
|
if (newAxisMask === void 0) { newAxisMask = 0; }
|
|
if (shrinkAxisMask === void 0) { shrinkAxisMask = 0; }
|
|
var $x = convertToTensor(x, 'x', 'stridedSlice', 'string_or_numeric');
|
|
var inputs = { x: $x };
|
|
var attrs = {
|
|
begin: begin,
|
|
end: end,
|
|
strides: strides,
|
|
beginMask: beginMask,
|
|
endMask: endMask,
|
|
ellipsisMask: ellipsisMask,
|
|
newAxisMask: newAxisMask,
|
|
shrinkAxisMask: shrinkAxisMask
|
|
};
|
|
return ENGINE.runKernel(StridedSlice, inputs, attrs);
|
|
}
|
|
var stridedSlice = /* @__PURE__ */ op({ stridedSlice_: stridedSlice_ });
|
|
|
|
/**
|
|
* @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 tan of the input `tf.Tensor` element-wise, `tan(x)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([0, Math.PI / 2, Math.PI * 3 / 4]);
|
|
*
|
|
* x.tan().print(); // or tf.tan(x)
|
|
* ```
|
|
* @param x The input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Basic math'}
|
|
*/
|
|
function tan_(x) {
|
|
var $x = convertToTensor(x, 'x', 'tan', 'float32');
|
|
var inputs = { x: $x };
|
|
return ENGINE.runKernel(Tan, inputs);
|
|
}
|
|
var tan = /* @__PURE__ */ op({ tan_: tan_ });
|
|
|
|
/**
|
|
* @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 the provided values, shape and dtype.
|
|
*
|
|
* ```js
|
|
* // Pass an array of values to create a vector.
|
|
* tf.tensor([1, 2, 3, 4]).print();
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Pass a nested array of values to make a matrix or a higher
|
|
* // dimensional tensor.
|
|
* tf.tensor([[1, 2], [3, 4]]).print();
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Pass a flat array and specify a shape yourself.
|
|
* tf.tensor([1, 2, 3, 4], [2, 2]).print();
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Pass a `WebGLData` object and specify a shape yourself.
|
|
*
|
|
* // This makes it possible for TF.js applications to avoid GPU / CPU sync.
|
|
* // For example, if your application includes a preprocessing step on the GPU,
|
|
* // you could upload the GPU output directly to TF.js, rather than first
|
|
* // downloading the values.
|
|
*
|
|
* // Example for WebGL2:
|
|
* if (tf.findBackend('custom-webgl') == null) {
|
|
* const customCanvas = document.createElement('canvas');
|
|
* const customBackend = new tf.MathBackendWebGL(customCanvas);
|
|
* tf.registerBackend('custom-webgl', () => customBackend);
|
|
* }
|
|
* const savedBackend = tf.getBackend();
|
|
* await tf.setBackend('custom-webgl');
|
|
* const gl = tf.backend().gpgpu.gl;
|
|
* const texture = gl.createTexture();
|
|
* const tex2d = gl.TEXTURE_2D;
|
|
* const width = 2;
|
|
* const height = 2;
|
|
*
|
|
* gl.bindTexture(tex2d, texture);
|
|
* gl.texParameteri(tex2d, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
* gl.texParameteri(tex2d, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
* gl.texParameteri(tex2d, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
* gl.texParameteri(tex2d, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
* gl.texImage2D(
|
|
* tex2d, 0, gl.RGBA32F, // internalFormat
|
|
* width, height, 0,
|
|
* gl.RGBA, // textureFormat
|
|
* gl.FLOAT, // textureType
|
|
* new Float32Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
* );
|
|
*
|
|
* // Currently, the `texture` has 4 pixels:
|
|
* // Pixel0 is {R:0, G:1, B:2, A:3}
|
|
* // Pixel1 is {R:4, G:5, B:6, A:7}
|
|
* // Pixel2 is {R:8, G:9, B:10, A:11}
|
|
* // Pixel3 is {R:12, G:13, B:14, A:15}
|
|
*
|
|
* const logicalShape = [height * width * 2];
|
|
* const a = tf.tensor({texture, height, width, channels: 'BR'}, logicalShape);
|
|
* a.print();
|
|
* // Tensor value will be [2, 0, 6, 4, 10, 8, 14, 12], since [2, 0] is the
|
|
* // values of 'B' and 'R' channels of Pixel0, [6, 4] is the values of 'B' and
|
|
* 'R'
|
|
* // channels of Pixel1...
|
|
*
|
|
* // For postprocessing on the GPU, it's possible to retrieve the texture
|
|
* // backing any tensor by calling the tensor's `dataToGPU` method like
|
|
* // so:
|
|
*
|
|
* const tex = a.dataToGPU();
|
|
* await tf.setBackend(savedBackend);
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // Pass a `WebGPUData` object and specify a shape yourself.
|
|
*
|
|
* // This makes it possible for TF.js applications to avoid GPU / CPU sync.
|
|
* // For example, if your application includes a preprocessing step on the GPU,
|
|
* // you could upload the GPU output directly to TF.js, rather than first
|
|
* // downloading the values. Unlike WebGL, this optionally supports zero copy
|
|
* // by WebGPUData.zeroCopy. When zeroCopy is false or undefined(default), this
|
|
* // passing GPUBuffer can be destroyed after tensor is created. When zeroCopy
|
|
* // is true, this GPUBuffer is bound directly by the tensor, so do not destroy
|
|
* // this GPUBuffer until all access is done.
|
|
*
|
|
* // Example for WebGPU:
|
|
* function createGPUBufferFromData(device, data, dtype) {
|
|
* const bytesPerElement = 4;
|
|
* const sizeInBytes = data.length * bytesPerElement;
|
|
*
|
|
* const gpuWriteBuffer = device.createBuffer({
|
|
* mappedAtCreation: true,
|
|
* size: sizeInBytes,
|
|
* usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC
|
|
* });
|
|
* const arrayBuffer = gpuWriteBuffer.getMappedRange();
|
|
* if (dtype === 'float32') {
|
|
* new Float32Array(arrayBuffer).set(data);
|
|
* } else if (dtype === 'int32') {
|
|
* new Int32Array(arrayBuffer).set(data);
|
|
* } else {
|
|
* throw new Error(
|
|
* `Creating tensor from GPUBuffer only supports` +
|
|
* `'float32'|'int32' dtype, while the dtype is ${dtype}.`);
|
|
* }
|
|
* gpuWriteBuffer.unmap();
|
|
*
|
|
* const gpuReadBuffer = device.createBuffer({
|
|
* mappedAtCreation: false,
|
|
* size: sizeInBytes,
|
|
* usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE |
|
|
* GPUBufferUsage.COPY_SRC
|
|
* });
|
|
*
|
|
* const copyEncoder = device.createCommandEncoder();
|
|
* copyEncoder.copyBufferToBuffer(
|
|
* gpuWriteBuffer, 0, gpuReadBuffer, 0, sizeInBytes);
|
|
* const copyCommands = copyEncoder.finish();
|
|
* device.queue.submit([copyCommands]);
|
|
* gpuWriteBuffer.destroy();
|
|
* return gpuReadBuffer;
|
|
* }
|
|
*
|
|
* const savedBackend = tf.getBackend();
|
|
* await tf.setBackend('webgpu').catch(
|
|
* () => {throw new Error(
|
|
* 'Failed to use WebGPU backend. Please use Chrome Canary to run.')});
|
|
* const dtype = 'float32';
|
|
* const device = tf.backend().device;
|
|
* const aData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
|
|
* const bData = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
|
|
* const expected = [2, 4, 6, 8, 6, 8, 10, 12, 10, 12, 14, 16, 14, 16, 18, 20];
|
|
* const aBuffer = createGPUBufferFromData(device, aData, dtype);
|
|
* const shape = [aData.length];
|
|
* // To use zeroCopy, use {buffer: aBuffer, zeroCopy: true} instead and destroy
|
|
* // aBuffer untill all access is done.
|
|
* const a = tf.tensor({buffer: aBuffer}, shape, dtype);
|
|
* const b = tf.tensor(bData, shape, dtype);
|
|
* const result = tf.add(a, b);
|
|
* result.print();
|
|
* a.dispose();
|
|
* b.dispose();
|
|
* result.dispose();
|
|
* aBuffer.destroy();
|
|
* await tf.setBackend(savedBackend);
|
|
* ```
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`(At the moment it supports Uint8Array,
|
|
* Uint8ClampedArray, Int32Array, Float32Array) data types, or a `WebGLData`
|
|
* object, or a `WebGPUData` object. If the values are strings, they will be
|
|
* encoded as utf-8 and kept as `Uint8Array[]`. If the values is a `WebGLData`
|
|
* object, the dtype could only be 'float32' or 'int32' and the object has to
|
|
* have: 1. texture, a `WebGLTexture`, the texture must share the same
|
|
* `WebGLRenderingContext` with TFJS's WebGL backend (you could create a custom
|
|
* WebGL backend from your texture's canvas) and the internal texture format
|
|
* for the input texture must be floating point or normalized integer; 2.
|
|
* height, the height of the texture; 3. width, the width of the texture; 4.
|
|
* channels, a non-empty subset of 'RGBA', indicating the values of which
|
|
* channels will be passed to the tensor, such as 'R' or 'BR' (The order of the
|
|
* channels affect the order of tensor values. ). (If the values passed from
|
|
* texture is less than the tensor size, zeros will be padded at the rear.). If
|
|
* the values is a `WebGPUData` object, the dtype could only be 'float32' or
|
|
* 'int32 and the object has to have: buffer, a `GPUBuffer`. The buffer must:
|
|
* 1. share the same `GPUDevice` with TFJS's WebGPU backend; 2. buffer.usage
|
|
* should at least support GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC; 3.
|
|
* buffer.size should not be smaller than the byte size of tensor shape.
|
|
* WebGPUData optionally supports zero copy by flag zeroCopy. When zeroCopy is
|
|
* false or undefined(default),this passing GPUBuffer can be destroyed after
|
|
* tensor is created. When zeroCopy is true, this GPUBuffer is bound directly
|
|
* by the tensor, so do not destroy this GPUBuffer until all access is done.
|
|
* @param shape The shape of the tensor. Optional. If not provided,
|
|
* it is inferred from `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor(values, shape, dtype) {
|
|
var inferredShape = inferShape(values, dtype);
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-1 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor1d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* tf.tensor1d([1, 2, 3]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be array of numbers,
|
|
* or a `TypedArray`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor1d(values, dtype) {
|
|
assertNonNull(values);
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 1) {
|
|
throw new Error('tensor1d() requires values to be a flat/TypedArray');
|
|
}
|
|
var shape = null;
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-2 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor2d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* // Pass a nested array.
|
|
* tf.tensor2d([[1, 2], [3, 4]]).print();
|
|
* ```
|
|
* ```js
|
|
* // Pass a flat array and specify a shape.
|
|
* tf.tensor2d([1, 2, 3, 4], [2, 2]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`.
|
|
* @param shape The shape of the tensor. If not provided, it is inferred from
|
|
* `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor2d(values, shape, dtype) {
|
|
assertNonNull(values);
|
|
if (shape != null && shape.length !== 2) {
|
|
throw new Error('tensor2d() requires shape to have two numbers');
|
|
}
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 2 && inferredShape.length !== 1) {
|
|
throw new Error('tensor2d() requires values to be number[][] or flat/TypedArray');
|
|
}
|
|
if (inferredShape.length === 1 && shape == null) {
|
|
throw new Error('tensor2d() requires shape to be provided when `values` ' +
|
|
'are a flat/TypedArray');
|
|
}
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-3 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor3d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* // Pass a nested array.
|
|
* tf.tensor3d([[[1], [2]], [[3], [4]]]).print();
|
|
* ```
|
|
* ```js
|
|
* // Pass a flat array and specify a shape.
|
|
* tf.tensor3d([1, 2, 3, 4], [2, 2, 1]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`.
|
|
* @param shape The shape of the tensor. If not provided, it is inferred from
|
|
* `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor3d(values, shape, dtype) {
|
|
assertNonNull(values);
|
|
if (shape != null && shape.length !== 3) {
|
|
throw new Error('tensor3d() requires shape to have three numbers');
|
|
}
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 3 && inferredShape.length !== 1) {
|
|
throw new Error('tensor3d() requires values to be number[][][] or flat/TypedArray');
|
|
}
|
|
if (inferredShape.length === 1 && shape == null) {
|
|
throw new Error('tensor3d() requires shape to be provided when `values` ' +
|
|
'are a flat array');
|
|
}
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-4 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor4d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* // Pass a nested array.
|
|
* tf.tensor4d([[[[1], [2]], [[3], [4]]]]).print();
|
|
* ```
|
|
* ```js
|
|
* // Pass a flat array and specify a shape.
|
|
* tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`.
|
|
* @param shape The shape of the tensor. Optional. If not provided,
|
|
* it is inferred from `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor4d(values, shape, dtype) {
|
|
assertNonNull(values);
|
|
if (shape != null && shape.length !== 4) {
|
|
throw new Error('tensor4d() requires shape to have four numbers');
|
|
}
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 4 && inferredShape.length !== 1) {
|
|
throw new Error('tensor4d() requires values to be number[][][][] or flat/TypedArray');
|
|
}
|
|
if (inferredShape.length === 1 && shape == null) {
|
|
throw new Error('tensor4d() requires shape to be provided when `values` ' +
|
|
'are a flat array');
|
|
}
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-5 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor5d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* // Pass a nested array.
|
|
* tf.tensor5d([[[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]]]).print();
|
|
* ```
|
|
* ```js
|
|
* // Pass a flat array and specify a shape.
|
|
* tf.tensor5d([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 2, 2, 1]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`.
|
|
* @param shape The shape of the tensor. Optional. If not provided,
|
|
* it is inferred from `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor5d(values, shape, dtype) {
|
|
assertNonNull(values);
|
|
if (shape != null && shape.length !== 5) {
|
|
throw new Error('tensor5d() requires shape to have five numbers');
|
|
}
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 5 && inferredShape.length !== 1) {
|
|
throw new Error('tensor5d() requires values to be ' +
|
|
'number[][][][][] or flat/TypedArray');
|
|
}
|
|
if (inferredShape.length === 1 && shape == null) {
|
|
throw new Error('tensor5d() requires shape to be provided when `values` ' +
|
|
'are a flat array');
|
|
}
|
|
return makeTensor(values, 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates rank-6 `tf.Tensor` with the provided values, shape and dtype.
|
|
*
|
|
* The same functionality can be achieved with `tf.tensor`, but in general
|
|
* we recommend using `tf.tensor6d` as it makes the code more readable.
|
|
*
|
|
* ```js
|
|
* // Pass a nested array.
|
|
* tf.tensor6d([[[[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]]]]).print();
|
|
* ```
|
|
* ```js
|
|
* // Pass a flat array and specify a shape.
|
|
* tf.tensor6d([1, 2, 3, 4, 5, 6, 7, 8], [1, 1, 2, 2, 2, 1]).print();
|
|
* ```
|
|
*
|
|
* @param values The values of the tensor. Can be nested array of numbers,
|
|
* or a flat array, or a `TypedArray`.
|
|
* @param shape The shape of the tensor. Optional. If not provided,
|
|
* it is inferred from `values`.
|
|
* @param dtype The data type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function tensor6d(values, shape, dtype) {
|
|
assertNonNull(values);
|
|
if (shape != null && shape.length !== 6) {
|
|
throw new Error('tensor6d() requires shape to have six numbers');
|
|
}
|
|
var inferredShape = inferShape(values, dtype);
|
|
if (inferredShape.length !== 6 && inferredShape.length !== 1) {
|
|
throw new Error('tensor6d() requires values to be number[][][][][][] or ' +
|
|
'flat/TypedArray');
|
|
}
|
|
if (inferredShape.length === 1 && shape == null) {
|
|
throw new Error('tensor6d() requires shape to be provided when `values` ' +
|
|
'are a flat array');
|
|
}
|
|
shape = shape ||
|
|
inferredShape;
|
|
return makeTensor(values, shape, inferredShape, dtype);
|
|
}
|
|
|
|
/**
|
|
* Check whether updates.shape = indices.shape[:batchDim] +
|
|
* shape[sliceDim:]
|
|
*
|
|
* @param x The input tensor.
|
|
*/
|
|
function validateUpdateShape(shape, indices, updates) {
|
|
var sliceDim = (indices.rank > 1) ? indices.shape[indices.rank - 1] : 1;
|
|
var batchDim = (indices.rank > 1) ? indices.rank - 1 : 1;
|
|
var shapeError = 'Must have updates.shape = indices.shape[:batchDim] + ' +
|
|
"shape[sliceDim:], got updates.shape: ".concat(updates.shape) +
|
|
", indices.shape: ".concat(indices.shape, ", shape: ").concat(shape) +
|
|
", sliceDim: ".concat(sliceDim, ", and batchDim: ").concat(batchDim, ".");
|
|
if (updates.rank < batchDim) {
|
|
throw new Error(shapeError + " update.rank < ".concat(batchDim, ". "));
|
|
}
|
|
if (shape.length < sliceDim + (updates.rank - batchDim)) {
|
|
throw new Error(shapeError +
|
|
" Output shape length < ".concat(sliceDim + (updates.rank - batchDim)));
|
|
}
|
|
if (updates.rank !== batchDim + shape.length - sliceDim) {
|
|
throw new Error(shapeError + " update.rank != ".concat(batchDim + shape.length - sliceDim));
|
|
}
|
|
for (var d = 0; d < batchDim; ++d) {
|
|
if (updates.shape[d] !== indices.shape[d]) {
|
|
throw new Error(shapeError +
|
|
" updates.shape[".concat(d, "] (").concat(updates.shape[d], ") != indices.shape[").concat(d, "] (").concat(indices.shape[d], ")."));
|
|
}
|
|
}
|
|
for (var d = 0; d < updates.rank - batchDim; ++d) {
|
|
if (updates.shape[d + batchDim] !== shape[d + sliceDim]) {
|
|
throw new Error(shapeError +
|
|
" updates.shape[".concat(d + batchDim, "] (").concat(updates.shape[d + batchDim], ") != shape[").concat(d + batchDim, "] (").concat(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$1(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 ".concat(indices.rank, "."));
|
|
}
|
|
if (updates.rank < 1) {
|
|
throw new Error('tf.scatterND() expects the updates to be rank 1 or higher,' +
|
|
" but the rank was ".concat(updates.rank, "."));
|
|
}
|
|
if (indices.dtype !== 'int32') {
|
|
throw new Error("The dtype of 'indices' should be int32, but got dtype: ".concat(indices.dtype));
|
|
}
|
|
if (shape.length < 1) {
|
|
throw new Error("Output rank must be greater or equal to 1, but got shape: ".concat(shape));
|
|
}
|
|
if (shape.length === 0) {
|
|
if (indices.size === 0) {
|
|
throw new Error("Indices specified for empty output. indices shape: ".concat(indices.shape));
|
|
}
|
|
if (updates.size === 0) {
|
|
throw new Error("Updates specified for empty output. updates shape: ".concat(updates.shape));
|
|
}
|
|
}
|
|
validateUpdateShape(shape, indices, updates);
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
var $tensor = convertToTensor(tensor, 'tensor', 'tensorScatterupdate');
|
|
var $indices = convertToTensor(indices, 'indices', 'tensorScatterupdate', 'int32');
|
|
var $updates = convertToTensor(updates, 'updates', 'tensorScatterupdate');
|
|
validateInput$1($updates, $indices, $tensor.shape);
|
|
if ($tensor.dtype !== $updates.dtype) {
|
|
throw new Error("tensor and updates must have the same dtype, instead they are ".concat($tensor.dtype, " and ").concat($updates.dtype, "."));
|
|
}
|
|
var inputs = {
|
|
tensor: $tensor,
|
|
indices: $indices,
|
|
updates: $updates
|
|
};
|
|
var attrs = {};
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(TensorScatterUpdate, inputs, attrs);
|
|
}
|
|
var tensorScatterUpdate = op({ tensorScatterUpdate_: tensorScatterUpdate_ });
|
|
|
|
/**
|
|
* Finds the values and indices of the `k` largest entries along the last
|
|
* dimension.
|
|
*
|
|
* If the input is a vector (rank=1), finds the k largest entries in the vector
|
|
* and outputs their values and indices as vectors. Thus values[j] is the j-th
|
|
* largest entry in input, and its index is indices[j].
|
|
* For higher rank inputs, computes the top k entries along the last dimension.
|
|
*
|
|
* If two elements are equal, the lower-index element appears first.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor2d([[1, 5], [4, 3]]);
|
|
* const {values, indices} = tf.topk(a);
|
|
* values.print();
|
|
* indices.print();
|
|
* ```
|
|
* @param x 1-D or higher `tf.Tensor` with last dimension being at least `k`.
|
|
* @param k Number of top elements to look for along the last dimension.
|
|
* @param sorted If true, the resulting `k` elements will be sorted by the
|
|
* values in descending order.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function topk_(x, k, sorted) {
|
|
if (k === void 0) { k = 1; }
|
|
if (sorted === void 0) { sorted = true; }
|
|
var $x = convertToTensor(x, 'x', 'topk');
|
|
if ($x.rank === 0) {
|
|
throw new Error('topk() expects the input to be of rank 1 or higher');
|
|
}
|
|
var lastDim = $x.shape[$x.shape.length - 1];
|
|
if (k < 0) {
|
|
throw new Error("'k' passed to topk() must be >= 0 but got ".concat(k));
|
|
}
|
|
if (k > lastDim) {
|
|
throw new Error("'k' passed to topk() must be <= the last dimension (".concat(lastDim, ") ") +
|
|
"but got ".concat(k));
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { k: k, sorted: sorted };
|
|
var _a = __read(ENGINE.runKernel(TopK, inputs, attrs), 2), values = _a[0], indices = _a[1];
|
|
return { values: values, indices: indices };
|
|
}
|
|
var topk = /* @__PURE__ */ op({ topk_: topk_ });
|
|
|
|
/**
|
|
* @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 `tf.Tensor` with values sampled from a truncated normal
|
|
* distribution.
|
|
*
|
|
* ```js
|
|
* tf.truncatedNormal([2, 2]).print();
|
|
* ```
|
|
*
|
|
* The generated values follow a normal distribution with specified mean and
|
|
* standard deviation, except that values whose magnitude is more than 2
|
|
* standard deviations from the mean are dropped and re-picked.
|
|
*
|
|
* @param shape An array of integers defining the output tensor shape.
|
|
* @param mean The mean of the normal distribution.
|
|
* @param stdDev The standard deviation of the normal distribution.
|
|
* @param dtype The data type of the output tensor.
|
|
* @param seed The seed for the random number generator.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function truncatedNormal_(shape, mean, stdDev, dtype, seed) {
|
|
if (mean === void 0) { mean = 0; }
|
|
if (stdDev === void 0) { stdDev = 1; }
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
if (dtype != null && dtype === 'bool') {
|
|
throw new Error("Unsupported data type $ { dtype }");
|
|
}
|
|
var randGauss = new MPRandGauss(mean, stdDev, dtype, true /* truncated */, seed);
|
|
var res = buffer(shape, dtype);
|
|
for (var i = 0; i < res.values.length; i++) {
|
|
res.values[i] = randGauss.nextValue();
|
|
}
|
|
return res.toTensor();
|
|
}
|
|
var truncatedNormal = /* @__PURE__ */ op({ truncatedNormal_: truncatedNormal_ });
|
|
|
|
/**
|
|
* Finds unique elements along an axis of a tensor.
|
|
*
|
|
* It returns a tensor `values` containing all of the unique elements along the
|
|
* `axis` of the given tensor `x` in the same order that they occur along the
|
|
* `axis` in `x`; `x` does not need to be sorted. It also returns a tensor
|
|
* `indices` the same size as the number of the elements in `x` along the `axis`
|
|
* dimension. It contains the index in the unique output `values`.
|
|
*
|
|
* ```js
|
|
* // A 1-D tensor
|
|
* const a = tf.tensor1d([1, 1, 2, 4, 4, 4, 7, 8, 8]);
|
|
* const {values, indices} = tf.unique(a);
|
|
* values.print(); // [1, 2, 4, 7, 8,]
|
|
* indices.print(); // [0, 0, 1, 2, 2, 2, 3, 4, 4]
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // A 2-D tensor with axis=0
|
|
* //
|
|
* // 'a' is: [[1, 0, 0],
|
|
* // [1, 0, 0],
|
|
* // [2, 0, 0]]
|
|
* const a = tf.tensor2d([[1, 0, 0], [1, 0, 0], [2, 0, 0]]);
|
|
* const {values, indices} = tf.unique(a, 0)
|
|
* values.print(); // [[1, 0, 0],
|
|
* // [2, 0, 0]]
|
|
* indices.print(); // [0, 0, 1]
|
|
* ```
|
|
*
|
|
* ```js
|
|
* // A 2-D tensor with axis=1
|
|
* //
|
|
* // 'a' is: [[1, 0, 0],
|
|
* // [1, 0, 0],
|
|
* // [2, 0, 0]]
|
|
* const a = tf.tensor2d([[1, 0, 0], [1, 0, 0], [2, 0, 0]]);
|
|
* const {values, indices} = tf.unique(a, 1)
|
|
* values.print(); // [[1, 0],
|
|
* // [1, 0],
|
|
* // [2, 0]]
|
|
* indices.print(); // [0, 1, 1]
|
|
* ```
|
|
* @param x A tensor (int32, string, bool).
|
|
* @param axis The axis of the tensor to find the unique elements.
|
|
* @returns [uniqueElements, indices] (see above for details)
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function unique_(x, axis) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'unique', 'string_or_numeric');
|
|
assert($x.rank > 0, function () { return 'The input tensor must be at least 1D'; });
|
|
var inputs = { x: $x };
|
|
var attrs = { axis: axis };
|
|
var _a = __read(ENGINE.runKernel(Unique, inputs, attrs), 2), values = _a[0], indices = _a[1];
|
|
return { values: values, indices: indices };
|
|
}
|
|
var unique = /* @__PURE__ */ op({ unique_: unique_ });
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'unsortedSegmentSum');
|
|
var $segmentIds = convertToTensor(segmentIds, 'segmentIds', 'unsortedSegmentSum', 'int32');
|
|
assert(isInt(numSegments), function () { return 'numSegments must be of dtype int'; });
|
|
var inputs = { x: $x, segmentIds: $segmentIds };
|
|
var attrs = { numSegments: numSegments };
|
|
return ENGINE.runKernel(UnsortedSegmentSum, inputs, attrs);
|
|
}
|
|
var unsortedSegmentSum = /* @__PURE__ */ op({ unsortedSegmentSum_: 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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* 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) {
|
|
if (axis === void 0) { axis = 0; }
|
|
var $x = convertToTensor(x, 'x', 'unstack', 'string_or_numeric');
|
|
assert(axis >= -$x.shape.length && axis < $x.shape.length, function () { return "Axis = ".concat(axis, " is not in [-").concat($x.shape.length, ", ").concat($x.shape.length, ")"); });
|
|
var inputs = { value: $x };
|
|
var attrs = { axis: axis };
|
|
return ENGINE.runKernel(Unpack, inputs, attrs);
|
|
}
|
|
var unstack = /* @__PURE__ */ op({ unstack_: unstack_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Searches for where a value would go in a sorted sequence.
|
|
*
|
|
* This is not a method for checking containment (like javascript in).
|
|
*
|
|
* The typical use case for this operation is "binning", "bucketing", or
|
|
* "discretizing". The values are assigned to bucket-indices based on the edges
|
|
* listed in 'sortedSequence'. This operation returns the bucket-index for each
|
|
* value.
|
|
*
|
|
* The index returned corresponds to the first edge greater than the value.
|
|
*
|
|
* The axis is not settable for this operation. It always operates on the
|
|
* innermost dimension (axis=-1). The operation will accept any number of outer
|
|
* dimensions.
|
|
*
|
|
* Note: This operation assumes that 'upperBound' is sorted along the
|
|
* innermost axis, maybe using 'sort(..., axis=-1)'. If the sequence is not
|
|
* sorted no error is raised and the content of the returned tensor is not well
|
|
* defined.
|
|
*
|
|
* ```js
|
|
* const seq = tf.tensor1d([0, 3, 9, 10, 10]);
|
|
* const values = tf.tensor1d([0, 4, 10]);
|
|
* const result = tf.upperBound(seq, values);
|
|
* result.print(); // [1, 2, 5]
|
|
* ```
|
|
* @param sortedSequence: N-D. Sorted sequence.
|
|
* @param values: N-D. Search values.
|
|
* @return An N-D int32 tensor the size of values containing the result of
|
|
* applying upper bound to each value. The result is not a global index to
|
|
* the entire Tensor, but the index in the last dimension.
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function upperBound(sortedSequence, values) {
|
|
return searchSorted(sortedSequence, values, 'right');
|
|
}
|
|
|
|
/**
|
|
* @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 new variable with the provided initial value.
|
|
* ```js
|
|
* const x = tf.variable(tf.tensor([1, 2, 3]));
|
|
* x.assign(tf.tensor([4, 5, 6]));
|
|
*
|
|
* x.print();
|
|
* ```
|
|
*
|
|
* @param initialValue Initial value for the tensor.
|
|
* @param trainable If true, optimizers are allowed to update it.
|
|
* @param name Name of the variable. Defaults to a unique id.
|
|
* @param dtype If set, initialValue will be converted to the given type.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
|
*/
|
|
function variable(initialValue, trainable, name, dtype) {
|
|
if (trainable === void 0) { trainable = true; }
|
|
return ENGINE.makeVariable(initialValue, trainable, name, 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.
|
|
* =============================================================================
|
|
*/
|
|
function whereImpl(condShape, condVals) {
|
|
var indices = [];
|
|
for (var i = 0; i < condVals.length; i++) {
|
|
if (condVals[i]) {
|
|
indices.push(i);
|
|
}
|
|
}
|
|
var inBuffer = buffer(condShape, 'int32');
|
|
var out = buffer([indices.length, condShape.length], 'int32');
|
|
for (var i = 0; i < indices.length; i++) {
|
|
var loc = inBuffer.indexToLoc(indices[i]);
|
|
var offset = i * condShape.length;
|
|
out.values.set(loc, offset);
|
|
}
|
|
return out.toTensor();
|
|
}
|
|
|
|
/**
|
|
* Returns the coordinates of true elements of condition.
|
|
*
|
|
* The coordinates are returned in a 2-D tensor where the first dimension (rows)
|
|
* represents the number of true elements, and the second dimension (columns)
|
|
* represents the coordinates of the true elements. Keep in mind, the shape of
|
|
* the output tensor can vary depending on how many true values there are in
|
|
* input. Indices are output in row-major order. The resulting tensor has the
|
|
* shape `[numTrueElems, condition.rank]`.
|
|
*
|
|
* This is analogous to calling the python `tf.where(cond)` without an x or y.
|
|
*
|
|
* ```js
|
|
* const cond = tf.tensor1d([false, false, true], 'bool');
|
|
* const result = await tf.whereAsync(cond);
|
|
* result.print();
|
|
* ```
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Logical'}
|
|
*/
|
|
function whereAsync_(condition) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $condition, vals, res;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
$condition = convertToTensor(condition, 'condition', 'whereAsync', 'bool');
|
|
return [4 /*yield*/, $condition.data()];
|
|
case 1:
|
|
vals = _a.sent();
|
|
res = whereImpl($condition.shape, vals);
|
|
if (condition !== $condition) {
|
|
$condition.dispose();
|
|
}
|
|
return [2 /*return*/, res];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var whereAsync = whereAsync_;
|
|
|
|
/**
|
|
* Apply boolean mask to tensor.
|
|
*
|
|
* ```js
|
|
* const tensor = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]);
|
|
* const mask = tf.tensor1d([1, 0, 1], 'bool');
|
|
* const result = await tf.booleanMaskAsync(tensor, mask);
|
|
* result.print();
|
|
* ```
|
|
*
|
|
* @param tensor N-D tensor.
|
|
* @param mask K-D boolean tensor, K <= N and K must be known statically.
|
|
* @param axis A 0-D int Tensor representing the axis in tensor to mask from.
|
|
* By default, axis is 0 which will mask from the first dimension.
|
|
* Otherwise K + axis <= N.
|
|
*
|
|
* @doc {heading: 'Tensors', subheading: 'Slicing and Joining'}
|
|
*/
|
|
function booleanMaskAsync_(tensor, mask, axis) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $tensor, $mask, axisFrom, maskDim, tensorShape, leadingSize, i, targetTensorShape, reshapedTensor, reshapedMask, positivePositions, indices, res;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
$tensor = convertToTensor(tensor, 'tensor', 'boolMask');
|
|
$mask = convertToTensor(mask, 'mask', 'boolMask', 'bool');
|
|
axisFrom = axis == null ? 0 : axis;
|
|
maskDim = $mask.rank;
|
|
tensorShape = $tensor.shape;
|
|
assert(maskDim > 0, function () { return 'mask cannot be scalar'; });
|
|
assertShapesMatch(tensorShape.slice(axisFrom, axisFrom + maskDim), $mask.shape, "mask's shape must match the first K dimensions of tensor's shape,");
|
|
leadingSize = 1;
|
|
for (i = axisFrom; i < axisFrom + maskDim; i++) {
|
|
leadingSize *= tensorShape[i];
|
|
}
|
|
targetTensorShape = tensorShape.slice(0, axisFrom)
|
|
.concat([leadingSize], tensorShape.slice(axisFrom + maskDim));
|
|
reshapedTensor = reshape($tensor, targetTensorShape);
|
|
reshapedMask = reshape($mask, [-1]);
|
|
return [4 /*yield*/, whereAsync(reshapedMask)];
|
|
case 1:
|
|
positivePositions = _a.sent();
|
|
indices = squeeze(positivePositions, [1]);
|
|
res = gather(reshapedTensor, indices, axisFrom);
|
|
// Ensure no memory leak.
|
|
if (tensor !== $tensor) {
|
|
$tensor.dispose();
|
|
}
|
|
if (mask !== $mask) {
|
|
$mask.dispose();
|
|
}
|
|
indices.dispose();
|
|
reshapedTensor.dispose();
|
|
reshapedMask.dispose();
|
|
positivePositions.dispose();
|
|
return [2 /*return*/, res];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var booleanMaskAsync = booleanMaskAsync_;
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
/**
|
|
* Disposes any `tf.Tensor`s found within the provided object.
|
|
*
|
|
* @param container an object that may be a `tf.Tensor` or may directly
|
|
* contain `tf.Tensor`s, such as a `Tensor[]` or `{key: Tensor, ...}`. If
|
|
* the object is not a `tf.Tensor` or does not contain `Tensors`, nothing
|
|
* happens. In general it is safe to pass any object here, except that
|
|
* `Promise`s are not supported.
|
|
*
|
|
* @doc {heading: 'Performance', subheading: 'Memory'}
|
|
*/
|
|
function dispose(container) {
|
|
var tensors = getTensorsInContainer(container);
|
|
tensors.forEach(function (tensor) { return tensor.dispose(); });
|
|
}
|
|
/**
|
|
* Returns the current backend name (cpu, webgl, etc). The backend is
|
|
* responsible for creating tensors and executing operations on those tensors.
|
|
*
|
|
* @doc {heading: 'Backends'}
|
|
*/
|
|
function getBackend() {
|
|
return ENGINE.backendName;
|
|
}
|
|
/**
|
|
* Gets the current backend. If no backends have been initialized, this will
|
|
* attempt to initialize the best backend. Will throw an error if the highest
|
|
* priority backend has async initialization, in which case you should call
|
|
* 'await tf.ready()' before running other code.
|
|
*
|
|
* @doc {heading: 'Backends'}
|
|
*/
|
|
function backend() {
|
|
return ENGINE.backend;
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
var $x = convertToTensor(x, 'x', 'transpose');
|
|
if (perm == null) {
|
|
perm = $x.shape.map(function (s, i) { return i; }).reverse();
|
|
}
|
|
assert($x.rank === perm.length, function () { return "Error in transpose: rank of input ".concat($x.rank, " ") +
|
|
"must match length of perm ".concat(perm, "."); });
|
|
perm.forEach(function (axis) {
|
|
assert(axis >= 0 && axis < $x.rank, function () { return "All entries in 'perm' must be between 0 and ".concat($x.rank - 1) +
|
|
" but got ".concat(perm); });
|
|
});
|
|
if ($x.rank <= 1) {
|
|
return $x.clone();
|
|
}
|
|
var inputs = { x: $x };
|
|
var attrs = { perm: perm };
|
|
if ($x.dtype === 'complex64') {
|
|
return tidy(function () {
|
|
var $real = real($x);
|
|
var $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);
|
|
}
|
|
var transpose = /* @__PURE__ */ op({ transpose_: transpose_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Compute the moving average of a variable.
|
|
*
|
|
* Without zeroDebias, the moving average operation is defined by:
|
|
* `v += delta`
|
|
* where
|
|
* `delta = (1 - decay) * (x - v)`
|
|
*
|
|
* With zeroDebias (default), the `delta` term is scaled to debias the
|
|
* effect of the (assumed) zero-initialization of `v`.
|
|
* `delta /= (1 - decay ^ step)`
|
|
*
|
|
* For more details on the zero-debiasing algorithm, see:
|
|
* https://arxiv.org/abs/1412.6980
|
|
*
|
|
* Note that this function is completely stateless and does not keep track of
|
|
* step count. The step count needs to be maintained by the caller and passed
|
|
* in as `step`.
|
|
*
|
|
* @param v The current moving average value.
|
|
* @param x New input value, must have the same shape and dtype as `v`.
|
|
* @param decay The decay factor. Typical values are 0.95 and 0.99.
|
|
* @param step Step count.
|
|
* @param zeroDebias: Whether zeroDebias is to be performed (default: `true`).
|
|
* @returns The new moving average value.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Moving Average'}
|
|
*/
|
|
function movingAverage_(v, x, decay, step, zeroDebias) {
|
|
if (zeroDebias === void 0) { zeroDebias = true; }
|
|
var $v = convertToTensor(v, 'v', 'movingAverage');
|
|
var $x = convertToTensor(x, 'x', 'movingAverage');
|
|
var $decay = convertToTensor(decay, 'decay', 'movingAverage');
|
|
assertTypesMatch($v, $x);
|
|
assert(arraysEqual($v.shape, $x.shape), function () { return 'Shape mismatch in v and x'; });
|
|
var one = scalar(1);
|
|
var oneMinusDecay = sub(one, $decay);
|
|
var update = mul(sub($x, $v), oneMinusDecay);
|
|
if (zeroDebias) {
|
|
assert(step != null, function () { return 'When using zeroDebias: true, step is required.'; });
|
|
var $step = convertToTensor(step, 'step', 'movingAverage');
|
|
update = div(update, sub(one, pow($decay, $step)));
|
|
}
|
|
return add($v, update);
|
|
}
|
|
var movingAverage = /* @__PURE__ */ op({ movingAverage_: movingAverage_ });
|
|
|
|
/**
|
|
* @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 new tensor by applying sparse updates to individual
|
|
* values or slices within a zero tensor of the given shape tensor according to
|
|
* indices. This operator is the inverse of the `tf.gatherND` operator which
|
|
* extracts values or slices from a given tensor.
|
|
*
|
|
* ```js
|
|
* const indices = tf.tensor2d([4, 3, 1, 7], [4, 1], 'int32');
|
|
* const updates = tf.tensor1d([9, 10, 11, 12]);
|
|
* const shape = [8];
|
|
* tf.scatterND(indices, updates, shape).print() //[0, 11, 0, 10, 9, 0, 0, 12]
|
|
* ```
|
|
*
|
|
* @param indices The tensor contains the indices into the output tensor.
|
|
* @param updates The tensor contains the value for the indices.
|
|
* @param shape: The shape of the output tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Slicing and Joining'}
|
|
*/
|
|
function scatterND_(indices, updates, shape) {
|
|
assertNonNegativeIntegerDimensions(shape);
|
|
var $indices = convertToTensor(indices, 'indices', 'scatterND', 'int32');
|
|
var $updates = convertToTensor(updates, 'updates', 'scatterND');
|
|
validateInput$1($updates, $indices, shape);
|
|
var inputs = { indices: $indices, updates: $updates };
|
|
var attrs = { shape: shape };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(ScatterNd, inputs, attrs);
|
|
}
|
|
var scatterND = /* @__PURE__ */ op({ scatterND_: scatterND_ });
|
|
|
|
/**
|
|
* Validate sparseToDense inputs.
|
|
*
|
|
* @param sparseIndices A 0-D, 1-D, or 2-D Tensor of type int32.
|
|
* sparseIndices[i] contains the complete index where sparseValues[i] will be
|
|
* placed.
|
|
* @param sparseValues A 0-D or 1-D Tensor. Values
|
|
* corresponding to each row of sparseIndices, or a scalar value to be used for
|
|
* all sparse indices.
|
|
* @param outputShape number[]. Shape of the dense output tensor.
|
|
* @param validateIndices boolean. indice validation is not supported, error
|
|
* will be thrown if it is set.
|
|
*/
|
|
function validateInput(sparseIndices, sparseValues, outputShape, defaultValues) {
|
|
if (sparseIndices.dtype !== 'int32') {
|
|
throw new Error('tf.sparseToDense() expects the indices to be int32 type,' +
|
|
" but the dtype was ".concat(sparseIndices.dtype, "."));
|
|
}
|
|
if (sparseIndices.rank > 2) {
|
|
throw new Error('sparseIndices should be a scalar, vector, or matrix,' +
|
|
" but got shape ".concat(sparseIndices.shape, "."));
|
|
}
|
|
var numElems = sparseIndices.rank > 0 ? sparseIndices.shape[0] : 1;
|
|
var numDims = sparseIndices.rank > 1 ? sparseIndices.shape[1] : 1;
|
|
if (outputShape.length !== numDims) {
|
|
throw new Error('outputShape has incorrect number of elements:,' +
|
|
" ".concat(outputShape.length, ", should be: ").concat(numDims, "."));
|
|
}
|
|
var numValues = sparseValues.size;
|
|
if (!(sparseValues.rank === 0 ||
|
|
sparseValues.rank === 1 && numValues === numElems)) {
|
|
throw new Error('sparseValues has incorrect shape ' +
|
|
"".concat(sparseValues.shape, ", should be [] or [").concat(numElems, "]"));
|
|
}
|
|
if (sparseValues.dtype !== defaultValues.dtype) {
|
|
throw new Error('sparseValues.dtype must match defaultValues.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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Converts a sparse representation into a dense tensor.
|
|
*
|
|
* Builds an array dense with shape outputShape such that:
|
|
*
|
|
* // If sparseIndices is scalar
|
|
* dense[i] = (i == sparseIndices ? sparseValues : defaultValue)
|
|
*
|
|
* // If sparseIndices is a vector, then for each i
|
|
* dense[sparseIndices[i]] = sparseValues[i]
|
|
*
|
|
* // If sparseIndices is an n by d matrix, then for each i in [0, n)
|
|
* dense[sparseIndices[i][0], ..., sparseIndices[i][d-1]] = sparseValues[i]
|
|
* All other values in dense are set to defaultValue. If sparseValues is a
|
|
* scalar, all sparse indices are set to this single value.
|
|
*
|
|
* If indices are repeated the final value is summed over all values for those
|
|
* indices.
|
|
*
|
|
* ```js
|
|
* const indices = tf.tensor1d([4, 5, 6, 1, 2, 3], 'int32');
|
|
* const values = tf.tensor1d([10, 11, 12, 13, 14, 15], 'float32');
|
|
* const shape = [8];
|
|
* tf.sparseToDense(indices, values, shape).print();
|
|
* ```
|
|
*
|
|
* @param sparseIndices A 0-D, 1-D, or 2-D Tensor of type int32.
|
|
* sparseIndices[i] contains the complete index where sparseValues[i] will be
|
|
* placed.
|
|
* @param sparseValues A 0-D or 1-D Tensor. Values
|
|
* corresponding to each row of sparseIndices, or a scalar value to be used for
|
|
* all sparse indices.
|
|
* @param outputShape Shape of the dense output tensor. The type is inferred.
|
|
* @param defaultValue Scalar. Value to set for indices not specified in
|
|
* sparseIndices. Defaults to zero.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Normalization'}
|
|
*/
|
|
function sparseToDense_(sparseIndices, sparseValues, outputShape, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = 0; }
|
|
assertNonNegativeIntegerDimensions(outputShape);
|
|
var $sparseIndices = convertToTensor(sparseIndices, 'sparseIndices', 'sparseToDense', 'int32');
|
|
var $sparseValues = convertToTensor(sparseValues, 'sparseValues', 'sparseToDense', 'string_or_numeric');
|
|
var $defaultValue = convertToTensor(defaultValue, 'defaultValue', 'sparseToDense', $sparseValues.dtype);
|
|
validateInput($sparseIndices, $sparseValues, outputShape, $defaultValue);
|
|
var inputs = {
|
|
sparseIndices: $sparseIndices,
|
|
sparseValues: $sparseValues,
|
|
defaultValue: $defaultValue
|
|
};
|
|
var attrs = { outputShape: outputShape };
|
|
return ENGINE.runKernel(SparseToDense, inputs, attrs);
|
|
}
|
|
var sparseToDense = /* @__PURE__ */ op({ sparseToDense_: sparseToDense_ });
|
|
|
|
/**
|
|
* @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 input tensor into a Tensor with shape specified by
|
|
* `indices`.
|
|
*
|
|
* `indices` is a K-dimensional integer tensor, best thought of as a
|
|
* (K-1)-dimensional tensor of indices into input, where each element defines a
|
|
* slice of input:
|
|
* output[\\(i_0, ..., i_{K-2}\\)] = input[indices[\\(i_0, ..., i_{K-2}\\)]]
|
|
*
|
|
* Whereas in `tf.gather`, `indices` defines slices into the first dimension of
|
|
* input, in `tf.gatherND`, `indices` defines slices into the first N dimensions
|
|
* of input, where N = indices.shape[-1].
|
|
*
|
|
* The last dimension of indices can be at most the rank of input:
|
|
* indices.shape[-1] <= input.rank
|
|
*
|
|
* The last dimension of `indices` corresponds to elements
|
|
* (if indices.shape[-1] == input.rank) or slices
|
|
* (if indices.shape[-1] < input.rank) along dimension indices.shape[-1] of
|
|
* input.
|
|
* The output tensor has shape
|
|
* indices.shape[:-1] + input.shape[indices.shape[-1]:]
|
|
*
|
|
* Note that on CPU, if an out of bound index is found, an error is returned. On
|
|
* GPU, if an out of bound index is found, a 0 is stored in the corresponding
|
|
* output value.
|
|
*
|
|
* ```js
|
|
* const indices = tf.tensor2d([0, 1, 1, 0], [2,2], 'int32');
|
|
* const input = tf.tensor2d([9, 10, 11, 12], [2, 2]);
|
|
* tf.gatherND(input, indices).print() // [10, 11]
|
|
* ```
|
|
*
|
|
* @param x The tensor from which to gather values.
|
|
* @param indices Index tensor, must be of type int32.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Slicing and Joining'}
|
|
*/
|
|
function gatherND_(x, indices) {
|
|
var $indices = convertToTensor(indices, 'indices', 'gatherND', 'int32');
|
|
var $x = convertToTensor(x, 'x', 'gatherND', 'string_or_numeric');
|
|
var inputs = { params: $x, indices: $indices };
|
|
return ENGINE.runKernel(GatherNd, inputs);
|
|
}
|
|
var gatherND = /* @__PURE__ */ op({ gatherND_: gatherND_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Normalize noise shape based on provided tensor and noise shape.
|
|
*
|
|
* @param x Tensor.
|
|
* @param noiseShape The shape for the randomly generated keep/drop flags, as
|
|
* an array of numbers. Optional.
|
|
* @returns Normalized noise shape.
|
|
*/
|
|
function getNoiseShape(x, noiseShape) {
|
|
if (noiseShape == null) {
|
|
return x.shape.slice();
|
|
}
|
|
if (arraysEqual(x.shape, noiseShape)) {
|
|
return noiseShape;
|
|
}
|
|
if (x.shape.length === noiseShape.length) {
|
|
var newDimension = [];
|
|
for (var i = 0; i < x.shape.length; i++) {
|
|
if (noiseShape[i] == null && x.shape[i] != null) {
|
|
newDimension.push(x.shape[i]);
|
|
}
|
|
else {
|
|
newDimension.push(noiseShape[i]);
|
|
}
|
|
}
|
|
return newDimension;
|
|
}
|
|
return noiseShape;
|
|
}
|
|
|
|
/**
|
|
* @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 dropout.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor1d([1, 2, 2, 1]);
|
|
* const rate = 0.75;
|
|
* const output = tf.dropout(x, rate);
|
|
* output.print();
|
|
* ```
|
|
*
|
|
* @param x A floating point Tensor or TensorLike.
|
|
* @param rate A float in the range [0, 1). The probability that each element
|
|
* of x is discarded.
|
|
* @param noiseShape An array of numbers of type int32, representing the
|
|
* shape for randomly generated keep/drop flags. If the noiseShape has null
|
|
* value, it will be automatically replaced with the x's relative dimension
|
|
* size. Optional.
|
|
* @param seed Used to create random seeds. Optional.
|
|
* @returns A Tensor of the same shape of x.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Dropout'}
|
|
*/
|
|
function dropout_(x, rate, noiseShape, seed) {
|
|
var $x = convertToTensor(x, 'x', 'dropout');
|
|
assert($x.dtype === 'float32', function () { return "x has to be a floating point tensor since it's going to be " +
|
|
"scaled, but got a ".concat($x.dtype, " tensor instead."); });
|
|
assert(rate >= 0 && rate < 1, function () { return "rate must be a float in the range [0, 1), but got ".concat(rate, "."); });
|
|
if (rate === 0) {
|
|
return x instanceof Tensor ? $x.clone() : $x;
|
|
}
|
|
var $noiseShape = getNoiseShape($x, noiseShape);
|
|
var keepProb = 1 - rate;
|
|
var multiplier = div(floor(add(randomUniform($noiseShape, 0, 1, 'float32', seed), keepProb)), keepProb);
|
|
return mul($x, multiplier);
|
|
}
|
|
var dropout = /* @__PURE__ */ op({ dropout_: dropout_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
function enclosingPowerOfTwo(value) {
|
|
// Return 2**N for integer N such that 2**N >= value.
|
|
return Math.floor(Math.pow(2, Math.ceil(Math.log(value) / Math.log(2.0))));
|
|
}
|
|
function cosineWindow(windowLength, a, b) {
|
|
var even = 1 - windowLength % 2;
|
|
var newValues = new Float32Array(windowLength);
|
|
for (var i = 0; i < windowLength; ++i) {
|
|
var cosArg = (2.0 * Math.PI * i) / (windowLength + even - 1);
|
|
newValues[i] = a - b * Math.cos(cosArg);
|
|
}
|
|
return tensor1d(newValues, 'float32');
|
|
}
|
|
|
|
/**
|
|
* Returns whether the targets are in the top K predictions.
|
|
*
|
|
* ```js
|
|
* const predictions = tf.tensor2d([[20, 10, 40, 30], [30, 50, -20, 10]]);
|
|
* const targets = tf.tensor1d([2, 0]);
|
|
* const precision = await tf.inTopKAsync(predictions, targets);
|
|
* precision.print();
|
|
* ```
|
|
* @param predictions 2-D or higher `tf.Tensor` with last dimension being
|
|
* at least `k`.
|
|
* @param targets 1-D or higher `tf.Tensor`.
|
|
* @param k Optional Number of top elements to look at for computing precision,
|
|
* default to 1.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Evaluation'}
|
|
*/
|
|
function inTopKAsync_(predictions, targets, k) {
|
|
if (k === void 0) { k = 1; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $predictions, $targets, lastDim, predictionsVals, targetsVals, _a, batch, size, precision, b, offset, vals, valAndInd, i, i;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
$predictions = convertToTensor(predictions, 'predictions', 'inTopK');
|
|
$targets = convertToTensor(targets, 'targets', 'inTopK');
|
|
assert($predictions.rank > 1, function () { return 'inTopK() expects the predictions to be of rank 2 or higher, ' +
|
|
"but got ".concat($predictions.rank); });
|
|
assert($predictions.rank - 1 === $targets.rank, function () { return "predictions rank should be 1 larger than " +
|
|
"targets rank, but got predictions rank " +
|
|
"".concat($predictions.rank, " and targets rank ").concat($targets.rank); });
|
|
assertShapesMatch($predictions.shape.slice(0, $predictions.shape.length - 1), $targets.shape, "predictions's shape should be align with the targets' shape, " +
|
|
'except the last dimension.');
|
|
lastDim = $predictions.shape[$predictions.shape.length - 1];
|
|
assert(k > 0 && k <= lastDim, function () { return "'k' passed to inTopK() must be > 0 && <= the predictions last " +
|
|
"dimension (".concat(lastDim, "), but got ").concat(k); });
|
|
return [4 /*yield*/, $predictions.data()];
|
|
case 1:
|
|
predictionsVals = _b.sent();
|
|
return [4 /*yield*/, $targets.data()];
|
|
case 2:
|
|
targetsVals = _b.sent();
|
|
_a = __read([predictionsVals.length / lastDim, lastDim], 2), batch = _a[0], size = _a[1];
|
|
precision = getTypedArrayFromDType('bool', batch);
|
|
for (b = 0; b < batch; b++) {
|
|
offset = b * size;
|
|
vals = predictionsVals.subarray(offset, offset + size);
|
|
valAndInd = [];
|
|
for (i = 0; i < vals.length; i++) {
|
|
valAndInd.push({ value: vals[i], index: i });
|
|
}
|
|
valAndInd.sort(function (a, b) { return b.value - a.value; });
|
|
precision[b] = 0;
|
|
for (i = 0; i < k; i++) {
|
|
if (valAndInd[i].index === targetsVals[b]) {
|
|
precision[b] = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (predictions !== $predictions) {
|
|
$predictions.dispose();
|
|
}
|
|
if (targets !== $targets) {
|
|
$targets.dispose();
|
|
}
|
|
// Output precision has the same shape as targets.
|
|
return [2 /*return*/, tensor(precision, $targets.shape, 'bool')];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var inTopKAsync = inTopKAsync_;
|
|
|
|
/**
|
|
* @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, dimRoundingMode) {
|
|
if (dataFormat === void 0) { dataFormat = 'NHWC'; }
|
|
var x4D = x;
|
|
if (x.rank === 3) {
|
|
x4D = reshape(x, [1, x.shape[0], x.shape[1], x.shape[2]]);
|
|
}
|
|
var dy4D = dy;
|
|
if (dy4D.rank === 3) {
|
|
dy4D = reshape(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in conv2dDerFilter: input must be rank 4, but got shape " +
|
|
"".concat(x4D.shape, "."); });
|
|
assert(dy4D.rank === 4, function () { return "Error in conv2dDerFilter: dy must be rank 4, but got shape " +
|
|
"".concat(dy4D.shape, "."); });
|
|
assert(filterShape.length === 4, function () { return "Error in conv2dDerFilter: filterShape must be length 4, but got " +
|
|
"".concat(filterShape, "."); });
|
|
var inDepth = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1];
|
|
var outDepth = dataFormat === 'NHWC' ? dy4D.shape[3] : dy4D.shape[1];
|
|
assert(inDepth === filterShape[2], function () { return "Error in conv2dDerFilter: depth of input ".concat(inDepth, ") must ") +
|
|
"match input depth in filter (".concat(filterShape[2], "."); });
|
|
assert(outDepth === filterShape[3], function () { return "Error in conv2dDerFilter: depth of dy (".concat(outDepth, ") must ") +
|
|
"match output depth for filter (".concat(filterShape[3], ")."); });
|
|
checkPadOnDimRoundingMode('conv2dDerFilter', pad, dimRoundingMode);
|
|
var inputs = { x: x4D, dy: dy4D };
|
|
var attrs = { strides: strides, pad: pad, dataFormat: dataFormat, dimRoundingMode: dimRoundingMode, filterShape: filterShape };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(Conv2DBackpropFilter, inputs, attrs);
|
|
}
|
|
var conv2DBackpropFilter = /* @__PURE__ */ op({ conv2DBackpropFilter_: conv2DBackpropFilter_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
// Returns gradient for fused activation.
|
|
function getFusedDyActivation(dy, y, activation) {
|
|
if (activation == null || activation === 'linear') {
|
|
return dy;
|
|
}
|
|
if (activation === 'relu') {
|
|
return mul(dy, step(y));
|
|
}
|
|
throw new Error("Cannot compute gradient for fused activation ".concat(activation, "."));
|
|
}
|
|
// Returns gradient for fused bias.
|
|
function getFusedBiasGradient(bias, dyActivation) {
|
|
var res = dyActivation;
|
|
var reduceAxes = getReductionAxes(bias.shape, dyActivation.shape);
|
|
if (reduceAxes.length > 0) {
|
|
res = sum(res, reduceAxes);
|
|
}
|
|
return reshape(res, bias.shape);
|
|
}
|
|
function applyActivation(x, activation, preluActivationWeights, leakyreluAlpha) {
|
|
if (activation === 'linear') {
|
|
return x;
|
|
}
|
|
else if (activation === 'relu') {
|
|
return relu(x);
|
|
}
|
|
else if (activation === 'elu') {
|
|
return elu(x);
|
|
}
|
|
else if (activation === 'relu6') {
|
|
return relu6(x);
|
|
}
|
|
else if (activation === 'prelu') {
|
|
return prelu(x, preluActivationWeights);
|
|
}
|
|
else if (activation === 'leakyrelu') {
|
|
return leakyRelu(x, leakyreluAlpha);
|
|
}
|
|
else if (activation === 'sigmoid') {
|
|
return sigmoid(x);
|
|
}
|
|
throw new Error("Unknown fused activation ".concat(activation, "."));
|
|
}
|
|
// Whether we should call fused ops.
|
|
var shouldFuse = function (gradientDepth, activation) {
|
|
var gradientMode = gradientDepth > 0;
|
|
return !gradientMode || activation === 'linear';
|
|
};
|
|
|
|
/**
|
|
* Computes a 2D convolution over the input x, optionally fused with adding a
|
|
* bias and applying an activation.
|
|
*
|
|
* ```js
|
|
* const inputDepth = 2;
|
|
* const inShape = [2, 2, 2, inputDepth];
|
|
* const outputDepth = 2;
|
|
* const fSize = 1;
|
|
* const pad = 0;
|
|
* const strides = 1;
|
|
*
|
|
* const x = tf.tensor4d( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
* 16], inShape);
|
|
* const w = tf.tensor4d([-1, 1, -2, 0.5], [fSize, fSize, inputDepth,
|
|
* outputDepth]);
|
|
*
|
|
* tf.fused.conv2d({ x, filter: w, strides, pad, dataFormat: 'NHWC',
|
|
* dilations: [1, 1], bias: tf.scalar(5), activation: 'relu' }).print();
|
|
* ```
|
|
*
|
|
* @param obj An object with the following properties:
|
|
* @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]. Only "NHWC" is currently supported.
|
|
* @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.
|
|
* @param bias Tensor to be added to the result.
|
|
* @param activation Name of activation kernel (defaults to `linear`) to be
|
|
* applied
|
|
* after biasAdd.
|
|
* @param preluActivationWeights Tensor of prelu weights to be applied as part
|
|
* of a `prelu` activation, typically the same shape as `x`.
|
|
* @param leakyreluAlpha Optional. Alpha to be applied as part of a `leakyrelu`
|
|
* activation.
|
|
*/
|
|
function fusedConv2d_(_a) {
|
|
var _b;
|
|
var x = _a.x, filter = _a.filter, strides = _a.strides, pad = _a.pad, _c = _a.dataFormat, dataFormat = _c === void 0 ? 'NHWC' : _c, _d = _a.dilations, dilations = _d === void 0 ? [1, 1] : _d, dimRoundingMode = _a.dimRoundingMode, bias = _a.bias, _e = _a.activation, activation = _e === void 0 ? 'linear' : _e, preluActivationWeights = _a.preluActivationWeights, leakyreluAlpha = _a.leakyreluAlpha;
|
|
activation = activation || 'linear';
|
|
if (shouldFuse(ENGINE.state.gradientDepth, activation) === false) {
|
|
// TODO: Transpose bias and preluActivationWeights properly for NCHW
|
|
// format before computation.
|
|
assert(dataFormat === 'NHWC', function () { return "Error in fused conv2d: got dataFormat of ".concat(dataFormat, " but ") +
|
|
"only NHWC is currently supported for the case of gradient depth " +
|
|
"is 0 and the activation is not linear."; });
|
|
var result = conv2d$1(x, filter, strides, pad, dataFormat, dilations, dimRoundingMode);
|
|
if (bias != null) {
|
|
result = add(result, bias);
|
|
}
|
|
return applyActivation(result, activation, preluActivationWeights, leakyreluAlpha);
|
|
}
|
|
var $x = convertToTensor(x, 'x', 'conv2d', 'float32');
|
|
var $filter = convertToTensor(filter, 'filter', 'conv2d', 'float32');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in fused conv2d: input must be rank 4, but got rank " +
|
|
"".concat(x4D.rank, "."); });
|
|
assert($filter.rank === 4, function () { return "Error in fused conv2d: filter must be rank 4, but got rank " +
|
|
"".concat($filter.rank, "."); });
|
|
checkPadOnDimRoundingMode('fused conv2d', pad, dimRoundingMode);
|
|
var inputChannels = dataFormat === 'NHWC' ? x4D.shape[3] : x4D.shape[1];
|
|
assert($filter.shape[2] === inputChannels, function () { return "Error in conv2d: depth of input (".concat(inputChannels, ") must match ") +
|
|
"input depth for filter ".concat($filter.shape[2], "."); });
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in conv2D: Either strides or dilations must be 1. ' +
|
|
"Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
var convInfo = computeConv2DInfo(x4D.shape, $filter.shape, strides, dilations, pad, dimRoundingMode);
|
|
var $bias;
|
|
if (bias != null) {
|
|
$bias = convertToTensor(bias, 'bias', 'fused conv2d');
|
|
_b = __read(makeTypesMatch($bias, $x), 1), $bias = _b[0];
|
|
// According to TensorFlow, the bias is supposed be a 1-D tensor or a
|
|
// scalar.
|
|
//
|
|
// 3-D or 4-D bias is not disabled for NHWC format, because they are
|
|
// currently being used in some cases. For examplem in our code base,
|
|
// https://github.com/tensorflow/tfjs/blob/b53bd47e880367ae57493f0ea628abaf08db2d5d/tfjs-core/src/ops/fused/fused_conv2d_test.ts#L1972.
|
|
if (dataFormat === 'NHWC') {
|
|
assertAndGetBroadcastShape(convInfo.outShape, $bias.shape);
|
|
}
|
|
else {
|
|
assert($bias.shape.length <= 1, function () { return "Error in fused conv2d: only supports scalar or 1-D Tensor " +
|
|
"bias for NCHW format but got the bias of " +
|
|
"rank-".concat($bias.shape.length, "."); });
|
|
assert($bias.shape.length === 0 || $bias.shape[0] === convInfo.outChannels ||
|
|
$bias.shape[0] === 1, function () { return "Error in fused conv2d: bias shape (".concat($bias.shape, ") is not ") +
|
|
"compatible with the number of output channels " +
|
|
"(".concat(convInfo.outChannels, ")"); });
|
|
}
|
|
}
|
|
var $preluActivationWeights;
|
|
if (preluActivationWeights != null) {
|
|
// PReLU's activation weights could be a scalar, a 1-D tensor or a 3-D
|
|
// tensor.
|
|
var alphaShape_1 = preluActivationWeights.shape;
|
|
assert(alphaShape_1.length <= 1 || alphaShape_1.length === 3, function () { return "Error in fused conv2d: only supports scalar, 1-D Tensor or " +
|
|
"3-D Tensor PReLU activation weights but got a tensor of " +
|
|
"rank-".concat(alphaShape_1.length, "."); });
|
|
if (alphaShape_1.length === 1) {
|
|
// Whether the data format is NCHW or NHWC, the 1-D PReLU activation
|
|
// weights tensor should be aligned with the output channels of conv2d
|
|
// result.
|
|
assert(alphaShape_1[0] === 1 || alphaShape_1[0] === convInfo.outChannels, function () { return "Error in fused conv2d: PReLU activation weights " +
|
|
"(".concat(alphaShape_1, ") is not compatible with the number of output ") +
|
|
"channels (".concat(convInfo.outChannels, ")."); });
|
|
}
|
|
else if (alphaShape_1.length === 3) {
|
|
// Whether the data format is NCHW or NHWC, the PReLU activation weights
|
|
// tensor should has the compatible shape with the result of conv2d.
|
|
try {
|
|
assertAndGetBroadcastShape(alphaShape_1, convInfo.outShape);
|
|
}
|
|
catch (e) {
|
|
var errMsg = "Error in fused conv2d: PReLU activation weights (".concat(alphaShape_1, ") ") +
|
|
"is not compatible with the output shape of the conv2d " +
|
|
"(".concat(convInfo.outShape, ").");
|
|
throw Error(errMsg);
|
|
}
|
|
}
|
|
$preluActivationWeights = convertToTensor(preluActivationWeights, 'prelu weights', 'fused conv2d');
|
|
}
|
|
var grad = function (dy, saved) {
|
|
assert(dataFormat === 'NHWC', function () { return "Error in gradient of fused conv2D: got dataFormat of ".concat(dataFormat, " but only NHWC is currently supported."); });
|
|
var _a = __read(saved, 4), $filter = _a[0], x4D = _a[1], y = _a[2], $bias = _a[3];
|
|
var dyActivation = getFusedDyActivation(dy, y, activation);
|
|
assert(tupleValuesAreOne(dilations), function () { return 'Error in gradient of fused conv2D: ' +
|
|
"dilation rates greater than 1 " +
|
|
"are not yet supported in gradients. Got dilations '".concat(dilations, "'"); });
|
|
var xDer = conv2DBackpropInput(x4D.shape, dyActivation, $filter, strides, pad);
|
|
var filterDer = conv2DBackpropFilter(x4D, dyActivation, $filter.shape, strides, pad);
|
|
var der = [xDer, filterDer];
|
|
if ($bias != null) {
|
|
var biasDer = getFusedBiasGradient($bias, dyActivation);
|
|
der.push(biasDer);
|
|
}
|
|
return der;
|
|
};
|
|
var inputs = {
|
|
x: x4D,
|
|
filter: $filter,
|
|
bias: $bias,
|
|
preluActivationWeights: $preluActivationWeights
|
|
};
|
|
var attrs = {
|
|
strides: strides,
|
|
pad: pad,
|
|
dataFormat: dataFormat,
|
|
dilations: dilations,
|
|
dimRoundingMode: dimRoundingMode,
|
|
activation: activation,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
};
|
|
// Depending on the the params passed in we will have different number of
|
|
// inputs and thus a a different number of elements in the gradient.
|
|
if (bias == null) {
|
|
var customOp = customGrad(function (x4D, filter, save) {
|
|
var res =
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
ENGINE.runKernel(FusedConv2D, inputs, attrs);
|
|
save([filter, x4D, res]);
|
|
if (reshapedTo4D) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
res = reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return { value: res, gradFunc: grad };
|
|
});
|
|
return customOp(x4D, $filter);
|
|
}
|
|
else {
|
|
var customOpWithBias = customGrad(function (x4D, filter, bias, save) {
|
|
var res = ENGINE.runKernel(FusedConv2D, inputs, attrs);
|
|
save([filter, x4D, res, bias]);
|
|
if (reshapedTo4D) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
res = reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return { value: res, gradFunc: grad };
|
|
});
|
|
return customOpWithBias(x4D, $filter, $bias);
|
|
}
|
|
}
|
|
var conv2d = /* @__PURE__ */ op({ fusedConv2d_: fusedConv2d_ });
|
|
|
|
/**
|
|
* @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, dimRoundingMode) {
|
|
if (dilations === void 0) { dilations = [1, 1]; }
|
|
var x4D = x;
|
|
if (x.rank === 3) {
|
|
x4D = reshape(x, [1, x.shape[0], x.shape[1], x.shape[2]]);
|
|
}
|
|
var dy4D = dy;
|
|
if (dy4D.rank === 3) {
|
|
dy4D = reshape(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]);
|
|
}
|
|
var inputs = { x: x4D, dy: dy4D };
|
|
var attrs = { strides: strides, pad: pad, dimRoundingMode: dimRoundingMode, dilations: dilations, filterShape: filterShape };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
return ENGINE.runKernel(DepthwiseConv2dNativeBackpropFilter, inputs, attrs);
|
|
}
|
|
var depthwiseConv2dNativeBackpropFilter = op({ depthwiseConv2dNativeBackpropFilter_: 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, dimRoundingMode) {
|
|
if (dilations === void 0) { dilations = [1, 1]; }
|
|
var dy4D = dy;
|
|
var reshapedTo4D = false;
|
|
if (dy.rank === 3) {
|
|
reshapedTo4D = true;
|
|
dy4D = reshape(dy, [1, dy.shape[0], dy.shape[1], dy.shape[2]]);
|
|
}
|
|
var inputs = { dy: dy4D, filter: filter };
|
|
var attrs = { strides: strides, pad: pad, dimRoundingMode: dimRoundingMode, dilations: dilations, inputShape: xShape };
|
|
var res =
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
ENGINE.runKernel(DepthwiseConv2dNativeBackpropInput, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var depthwiseConv2dNativeBackpropInput = op({ depthwiseConv2dNativeBackpropInput_: depthwiseConv2dNativeBackpropInput_ });
|
|
|
|
/**
|
|
* Computes depthwise 2D convolution, optionally fused with adding a
|
|
* bias and applying an activation.
|
|
*
|
|
* Given a 4D `input` array and a `filter` array of shape
|
|
* `[filterHeight, filterWidth, inChannels, channelMultiplier]` containing
|
|
* `inChannels` convolutional filters of depth 1, this op applies a
|
|
* different filter to each input channel (expanding from 1 channel to
|
|
* `channelMultiplier` channels for each), then concatenates the results
|
|
* together. The output has `inChannels * channelMultiplier` channels.
|
|
*
|
|
* See
|
|
* [https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d](
|
|
* https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d)
|
|
* for more details.
|
|
*
|
|
* @param obj An object with the following properties:
|
|
* @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 tensor, rank 4, of shape
|
|
* `[filterHeight, filterWidth, inChannels, channelMultiplier]`.
|
|
* @param strides The strides of the convolution: `[strideHeight,
|
|
* strideWidth]`. If strides is a single number, then `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 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 `rate` is a single
|
|
* number, then `dilationHeight == dilationWidth`. If it is greater than
|
|
* 1, then all values of `strides` must be 1.
|
|
* @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]. Only "NHWC" is currently supported.
|
|
* @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
|
* provided, it will default to truncate.
|
|
* @param bias Tensor to be added to the result.
|
|
* @param activation Name of activation kernel (defaults to `linear`).
|
|
* @param preluActivationWeights Tensor of prelu weights to be applied as part
|
|
* of a `prelu` activation, typically the same shape as `x`.
|
|
* @param leakyreluAlpha Optional. Alpha to be applied as part of a `leakyrelu`
|
|
* activation.
|
|
*/
|
|
function fusedDepthwiseConv2d_(_a) {
|
|
var _b;
|
|
var x = _a.x, filter = _a.filter, strides = _a.strides, pad = _a.pad, _c = _a.dataFormat, dataFormat = _c === void 0 ? 'NHWC' : _c, _d = _a.dilations, dilations = _d === void 0 ? [1, 1] : _d, dimRoundingMode = _a.dimRoundingMode, bias = _a.bias, _e = _a.activation, activation = _e === void 0 ? 'linear' : _e, preluActivationWeights = _a.preluActivationWeights, leakyreluAlpha = _a.leakyreluAlpha;
|
|
if (shouldFuse(ENGINE.state.gradientDepth, activation) === false) {
|
|
var result = depthwiseConv2d$1(x, filter, strides, pad, dataFormat, dilations, dimRoundingMode);
|
|
if (bias != null) {
|
|
result = add(result, bias);
|
|
}
|
|
return applyActivation(result, activation, preluActivationWeights, leakyreluAlpha);
|
|
}
|
|
var $x = convertToTensor(x, 'x', 'depthwiseConv2d', 'float32');
|
|
var $filter = convertToTensor(filter, 'filter', 'depthwiseConv2d', 'float32');
|
|
var x4D = $x;
|
|
var reshapedTo4D = false;
|
|
if ($x.rank === 3) {
|
|
reshapedTo4D = true;
|
|
x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
|
}
|
|
assert(x4D.rank === 4, function () { return "Error in fused depthwiseConv2d: input must be rank 4, but got " +
|
|
"rank ".concat(x4D.rank, "."); });
|
|
assert($filter.rank === 4, function () { return "Error in fused depthwiseConv2d: filter must be rank 4, " +
|
|
"but got rank ".concat($filter.rank, "."); });
|
|
assert(x4D.shape[3] === $filter.shape[2], function () { return "Error in fused depthwiseConv2d: number of input channels " +
|
|
"(".concat(x4D.shape[3], ") must match the inChannels dimension in ") +
|
|
"filter ".concat($filter.shape[2], "."); });
|
|
if (dilations == null) {
|
|
dilations = [1, 1];
|
|
}
|
|
assert(eitherStridesOrDilationsAreOne(strides, dilations), function () { return 'Error in fused depthwiseConv2d: Either strides or dilations must ' +
|
|
"be 1. Got strides ".concat(strides, " and dilations '").concat(dilations, "'"); });
|
|
checkPadOnDimRoundingMode('fused depthwiseConv2d', pad, dimRoundingMode);
|
|
var convInfo = computeConv2DInfo(x4D.shape, $filter.shape, strides, dilations, pad, dimRoundingMode, true /* depthwise */);
|
|
var $bias;
|
|
if (bias != null) {
|
|
$bias = convertToTensor(bias, 'bias', 'fused conv2d');
|
|
_b = __read(makeTypesMatch($bias, $x), 1), $bias = _b[0];
|
|
assertAndGetBroadcastShape(convInfo.outShape, $bias.shape);
|
|
}
|
|
var $preluActivationWeights;
|
|
if (preluActivationWeights != null) {
|
|
$preluActivationWeights = convertToTensor(preluActivationWeights, 'prelu weights', 'fused depthwiseConv2d');
|
|
}
|
|
var grad = function (dy, saved) {
|
|
assert(tupleValuesAreOne(dilations), function () { return 'Error in gradient of fused depthwiseConv2d: dilation rates ' +
|
|
"greater than 1 are not yet supported. Got dilations " +
|
|
"'".concat(dilations, "'"); });
|
|
var _a = __read(saved, 4), $filter = _a[0], x4D = _a[1], y = _a[2], bias = _a[3];
|
|
var dyActivation = getFusedDyActivation(dy, y, activation);
|
|
var xDer = depthwiseConv2dNativeBackpropInput(x4D.shape, dyActivation, $filter, strides, pad, dilations, dimRoundingMode);
|
|
var filterDer = depthwiseConv2dNativeBackpropFilter(x4D, dyActivation, $filter.shape, strides, pad, dilations, dimRoundingMode);
|
|
if (bias != null) {
|
|
var biasDer = getFusedBiasGradient($bias, dyActivation);
|
|
return [xDer, filterDer, biasDer];
|
|
}
|
|
return [xDer, filterDer];
|
|
};
|
|
var inputs = {
|
|
x: x4D,
|
|
filter: $filter,
|
|
bias: $bias,
|
|
preluActivationWeights: $preluActivationWeights
|
|
};
|
|
var attrs = {
|
|
strides: strides,
|
|
pad: pad,
|
|
dataFormat: dataFormat,
|
|
dilations: dilations,
|
|
dimRoundingMode: dimRoundingMode,
|
|
activation: activation,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
};
|
|
// Depending on the the params passed in we will have different number of
|
|
// inputs and thus a a different number of elements in the gradient.
|
|
if (bias == null) {
|
|
var customOp = customGrad(function (x4D, filter, save) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(FusedDepthwiseConv2D, inputs, attrs);
|
|
save([filter, x4D, res]);
|
|
if (reshapedTo4D) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
res = reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return { value: res, gradFunc: grad };
|
|
});
|
|
return customOp(x4D, $filter);
|
|
}
|
|
else {
|
|
var customOpWithBias = customGrad(function (x4D, filter, bias, save) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(FusedDepthwiseConv2D, inputs, attrs);
|
|
save([filter, x4D, res, bias]);
|
|
if (reshapedTo4D) {
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
res = reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return { value: res, gradFunc: grad };
|
|
});
|
|
return customOpWithBias(x4D, $filter, $bias);
|
|
}
|
|
}
|
|
var depthwiseConv2d = /* @__PURE__ */ op({ fusedDepthwiseConv2d_: fusedDepthwiseConv2d_ });
|
|
|
|
/**
|
|
* Computes the dot product of two matrices with optional activation and bias.
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor2d([-1, -2], [1, 2]);
|
|
* const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);
|
|
* const bias = tf.tensor2d([1, 2], [1, 2]);
|
|
*
|
|
* tf.fused.matMul({a, b, bias, activation: 'relu'}).print();
|
|
* ```
|
|
*
|
|
* @param obj An object with the following properties:
|
|
* - `a` First matrix in dot product operation.
|
|
* - `b` Second matrix in dot product operation.
|
|
* - `transposeA` If true, `a` is transposed before multiplication.
|
|
* - `transposeB` If true, `b` is transposed before multiplication.
|
|
* - `bias` Matrix to be added to the result.
|
|
* - `activation` Name of activation kernel (defaults to `linear`).
|
|
* - `preluActivationWeights` Tensor of prelu weights.
|
|
* - `leakyreluAlpha` Alpha of leakyrelu.
|
|
*/
|
|
function fusedMatMul_(_a) {
|
|
var _b, _c;
|
|
var a = _a.a, b = _a.b, _d = _a.transposeA, transposeA = _d === void 0 ? false : _d, _e = _a.transposeB, transposeB = _e === void 0 ? false : _e, bias = _a.bias, _f = _a.activation, activation = _f === void 0 ? 'linear' : _f, preluActivationWeights = _a.preluActivationWeights, _g = _a.leakyreluAlpha, leakyreluAlpha = _g === void 0 ? 0.2 : _g;
|
|
if (shouldFuse(ENGINE.state.gradientDepth, activation) === false) {
|
|
var result = matMul$1(a, b, transposeA, transposeB);
|
|
if (bias != null) {
|
|
result = add(result, bias);
|
|
}
|
|
return applyActivation(result, activation, preluActivationWeights, leakyreluAlpha);
|
|
}
|
|
var $a = convertToTensor(a, 'a', 'fused matMul');
|
|
var $b = convertToTensor(b, 'b', 'fused matMul');
|
|
_b = __read(makeTypesMatch($a, $b), 2), $a = _b[0], $b = _b[1];
|
|
var innerShapeA = transposeA ? $a.shape[$a.rank - 2] : $a.shape[$a.rank - 1];
|
|
var innerShapeB = transposeB ? $b.shape[$b.rank - 1] : $b.shape[$b.rank - 2];
|
|
var outerShapeA = transposeA ? $a.shape[$a.rank - 1] : $a.shape[$a.rank - 2];
|
|
var outerShapeB = transposeB ? $b.shape[$b.rank - 2] : $b.shape[$b.rank - 1];
|
|
var outerDimsA = $a.shape.slice(0, -2);
|
|
var outerDimsB = $b.shape.slice(0, -2);
|
|
var batchDimA = sizeFromShape(outerDimsA);
|
|
var batchDimB = sizeFromShape(outerDimsB);
|
|
assert(innerShapeA === innerShapeB, function () { return "Error in fused matMul: inner shapes (".concat(innerShapeA, ") and (") +
|
|
"".concat(innerShapeB, ") of Tensors with shapes ").concat($a.shape, " and ") +
|
|
"".concat($b.shape, " and transposeA=").concat(transposeA) +
|
|
" and transposeB=".concat(transposeB, " must match."); });
|
|
var outShapeOuterDims = assertAndGetBroadcastShape($a.shape.slice(0, -2), $b.shape.slice(0, -2));
|
|
var outShape = outShapeOuterDims.concat([outerShapeA, outerShapeB]);
|
|
var a3D = transposeA ?
|
|
reshape($a, [batchDimA, innerShapeA, outerShapeA]) :
|
|
reshape($a, [batchDimA, outerShapeA, innerShapeA]);
|
|
var b3D = transposeB ?
|
|
reshape($b, [batchDimB, outerShapeB, innerShapeB]) :
|
|
reshape($b, [batchDimB, innerShapeB, outerShapeB]);
|
|
var $bias;
|
|
if (bias != null) {
|
|
$bias = convertToTensor(bias, 'bias', 'fused matMul');
|
|
_c = __read(makeTypesMatch($bias, $a), 1), $bias = _c[0];
|
|
assertAndGetBroadcastShape(outShape, $bias.shape);
|
|
}
|
|
var $preluActivationWeights;
|
|
if (preluActivationWeights != null) {
|
|
$preluActivationWeights = convertToTensor(preluActivationWeights, 'prelu weights', 'fused matMul');
|
|
}
|
|
var grad = function (dy, saved) {
|
|
var _a = __read(saved, 4), a3D = _a[0], b3D = _a[1], y = _a[2], $bias = _a[3];
|
|
// we reshape dy because the result of the forward is not
|
|
// necessarily going to be a 3d tensor due to a reshape done at the end of
|
|
// the customOp.
|
|
var dyActivation = getFusedDyActivation(reshape(dy, y.shape), y, activation);
|
|
var aDer;
|
|
var bDer;
|
|
if (!transposeA && !transposeB) {
|
|
aDer = matMul$1(dyActivation, b3D, false, true);
|
|
bDer = matMul$1(a3D, dyActivation, true, false);
|
|
}
|
|
else if (!transposeA && transposeB) {
|
|
aDer = matMul$1(dyActivation, b3D, false, false);
|
|
bDer = matMul$1(dyActivation, a3D, true, false);
|
|
}
|
|
else if (transposeA && !transposeB) {
|
|
aDer = matMul$1(b3D, dyActivation, false, true);
|
|
bDer = matMul$1(a3D, dyActivation, false, false);
|
|
}
|
|
else {
|
|
aDer = matMul$1(b3D, dyActivation, true, true);
|
|
bDer = matMul$1(dyActivation, a3D, true, true);
|
|
}
|
|
if (bias != null) {
|
|
var biasDer = getFusedBiasGradient($bias, dyActivation);
|
|
return [aDer, bDer, biasDer];
|
|
}
|
|
else {
|
|
return [aDer, bDer];
|
|
}
|
|
};
|
|
var inputs = {
|
|
a: a3D,
|
|
b: b3D,
|
|
bias: $bias,
|
|
preluActivationWeights: $preluActivationWeights
|
|
};
|
|
var attrs = { transposeA: transposeA, transposeB: transposeB, activation: activation, leakyreluAlpha: leakyreluAlpha };
|
|
// Depending on the the params passed in we will have different number of
|
|
// inputs and thus a a different number of elements in the gradient.
|
|
if (bias == null) {
|
|
var customOp = customGrad(function (a3D, b3D, save) {
|
|
var res =
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
ENGINE.runKernel(_FusedMatMul, inputs, attrs);
|
|
save([a3D, b3D, res]);
|
|
return { value: reshape(res, outShape), gradFunc: grad };
|
|
});
|
|
return customOp(a3D, b3D);
|
|
}
|
|
else {
|
|
var customOpWithBias = customGrad(function (a3D, b3D, $bias, save) {
|
|
var res =
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
ENGINE.runKernel(_FusedMatMul, inputs, attrs);
|
|
save([a3D, b3D, res, $bias]);
|
|
return { value: reshape(res, outShape), gradFunc: grad };
|
|
});
|
|
return customOpWithBias(a3D, b3D, $bias);
|
|
}
|
|
}
|
|
var matMul = /* @__PURE__ */ op({ fusedMatMul_: fusedMatMul_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
|
|
var fused_ops = {
|
|
__proto__: null,
|
|
conv2d: conv2d,
|
|
depthwiseConv2d: depthwiseConv2d,
|
|
matMul: matMul
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Generate a hamming window.
|
|
*
|
|
* See: https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
|
|
*
|
|
* ```js
|
|
* tf.signal.hammingWindow(10).print();
|
|
* ```
|
|
* @param The length of window
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Signal', namespace: 'signal'}
|
|
*/
|
|
function hammingWindow_(windowLength) {
|
|
return cosineWindow(windowLength, 0.54, 0.46);
|
|
}
|
|
var hammingWindow = /* @__PURE__ */ op({ hammingWindow_: hammingWindow_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Generate a Hann window.
|
|
*
|
|
* See: https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
|
|
*
|
|
* ```js
|
|
* tf.signal.hannWindow(10).print();
|
|
* ```
|
|
* @param The length of window
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Signal', namespace: 'signal'}
|
|
*/
|
|
function hannWindow_(windowLength) {
|
|
return cosineWindow(windowLength, 0.5, 0.5);
|
|
}
|
|
var hannWindow = /* @__PURE__ */ op({ hannWindow_: hannWindow_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Expands input into frames of frameLength.
|
|
* Slides a window size with frameStep.
|
|
*
|
|
* ```js
|
|
* tf.signal.frame([1, 2, 3], 2, 1).print();
|
|
* ```
|
|
* @param signal The input tensor to be expanded
|
|
* @param frameLength Length of each frame
|
|
* @param frameStep The frame hop size in samples.
|
|
* @param padEnd Whether to pad the end of signal with padValue.
|
|
* @param padValue A number to use where the input signal does
|
|
* not exist when padEnd is True.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Signal', namespace: 'signal'}
|
|
*/
|
|
function frame_(signal, frameLength, frameStep, padEnd, padValue) {
|
|
if (padEnd === void 0) { padEnd = false; }
|
|
if (padValue === void 0) { padValue = 0; }
|
|
var start = 0;
|
|
var output = [];
|
|
while (start + frameLength <= signal.size) {
|
|
output.push(slice(signal, start, frameLength));
|
|
start += frameStep;
|
|
}
|
|
if (padEnd) {
|
|
while (start < signal.size) {
|
|
var padLen = (start + frameLength) - signal.size;
|
|
var pad = concat([
|
|
slice(signal, start, frameLength - padLen), fill([padLen], padValue)
|
|
]);
|
|
output.push(pad);
|
|
start += frameStep;
|
|
}
|
|
}
|
|
if (output.length === 0) {
|
|
return tensor2d([], [0, frameLength]);
|
|
}
|
|
return reshape(concat(output), [output.length, frameLength]);
|
|
}
|
|
var frame = /* @__PURE__ */ op({ frame_: frame_ });
|
|
|
|
/**
|
|
* @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 the Short-time Fourier Transform of signals
|
|
* See: https://en.wikipedia.org/wiki/Short-time_Fourier_transform
|
|
*
|
|
* ```js
|
|
* const input = tf.tensor1d([1, 1, 1, 1, 1])
|
|
* tf.signal.stft(input, 3, 1).print();
|
|
* ```
|
|
* @param signal 1-dimensional real value tensor.
|
|
* @param frameLength The window length of samples.
|
|
* @param frameStep The number of samples to step.
|
|
* @param fftLength The size of the FFT to apply.
|
|
* @param windowFn A callable that takes a window length and returns 1-d tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Signal', namespace: 'signal'}
|
|
*/
|
|
function stft_(signal, frameLength, frameStep, fftLength, windowFn) {
|
|
if (windowFn === void 0) { windowFn = hannWindow; }
|
|
if (fftLength == null) {
|
|
fftLength = enclosingPowerOfTwo(frameLength);
|
|
}
|
|
var framedSignal = frame(signal, frameLength, frameStep);
|
|
var windowedSignal = mul(framedSignal, windowFn(frameLength));
|
|
return rfft(windowedSignal, fftLength);
|
|
}
|
|
var stft = /* @__PURE__ */ op({ stft_: stft_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Extracts crops from the input image tensor and resizes them using bilinear
|
|
* sampling or nearest neighbor sampling (possibly with aspect ratio change)
|
|
* to a common output size specified by cropSize.
|
|
*
|
|
* @param image 4d tensor of shape `[batch,imageHeight,imageWidth, depth]`,
|
|
* where imageHeight and imageWidth must be positive, specifying the
|
|
* batch of images from which to take crops
|
|
* @param boxes 2d float32 tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the normalized
|
|
* coordinates of the box in the `boxInd[i]`th image in the batch
|
|
* @param boxInd 1d int32 tensor of shape `[numBoxes]` with values in range
|
|
* `[0, batch)` that specifies the image that the `i`-th box refers to.
|
|
* @param cropSize 1d int32 tensor of 2 elements `[cropHeigh, cropWidth]`
|
|
* specifying the size to which all crops are resized to.
|
|
* @param method Optional string from `'bilinear' | 'nearest'`,
|
|
* defaults to bilinear, which specifies the sampling method for resizing
|
|
* @param extrapolationValue A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to 0.
|
|
* @return A 4D tensor of the shape `[numBoxes,cropHeight,cropWidth,depth]`
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function cropAndResize_(image, boxes, boxInd, cropSize, method, extrapolationValue) {
|
|
if (method === void 0) { method = 'bilinear'; }
|
|
if (extrapolationValue === void 0) { extrapolationValue = 0; }
|
|
var $image = convertToTensor(image, 'image', 'cropAndResize');
|
|
var $boxes = convertToTensor(boxes, 'boxes', 'cropAndResize', 'float32');
|
|
var $boxInd = convertToTensor(boxInd, 'boxInd', 'cropAndResize', 'int32');
|
|
var numBoxes = $boxes.shape[0];
|
|
assert($image.rank === 4, function () { return 'Error in cropAndResize: image must be rank 4,' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
assert($boxes.rank === 2 && $boxes.shape[1] === 4, function () { return "Error in cropAndResize: boxes must be have size [".concat(numBoxes, ",4] ") +
|
|
"but had shape ".concat($boxes.shape, "."); });
|
|
assert($boxInd.rank === 1 && $boxInd.shape[0] === numBoxes, function () { return "Error in cropAndResize: boxInd must be have size [".concat(numBoxes, "] ") +
|
|
"but had shape ".concat($boxes.shape, "."); });
|
|
assert(cropSize.length === 2, function () { return "Error in cropAndResize: cropSize must be of length 2, but got " +
|
|
"length ".concat(cropSize.length, "."); });
|
|
assert(cropSize[0] >= 1 && cropSize[1] >= 1, function () { return "cropSize must be atleast [1,1], but was ".concat(cropSize); });
|
|
assert(method === 'bilinear' || method === 'nearest', function () { return "method must be bilinear or nearest, but was ".concat(method); });
|
|
var inputs = { image: $image, boxes: $boxes, boxInd: $boxInd };
|
|
var attrs = { method: method, extrapolationValue: extrapolationValue, cropSize: cropSize };
|
|
var res = ENGINE.runKernel(CropAndResize, inputs, attrs);
|
|
return res;
|
|
}
|
|
var cropAndResize = /* @__PURE__ */ op({ cropAndResize_: cropAndResize_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Flips the image left to right. Currently available in the CPU, WebGL, and
|
|
* WASM backends.
|
|
*
|
|
* @param image 4d tensor of shape `[batch, imageHeight, imageWidth, depth]`.
|
|
*/
|
|
/** @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'} */
|
|
function flipLeftRight_(image) {
|
|
var $image = convertToTensor(image, 'image', 'flipLeftRight', 'float32');
|
|
assert($image.rank === 4, function () { return 'Error in flipLeftRight: image must be rank 4,' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
var inputs = { image: $image };
|
|
var res = ENGINE.runKernel(FlipLeftRight, inputs, {});
|
|
return res;
|
|
}
|
|
var flipLeftRight = /* @__PURE__ */ op({ flipLeftRight_: flipLeftRight_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Converts images from grayscale to RGB format.
|
|
*
|
|
* @param image A grayscale tensor to convert. The `image`'s last dimension must
|
|
* be size 1 with at least a two-dimensional shape.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function grayscaleToRGB_(image) {
|
|
var $image = convertToTensor(image, 'image', 'grayscaleToRGB');
|
|
var lastDimsIdx = $image.rank - 1;
|
|
var lastDims = $image.shape[lastDimsIdx];
|
|
assert($image.rank >= 2, function () { return 'Error in grayscaleToRGB: images must be at least rank 2, ' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
assert(lastDims === 1, function () { return 'Error in grayscaleToRGB: last dimension of a grayscale image ' +
|
|
"should be size 1, but got size ".concat(lastDims, "."); });
|
|
var reps = new Array($image.rank);
|
|
reps.fill(1, 0, lastDimsIdx);
|
|
reps[lastDimsIdx] = 3;
|
|
return tile($image, reps);
|
|
}
|
|
var grayscaleToRGB = /* @__PURE__ */ op({ grayscaleToRGB_: grayscaleToRGB_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Converts images from RGB format to grayscale.
|
|
*
|
|
* @param image A RGB tensor to convert. The `image`'s last dimension must
|
|
* be size 3 with at least a two-dimensional shape.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function rgbToGrayscale_(image) {
|
|
var $image = convertToTensor(image, 'image', 'RGBToGrayscale');
|
|
var lastDimsIdx = $image.rank - 1;
|
|
var lastDims = $image.shape[lastDimsIdx];
|
|
assert($image.rank >= 2, function () { return 'Error in RGBToGrayscale: images must be at least rank 2, ' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
assert(lastDims === 3, function () { return 'Error in RGBToGrayscale: last dimension of an RGB image ' +
|
|
"should be size 3, but got size ".concat(lastDims, "."); });
|
|
// Remember original dtype so we can convert back if needed
|
|
var origDtype = $image.dtype;
|
|
var fltImage = cast($image, 'float32');
|
|
var rgbWeights = tensor1d([0.2989, 0.5870, 0.1140]);
|
|
var grayFloat;
|
|
switch ($image.rank) {
|
|
case 2:
|
|
grayFloat = einsum('ij,j->i', fltImage, rgbWeights);
|
|
break;
|
|
case 3:
|
|
grayFloat = einsum('ijk,k->ij', fltImage, rgbWeights);
|
|
break;
|
|
case 4:
|
|
grayFloat = einsum('ijkl,l->ijk', fltImage, rgbWeights);
|
|
break;
|
|
case 5:
|
|
grayFloat = einsum('ijklm,m->ijkl', fltImage, rgbWeights);
|
|
break;
|
|
case 6:
|
|
grayFloat = einsum('ijklmn,n->ijklm', fltImage, rgbWeights);
|
|
break;
|
|
default:
|
|
throw new Error('Not a valid tensor rank.');
|
|
}
|
|
grayFloat = expandDims(grayFloat, -1);
|
|
return cast(grayFloat, origDtype);
|
|
}
|
|
var rgbToGrayscale = /* @__PURE__ */ op({ rgbToGrayscale_: rgbToGrayscale_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Rotates the input image tensor counter-clockwise with an optional offset
|
|
* center of rotation. Currently available in the CPU, WebGL, and WASM backends.
|
|
*
|
|
* @param image 4d tensor of shape `[batch, imageHeight, imageWidth, depth]`.
|
|
* @param radians The amount of rotation.
|
|
* @param fillValue The value to fill in the empty space leftover
|
|
* after rotation. Can be either a single grayscale value (0-255), or an
|
|
* array of three numbers `[red, green, blue]` specifying the red, green,
|
|
* and blue channels. Defaults to `0` (black).
|
|
* @param center The center of rotation. Can be either a single value (0-1), or
|
|
* an array of two numbers `[centerX, centerY]`. Defaults to `0.5` (rotates
|
|
* the image around its center).
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function rotateWithOffset_(image, radians, fillValue, center) {
|
|
if (fillValue === void 0) { fillValue = 0; }
|
|
if (center === void 0) { center = 0.5; }
|
|
var $image = convertToTensor(image, 'image', 'rotateWithOffset', 'float32');
|
|
assert($image.rank === 4, function () { return 'Error in rotateWithOffset: image must be rank 4,' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
var inputs = { image: $image };
|
|
var attrs = { radians: radians, fillValue: fillValue, center: center };
|
|
var res = ENGINE.runKernel(RotateWithOffset, inputs, attrs);
|
|
return res;
|
|
}
|
|
var rotateWithOffset = /* @__PURE__ */ op({ rotateWithOffset_: rotateWithOffset_ });
|
|
|
|
/**
|
|
* @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 nonMaxSuppSanityCheck(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma) {
|
|
if (iouThreshold == null) {
|
|
iouThreshold = 0.5;
|
|
}
|
|
if (scoreThreshold == null) {
|
|
scoreThreshold = Number.NEGATIVE_INFINITY;
|
|
}
|
|
if (softNmsSigma == null) {
|
|
softNmsSigma = 0.0;
|
|
}
|
|
var numBoxes = boxes.shape[0];
|
|
maxOutputSize = Math.min(maxOutputSize, numBoxes);
|
|
assert(0 <= iouThreshold && iouThreshold <= 1, function () { return "iouThreshold must be in [0, 1], but was '".concat(iouThreshold, "'"); });
|
|
assert(boxes.rank === 2, function () { return "boxes must be a 2D tensor, but was of rank '".concat(boxes.rank, "'"); });
|
|
assert(boxes.shape[1] === 4, function () { return "boxes must have 4 columns, but 2nd dimension was ".concat(boxes.shape[1]); });
|
|
assert(scores.rank === 1, function () { return 'scores must be a 1D tensor'; });
|
|
assert(scores.shape[0] === numBoxes, function () { return "scores has incompatible shape with boxes. Expected ".concat(numBoxes, ", ") +
|
|
"but was ".concat(scores.shape[0]); });
|
|
assert(0 <= softNmsSigma && softNmsSigma <= 1, function () { return "softNmsSigma must be in [0, 1], but was '".concat(softNmsSigma, "'"); });
|
|
return { maxOutputSize: maxOutputSize, iouThreshold: iouThreshold, scoreThreshold: scoreThreshold, softNmsSigma: softNmsSigma };
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union).
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @return A 1D tensor with the selected box indices.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppression_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
var $boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppression', 'float32');
|
|
var $scores = convertToTensor(scores, 'scores', 'nonMaxSuppression', 'float32');
|
|
var inputs = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold);
|
|
maxOutputSize = inputs.maxOutputSize;
|
|
iouThreshold = inputs.iouThreshold;
|
|
scoreThreshold = inputs.scoreThreshold;
|
|
var attrs = { maxOutputSize: maxOutputSize, iouThreshold: iouThreshold, scoreThreshold: scoreThreshold };
|
|
return ENGINE.runKernel(NonMaxSuppressionV3, { boxes: $boxes, scores: $scores }, attrs);
|
|
}
|
|
var nonMaxSuppression = /* @__PURE__ */ op({ nonMaxSuppression_: nonMaxSuppression_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Inserts a value into a sorted array. This method allows duplicate, meaning it
|
|
* allows inserting duplicate value, in which case, the element will be inserted
|
|
* at the lowest index of the value.
|
|
* @param arr The array to modify.
|
|
* @param element The element to insert.
|
|
* @param comparator Optional. If no comparator is specified, elements are
|
|
* compared using array_util.defaultComparator, which is suitable for Strings
|
|
* and Numbers in ascending arrays. If the array contains multiple instances of
|
|
* the target value, the left-most instance will be returned. To provide a
|
|
* comparator, it should take 2 arguments to compare and return a negative,
|
|
* zero, or a positive number.
|
|
*/
|
|
function binaryInsert(arr, element, comparator) {
|
|
var index = binarySearch(arr, element, comparator);
|
|
var insertionPoint = index < 0 ? -(index + 1) : index;
|
|
arr.splice(insertionPoint, 0, element);
|
|
}
|
|
/**
|
|
* Searches the array for the target using binary search, returns the index
|
|
* of the found element, or position to insert if element not found. If no
|
|
* comparator is specified, elements are compared using array_
|
|
* util.defaultComparator, which is suitable for Strings and Numbers in
|
|
* ascending arrays. If the array contains multiple instances of the target
|
|
* value, the left-most instance will be returned.
|
|
* @param arr The array to be searched in.
|
|
* @param target The target to be searched for.
|
|
* @param comparator Should take 2 arguments to compare and return a negative,
|
|
* zero, or a positive number.
|
|
* @return Lowest index of the target value if found, otherwise the insertion
|
|
* point where the target should be inserted, in the form of
|
|
* (-insertionPoint - 1).
|
|
*/
|
|
function binarySearch(arr, target, comparator) {
|
|
return binarySearch_(arr, target, comparator || defaultComparator);
|
|
}
|
|
/**
|
|
* Compares its two arguments for order.
|
|
* @param a The first element to be compared.
|
|
* @param b The second element to be compared.
|
|
* @return A negative number, zero, or a positive number as the first
|
|
* argument is less than, equal to, or greater than the second.
|
|
*/
|
|
function defaultComparator(a, b) {
|
|
return a > b ? 1 : a < b ? -1 : 0;
|
|
}
|
|
function binarySearch_(arr, target, comparator) {
|
|
var left = 0;
|
|
var right = arr.length;
|
|
var middle = 0;
|
|
var found = false;
|
|
while (left < right) {
|
|
middle = left + ((right - left) >>> 1);
|
|
var compareResult = comparator(target, arr[middle]);
|
|
if (compareResult > 0) {
|
|
left = middle + 1;
|
|
}
|
|
else {
|
|
right = middle;
|
|
// If compareResult is 0, the value is found. We record it is found,
|
|
// and then keep looking because there may be duplicate.
|
|
found = !compareResult;
|
|
}
|
|
}
|
|
return found ? left : -left - 1;
|
|
}
|
|
|
|
function nonMaxSuppressionV3Impl(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) {
|
|
return nonMaxSuppressionImpl_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, 0 /* softNmsSigma */);
|
|
}
|
|
function nonMaxSuppressionV4Impl(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, padToMaxOutputSize) {
|
|
return nonMaxSuppressionImpl_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, 0 /* softNmsSigma */, false /* returnScoresTensor */, padToMaxOutputSize /* padToMaxOutputSize */, true
|
|
/* returnValidOutputs */ );
|
|
}
|
|
function nonMaxSuppressionV5Impl(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma) {
|
|
return nonMaxSuppressionImpl_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma, true /* returnScoresTensor */);
|
|
}
|
|
function nonMaxSuppressionImpl_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma, returnScoresTensor, padToMaxOutputSize, returnValidOutputs) {
|
|
if (returnScoresTensor === void 0) { returnScoresTensor = false; }
|
|
if (padToMaxOutputSize === void 0) { padToMaxOutputSize = false; }
|
|
if (returnValidOutputs === void 0) { returnValidOutputs = false; }
|
|
// The list is sorted in ascending order, so that we can always pop the
|
|
// candidate with the largest score in O(1) time.
|
|
var candidates = [];
|
|
for (var i = 0; i < scores.length; i++) {
|
|
if (scores[i] > scoreThreshold) {
|
|
candidates.push({ score: scores[i], boxIndex: i, suppressBeginIndex: 0 });
|
|
}
|
|
}
|
|
candidates.sort(ascendingComparator);
|
|
// If softNmsSigma is 0, the outcome of this algorithm is exactly same as
|
|
// before.
|
|
var scale = softNmsSigma > 0 ? (-0.5 / softNmsSigma) : 0.0;
|
|
var selectedIndices = [];
|
|
var selectedScores = [];
|
|
while (selectedIndices.length < maxOutputSize && candidates.length > 0) {
|
|
var candidate = candidates.pop();
|
|
var originalScore = candidate.score, boxIndex = candidate.boxIndex, suppressBeginIndex = candidate.suppressBeginIndex;
|
|
if (originalScore < scoreThreshold) {
|
|
break;
|
|
}
|
|
// Overlapping boxes are likely to have similar scores, therefore we
|
|
// iterate through the previously selected boxes backwards in order to
|
|
// see if candidate's score should be suppressed. We use
|
|
// suppressBeginIndex to track and ensure a candidate can be suppressed
|
|
// by a selected box no more than once. Also, if the overlap exceeds
|
|
// iouThreshold, we simply ignore the candidate.
|
|
var ignoreCandidate = false;
|
|
for (var j = selectedIndices.length - 1; j >= suppressBeginIndex; --j) {
|
|
var iou = intersectionOverUnion(boxes, boxIndex, selectedIndices[j]);
|
|
if (iou >= iouThreshold) {
|
|
ignoreCandidate = true;
|
|
break;
|
|
}
|
|
candidate.score =
|
|
candidate.score * suppressWeight(iouThreshold, scale, iou);
|
|
if (candidate.score <= scoreThreshold) {
|
|
break;
|
|
}
|
|
}
|
|
// At this point, if `candidate.score` has not dropped below
|
|
// `scoreThreshold`, then we know that we went through all of the
|
|
// previous selections and can safely update `suppressBeginIndex` to the
|
|
// end of the selected array. Then we can re-insert the candidate with
|
|
// the updated score and suppressBeginIndex back in the candidate list.
|
|
// If on the other hand, `candidate.score` has dropped below the score
|
|
// threshold, we will not add it back to the candidates list.
|
|
candidate.suppressBeginIndex = selectedIndices.length;
|
|
if (!ignoreCandidate) {
|
|
// Candidate has passed all the tests, and is not suppressed, so
|
|
// select the candidate.
|
|
if (candidate.score === originalScore) {
|
|
selectedIndices.push(boxIndex);
|
|
selectedScores.push(candidate.score);
|
|
}
|
|
else if (candidate.score > scoreThreshold) {
|
|
// Candidate's score is suppressed but is still high enough to be
|
|
// considered, so add back to the candidates list.
|
|
binaryInsert(candidates, candidate, ascendingComparator);
|
|
}
|
|
}
|
|
}
|
|
// NonMaxSuppressionV4 feature: padding output to maxOutputSize.
|
|
var validOutputs = selectedIndices.length;
|
|
var elemsToPad = maxOutputSize - validOutputs;
|
|
if (padToMaxOutputSize && elemsToPad > 0) {
|
|
selectedIndices.push.apply(selectedIndices, __spreadArray([], __read(new Array(elemsToPad).fill(0)), false));
|
|
selectedScores.push.apply(selectedScores, __spreadArray([], __read(new Array(elemsToPad).fill(0.0)), false));
|
|
}
|
|
var result = { selectedIndices: selectedIndices };
|
|
if (returnScoresTensor) {
|
|
result['selectedScores'] = selectedScores;
|
|
}
|
|
if (returnValidOutputs) {
|
|
result['validOutputs'] = validOutputs;
|
|
}
|
|
return result;
|
|
}
|
|
function intersectionOverUnion(boxes, i, j) {
|
|
var iCoord = boxes.subarray(i * 4, i * 4 + 4);
|
|
var jCoord = boxes.subarray(j * 4, j * 4 + 4);
|
|
var yminI = Math.min(iCoord[0], iCoord[2]);
|
|
var xminI = Math.min(iCoord[1], iCoord[3]);
|
|
var ymaxI = Math.max(iCoord[0], iCoord[2]);
|
|
var xmaxI = Math.max(iCoord[1], iCoord[3]);
|
|
var yminJ = Math.min(jCoord[0], jCoord[2]);
|
|
var xminJ = Math.min(jCoord[1], jCoord[3]);
|
|
var ymaxJ = Math.max(jCoord[0], jCoord[2]);
|
|
var xmaxJ = Math.max(jCoord[1], jCoord[3]);
|
|
var areaI = (ymaxI - yminI) * (xmaxI - xminI);
|
|
var areaJ = (ymaxJ - yminJ) * (xmaxJ - xminJ);
|
|
if (areaI <= 0 || areaJ <= 0) {
|
|
return 0.0;
|
|
}
|
|
var intersectionYmin = Math.max(yminI, yminJ);
|
|
var intersectionXmin = Math.max(xminI, xminJ);
|
|
var intersectionYmax = Math.min(ymaxI, ymaxJ);
|
|
var intersectionXmax = Math.min(xmaxI, xmaxJ);
|
|
var intersectionArea = Math.max(intersectionYmax - intersectionYmin, 0.0) *
|
|
Math.max(intersectionXmax - intersectionXmin, 0.0);
|
|
return intersectionArea / (areaI + areaJ - intersectionArea);
|
|
}
|
|
// A Gaussian penalty function, this method always returns values in [0, 1].
|
|
// The weight is a function of similarity, the more overlap two boxes are, the
|
|
// smaller the weight is, meaning highly overlapping boxe will be significantly
|
|
// penalized. On the other hand, a non-overlapping box will not be penalized.
|
|
function suppressWeight(iouThreshold, scale, iou) {
|
|
var weight = Math.exp(scale * iou * iou);
|
|
return iou <= iouThreshold ? weight : 0.0;
|
|
}
|
|
function ascendingComparator(c1, c2) {
|
|
// For objects with same scores, we make the object with the larger index go
|
|
// first. In an array that pops from the end, this means that the object with
|
|
// the smaller index will be popped first. This ensures the same output as
|
|
// the TensorFlow python version.
|
|
return (c1.score - c2.score) ||
|
|
((c1.score === c2.score) && (c2.boxIndex - c1.boxIndex));
|
|
}
|
|
|
|
/**
|
|
* Performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union).
|
|
*
|
|
* This is the async version of `nonMaxSuppression`
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @return A 1D tensor with the selected box indices.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppressionAsync_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $boxes, $scores, inputs, boxesAndScores, boxesVals, scoresVals, selectedIndices;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
$boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppressionAsync');
|
|
$scores = convertToTensor(scores, 'scores', 'nonMaxSuppressionAsync');
|
|
inputs = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold);
|
|
maxOutputSize = inputs.maxOutputSize;
|
|
iouThreshold = inputs.iouThreshold;
|
|
scoreThreshold = inputs.scoreThreshold;
|
|
return [4 /*yield*/, Promise.all([$boxes.data(), $scores.data()])];
|
|
case 1:
|
|
boxesAndScores = _a.sent();
|
|
boxesVals = boxesAndScores[0];
|
|
scoresVals = boxesAndScores[1];
|
|
selectedIndices = nonMaxSuppressionV3Impl(boxesVals, scoresVals, maxOutputSize, iouThreshold, scoreThreshold).selectedIndices;
|
|
if ($boxes !== boxes) {
|
|
$boxes.dispose();
|
|
}
|
|
if ($scores !== scores) {
|
|
$scores.dispose();
|
|
}
|
|
return [2 /*return*/, tensor1d(selectedIndices, 'int32')];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var nonMaxSuppressionAsync = nonMaxSuppressionAsync_;
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union).
|
|
*
|
|
* This op also supports a Soft-NMS mode (cf.
|
|
* Bodla et al, https://arxiv.org/abs/1704.04503) where boxes reduce the score
|
|
* of other overlapping boxes, therefore favoring different regions of the image
|
|
* with high scores. To enable this Soft-NMS mode, set the `softNmsSigma`
|
|
* parameter to be larger than 0.
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @param softNmsSigma A float representing the sigma parameter for Soft NMS.
|
|
* When sigma is 0, it falls back to nonMaxSuppression.
|
|
* @return A map with the following properties:
|
|
* - selectedIndices: A 1D tensor with the selected box indices.
|
|
* - selectedScores: A 1D tensor with the corresponding scores for each
|
|
* selected box.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppressionWithScore_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
if (softNmsSigma === void 0) { softNmsSigma = 0.0; }
|
|
var $boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppression');
|
|
var $scores = convertToTensor(scores, 'scores', 'nonMaxSuppression');
|
|
var params = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma);
|
|
maxOutputSize = params.maxOutputSize;
|
|
iouThreshold = params.iouThreshold;
|
|
scoreThreshold = params.scoreThreshold;
|
|
softNmsSigma = params.softNmsSigma;
|
|
var inputs = { boxes: $boxes, scores: $scores };
|
|
var attrs = { maxOutputSize: maxOutputSize, iouThreshold: iouThreshold, scoreThreshold: scoreThreshold, softNmsSigma: softNmsSigma };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var result = ENGINE.runKernel(NonMaxSuppressionV5, inputs, attrs);
|
|
return { selectedIndices: result[0], selectedScores: result[1] };
|
|
}
|
|
var nonMaxSuppressionWithScore = /* @__PURE__ */ op({ nonMaxSuppressionWithScore_: nonMaxSuppressionWithScore_ });
|
|
|
|
/**
|
|
* Asynchronously performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union).
|
|
*
|
|
* This op also supports a Soft-NMS mode (cf.
|
|
* Bodla et al, https://arxiv.org/abs/1704.04503) where boxes reduce the score
|
|
* of other overlapping boxes, therefore favoring different regions of the image
|
|
* with high scores. To enable this Soft-NMS mode, set the `softNmsSigma`
|
|
* parameter to be larger than 0.
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @param softNmsSigma A float representing the sigma parameter for Soft NMS.
|
|
* When sigma is 0, it falls back to nonMaxSuppression.
|
|
* @return A map with the following properties:
|
|
* - selectedIndices: A 1D tensor with the selected box indices.
|
|
* - selectedScores: A 1D tensor with the corresponding scores for each
|
|
* selected box.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppressionWithScoreAsync_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
if (softNmsSigma === void 0) { softNmsSigma = 0.0; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $boxes, $scores, params, boxesAndScores, boxesVals, scoresVals, _a, selectedIndices, selectedScores;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
$boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppressionAsync');
|
|
$scores = convertToTensor(scores, 'scores', 'nonMaxSuppressionAsync');
|
|
params = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma);
|
|
maxOutputSize = params.maxOutputSize;
|
|
iouThreshold = params.iouThreshold;
|
|
scoreThreshold = params.scoreThreshold;
|
|
softNmsSigma = params.softNmsSigma;
|
|
return [4 /*yield*/, Promise.all([$boxes.data(), $scores.data()])];
|
|
case 1:
|
|
boxesAndScores = _b.sent();
|
|
boxesVals = boxesAndScores[0];
|
|
scoresVals = boxesAndScores[1];
|
|
_a = nonMaxSuppressionV5Impl(boxesVals, scoresVals, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma), selectedIndices = _a.selectedIndices, selectedScores = _a.selectedScores;
|
|
if ($boxes !== boxes) {
|
|
$boxes.dispose();
|
|
}
|
|
if ($scores !== scores) {
|
|
$scores.dispose();
|
|
}
|
|
return [2 /*return*/, {
|
|
selectedIndices: tensor1d(selectedIndices, 'int32'),
|
|
selectedScores: tensor1d(selectedScores)
|
|
}];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var nonMaxSuppressionWithScoreAsync = nonMaxSuppressionWithScoreAsync_;
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Asynchronously performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union), with an option to pad results.
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @param padToMaxOutputSize Defaults to false. If true, size of output
|
|
* `selectedIndices` is padded to maxOutputSize.
|
|
* @return A map with the following properties:
|
|
* - selectedIndices: A 1D tensor with the selected box indices.
|
|
* - validOutputs: A scalar denoting how many elements in `selectedIndices`
|
|
* are valid. Valid elements occur first, then padding.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppressionPadded_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, padToMaxOutputSize) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
if (padToMaxOutputSize === void 0) { padToMaxOutputSize = false; }
|
|
var $boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppression');
|
|
var $scores = convertToTensor(scores, 'scores', 'nonMaxSuppression');
|
|
var params = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold, null /* softNmsSigma */);
|
|
var $maxOutputSize = params.maxOutputSize;
|
|
var $iouThreshold = params.iouThreshold;
|
|
var $scoreThreshold = params.scoreThreshold;
|
|
var inputs = { boxes: $boxes, scores: $scores };
|
|
var attrs = {
|
|
maxOutputSize: $maxOutputSize,
|
|
iouThreshold: $iouThreshold,
|
|
scoreThreshold: $scoreThreshold,
|
|
padToMaxOutputSize: padToMaxOutputSize
|
|
};
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var result = ENGINE.runKernel(NonMaxSuppressionV4, inputs, attrs);
|
|
return { selectedIndices: result[0], validOutputs: result[1] };
|
|
}
|
|
var nonMaxSuppressionPadded = /* @__PURE__ */ op({ nonMaxSuppressionPadded_: nonMaxSuppressionPadded_ });
|
|
|
|
/**
|
|
* Asynchronously performs non maximum suppression of bounding boxes based on
|
|
* iou (intersection over union), with an option to pad results.
|
|
*
|
|
* @param boxes a 2d tensor of shape `[numBoxes, 4]`. Each entry is
|
|
* `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the corners of
|
|
* the bounding box.
|
|
* @param scores a 1d tensor providing the box scores of shape `[numBoxes]`.
|
|
* @param maxOutputSize The maximum number of boxes to be selected.
|
|
* @param iouThreshold A float representing the threshold for deciding whether
|
|
* boxes overlap too much with respect to IOU. Must be between [0, 1].
|
|
* Defaults to 0.5 (50% box overlap).
|
|
* @param scoreThreshold A threshold for deciding when to remove boxes based
|
|
* on score. Defaults to -inf, which means any score is accepted.
|
|
* @param padToMaxOutputSize Defaults to false. If true, size of output
|
|
* `selectedIndices` is padded to maxOutputSize.
|
|
* @return A map with the following properties:
|
|
* - selectedIndices: A 1D tensor with the selected box indices.
|
|
* - validOutputs: A scalar denoting how many elements in `selectedIndices`
|
|
* are valid. Valid elements occur first, then padding.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function nonMaxSuppressionPaddedAsync_(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, padToMaxOutputSize) {
|
|
if (iouThreshold === void 0) { iouThreshold = 0.5; }
|
|
if (scoreThreshold === void 0) { scoreThreshold = Number.NEGATIVE_INFINITY; }
|
|
if (padToMaxOutputSize === void 0) { padToMaxOutputSize = false; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $boxes, $scores, params, $maxOutputSize, $iouThreshold, $scoreThreshold, _a, boxesVals, scoresVals, _b, selectedIndices, validOutputs;
|
|
return __generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
$boxes = convertToTensor(boxes, 'boxes', 'nonMaxSuppressionAsync');
|
|
$scores = convertToTensor(scores, 'scores', 'nonMaxSuppressionAsync');
|
|
params = nonMaxSuppSanityCheck($boxes, $scores, maxOutputSize, iouThreshold, scoreThreshold, null /* softNmsSigma */);
|
|
$maxOutputSize = params.maxOutputSize;
|
|
$iouThreshold = params.iouThreshold;
|
|
$scoreThreshold = params.scoreThreshold;
|
|
return [4 /*yield*/, Promise.all([$boxes.data(), $scores.data()])];
|
|
case 1:
|
|
_a = __read.apply(void 0, [_c.sent(), 2]), boxesVals = _a[0], scoresVals = _a[1];
|
|
_b = nonMaxSuppressionV4Impl(boxesVals, scoresVals, $maxOutputSize, $iouThreshold, $scoreThreshold, padToMaxOutputSize), selectedIndices = _b.selectedIndices, validOutputs = _b.validOutputs;
|
|
if ($boxes !== boxes) {
|
|
$boxes.dispose();
|
|
}
|
|
if ($scores !== scores) {
|
|
$scores.dispose();
|
|
}
|
|
return [2 /*return*/, {
|
|
selectedIndices: tensor1d(selectedIndices, 'int32'),
|
|
validOutputs: scalar(validOutputs, 'int32')
|
|
}];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
var nonMaxSuppressionPaddedAsync = nonMaxSuppressionPaddedAsync_;
|
|
|
|
/**
|
|
* Bilinear resize a single 3D image or a batch of 3D images to a new shape.
|
|
*
|
|
* @param images The images, of rank 4 or rank 3, of shape
|
|
* `[batch, height, width, inChannels]`. If rank 3, batch of 1 is assumed.
|
|
* @param size The new shape `[newHeight, newWidth]` to resize the
|
|
* images to. Each channel is resized individually.
|
|
* @param alignCorners Defaults to `false`. If true, rescale
|
|
* input by `(new_height - 1) / (height - 1)`, which exactly aligns the 4
|
|
* corners of images and resized images. If false, rescale by
|
|
* `new_height / height`. Treat similarly the width dimension.
|
|
* @param halfPixelCenters Defaults to `false`. Whether to assume pixel centers
|
|
* are at 0.5, which would make the floating point coordinates of the top
|
|
* left pixel 0.5, 0.5.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function resizeBilinear_(images, size, alignCorners, halfPixelCenters) {
|
|
if (alignCorners === void 0) { alignCorners = false; }
|
|
if (halfPixelCenters === void 0) { halfPixelCenters = false; }
|
|
var $images = convertToTensor(images, 'images', 'resizeBilinear');
|
|
assert($images.rank === 3 || $images.rank === 4, function () { return "Error in resizeBilinear: x must be rank 3 or 4, but got " +
|
|
"rank ".concat($images.rank, "."); });
|
|
assert(size.length === 2, function () { return "Error in resizeBilinear: new shape must 2D, but got shape " +
|
|
"".concat(size, "."); });
|
|
assert(halfPixelCenters === false || alignCorners === false, function () { return "Error in resizeBilinear: If halfPixelCenters is true, " +
|
|
"alignCorners must be false."; });
|
|
var batchImages = $images;
|
|
var reshapedTo4D = false;
|
|
if ($images.rank === 3) {
|
|
reshapedTo4D = true;
|
|
batchImages = reshape($images, [1, $images.shape[0], $images.shape[1], $images.shape[2]]);
|
|
}
|
|
__read(size, 0);
|
|
var inputs = { images: batchImages };
|
|
var attrs = { alignCorners: alignCorners, halfPixelCenters: halfPixelCenters, size: size };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(ResizeBilinear, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var resizeBilinear = /* @__PURE__ */ op({ resizeBilinear_: resizeBilinear_ });
|
|
|
|
/**
|
|
* NearestNeighbor resize a batch of 3D images to a new shape.
|
|
*
|
|
* @param images The images, of rank 4 or rank 3, of shape
|
|
* `[batch, height, width, inChannels]`. If rank 3, batch of 1 is assumed.
|
|
* @param size The new shape `[newHeight, newWidth]` to resize the
|
|
* images to. Each channel is resized individually.
|
|
* @param alignCorners Defaults to False. If true, rescale
|
|
* input by `(new_height - 1) / (height - 1)`, which exactly aligns the 4
|
|
* corners of images and resized images. If false, rescale by
|
|
* `new_height / height`. Treat similarly the width dimension.
|
|
* @param halfPixelCenters Defaults to `false`. Whether to assume pixels are of
|
|
* half the actual dimensions, and yield more accurate resizes. This flag
|
|
* would also make the floating point coordinates of the top left pixel
|
|
* 0.5, 0.5.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function resizeNearestNeighbor_(images, size, alignCorners, halfPixelCenters) {
|
|
if (alignCorners === void 0) { alignCorners = false; }
|
|
if (halfPixelCenters === void 0) { halfPixelCenters = false; }
|
|
var $images = convertToTensor(images, 'images', 'resizeNearestNeighbor');
|
|
assert($images.rank === 3 || $images.rank === 4, function () { return "Error in resizeNearestNeighbor: x must be rank 3 or 4, but got " +
|
|
"rank ".concat($images.rank, "."); });
|
|
assert(size.length === 2, function () { return "Error in resizeNearestNeighbor: new shape must 2D, but got shape " +
|
|
"".concat(size, "."); });
|
|
assert($images.dtype === 'float32' || $images.dtype === 'int32', function () { return '`images` must have `int32` or `float32` as dtype'; });
|
|
assert(halfPixelCenters === false || alignCorners === false, function () { return "Error in resizeNearestNeighbor: If halfPixelCenters is true, " +
|
|
"alignCorners must be false."; });
|
|
var batchImages = $images;
|
|
var reshapedTo4D = false;
|
|
if ($images.rank === 3) {
|
|
reshapedTo4D = true;
|
|
batchImages = reshape($images, [1, $images.shape[0], $images.shape[1], $images.shape[2]]);
|
|
}
|
|
__read(size, 0);
|
|
var inputs = { images: batchImages };
|
|
var attrs = { alignCorners: alignCorners, halfPixelCenters: halfPixelCenters, size: size };
|
|
// tslint:disable-next-line: no-unnecessary-type-assertion
|
|
var res = ENGINE.runKernel(ResizeNearestNeighbor, inputs, attrs);
|
|
if (reshapedTo4D) {
|
|
return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
|
}
|
|
return res;
|
|
}
|
|
var resizeNearestNeighbor = /* @__PURE__ */ op({ resizeNearestNeighbor_: resizeNearestNeighbor_ });
|
|
|
|
/**
|
|
* Performs image binarization with corresponding threshold
|
|
* (depends on the method)value, which creates a binary image from a grayscale.
|
|
* @param image 3d tensor of shape [imageHeight,imageWidth, depth],
|
|
* where imageHeight and imageWidth must be positive.The image color
|
|
* range should be [0, 255].
|
|
* @param method Optional string from `'binary' | 'otsu'`
|
|
* which specifies the method for thresholding. Defaults to 'binary'.
|
|
* @param inverted Optional boolean whichspecifies
|
|
* if colours should be inverted. Defaults to false.
|
|
* @param threshValue Optional number which defines threshold value from 0 to 1.
|
|
* Defaults to 0.5.
|
|
* @return A 3d tensor of shape [imageHeight,imageWidth, depth], which
|
|
* contains binarized image.
|
|
*/
|
|
function threshold_(image, method, inverted, threshValue) {
|
|
var _a;
|
|
if (method === void 0) { method = 'binary'; }
|
|
if (inverted === void 0) { inverted = false; }
|
|
if (threshValue === void 0) { threshValue = 0.5; }
|
|
var $image = convertToTensor(image, 'image', 'threshold');
|
|
/* 0.2989, 0.5870, 0.1140 are represent luma coefficients in CCIR601.
|
|
Reference for converting between RGB and grayscale: https://en.wikipedia.org/wiki/Luma_%28video%29 */
|
|
var RED_INTENCITY_COEF = 0.2989;
|
|
var GREEN_INTENCITY_COEF = 0.5870;
|
|
var BLUE_INTENCITY_COEF = 0.1140;
|
|
var totalPixelsInImage = $image.shape[0] * $image.shape[1];
|
|
var $threshold = mul(tensor1d([threshValue]), 255);
|
|
var r, g, b, grayscale;
|
|
assert($image.rank === 3, function () { return 'Error in threshold: image must be rank 3,' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
assert($image.shape[2] === 3 || $image.shape[2] === 1, function () { return 'Error in threshold: ' +
|
|
'image color channel must be equal to 3 or 1' +
|
|
"but got ".concat($image.shape[2], "."); });
|
|
assert($image.dtype === 'int32' || $image.dtype === 'float32', function () { return 'Error in dtype: image dtype must be int32 or float32,' +
|
|
"but got dtype ".concat($image.dtype, "."); });
|
|
assert(method === 'otsu' || method === 'binary', function () { return "Method must be binary or otsu, but was ".concat(method); });
|
|
if ($image.shape[2] === 3) {
|
|
_a = __read(split$1($image, [1, 1, 1], -1), 3), r = _a[0], g = _a[1], b = _a[2];
|
|
var $r = mul(r, RED_INTENCITY_COEF);
|
|
var $g = mul(g, GREEN_INTENCITY_COEF);
|
|
var $b = mul(b, BLUE_INTENCITY_COEF);
|
|
grayscale = add(add($r, $g), $b);
|
|
}
|
|
else {
|
|
grayscale = image;
|
|
}
|
|
if (method === 'otsu') {
|
|
var $histogram = bincount(cast(round(grayscale), 'int32'), tensor([]), 256);
|
|
$threshold = otsu($histogram, totalPixelsInImage);
|
|
}
|
|
var invCondition = inverted ?
|
|
lessEqual(grayscale, $threshold) : greater(grayscale, $threshold);
|
|
var result = cast(mul(invCondition, 255), 'int32');
|
|
return result;
|
|
}
|
|
function otsu(histogram, total) {
|
|
var bestThresh = tensor1d([-1]);
|
|
var bestInBetVar = tensor1d([0]);
|
|
var cInBetVar = tensor1d([0]);
|
|
var classFirst, classSecond, meanFirst, meanSec, weightForeground, weightBack;
|
|
for (var index = 0; index < histogram.size - 1; index++) {
|
|
classFirst = slice(histogram, 0, index + 1);
|
|
classSecond = slice(histogram, index + 1);
|
|
weightForeground = div(sum(classFirst), total);
|
|
weightBack = div(sum(classSecond), total);
|
|
var meanFirstDivA = sum(mul(classFirst, range(0, classFirst.size)));
|
|
meanFirst = div(meanFirstDivA, sum(classFirst));
|
|
var meanSecFill = fill(classSecond.shape, classFirst.size);
|
|
var meanSecAdd = add(range(0, classSecond.size), meanSecFill);
|
|
var meanSecMul = mul(classSecond, (meanSecAdd));
|
|
meanSec = div(sum(meanSecMul), sum(classSecond));
|
|
var cInBetVarSubA = sub(meanFirst, meanSec);
|
|
var cInBetVarSubB = sub(meanFirst, meanSec);
|
|
var cInBetVarMul = mul(weightForeground, weightBack);
|
|
cInBetVar = mul(mul(cInBetVarMul, cInBetVarSubA), cInBetVarSubB);
|
|
var condition = greater(cInBetVar, bestInBetVar);
|
|
bestInBetVar = where(condition, cInBetVar, bestInBetVar);
|
|
bestThresh = where(condition, tensor1d([index]), bestThresh);
|
|
}
|
|
return bestThresh;
|
|
}
|
|
var threshold = /* @__PURE__ */ op({ threshold_: threshold_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Applies the given transform(s) to the image(s).
|
|
*
|
|
* @param image 4d tensor of shape `[batch, imageHeight, imageWidth, depth]`.
|
|
* @param transforms Projective transform matrix/matrices. A tensor1d of length
|
|
* 8 or tensor of size N x 8. If one row of transforms is [a0, a1, a2, b0,
|
|
* b1, b2, c0, c1], then it maps the output point (x, y) to a transformed
|
|
* input point (x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k),
|
|
* where k = c0 x + c1 y + 1. The transforms are inverted compared to the
|
|
* transform mapping input points to output points.
|
|
* @param interpolation Interpolation mode.
|
|
* Supported values: 'nearest', 'bilinear'. Default to 'nearest'.
|
|
* @param fillMode Points outside the boundaries of the input are filled
|
|
* according to the given mode, one of 'constant', 'reflect', 'wrap',
|
|
* 'nearest'. Default to 'constant'.
|
|
* 'reflect': (d c b a | a b c d | d c b a ) The input is extended by
|
|
* reflecting about the edge of the last pixel.
|
|
* 'constant': (k k k k | a b c d | k k k k) The input is extended by
|
|
* filling all values beyond the edge with the same constant value k.
|
|
* 'wrap': (a b c d | a b c d | a b c d) The input is extended by
|
|
* wrapping around to the opposite edge.
|
|
* 'nearest': (a a a a | a b c d | d d d d) The input is extended by
|
|
* the nearest pixel.
|
|
* @param fillValue A float represents the value to be filled outside the
|
|
* boundaries when fillMode is 'constant'.
|
|
* @param Output dimension after the transform, [height, width]. If undefined,
|
|
* output is the same size as input image.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Images', namespace: 'image'}
|
|
*/
|
|
function transform_(image, transforms, interpolation, fillMode, fillValue, outputShape) {
|
|
if (interpolation === void 0) { interpolation = 'nearest'; }
|
|
if (fillMode === void 0) { fillMode = 'constant'; }
|
|
if (fillValue === void 0) { fillValue = 0; }
|
|
var $image = convertToTensor(image, 'image', 'transform', 'float32');
|
|
var $transforms = convertToTensor(transforms, 'transforms', 'transform', 'float32');
|
|
assert($image.rank === 4, function () { return 'Error in transform: image must be rank 4,' +
|
|
"but got rank ".concat($image.rank, "."); });
|
|
assert($transforms.rank === 2 &&
|
|
($transforms.shape[0] === $image.shape[0] ||
|
|
$transforms.shape[0] === 1) &&
|
|
$transforms.shape[1] === 8, function () { return "Error in transform: Input transform should be batch x 8 or 1 x 8"; });
|
|
assert(outputShape == null || outputShape.length === 2, function () { return 'Error in transform: outputShape must be [height, width] or null, ' +
|
|
"but got ".concat(outputShape, "."); });
|
|
var inputs = { image: $image, transforms: $transforms };
|
|
var attrs = { interpolation: interpolation, fillMode: fillMode, fillValue: fillValue, outputShape: outputShape };
|
|
return ENGINE.runKernel(Transform, inputs, attrs);
|
|
}
|
|
var transform = /* @__PURE__ */ op({ transform_: transform_ });
|
|
|
|
/**
|
|
* Copy a tensor setting everything outside a central band in each innermost
|
|
* matrix to zero.
|
|
*
|
|
* The band part is computed as follows: Assume input has `k` dimensions
|
|
* `[I, J, K, ..., M, N]`, then the output is a tensor with the same shape where
|
|
* `band[i, j, k, ..., m, n] = in_band(m, n) * input[i, j, k, ..., m, n]`.
|
|
* The indicator function
|
|
* `in_band(m, n) = (num_lower < 0 || (m-n) <= num_lower)`
|
|
* `&& (num_upper < 0 || (n-m) <= num_upper)`
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([[ 0, 1, 2, 3],
|
|
* [-1, 0, 1, 2],
|
|
* [-2, -1, 0, 1],
|
|
* [-3, -2, -1, 0]]);
|
|
* let y = tf.linalg.bandPart(x, 1, -1);
|
|
* y.print(); // [[ 0, 1, 2, 3],
|
|
* // [-1, 0, 1, 2],
|
|
* // [ 0, -1, 0, 1],
|
|
* // [ 0, 0 , -1, 0]]
|
|
* let z = tf.linalg.bandPart(x, 2, 1);
|
|
* z.print(); // [[ 0, 1, 0, 0],
|
|
* // [-1, 0, 1, 0],
|
|
* // [-2, -1, 0, 1],
|
|
* // [ 0, -2, -1, 0]]
|
|
* ```
|
|
*
|
|
* @param x Rank `k` tensor
|
|
* @param numLower Number of subdiagonals to keep.
|
|
* If negative, keep entire lower triangle.
|
|
* @param numUpper Number of subdiagonals to keep.
|
|
* If negative, keep entire upper triangle.
|
|
* @returns Rank `k` tensor of the same shape as input.
|
|
* The extracted banded tensor.
|
|
*
|
|
* @doc {heading:'Operations', subheading:'Linear Algebra', namespace:'linalg'}
|
|
*/
|
|
function bandPart_(a, numLower, numUpper) {
|
|
var $a = convertToTensor(a, 'a', 'bandPart');
|
|
assert($a.rank >= 2, function () { return "bandPart(): Rank must be at least 2, got ".concat($a.rank, "."); });
|
|
var shape = $a.shape;
|
|
var _a = __read($a.shape.slice(-2), 2), M = _a[0], N = _a[1];
|
|
var $numLower;
|
|
var $numUpper;
|
|
if (typeof numLower === 'number') {
|
|
assert(numLower % 1 === 0, function () { return "bandPart(): numLower must be an integer, got ".concat(numLower, "."); });
|
|
assert(numLower <= M, function () { return "bandPart(): numLower (".concat(numLower, ")") +
|
|
" must not be greater than the number of rows (".concat(M, ")."); });
|
|
$numLower =
|
|
convertToTensor(numLower < 0 ? M : numLower, 'numLower', 'bandPart');
|
|
}
|
|
else {
|
|
assert(numLower.dtype === 'int32', function () { return "bandPart(): numLower's dtype must be an int32."; });
|
|
// If numLower is a Scalar, checking `numLower <= M` could hurt performance,
|
|
// but minimum(numLower, M) could avoid unexpected results.
|
|
$numLower = where(less(numLower, 0), M, minimum(numLower, M));
|
|
}
|
|
if (typeof numUpper === 'number') {
|
|
assert(numUpper % 1 === 0, function () { return "bandPart(): numUpper must be an integer, got ".concat(numUpper, "."); });
|
|
assert(numUpper <= N, function () { return "bandPart(): numUpper (".concat(numUpper, ")") +
|
|
" must not be greater than the number of columns (".concat(N, ")."); });
|
|
$numUpper =
|
|
convertToTensor(numUpper < 0 ? N : numUpper, 'numUpper', 'bandPart');
|
|
}
|
|
else {
|
|
assert(numUpper.dtype === 'int32', function () { return "bandPart(): numUpper's dtype must be an int32."; });
|
|
$numUpper = where(less(numUpper, 0), N, minimum(numUpper, N));
|
|
}
|
|
var i = reshape(range(0, M, 1, 'int32'), [-1, 1]);
|
|
var j = range(0, N, 1, 'int32');
|
|
var ij = sub(i, j);
|
|
var inBand = logicalAnd(lessEqual(ij, $numLower), greaterEqual(ij, neg($numUpper)));
|
|
var zero = zeros([M, N], $a.dtype);
|
|
return reshape(stack(unstack(reshape($a, [-1, M, N]))
|
|
.map(function (mat) { return where(inBand, mat, zero); })), shape);
|
|
}
|
|
var bandPart = /* @__PURE__ */ op({ bandPart_: bandPart_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Gram-Schmidt orthogonalization.
|
|
*
|
|
* ```js
|
|
* const x = tf.tensor2d([[1, 2], [3, 4]]);
|
|
* let y = tf.linalg.gramSchmidt(x);
|
|
* y.print();
|
|
* console.log('Orthogonalized:');
|
|
* y.dot(y.transpose()).print(); // should be nearly the identity matrix.
|
|
* console.log('First row direction maintained:');
|
|
* const data = await y.array();
|
|
* console.log(data[0][1] / data[0][0]); // should be nearly 2.
|
|
* ```
|
|
*
|
|
* @param xs The vectors to be orthogonalized, in one of the two following
|
|
* formats:
|
|
* - An Array of `tf.Tensor1D`.
|
|
* - A `tf.Tensor2D`, i.e., a matrix, in which case the vectors are the rows
|
|
* of `xs`.
|
|
* In each case, all the vectors must have the same length and the length
|
|
* must be greater than or equal to the number of vectors.
|
|
* @returns The orthogonalized and normalized vectors or matrix.
|
|
* Orthogonalization means that the vectors or the rows of the matrix
|
|
* are orthogonal (zero inner products). Normalization means that each
|
|
* vector or each row of the matrix has an L2 norm that equals `1`.
|
|
*
|
|
* @doc {heading:'Operations', subheading:'Linear Algebra', namespace:'linalg'}
|
|
*/
|
|
function gramSchmidt_(xs) {
|
|
var inputIsTensor2D;
|
|
if (Array.isArray(xs)) {
|
|
inputIsTensor2D = false;
|
|
assert(xs != null && xs.length > 0, function () { return 'Gram-Schmidt process: input must not be null, undefined, or ' +
|
|
'empty'; });
|
|
var dim_1 = xs[0].shape[0];
|
|
var _loop_1 = function (i) {
|
|
assert(xs[i].shape[0] === dim_1, function () { return 'Gram-Schmidt: Non-unique lengths found in the input vectors: ' +
|
|
"(".concat(xs[i].shape[0], " vs. ").concat(dim_1, ")"); });
|
|
};
|
|
for (var i = 1; i < xs.length; ++i) {
|
|
_loop_1(i);
|
|
}
|
|
}
|
|
else {
|
|
inputIsTensor2D = true;
|
|
xs = split$1(xs, xs.shape[0], 0).map(function (x) { return squeeze(x, [0]); });
|
|
}
|
|
assert(xs.length <= xs[0].shape[0], function () { return "Gram-Schmidt: Number of vectors (".concat(xs.length, ") exceeds ") +
|
|
"number of dimensions (".concat(xs[0].shape[0], ")."); });
|
|
var ys = [];
|
|
var xs1d = xs;
|
|
var _loop_2 = function (i) {
|
|
ys.push(ENGINE.tidy(function () {
|
|
var x = xs1d[i];
|
|
if (i > 0) {
|
|
for (var j = 0; j < i; ++j) {
|
|
var proj = mul(sum(mul(ys[j], x)), ys[j]);
|
|
x = sub(x, proj);
|
|
}
|
|
}
|
|
return div(x, norm(x, 'euclidean'));
|
|
}));
|
|
};
|
|
for (var i = 0; i < xs.length; ++i) {
|
|
_loop_2(i);
|
|
}
|
|
if (inputIsTensor2D) {
|
|
return stack(ys, 0);
|
|
}
|
|
else {
|
|
return ys;
|
|
}
|
|
}
|
|
var gramSchmidt = /* @__PURE__ */ op({ gramSchmidt_: gramSchmidt_ });
|
|
|
|
/**
|
|
* Compute QR decomposition of m-by-n matrix using Householder transformation.
|
|
*
|
|
* Implementation based on
|
|
* [http://www.cs.cornell.edu/~bindel/class/cs6210-f09/lec18.pdf]
|
|
* (http://www.cs.cornell.edu/~bindel/class/cs6210-f09/lec18.pdf)
|
|
*
|
|
* ```js
|
|
* const a = tf.tensor2d([[1, 2], [3, 4]]);
|
|
* let [q, r] = tf.linalg.qr(a);
|
|
* console.log('Q');
|
|
* q.print();
|
|
* console.log('R');
|
|
* r.print();
|
|
* console.log('Orthogonalized');
|
|
* q.dot(q.transpose()).print() // should be nearly the identity matrix.
|
|
* console.log('Reconstructed');
|
|
* q.dot(r).print(); // should be nearly [[1, 2], [3, 4]];
|
|
* ```
|
|
*
|
|
* @param x The `tf.Tensor` to be QR-decomposed. Must have rank >= 2. Suppose
|
|
* it has the shape `[..., M, N]`.
|
|
* @param fullMatrices An optional boolean parameter. Defaults to `false`.
|
|
* If `true`, compute full-sized `Q`. If `false` (the default),
|
|
* compute only the leading N columns of `Q` and `R`.
|
|
* @returns An `Array` of two `tf.Tensor`s: `[Q, R]`. `Q` is a unitary matrix,
|
|
* i.e., its columns all have unit norm and are mutually orthogonal.
|
|
* If `M >= N`,
|
|
* If `fullMatrices` is `false` (default),
|
|
* - `Q` has a shape of `[..., M, N]`,
|
|
* - `R` has a shape of `[..., N, N]`.
|
|
* If `fullMatrices` is `true` (default),
|
|
* - `Q` has a shape of `[..., M, M]`,
|
|
* - `R` has a shape of `[..., M, N]`.
|
|
* If `M < N`,
|
|
* - `Q` has a shape of `[..., M, M]`,
|
|
* - `R` has a shape of `[..., M, N]`.
|
|
* @throws If the rank of `x` is less than 2.
|
|
*
|
|
* @doc {heading:'Operations',
|
|
* subheading:'Linear Algebra',
|
|
* namespace:'linalg'}
|
|
*/
|
|
function qr_(x, fullMatrices) {
|
|
if (fullMatrices === void 0) { fullMatrices = false; }
|
|
assert(x.rank >= 2, function () { return "qr() requires input tensor to have a rank >= 2, but got rank ".concat(x.rank); });
|
|
if (x.rank === 2) {
|
|
return qr2d(x, fullMatrices);
|
|
}
|
|
else {
|
|
// Rank > 2.
|
|
// TODO(cais): Below we split the input into individual 2D tensors,
|
|
// perform QR decomposition on them and then stack the results back
|
|
// together. We should explore whether this can be parallelized.
|
|
var outerDimsProd = x.shape.slice(0, x.shape.length - 2)
|
|
.reduce(function (value, prev) { return value * prev; });
|
|
var x2ds = unstack(reshape(x, [
|
|
outerDimsProd, x.shape[x.shape.length - 2],
|
|
x.shape[x.shape.length - 1]
|
|
]), 0);
|
|
var q2ds_1 = [];
|
|
var r2ds_1 = [];
|
|
x2ds.forEach(function (x2d) {
|
|
var _a = __read(qr2d(x2d, fullMatrices), 2), q2d = _a[0], r2d = _a[1];
|
|
q2ds_1.push(q2d);
|
|
r2ds_1.push(r2d);
|
|
});
|
|
var q = reshape(stack(q2ds_1, 0), x.shape);
|
|
var r = reshape(stack(r2ds_1, 0), x.shape);
|
|
return [q, r];
|
|
}
|
|
}
|
|
function qr2d(x, fullMatrices) {
|
|
if (fullMatrices === void 0) { fullMatrices = false; }
|
|
return ENGINE.tidy(function () {
|
|
assert(x.shape.length === 2, function () { return "qr2d() requires a 2D Tensor, but got a ".concat(x.shape.length, "D Tensor."); });
|
|
var m = x.shape[0];
|
|
var n = x.shape[1];
|
|
var q = eye(m); // Orthogonal transform so far.
|
|
var r = clone(x); // Transformed matrix so far.
|
|
var one2D = tensor2d([[1]], [1, 1]);
|
|
var w = clone(one2D);
|
|
var iters = m >= n ? n : m;
|
|
var _loop_1 = function (j) {
|
|
var _a;
|
|
// This tidy within the for-loop ensures we clean up temporary
|
|
// tensors as soon as they are no longer needed.
|
|
var rTemp = r;
|
|
var wTemp = w;
|
|
var qTemp = q;
|
|
_a = __read(ENGINE.tidy(function () {
|
|
// Find H = I - tau * w * w', to put zeros below R(j, j).
|
|
var rjEnd1 = slice(r, [j, j], [m - j, 1]);
|
|
var normX = norm(rjEnd1);
|
|
var rjj = slice(r, [j, j], [1, 1]);
|
|
// The sign() function returns 0 on 0, which causes division by zero.
|
|
var s = where(greater(rjj, 0), tensor2d([[-1]]), tensor2d([[1]]));
|
|
var u1 = sub(rjj, mul(s, normX));
|
|
var wPre = div(rjEnd1, u1);
|
|
if (wPre.shape[0] === 1) {
|
|
w = clone(one2D);
|
|
}
|
|
else {
|
|
w = concat([
|
|
one2D,
|
|
slice(wPre, [1, 0], [wPre.shape[0] - 1, wPre.shape[1]])
|
|
], 0);
|
|
}
|
|
var tau = neg(div(matMul$1(s, u1), normX));
|
|
// -- R := HR, Q := QH.
|
|
var rjEndAll = slice(r, [j, 0], [m - j, n]);
|
|
var tauTimesW = mul(tau, w);
|
|
var wT = transpose(w);
|
|
if (j === 0) {
|
|
r = sub(rjEndAll, matMul$1(tauTimesW, matMul$1(wT, rjEndAll)));
|
|
}
|
|
else {
|
|
var rTimesTau = sub(rjEndAll, matMul$1(tauTimesW, matMul$1(wT, rjEndAll)));
|
|
r = concat([slice(r, [0, 0], [j, n]), rTimesTau], 0);
|
|
}
|
|
var tawTimesWT = transpose(tauTimesW);
|
|
var qAllJEnd = slice(q, [0, j], [m, q.shape[1] - j]);
|
|
if (j === 0) {
|
|
q = sub(qAllJEnd, matMul$1(matMul$1(qAllJEnd, w), tawTimesWT));
|
|
}
|
|
else {
|
|
var qTimesTau = sub(qAllJEnd, matMul$1(matMul$1(qAllJEnd, w), tawTimesWT));
|
|
q = concat([slice(q, [0, 0], [m, j]), qTimesTau], 1);
|
|
}
|
|
return [w, r, q];
|
|
}), 3), w = _a[0], r = _a[1], q = _a[2];
|
|
dispose([rTemp, wTemp, qTemp]);
|
|
};
|
|
for (var j = 0; j < iters; ++j) {
|
|
_loop_1(j);
|
|
}
|
|
if (!fullMatrices && m > n) {
|
|
q = slice(q, [0, 0], [m, n]);
|
|
r = slice(r, [0, 0], [n, n]);
|
|
}
|
|
return [q, r];
|
|
});
|
|
}
|
|
var qr = /* @__PURE__ */ op({ qr_: qr_ });
|
|
|
|
/**
|
|
* @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 = {}));
|
|
|
|
/**
|
|
* Computes the weighted loss between two tensors.
|
|
*
|
|
* @param losses Tensor of shape `[batch_size, d1, ..., dN]`.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `losses`, and must be broadcastable to `losses` (i.e., all
|
|
* dimensions must be either `1`, or the same as the corresponding
|
|
* `losses` dimension).
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function computeWeightedLoss_(losses, weights, reduction) {
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $losses = convertToTensor(losses, 'losses', 'computeWeightedLoss');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'computeWeightedLoss');
|
|
}
|
|
var weightedLoss = ($weights == null) ? $losses : mul($losses, $weights);
|
|
if (reduction === Reduction.NONE) {
|
|
return weightedLoss;
|
|
}
|
|
if (reduction === Reduction.SUM) {
|
|
return sum(weightedLoss);
|
|
}
|
|
if (reduction === Reduction.MEAN) {
|
|
if ($weights == null) {
|
|
return mean(weightedLoss);
|
|
}
|
|
else {
|
|
var broadcastFactor = $losses.size / $weights.size;
|
|
var result = div(sum(weightedLoss), sum($weights));
|
|
return broadcastFactor > 1 ? div(result, scalar(broadcastFactor)) :
|
|
result;
|
|
}
|
|
}
|
|
if (reduction === Reduction.SUM_BY_NONZERO_WEIGHTS) {
|
|
if ($weights == null) {
|
|
return div(sum(weightedLoss), scalar($losses.size));
|
|
}
|
|
else {
|
|
var broadcastedWeights = mul($weights, ones($losses.shape));
|
|
var numNonZeros = cast(sum(notEqual(broadcastedWeights, scalar(0))), 'float32');
|
|
return div(sum(weightedLoss), numNonZeros);
|
|
}
|
|
}
|
|
throw Error("Unknown reduction: ".concat(reduction));
|
|
}
|
|
var computeWeightedLoss = /* @__PURE__ */ op({ computeWeightedLoss_: computeWeightedLoss_ });
|
|
|
|
/**
|
|
* @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 absolute difference loss between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function absoluteDifference_(labels, predictions, weights, reduction) {
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'absoluteDifference');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'absoluteDifference');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'absoluteDifference');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in absoluteDifference: ');
|
|
var losses = abs(sub($labels, $predictions));
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var absoluteDifference = /* @__PURE__ */ op({ absoluteDifference_: absoluteDifference_ });
|
|
|
|
/**
|
|
* Computes the cosine distance loss between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param axis The dimension along which the cosine distance is computed.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function cosineDistance_(labels, predictions, axis, weights, reduction) {
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'cosineDistance');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'cosineDistance');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'cosineDistance');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in cosineDistance: ');
|
|
var one = scalar(1);
|
|
var losses = sub(one, sum(mul($labels, $predictions), axis, true));
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var cosineDistance = /* @__PURE__ */ op({ cosineDistance_: cosineDistance_ });
|
|
|
|
/**
|
|
* Computes the Hinge loss between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function hingeLoss_(labels, predictions, weights, reduction) {
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'hingeLoss');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'hingeLoss');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'hingeLoss');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in hingeLoss: ');
|
|
var one = scalar(1);
|
|
// Convert binary labels to (-1, 1)
|
|
$labels = sub(mul(scalar(2), $labels), one);
|
|
var losses = relu(sub(one, mul($labels, $predictions)));
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var hingeLoss = /* @__PURE__ */ op({ hingeLoss_: hingeLoss_ });
|
|
|
|
/**
|
|
* @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 Huber loss between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param delta Point where Huber loss changes from quadratic to linear.
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`.
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function huberLoss_(labels, predictions, weights, delta, reduction) {
|
|
if (delta === void 0) { delta = 1.0; }
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'huberLoss');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'huberLoss');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'huberLoss');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in huberLoss: ');
|
|
var deltaScalar = scalar(delta);
|
|
var error = abs(sub($predictions, $labels));
|
|
var quadratic = minimum(error, deltaScalar);
|
|
var linear = sub(error, quadratic);
|
|
var losses = add(mul(scalar(0.5), square(quadratic)), mul(deltaScalar, linear));
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var huberLoss = /* @__PURE__ */ op({ huberLoss_: huberLoss_ });
|
|
|
|
/**
|
|
* @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 log loss between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param epsilon A small increment to avoid taking log of zero
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function logLoss_(labels, predictions, weights, epsilon, reduction) {
|
|
if (epsilon === void 0) { epsilon = 1e-7; }
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'logLoss');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'logLoss');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'logLoss');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in logLoss: ');
|
|
var one = scalar(1);
|
|
var epsilonScalar = scalar(epsilon);
|
|
var l1 = neg(mul($labels, log(add($predictions, epsilonScalar))));
|
|
var l2 = mul(sub(one, $labels), log(add(sub(one, $predictions), epsilonScalar)));
|
|
var losses = sub(l1, l2);
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var logLoss = /* @__PURE__ */ op({ logLoss_: logLoss_ });
|
|
|
|
/**
|
|
* @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 mean squared error between two tensors.
|
|
*
|
|
* @param labels The ground truth output tensor, same dimensions as
|
|
* 'predictions'.
|
|
* @param predictions The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc {heading: 'Training', subheading: 'Losses', namespace: 'losses'}
|
|
*/
|
|
function meanSquaredError_(labels, predictions, weights, reduction) {
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $labels = convertToTensor(labels, 'labels', 'meanSquaredError');
|
|
var $predictions = convertToTensor(predictions, 'predictions', 'meanSquaredError');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'meanSquaredError');
|
|
}
|
|
assertShapesMatch($labels.shape, $predictions.shape, 'Error in meanSquaredError: ');
|
|
var losses = squaredDifference($labels, $predictions);
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var meanSquaredError = /* @__PURE__ */ op({ meanSquaredError_: meanSquaredError_ });
|
|
|
|
/**
|
|
* @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 sigmoidCrossEntropyWithLogits_(labels, logits) {
|
|
var $labels = convertToTensor(labels, 'labels', 'sigmoidCrossEntropyWithLogits');
|
|
var $logits = convertToTensor(logits, 'logits', 'sigmoidCrossEntropyWithLogits');
|
|
assertShapesMatch($labels.shape, $logits.shape, 'Error in sigmoidCrossEntropyWithLogits: ');
|
|
/**
|
|
* Implementation Details:
|
|
*
|
|
* 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)))
|
|
*/
|
|
var maxOutput = relu($logits);
|
|
var outputXTarget = mul($logits, $labels);
|
|
var sigmoidOutput = log1p(exp(neg(abs($logits))));
|
|
return add(sub(maxOutput, outputXTarget), sigmoidOutput);
|
|
}
|
|
/**
|
|
* Computes the sigmoid cross entropy loss between two tensors.
|
|
*
|
|
* If labelSmoothing is nonzero, smooth the labels towards 1/2:
|
|
*
|
|
* newMulticlassLabels = multiclassLabels * (1 - labelSmoothing)
|
|
* + 0.5 * labelSmoothing
|
|
*
|
|
* @param multiClassLabels The ground truth output tensor of shape
|
|
* [batch_size, num_classes], same dimensions as 'predictions'.
|
|
* @param logits The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or the same rank as
|
|
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
|
|
* must be either `1`, or the same as the corresponding `losses`
|
|
* dimension).
|
|
* @param labelSmoothing If greater than 0, then smooth the labels.
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc { heading: 'Training', subheading: 'Losses', namespace: 'losses' }
|
|
*/
|
|
function sigmoidCrossEntropy_(multiClassLabels, logits, weights, labelSmoothing, reduction) {
|
|
if (labelSmoothing === void 0) { labelSmoothing = 0; }
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $multiClassLabels = convertToTensor(multiClassLabels, 'multiClassLabels', 'sigmoidCrossEntropy');
|
|
var $logits = convertToTensor(logits, 'logits', 'sigmoidCrossEntropy');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'sigmoidCrossEntropy');
|
|
}
|
|
assertShapesMatch($multiClassLabels.shape, $logits.shape, 'Error in sigmoidCrossEntropy: ');
|
|
if (labelSmoothing > 0) {
|
|
var labelSmoothingScalar = scalar(labelSmoothing);
|
|
var one = scalar(1);
|
|
var half = scalar(0.5);
|
|
$multiClassLabels =
|
|
add(mul($multiClassLabels, sub(one, labelSmoothingScalar)), mul(half, labelSmoothingScalar));
|
|
}
|
|
var losses = sigmoidCrossEntropyWithLogits_($multiClassLabels, $logits);
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var sigmoidCrossEntropy = /* @__PURE__ */ op({ sigmoidCrossEntropy_: sigmoidCrossEntropy_ });
|
|
|
|
/**
|
|
* Computes softmax cross entropy between logits and labels.
|
|
*
|
|
* Measures the probability error in discrete classification tasks in which
|
|
* the classes are mutually exclusive (each entry is in exactly one class).
|
|
* For example, each CIFAR-10 image is labeled with one and only one label: an
|
|
* image can be a dog or a truck, but not both.
|
|
*
|
|
* `NOTE`: While the classes are mutually exclusive, their probabilities need
|
|
* not be. All that is required is that each row of labels is a valid
|
|
* probability distribution. If they are not, the computation of the gradient
|
|
* will be incorrect.
|
|
*
|
|
* `WARNING`: This op expects unscaled logits, since it performs a softmax on
|
|
* logits internally for efficiency. Do not call this op with the output of
|
|
* softmax, as it will produce incorrect results.
|
|
*
|
|
* logits and labels must have the same shape, e.g. [batch_size, num_classes]
|
|
* and the same dtype.
|
|
* @param labels The labels array.
|
|
* @param logits The logits array.
|
|
* @param dim The dimension softmax would be performed on. Defaults to `-1`
|
|
* which indicates the last dimension.
|
|
*/
|
|
function softmaxCrossEntropyWithLogits_(labels, logits, dim) {
|
|
if (dim === void 0) { dim = -1; }
|
|
if (dim === -1) {
|
|
dim = logits.rank - 1;
|
|
}
|
|
if (dim !== logits.rank - 1) {
|
|
throw Error("Softmax cross entropy along a non-last dimension is not yet " +
|
|
"supported. Labels / logits was rank ".concat(logits.rank, " ") +
|
|
"and dim was ".concat(dim));
|
|
}
|
|
// Use a custom gradient for numerical stability.
|
|
var customOp = customGrad(function (labels, logits, save) {
|
|
// Reference:
|
|
// 1. http://cs231n.github.io/linear-classify/#softmax
|
|
// 2. https://blog.feedly.com/tricks-of-the-trade-logsumexp/
|
|
var keepDims = true;
|
|
var lse = logSumExp(logits, [dim], keepDims);
|
|
var logResult = sub(cast(logits, 'float32'), lse);
|
|
save([labels, logResult]);
|
|
var costVector = neg(mul(logResult, labels));
|
|
var value = sum(costVector, [dim]);
|
|
var gradFunc = function (dy, saved) {
|
|
var _a = __read(saved, 2), labels = _a[0], logResult = _a[1];
|
|
var dyShape = expandShapeToKeepDim(dy.shape, [dim]);
|
|
return [
|
|
mul(reshape(dy, dyShape), sub(cast(labels, 'float32'), exp(logResult))),
|
|
mul(reshape(dy, dyShape), sub(exp(logResult), cast(labels, 'float32'))),
|
|
];
|
|
};
|
|
return { value: value, gradFunc: gradFunc };
|
|
});
|
|
return customOp(labels, logits);
|
|
}
|
|
/**
|
|
* Computes the softmax cross entropy loss between two tensors.
|
|
*
|
|
* If labelSmoothing is nonzero, smooth the labels towards 1/2:
|
|
*
|
|
* newOnehotLabels = onehotLabels * (1 - labelSmoothing)
|
|
* + labelSmoothing / numClasses
|
|
*
|
|
* @param onehotLabels One hot encoded labels
|
|
* [batch_size, num_classes], same dimensions as 'predictions'.
|
|
* @param logits The predicted outputs.
|
|
* @param weights Tensor whose rank is either 0, or 1, and must be
|
|
* broadcastable to `loss` of shape [batch_size]
|
|
* @param labelSmoothing If greater than 0, then smooth the labels.
|
|
* @param reduction Type of reduction to apply to loss. Should be of type
|
|
* `Reduction`
|
|
*
|
|
* @doc { heading: 'Training', subheading: 'Losses', namespace: 'losses' }
|
|
*/
|
|
function softmaxCrossEntropy_(onehotLabels, logits, weights, labelSmoothing, reduction) {
|
|
if (labelSmoothing === void 0) { labelSmoothing = 0; }
|
|
if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
|
|
var $onehotLabels = convertToTensor(onehotLabels, 'onehotLabels', 'softmaxCrossEntropy');
|
|
var $logits = convertToTensor(logits, 'logits', 'softmaxCrossEntropy');
|
|
var $weights = null;
|
|
if (weights != null) {
|
|
$weights = convertToTensor(weights, 'weights', 'softmaxCrossEntropy');
|
|
}
|
|
assertShapesMatch($onehotLabels.shape, $logits.shape, 'Error in softmaxCrossEntropy: ');
|
|
if (labelSmoothing > 0) {
|
|
var labelSmoothingScalar = scalar(labelSmoothing);
|
|
var one = scalar(1);
|
|
var numClasses = scalar($onehotLabels.shape[1]);
|
|
$onehotLabels =
|
|
add(mul($onehotLabels, sub(one, labelSmoothingScalar)), div(labelSmoothingScalar, numClasses));
|
|
}
|
|
var losses = softmaxCrossEntropyWithLogits_($onehotLabels, $logits);
|
|
return computeWeightedLoss(losses, $weights, reduction);
|
|
}
|
|
var softmaxCrossEntropy = /* @__PURE__ */ op({ softmaxCrossEntropy_: softmaxCrossEntropy_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* The input SparseTensor is represented via the map of inputs {`indices`,
|
|
* `values`, `denseShape`}. The output SparseTensor has the same `denseShape`
|
|
* but with indices `outputIndices` and values `outputValues`. This op inserts a
|
|
* single entry for every row that doesn't have any values. The index is created
|
|
* as `[row, 0, ..., 0]` and the inserted value is `defaultValue`.
|
|
*
|
|
* For example, suppose `spInput` has shape [5, 6] and non-empty values:
|
|
* [0, 1]: a
|
|
* [0, 3]: b
|
|
* [2, 0]: c
|
|
* [3, 1]: d
|
|
*
|
|
* Rows 1 and 4 are empty, so the output will be of shape [5, 6] with values:
|
|
* [0, 1]: a
|
|
* [0, 3]: b
|
|
* [1, 0]: `defaultValue`
|
|
* [2, 0]: c
|
|
* [3, 1]: d
|
|
* [4, 0]: `defaultValue`
|
|
*
|
|
* The output SparseTensor will be in row-major order and will have the same
|
|
* shape as the input.
|
|
*
|
|
* This op also returns an indicator vector shaped [dense_shape[0]] such that
|
|
* emptyRowIndicator[i] = True iff row i was an empty row.
|
|
*
|
|
* And a reverse index map vector shaped [indices.shape[0]] that is used during
|
|
* backpropagation, reverseIndexMap[i] = outi s.t. indices[i, j] ==
|
|
* outputIndices[outi, j] for all j
|
|
*
|
|
* ```js
|
|
* const result = tf.sparse.sparseFillEmptyRows(
|
|
* [[0, 0], [1, 0], [1, 3], [1, 4], [3, 2], [3, 3]],
|
|
* [0, 10, 13, 14, 32, 33], [5, 6], -1);
|
|
* console.log(result);
|
|
* result['outputIndices'].print(); // [[0, 0], [1, 0], [1, 3], [1, 4],
|
|
* // [2, 0], [3, 2], [3, 3], [4, 0]]
|
|
* result['outputValues'].print(); // [0, 10, 13, 14,-1, 32, 33, -1]
|
|
* result['emptyRowIndicator'].print(); // [false, false, true, false, true]
|
|
* result['reverseIndexMap'].print(); // [0, 1, 2, 3, 5, 6]
|
|
* ```
|
|
* @param indices: 2-D. The indices of the sparse tensor.
|
|
* @param values: 1-D. The values of the sparse tensor.
|
|
* @param denseShape: 1-D. The shape of the sparse tensor.
|
|
* @param defaultValue: 0-D. Default value to insert into location [row, 0, ...,
|
|
* 0] for rows missing from the input sparse tensor.
|
|
* @return A map with the following properties:
|
|
* - outputIndices
|
|
* - outputValues: 1-D. The values of the filled sparse tensor.
|
|
* - emptyRowIndicator: 1-D. Whether the dense row was missing in the input
|
|
* sparse tensor.
|
|
* - reverseIndexMap: 1-D. A map from the input indices to the output
|
|
* indices.
|
|
* @doc {heading: 'Operations', subheading: 'Sparse'}
|
|
*/
|
|
function sparseFillEmptyRows_(indices, values, denseShape, defaultValue) {
|
|
var $indices = convertToTensor(indices, 'indices', 'sparseFillEmptyRows', 'int32');
|
|
var $values = convertToTensor(values, 'values', 'sparseFillEmptyRows');
|
|
var $denseShape = convertToTensor(denseShape, 'denseShape', 'sparseFillEmptyRows', 'int32');
|
|
var $defaultValue = convertToTensor(defaultValue, 'defaultValue', 'sparseFillEmptyRows', $values.dtype);
|
|
if ($indices.rank !== 2) {
|
|
throw new Error("Indices should be Tensor2D but received shape\n ".concat($indices.shape));
|
|
}
|
|
if ($values.rank !== 1) {
|
|
throw new Error("Values should be Tensor1D but received shape ".concat($values.shape));
|
|
}
|
|
if ($denseShape.rank !== 1) {
|
|
throw new Error("Dense shape should be Tensor1D but received shape ".concat($denseShape.shape));
|
|
}
|
|
if ($defaultValue.rank !== 0) {
|
|
throw new Error("Default value should be a scalar but received shape ".concat($defaultValue.shape));
|
|
}
|
|
var inputs = {
|
|
indices: $indices,
|
|
values: $values,
|
|
denseShape: $denseShape,
|
|
defaultValue: $defaultValue
|
|
};
|
|
var result = ENGINE.runKernel(SparseFillEmptyRows, inputs);
|
|
return {
|
|
outputIndices: result[0],
|
|
outputValues: result[1],
|
|
emptyRowIndicator: result[2],
|
|
reverseIndexMap: result[3]
|
|
};
|
|
}
|
|
var sparseFillEmptyRows = /* @__PURE__ */ op({ sparseFillEmptyRows_: sparseFillEmptyRows_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* This operation has the same semantics as reshape on the represented dense
|
|
* tensor. The `inputIndices` are recomputed based on the requested `newShape`.
|
|
* If one component of `newShape` is the special value -1, the size of that
|
|
* dimension is computed so that the total dense size remains constant. At most
|
|
* one component of `newShape` can be -1. The number of dense elements implied
|
|
* by `newShape` must be the same as the number of dense elements originally
|
|
* implied by `inputShape`. Reshaping does not affect the order of values in the
|
|
* SparseTensor. If the input tensor has rank R_in and N non-empty values, and
|
|
* `newShape` has length R_out, then `inputIndices` has shape [N, R_in],
|
|
* `inputShape` has length R_in, `outputIndices` has shape [N, R_out], and
|
|
* `outputShape` has length R_out.
|
|
*
|
|
* ```js
|
|
* const result = tf.sparse.sparseReshape(
|
|
* [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 2, 3]],
|
|
* [2, 3, 6], [9, -1]);
|
|
* console.log(result);
|
|
* result['outputIndices'].print(); //[[0, 0], [0, 1], [1, 2], [4, 2], [8, 1]]
|
|
* result['outputShape'].print(); // [9, 4]
|
|
* ```
|
|
* @param inputIndices: 2-D. N x R_in matrix with the indices of non-empty
|
|
* values in a SparseTensor.
|
|
* @param inputShape: 1-D. R_in Tensor1D with the input SparseTensor's dense
|
|
* shape.
|
|
* @param newShape: 1-D. R_out Tensor1D with the requested new dense shape.
|
|
* @return A map with the following properties:
|
|
* - outputIndices: 2-D. N x R_out matrix with the updated indices of
|
|
* non-empty values in the output SparseTensor.
|
|
* - outputShape: 1-D. R_out vector with the full dense shape of the output
|
|
* SparseTensor. This is the same as newShape but with any -1 dimensions
|
|
* filled in.
|
|
* @doc {heading: 'Operations', subheading: 'Sparse'}
|
|
*/
|
|
function sparseReshape_(inputIndices, inputShape, newShape) {
|
|
var $inputIndices = convertToTensor(inputIndices, 'inputIndices', 'sparseReshape', 'int32');
|
|
var $inputShape = convertToTensor(inputShape, 'inputShape', 'sparseReshape', 'int32');
|
|
var $newShape = convertToTensor(newShape, 'newShape', 'sparseReshape', 'int32');
|
|
if ($inputIndices.rank !== 2) {
|
|
throw new Error("Input indices should be Tensor2D but received shape\n ".concat($inputIndices.shape));
|
|
}
|
|
if ($inputShape.rank !== 1) {
|
|
throw new Error("Input shape should be Tensor1D but received shape ".concat($inputShape.shape));
|
|
}
|
|
if ($newShape.rank !== 1) {
|
|
throw new Error("New shape should be Tensor1D but received shape ".concat($newShape.shape));
|
|
}
|
|
var inputs = {
|
|
inputIndices: $inputIndices,
|
|
inputShape: $inputShape,
|
|
newShape: $newShape
|
|
};
|
|
var result = ENGINE.runKernel(SparseReshape, inputs);
|
|
return { outputIndices: result[0], outputShape: result[1] };
|
|
}
|
|
var sparseReshape = /* @__PURE__ */ op({ sparseReshape_: sparseReshape_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes the mean along sparse segments of a tensor.
|
|
*
|
|
* ```js
|
|
* const c = tf.tensor2d([[1,2,3,4], [-1,-2,-3,-4], [6,7,8,9]]);
|
|
* // Select two rows, one segment.
|
|
* const result1 = tf.sparse.sparseSegmentMean(c,
|
|
* tf.tensor1d([0, 1], 'int32'),
|
|
* tf.tensor1d([0, 0], 'int32'));
|
|
* result1.print(); // [[0, 0, 0, 0]]
|
|
*
|
|
* // Select two rows, two segments.
|
|
* const result2 = tf.sparse.sparseSegmentMean(c,
|
|
* tf.tensor1d([0, 1], 'int32'),
|
|
* tf.tensor1d([0, 1], 'int32'));
|
|
* result2.print(); // [[1, 2, 3, 4], [-1, -2, -3, -4]]
|
|
*
|
|
* // Select all rows, two segments.
|
|
* const result3 = tf.sparse.sparseSegmentMean(c,
|
|
* tf.tensor1d([0, 1, 2], 'int32'),
|
|
* tf.tensor1d([0, 1, 1], 'int32'));
|
|
* result3.print(); // [[1.0, 2.0, 3.0, 4.0], [2.5, 2.5, 2.5, 2.5]]
|
|
* ```
|
|
* @param data: A Tensor of at least one dimension with data that will be
|
|
* assembled in the output.
|
|
* @param indices: A 1-D Tensor with indices into data. Has same rank as
|
|
* segmentIds.
|
|
* @param segmentIds: A 1-D Tensor with indices into the output Tensor. Values
|
|
* should be sorted and can be repeated.
|
|
* @return Has same shape as data, except for dimension 0 which has equal to
|
|
* the number of segments.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Sparse'}
|
|
*/
|
|
function sparseSegmentMean_(data, indices, segmentIds) {
|
|
var $data = convertToTensor(data, 'data', 'sparseSegmentMean');
|
|
var $indices = convertToTensor(indices, 'indices', 'sparseSegmentMean', 'int32');
|
|
var $segmentIds = convertToTensor(segmentIds, 'segmentIds', 'sparseSegmentMean', 'int32');
|
|
if ($data.rank < 1) {
|
|
throw new Error("Data should be at least 1 dimensional but received scalar");
|
|
}
|
|
if ($indices.rank !== 1) {
|
|
throw new Error("Indices should be Tensor1D but received shape\n ".concat($indices.shape));
|
|
}
|
|
if ($segmentIds.rank !== 1) {
|
|
throw new Error("Segment ids should be Tensor1D but received shape\n ".concat($segmentIds.shape));
|
|
}
|
|
var inputs = {
|
|
data: $data,
|
|
indices: $indices,
|
|
segmentIds: $segmentIds
|
|
};
|
|
return ENGINE.runKernel(SparseSegmentMean, inputs);
|
|
}
|
|
var sparseSegmentMean = /* @__PURE__ */ op({ sparseSegmentMean_: sparseSegmentMean_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Computes the sum along sparse segments of a tensor.
|
|
*
|
|
* ```js
|
|
* const c = tf.tensor2d([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]);
|
|
* // Select two rows, one segment.
|
|
* const result1 = tf.sparse.sparseSegmentSum(c,
|
|
* tf.tensor1d([0, 1], 'int32'),
|
|
* tf.tensor1d([0, 0], 'int32'));
|
|
* result1.print(); // [[0, 0, 0, 0]]
|
|
*
|
|
* // Select two rows, two segments.
|
|
* const result2 = tf.sparse.sparseSegmentSum(c,
|
|
* tf.tensor1d([0, 1], 'int32'),
|
|
* tf.tensor1d([0, 1], 'int32'));
|
|
* result2.print(); // [[1, 2, 3, 4], [-1, -2, -3, -4]]
|
|
*
|
|
* // Select all rows, two segments.
|
|
* const result3 = tf.sparse.sparseSegmentSum(c,
|
|
* tf.tensor1d([0, 1, 2], 'int32'),
|
|
* tf.tensor1d([0, 0, 1], 'int32'));
|
|
* result3.print(); // [[0, 0, 0, 0], [5, 6, 7, 8]]
|
|
* ```
|
|
* @param data: A Tensor of at least one dimension with data that will be
|
|
* assembled in the output.
|
|
* @param indices: A 1-D Tensor with indices into data. Has same rank as
|
|
* segmentIds.
|
|
* @param segmentIds: A 1-D Tensor with indices into the output Tensor. Values
|
|
* should be sorted and can be repeated.
|
|
* @return Has same shape as data, except for dimension 0 which has equal to
|
|
* the number of segments.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'Sparse'}
|
|
*/
|
|
function sparseSegmentSum_(data, indices, segmentIds) {
|
|
var $data = convertToTensor(data, 'data', 'sparseSegmentSum');
|
|
var $indices = convertToTensor(indices, 'indices', 'sparseSegmentSum', 'int32');
|
|
var $segmentIds = convertToTensor(segmentIds, 'segmentIds', 'sparseSegmentSum', 'int32');
|
|
if ($data.rank < 1) {
|
|
throw new Error("Data should be at least 1 dimensional but received scalar");
|
|
}
|
|
if ($indices.rank !== 1) {
|
|
throw new Error("Indices should be Tensor1D but received shape\n ".concat($indices.shape));
|
|
}
|
|
if ($segmentIds.rank !== 1) {
|
|
throw new Error("Segment ids should be Tensor1D but received shape\n ".concat($segmentIds.shape));
|
|
}
|
|
var inputs = {
|
|
data: $data,
|
|
indices: $indices,
|
|
segmentIds: $segmentIds
|
|
};
|
|
return ENGINE.runKernel(SparseSegmentSum, inputs);
|
|
}
|
|
var sparseSegmentSum = /* @__PURE__ */ op({ sparseSegmentSum_: sparseSegmentSum_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Creates ngrams from ragged string data.
|
|
*
|
|
* This op accepts a ragged tensor with 1 ragged dimension containing only
|
|
* strings and outputs a ragged tensor with 1 ragged dimension containing ngrams
|
|
* of that string, joined along the innermost axis.
|
|
*
|
|
* ```js
|
|
* const result = tf.string.stringNGrams(
|
|
* ['a', 'b', 'c', 'd'], tf.tensor1d([0, 2, 4], 'int32'),
|
|
* '|', [1, 2], 'LP', 'RP', -1, false);
|
|
* result['nGrams'].print(); // ['a', 'b', 'LP|a', 'a|b', 'b|RP',
|
|
* // 'c', 'd', 'LP|c', 'c|d', 'd|RP']
|
|
* result['nGramsSplits'].print(); // [0, 5, 10]
|
|
* ```
|
|
* @param data: The values tensor of the ragged string tensor to make ngrams out
|
|
* of. Must be a 1D string tensor.
|
|
* @param dataSplits: The splits tensor of the ragged string tensor to make
|
|
* ngrams out of.
|
|
* @param separator: The string to append between elements of the token. Use ""
|
|
* for no separator.
|
|
* @param nGramWidths: The sizes of the ngrams to create.
|
|
* @param leftPad: The string to use to pad the left side of the ngram sequence.
|
|
* Only used if pad_width !== 0.
|
|
* @param rightPad: The string to use to pad the right side of the ngram
|
|
* sequence. Only used if pad_width !== 0.
|
|
* @param padWidth: The number of padding elements to add to each side of each
|
|
* sequence. Note that padding will never be greater than `nGramWidths`-1
|
|
* regardless of this value. If `padWidth`=-1, then add max(`nGramWidths`)-1
|
|
* elements.
|
|
* @param preserveShortSequences: If true, then ensure that at least one ngram
|
|
* is generated for each input sequence. In particular, if an input sequence
|
|
* is shorter than min(ngramWidth) + 2*padWidth, then generate a single
|
|
* ngram containing the entire sequence. If false, then no ngrams are
|
|
* generated for these short input sequences.
|
|
* @return A map with the following properties:
|
|
* - nGrams: The values tensor of the output ngrams ragged tensor.
|
|
* - nGramsSplits: The splits tensor of the output ngrams ragged tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'String'}
|
|
*/
|
|
function stringNGrams_(data, dataSplits, separator, nGramWidths, leftPad, rightPad, padWidth, preserveShortSequences) {
|
|
var $data = convertToTensor(data, 'data', 'stringNGrams', 'string');
|
|
if ($data.dtype !== 'string') {
|
|
throw new Error('Data must be of datatype string');
|
|
}
|
|
if ($data.shape.length !== 1) {
|
|
throw new Error("Data must be a vector, saw: ".concat($data.shape));
|
|
}
|
|
var $dataSplits = convertToTensor(dataSplits, 'dataSplits', 'stringNGrams');
|
|
if ($dataSplits.dtype !== 'int32') {
|
|
throw new Error('Data splits must be of datatype int32');
|
|
}
|
|
var attrs = {
|
|
separator: separator,
|
|
nGramWidths: nGramWidths,
|
|
leftPad: leftPad,
|
|
rightPad: rightPad,
|
|
padWidth: padWidth,
|
|
preserveShortSequences: preserveShortSequences
|
|
};
|
|
var inputs = { data: $data, dataSplits: $dataSplits };
|
|
var result = ENGINE.runKernel(StringNGrams, inputs, attrs);
|
|
return { nGrams: result[0], nGramsSplits: result[1] };
|
|
}
|
|
var stringNGrams = /* @__PURE__ */ op({ stringNGrams_: stringNGrams_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Split elements of `input` based on `delimiter` into a SparseTensor .
|
|
*
|
|
* Let N be the size of source (typically N will be the batch size). Split each
|
|
* element of `input` based on `delimiter` and return a SparseTensor containing
|
|
* the splitted tokens. Empty tokens are ignored if `skipEmpty` is set to True.
|
|
*
|
|
* `delimiter` can be empty, or a string of split characters. If `delimiter` is
|
|
* an empty string, each element of `input` is split into individual
|
|
* character strings. Otherwise every character of `delimiter` is a potential
|
|
* split point.
|
|
*
|
|
* ```js
|
|
* const result = tf.string.stringSplit(['hello world', 'a b c'], ' ');
|
|
* result['indices'].print(); // [[0, 0], [0, 1], [1, 0], [1, 1], [1, 2]]
|
|
* result['values'].print(); // ['hello', 'world', 'a', 'b', 'c']
|
|
* result['shape'].print(); // [2, 3]
|
|
* ```
|
|
* @param input: 1-D. Strings to split.
|
|
* @param delimiter: 0-D. Delimiter characters, or empty string.
|
|
* @param skipEmpty: Optional. If true, skip the empty strings from the result.
|
|
* Defaults to true.
|
|
* @return A map with the following properties:
|
|
* - indices: A dense matrix of int32 representing the indices of the sparse
|
|
* tensor.
|
|
* - values: A vector of strings corresponding to the splited values.
|
|
* - shape: a length-2 vector of int32 representing the shape of the sparse
|
|
* tensor, where the first value is N and the second value is the maximum number
|
|
* of tokens in a single input entry.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'String'}
|
|
*/
|
|
function stringSplit_(input, delimiter, skipEmpty) {
|
|
if (skipEmpty === void 0) { skipEmpty = true; }
|
|
var $input = convertToTensor(input, 'input', 'stringSplit', 'string');
|
|
var $delimiter = convertToTensor(delimiter, 'delimiter', 'stringSplit', 'string');
|
|
if ($input.rank !== 1) {
|
|
throw new Error("Input should be Tensor1D but received shape ".concat($input.shape));
|
|
}
|
|
if ($delimiter.rank !== 0) {
|
|
throw new Error("Delimiter should be a scalar but received shape ".concat($delimiter.shape));
|
|
}
|
|
var attrs = { skipEmpty: skipEmpty };
|
|
var inputs = { input: $input, delimiter: $delimiter };
|
|
var result = ENGINE.runKernel(StringSplit, inputs, attrs);
|
|
return { indices: result[0], values: result[1], shape: result[2] };
|
|
}
|
|
var stringSplit = /* @__PURE__ */ op({ stringSplit_: stringSplit_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Converts each string in the input Tensor to its hash mod by a number of
|
|
* buckets.
|
|
*
|
|
* The hash function is deterministic on the content of the string within the
|
|
* process and will never change. However, it is not suitable for cryptography.
|
|
* This function may be used when CPU time is scarce and inputs are trusted or
|
|
* unimportant. There is a risk of adversaries constructing inputs that all hash
|
|
* to the same bucket.
|
|
*
|
|
* ```js
|
|
* const result = tf.string.stringToHashBucketFast(
|
|
* ['Hello', 'TensorFlow', '2.x'], 3);
|
|
* result.print(); // [0, 2, 2]
|
|
* ```
|
|
* @param input: The strings to assign a hash bucket.
|
|
* @param numBuckets: The number of buckets.
|
|
* @return A Tensor of the same shape as the input tensor.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'String'}
|
|
*/
|
|
function stringToHashBucketFast_(input, numBuckets) {
|
|
var $input = convertToTensor(input, 'input', 'stringToHashBucketFast', 'string');
|
|
var attrs = { numBuckets: numBuckets };
|
|
if (numBuckets <= 0) {
|
|
throw new Error("Number of buckets must be at least 1");
|
|
}
|
|
var inputs = { input: $input };
|
|
return ENGINE.runKernel(StringToHashBucketFast, inputs, attrs);
|
|
}
|
|
var stringToHashBucketFast = /* @__PURE__ */ op({ stringToHashBucketFast_: stringToHashBucketFast_ });
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Replace the match of a `pattern` in `input` with `rewrite`.
|
|
*
|
|
* ```js
|
|
* const result = tf.string.staticRegexReplace(
|
|
* ['format this spacing better'], ' +', ' ');
|
|
* result.print(); // ['format this spacing better']
|
|
* ```
|
|
* @param input: A Tensor of type string. The text to be processed.
|
|
* @param pattern: A string. The regular expression to match the input.
|
|
* @param rewrite: A string. The rewrite to be applied to the matched
|
|
* expression.
|
|
* @param replaceGlobal: An optional bool. Defaults to True. If True, the
|
|
* replacement is global, otherwise the replacement is done only on the
|
|
* first match.
|
|
* @return A Tensor of type string.
|
|
*
|
|
* @doc {heading: 'Operations', subheading: 'String'}
|
|
*/
|
|
function staticRegexReplace_(input, pattern, rewrite, replaceGlobal) {
|
|
if (replaceGlobal === void 0) { replaceGlobal = true; }
|
|
var $input = convertToTensor(input, 'input', 'staticRegexReplace', 'string');
|
|
var attrs = { pattern: pattern, rewrite: rewrite, replaceGlobal: replaceGlobal };
|
|
return ENGINE.runKernel(StaticRegexReplace, { x: $input }, attrs);
|
|
}
|
|
var staticRegexReplace = /* @__PURE__ */ op({ staticRegexReplace_: staticRegexReplace_ });
|
|
|
|
/**
|
|
* @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 spectral = {
|
|
fft: fft,
|
|
ifft: ifft,
|
|
rfft: rfft,
|
|
irfft: irfft
|
|
};
|
|
var signal = {
|
|
hammingWindow: hammingWindow,
|
|
hannWindow: hannWindow,
|
|
frame: frame,
|
|
stft: stft,
|
|
};
|
|
var image = {
|
|
flipLeftRight: flipLeftRight,
|
|
grayscaleToRGB: grayscaleToRGB,
|
|
resizeNearestNeighbor: resizeNearestNeighbor,
|
|
resizeBilinear: resizeBilinear,
|
|
rgbToGrayscale: rgbToGrayscale,
|
|
rotateWithOffset: rotateWithOffset,
|
|
cropAndResize: cropAndResize,
|
|
nonMaxSuppression: nonMaxSuppression,
|
|
nonMaxSuppressionAsync: nonMaxSuppressionAsync,
|
|
nonMaxSuppressionWithScore: nonMaxSuppressionWithScore,
|
|
nonMaxSuppressionWithScoreAsync: nonMaxSuppressionWithScoreAsync,
|
|
nonMaxSuppressionPadded: nonMaxSuppressionPadded,
|
|
nonMaxSuppressionPaddedAsync: nonMaxSuppressionPaddedAsync,
|
|
threshold: threshold,
|
|
transform: transform
|
|
};
|
|
var linalg = {
|
|
bandPart: bandPart,
|
|
gramSchmidt: gramSchmidt,
|
|
qr: qr
|
|
};
|
|
var losses = {
|
|
absoluteDifference: absoluteDifference,
|
|
computeWeightedLoss: computeWeightedLoss,
|
|
cosineDistance: cosineDistance,
|
|
hingeLoss: hingeLoss,
|
|
huberLoss: huberLoss,
|
|
logLoss: logLoss,
|
|
meanSquaredError: meanSquaredError,
|
|
sigmoidCrossEntropy: sigmoidCrossEntropy,
|
|
softmaxCrossEntropy: softmaxCrossEntropy
|
|
};
|
|
var sparse = {
|
|
sparseFillEmptyRows: sparseFillEmptyRows,
|
|
sparseReshape: sparseReshape,
|
|
sparseSegmentMean: sparseSegmentMean,
|
|
sparseSegmentSum: sparseSegmentSum
|
|
};
|
|
// tslint:disable-next-line:variable-name
|
|
var string = {
|
|
stringNGrams: stringNGrams,
|
|
stringSplit: stringSplit,
|
|
stringToHashBucketFast: stringToHashBucketFast,
|
|
staticRegexReplace: staticRegexReplace,
|
|
};
|
|
|
|
/**
|
|
* @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 tfOps = {
|
|
__proto__: null,
|
|
OP_SCOPE_SUFFIX: OP_SCOPE_SUFFIX,
|
|
abs: abs,
|
|
acos: acos,
|
|
acosh: acosh,
|
|
add: add,
|
|
addN: addN,
|
|
all: all,
|
|
any: any,
|
|
argMax: argMax,
|
|
argMin: argMin,
|
|
asin: asin,
|
|
asinh: asinh,
|
|
atan: atan,
|
|
atan2: atan2,
|
|
atanh: atanh,
|
|
avgPool: avgPool,
|
|
avgPool3d: avgPool3d,
|
|
basicLSTMCell: basicLSTMCell,
|
|
batchNorm: batchNorm,
|
|
batchNorm2d: batchNorm2d,
|
|
batchNorm3d: batchNorm3d,
|
|
batchNorm4d: batchNorm4d,
|
|
batchToSpaceND: batchToSpaceND,
|
|
bincount: bincount,
|
|
bitwiseAnd: bitwiseAnd,
|
|
booleanMaskAsync: booleanMaskAsync,
|
|
broadcastArgs: broadcastArgs,
|
|
broadcastTo: broadcastTo,
|
|
buffer: buffer,
|
|
cast: cast,
|
|
ceil: ceil,
|
|
clipByValue: clipByValue,
|
|
clone: clone,
|
|
complex: complex,
|
|
concat: concat,
|
|
concat1d: concat1d,
|
|
concat2d: concat2d,
|
|
concat3d: concat3d,
|
|
concat4d: concat4d,
|
|
conv1d: conv1d,
|
|
conv2d: conv2d$1,
|
|
conv2dTranspose: conv2dTranspose,
|
|
conv3d: conv3d,
|
|
conv3dTranspose: conv3dTranspose,
|
|
cos: cos,
|
|
cosh: cosh,
|
|
cosineWindow: cosineWindow,
|
|
cumprod: cumprod,
|
|
cumsum: cumsum,
|
|
denseBincount: denseBincount,
|
|
depthToSpace: depthToSpace,
|
|
depthwiseConv2d: depthwiseConv2d$1,
|
|
diag: diag,
|
|
dilation2d: dilation2d,
|
|
div: div,
|
|
divNoNan: divNoNan,
|
|
dot: dot,
|
|
dropout: dropout,
|
|
einsum: einsum,
|
|
elu: elu,
|
|
enclosingPowerOfTwo: enclosingPowerOfTwo,
|
|
ensureShape: ensureShape,
|
|
equal: equal,
|
|
erf: erf,
|
|
euclideanNorm: euclideanNorm,
|
|
exp: exp,
|
|
expandDims: expandDims,
|
|
expm1: expm1,
|
|
eye: eye,
|
|
fft: fft,
|
|
fill: fill,
|
|
floor: floor,
|
|
floorDiv: floorDiv,
|
|
fused: fused_ops,
|
|
gather: gather,
|
|
gatherND: gatherND,
|
|
greater: greater,
|
|
greaterEqual: greaterEqual,
|
|
ifft: ifft,
|
|
imag: imag,
|
|
image: image,
|
|
inTopKAsync: inTopKAsync,
|
|
irfft: irfft,
|
|
isFinite: isFinite$1,
|
|
isInf: isInf,
|
|
isNaN: isNaN$1,
|
|
leakyRelu: leakyRelu,
|
|
less: less,
|
|
lessEqual: lessEqual,
|
|
linalg: linalg,
|
|
linspace: linspace,
|
|
localResponseNormalization: localResponseNormalization,
|
|
log: log,
|
|
log1p: log1p,
|
|
logSigmoid: logSigmoid,
|
|
logSoftmax: logSoftmax,
|
|
logSumExp: logSumExp,
|
|
logicalAnd: logicalAnd,
|
|
logicalNot: logicalNot,
|
|
logicalOr: logicalOr,
|
|
logicalXor: logicalXor,
|
|
losses: losses,
|
|
lowerBound: lowerBound,
|
|
matMul: matMul$1,
|
|
max: max,
|
|
maxPool: maxPool,
|
|
maxPool3d: maxPool3d,
|
|
maxPoolWithArgmax: maxPoolWithArgmax,
|
|
maximum: maximum,
|
|
mean: mean,
|
|
meshgrid: meshgrid,
|
|
min: min,
|
|
minimum: minimum,
|
|
mirrorPad: mirrorPad,
|
|
mod: mod,
|
|
moments: moments,
|
|
movingAverage: movingAverage,
|
|
mul: mul,
|
|
multiRNNCell: multiRNNCell,
|
|
multinomial: multinomial,
|
|
neg: neg,
|
|
norm: norm,
|
|
notEqual: notEqual,
|
|
oneHot: oneHot,
|
|
ones: ones,
|
|
onesLike: onesLike,
|
|
op: op,
|
|
outerProduct: outerProduct,
|
|
pad: pad,
|
|
pad1d: pad1d,
|
|
pad2d: pad2d,
|
|
pad3d: pad3d,
|
|
pad4d: pad4d,
|
|
pool: pool,
|
|
pow: pow,
|
|
prelu: prelu,
|
|
print: print,
|
|
prod: prod,
|
|
raggedGather: raggedGather,
|
|
raggedRange: raggedRange,
|
|
raggedTensorToTensor: raggedTensorToTensor,
|
|
rand: rand,
|
|
randomGamma: randomGamma,
|
|
randomNormal: randomNormal,
|
|
randomStandardNormal: randomStandardNormal,
|
|
randomUniform: randomUniform,
|
|
randomUniformInt: randomUniformInt,
|
|
range: range,
|
|
real: real,
|
|
reciprocal: reciprocal,
|
|
relu: relu,
|
|
relu6: relu6,
|
|
reshape: reshape,
|
|
reverse: reverse,
|
|
reverse1d: reverse1d,
|
|
reverse2d: reverse2d,
|
|
reverse3d: reverse3d,
|
|
reverse4d: reverse4d,
|
|
rfft: rfft,
|
|
round: round,
|
|
rsqrt: rsqrt,
|
|
scalar: scalar,
|
|
scatterND: scatterND,
|
|
searchSorted: searchSorted,
|
|
selu: selu,
|
|
separableConv2d: separableConv2d,
|
|
setdiff1dAsync: setdiff1dAsync,
|
|
sigmoid: sigmoid,
|
|
sign: sign,
|
|
signal: signal,
|
|
sin: sin,
|
|
sinh: sinh,
|
|
slice: slice,
|
|
slice1d: slice1d,
|
|
slice2d: slice2d,
|
|
slice3d: slice3d,
|
|
slice4d: slice4d,
|
|
softmax: softmax,
|
|
softplus: softplus,
|
|
spaceToBatchND: spaceToBatchND,
|
|
sparse: sparse,
|
|
sparseToDense: sparseToDense,
|
|
spectral: spectral,
|
|
split: split$1,
|
|
sqrt: sqrt,
|
|
square: square,
|
|
squaredDifference: squaredDifference,
|
|
squeeze: squeeze,
|
|
stack: stack,
|
|
step: step,
|
|
stridedSlice: stridedSlice,
|
|
string: string,
|
|
sub: sub,
|
|
sum: sum,
|
|
tan: tan,
|
|
tanh: tanh,
|
|
tensor: tensor,
|
|
tensor1d: tensor1d,
|
|
tensor2d: tensor2d,
|
|
tensor3d: tensor3d,
|
|
tensor4d: tensor4d,
|
|
tensor5d: tensor5d,
|
|
tensor6d: tensor6d,
|
|
tensorScatterUpdate: tensorScatterUpdate,
|
|
tile: tile,
|
|
topk: topk,
|
|
transpose: transpose,
|
|
truncatedNormal: truncatedNormal,
|
|
unique: unique,
|
|
unsortedSegmentSum: unsortedSegmentSum,
|
|
unstack: unstack,
|
|
upperBound: upperBound,
|
|
variable: variable,
|
|
where: where,
|
|
whereAsync: whereAsync,
|
|
zeros: zeros,
|
|
zerosLike: zerosLike
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$k = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'BiasAdd':
|
|
case 'AddV2':
|
|
case 'Add': {
|
|
return [ops.add(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'AddN': {
|
|
return [ops.addN(getParamValue('tensors', node, tensorMap, context))];
|
|
}
|
|
case 'FloorMod':
|
|
case 'Mod':
|
|
return [ops.mod(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
case 'Mul':
|
|
return [ops.mul(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
case 'RealDiv':
|
|
case 'Div': {
|
|
return [ops.div(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'DivNoNan': {
|
|
return [ops.divNoNan(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'FloorDiv': {
|
|
return [ops.floorDiv(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Sub': {
|
|
return [ops.sub(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Minimum': {
|
|
return [ops.minimum(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Maximum': {
|
|
return [ops.maximum(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Pow': {
|
|
return [ops.pow(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'SquaredDifference': {
|
|
return [ops.squaredDifference(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$j = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Abs':
|
|
case 'ComplexAbs':
|
|
return [ops.abs(getParamValue('x', node, tensorMap, context))];
|
|
case 'Acos':
|
|
return [ops.acos(getParamValue('x', node, tensorMap, context))];
|
|
case 'Acosh':
|
|
return [ops.acosh(getParamValue('x', node, tensorMap, context))];
|
|
case 'Asin':
|
|
return [ops.asin(getParamValue('x', node, tensorMap, context))];
|
|
case 'Asinh':
|
|
return [ops.asinh(getParamValue('x', node, tensorMap, context))];
|
|
case 'Atan':
|
|
return [ops.atan(getParamValue('x', node, tensorMap, context))];
|
|
case 'Atan2':
|
|
return [ops.atan2(getParamValue('x', node, tensorMap, context), getParamValue('y', node, tensorMap, context))];
|
|
case 'Atanh':
|
|
return [ops.atanh(getParamValue('x', node, tensorMap, context))];
|
|
case 'Ceil':
|
|
return [ops.ceil(getParamValue('x', node, tensorMap, context))];
|
|
case 'Complex':
|
|
return [ops.complex(getParamValue('real', node, tensorMap, context), getParamValue('imag', node, tensorMap, context))];
|
|
case 'Cos':
|
|
return [ops.cos(getParamValue('x', node, tensorMap, context))];
|
|
case 'Cosh':
|
|
return [ops.cosh(getParamValue('x', node, tensorMap, context))];
|
|
case 'Elu':
|
|
return [ops.elu(getParamValue('x', node, tensorMap, context))];
|
|
case 'Erf':
|
|
return [ops.erf(getParamValue('x', node, tensorMap, context))];
|
|
case 'Exp':
|
|
return [ops.exp(getParamValue('x', node, tensorMap, context))];
|
|
case 'Expm1': {
|
|
return [ops.expm1(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Floor':
|
|
return [ops.floor(getParamValue('x', node, tensorMap, context))];
|
|
case 'Log':
|
|
return [ops.log(getParamValue('x', node, tensorMap, context))];
|
|
case 'Log1p': {
|
|
return [ops.log1p(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Imag':
|
|
return [ops.imag(getParamValue('x', node, tensorMap, context))];
|
|
case 'Neg':
|
|
return [ops.neg(getParamValue('x', node, tensorMap, context))];
|
|
case 'Reciprocal': {
|
|
return [ops.reciprocal(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Real':
|
|
return [ops.real(getParamValue('x', node, tensorMap, context))];
|
|
case 'Relu':
|
|
return [ops.relu(getParamValue('x', node, tensorMap, context))];
|
|
case 'Round': {
|
|
return [ops.round(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Selu':
|
|
return [ops.selu(getParamValue('x', node, tensorMap, context))];
|
|
case 'Sigmoid':
|
|
return [ops.sigmoid(getParamValue('x', node, tensorMap, context))];
|
|
case 'Sin':
|
|
return [ops.sin(getParamValue('x', node, tensorMap, context))];
|
|
case 'Sign': {
|
|
return [ops.sign(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Sinh': {
|
|
return [ops.sinh(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Softplus': {
|
|
return [ops.softplus(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Sqrt': {
|
|
return [ops.sqrt(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Square': {
|
|
return [ops.square(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Tanh': {
|
|
return [ops.tanh(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'Tan':
|
|
return [ops.tan(getParamValue('x', node, tensorMap, context))];
|
|
case 'ClipByValue':
|
|
return [ops.clipByValue(getParamValue('x', node, tensorMap, context), getParamValue('clipValueMin', node, tensorMap, context), getParamValue('clipValueMax', node, tensorMap, context))];
|
|
case 'Relu6':
|
|
return [ops.relu6(getParamValue('x', node, tensorMap, context))];
|
|
case 'Rsqrt':
|
|
return [ops.rsqrt(getTensor(node.inputNames[0], tensorMap, context))];
|
|
case 'LeakyRelu':
|
|
return [ops.leakyRelu(getParamValue('x', node, tensorMap, context), getParamValue('alpha', node, tensorMap, context))];
|
|
case 'Prelu':
|
|
return [ops.prelu(getParamValue('x', node, tensorMap, context), getParamValue('alpha', node, tensorMap, context))];
|
|
case 'IsNan':
|
|
return [ops.isNaN(getTensor(node.inputNames[0], tensorMap, context))];
|
|
case 'IsInf':
|
|
return [ops.isInf(getTensor(node.inputNames[0], tensorMap, context))];
|
|
case 'IsFinite':
|
|
return [ops.isFinite(getTensor(node.inputNames[0], tensorMap, context))];
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/**
|
|
* Used by TensorList and TensorArray to verify if elementShape matches, support
|
|
* negative value as the dim shape.
|
|
* @param shapeA
|
|
* @param shapeB
|
|
* @param errorMessagePrefix
|
|
*/
|
|
function assertShapesMatchAllowUndefinedSize(shapeA, shapeB, errorMessagePrefix) {
|
|
if (errorMessagePrefix === void 0) { errorMessagePrefix = ''; }
|
|
// constant shape means unknown rank
|
|
if (typeof shapeA === 'number' || typeof shapeB === 'number') {
|
|
return;
|
|
}
|
|
tfc.util.assert(shapeA.length === shapeB.length, function () { return errorMessagePrefix + " Shapes ".concat(shapeA, " and ").concat(shapeB, " must match"); });
|
|
for (var i = 0; i < shapeA.length; i++) {
|
|
var dim0 = shapeA[i];
|
|
var dim1 = shapeB[i];
|
|
tfc.util.assert(dim0 < 0 || dim1 < 0 || dim0 === dim1, function () { return errorMessagePrefix + " Shapes ".concat(shapeA, " and ").concat(shapeB, " must match"); });
|
|
}
|
|
}
|
|
function fullDefinedShape(elementShape) {
|
|
if (typeof elementShape === 'number' || elementShape.some(function (dim) { return dim < 0; })) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* Generate the output element shape from the list elementShape, list tensors
|
|
* and input param.
|
|
* @param listElementShape
|
|
* @param tensors
|
|
* @param elementShape
|
|
*/
|
|
function inferElementShape(listElementShape, tensors, elementShape) {
|
|
var partialShape = mergeElementShape(listElementShape, elementShape);
|
|
var notfullDefinedShape = !fullDefinedShape(partialShape);
|
|
if (notfullDefinedShape && tensors.length === 0) {
|
|
throw new Error("Tried to calculate elements of an empty list" +
|
|
" with non-fully-defined elementShape: ".concat(partialShape));
|
|
}
|
|
if (notfullDefinedShape) {
|
|
tensors.forEach(function (tensor) {
|
|
partialShape = mergeElementShape(tensor.shape, partialShape);
|
|
});
|
|
}
|
|
if (!fullDefinedShape(partialShape)) {
|
|
throw new Error("Non-fully-defined elementShape: ".concat(partialShape));
|
|
}
|
|
return partialShape;
|
|
}
|
|
function mergeElementShape(elementShapeA, elementShapeB) {
|
|
if (typeof elementShapeA === 'number') {
|
|
return elementShapeB;
|
|
}
|
|
if (typeof elementShapeB === 'number') {
|
|
return elementShapeA;
|
|
}
|
|
if (elementShapeA.length !== elementShapeB.length) {
|
|
throw new Error("Incompatible ranks during merge: ".concat(elementShapeA, " vs. ").concat(elementShapeB));
|
|
}
|
|
var result = [];
|
|
for (var i = 0; i < elementShapeA.length; ++i) {
|
|
var dim0 = elementShapeA[i];
|
|
var dim1 = elementShapeB[i];
|
|
if (dim0 >= 0 && dim1 >= 0 && dim0 !== dim1) {
|
|
throw new Error("Incompatible shape during merge: ".concat(elementShapeA, " vs. ").concat(elementShapeB));
|
|
}
|
|
result[i] = dim0 >= 0 ? dim0 : dim1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* The TensorArray object keeps an array of Tensors. It
|
|
* allows reading from the array and writing to the array.
|
|
*/
|
|
var TensorArray = /** @class */ (function () {
|
|
function TensorArray(name, dtype, maxSize, elementShape, identicalElementShapes, dynamicSize, clearAfterRead) {
|
|
this.name = name;
|
|
this.dtype = dtype;
|
|
this.maxSize = maxSize;
|
|
this.elementShape = elementShape;
|
|
this.identicalElementShapes = identicalElementShapes;
|
|
this.dynamicSize = dynamicSize;
|
|
this.clearAfterRead = clearAfterRead;
|
|
this.tensors = [];
|
|
this.closed_ = false;
|
|
this.idTensor = tfc.scalar(0);
|
|
tfc.keep(this.idTensor);
|
|
}
|
|
Object.defineProperty(TensorArray.prototype, "id", {
|
|
get: function () {
|
|
return this.idTensor.id;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(TensorArray.prototype, "closed", {
|
|
get: function () {
|
|
return this.closed_;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Dispose the tensors and idTensor and mark the TensoryArray as closed.
|
|
*/
|
|
TensorArray.prototype.clearAndClose = function (keepIds) {
|
|
this.tensors.forEach(function (tensor) {
|
|
if (keepIds == null || !keepIds.has(tensor.tensor.id)) {
|
|
tensor.tensor.dispose();
|
|
}
|
|
});
|
|
this.tensors = [];
|
|
this.closed_ = true;
|
|
this.idTensor.dispose();
|
|
};
|
|
TensorArray.prototype.size = function () {
|
|
return this.tensors.length;
|
|
};
|
|
/**
|
|
* Read the value at location index in the TensorArray.
|
|
* @param index Number the index to read from.
|
|
*/
|
|
TensorArray.prototype.read = function (index) {
|
|
if (this.closed_) {
|
|
throw new Error("TensorArray ".concat(this.name, " has already been closed."));
|
|
}
|
|
if (index < 0 || index >= this.size()) {
|
|
throw new Error("Tried to read from index ".concat(index, ", but array size is: ").concat(this.size()));
|
|
}
|
|
var tensorWithState = this.tensors[index];
|
|
if (tensorWithState.cleared) {
|
|
throw new Error("TensorArray ".concat(this.name, ": Could not read index ").concat(index, " twice because it was cleared after a previous read ") +
|
|
"(perhaps try setting clear_after_read = false?).");
|
|
}
|
|
if (this.clearAfterRead) {
|
|
tensorWithState.cleared = true;
|
|
}
|
|
tensorWithState.read = true;
|
|
return tensorWithState.tensor;
|
|
};
|
|
/**
|
|
* Helper method to read multiple tensors from the specified indices.
|
|
*/
|
|
TensorArray.prototype.readMany = function (indices) {
|
|
var _this = this;
|
|
return indices.map(function (index) { return _this.read(index); });
|
|
};
|
|
/**
|
|
* Write value into the index of the TensorArray.
|
|
* @param index number the index to write to.
|
|
* @param tensor
|
|
*/
|
|
TensorArray.prototype.write = function (index, tensor) {
|
|
if (this.closed_) {
|
|
throw new Error("TensorArray ".concat(this.name, " has already been closed."));
|
|
}
|
|
if (index < 0 || !this.dynamicSize && index >= this.maxSize) {
|
|
throw new Error("Tried to write to index ".concat(index, ", but array is not resizeable and size is: ").concat(this.maxSize));
|
|
}
|
|
var t = this.tensors[index] || {};
|
|
if (tensor.dtype !== this.dtype) {
|
|
throw new Error("TensorArray ".concat(this.name, ": Could not write to TensorArray index ").concat(index, ",\n because the value dtype is ").concat(tensor.dtype, ", but TensorArray dtype is ").concat(this.dtype, "."));
|
|
}
|
|
// Set the shape for the first time write to unknow shape tensor array
|
|
if (this.size() === 0 &&
|
|
(this.elementShape == null || this.elementShape.length === 0)) {
|
|
this.elementShape = tensor.shape;
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, tensor.shape, "TensorArray ".concat(this.name, ": Could not write to TensorArray index ").concat(index, "."));
|
|
if (t.read) {
|
|
throw new Error("TensorArray ".concat(this.name, ": Could not write to TensorArray index ").concat(index, ", because it has already been read."));
|
|
}
|
|
if (t.written) {
|
|
throw new Error("TensorArray ".concat(this.name, ": Could not write to TensorArray index ").concat(index, ", because it has already been written."));
|
|
}
|
|
t.tensor = tensor;
|
|
tfc.keep(tensor);
|
|
t.written = true;
|
|
this.tensors[index] = t;
|
|
};
|
|
/**
|
|
* Helper method to write multiple tensors to the specified indices.
|
|
*/
|
|
TensorArray.prototype.writeMany = function (indices, tensors) {
|
|
var _this = this;
|
|
if (indices.length !== tensors.length) {
|
|
throw new Error("TensorArray ".concat(this.name, ": could not write multiple tensors,") +
|
|
"because the index size: ".concat(indices.length, " is not the same as tensors size: ").concat(tensors.length, "."));
|
|
}
|
|
indices.forEach(function (i, index) { return _this.write(i, tensors[index]); });
|
|
};
|
|
/**
|
|
* Return selected values in the TensorArray as a packed Tensor. All of
|
|
* selected values must have been written and their shapes must all match.
|
|
* @param [indices] number[] Optional. Taking values in [0, max_value). If the
|
|
* TensorArray is not dynamic, max_value=size(). If not specified returns
|
|
* all tensors in the original order.
|
|
* @param [dtype]
|
|
*/
|
|
TensorArray.prototype.gather = function (indices, dtype) {
|
|
if (!!dtype && dtype !== this.dtype) {
|
|
throw new Error("TensorArray dtype is ".concat(this.dtype, " but gather requested dtype ").concat(dtype));
|
|
}
|
|
if (!indices) {
|
|
indices = [];
|
|
for (var i = 0; i < this.size(); i++) {
|
|
indices.push(i);
|
|
}
|
|
}
|
|
else {
|
|
indices = indices.slice(0, this.size());
|
|
}
|
|
if (indices.length === 0) {
|
|
return tfc.tensor([], [0].concat(this.elementShape));
|
|
}
|
|
// Read all the PersistentTensors into a vector to keep track of
|
|
// their memory.
|
|
var tensors = this.readMany(indices);
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, tensors[0].shape, 'TensorArray shape mismatch: ');
|
|
return tfc.stack(tensors, 0);
|
|
};
|
|
/**
|
|
* Return the values in the TensorArray as a concatenated Tensor.
|
|
*/
|
|
TensorArray.prototype.concat = function (dtype) {
|
|
if (!!dtype && dtype !== this.dtype) {
|
|
throw new Error("TensorArray dtype is ".concat(this.dtype, " but concat requested dtype ").concat(dtype));
|
|
}
|
|
if (this.size() === 0) {
|
|
return tfc.tensor([], [0].concat(this.elementShape));
|
|
}
|
|
var indices = [];
|
|
for (var i = 0; i < this.size(); i++) {
|
|
indices.push(i);
|
|
}
|
|
// Collect all the tensors from the tensors array.
|
|
var tensors = this.readMany(indices);
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, tensors[0].shape, "TensorArray shape mismatch: tensor array shape (".concat(this.elementShape, ") vs first tensor shape (").concat(tensors[0].shape, ")"));
|
|
return tfc.concat(tensors, 0);
|
|
};
|
|
/**
|
|
* Scatter the values of a Tensor in specific indices of a TensorArray.
|
|
* @param indices number[] values in [0, max_value). If the
|
|
* TensorArray is not dynamic, max_value=size().
|
|
* @param tensor Tensor input tensor.
|
|
*/
|
|
TensorArray.prototype.scatter = function (indices, tensor) {
|
|
if (tensor.dtype !== this.dtype) {
|
|
throw new Error("TensorArray dtype is ".concat(this.dtype, " but tensor has dtype ").concat(tensor.dtype));
|
|
}
|
|
if (indices.length !== tensor.shape[0]) {
|
|
throw new Error("Expected len(indices) == tensor.shape[0], but saw: ".concat(indices.length, " vs. ").concat(tensor.shape[0]));
|
|
}
|
|
var maxIndex = Math.max.apply(Math, __spreadArray([], __read(indices), false));
|
|
if (!this.dynamicSize && maxIndex >= this.maxSize) {
|
|
throw new Error("Max index must be < array size (".concat(maxIndex, " vs. ").concat(this.maxSize, ")"));
|
|
}
|
|
this.writeMany(indices, tfc.unstack(tensor, 0));
|
|
};
|
|
/**
|
|
* Split the values of a Tensor into the TensorArray.
|
|
* @param length number[] with the lengths to use when splitting value along
|
|
* its first dimension.
|
|
* @param tensor Tensor, the tensor to split.
|
|
*/
|
|
TensorArray.prototype.split = function (length, tensor) {
|
|
var _this = this;
|
|
if (tensor.dtype !== this.dtype) {
|
|
throw new Error("TensorArray dtype is ".concat(this.dtype, " but tensor has dtype ").concat(tensor.dtype));
|
|
}
|
|
var totalLength = 0;
|
|
var cumulativeLengths = length.map(function (len) {
|
|
totalLength += len;
|
|
return totalLength;
|
|
});
|
|
if (totalLength !== tensor.shape[0]) {
|
|
throw new Error("Expected sum of lengths to be equal to\n tensor.shape[0], but sum of lengths is\n ".concat(totalLength, ", and tensor's shape is: ").concat(tensor.shape));
|
|
}
|
|
if (!this.dynamicSize && length.length !== this.maxSize) {
|
|
throw new Error("TensorArray's size is not equal to the size of lengths (".concat(this.maxSize, " vs. ").concat(length.length, "), ") +
|
|
'and the TensorArray is not marked as dynamically resizeable');
|
|
}
|
|
var elementPerRow = totalLength === 0 ? 0 : tensor.size / totalLength;
|
|
var tensors = [];
|
|
tfc.tidy(function () {
|
|
tensor = tfc.reshape(tensor, [1, totalLength, elementPerRow]);
|
|
for (var i = 0; i < length.length; ++i) {
|
|
var previousLength = (i === 0) ? 0 : cumulativeLengths[i - 1];
|
|
var indices_1 = [0, previousLength, 0];
|
|
var sizes = [1, length[i], elementPerRow];
|
|
tensors[i] = tfc.reshape(tfc.slice(tensor, indices_1, sizes), _this.elementShape);
|
|
}
|
|
return tensors;
|
|
});
|
|
var indices = [];
|
|
for (var i = 0; i < length.length; i++) {
|
|
indices[i] = i;
|
|
}
|
|
this.writeMany(indices, tensors);
|
|
};
|
|
return TensorArray;
|
|
}());
|
|
|
|
/**
|
|
* TensorList stores a container of `tf.Tensor` objects, which are accessible
|
|
* via tensors field.
|
|
*
|
|
* In order to get a copy of the underlying list, use the copy method:
|
|
* ```
|
|
* TensorList b = a.copy();
|
|
* b.tensors().pushBack(t); // This does not modify a.tensors().
|
|
* ```
|
|
*
|
|
* Note that this is not a deep copy: the memory locations of the underlying
|
|
* tensors will still point to the same locations of the corresponding tensors
|
|
* in the original.
|
|
*/
|
|
var TensorList = /** @class */ (function () {
|
|
/**
|
|
*
|
|
* @param tensors list of tensors
|
|
* @param elementShape shape of each tensor, this can be a single number (any
|
|
* shape is allowed) or partial shape (dim = -1).
|
|
* @param elementDtype data type of each tensor
|
|
* @param maxNumElements The maximum allowed size of `tensors`. Defaults to -1
|
|
* meaning that the size of `tensors` is unbounded.
|
|
*/
|
|
function TensorList(tensors, elementShape, elementDtype, maxNumElements) {
|
|
if (maxNumElements === void 0) { maxNumElements = -1; }
|
|
this.tensors = tensors;
|
|
this.elementShape = elementShape;
|
|
this.elementDtype = elementDtype;
|
|
if (tensors != null) {
|
|
tensors.forEach(function (tensor) {
|
|
if (elementDtype !== tensor.dtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(elementDtype, ", but list elements ").concat(tensor.dtype));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(elementShape, tensor.shape, 'TensorList shape mismatch: ');
|
|
tfc.keep(tensor);
|
|
});
|
|
}
|
|
this.idTensor = tfc.scalar(0);
|
|
this.maxNumElements = maxNumElements;
|
|
tfc.keep(this.idTensor);
|
|
}
|
|
Object.defineProperty(TensorList.prototype, "id", {
|
|
get: function () {
|
|
return this.idTensor.id;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Get a new TensorList containing a copy of the underlying tensor container.
|
|
*/
|
|
TensorList.prototype.copy = function () {
|
|
return new TensorList(__spreadArray([], __read(this.tensors), false), this.elementShape, this.elementDtype);
|
|
};
|
|
/**
|
|
* Dispose the tensors and idTensor and clear the tensor list.
|
|
*/
|
|
TensorList.prototype.clearAndClose = function (keepIds) {
|
|
this.tensors.forEach(function (tensor) {
|
|
if (keepIds == null || !keepIds.has(tensor.id)) {
|
|
tensor.dispose();
|
|
}
|
|
});
|
|
this.tensors.length = 0;
|
|
this.idTensor.dispose();
|
|
};
|
|
/**
|
|
* The size of the tensors in the tensor list.
|
|
*/
|
|
TensorList.prototype.size = function () {
|
|
return this.tensors.length;
|
|
};
|
|
/**
|
|
* Return a tensor that stacks a list of rank-R tf.Tensors into one rank-(R+1)
|
|
* tf.Tensor.
|
|
* @param elementShape shape of each tensor
|
|
* @param elementDtype data type of each tensor
|
|
* @param numElements the number of elements to stack
|
|
*/
|
|
TensorList.prototype.stack = function (elementShape, elementDtype, numElements) {
|
|
var _this = this;
|
|
if (numElements === void 0) { numElements = -1; }
|
|
if (elementDtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(elementDtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
if (numElements !== -1 && this.tensors.length !== numElements) {
|
|
throw new Error("Operation expected a list with ".concat(numElements, " elements but got a list with ").concat(this.tensors.length, " elements."));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(elementShape, this.elementShape, 'TensorList shape mismatch: ');
|
|
var outputElementShape = inferElementShape(this.elementShape, this.tensors, elementShape);
|
|
return tfc.tidy(function () {
|
|
var reshapedTensors = _this.tensors.map(function (tensor) { return tfc.reshape(tensor, outputElementShape); });
|
|
return tfc.stack(reshapedTensors, 0);
|
|
});
|
|
};
|
|
/**
|
|
* Pop a tensor from the end of the list.
|
|
* @param elementShape shape of the tensor
|
|
* @param elementDtype data type of the tensor
|
|
*/
|
|
TensorList.prototype.popBack = function (elementShape, elementDtype) {
|
|
if (elementDtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(elementDtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
if (this.size() === 0) {
|
|
throw new Error('Trying to pop from an empty list.');
|
|
}
|
|
var outputElementShape = inferElementShape(this.elementShape, this.tensors, elementShape);
|
|
var tensor = this.tensors.pop();
|
|
tensor.kept = false;
|
|
assertShapesMatchAllowUndefinedSize(tensor.shape, elementShape, 'TensorList shape mismatch: ');
|
|
return tfc.reshape(tensor, outputElementShape);
|
|
};
|
|
/**
|
|
* Push a tensor to the end of the list.
|
|
* @param tensor Tensor to be pushed.
|
|
*/
|
|
TensorList.prototype.pushBack = function (tensor) {
|
|
if (tensor.dtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(tensor.dtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(tensor.shape, this.elementShape, 'TensorList shape mismatch: ');
|
|
if (this.maxNumElements === this.size()) {
|
|
throw new Error("Trying to push element into a full list.");
|
|
}
|
|
tfc.keep(tensor);
|
|
this.tensors.push(tensor);
|
|
};
|
|
/**
|
|
* Update the size of the list.
|
|
* @param size the new size of the list.
|
|
*/
|
|
TensorList.prototype.resize = function (size) {
|
|
if (size < 0) {
|
|
throw new Error("TensorListResize expects size to be non-negative. Got: ".concat(size));
|
|
}
|
|
if (this.maxNumElements !== -1 && size > this.maxNumElements) {
|
|
throw new Error("TensorListResize input size ".concat(size, " is greater maxNumElement ").concat(this.maxNumElements, "."));
|
|
}
|
|
var destTensorList = new TensorList([], this.elementShape, this.elementDtype, this.maxNumElements);
|
|
destTensorList.tensors.length = size;
|
|
for (var i = 0; i < Math.min(this.tensors.length, size); ++i) {
|
|
destTensorList.tensors[i] = this.tensors[i];
|
|
}
|
|
return destTensorList;
|
|
};
|
|
/**
|
|
* Retrieve the element at the provided index
|
|
* @param elementShape shape of the tensor
|
|
* @param elementDtype dtype of the tensor
|
|
* @param elementIndex index of the tensor
|
|
*/
|
|
TensorList.prototype.getItem = function (elementIndex, elementShape, elementDtype) {
|
|
if (elementDtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(elementDtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
if (elementIndex < 0 || elementIndex > this.tensors.length) {
|
|
throw new Error("Trying to access element ".concat(elementIndex, " in a list with ").concat(this.tensors.length, " elements."));
|
|
}
|
|
if (this.tensors[elementIndex] == null) {
|
|
throw new Error("element at index ".concat(elementIndex, " is null."));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(this.tensors[elementIndex].shape, elementShape, 'TensorList shape mismatch: ');
|
|
var outputElementShape = inferElementShape(this.elementShape, this.tensors, elementShape);
|
|
return tfc.reshape(this.tensors[elementIndex], outputElementShape);
|
|
};
|
|
/**
|
|
* Set the tensor at the index
|
|
* @param elementIndex index of the tensor
|
|
* @param tensor the tensor to be inserted into the list
|
|
*/
|
|
TensorList.prototype.setItem = function (elementIndex, tensor) {
|
|
if (tensor.dtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(tensor.dtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
if (elementIndex < 0 ||
|
|
this.maxNumElements !== -1 && elementIndex >= this.maxNumElements) {
|
|
throw new Error("Trying to set element ".concat(elementIndex, " in a list with max ").concat(this.maxNumElements, " elements."));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, tensor.shape, 'TensorList shape mismatch: ');
|
|
tfc.keep(tensor);
|
|
// dispose the previous value if it is replacing.
|
|
if (this.tensors[elementIndex] != null) {
|
|
this.tensors[elementIndex].kept = false;
|
|
}
|
|
this.tensors[elementIndex] = tensor;
|
|
};
|
|
/**
|
|
* Return selected values in the TensorList as a stacked Tensor. All of
|
|
* selected values must have been written and their shapes must all match.
|
|
* @param indices indices of tensors to gather
|
|
* @param elementDtype output tensor dtype
|
|
* @param elementShape output tensor element shape
|
|
*/
|
|
TensorList.prototype.gather = function (indices, elementDtype, elementShape) {
|
|
var _this = this;
|
|
if (elementDtype !== this.elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(elementDtype, ", but list elements ").concat(this.elementDtype));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, elementShape, 'TensorList shape mismatch: ');
|
|
// When indices is greater than the size of the list, indices beyond the
|
|
// size of the list are ignored.
|
|
indices = indices.slice(0, this.size());
|
|
var outputElementShape = inferElementShape(this.elementShape, this.tensors, elementShape);
|
|
if (indices.length === 0) {
|
|
return tfc.tensor([], [0].concat(outputElementShape));
|
|
}
|
|
return tfc.tidy(function () {
|
|
var tensors = indices.map(function (i) { return tfc.reshape(_this.tensors[i], outputElementShape); });
|
|
return tfc.stack(tensors, 0);
|
|
});
|
|
};
|
|
/**
|
|
* Return the values in the TensorList as a concatenated Tensor.
|
|
* @param elementDtype output tensor dtype
|
|
* @param elementShape output tensor element shape
|
|
*/
|
|
TensorList.prototype.concat = function (elementDtype, elementShape) {
|
|
var _this = this;
|
|
if (!!elementDtype && elementDtype !== this.elementDtype) {
|
|
throw new Error("TensorList dtype is ".concat(this.elementDtype, " but concat requested dtype ").concat(elementDtype));
|
|
}
|
|
assertShapesMatchAllowUndefinedSize(this.elementShape, elementShape, 'TensorList shape mismatch: ');
|
|
var outputElementShape = inferElementShape(this.elementShape, this.tensors, elementShape);
|
|
if (this.size() === 0) {
|
|
return tfc.tensor([], [0].concat(outputElementShape));
|
|
}
|
|
return tfc.tidy(function () {
|
|
var tensors = _this.tensors.map(function (t) { return tfc.reshape(t, outputElementShape); });
|
|
return tfc.concat(tensors, 0);
|
|
});
|
|
};
|
|
return TensorList;
|
|
}());
|
|
/**
|
|
* Creates a TensorList which, when stacked, has the value of tensor.
|
|
* @param tensor from tensor
|
|
* @param elementShape output tensor element shape
|
|
*/
|
|
function fromTensor(tensor, elementShape, elementDtype) {
|
|
var dtype = tensor.dtype;
|
|
if (tensor.shape.length < 1) {
|
|
throw new Error("Tensor must be at least a vector, but saw shape: ".concat(tensor.shape));
|
|
}
|
|
if (tensor.dtype !== elementDtype) {
|
|
throw new Error("Invalid data types; op elements ".concat(tensor.dtype, ", but list elements ").concat(elementDtype));
|
|
}
|
|
var tensorElementShape = tensor.shape.slice(1);
|
|
assertShapesMatchAllowUndefinedSize(tensorElementShape, elementShape, 'TensorList shape mismatch: ');
|
|
var tensorList = tfc.unstack(tensor);
|
|
return new TensorList(tensorList, elementShape, dtype);
|
|
}
|
|
/**
|
|
* Return a TensorList of the given size with empty elements.
|
|
* @param elementShape the shape of the future elements of the list
|
|
* @param elementDtype the desired type of elements in the list
|
|
* @param numElements the number of elements to reserve
|
|
* @param maxNumElements the maximum number of elements in th list
|
|
*/
|
|
function reserve(elementShape, elementDtype, numElements, maxNumElements) {
|
|
return new TensorList([], elementShape, elementDtype, maxNumElements);
|
|
}
|
|
/**
|
|
* Put tensors at specific indices of a stacked tensor into a TensorList.
|
|
* @param indices list of indices on how to scatter the tensor.
|
|
* @param tensor input tensor.
|
|
* @param elementShape the shape of the future elements of the list
|
|
* @param numElements the number of elements to scatter
|
|
*/
|
|
function scatter(tensor, indices, elementShape, numElements) {
|
|
if (indices.length !== tensor.shape[0]) {
|
|
throw new Error("Expected len(indices) == tensor.shape[0], but saw: ".concat(indices.length, " vs. ").concat(tensor.shape[0]));
|
|
}
|
|
var maxIndex = Math.max.apply(Math, __spreadArray([], __read(indices), false));
|
|
if (numElements != null && numElements !== -1 && maxIndex >= numElements) {
|
|
throw new Error("Max index must be < array size (".concat(maxIndex, " vs. ").concat(numElements, ")"));
|
|
}
|
|
var list = new TensorList([], elementShape, tensor.dtype, numElements);
|
|
var tensors = tfc.unstack(tensor, 0);
|
|
indices.forEach(function (value, index) {
|
|
list.setItem(value, tensors[index]);
|
|
});
|
|
return list;
|
|
}
|
|
/**
|
|
* Split the values of a Tensor into a TensorList.
|
|
* @param length the lengths to use when splitting value along
|
|
* its first dimension.
|
|
* @param tensor the tensor to split.
|
|
* @param elementShape the shape of the future elements of the list
|
|
*/
|
|
function split(tensor, length, elementShape) {
|
|
var totalLength = 0;
|
|
var cumulativeLengths = length.map(function (len) {
|
|
totalLength += len;
|
|
return totalLength;
|
|
});
|
|
if (totalLength !== tensor.shape[0]) {
|
|
throw new Error("Expected sum of lengths to be equal to\n tensor.shape[0], but sum of lengths is\n ".concat(totalLength, ", and tensor's shape is: ").concat(tensor.shape));
|
|
}
|
|
var shapeWithoutFirstDim = tensor.shape.slice(1);
|
|
var outputElementShape = mergeElementShape(shapeWithoutFirstDim, elementShape);
|
|
var elementPerRow = totalLength === 0 ? 0 : tensor.size / totalLength;
|
|
var tensors = tfc.tidy(function () {
|
|
var tensors = [];
|
|
tensor = tfc.reshape(tensor, [1, totalLength, elementPerRow]);
|
|
for (var i = 0; i < length.length; ++i) {
|
|
var previousLength = (i === 0) ? 0 : cumulativeLengths[i - 1];
|
|
var indices = [0, previousLength, 0];
|
|
var sizes = [1, length[i], elementPerRow];
|
|
tensors[i] = tfc.reshape(tfc.slice(tensor, indices, sizes), outputElementShape);
|
|
}
|
|
tensor.dispose();
|
|
return tensors;
|
|
});
|
|
var list = new TensorList([], elementShape, tensor.dtype, length.length);
|
|
for (var i = 0; i < tensors.length; i++) {
|
|
list.setItem(i, tensors[i]);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
var executeOp$i = function (node, tensorMap, context) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var _a, thenFunc, elseFunc, cond, args, condValue, bodyFunc, condFunc, args, condResult, argIds_1, condValue, result, _loop_1, pred, pred, data, inputName, data, frameId, data, data, data, size, dtype, elementShape, dynamicSize, clearAfterRead, identicalElementShapes, name, tensorArray, id, index, writeTensor, writeTensorArray, readId, readIndex, readTensorArray, gatherId, gatherIndices, gatherDtype, gatherTensorArray, scatterId, scatterIndices, scatterTensor, scatterTensorArray, concatId, concatTensorArray, concatDtype, splitId, splitTensor, lengths, splitTensorArray, sizeId, sizeTensorArray, closeId, closeTensorArray, idTensor, index, writeTensor, tensorList, idTensor, readIndex, elementShape, elementDType, tensorList, scatterIndices, scatterTensor, elementShape, numElements, tensorList, elementShape, elementDtype, numElementsParam, numElements, maxNumElements, tensorList, gatherId, gatherIndices, elementShape, elementDtype, tensorList, idTensor, elementShape, elementDtype, numElements, tensorList, tensor, elementShape, elementDtype, tensorList, concatId, tensorList, concatDtype, elementShape, idTensor, writeTensor, tensorList, idTensor, elementShape, elementDType, tensorList, splitTensor, elementShape, lengths, tensorList, idTensor, tensorList, idTensor, size, srcTensorList, destTensorList;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
_a = node.op;
|
|
switch (_a) {
|
|
case 'If': return [3 /*break*/, 1];
|
|
case 'StatelessIf': return [3 /*break*/, 1];
|
|
case 'While': return [3 /*break*/, 3];
|
|
case 'StatelessWhile': return [3 /*break*/, 3];
|
|
case 'LoopCond': return [3 /*break*/, 9];
|
|
case 'Switch': return [3 /*break*/, 10];
|
|
case 'Merge': return [3 /*break*/, 12];
|
|
case 'Enter': return [3 /*break*/, 13];
|
|
case 'Exit': return [3 /*break*/, 14];
|
|
case 'NextIteration': return [3 /*break*/, 15];
|
|
case 'TensorArrayV3': return [3 /*break*/, 16];
|
|
case 'TensorArrayWriteV3': return [3 /*break*/, 17];
|
|
case 'TensorArrayReadV3': return [3 /*break*/, 18];
|
|
case 'TensorArrayGatherV3': return [3 /*break*/, 19];
|
|
case 'TensorArrayScatterV3': return [3 /*break*/, 20];
|
|
case 'TensorArrayConcatV3': return [3 /*break*/, 21];
|
|
case 'TensorArraySplitV3': return [3 /*break*/, 22];
|
|
case 'TensorArraySizeV3': return [3 /*break*/, 23];
|
|
case 'TensorArrayCloseV3': return [3 /*break*/, 24];
|
|
case 'TensorListSetItem': return [3 /*break*/, 25];
|
|
case 'TensorListGetItem': return [3 /*break*/, 26];
|
|
case 'TensorListScatterV2': return [3 /*break*/, 27];
|
|
case 'TensorListScatter': return [3 /*break*/, 27];
|
|
case 'TensorListReserve': return [3 /*break*/, 28];
|
|
case 'EmptyTensorList': return [3 /*break*/, 28];
|
|
case 'TensorListGather': return [3 /*break*/, 29];
|
|
case 'TensorListStack': return [3 /*break*/, 30];
|
|
case 'TensorListFromTensor': return [3 /*break*/, 31];
|
|
case 'TensorListConcat': return [3 /*break*/, 32];
|
|
case 'TensorListConcatV2': return [3 /*break*/, 32];
|
|
case 'TensorListPushBack': return [3 /*break*/, 33];
|
|
case 'TensorListPopBack': return [3 /*break*/, 34];
|
|
case 'TensorListSplit': return [3 /*break*/, 35];
|
|
case 'TensorListLength': return [3 /*break*/, 36];
|
|
case 'TensorListResize': return [3 /*break*/, 37];
|
|
}
|
|
return [3 /*break*/, 38];
|
|
case 1:
|
|
thenFunc = getParamValue('thenBranch', node, tensorMap, context);
|
|
elseFunc = getParamValue('elseBranch', node, tensorMap, context);
|
|
cond = getParamValue('cond', node, tensorMap, context);
|
|
args = getParamValue('args', node, tensorMap, context);
|
|
return [4 /*yield*/, cond.data()];
|
|
case 2:
|
|
condValue = _b.sent();
|
|
if (condValue[0]) {
|
|
return [2 /*return*/, context.functionMap[thenFunc].executeFunctionAsync(args, context.tensorArrayMap, context.tensorListMap)];
|
|
}
|
|
else {
|
|
return [2 /*return*/, context.functionMap[elseFunc].executeFunctionAsync(args, context.tensorArrayMap, context.tensorListMap)];
|
|
}
|
|
case 3:
|
|
bodyFunc = getParamValue('body', node, tensorMap, context);
|
|
condFunc = getParamValue('cond', node, tensorMap, context);
|
|
args = getParamValue('args', node, tensorMap, context);
|
|
return [4 /*yield*/, context.functionMap[condFunc].executeFunctionAsync(args, context.tensorArrayMap, context.tensorListMap)];
|
|
case 4:
|
|
condResult = (_b.sent());
|
|
argIds_1 = args.map(function (tensor) { return tensor.id; });
|
|
return [4 /*yield*/, condResult[0].data()];
|
|
case 5:
|
|
condValue = _b.sent();
|
|
// Dispose the intermediate tensors for condition function
|
|
condResult.forEach(function (tensor) {
|
|
if (!tensor.kept && argIds_1.indexOf(tensor.id) === -1) {
|
|
tensor.dispose();
|
|
}
|
|
});
|
|
result = args;
|
|
_loop_1 = function () {
|
|
var origResult, resultIds, condResult_1;
|
|
return __generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
origResult = result;
|
|
return [4 /*yield*/, context.functionMap[bodyFunc].executeFunctionAsync(result, context.tensorArrayMap, context.tensorListMap)];
|
|
case 1:
|
|
// Execution the body of the loop
|
|
result = _c.sent();
|
|
resultIds = result.map(function (tensor) { return tensor.id; });
|
|
// Dispose the intermediate tensor for body function that is not global
|
|
// kept, not input/output of the body function
|
|
origResult.forEach(function (tensor) {
|
|
if (!tensor.kept && argIds_1.indexOf(tensor.id) === -1 &&
|
|
resultIds.indexOf(tensor.id) === -1) {
|
|
tensor.dispose();
|
|
}
|
|
});
|
|
return [4 /*yield*/, context.functionMap[condFunc].executeFunctionAsync(result, context.tensorArrayMap, context.tensorListMap)];
|
|
case 2:
|
|
condResult_1 = (_c.sent());
|
|
return [4 /*yield*/, condResult_1[0].data()];
|
|
case 3:
|
|
condValue = _c.sent();
|
|
// Dispose the intermediate tensors for condition function
|
|
condResult_1.forEach(function (tensor) {
|
|
if (!tensor.kept && argIds_1.indexOf(tensor.id) === -1 &&
|
|
resultIds.indexOf(tensor.id) === -1) {
|
|
tensor.dispose();
|
|
}
|
|
});
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
};
|
|
_b.label = 6;
|
|
case 6:
|
|
if (!condValue[0]) return [3 /*break*/, 8];
|
|
return [5 /*yield**/, _loop_1()];
|
|
case 7:
|
|
_b.sent();
|
|
return [3 /*break*/, 6];
|
|
case 8: return [2 /*return*/, result];
|
|
case 9:
|
|
{
|
|
pred = getParamValue('pred', node, tensorMap, context);
|
|
return [2 /*return*/, [cloneTensor(pred)]];
|
|
}
|
|
case 10:
|
|
pred = getParamValue('pred', node, tensorMap, context);
|
|
data = getParamValue('data', node, tensorMap, context);
|
|
if (!data.kept) {
|
|
data = cloneTensor(data);
|
|
}
|
|
return [4 /*yield*/, pred.data()];
|
|
case 11:
|
|
// Outputs nodes :0 => false, :1 => true
|
|
return [2 /*return*/, (_b.sent())[0] ? [undefined, data] : [data, undefined]];
|
|
case 12:
|
|
{
|
|
inputName = node.inputNames.find(function (name) { return getTensor(name, tensorMap, context) !== undefined; });
|
|
if (inputName) {
|
|
data = getTensor(inputName, tensorMap, context);
|
|
return [2 /*return*/, [cloneTensor(data)]];
|
|
}
|
|
return [2 /*return*/, undefined];
|
|
}
|
|
case 13:
|
|
{
|
|
frameId = getParamValue('frameName', node, tensorMap, context);
|
|
data = getParamValue('tensor', node, tensorMap, context);
|
|
context.enterFrame(frameId);
|
|
return [2 /*return*/, [cloneTensor(data)]];
|
|
}
|
|
case 14:
|
|
{
|
|
data = getParamValue('tensor', node, tensorMap, context);
|
|
context.exitFrame();
|
|
return [2 /*return*/, [cloneTensor(data)]];
|
|
}
|
|
case 15:
|
|
{
|
|
data = getParamValue('tensor', node, tensorMap, context);
|
|
context.nextIteration();
|
|
return [2 /*return*/, [cloneTensor(data)]];
|
|
}
|
|
case 16:
|
|
{
|
|
size = getParamValue('size', node, tensorMap, context);
|
|
dtype = getParamValue('dtype', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
dynamicSize = getParamValue('dynamicSize', node, tensorMap, context);
|
|
clearAfterRead = getParamValue('clearAfterRead', node, tensorMap, context);
|
|
identicalElementShapes = getParamValue('identicalElementShapes', node, tensorMap, context);
|
|
name = getParamValue('name', node, tensorMap, context);
|
|
tensorArray = new TensorArray(name, dtype, size, elementShape, identicalElementShapes, dynamicSize, clearAfterRead);
|
|
context.addTensorArray(tensorArray);
|
|
return [2 /*return*/, [tensorArray.idTensor, tfc.scalar(1.0)]];
|
|
}
|
|
case 17:
|
|
{
|
|
id = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
index = getParamValue('index', node, tensorMap, context);
|
|
writeTensor = getParamValue('tensor', node, tensorMap, context);
|
|
writeTensorArray = context.getTensorArray(id.id);
|
|
writeTensorArray.write(index, writeTensor);
|
|
return [2 /*return*/, [writeTensorArray.idTensor]];
|
|
}
|
|
case 18:
|
|
{
|
|
readId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
readIndex = getParamValue('index', node, tensorMap, context);
|
|
readTensorArray = context.getTensorArray(readId.id);
|
|
return [2 /*return*/, [readTensorArray.read(readIndex)]];
|
|
}
|
|
case 19:
|
|
{
|
|
gatherId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
gatherIndices = getParamValue('indices', node, tensorMap, context);
|
|
gatherDtype = getParamValue('dtype', node, tensorMap, context);
|
|
gatherTensorArray = context.getTensorArray(gatherId.id);
|
|
return [2 /*return*/, [gatherTensorArray.gather(gatherIndices, gatherDtype)]];
|
|
}
|
|
case 20:
|
|
{
|
|
scatterId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
scatterIndices = getParamValue('indices', node, tensorMap, context);
|
|
scatterTensor = getParamValue('tensor', node, tensorMap, context);
|
|
scatterTensorArray = context.getTensorArray(scatterId.id);
|
|
scatterTensorArray.scatter(scatterIndices, scatterTensor);
|
|
return [2 /*return*/, [scatterTensorArray.idTensor]];
|
|
}
|
|
case 21:
|
|
{
|
|
concatId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
concatTensorArray = context.getTensorArray(concatId.id);
|
|
concatDtype = getParamValue('dtype', node, tensorMap, context);
|
|
return [2 /*return*/, [concatTensorArray.concat(concatDtype)]];
|
|
}
|
|
case 22:
|
|
{
|
|
splitId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
splitTensor = getParamValue('tensor', node, tensorMap, context);
|
|
lengths = getParamValue('lengths', node, tensorMap, context);
|
|
splitTensorArray = context.getTensorArray(splitId.id);
|
|
splitTensorArray.split(lengths, splitTensor);
|
|
return [2 /*return*/, [splitTensorArray.idTensor]];
|
|
}
|
|
case 23:
|
|
{
|
|
sizeId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
sizeTensorArray = context.getTensorArray(sizeId.id);
|
|
return [2 /*return*/, [tfc.scalar(sizeTensorArray.size(), 'int32')]];
|
|
}
|
|
case 24:
|
|
{
|
|
closeId = getParamValue('tensorArrayId', node, tensorMap, context);
|
|
closeTensorArray = context.getTensorArray(closeId.id);
|
|
closeTensorArray.clearAndClose();
|
|
return [2 /*return*/, [closeTensorArray.idTensor]];
|
|
}
|
|
case 25:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
index = getParamValue('index', node, tensorMap, context);
|
|
writeTensor = getParamValue('tensor', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
tensorList.setItem(index, writeTensor);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 26:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
readIndex = getParamValue('index', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDType = getParamValue('elementDType', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
return [2 /*return*/, [tensorList.getItem(readIndex, elementShape, elementDType)]];
|
|
}
|
|
case 27:
|
|
{
|
|
scatterIndices = getParamValue('indices', node, tensorMap, context);
|
|
scatterTensor = getParamValue('tensor', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
numElements = getParamValue('numElements', node, tensorMap, context);
|
|
tensorList = scatter(scatterTensor, scatterIndices, elementShape, numElements);
|
|
context.addTensorList(tensorList);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 28:
|
|
{
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDtype = getParamValue('elementDType', node, tensorMap, context);
|
|
numElementsParam = void 0;
|
|
if (node.op === 'TensorListReserve') {
|
|
numElementsParam = 'numElements';
|
|
}
|
|
else {
|
|
numElementsParam = 'maxNumElements';
|
|
}
|
|
numElements = getParamValue(numElementsParam, node, tensorMap, context);
|
|
maxNumElements = node.op === 'TensorListReserve' ? -1 : numElements;
|
|
tensorList = reserve(elementShape, elementDtype, numElements, maxNumElements);
|
|
context.addTensorList(tensorList);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 29:
|
|
{
|
|
gatherId = getParamValue('tensorListId', node, tensorMap, context);
|
|
gatherIndices = getParamValue('indices', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDtype = getParamValue('elementDType', node, tensorMap, context);
|
|
tensorList = context.getTensorList(gatherId.id);
|
|
return [2 /*return*/, [tensorList.gather(gatherIndices, elementDtype, elementShape)]];
|
|
}
|
|
case 30:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDtype = getParamValue('elementDType', node, tensorMap, context);
|
|
numElements = getParamValue('numElements', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
return [2 /*return*/, [tensorList.stack(elementShape, elementDtype, numElements)]];
|
|
}
|
|
case 31:
|
|
{
|
|
tensor = getParamValue('tensor', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDtype = getParamValue('elementDType', node, tensorMap, context);
|
|
tensorList = fromTensor(tensor, elementShape, elementDtype);
|
|
context.addTensorList(tensorList);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 32:
|
|
{
|
|
concatId = getParamValue('tensorListId', node, tensorMap, context);
|
|
tensorList = context.getTensorList(concatId.id);
|
|
concatDtype = getParamValue('dtype', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
return [2 /*return*/, [tensorList.concat(concatDtype, elementShape)]];
|
|
}
|
|
case 33:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
writeTensor = getParamValue('tensor', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
tensorList.pushBack(writeTensor);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 34:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
elementDType = getParamValue('elementDType', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
return [2 /*return*/, [tensorList.popBack(elementShape, elementDType)]];
|
|
}
|
|
case 35:
|
|
{
|
|
splitTensor = getParamValue('tensor', node, tensorMap, context);
|
|
elementShape = getParamValue('elementShape', node, tensorMap, context);
|
|
lengths = getParamValue('lengths', node, tensorMap, context);
|
|
tensorList = split(splitTensor, lengths, elementShape);
|
|
context.addTensorList(tensorList);
|
|
return [2 /*return*/, [tensorList.idTensor]];
|
|
}
|
|
case 36:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
tensorList = context.getTensorList(idTensor.id);
|
|
return [2 /*return*/, [tfc.scalar(tensorList.size(), 'int32')]];
|
|
}
|
|
case 37:
|
|
{
|
|
idTensor = getParamValue('tensorListId', node, tensorMap, context);
|
|
size = getParamValue('size', node, tensorMap, context);
|
|
srcTensorList = context.getTensorList(idTensor.id);
|
|
destTensorList = srcTensorList.resize(size);
|
|
context.addTensorList(destTensorList);
|
|
return [2 /*return*/, [destTensorList.idTensor]];
|
|
}
|
|
case 38: throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
});
|
|
}); };
|
|
|
|
function fusedConvAndDepthWiseParams(node, tensorMap, context) {
|
|
var _a = __read(getParamValue('fusedOps', node, tensorMap, context), 2), extraOp = _a[0], activationFunc = _a[1];
|
|
var isBiasAdd = extraOp === 'biasadd';
|
|
var noBiasAdd = !isBiasAdd;
|
|
var isPrelu = activationFunc === 'prelu';
|
|
var isBatchNorm = extraOp === 'fusedbatchnorm';
|
|
var numArgs = getParamValue('numArgs', node, tensorMap, context);
|
|
if (isBiasAdd) {
|
|
if (isPrelu && numArgs !== 2) {
|
|
throw new Error('FusedConv2d and DepthwiseConv2d with BiasAdd and Prelu ' +
|
|
'must have two extra arguments: bias and alpha.');
|
|
}
|
|
if (!isPrelu && isBiasAdd && numArgs !== 1) {
|
|
throw new Error('FusedConv2d and DepthwiseConv2d with BiasAdd must have ' +
|
|
'one extra argument: bias.');
|
|
}
|
|
}
|
|
if (isBatchNorm) {
|
|
throw new Error('FusedConv2d and DepthwiseConv2d with FusedBatchNorm is not supported');
|
|
}
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getPadding(node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context)
|
|
.toUpperCase();
|
|
var dilations = getParamValue('dilations', node, tensorMap, context);
|
|
var _b = __read(getParamValue('args', node, tensorMap, context), 2), biasArg = _b[0], preluArg = _b[1];
|
|
if (noBiasAdd) {
|
|
preluArg = biasArg;
|
|
biasArg = undefined;
|
|
}
|
|
var leakyreluAlpha = getParamValue('leakyreluAlpha', node, tensorMap, context);
|
|
return {
|
|
stride: stride,
|
|
pad: pad,
|
|
dataFormat: dataFormat,
|
|
dilations: dilations,
|
|
biasArg: biasArg,
|
|
preluArg: preluArg,
|
|
activationFunc: activationFunc,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
};
|
|
}
|
|
var executeOp$h = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Conv1D': {
|
|
var stride = getParamValue('stride', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context)
|
|
.toUpperCase();
|
|
var dilation = getParamValue('dilation', node, tensorMap, context);
|
|
return [ops.conv1d(getParamValue('x', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), stride, pad, dataFormat, dilation)];
|
|
}
|
|
case 'Conv2D': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getPadding(node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context)
|
|
.toUpperCase();
|
|
var dilations = getParamValue('dilations', node, tensorMap, context);
|
|
return [ops.conv2d(getParamValue('x', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), [stride[1], stride[2]], pad, dataFormat, [dilations[1], dilations[2]])];
|
|
}
|
|
case '_FusedConv2D': {
|
|
var _a = fusedConvAndDepthWiseParams(node, tensorMap, context), stride = _a.stride, pad = _a.pad, dataFormat = _a.dataFormat, dilations = _a.dilations, biasArg = _a.biasArg, preluArg = _a.preluArg, activationFunc = _a.activationFunc, leakyreluAlpha = _a.leakyreluAlpha;
|
|
return [ops.fused.conv2d({
|
|
x: getParamValue('x', node, tensorMap, context),
|
|
filter: getParamValue('filter', node, tensorMap, context),
|
|
strides: [stride[1], stride[2]],
|
|
pad: pad,
|
|
dataFormat: dataFormat,
|
|
dilations: [dilations[1], dilations[2]],
|
|
bias: biasArg,
|
|
activation: activationFunc,
|
|
preluActivationWeights: preluArg,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
})];
|
|
}
|
|
case 'FusedDepthwiseConv2dNative': {
|
|
var _b = fusedConvAndDepthWiseParams(node, tensorMap, context), stride = _b.stride, pad = _b.pad, dataFormat = _b.dataFormat, dilations = _b.dilations, biasArg = _b.biasArg, preluArg = _b.preluArg, activationFunc = _b.activationFunc, leakyreluAlpha = _b.leakyreluAlpha;
|
|
return [ops.fused.depthwiseConv2d({
|
|
x: getParamValue('x', node, tensorMap, context),
|
|
filter: getParamValue('filter', node, tensorMap, context),
|
|
strides: [stride[1], stride[2]],
|
|
pad: pad,
|
|
dataFormat: dataFormat,
|
|
dilations: [dilations[1], dilations[2]],
|
|
bias: biasArg,
|
|
activation: activationFunc,
|
|
preluActivationWeights: preluArg,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
})];
|
|
}
|
|
case 'Conv2DBackpropInput':
|
|
case 'Conv2dTranspose': {
|
|
var shape = getParamValue('outputShape', node, tensorMap, context);
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getPadding(node, tensorMap, context);
|
|
return [ops.conv2dTranspose(getParamValue('x', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), shape, [stride[1], stride[2]], pad)];
|
|
}
|
|
case 'DepthwiseConv2dNative':
|
|
case 'DepthwiseConv2d': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getPadding(node, tensorMap, context);
|
|
var dilations = getParamValue('dilations', node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context)
|
|
.toUpperCase();
|
|
return [ops.depthwiseConv2d(getParamValue('input', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), [stride[1], stride[2]], pad, dataFormat, [dilations[1], dilations[2]])];
|
|
}
|
|
case 'Conv3D': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context)
|
|
.toUpperCase();
|
|
var dilations = getParamValue('dilations', node, tensorMap, context);
|
|
return [ops.conv3d(getParamValue('x', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), [stride[1], stride[2], stride[3]], pad, dataFormat, [dilations[1], dilations[2], dilations[3]])];
|
|
}
|
|
case 'AvgPool': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var kernelSize = getParamValue('kernelSize', node, tensorMap, context);
|
|
return [ops.avgPool(getParamValue('x', node, tensorMap, context), [kernelSize[1], kernelSize[2]], [stride[1], stride[2]], pad)];
|
|
}
|
|
case 'MaxPool': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var kernelSize = getParamValue('kernelSize', node, tensorMap, context);
|
|
return [ops.maxPool(getParamValue('x', node, tensorMap, context), [kernelSize[1], kernelSize[2]], [stride[1], stride[2]], pad)];
|
|
}
|
|
case 'MaxPoolWithArgmax': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var kernelSize = getParamValue('kernelSize', node, tensorMap, context);
|
|
var includeBatchInIndex = getParamValue('includeBatchInIndex', node, tensorMap, context);
|
|
var _c = ops.maxPoolWithArgmax(getParamValue('x', node, tensorMap, context), [kernelSize[1], kernelSize[2]], [stride[1], stride[2]], pad, includeBatchInIndex), result = _c.result, indexes = _c.indexes;
|
|
return [result, indexes];
|
|
}
|
|
case 'AvgPool3D': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var kernelSize = getParamValue('kernelSize', node, tensorMap, context);
|
|
return [ops.avgPool3d(getParamValue('x', node, tensorMap, context), [kernelSize[1], kernelSize[2], kernelSize[3]], [stride[1], stride[2], stride[3]], pad)];
|
|
}
|
|
case 'MaxPool3D': {
|
|
var stride = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var kernelSize = getParamValue('kernelSize', node, tensorMap, context);
|
|
return [ops.maxPool3d(getParamValue('x', node, tensorMap, context), [kernelSize[1], kernelSize[2], kernelSize[3]], [stride[1], stride[2], stride[3]], pad)];
|
|
}
|
|
case 'Dilation2D': {
|
|
var strides = getParamValue('strides', node, tensorMap, context);
|
|
var pad = getParamValue('pad', node, tensorMap, context);
|
|
var dilations = getParamValue('dilations', node, tensorMap, context);
|
|
// strides: [1, stride_height, stride_width, 1].
|
|
var strideHeight = strides[1];
|
|
var strideWidth = strides[2];
|
|
// dilations: [1, dilation_height, dilation_width, 1].
|
|
var dilationHeight = dilations[1];
|
|
var dilationWidth = dilations[2];
|
|
return [ops.dilation2d(getParamValue('x', node, tensorMap, context), getParamValue('filter', node, tensorMap, context), [strideHeight, strideWidth], pad, [dilationHeight, dilationWidth], 'NHWC' /* dataFormat */)];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$g = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Fill': {
|
|
var shape = getParamValue('shape', node, tensorMap, context);
|
|
var dtype = getParamValue('dtype', node, tensorMap, context);
|
|
var value = getParamValue('value', node, tensorMap, context);
|
|
return [ops.fill(shape, value, dtype)];
|
|
}
|
|
case 'LinSpace': {
|
|
var start = getParamValue('start', node, tensorMap, context);
|
|
var stop = getParamValue('stop', node, tensorMap, context);
|
|
var num = getParamValue('num', node, tensorMap, context);
|
|
return [ops.linspace(start, stop, num)];
|
|
}
|
|
case 'Multinomial': {
|
|
var logits = getParamValue('logits', node, tensorMap, context);
|
|
var numSamples = getParamValue('numSamples', node, tensorMap, context);
|
|
var seed = getParamValue('seed', node, tensorMap, context);
|
|
return [ops.multinomial(logits, numSamples, seed)];
|
|
}
|
|
case 'OneHot': {
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
var depth = getParamValue('depth', node, tensorMap, context);
|
|
var onValue = getParamValue('onValue', node, tensorMap, context);
|
|
var offValue = getParamValue('offValue', node, tensorMap, context);
|
|
var dtype = getParamValue('dtype', node, tensorMap, context);
|
|
return [ops.oneHot(indices, depth, onValue, offValue, dtype)];
|
|
}
|
|
case 'Ones': {
|
|
return [ops.ones(getParamValue('shape', node, tensorMap, context), getParamValue('dtype', node, tensorMap, context))];
|
|
}
|
|
case 'OnesLike': {
|
|
return [ops.onesLike(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'RandomStandardNormal': {
|
|
return [ops.randomStandardNormal(getParamValue('shape', node, tensorMap, context), getParamValue('dtype', node, tensorMap, context), getParamValue('seed', node, tensorMap, context))];
|
|
}
|
|
case 'RandomUniform': {
|
|
return [ops.randomUniform(
|
|
// tslint:disable-next-line:no-any
|
|
getParamValue('shape', node, tensorMap, context), getParamValue('minval', node, tensorMap, context), getParamValue('maxval', node, tensorMap, context), getParamValue('dtype', node, tensorMap, context))];
|
|
}
|
|
case 'RandomUniformInt': {
|
|
return [ops.randomUniformInt(getParamValue('shape', node, tensorMap, context), getParamValue('minval', node, tensorMap, context), getParamValue('maxval', node, tensorMap, context), getParamValue('seed', node, tensorMap, context))];
|
|
}
|
|
case 'Range': {
|
|
var start = getParamValue('start', node, tensorMap, context);
|
|
var stop = getParamValue('stop', node, tensorMap, context);
|
|
var step = getParamValue('step', node, tensorMap, context);
|
|
return [ops.range(start, stop, step, getParamValue('dtype', node, tensorMap, context))];
|
|
}
|
|
case 'TruncatedNormal': {
|
|
var shape = getParamValue('shape', node, tensorMap, context);
|
|
var mean = getParamValue('mean', node, tensorMap, context);
|
|
var stdDev = getParamValue('stdDev', node, tensorMap, context);
|
|
var seed = getParamValue('seed', node, tensorMap, context);
|
|
return [ops.truncatedNormal(shape, mean, stdDev, getParamValue('dtype', node, tensorMap, context), seed)];
|
|
}
|
|
case 'Zeros': {
|
|
return [ops.zeros(getParamValue('shape', node, tensorMap, context), getParamValue('dtype', node, tensorMap, context))];
|
|
}
|
|
case 'ZerosLike': {
|
|
return [ops.zerosLike(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
function nmsParams(node, tensorMap, context) {
|
|
var boxes = getParamValue('boxes', node, tensorMap, context);
|
|
var scores = getParamValue('scores', node, tensorMap, context);
|
|
var maxOutputSize = getParamValue('maxOutputSize', node, tensorMap, context);
|
|
var iouThreshold = getParamValue('iouThreshold', node, tensorMap, context);
|
|
var scoreThreshold = getParamValue('scoreThreshold', node, tensorMap, context);
|
|
var softNmsSigma = getParamValue('softNmsSigma', node, tensorMap, context);
|
|
return {
|
|
boxes: boxes,
|
|
scores: scores,
|
|
maxOutputSize: maxOutputSize,
|
|
iouThreshold: iouThreshold,
|
|
scoreThreshold: scoreThreshold,
|
|
softNmsSigma: softNmsSigma
|
|
};
|
|
}
|
|
var executeOp$f = function (node, tensorMap, context, resourceManager, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
var _a, _b, boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma, result, _c, boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, padToMaxOutputSize, result, _d, boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, condition, result;
|
|
return __generator(this, function (_e) {
|
|
switch (_e.label) {
|
|
case 0:
|
|
_a = node.op;
|
|
switch (_a) {
|
|
case 'NonMaxSuppressionV5': return [3 /*break*/, 1];
|
|
case 'NonMaxSuppressionV4': return [3 /*break*/, 3];
|
|
case 'NonMaxSuppressionV3': return [3 /*break*/, 5];
|
|
case 'NonMaxSuppressionV2': return [3 /*break*/, 5];
|
|
case 'Where': return [3 /*break*/, 7];
|
|
case 'ListDiff': return [3 /*break*/, 9];
|
|
}
|
|
return [3 /*break*/, 10];
|
|
case 1:
|
|
_b = nmsParams(node, tensorMap, context), boxes = _b.boxes, scores = _b.scores, maxOutputSize = _b.maxOutputSize, iouThreshold = _b.iouThreshold, scoreThreshold = _b.scoreThreshold, softNmsSigma = _b.softNmsSigma;
|
|
return [4 /*yield*/, ops.image.nonMaxSuppressionWithScoreAsync(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, softNmsSigma)];
|
|
case 2:
|
|
result = _e.sent();
|
|
return [2 /*return*/, [result.selectedIndices, result.selectedScores]];
|
|
case 3:
|
|
_c = nmsParams(node, tensorMap, context), boxes = _c.boxes, scores = _c.scores, maxOutputSize = _c.maxOutputSize, iouThreshold = _c.iouThreshold, scoreThreshold = _c.scoreThreshold;
|
|
padToMaxOutputSize = getParamValue('padToMaxOutputSize', node, tensorMap, context);
|
|
return [4 /*yield*/, ops.image.nonMaxSuppressionPaddedAsync(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold, padToMaxOutputSize)];
|
|
case 4:
|
|
result = _e.sent();
|
|
return [2 /*return*/, [result.selectedIndices, result.validOutputs]];
|
|
case 5:
|
|
_d = nmsParams(node, tensorMap, context), boxes = _d.boxes, scores = _d.scores, maxOutputSize = _d.maxOutputSize, iouThreshold = _d.iouThreshold, scoreThreshold = _d.scoreThreshold;
|
|
return [4 /*yield*/, ops.image.nonMaxSuppressionAsync(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold)];
|
|
case 6: return [2 /*return*/, [_e.sent()]];
|
|
case 7:
|
|
condition = ops.cast(getParamValue('condition', node, tensorMap, context), 'bool');
|
|
return [4 /*yield*/, ops.whereAsync(condition)];
|
|
case 8:
|
|
result = [_e.sent()];
|
|
condition.dispose();
|
|
return [2 /*return*/, result];
|
|
case 9:
|
|
{
|
|
return [2 /*return*/, ops.setdiff1dAsync(getParamValue('x', node, tensorMap, context), getParamValue('y', node, tensorMap, context))];
|
|
}
|
|
case 10: throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$e = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'LowerBound': {
|
|
var sortedSequence = getParamValue('sortedSequence', node, tensorMap, context);
|
|
var values = getParamValue('values', node, tensorMap, context);
|
|
return [ops.lowerBound(sortedSequence, values)];
|
|
}
|
|
case 'TopKV2': {
|
|
var x = getParamValue('x', node, tensorMap, context);
|
|
var k = getParamValue('k', node, tensorMap, context);
|
|
var sorted = getParamValue('sorted', node, tensorMap, context);
|
|
var result = ops.topk(x, k, sorted);
|
|
return [result.values, result.indices];
|
|
}
|
|
case 'UpperBound': {
|
|
var sortedSequence = getParamValue('sortedSequence', node, tensorMap, context);
|
|
var values = getParamValue('values', node, tensorMap, context);
|
|
return [ops.upperBound(sortedSequence, values)];
|
|
}
|
|
case 'Unique': {
|
|
var x = getParamValue('x', node, tensorMap, context);
|
|
var result = ops.unique(x);
|
|
return [result.values, result.indices];
|
|
}
|
|
case 'UniqueV2': {
|
|
var x = getParamValue('x', node, tensorMap, context);
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var result = ops.unique(x, axis);
|
|
return [result.values, result.indices];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$d = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Const': {
|
|
return tensorMap[node.name];
|
|
}
|
|
case 'PlaceholderWithDefault':
|
|
var def = getParamValue('default', node, tensorMap, context);
|
|
return [getTensor(node.name, tensorMap, context) || def];
|
|
case 'Placeholder':
|
|
return [getTensor(node.name, tensorMap, context)];
|
|
case 'Identity':
|
|
case 'StopGradient':
|
|
case 'FakeQuantWithMinMaxVars': { // This op is currently ignored.
|
|
var data_1 = getParamValue('x', node, tensorMap, context);
|
|
return [cloneTensor(data_1)];
|
|
}
|
|
case 'IdentityN':
|
|
return getParamValue('x', node, tensorMap, context)
|
|
.map(function (t) { return cloneTensor(t); });
|
|
case 'Snapshot':
|
|
var snapshot = getParamValue('x', node, tensorMap, context);
|
|
return [cloneTensor(snapshot)];
|
|
case 'Shape':
|
|
return [ops.tensor1d(getParamValue('x', node, tensorMap, context).shape, 'int32')];
|
|
case 'ShapeN':
|
|
return getParamValue('x', node, tensorMap, context)
|
|
.map(function (t) { return ops.tensor1d(t.shape); });
|
|
case 'Size':
|
|
return [ops.scalar(getParamValue('x', node, tensorMap, context).size, 'int32')];
|
|
case 'Rank':
|
|
return [ops.scalar(getParamValue('x', node, tensorMap, context).rank, 'int32')];
|
|
case 'NoOp':
|
|
return [ops.scalar(1)];
|
|
case 'Print':
|
|
var input = getParamValue('x', node, tensorMap, context);
|
|
var data = getParamValue('data', node, tensorMap, context);
|
|
var message = getParamValue('message', node, tensorMap, context);
|
|
var summarize = getParamValue('summarize', node, tensorMap, context);
|
|
console.warn('The graph has a tf.print() operation,' +
|
|
'usually used for debugging, which slows down performance.');
|
|
console.log(message);
|
|
for (var i = 0; i < data.length; i++) {
|
|
console.log(Array.prototype.slice.call(data[i].dataSync())
|
|
.slice(0, summarize));
|
|
}
|
|
return [input];
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Hashtable contains a set of tensors, which can be accessed by key.
|
|
*/
|
|
var HashTable = /** @class */ (function () {
|
|
/**
|
|
* Constructor of HashTable. Creates a hash table.
|
|
*
|
|
* @param keyDType `dtype` of the table keys.
|
|
* @param valueDType `dtype` of the table values.
|
|
*/
|
|
function HashTable(keyDType, valueDType) {
|
|
this.keyDType = keyDType;
|
|
this.valueDType = valueDType;
|
|
this.handle = tfc.scalar(0);
|
|
// tslint:disable-next-line: no-any
|
|
this.tensorMap = new Map();
|
|
tfc.keep(this.handle);
|
|
}
|
|
Object.defineProperty(HashTable.prototype, "id", {
|
|
get: function () {
|
|
return this.handle.id;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Dispose the tensors and handle and clear the hashtable.
|
|
*/
|
|
HashTable.prototype.clearAndClose = function () {
|
|
this.tensorMap.forEach(function (value) { return value.dispose(); });
|
|
this.tensorMap.clear();
|
|
this.handle.dispose();
|
|
};
|
|
/**
|
|
* The number of items in the hash table.
|
|
*/
|
|
HashTable.prototype.size = function () {
|
|
return this.tensorMap.size;
|
|
};
|
|
/**
|
|
* The number of items in the hash table as a rank-0 tensor.
|
|
*/
|
|
HashTable.prototype.tensorSize = function () {
|
|
return scalar(this.size(), 'int32');
|
|
};
|
|
/**
|
|
* Replaces the contents of the table with the specified keys and values.
|
|
* @param keys Keys to store in the hashtable.
|
|
* @param values Values to store in the hashtable.
|
|
*/
|
|
HashTable.prototype.import = function (keys, values) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $keys;
|
|
var _this = this;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
this.checkKeyAndValueTensor(keys, values);
|
|
return [4 /*yield*/, keys.data()];
|
|
case 1:
|
|
$keys = _a.sent();
|
|
// Clear the hashTable before inserting new values.
|
|
this.tensorMap.forEach(function (value) { return value.dispose(); });
|
|
this.tensorMap.clear();
|
|
return [2 /*return*/, tfc.tidy(function () {
|
|
var $values = tfc.unstack(values);
|
|
var keysLength = $keys.length;
|
|
var valuesLength = $values.length;
|
|
tfc.util.assert(keysLength === valuesLength, function () { return "The number of elements doesn't match, keys has " +
|
|
"".concat(keysLength, " elements, the values has ").concat(valuesLength, " ") +
|
|
"elements."; });
|
|
for (var i = 0; i < keysLength; i++) {
|
|
var key = $keys[i];
|
|
var value = $values[i];
|
|
tfc.keep(value);
|
|
_this.tensorMap.set(key, value);
|
|
}
|
|
return _this.handle;
|
|
})];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* Looks up keys in a hash table, outputs the corresponding values.
|
|
*
|
|
* Performs batch lookups, for every element in the key tensor, `find`
|
|
* stacks the corresponding value into the return tensor.
|
|
*
|
|
* If an element is not present in the table, the given `defaultValue` is
|
|
* used.
|
|
*
|
|
* @param keys Keys to look up. Must have the same type as the keys of the
|
|
* table.
|
|
* @param defaultValue The scalar `defaultValue` is the value output for keys
|
|
* not present in the table. It must also be of the same type as the
|
|
* table values.
|
|
*/
|
|
HashTable.prototype.find = function (keys, defaultValue) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var $keys;
|
|
var _this = this;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
this.checkKeyAndValueTensor(keys, defaultValue);
|
|
return [4 /*yield*/, keys.data()];
|
|
case 1:
|
|
$keys = _a.sent();
|
|
return [2 /*return*/, tfc.tidy(function () {
|
|
var result = [];
|
|
for (var i = 0; i < $keys.length; i++) {
|
|
var key = $keys[i];
|
|
var value = _this.findWithDefault(key, defaultValue);
|
|
result.push(value);
|
|
}
|
|
return tfc.stack(result);
|
|
})];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
// tslint:disable-next-line: no-any
|
|
HashTable.prototype.findWithDefault = function (key, defaultValue) {
|
|
var result = this.tensorMap.get(key);
|
|
return result != null ? result : defaultValue;
|
|
};
|
|
HashTable.prototype.checkKeyAndValueTensor = function (key, value) {
|
|
if (key.dtype !== this.keyDType) {
|
|
throw new Error("Expect key dtype ".concat(this.keyDType, ", but got ") +
|
|
"".concat(key.dtype));
|
|
}
|
|
if (value.dtype !== this.valueDType) {
|
|
throw new Error("Expect value dtype ".concat(this.valueDType, ", but got ") +
|
|
"".concat(value.dtype));
|
|
}
|
|
};
|
|
return HashTable;
|
|
}());
|
|
|
|
var executeOp$c = function (node, tensorMap, context, resourceManager) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var _a, existingTableHandle, keyDType, valueDType, hashTable, handle, keys, values, hashTable, handle, keys, defaultValue, hashTable, handle, hashTable;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
_a = node.op;
|
|
switch (_a) {
|
|
case 'HashTable': return [3 /*break*/, 1];
|
|
case 'HashTableV2': return [3 /*break*/, 1];
|
|
case 'InitializeTable': return [3 /*break*/, 2];
|
|
case 'InitializeTableV2': return [3 /*break*/, 2];
|
|
case 'LookupTableImport': return [3 /*break*/, 2];
|
|
case 'LookupTableImportV2': return [3 /*break*/, 2];
|
|
case 'LookupTableFind': return [3 /*break*/, 4];
|
|
case 'LookupTableFindV2': return [3 /*break*/, 4];
|
|
case 'LookupTableSize': return [3 /*break*/, 6];
|
|
case 'LookupTableSizeV2': return [3 /*break*/, 6];
|
|
}
|
|
return [3 /*break*/, 7];
|
|
case 1:
|
|
{
|
|
existingTableHandle = resourceManager.getHashTableHandleByName(node.name);
|
|
// Table is shared with initializer.
|
|
if (existingTableHandle != null) {
|
|
return [2 /*return*/, [existingTableHandle]];
|
|
}
|
|
else {
|
|
keyDType = getParamValue('keyDType', node, tensorMap, context);
|
|
valueDType = getParamValue('valueDType', node, tensorMap, context);
|
|
hashTable = new HashTable(keyDType, valueDType);
|
|
resourceManager.addHashTable(node.name, hashTable);
|
|
return [2 /*return*/, [hashTable.handle]];
|
|
}
|
|
}
|
|
case 2:
|
|
handle = getParamValue('tableHandle', node, tensorMap, context, resourceManager);
|
|
keys = getParamValue('keys', node, tensorMap, context);
|
|
values = getParamValue('values', node, tensorMap, context);
|
|
hashTable = resourceManager.getHashTableById(handle.id);
|
|
return [4 /*yield*/, hashTable.import(keys, values)];
|
|
case 3: return [2 /*return*/, [_b.sent()]];
|
|
case 4:
|
|
handle = getParamValue('tableHandle', node, tensorMap, context, resourceManager);
|
|
keys = getParamValue('keys', node, tensorMap, context);
|
|
defaultValue = getParamValue('defaultValue', node, tensorMap, context);
|
|
hashTable = resourceManager.getHashTableById(handle.id);
|
|
return [4 /*yield*/, hashTable.find(keys, defaultValue)];
|
|
case 5: return [2 /*return*/, [_b.sent()]];
|
|
case 6:
|
|
{
|
|
handle = getParamValue('tableHandle', node, tensorMap, context, resourceManager);
|
|
hashTable = resourceManager.getHashTableById(handle.id);
|
|
return [2 /*return*/, [hashTable.tensorSize()]];
|
|
}
|
|
case 7: throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
});
|
|
}); };
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$b = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'ResizeBilinear': {
|
|
var images = getParamValue('images', node, tensorMap, context);
|
|
var size = getParamValue('size', node, tensorMap, context);
|
|
var alignCorners = getParamValue('alignCorners', node, tensorMap, context);
|
|
var halfPixelCenters = getParamValue('halfPixelCenters', node, tensorMap, context);
|
|
return [ops.image.resizeBilinear(images, [size[0], size[1]], alignCorners, halfPixelCenters)];
|
|
}
|
|
case 'ResizeNearestNeighbor': {
|
|
var images = getParamValue('images', node, tensorMap, context);
|
|
var size = getParamValue('size', node, tensorMap, context);
|
|
var alignCorners = getParamValue('alignCorners', node, tensorMap, context);
|
|
var halfPixelCenters = getParamValue('halfPixelCenters', node, tensorMap, context);
|
|
return [ops.image.resizeNearestNeighbor(images, [size[0], size[1]], alignCorners, halfPixelCenters)];
|
|
}
|
|
case 'CropAndResize': {
|
|
var image = getParamValue('image', node, tensorMap, context);
|
|
var boxes = getParamValue('boxes', node, tensorMap, context);
|
|
var boxInd = getParamValue('boxInd', node, tensorMap, context);
|
|
var cropSize = getParamValue('cropSize', node, tensorMap, context);
|
|
var method = getParamValue('method', node, tensorMap, context);
|
|
var extrapolationValue = getParamValue('extrapolationValue', node, tensorMap, context);
|
|
return [ops.image.cropAndResize(image, boxes, boxInd, cropSize, method, extrapolationValue)];
|
|
}
|
|
case 'ImageProjectiveTransformV3': {
|
|
var images = getParamValue('images', node, tensorMap, context);
|
|
var transforms = getParamValue('transforms', node, tensorMap, context);
|
|
var outputShape = getParamValue('outputShape', node, tensorMap, context);
|
|
var fillValue = getParamValue('fillValue', node, tensorMap, context);
|
|
var interpolation = getParamValue('interpolation', node, tensorMap, context);
|
|
var fillMode = getParamValue('fillMode', node, tensorMap, context);
|
|
return [ops.image.transform(images, transforms, interpolation.toLowerCase(), fillMode.toLowerCase(), fillValue, outputShape)];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$a = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Equal': {
|
|
return [ops.equal(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'NotEqual': {
|
|
return [ops.notEqual(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Greater': {
|
|
return [ops.greater(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'GreaterEqual': {
|
|
return [ops.greaterEqual(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Less': {
|
|
return [ops.less(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'LessEqual': {
|
|
return [ops.lessEqual(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'LogicalAnd': {
|
|
return [ops.logicalAnd(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'LogicalNot': {
|
|
return [ops.logicalNot(getParamValue('a', node, tensorMap, context))];
|
|
}
|
|
case 'LogicalOr': {
|
|
return [ops.logicalOr(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'Select':
|
|
case 'SelectV2': {
|
|
return [ops.where(getParamValue('condition', node, tensorMap, context), getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
case 'BitwiseAnd': {
|
|
return [ops.bitwiseAnd(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
var executeOp$9 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'BatchMatMul':
|
|
case 'BatchMatMulV2':
|
|
case 'MatMul':
|
|
return [ops.matMul(getParamValue('a', node, tensorMap, context), getParamValue('b', node, tensorMap, context), getParamValue('transposeA', node, tensorMap, context), getParamValue('transposeB', node, tensorMap, context))];
|
|
case 'Einsum':
|
|
return [ops.einsum.apply(ops, __spreadArray([getParamValue('equation', node, tensorMap, context)], __read(getParamValue('tensors', node, tensorMap, context)), false))];
|
|
case 'Transpose':
|
|
return [ops.transpose(getParamValue('x', node, tensorMap, context), getParamValue('perm', node, tensorMap, context))];
|
|
case '_FusedMatMul':
|
|
var _a = __read(getParamValue('fusedOps', node, tensorMap, context), 2), extraOp = _a[0], activationFunc = _a[1];
|
|
var isBiasAdd = extraOp === 'biasadd';
|
|
var isPrelu = activationFunc === 'prelu';
|
|
var numArgs = getParamValue('numArgs', node, tensorMap, context);
|
|
var leakyreluAlpha = getParamValue('leakyreluAlpha', node, tensorMap, context);
|
|
if (isBiasAdd) {
|
|
if (isPrelu && numArgs !== 2) {
|
|
throw new Error('Fused MatMul with BiasAdd and Prelu must have two ' +
|
|
'extra arguments: bias and alpha.');
|
|
}
|
|
if (!isPrelu && numArgs !== 1) {
|
|
throw new Error('Fused MatMul with BiasAdd must have one extra argument: bias.');
|
|
}
|
|
}
|
|
var _b = __read(getParamValue('args', node, tensorMap, context), 2), biasArg = _b[0], preluArg = _b[1];
|
|
return [ops.fused.matMul({
|
|
a: getParamValue('a', node, tensorMap, context),
|
|
b: getParamValue('b', node, tensorMap, context),
|
|
transposeA: getParamValue('transposeA', node, tensorMap, context),
|
|
transposeB: getParamValue('transposeB', node, tensorMap, context),
|
|
bias: biasArg,
|
|
activation: activationFunc,
|
|
preluActivationWeights: preluArg,
|
|
leakyreluAlpha: leakyreluAlpha
|
|
})];
|
|
case 'MatrixBandPart':
|
|
return [ops.linalg.bandPart(getParamValue('a', node, tensorMap, context), getParamValue('numLower', node, tensorMap, context), getParamValue('numUpper', node, tensorMap, context))];
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$8 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'EuclideanNorm':
|
|
return [ops.euclideanNorm(getParamValue('x', node, tensorMap, context), getParamValue('axis', node, tensorMap, context), getParamValue('keepDims', node, tensorMap, context))];
|
|
case 'FusedBatchNorm':
|
|
case 'FusedBatchNormV2': {
|
|
return [ops.batchNorm(getParamValue('x', node, tensorMap, context), getParamValue('mean', node, tensorMap, context), getParamValue('variance', node, tensorMap, context), getParamValue('offset', node, tensorMap, context), getParamValue('scale', node, tensorMap, context), getParamValue('epsilon', node, tensorMap, context))];
|
|
}
|
|
case 'FusedBatchNormV3': {
|
|
return [ops.batchNorm(getParamValue('x', node, tensorMap, context), getParamValue('mean', node, tensorMap, context), getParamValue('variance', node, tensorMap, context), getParamValue('offset', node, tensorMap, context), getParamValue('scale', node, tensorMap, context), getParamValue('epsilon', node, tensorMap, context))];
|
|
}
|
|
case 'LRN': {
|
|
return [ops.localResponseNormalization(getParamValue('x', node, tensorMap, context), getParamValue('radius', node, tensorMap, context), getParamValue('bias', node, tensorMap, context), getParamValue('alpha', node, tensorMap, context), getParamValue('beta', node, tensorMap, context))];
|
|
}
|
|
case 'Softmax': {
|
|
return [ops.softmax(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'LogSoftmax': {
|
|
return [ops.logSoftmax(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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 executeOp$7 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'RaggedGather': {
|
|
var _a = ops.raggedGather(getParamValue('paramsNestedSplits', node, tensorMap, context), getParamValue('paramsDenseValues', node, tensorMap, context), getParamValue('indices', node, tensorMap, context), getParamValue('outputRaggedRank', node, tensorMap, context)), outputNestedSplits = _a.outputNestedSplits, outputDenseValues = _a.outputDenseValues;
|
|
return outputNestedSplits.concat(outputDenseValues);
|
|
}
|
|
case 'RaggedRange': {
|
|
var _b = ops.raggedRange(getParamValue('starts', node, tensorMap, context), getParamValue('limits', node, tensorMap, context), getParamValue('splits', node, tensorMap, context)), rtNestedSplits = _b.rtNestedSplits, rtDenseValues = _b.rtDenseValues;
|
|
return [rtNestedSplits, rtDenseValues];
|
|
}
|
|
case 'RaggedTensorToTensor': {
|
|
return [ops.raggedTensorToTensor(getParamValue('shape', node, tensorMap, context), getParamValue('values', node, tensorMap, context), getParamValue('defaultValue', node, tensorMap, context), getParamValue('rowPartitionTensors', node, tensorMap, context), getParamValue('rowPartitionTypes', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$6 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Max': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.max(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'Mean': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.mean(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'Min': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.min(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'Sum': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.sum(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'All': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.all(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'Any': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.any(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'ArgMax': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
return [ops.argMax(getParamValue('x', node, tensorMap, context), axis)];
|
|
}
|
|
case 'ArgMin': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
return [ops.argMin(getParamValue('x', node, tensorMap, context), axis)];
|
|
}
|
|
case 'Prod': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var keepDims = getParamValue('keepDims', node, tensorMap, context);
|
|
return [ops.prod(getParamValue('x', node, tensorMap, context), axis, keepDims)];
|
|
}
|
|
case 'Cumprod': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var exclusive = getParamValue('exclusive', node, tensorMap, context);
|
|
var reverse = getParamValue('reverse', node, tensorMap, context);
|
|
return [ops.cumprod(getParamValue('x', node, tensorMap, context), axis, exclusive, reverse)];
|
|
}
|
|
case 'Cumsum': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var exclusive = getParamValue('exclusive', node, tensorMap, context);
|
|
var reverse = getParamValue('reverse', node, tensorMap, context);
|
|
return [ops.cumsum(getParamValue('x', node, tensorMap, context), axis, exclusive, reverse)];
|
|
}
|
|
case 'Bincount':
|
|
var x = getParamValue('x', node, tensorMap, context);
|
|
var weights = getParamValue('weights', node, tensorMap, context);
|
|
var size = getParamValue('size', node, tensorMap, context);
|
|
return [ops.bincount(x, weights, size)];
|
|
case 'DenseBincount': {
|
|
var x_1 = getParamValue('x', node, tensorMap, context);
|
|
var weights_1 = getParamValue('weights', node, tensorMap, context);
|
|
var size_1 = getParamValue('size', node, tensorMap, context);
|
|
var binaryOutput = getParamValue('binaryOutput', node, tensorMap, context);
|
|
return [ops.denseBincount(x_1, weights_1, size_1, binaryOutput)];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$5 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'ConcatV2':
|
|
case 'Concat': {
|
|
var n = getParamValue('n', node, tensorMap, context);
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var inputs = getParamValue('tensors', node, tensorMap, context);
|
|
inputs = inputs.slice(0, n);
|
|
return [ops.concat(inputs, axis)];
|
|
}
|
|
case 'Gather': {
|
|
var input = getParamValue('x', node, tensorMap, context);
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
return [ops.gather(input, ops.cast(indices, 'int32'), 0)];
|
|
}
|
|
case 'GatherV2': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var batchDims = getParamValue('batchDims', node, tensorMap, context);
|
|
var input = getParamValue('x', node, tensorMap, context);
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
return [ops.gather(input, ops.cast(indices, 'int32'), axis, batchDims)];
|
|
}
|
|
case 'Reverse': {
|
|
var dims = getParamValue('dims', node, tensorMap, context);
|
|
var axis = [];
|
|
for (var i = 0; i < dims.length; i++) {
|
|
if (dims[i]) {
|
|
axis.push(i);
|
|
}
|
|
}
|
|
var input = getParamValue('x', node, tensorMap, context);
|
|
return [ops.reverse(input, axis)];
|
|
}
|
|
case 'ReverseV2': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var input = getParamValue('x', node, tensorMap, context);
|
|
return [ops.reverse(input, axis)];
|
|
}
|
|
case 'Slice': {
|
|
// tslint:disable-next-line:no-any
|
|
var begin = getParamValue('begin', node, tensorMap, context);
|
|
// tslint:disable-next-line:no-any
|
|
var size = getParamValue('size', node, tensorMap, context);
|
|
return [ops.slice(getParamValue('x', node, tensorMap, context), begin, size)];
|
|
}
|
|
case 'StridedSlice': {
|
|
var begin = getParamValue('begin', node, tensorMap, context);
|
|
var end = getParamValue('end', node, tensorMap, context);
|
|
var strides = getParamValue('strides', node, tensorMap, context);
|
|
var beginMask = getParamValue('beginMask', node, tensorMap, context);
|
|
var endMask = getParamValue('endMask', node, tensorMap, context);
|
|
var ellipsisMask = getParamValue('ellipsisMask', node, tensorMap, context);
|
|
var newAxisMask = getParamValue('newAxisMask', node, tensorMap, context);
|
|
var shrinkAxisMask = getParamValue('shrinkAxisMask', node, tensorMap, context);
|
|
var tensor = getParamValue('x', node, tensorMap, context);
|
|
return [ops.stridedSlice(tensor, begin, end, strides, beginMask, endMask, ellipsisMask, newAxisMask, shrinkAxisMask)];
|
|
}
|
|
case 'Pack': {
|
|
return tfc.tidy(function () {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var tensors = getParamValue('tensors', node, tensorMap, context);
|
|
// Reshape the tensors to the first tensor's shape if they don't
|
|
// match.
|
|
var shape = tensors[0].shape;
|
|
var squeezedShape = ops.squeeze(tensors[0]).shape;
|
|
var mapped = tensors.map(function (tensor) {
|
|
var sameShape = tfc.util.arraysEqual(tensor.shape, shape);
|
|
if (!sameShape &&
|
|
!tfc.util.arraysEqual(ops.squeeze(tensor).shape, squeezedShape)) {
|
|
throw new Error('the input tensors shape does not match');
|
|
}
|
|
return sameShape ? tensor : ops.reshape(tensor, shape);
|
|
});
|
|
return [ops.stack(mapped, axis)];
|
|
});
|
|
}
|
|
case 'Unpack': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var tensor = getParamValue('tensor', node, tensorMap, context);
|
|
return ops.unstack(tensor, axis);
|
|
}
|
|
case 'Tile': {
|
|
var reps = getParamValue('reps', node, tensorMap, context);
|
|
return [ops.tile(getParamValue('x', node, tensorMap, context), reps)];
|
|
}
|
|
case 'Split':
|
|
case 'SplitV': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
var numOrSizeSplits = getParamValue('numOrSizeSplits', node, tensorMap, context);
|
|
var tensor = getParamValue('x', node, tensorMap, context);
|
|
return ops.split(tensor, numOrSizeSplits, axis);
|
|
}
|
|
case 'ScatterNd': {
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
var values = getParamValue('values', node, tensorMap, context);
|
|
var shape = getParamValue('shape', node, tensorMap, context);
|
|
return [ops.scatterND(indices, values, shape)];
|
|
}
|
|
case 'GatherNd': {
|
|
var x = getParamValue('x', node, tensorMap, context);
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
return [ops.gatherND(x, indices)];
|
|
}
|
|
case 'SparseToDense': {
|
|
var indices = getParamValue('sparseIndices', node, tensorMap, context);
|
|
var shape = getParamValue('outputShape', node, tensorMap, context);
|
|
var sparseValues = getParamValue('sparseValues', node, tensorMap, context);
|
|
var defaultValue = getParamValue('defaultValue', node, tensorMap, context);
|
|
return [ops.sparseToDense(indices, sparseValues, shape, sparseValues.dtype === defaultValue.dtype ?
|
|
defaultValue :
|
|
ops.cast(defaultValue, sparseValues.dtype))];
|
|
}
|
|
case 'TensorScatterUpdate': {
|
|
var indices = getParamValue('indices', node, tensorMap, context);
|
|
var values = getParamValue('values', node, tensorMap, context);
|
|
var tensor = getParamValue('tensor', node, tensorMap, context);
|
|
return [ops.tensorScatterUpdate(tensor, indices, values)];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$4 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'SparseFillEmptyRows': {
|
|
var _a = ops.sparse.sparseFillEmptyRows(getParamValue('indices', node, tensorMap, context), getParamValue('values', node, tensorMap, context), getParamValue('denseShape', node, tensorMap, context), getParamValue('defaultValue', node, tensorMap, context)), outputIndices = _a.outputIndices, outputValues = _a.outputValues, emptyRowIndicator = _a.emptyRowIndicator, reverseIndexMap = _a.reverseIndexMap;
|
|
return [
|
|
outputIndices, outputValues, emptyRowIndicator, reverseIndexMap
|
|
];
|
|
}
|
|
case 'SparseReshape': {
|
|
var _b = ops.sparse.sparseReshape(getParamValue('inputIndices', node, tensorMap, context), getParamValue('inputShape', node, tensorMap, context), getParamValue('newShape', node, tensorMap, context)), outputIndices = _b.outputIndices, outputShape = _b.outputShape;
|
|
return [outputIndices, outputShape];
|
|
}
|
|
case 'SparseSegmentMean': {
|
|
var outputData = ops.sparse.sparseSegmentMean(getParamValue('data', node, tensorMap, context), getParamValue('indices', node, tensorMap, context), getParamValue('segmentIds', node, tensorMap, context));
|
|
return [outputData];
|
|
}
|
|
case 'SparseSegmentSum': {
|
|
var outputData = ops.sparse.sparseSegmentSum(getParamValue('data', node, tensorMap, context), getParamValue('indices', node, tensorMap, context), getParamValue('segmentIds', node, tensorMap, context));
|
|
return [outputData];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$3 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'FFT': {
|
|
return [ops.fft(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'IFFT': {
|
|
return [ops.ifft(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'RFFT': {
|
|
return [ops.rfft(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
case 'IRFFT': {
|
|
return [ops.irfft(getParamValue('x', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$2 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'StaticRegexReplace': {
|
|
return [ops.string.staticRegexReplace(getParamValue('input', node, tensorMap, context), getParamValue('pattern', node, tensorMap, context), getParamValue('rewrite', node, tensorMap, context), getParamValue('replaceGlobal', node, tensorMap, context))];
|
|
}
|
|
case 'StringNGrams': {
|
|
var _a = ops.string.stringNGrams(getParamValue('data', node, tensorMap, context), getParamValue('dataSplits', node, tensorMap, context), getParamValue('separator', node, tensorMap, context), getParamValue('nGramWidths', node, tensorMap, context), getParamValue('leftPad', node, tensorMap, context), getParamValue('rightPad', node, tensorMap, context), getParamValue('padWidth', node, tensorMap, context), getParamValue('preserveShortSequences', node, tensorMap, context)), nGrams = _a.nGrams, nGramsSplits = _a.nGramsSplits;
|
|
return [nGrams, nGramsSplits];
|
|
}
|
|
case 'StringSplit': {
|
|
var _b = ops.string.stringSplit(getParamValue('input', node, tensorMap, context), getParamValue('delimiter', node, tensorMap, context), getParamValue('skipEmpty', node, tensorMap, context)), indices = _b.indices, values = _b.values, shape = _b.shape;
|
|
return [indices, values, shape];
|
|
}
|
|
case 'StringToHashBucketFast': {
|
|
var output = ops.string.stringToHashBucketFast(getParamValue('input', node, tensorMap, context), getParamValue('numBuckets', node, tensorMap, context));
|
|
return [output];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
var executeOp$1 = function (node, tensorMap, context, ops) {
|
|
if (ops === void 0) { ops = tfOps; }
|
|
switch (node.op) {
|
|
case 'Cast': {
|
|
return [ops.cast(getParamValue('x', node, tensorMap, context), getParamValue('dtype', node, tensorMap, context))];
|
|
}
|
|
case 'ExpandDims': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
return [ops.expandDims(getParamValue('x', node, tensorMap, context), axis)];
|
|
}
|
|
case 'Squeeze': {
|
|
var axis = getParamValue('axis', node, tensorMap, context);
|
|
return [ops.squeeze(getParamValue('x', node, tensorMap, context), axis)];
|
|
}
|
|
case 'Reshape': {
|
|
return [ops.reshape(getParamValue('x', node, tensorMap, context), getParamValue('shape', node, tensorMap, context))];
|
|
}
|
|
case 'EnsureShape': {
|
|
return [ops.ensureShape(getParamValue('x', node, tensorMap, context), getParamValue('shape', node, tensorMap, context))];
|
|
}
|
|
case 'MirrorPad': {
|
|
return [ops.mirrorPad(getParamValue('x', node, tensorMap, context), getParamValue('padding', node, tensorMap, context), getParamValue('mode', node, tensorMap, context))];
|
|
}
|
|
case 'PadV2':
|
|
case 'Pad': {
|
|
return [ops.pad(getParamValue('x', node, tensorMap, context), getParamValue('padding', node, tensorMap, context), getParamValue('constantValue', node, tensorMap, context))];
|
|
}
|
|
case 'SpaceToBatchND': {
|
|
var blockShape = getParamValue('blockShape', node, tensorMap, context);
|
|
var paddings = getParamValue('paddings', node, tensorMap, context);
|
|
return [ops.spaceToBatchND(getParamValue('x', node, tensorMap, context), blockShape, paddings)];
|
|
}
|
|
case 'BatchToSpaceND': {
|
|
var blockShape = getParamValue('blockShape', node, tensorMap, context);
|
|
var crops = getParamValue('crops', node, tensorMap, context);
|
|
return [ops.batchToSpaceND(getParamValue('x', node, tensorMap, context), blockShape, crops)];
|
|
}
|
|
case 'DepthToSpace': {
|
|
var blockSize = getParamValue('blockSize', node, tensorMap, context);
|
|
var dataFormat = getParamValue('dataFormat', node, tensorMap, context).toUpperCase();
|
|
return [ops.depthToSpace(getParamValue('x', node, tensorMap, context), blockSize, dataFormat)];
|
|
}
|
|
case 'BroadcastTo': {
|
|
return [ops.broadcastTo(getParamValue('x', node, tensorMap, context), getParamValue('shape', node, tensorMap, context))];
|
|
}
|
|
case 'BroadcastArgs': {
|
|
return [ops.broadcastArgs(getParamValue('s0', node, tensorMap, context), getParamValue('s1', node, tensorMap, context))];
|
|
}
|
|
default:
|
|
throw TypeError("Node type ".concat(node.op, " is not implemented"));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @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 op defined by the node object.
|
|
* @param node
|
|
* @param tensorMap contains tensors for executed nodes and weights
|
|
* @param context contains tensors and information for running the current node.
|
|
* @param resourceManager Optional. Contains global resources of the model.
|
|
*/
|
|
function executeOp(node, tensorMap, context, resourceManager, tidy) {
|
|
if (tidy === void 0) { tidy = tfc__namespace.tidy; }
|
|
var value = (function (node, tensorMap, context) {
|
|
switch (node.category) {
|
|
case 'arithmetic':
|
|
return tidy(function () { return executeOp$k(node, tensorMap, context); });
|
|
case 'basic_math':
|
|
return tidy(function () { return executeOp$j(node, tensorMap, context); });
|
|
case 'control':
|
|
return executeOp$i(node, tensorMap, context);
|
|
case 'convolution':
|
|
return tidy(function () { return executeOp$h(node, tensorMap, context); });
|
|
case 'creation':
|
|
return tidy(function () { return executeOp$g(node, tensorMap, context); });
|
|
case 'dynamic':
|
|
return executeOp$f(node, tensorMap, context);
|
|
case 'evaluation':
|
|
return tidy(function () { return executeOp$e(node, tensorMap, context); });
|
|
case 'image':
|
|
return tidy(function () { return executeOp$b(node, tensorMap, context); });
|
|
case 'graph':
|
|
return tidy(function () { return executeOp$d(node, tensorMap, context); });
|
|
case 'logical':
|
|
return tidy(function () { return executeOp$a(node, tensorMap, context); });
|
|
case 'matrices':
|
|
return tidy(function () { return executeOp$9(node, tensorMap, context); });
|
|
case 'normalization':
|
|
return tidy(function () { return executeOp$8(node, tensorMap, context); });
|
|
case 'ragged':
|
|
return tidy(function () { return executeOp$7(node, tensorMap, context); });
|
|
case 'reduction':
|
|
return tidy(function () { return executeOp$6(node, tensorMap, context); });
|
|
case 'slice_join':
|
|
return tidy(function () { return executeOp$5(node, tensorMap, context); });
|
|
case 'sparse':
|
|
return tidy(function () { return executeOp$4(node, tensorMap, context); });
|
|
case 'spectral':
|
|
return tidy(function () { return executeOp$3(node, tensorMap, context); });
|
|
case 'string':
|
|
return tidy(function () { return executeOp$2(node, tensorMap, context); });
|
|
case 'transformation':
|
|
return tidy(function () { return executeOp$1(node, tensorMap, context); });
|
|
case 'hash_table':
|
|
return executeOp$c(node, tensorMap, context, resourceManager);
|
|
case 'custom':
|
|
var opMapper = getRegisteredOp(node.op);
|
|
if (opMapper && opMapper.customExecutor) {
|
|
return opMapper.customExecutor(new NodeValueImpl(node, tensorMap, context));
|
|
}
|
|
else {
|
|
throw TypeError("Custom op ".concat(node.op, " is not registered."));
|
|
}
|
|
default:
|
|
throw TypeError("Unknown op '".concat(node.op, "'. File an issue at ") +
|
|
"https://github.com/tensorflow/tfjs/issues so we can add it" +
|
|
", or register a custom execution with tf.registerOp()");
|
|
}
|
|
})(node, tensorMap, context);
|
|
if (tfc__namespace.util.isPromise(value)) {
|
|
return value.then(function (data) { return [].concat(data); });
|
|
}
|
|
return [].concat(value);
|
|
}
|
|
|
|
/**
|
|
* ExecutionContext captures the runtime environment of the node. It keeps
|
|
* track of the current frame and iteration for the control flow ops.
|
|
*
|
|
* For example, typical Dynamic RNN model may contain loops, for which
|
|
* TensorFlow will generate graphs with Enter/Exit nodes to control the
|
|
* current execution frame, and NextIteration Nodes for iteration id increment.
|
|
* For model with branch logic, TensorFLow will generate Switch/Merge ops.
|
|
*/
|
|
var ExecutionContext = /** @class */ (function () {
|
|
function ExecutionContext(weightMap, tensorArrayMap, tensorListMap, functionMap, parseNodeNameCache) {
|
|
if (weightMap === void 0) { weightMap = {}; }
|
|
if (tensorArrayMap === void 0) { tensorArrayMap = {}; }
|
|
if (tensorListMap === void 0) { tensorListMap = {}; }
|
|
if (functionMap === void 0) { functionMap = {}; }
|
|
this.weightMap = weightMap;
|
|
this.tensorArrayMap = tensorArrayMap;
|
|
this.tensorListMap = tensorListMap;
|
|
this.functionMap = functionMap;
|
|
this.parseNodeNameCache = parseNodeNameCache;
|
|
this.rootContext = { id: 0, frameName: '', iterationId: 0 };
|
|
this.contexts = [this.rootContext];
|
|
this.lastId = 0;
|
|
this.generateCurrentContextIds();
|
|
}
|
|
ExecutionContext.prototype.newFrame = function (id, frameName) {
|
|
return { id: id, frameName: frameName, iterationId: 0 };
|
|
};
|
|
Object.defineProperty(ExecutionContext.prototype, "currentContext", {
|
|
get: function () {
|
|
return this.contexts;
|
|
},
|
|
/**
|
|
* Set the current context
|
|
* @param contexts: ExecutionContextInfo[] the current path of execution
|
|
* frames
|
|
*/
|
|
set: function (contexts) {
|
|
if (this.contexts !== contexts) {
|
|
this.contexts = contexts;
|
|
this.generateCurrentContextIds();
|
|
}
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(ExecutionContext.prototype, "currentContextId", {
|
|
/**
|
|
* Returns the current context in string format.
|
|
*/
|
|
get: function () {
|
|
return this._currentContextIds[0];
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(ExecutionContext.prototype, "currentContextIds", {
|
|
/**
|
|
* Returns the current context and all parent contexts in string format.
|
|
* This allow access to the nodes in the current and parent frames.
|
|
*/
|
|
get: function () {
|
|
return this._currentContextIds;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
ExecutionContext.prototype.generateCurrentContextIds = function () {
|
|
var names = [];
|
|
for (var i = 0; i < this.contexts.length - 1; i++) {
|
|
var contexts = this.contexts.slice(0, this.contexts.length - i);
|
|
names.push(this.contextIdforContexts(contexts));
|
|
}
|
|
names.push('');
|
|
this._currentContextIds = names;
|
|
};
|
|
ExecutionContext.prototype.contextIdforContexts = function (contexts) {
|
|
return contexts ?
|
|
contexts
|
|
.map(function (context) { return (context.id === 0 && context.iterationId === 0) ?
|
|
'' :
|
|
"".concat(context.frameName, "-").concat(context.iterationId); })
|
|
.join('/') :
|
|
'';
|
|
};
|
|
/**
|
|
* Enter a new frame, a new context is pushed on the current context list.
|
|
* @param frameId new frame id
|
|
*/
|
|
ExecutionContext.prototype.enterFrame = function (frameId) {
|
|
if (this.contexts) {
|
|
this.lastId++;
|
|
this.contexts = this.contexts.slice();
|
|
this.contexts.push(this.newFrame(this.lastId, frameId));
|
|
this._currentContextIds.unshift(this.contextIdforContexts(this.contexts));
|
|
}
|
|
};
|
|
/**
|
|
* Exit the current frame, the last context is removed from the current
|
|
* context list.
|
|
*/
|
|
ExecutionContext.prototype.exitFrame = function () {
|
|
if (this.contexts && this.contexts.length > 1) {
|
|
this.contexts = this.contexts.slice();
|
|
this.contexts.splice(-1);
|
|
this.currentContextIds.shift();
|
|
}
|
|
else {
|
|
throw new Error('Cannot exit frame, the context is empty');
|
|
}
|
|
};
|
|
/**
|
|
* Enter the next iteration of a loop, the iteration id of last context is
|
|
* increased.
|
|
*/
|
|
ExecutionContext.prototype.nextIteration = function () {
|
|
if (this.contexts && this.contexts.length > 0) {
|
|
this.contexts = this.contexts.slice();
|
|
this.lastId++;
|
|
var context = Object.assign({}, this.contexts[this.contexts.length - 1]);
|
|
context.iterationId += 1;
|
|
context.id = this.lastId;
|
|
this.contexts.splice(-1, 1, context);
|
|
this._currentContextIds.splice(0, 1, this.contextIdforContexts(this.contexts));
|
|
}
|
|
else {
|
|
throw new Error('Cannot increase frame iteration, the context is empty');
|
|
}
|
|
};
|
|
ExecutionContext.prototype.getWeight = function (name) {
|
|
return this.weightMap[name];
|
|
};
|
|
ExecutionContext.prototype.addTensorArray = function (tensorArray) {
|
|
this.tensorArrayMap[tensorArray.id] = tensorArray;
|
|
};
|
|
ExecutionContext.prototype.getTensorArray = function (id) {
|
|
return this.tensorArrayMap[id];
|
|
};
|
|
ExecutionContext.prototype.addTensorList = function (tensorList) {
|
|
this.tensorListMap[tensorList.id] = tensorList;
|
|
};
|
|
ExecutionContext.prototype.getTensorList = function (id) {
|
|
return this.tensorListMap[id];
|
|
};
|
|
ExecutionContext.prototype.dispose = function (keepIds) {
|
|
for (var key in this.tensorArrayMap) {
|
|
this.tensorArrayMap[key].clearAndClose(keepIds);
|
|
}
|
|
for (var key in this.tensorListMap) {
|
|
this.tensorListMap[key].clearAndClose(keepIds);
|
|
}
|
|
};
|
|
return ExecutionContext;
|
|
}());
|
|
|
|
/**
|
|
* Given graph inputs and desired outputs, find the minimal set of nodes
|
|
* to execute in order to compute the outputs. In addition return other useful
|
|
* info such:
|
|
* - Missing inputs needed to compute the output.
|
|
* - Whether the subgraph contains dynamic ops (control flow, dynamic shape).
|
|
* - Alternative inputs in order to avoid async (dynamic op) execution.
|
|
*/
|
|
function getExecutionSubgraph(inputs, outputs, weightMap, initNodes) {
|
|
var usedNodes = new Set();
|
|
var missingInputs = [];
|
|
var dynamicNode = null;
|
|
var syncInputs = null;
|
|
// Start with the outputs, going backwards and find all the nodes that are
|
|
// needed to compute those outputs.
|
|
var seen = new Set();
|
|
var inputNodeNames = new Set(Object.keys(inputs).map(function (name) { return parseNodeName(name)[0]; }));
|
|
initNodes = initNodes || [];
|
|
var initNodeNames = new Set(initNodes.map(function (node) { return parseNodeName(node.name)[0]; }));
|
|
var frontier = __spreadArray([], __read(outputs), false);
|
|
while (frontier.length > 0) {
|
|
var node = frontier.pop();
|
|
if (isControlFlow(node) || isDynamicShape(node) || isHashTable(node)) {
|
|
if (dynamicNode == null) {
|
|
dynamicNode = node;
|
|
syncInputs = dynamicNode.children.map(function (child) { return child.name; })
|
|
.filter(function (name) { return usedNodes.has(name); });
|
|
}
|
|
}
|
|
usedNodes.add(node.name);
|
|
// Weights are dead end since we already have their values.
|
|
if (weightMap[node.name] != null) {
|
|
continue;
|
|
}
|
|
// This node is a dead end since it's one of the user-provided inputs.
|
|
if (inputNodeNames.has(node.name)) {
|
|
continue;
|
|
}
|
|
// This node is a dead end since it doesn't have any inputs.
|
|
if (initNodeNames.has(node.name)) {
|
|
continue;
|
|
}
|
|
if (node.inputs.length === 0) {
|
|
missingInputs.push(node.name);
|
|
continue;
|
|
}
|
|
node.inputs.forEach(function (input) {
|
|
// Don't add to the frontier if it is already there.
|
|
if (seen.has(input.name)) {
|
|
return;
|
|
}
|
|
seen.add(input.name);
|
|
frontier.push(input);
|
|
});
|
|
}
|
|
return { inputs: inputs, outputs: outputs, usedNodes: usedNodes, missingInputs: missingInputs, dynamicNode: dynamicNode, syncInputs: syncInputs };
|
|
}
|
|
/**
|
|
* Given the execution info, return a list of nodes in topological order that
|
|
* need to be executed to compute the output.
|
|
*/
|
|
function getNodesInTopologicalOrder(graph, executionInfo) {
|
|
var e_1, _a, e_2, _b, e_3, _c;
|
|
var usedNodes = executionInfo.usedNodes, inputs = executionInfo.inputs;
|
|
var inputNodes = Object.keys(inputs)
|
|
.map(function (name) { return parseNodeName(name)[0]; })
|
|
.map(function (name) { return graph.nodes[name]; });
|
|
var initNodes = graph.initNodes || [];
|
|
var isUsed = function (node) { return usedNodes.has(typeof node === 'string' ? node : node.name); };
|
|
function unique(nodes) {
|
|
return __spreadArray([], __read(new Map(nodes.map(function (node) { return [node.name, node]; })).values()), false);
|
|
}
|
|
var predefinedNodes = unique(__spreadArray(__spreadArray(__spreadArray([], __read(inputNodes), false), __read(graph.weights), false), __read(initNodes), false)).filter(isUsed);
|
|
var allNodes = unique(__spreadArray(__spreadArray([], __read(predefinedNodes), false), __read(Object.values(graph.nodes)), false)).filter(isUsed);
|
|
var nameToNode = new Map(allNodes.map(function (node) { return [node.name, node]; }));
|
|
var inCounts = {};
|
|
try {
|
|
for (var allNodes_1 = __values(allNodes), allNodes_1_1 = allNodes_1.next(); !allNodes_1_1.done; allNodes_1_1 = allNodes_1.next()) {
|
|
var node = allNodes_1_1.value;
|
|
inCounts[node.name] = inCounts[node.name] || 0;
|
|
try {
|
|
for (var _d = (e_2 = void 0, __values(node.children)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
var child = _e.value;
|
|
// When the child is unused, set in counts to infinity so that it will
|
|
// never be decreased to 0 and added to the execution list.
|
|
if (!isUsed(child)) {
|
|
inCounts[child.name] = Number.POSITIVE_INFINITY;
|
|
}
|
|
inCounts[child.name] = (inCounts[child.name] || 0) + 1;
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (allNodes_1_1 && !allNodes_1_1.done && (_a = allNodes_1.return)) _a.call(allNodes_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
// Build execution order for all used nodes regardless whether they are
|
|
// predefined or not.
|
|
var frontier = Object.entries(inCounts)
|
|
.filter(function (_a) {
|
|
var _b = __read(_a, 2), inCount = _b[1];
|
|
return inCount === 0;
|
|
})
|
|
.map(function (_a) {
|
|
var _b = __read(_a, 1), name = _b[0];
|
|
return name;
|
|
});
|
|
var orderedNodeNames = __spreadArray([], __read(frontier), false);
|
|
while (frontier.length > 0) {
|
|
var nodeName = frontier.pop();
|
|
var node = nameToNode.get(nodeName);
|
|
try {
|
|
for (var _f = (e_3 = void 0, __values(node.children.filter(isUsed))), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
var child = _g.value;
|
|
if (--inCounts[child.name] === 0) {
|
|
orderedNodeNames.push(child.name);
|
|
frontier.push(child.name);
|
|
}
|
|
}
|
|
}
|
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
finally {
|
|
try {
|
|
if (_g && !_g.done && (_c = _f.return)) _c.call(_f);
|
|
}
|
|
finally { if (e_3) throw e_3.error; }
|
|
}
|
|
}
|
|
var orderedNodes = orderedNodeNames.map(function (name) { return nameToNode.get(name); });
|
|
var filteredOrderedNodes = filterPredefinedReachableNodes(orderedNodes, predefinedNodes);
|
|
// TODO: Turn validation on/off with tf env flag.
|
|
validateNodesExecutionOrder(filteredOrderedNodes, predefinedNodes);
|
|
return filteredOrderedNodes;
|
|
}
|
|
/**
|
|
* This is a helper function of `getNodesInTopologicalOrder`.
|
|
* Returns ordered nodes reachable by at least one predefined node.
|
|
* This can help us filter out redundant nodes from the returned node list.
|
|
* For example:
|
|
* If we have four nodes with dependencies like this:
|
|
* a --> b --> c --> d
|
|
* when node `c` is predefined (e.g. given as an input tensor), we can
|
|
* skip node `a` and `b` since their outputs will never be used.
|
|
*
|
|
* @param orderedNodes Graph nodes in execution order.
|
|
* @param predefinedNodes Graph inputs, weights, and init nodes. Nodes in this
|
|
* list must have distinct names.
|
|
*/
|
|
function filterPredefinedReachableNodes(orderedNodes, predefinedNodes) {
|
|
var e_4, _a;
|
|
var nameToNode = new Map(orderedNodes.map(function (node) { return [node.name, node]; }));
|
|
// TODO: Filter out more nodes when >=2 nodes are predefined in a path.
|
|
var stack = predefinedNodes.map(function (node) { return node.name; });
|
|
var predefinedReachableNodeNames = new Set(stack);
|
|
// Perform a DFS starting from the set of all predefined nodes
|
|
// to find the set of all nodes reachable from the predefined nodes.
|
|
while (stack.length > 0) {
|
|
var nodeName = stack.pop();
|
|
var node = nameToNode.get(nodeName);
|
|
try {
|
|
for (var _b = (e_4 = void 0, __values(node.children)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var child = _c.value;
|
|
if (!nameToNode.has(child.name) ||
|
|
predefinedReachableNodeNames.has(child.name)) {
|
|
continue;
|
|
}
|
|
predefinedReachableNodeNames.add(child.name);
|
|
stack.push(child.name);
|
|
}
|
|
}
|
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_4) throw e_4.error; }
|
|
}
|
|
}
|
|
// Filter out unreachable nodes and build the ordered node list.
|
|
var filteredOrderedNodes = orderedNodes.filter(function (node) { return predefinedReachableNodeNames.has(node.name); });
|
|
return filteredOrderedNodes;
|
|
}
|
|
var NodesExecutionOrderError = /** @class */ (function (_super) {
|
|
__extends(NodesExecutionOrderError, _super);
|
|
function NodesExecutionOrderError(message) {
|
|
return _super.call(this, "NodesExecutionOrderError: ".concat(message)) || this;
|
|
}
|
|
return NodesExecutionOrderError;
|
|
}(Error));
|
|
/**
|
|
* This is a helper function of `getNodesInTopologicalOrder`.
|
|
* Validates property: given nodes `a` and `b`, Order(a) > Order(b) if `a`
|
|
* is a child of `b`. This function throws an error if validation fails.
|
|
*
|
|
* @param orderedNodes Graph nodes in execution order.
|
|
* @param predefinedNodes Graph inputs, weights, and init nodes. Nodes in this
|
|
* list must have distinct names.
|
|
*/
|
|
function validateNodesExecutionOrder(orderedNodes, predefinedNodes) {
|
|
var e_5, _a, e_6, _b, e_7, _c;
|
|
var nodeNameToOrder = new Map(orderedNodes.map(function (node, order) { return [node.name, order]; }));
|
|
var predefinedNodeNames = new Set(predefinedNodes.map(function (node) { return node.name; }));
|
|
var isPredefined = function (node) { return predefinedNodeNames.has(typeof node === 'string' ? node : node.name); };
|
|
var willBeExecutedNodeNames = new Set(orderedNodes.map(function (node) { return node.name; }));
|
|
var willBeExecuted = function (node) { return willBeExecutedNodeNames.has(typeof node === 'string' ? node : node.name); };
|
|
try {
|
|
for (var orderedNodes_1 = __values(orderedNodes), orderedNodes_1_1 = orderedNodes_1.next(); !orderedNodes_1_1.done; orderedNodes_1_1 = orderedNodes_1.next()) {
|
|
var node = orderedNodes_1_1.value;
|
|
try {
|
|
for (var _d = (e_6 = void 0, __values(node.children.filter(willBeExecuted))), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
var child = _e.value;
|
|
if (!nodeNameToOrder.has(child.name)) {
|
|
throw new NodesExecutionOrderError("Child ".concat(child.name, " of node ").concat(node.name, " is unreachable."));
|
|
}
|
|
if (nodeNameToOrder.get(node.name) > nodeNameToOrder.get(child.name)) {
|
|
throw new NodesExecutionOrderError("Node ".concat(node.name, " is scheduled to run after its child ").concat(child.name, "."));
|
|
}
|
|
}
|
|
}
|
|
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
finally {
|
|
try {
|
|
if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
|
|
}
|
|
finally { if (e_6) throw e_6.error; }
|
|
}
|
|
if (!isPredefined(node)) {
|
|
try {
|
|
for (var _f = (e_7 = void 0, __values(node.inputs)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
var input = _g.value;
|
|
if (!nodeNameToOrder.has(input.name)) {
|
|
throw new NodesExecutionOrderError("Input ".concat(input.name, " of node ").concat(node.name, " is unreachable."));
|
|
}
|
|
if (nodeNameToOrder.get(input.name) > nodeNameToOrder.get(node.name)) {
|
|
throw new NodesExecutionOrderError("Node ".concat(node.name, " is scheduled to run before its input ").concat(input.name, "."));
|
|
}
|
|
}
|
|
}
|
|
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
finally {
|
|
try {
|
|
if (_g && !_g.done && (_c = _f.return)) _c.call(_f);
|
|
}
|
|
finally { if (e_7) throw e_7.error; }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
finally {
|
|
try {
|
|
if (orderedNodes_1_1 && !orderedNodes_1_1.done && (_a = orderedNodes_1.return)) _a.call(orderedNodes_1);
|
|
}
|
|
finally { if (e_5) throw e_5.error; }
|
|
}
|
|
}
|
|
/**
|
|
* Given the execution info, return a map from node name to the disposable
|
|
* node name list after its execution.
|
|
*
|
|
* @returns A map from node name to disposable nodes after its
|
|
* execution. That is, for a node `x`, `nodeLiveUntilMap[x]` indicates
|
|
* all nodes which their intermediate tensors should be disposed after `x`
|
|
* being executed.
|
|
*/
|
|
function getNodeLiveUntilMap(orderedNodes) {
|
|
var nodeNameToOrder = new Map(orderedNodes.map(function (node, order) { return [node.name, order]; }));
|
|
var INF_LIFE = Number.MAX_SAFE_INTEGER;
|
|
// Make control flow nodes (and consequently their direct parents)
|
|
// live forever since they're tricky to track correctly.
|
|
var selfLifespans = orderedNodes.map(function (node, nodeOrder) { return isControlFlow(node) ? INF_LIFE : nodeOrder; });
|
|
var getSelfLifeSpan = function (node) {
|
|
var selfLife = selfLifespans[nodeNameToOrder.get(node.name)];
|
|
if (selfLife == null) {
|
|
// If nodeToOrder does not contain the node, it is unused or
|
|
// unreachable in graph.
|
|
return -1;
|
|
}
|
|
return selfLife;
|
|
};
|
|
// `liveUntil[i]` points to the last node in the `orderedNodes` array that
|
|
// may depend on tensors from node `i`. It indicates that all the
|
|
// intermediate tensors from `orderedNodes[i]` should be disposed after
|
|
// `orderedNodes[liveUntil[i]]` is executed.
|
|
// A node lives long enough to pass on its tensors to its children.
|
|
// It lives until at least `max(node's position, children's positions)`.
|
|
var liveUntilOrders = orderedNodes.map(function (node, nodeOrder) {
|
|
return node.children.map(getSelfLifeSpan)
|
|
.reduce(function (a, b) { return Math.max(a, b); }, selfLifespans[nodeOrder]);
|
|
});
|
|
// liveUntilMap:
|
|
// - Key: Name of a node `x`
|
|
// - Values: All nodes whose intermediate tensors should be disposed
|
|
// after `x` is executed.
|
|
var liveUntilMap = new Map();
|
|
for (var nodeOrder = 0; nodeOrder < orderedNodes.length; ++nodeOrder) {
|
|
var liveUntilOrder = liveUntilOrders[nodeOrder];
|
|
if (liveUntilOrder === INF_LIFE) {
|
|
continue;
|
|
}
|
|
var node = orderedNodes[nodeOrder];
|
|
var liveUntilNode = orderedNodes[liveUntilOrder];
|
|
if (!liveUntilMap.has(liveUntilNode.name)) {
|
|
liveUntilMap.set(liveUntilNode.name, []);
|
|
}
|
|
liveUntilMap.get(liveUntilNode.name).push(node);
|
|
}
|
|
return liveUntilMap;
|
|
}
|
|
var CONTROL_FLOW_OPS = new Set([
|
|
'Switch', 'Merge', 'Enter', 'Exit', 'NextIteration', 'StatelessIf',
|
|
'StatelessWhile', 'if', 'While'
|
|
]);
|
|
var DYNAMIC_SHAPE_OPS = new Set([
|
|
'NonMaxSuppressionV2', 'NonMaxSuppressionV3', 'NonMaxSuppressionV5', 'Where'
|
|
]);
|
|
var HASH_TABLE_OPS = new Set([
|
|
'HashTable', 'HashTableV2', 'LookupTableImport', 'LookupTableImportV2',
|
|
'LookupTableFind', 'LookupTableFindV2', 'LookupTableSize', 'LookupTableSizeV2'
|
|
]);
|
|
function isControlFlow(node) {
|
|
return CONTROL_FLOW_OPS.has(node.op);
|
|
}
|
|
function isDynamicShape(node) {
|
|
return DYNAMIC_SHAPE_OPS.has(node.op);
|
|
}
|
|
function isHashTable(node) {
|
|
return HASH_TABLE_OPS.has(node.op);
|
|
}
|
|
|
|
var GraphExecutor = /** @class */ (function () {
|
|
/**
|
|
*
|
|
* @param graph Graph the model or function graph to be executed.
|
|
* @param parent When building function exector you need to set the parent
|
|
* executor. Since the weights and function executor maps are set at parant
|
|
* level, that function executor can access the function maps and weight maps
|
|
* through the parent.
|
|
*/
|
|
function GraphExecutor(graph, parent) {
|
|
var _this = this;
|
|
this.graph = graph;
|
|
this.parent = parent;
|
|
this.compiledMap = new Map();
|
|
this.parseNodeNameCache = new Map();
|
|
this._weightMap = {};
|
|
this.SEPARATOR = ',';
|
|
this._functions = {};
|
|
this._functionExecutorMap = {};
|
|
this.keepIntermediateTensors = false;
|
|
this._outputs = graph.outputs;
|
|
this._inputs = graph.inputs;
|
|
this._initNodes = graph.initNodes;
|
|
this._signature = graph.signature;
|
|
this._functions = graph.functions;
|
|
// create sub-graph executors
|
|
if (graph.functions != null) {
|
|
Object.keys(graph.functions).forEach(function (name) {
|
|
_this._functionExecutorMap[name] =
|
|
new GraphExecutor(graph.functions[name], _this);
|
|
});
|
|
}
|
|
}
|
|
Object.defineProperty(GraphExecutor.prototype, "weightIds", {
|
|
get: function () {
|
|
return this.parent ? this.parent.weightIds : this._weightIds;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "functionExecutorMap", {
|
|
get: function () {
|
|
return this.parent ? this.parent.functionExecutorMap :
|
|
this._functionExecutorMap;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "weightMap", {
|
|
get: function () {
|
|
return this.parent ? this.parent.weightMap : this._weightMap;
|
|
},
|
|
set: function (weightMap) {
|
|
var weightIds = Object.keys(weightMap).map(function (key) { return weightMap[key].map(function (tensor) { return tensor.id; }); });
|
|
this._weightIds = [].concat.apply([], __spreadArray([], __read(weightIds), false));
|
|
this._weightMap = weightMap;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "resourceManager", {
|
|
/**
|
|
* Set `ResourceManager` shared by executors of a model.
|
|
* @param resourceManager: `ResourceManager` of the `GraphModel`.
|
|
*/
|
|
set: function (resourceManager) {
|
|
this._resourceManager = resourceManager;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "inputs", {
|
|
get: function () {
|
|
return this._inputs.map(function (node) {
|
|
return {
|
|
name: node.name,
|
|
shape: node.attrParams['shape'] ?
|
|
node.attrParams['shape'].value :
|
|
undefined,
|
|
dtype: node.attrParams['dtype'] ?
|
|
node.attrParams['dtype'].value :
|
|
undefined
|
|
};
|
|
});
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "outputs", {
|
|
get: function () {
|
|
return this._outputs.map(function (node) {
|
|
return {
|
|
name: node.name,
|
|
shape: node.attrParams['shape'] ?
|
|
node.attrParams['shape'].value :
|
|
undefined,
|
|
dtype: node.attrParams['dtype'] ?
|
|
node.attrParams['dtype'].value :
|
|
undefined
|
|
};
|
|
});
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "inputNodes", {
|
|
get: function () {
|
|
return this._inputs.map(function (node) { return node.signatureKey || node.name; });
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "outputNodes", {
|
|
get: function () {
|
|
return this._outputs.map(function (node) {
|
|
var name = node.signatureKey || node.name;
|
|
return node.defaultOutput ? ("".concat(name, ":").concat(node.defaultOutput)) : name;
|
|
});
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphExecutor.prototype, "functions", {
|
|
get: function () {
|
|
var _this = this;
|
|
return Object.keys(this._functions).reduce(function (map, key) {
|
|
map[key] = _this._functions[key].signature;
|
|
return map;
|
|
}, {});
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
GraphExecutor.prototype.getCompilationKey = function (inputs, outputs) {
|
|
var sortedInputs = inputs.map(function (node) { return node.name; }).sort();
|
|
var sortedOutputs = outputs.map(function (node) { return node.name; }).sort();
|
|
return sortedInputs.join(this.SEPARATOR) + '--' +
|
|
sortedOutputs.join(this.SEPARATOR);
|
|
};
|
|
/**
|
|
* Compiles the inference graph and returns the minimal set of nodes that are
|
|
* required for execution, in the correct execution order.
|
|
* @returns {Object} compilation The compile result.
|
|
* @returns {Node[]} compilation.orderedNodes Nodes in the correct execution
|
|
* order.
|
|
* @returns {Map<string, Node[]>} compilation.nodeLiveUntilMap A map from node
|
|
* to disposable nodes after its execution. That is, for a node `x`,
|
|
* `nodeLiveUntilMap[x]` indicates all nodes whose intermediate
|
|
* tensors should be disposed after `x` is executed.
|
|
*/
|
|
GraphExecutor.prototype.compile = function (inputs, outputs) {
|
|
var executionInfo = getExecutionSubgraph(inputs, outputs, this.weightMap, this._initNodes);
|
|
var missingInputs = executionInfo.missingInputs, dynamicNode = executionInfo.dynamicNode, syncInputs = executionInfo.syncInputs;
|
|
if (dynamicNode != null) {
|
|
throw new Error("This execution contains the node '".concat(dynamicNode.name, "', which has ") +
|
|
"the dynamic op '".concat(dynamicNode.op, "'. Please use ") +
|
|
"model.executeAsync() instead. Alternatively, to avoid the " +
|
|
"dynamic ops, specify the inputs [".concat(syncInputs, "]"));
|
|
}
|
|
if (missingInputs.length > 0) {
|
|
var outNames = outputs.map(function (n) { return n.name; });
|
|
var inNames = Object.keys(inputs);
|
|
throw new Error("Cannot compute the outputs [".concat(outNames, "] from the provided inputs ") +
|
|
"[".concat(inNames, "]. Missing the following inputs: [").concat(missingInputs, "]"));
|
|
}
|
|
var orderedNodes = getNodesInTopologicalOrder(this.graph, executionInfo);
|
|
var nodeLiveUntilMap = getNodeLiveUntilMap(orderedNodes);
|
|
return { orderedNodes: orderedNodes, nodeLiveUntilMap: nodeLiveUntilMap };
|
|
};
|
|
GraphExecutor.prototype.cloneAndKeepTensor = function (tensor) {
|
|
if (tensor == null) {
|
|
return null;
|
|
}
|
|
var clone = tensor.clone();
|
|
// Keep the clone because`model.execute()` may be called within
|
|
// a `tidy()`, but the user may inspect these tensors after the
|
|
// tidy.
|
|
tfc.keep(clone);
|
|
return clone;
|
|
};
|
|
GraphExecutor.prototype.cloneTensorList = function (tensors) {
|
|
var _this = this;
|
|
if (!tensors) {
|
|
return null;
|
|
}
|
|
var clonedTensor = tensors.map(function (tensor) {
|
|
return _this.cloneAndKeepTensor(tensor);
|
|
});
|
|
return clonedTensor;
|
|
};
|
|
GraphExecutor.prototype.cloneTensorMap = function (tensorsMap) {
|
|
var _this = this;
|
|
return Object.fromEntries(Object.entries(tensorsMap).map(function (_c) {
|
|
var _d = __read(_c, 2), name = _d[0], tensorsList = _d[1];
|
|
return [name, _this.cloneTensorList(tensorsList)];
|
|
}));
|
|
};
|
|
/**
|
|
* Executes the inference for given input tensors.
|
|
* @param inputs Tensor map for the model inputs, keyed by the input node
|
|
* names.
|
|
* @param outputs Optional. output node name from the Tensorflow model, if
|
|
* no outputs are specified, the default outputs of the model would be used.
|
|
* You can inspect intermediate nodes of the model by adding them to the
|
|
* outputs array.
|
|
*/
|
|
GraphExecutor.prototype.execute = function (inputs, outputs) {
|
|
var _this = this;
|
|
// Dispose any tensors from a prior run to avoid leaking them.
|
|
this.disposeIntermediateTensors();
|
|
inputs = this.mapInputs(inputs);
|
|
var names = Object.keys(inputs).sort();
|
|
this.checkInputs(inputs);
|
|
this.checkInputShapeAndType(inputs);
|
|
outputs = this.mapOutputs(outputs);
|
|
this.checkOutputs(outputs);
|
|
var inputNodes = names.map(function (name) { return _this.graph.nodes[parseNodeName(name)[0]]; });
|
|
var outputNodeNames = outputs.map(function (name) { return parseNodeName(name)[0]; });
|
|
var outputNodeNameSet = new Set(outputNodeNames);
|
|
var outputNodes = outputNodeNames.map(function (name) { return _this.graph.nodes[name]; });
|
|
// If no outputs are specified, then use the default outputs of the model.
|
|
if (outputNodes.length === 0) {
|
|
outputNodes = this._outputs;
|
|
}
|
|
var compilationKey = this.getCompilationKey(inputNodes, outputNodes);
|
|
// Do nothing if the compiled graph cache contains the input.
|
|
var compilation = this.compiledMap.get(compilationKey);
|
|
if (compilation == null) {
|
|
compilation = this.compile(inputs, outputNodes);
|
|
this.compiledMap.set(compilationKey, compilation);
|
|
}
|
|
// Keep tensors if KEEP_INTERMEDIATE_TENSORS is on.
|
|
try {
|
|
this.keepIntermediateTensors = tfc.env().getBool('KEEP_INTERMEDIATE_TENSORS');
|
|
}
|
|
catch (e) {
|
|
this.keepIntermediateTensors = false;
|
|
console.warn(e.message);
|
|
}
|
|
var tensorArrayMap = {};
|
|
var tensorListMap = {};
|
|
return tfc.tidy(function () {
|
|
var e_1, _c;
|
|
var context = new ExecutionContext(_this.weightMap, tensorArrayMap, tensorListMap, _this.functionExecutorMap, _this.parseNodeNameCache);
|
|
var tensorsMap = Object.assign({}, _this.weightMap);
|
|
if (_this.keepIntermediateTensors) {
|
|
_this.clonedTensorsMap = _this.cloneTensorMap(_this.weightMap);
|
|
}
|
|
Object.keys(inputs).forEach(function (name) {
|
|
var _c = __read(parseNodeName(name, context), 2), nodeName = _c[0], index = _c[1];
|
|
var tensors = [];
|
|
tensors[index] = inputs[name];
|
|
tensorsMap[nodeName] = tensors;
|
|
if (_this.keepIntermediateTensors) {
|
|
_this.clonedTensorsMap[nodeName] = _this.cloneTensorList(tensors);
|
|
}
|
|
});
|
|
var tensorsToKeep = _this.getFrozenTensorIds(tensorsMap);
|
|
var orderedNodes = compilation.orderedNodes, nodeLiveUntilMap = compilation.nodeLiveUntilMap;
|
|
try {
|
|
for (var orderedNodes_1 = __values(orderedNodes), orderedNodes_1_1 = orderedNodes_1.next(); !orderedNodes_1_1.done; orderedNodes_1_1 = orderedNodes_1.next()) {
|
|
var node = orderedNodes_1_1.value;
|
|
if (tensorsMap[node.name]) {
|
|
continue;
|
|
}
|
|
var tensors = executeOp(node, tensorsMap, context, _this._resourceManager);
|
|
if (tfc.util.isPromise(tensors)) {
|
|
throw new Error("The execution of the op '".concat(node.op, "' returned a promise. ") +
|
|
"Please use model.executeAsync() instead.");
|
|
}
|
|
tensorsMap[node.name] = tensors;
|
|
if (_this.keepIntermediateTensors) {
|
|
_this.clonedTensorsMap[node.name] = _this.cloneTensorList(tensors);
|
|
}
|
|
_this.checkTensorForDisposalWithNodeLiveUntilInfo(node, tensorsMap, context, tensorsToKeep, outputNodeNameSet, nodeLiveUntilMap.get(node.name));
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (orderedNodes_1_1 && !orderedNodes_1_1.done && (_c = orderedNodes_1.return)) _c.call(orderedNodes_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
// dispose the context for the root executor
|
|
if (_this.parent == null) {
|
|
context.dispose(tensorsToKeep);
|
|
}
|
|
return outputs.map(function (name) { return getTensor(name, tensorsMap, context); });
|
|
});
|
|
};
|
|
GraphExecutor.prototype.getFrozenTensorIds = function (tensorMap) {
|
|
var ids = [].concat.apply([], Object.keys(tensorMap)
|
|
.map(function (key) { return tensorMap[key]; })
|
|
.map(function (tensors) { return tensors.map(function (tensor) { return tensor.id; }); }));
|
|
return new Set(ids);
|
|
};
|
|
GraphExecutor.prototype.checkTensorForDisposal = function (nodeName, node, tensorMap, context, tensorsToKeep, outputNodeNameSet, intermediateTensorConsumerCount) {
|
|
var e_2, _c, e_3, _d, e_4, _e;
|
|
// Skip output nodes and any control flow nodes, since its dependency is
|
|
// tricky to track correctly.
|
|
if (isControlFlow(node) || outputNodeNameSet.has(nodeName)) {
|
|
return;
|
|
}
|
|
try {
|
|
for (var _f = __values(tensorMap[nodeName]), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
var tensor = _g.value;
|
|
if (tensor == null) {
|
|
continue;
|
|
}
|
|
intermediateTensorConsumerCount[tensor.id] =
|
|
(intermediateTensorConsumerCount[tensor.id] || 0) +
|
|
node.children.length;
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_g && !_g.done && (_c = _f.return)) _c.call(_f);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
try {
|
|
for (var _h = __values(node.inputs), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
var input = _j.value;
|
|
// Skip any control flow nodes, since its dependency is tricky to track
|
|
// correctly.
|
|
if (isControlFlow(input)) {
|
|
continue;
|
|
}
|
|
var tensors = getTensorsForCurrentContext(input.name, tensorMap, context);
|
|
if (tensors == null) {
|
|
continue;
|
|
}
|
|
try {
|
|
for (var tensors_1 = (e_4 = void 0, __values(tensors)), tensors_1_1 = tensors_1.next(); !tensors_1_1.done; tensors_1_1 = tensors_1.next()) {
|
|
var tensor = tensors_1_1.value;
|
|
if (!tensor || tensor.kept || tensorsToKeep.has(tensor.id)) {
|
|
continue;
|
|
}
|
|
// Only intermediate nodes' tensors have counts set, not marked as
|
|
// kept, and not in `tensorsToKeep`.
|
|
// Input and weight nodes' tensors should exist in `tensorsToKeep`.
|
|
// Output and control flow nodes' tensors should never have count set.
|
|
var count = intermediateTensorConsumerCount[tensor.id];
|
|
if (count === 1) {
|
|
tensor.dispose();
|
|
delete intermediateTensorConsumerCount[tensor.id];
|
|
}
|
|
else if (count != null) {
|
|
intermediateTensorConsumerCount[tensor.id]--;
|
|
}
|
|
}
|
|
}
|
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
finally {
|
|
try {
|
|
if (tensors_1_1 && !tensors_1_1.done && (_e = tensors_1.return)) _e.call(tensors_1);
|
|
}
|
|
finally { if (e_4) throw e_4.error; }
|
|
}
|
|
}
|
|
}
|
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
finally {
|
|
try {
|
|
if (_j && !_j.done && (_d = _h.return)) _d.call(_h);
|
|
}
|
|
finally { if (e_3) throw e_3.error; }
|
|
}
|
|
};
|
|
GraphExecutor.prototype.checkTensorForDisposalWithNodeLiveUntilInfo = function (node, tensorMap, context, tensorsToKeep, outputNodeNameSet, liveUntilNodes) {
|
|
var e_5, _c, e_6, _d;
|
|
function isNonDisposableNode(node) {
|
|
// Skip output nodes and any control flow nodes, since its dependency is
|
|
// tricky to track correctly.
|
|
return isControlFlow(node) || outputNodeNameSet.has(node.name);
|
|
}
|
|
if (isControlFlow(node) || liveUntilNodes == null) {
|
|
return;
|
|
}
|
|
try {
|
|
for (var liveUntilNodes_1 = __values(liveUntilNodes), liveUntilNodes_1_1 = liveUntilNodes_1.next(); !liveUntilNodes_1_1.done; liveUntilNodes_1_1 = liveUntilNodes_1.next()) {
|
|
var nodeToDispose = liveUntilNodes_1_1.value;
|
|
if (isNonDisposableNode(nodeToDispose)) {
|
|
continue;
|
|
}
|
|
var tensors = getTensorsForCurrentContext(nodeToDispose.name, tensorMap, context);
|
|
try {
|
|
for (var tensors_2 = (e_6 = void 0, __values(tensors)), tensors_2_1 = tensors_2.next(); !tensors_2_1.done; tensors_2_1 = tensors_2.next()) {
|
|
var tensor = tensors_2_1.value;
|
|
if (!tensor || tensor.kept || tensorsToKeep.has(tensor.id)) {
|
|
continue;
|
|
}
|
|
tensor.dispose();
|
|
}
|
|
}
|
|
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
finally {
|
|
try {
|
|
if (tensors_2_1 && !tensors_2_1.done && (_d = tensors_2.return)) _d.call(tensors_2);
|
|
}
|
|
finally { if (e_6) throw e_6.error; }
|
|
}
|
|
}
|
|
}
|
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
finally {
|
|
try {
|
|
if (liveUntilNodes_1_1 && !liveUntilNodes_1_1.done && (_c = liveUntilNodes_1.return)) _c.call(liveUntilNodes_1);
|
|
}
|
|
finally { if (e_5) throw e_5.error; }
|
|
}
|
|
};
|
|
/**
|
|
* Executes the inference for given input tensors in Async fashion.
|
|
* @param inputs Tensor map for the model inputs, keyed by the input node
|
|
* names.
|
|
* @param outputs output node name from the Tensorflow model, if no outputs
|
|
* are specified, the default outputs of the model would be used. You can
|
|
* inspect intermediate nodes of the model by adding them to the outputs
|
|
* array.
|
|
*/
|
|
GraphExecutor.prototype.executeAsync = function (inputs, outputs) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
return __generator(this, function (_c) {
|
|
return [2 /*return*/, this._executeAsync(inputs, outputs)];
|
|
});
|
|
});
|
|
};
|
|
GraphExecutor.prototype.disposeIntermediateTensors = function () {
|
|
if (!this.clonedTensorsMap) {
|
|
return;
|
|
}
|
|
Object.values(this.clonedTensorsMap).forEach(function (tensorsList) {
|
|
var e_7, _c;
|
|
try {
|
|
for (var tensorsList_1 = __values(tensorsList), tensorsList_1_1 = tensorsList_1.next(); !tensorsList_1_1.done; tensorsList_1_1 = tensorsList_1.next()) {
|
|
var tensor = tensorsList_1_1.value;
|
|
if (tensor && !tensor.isDisposed) {
|
|
tensor.dispose();
|
|
}
|
|
}
|
|
}
|
|
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
finally {
|
|
try {
|
|
if (tensorsList_1_1 && !tensorsList_1_1.done && (_c = tensorsList_1.return)) _c.call(tensorsList_1);
|
|
}
|
|
finally { if (e_7) throw e_7.error; }
|
|
}
|
|
});
|
|
this.clonedTensorsMap = null;
|
|
};
|
|
GraphExecutor.prototype.getIntermediateTensors = function () {
|
|
return this.clonedTensorsMap;
|
|
};
|
|
/**
|
|
* Executes the inference for given input tensors in Async fashion.
|
|
* @param inputs Tensor map for the model inputs, keyed by the input node
|
|
* names.
|
|
* @param outputs Optional. output node name from the Tensorflow model,
|
|
* if no outputs are specified, the default outputs of the model would be
|
|
* used. You can inspect intermediate nodes of the model by adding them to
|
|
* the outputs array.
|
|
* @param isFunctionExecution Optional. Flag for executing a function.
|
|
* @param tensorArrayMap Optional, global TensorArray map by id. Used for
|
|
* function execution.
|
|
* @param tensorArrayMap Optional global TensorList map by id. Used for
|
|
* function execution.
|
|
*/
|
|
GraphExecutor.prototype._executeAsync = function (inputs, outputs, isFunctionExecution, tensorArrayMap, tensorListMap) {
|
|
if (isFunctionExecution === void 0) { isFunctionExecution = false; }
|
|
if (tensorArrayMap === void 0) { tensorArrayMap = {}; }
|
|
if (tensorListMap === void 0) { tensorListMap = {}; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var context, tensorsMap, results, outputIds, inputIds, keepIds;
|
|
return __generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
// Dispose any tensors from a prior run to avoid leaking them.
|
|
this.disposeIntermediateTensors();
|
|
if (!isFunctionExecution) {
|
|
inputs = this.mapInputs(inputs);
|
|
this.checkInputs(inputs);
|
|
this.checkInputShapeAndType(inputs);
|
|
outputs = this.mapOutputs(outputs);
|
|
this.checkOutputs(outputs);
|
|
}
|
|
// Keep tensors if KEEP_INTERMEDIATE_TENSORS is on.
|
|
try {
|
|
this.keepIntermediateTensors = tfc.env().getBool('KEEP_INTERMEDIATE_TENSORS');
|
|
}
|
|
catch (e) {
|
|
this.keepIntermediateTensors = false;
|
|
console.warn(e.message);
|
|
}
|
|
context = new ExecutionContext(this.weightMap, tensorArrayMap, tensorListMap, this.functionExecutorMap, this.parseNodeNameCache);
|
|
if (this.keepIntermediateTensors) {
|
|
this.clonedTensorsMap = this.cloneTensorMap(this.weightMap);
|
|
}
|
|
return [4 /*yield*/, this.executeWithControlFlow(inputs, context, outputs, isFunctionExecution)];
|
|
case 1:
|
|
tensorsMap = _c.sent();
|
|
results = outputs.map(function (name) { return getTensor(name, tensorsMap, context); });
|
|
outputIds = results.map(function (t) { return t.id; });
|
|
inputIds = Object.keys(inputs).map(function (name) { return inputs[name].id; });
|
|
keepIds = new Set(__spreadArray(__spreadArray(__spreadArray([], __read(outputIds), false), __read(inputIds), false), __read(this.weightIds), false));
|
|
Object.values(tensorsMap).forEach(function (tensorsList) {
|
|
tensorsList.forEach(function (tensor) {
|
|
if (tensor && !tensor.isDisposed && !keepIds.has(tensor.id)) {
|
|
tensor.dispose();
|
|
}
|
|
});
|
|
});
|
|
// dispose the context for the root executor
|
|
if (this.parent == null) {
|
|
context.dispose(keepIds);
|
|
}
|
|
return [2 /*return*/, results];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
GraphExecutor.prototype.executeFunctionAsync = function (inputs, tensorArrayMap, tensorListMap) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var mappedInputs;
|
|
var _this = this;
|
|
return __generator(this, function (_c) {
|
|
mappedInputs = inputs.reduce(function (map, tensor, index) {
|
|
map[_this.inputs[index].name] = tensor;
|
|
return map;
|
|
}, {});
|
|
return [2 /*return*/, this._executeAsync(mappedInputs, this.outputNodes, true, tensorArrayMap, tensorListMap)];
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* When there are control flow nodes in the graph, the graph execution use
|
|
* ExecutionContext to keep track of the frames and loop iterators.
|
|
* @param inputs placeholder tensors for the graph.
|
|
* @param context the execution context object for current execution.
|
|
* @param outputNames Optional. output node name from the Tensorflow model,
|
|
* if no outputs are specified, the default outputs of the model would be
|
|
* used. You can inspect intermediate nodes of the model by adding them to
|
|
* the outputs array.
|
|
* @param isFunctionExecution Flag for executing a function.
|
|
*/
|
|
GraphExecutor.prototype.executeWithControlFlow = function (inputs, context, outputNames, isFunctionExecution) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var names, inputNodes, outputNodeNames, outputNodeNameSet, outputNodes, _c, usedNodes, missingInputs, dynamicNode, syncInputs, stack, tensorsMap, intermediateTensorConsumerCount, tensorsToKeep, added, promises, missingOutputs, alternativeMsg;
|
|
var _this = this;
|
|
return __generator(this, function (_d) {
|
|
switch (_d.label) {
|
|
case 0:
|
|
names = Object.keys(inputs);
|
|
inputNodes = names.map(function (name) { return _this.graph.nodes[parseNodeName(name)[0]]; });
|
|
outputNodeNames = outputNames.map(function (name) { return parseNodeName(name)[0]; });
|
|
outputNodeNameSet = new Set(outputNodeNames);
|
|
outputNodes = outputNodeNames.map(function (name) { return _this.graph.nodes[name]; });
|
|
// If no outputs are specified, then use the default outputs of the model.
|
|
if (outputNodes.length === 0) {
|
|
outputNodes = this._outputs;
|
|
}
|
|
_c = getExecutionSubgraph(inputs, outputNodes, this.weightMap, this._initNodes), usedNodes = _c.usedNodes, missingInputs = _c.missingInputs, dynamicNode = _c.dynamicNode, syncInputs = _c.syncInputs;
|
|
stack = __spreadArray(__spreadArray(__spreadArray([], __read(inputNodes), false), __read(this.graph.weights), false), __read((this._initNodes || [])), false).map(function (node) {
|
|
return { node: node, contexts: context.currentContext };
|
|
});
|
|
tensorsMap = Object.assign({}, this.weightMap);
|
|
Object.keys(inputs).forEach(function (name) {
|
|
var _c = __read(parseNodeName(name), 2), nodeName = _c[0], index = _c[1];
|
|
var tensors = [];
|
|
tensors[index] = inputs[name];
|
|
tensorsMap[nodeName] = tensors;
|
|
});
|
|
intermediateTensorConsumerCount = {};
|
|
tensorsToKeep = this.getFrozenTensorIds(tensorsMap);
|
|
added = {};
|
|
_d.label = 1;
|
|
case 1:
|
|
if (!(stack.length > 0)) return [3 /*break*/, 3];
|
|
promises = this.processStack(inputNodes, stack, context, tensorsMap, added, tensorsToKeep, outputNodeNameSet, intermediateTensorConsumerCount, usedNodes);
|
|
return [4 /*yield*/, Promise.all(promises)];
|
|
case 2:
|
|
_d.sent();
|
|
return [3 /*break*/, 1];
|
|
case 3:
|
|
if (dynamicNode == null && !isFunctionExecution) {
|
|
console.warn("This model execution did not contain any nodes with control flow " +
|
|
"or dynamic output shapes. You can use model.execute() instead.");
|
|
}
|
|
missingOutputs = outputNodes
|
|
.filter(function (node) { return !isControlFlow(node) &&
|
|
!getTensor(node.name, tensorsMap, context); })
|
|
.map(function (node) { return node.name; });
|
|
if (missingOutputs.length > 0) {
|
|
alternativeMsg = '';
|
|
if (dynamicNode != null) {
|
|
alternativeMsg =
|
|
"Alternatively, to avoid the dynamic ops, use model.execute() " +
|
|
"and specify the inputs [".concat(syncInputs, "]");
|
|
}
|
|
throw new Error("Cannot compute the outputs [".concat(missingOutputs, "] from the provided ") +
|
|
"inputs [".concat(names, "]. Consider providing the following inputs: ") +
|
|
"[".concat(missingInputs, "]. ").concat(alternativeMsg));
|
|
}
|
|
return [2 /*return*/, tensorsMap];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
GraphExecutor.prototype.processStack = function (inputNodes, stack, context, tensorMap, added, tensorsToKeep, outputNodeNameSet, intermediateTensorConsumerCount, usedNodes) {
|
|
var _this = this;
|
|
var promises = [];
|
|
var _loop_1 = function () {
|
|
var _c, _d;
|
|
var item = stack.pop();
|
|
context.currentContext = item.contexts;
|
|
var nodeName = '';
|
|
// The tensor of the Enter op with isConstant set should be set
|
|
// in the parent scope, so it will be available as constant for the
|
|
// whole loop.
|
|
if (item.node.op === 'Enter' &&
|
|
getParamValue('isConstant', item.node, tensorMap, context)) {
|
|
_c = __read(getNodeNameAndIndex(item.node.name, context), 1), nodeName = _c[0];
|
|
}
|
|
// only process nodes that are not in the tensorMap yet, this include
|
|
// inputNodes and internal initNodes.
|
|
if (tensorMap[item.node.name] == null) {
|
|
var tensors = executeOp(item.node, tensorMap, context, this_1._resourceManager);
|
|
if (!nodeName) {
|
|
_d = __read(getNodeNameAndIndex(item.node.name, context), 1), nodeName = _d[0];
|
|
}
|
|
var currentContext_1 = context.currentContext;
|
|
if (tfc.util.isPromise(tensors)) {
|
|
promises.push(tensors.then(function (t) {
|
|
tensorMap[nodeName] = t;
|
|
if (_this.keepIntermediateTensors) {
|
|
_this.clonedTensorsMap[nodeName] = _this.cloneTensorList(t);
|
|
}
|
|
context.currentContext = currentContext_1;
|
|
_this.checkTensorForDisposal(nodeName, item.node, tensorMap, context, tensorsToKeep, outputNodeNameSet, intermediateTensorConsumerCount);
|
|
_this.processChildNodes(item.node, stack, context, tensorMap, added, usedNodes);
|
|
return t;
|
|
}));
|
|
}
|
|
else {
|
|
tensorMap[nodeName] = tensors;
|
|
if (this_1.keepIntermediateTensors) {
|
|
this_1.clonedTensorsMap[nodeName] = this_1.cloneTensorList(tensors);
|
|
}
|
|
this_1.checkTensorForDisposal(nodeName, item.node, tensorMap, context, tensorsToKeep, outputNodeNameSet, intermediateTensorConsumerCount);
|
|
this_1.processChildNodes(item.node, stack, context, tensorMap, added, usedNodes);
|
|
}
|
|
}
|
|
else {
|
|
this_1.processChildNodes(item.node, stack, context, tensorMap, added, usedNodes);
|
|
}
|
|
};
|
|
var this_1 = this;
|
|
while (stack.length > 0) {
|
|
_loop_1();
|
|
}
|
|
return promises;
|
|
};
|
|
GraphExecutor.prototype.processChildNodes = function (node, stack, context, tensorMap, added, usedNodes) {
|
|
node.children.forEach(function (childNode) {
|
|
var _c = __read(getNodeNameAndIndex(childNode.name, context), 1), nodeName = _c[0];
|
|
if (added[nodeName] || !usedNodes.has(childNode.name)) {
|
|
return;
|
|
}
|
|
// Merge op can be pushed if any of its inputs has value.
|
|
if (childNode.op === 'Merge') {
|
|
if (childNode.inputNames.some(function (name) {
|
|
return !!getTensor(name, tensorMap, context);
|
|
})) {
|
|
added[nodeName] = true;
|
|
stack.push({ contexts: context.currentContext, node: childNode });
|
|
}
|
|
}
|
|
else // Otherwise all inputs must to have value.
|
|
if (childNode.inputNames.every(function (name) {
|
|
return !!getTensor(name, tensorMap, context);
|
|
})) {
|
|
added[nodeName] = true;
|
|
stack.push({ contexts: context.currentContext, node: childNode });
|
|
}
|
|
});
|
|
};
|
|
/**
|
|
* Releases the memory used by the weight tensors.
|
|
*/
|
|
GraphExecutor.prototype.dispose = function () {
|
|
var _this = this;
|
|
Object.keys(this.weightMap)
|
|
.forEach(function (key) { return _this.weightMap[key].forEach(function (tensor) { return tensor.dispose(); }); });
|
|
};
|
|
GraphExecutor.prototype.checkInputShapeAndType = function (inputs) {
|
|
var _this = this;
|
|
Object.keys(inputs).forEach(function (name) {
|
|
var input = inputs[name];
|
|
var _c = __read(parseNodeName(name), 1), nodeName = _c[0];
|
|
var node = _this.graph.nodes[nodeName];
|
|
if (node.attrParams['shape'] && node.attrParams['shape'].value) {
|
|
var shape_1 = node.attrParams['shape'].value;
|
|
var match = shape_1.length === input.shape.length &&
|
|
input.shape.every(function (dim, index) { return shape_1[index] === -1 || shape_1[index] === dim; });
|
|
tfc.util.assert(match, function () { return "The shape of dict['".concat(node.name, "'] provided in ") +
|
|
"model.execute(dict) must be [".concat(shape_1, "], but was ") +
|
|
"[".concat(input.shape, "]"); });
|
|
}
|
|
if (node.attrParams['dtype'] && node.attrParams['dtype'].value) {
|
|
tfc.util.assert(input.dtype === node.attrParams['dtype'].value, function () { return "The dtype of dict['".concat(node.name, "'] provided in ") +
|
|
"model.execute(dict) must be " +
|
|
"".concat(node.attrParams['dtype'].value, ", but was ").concat(input.dtype); });
|
|
}
|
|
});
|
|
};
|
|
GraphExecutor.prototype.mapInputs = function (inputs) {
|
|
var _a, _b;
|
|
var result = {};
|
|
for (var inputName in inputs) {
|
|
var tensor = (_b = (_a = this._signature) === null || _a === void 0 ? void 0 : _a.inputs) === null || _b === void 0 ? void 0 : _b[inputName];
|
|
if (tensor != null) {
|
|
result[tensor.name] = inputs[inputName];
|
|
}
|
|
else {
|
|
result[inputName] = inputs[inputName];
|
|
}
|
|
}
|
|
return result;
|
|
};
|
|
GraphExecutor.prototype.checkInputs = function (inputs) {
|
|
var _this = this;
|
|
var notInGraph = Object.keys(inputs).filter(function (name) {
|
|
var _c = __read(parseNodeName(name), 1), nodeName = _c[0];
|
|
return _this.graph.nodes[nodeName] == null;
|
|
});
|
|
if (notInGraph.length > 0) {
|
|
throw new Error("The dict provided in model.execute(dict) has " +
|
|
"keys: [".concat(notInGraph, "] that are not part of graph"));
|
|
}
|
|
};
|
|
GraphExecutor.prototype.mapOutputs = function (outputs) {
|
|
var _this = this;
|
|
return outputs.map(function (name) {
|
|
var _a, _b;
|
|
var tensor = (_b = (_a = _this._signature) === null || _a === void 0 ? void 0 : _a.outputs) === null || _b === void 0 ? void 0 : _b[name];
|
|
if (tensor != null) {
|
|
return tensor.name;
|
|
}
|
|
return name;
|
|
}, {});
|
|
};
|
|
GraphExecutor.prototype.checkOutputs = function (outputs) {
|
|
var _this = this;
|
|
outputs.forEach(function (name) {
|
|
var _c = __read(parseNodeName(name), 1), normalizedName = _c[0];
|
|
if (!_this.graph.nodes[normalizedName]) {
|
|
throw new Error("The output '".concat(name, "' is not found in the graph"));
|
|
}
|
|
});
|
|
};
|
|
return GraphExecutor;
|
|
}());
|
|
|
|
/**
|
|
* Contains global resources of a model.
|
|
*/
|
|
var ResourceManager = /** @class */ (function () {
|
|
function ResourceManager(hashTableNameToHandle, hashTableMap) {
|
|
if (hashTableNameToHandle === void 0) { hashTableNameToHandle = {}; }
|
|
if (hashTableMap === void 0) { hashTableMap = {}; }
|
|
this.hashTableNameToHandle = hashTableNameToHandle;
|
|
this.hashTableMap = hashTableMap;
|
|
}
|
|
/**
|
|
* Register a `HashTable` in the resource manager.
|
|
*
|
|
* The `HashTable` can be retrieved by `resourceManager.getHashTableById`,
|
|
* where id is the table handle tensor's id.
|
|
*
|
|
* @param name Op node name that creates the `HashTable`.
|
|
* @param hashTable The `HashTable` to be added to resource manager.
|
|
*/
|
|
ResourceManager.prototype.addHashTable = function (name, hashTable) {
|
|
this.hashTableNameToHandle[name] = hashTable.handle;
|
|
this.hashTableMap[hashTable.id] = hashTable;
|
|
};
|
|
/**
|
|
* Get the table handle by node name.
|
|
* @param name Op node name that creates the `HashTable`. This name is also
|
|
* used in the inputs list of lookup and import `HashTable` ops.
|
|
*/
|
|
ResourceManager.prototype.getHashTableHandleByName = function (name) {
|
|
return this.hashTableNameToHandle[name];
|
|
};
|
|
/**
|
|
* Get the actual `HashTable` by its handle tensor's id.
|
|
* @param id The id of the handle tensor.
|
|
*/
|
|
ResourceManager.prototype.getHashTableById = function (id) {
|
|
return this.hashTableMap[id];
|
|
};
|
|
/**
|
|
* Dispose `ResourceManager`, including its hashTables and tensors in them.
|
|
*/
|
|
ResourceManager.prototype.dispose = function () {
|
|
for (var key in this.hashTableMap) {
|
|
this.hashTableMap[key].clearAndClose();
|
|
delete this.hashTableMap[key];
|
|
}
|
|
for (var name in this.hashTableNameToHandle) {
|
|
this.hashTableNameToHandle[name].dispose();
|
|
delete this.hashTableNameToHandle[name];
|
|
}
|
|
};
|
|
return ResourceManager;
|
|
}());
|
|
|
|
/**
|
|
* @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.
|
|
* =============================================================================
|
|
*/
|
|
/* Type definitions for exporting and importing of models. */
|
|
/**
|
|
* A map from Tensor dtype to number of bytes per element of the Tensor.
|
|
*/
|
|
var DTYPE_VALUE_SIZE_MAP = {
|
|
'float32': 4,
|
|
'float16': 2,
|
|
'int32': 4,
|
|
'uint16': 2,
|
|
'uint8': 1,
|
|
'bool': 1,
|
|
'complex64': 8
|
|
};
|
|
|
|
/** Number of bytes reserved for the length of the string. (32bit integer). */
|
|
var NUM_BYTES_STRING_LENGTH = 4;
|
|
function getWeightBytelengthAsync(spec, slice) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var size, bytesPerValue, quantization, byteLength, i, _a, _b, _c;
|
|
return __generator(this, function (_d) {
|
|
switch (_d.label) {
|
|
case 0:
|
|
size = sizeFromShape(spec.shape);
|
|
if (!('quantization' in spec)) return [3 /*break*/, 1];
|
|
quantization = spec.quantization;
|
|
bytesPerValue = DTYPE_VALUE_SIZE_MAP[quantization.dtype];
|
|
return [3 /*break*/, 7];
|
|
case 1:
|
|
if (!(spec.dtype === 'string')) return [3 /*break*/, 6];
|
|
byteLength = 0;
|
|
i = 0;
|
|
_d.label = 2;
|
|
case 2:
|
|
if (!(i < size)) return [3 /*break*/, 5];
|
|
_a = byteLength;
|
|
_b = NUM_BYTES_STRING_LENGTH;
|
|
_c = Uint32Array.bind;
|
|
return [4 /*yield*/, slice(byteLength, byteLength + NUM_BYTES_STRING_LENGTH)];
|
|
case 3:
|
|
byteLength = _a + (_b + new (_c.apply(Uint32Array, [void 0, _d.sent()]))()[0]);
|
|
_d.label = 4;
|
|
case 4:
|
|
i++;
|
|
return [3 /*break*/, 2];
|
|
case 5: return [2 /*return*/, byteLength];
|
|
case 6:
|
|
bytesPerValue = DTYPE_VALUE_SIZE_MAP[spec.dtype];
|
|
_d.label = 7;
|
|
case 7: return [2 /*return*/, size * bytesPerValue];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
function decodeWeight(spec, byteBuffer) {
|
|
var name = spec.name;
|
|
var dtype = spec.dtype;
|
|
var shape = spec.shape;
|
|
var size = sizeFromShape(shape);
|
|
var values;
|
|
var offset = 0;
|
|
if ('quantization' in spec) {
|
|
var quantization = spec.quantization;
|
|
if (quantization.dtype === 'uint8' || quantization.dtype === 'uint16') {
|
|
if (!('min' in quantization && 'scale' in quantization)) {
|
|
throw new Error("Weight ".concat(spec.name, " with quantization ").concat(quantization.dtype, " ") +
|
|
"doesn't have corresponding metadata min and scale.");
|
|
}
|
|
}
|
|
else if (quantization.dtype === 'float16') {
|
|
if (dtype !== 'float32') {
|
|
throw new Error("Weight ".concat(spec.name, " is quantized with ").concat(quantization.dtype, " ") +
|
|
"which only supports weights of type float32 not ".concat(dtype, "."));
|
|
}
|
|
}
|
|
else {
|
|
throw new Error("Weight ".concat(spec.name, " has unknown ") +
|
|
"quantization dtype ".concat(quantization.dtype, ". ") +
|
|
"Supported quantization dtypes are: " +
|
|
"'uint8', 'uint16', and 'float16'.");
|
|
}
|
|
var quantizationSizeFactor = DTYPE_VALUE_SIZE_MAP[quantization.dtype];
|
|
var quantizedArray = (quantization.dtype === 'uint8') ?
|
|
new Uint8Array(byteBuffer) :
|
|
new Uint16Array(byteBuffer);
|
|
if (dtype === 'float32') {
|
|
if (quantization.dtype === 'uint8' || quantization.dtype === 'uint16') {
|
|
values = new Float32Array(quantizedArray.length);
|
|
for (var i = 0; i < quantizedArray.length; i++) {
|
|
var v = quantizedArray[i];
|
|
values[i] = v * quantization.scale + quantization.min;
|
|
}
|
|
}
|
|
else if (quantization.dtype === 'float16') {
|
|
// TODO: This is inefficient. Make getFloat16Decoder efficient.
|
|
var float16Decode = getFloat16Decoder();
|
|
values = float16Decode(quantizedArray);
|
|
}
|
|
else {
|
|
throw new Error("Unsupported quantization type ".concat(quantization.dtype, " ") +
|
|
"for weight type float32.");
|
|
}
|
|
}
|
|
else if (dtype === 'int32') {
|
|
if (quantization.dtype !== 'uint8' && quantization.dtype !== 'uint16') {
|
|
throw new Error("Unsupported quantization type ".concat(quantization.dtype, " ") +
|
|
"for weight type int32.");
|
|
}
|
|
values = new Int32Array(quantizedArray.length);
|
|
for (var i = 0; i < quantizedArray.length; i++) {
|
|
var v = quantizedArray[i];
|
|
values[i] = Math.round(v * quantization.scale + quantization.min);
|
|
}
|
|
}
|
|
else {
|
|
throw new Error("Unsupported dtype in weight '".concat(name, "': ").concat(dtype));
|
|
}
|
|
offset += size * quantizationSizeFactor;
|
|
}
|
|
else if (dtype === 'string') {
|
|
var size_1 = sizeFromShape(spec.shape);
|
|
values = [];
|
|
for (var i = 0; i < size_1; i++) {
|
|
var byteLength = new Uint32Array(byteBuffer.slice(offset, offset + NUM_BYTES_STRING_LENGTH))[0];
|
|
offset += NUM_BYTES_STRING_LENGTH;
|
|
var bytes = new Uint8Array(byteBuffer.slice(offset, offset + byteLength));
|
|
values.push(bytes);
|
|
offset += byteLength;
|
|
}
|
|
}
|
|
else {
|
|
var dtypeFactor = DTYPE_VALUE_SIZE_MAP[dtype];
|
|
if (dtype === 'float32') {
|
|
values = new Float32Array(byteBuffer);
|
|
}
|
|
else if (dtype === 'int32') {
|
|
values = new Int32Array(byteBuffer);
|
|
}
|
|
else if (dtype === 'bool') {
|
|
values = new Uint8Array(byteBuffer);
|
|
}
|
|
else if (dtype === 'complex64') {
|
|
values = new Float32Array(byteBuffer);
|
|
var real = new Float32Array(values.length / 2);
|
|
var image = new Float32Array(values.length / 2);
|
|
for (var i = 0; i < real.length; i++) {
|
|
real[i] = values[i * 2];
|
|
image[i] = values[i * 2 + 1];
|
|
}
|
|
var realTensor = tensor(real, shape, 'float32');
|
|
var imageTensor = tensor(image, shape, 'float32');
|
|
var complexTensor = complex(realTensor, imageTensor);
|
|
realTensor.dispose();
|
|
imageTensor.dispose();
|
|
return complexTensor;
|
|
}
|
|
else {
|
|
throw new Error("Unsupported dtype in weight '".concat(name, "': ").concat(dtype));
|
|
}
|
|
offset += size * dtypeFactor;
|
|
}
|
|
return tensor(values, shape, dtype);
|
|
}
|
|
function readToLength(reader, initialData, length) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var data, _a, done, value, missing, newData;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
data = new Uint8Array(initialData);
|
|
_b.label = 1;
|
|
case 1:
|
|
if (!(data.byteLength < length)) return [3 /*break*/, 3];
|
|
return [4 /*yield*/, reader.read()];
|
|
case 2:
|
|
_a = _b.sent(), done = _a.done, value = _a.value;
|
|
if (done && value == null) {
|
|
missing = length - data.byteLength;
|
|
throw new Error("Reader is done but ".concat(missing, " bytes are still expected"));
|
|
}
|
|
newData = new Uint8Array(data.length + value.byteLength);
|
|
newData.set(data, 0);
|
|
newData.set(new Uint8Array(value), data.length);
|
|
data = newData;
|
|
return [3 /*break*/, 1];
|
|
case 3: return [2 /*return*/, data.buffer];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
function decodeWeightsStream(weightStream, specs) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var tensors, reader, data, specs_2, specs_2_1, spec, byteLength, tensorData, weightTensor, b, e_2_1;
|
|
var e_2, _a;
|
|
var _this = this;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
tensors = {};
|
|
reader = weightStream.getReader();
|
|
data = new ArrayBuffer(0);
|
|
_b.label = 1;
|
|
case 1:
|
|
_b.trys.push([1, 7, 8, 9]);
|
|
specs_2 = __values(specs), specs_2_1 = specs_2.next();
|
|
_b.label = 2;
|
|
case 2:
|
|
if (!!specs_2_1.done) return [3 /*break*/, 6];
|
|
spec = specs_2_1.value;
|
|
return [4 /*yield*/, getWeightBytelengthAsync(spec, function (start, end) { return __awaiter(_this, void 0, void 0, function () {
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, readToLength(reader, data, end)];
|
|
case 1:
|
|
data = _a.sent();
|
|
return [2 /*return*/, data.slice(start, end)];
|
|
}
|
|
});
|
|
}); })];
|
|
case 3:
|
|
byteLength = _b.sent();
|
|
return [4 /*yield*/, readToLength(reader, data, byteLength)];
|
|
case 4:
|
|
data = _b.sent();
|
|
tensorData = data.slice(0, byteLength);
|
|
data = data.slice(byteLength);
|
|
weightTensor = decodeWeight(spec, tensorData);
|
|
tensors[spec.name] = weightTensor;
|
|
// TODO(mattsoulanille): Better way to call uploadToGPU.
|
|
// TODO(mattsoulanille): Make this work for webgl too.
|
|
if (getBackend() === 'webgpu') {
|
|
b = backend();
|
|
if ('uploadToGPU' in b &&
|
|
sizeFromShape(weightTensor.shape) >= env()
|
|
.get('WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD')) {
|
|
b.uploadToGPU(weightTensor.dataId);
|
|
}
|
|
}
|
|
_b.label = 5;
|
|
case 5:
|
|
specs_2_1 = specs_2.next();
|
|
return [3 /*break*/, 2];
|
|
case 6: return [3 /*break*/, 9];
|
|
case 7:
|
|
e_2_1 = _b.sent();
|
|
e_2 = { error: e_2_1 };
|
|
return [3 /*break*/, 9];
|
|
case 8:
|
|
try {
|
|
if (specs_2_1 && !specs_2_1.done && (_a = specs_2.return)) _a.call(specs_2);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
return [7 /*endfinally*/];
|
|
case 9: return [2 /*return*/, tensors];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Computes mantisa table for casting Float16 to Float32
|
|
* See http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
|
|
*
|
|
* @returns Uint32Array, 2048 mantissa lookup values.
|
|
*/
|
|
function computeFloat16MantisaTable() {
|
|
var convertMantissa = function (i) {
|
|
var m = i << 13;
|
|
var e = 0;
|
|
while ((m & 0x00800000) === 0) {
|
|
e -= 0x00800000;
|
|
m <<= 1;
|
|
}
|
|
m &= ~0x00800000;
|
|
e += 0x38800000;
|
|
return m | e;
|
|
};
|
|
var mantisaTable = new Uint32Array(2048);
|
|
mantisaTable[0] = 0;
|
|
for (var i = 1; i < 1024; i++) {
|
|
mantisaTable[i] = convertMantissa(i);
|
|
}
|
|
for (var i = 1024; i < 2048; i++) {
|
|
mantisaTable[i] = 0x38000000 + ((i - 1024) << 13);
|
|
}
|
|
return mantisaTable;
|
|
}
|
|
/**
|
|
* Computes exponent table for casting Float16 to Float32
|
|
* See http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
|
|
*
|
|
* @returns Uint32Array, 64 exponent lookup values.
|
|
*/
|
|
function computeFloat16ExponentTable() {
|
|
var exponentTable = new Uint32Array(64);
|
|
exponentTable[0] = 0;
|
|
exponentTable[31] = 0x47800000;
|
|
exponentTable[32] = 0x80000000;
|
|
exponentTable[63] = 0xc7800000;
|
|
for (var i = 1; i < 31; i++) {
|
|
exponentTable[i] = i << 23;
|
|
}
|
|
for (var i = 33; i < 63; i++) {
|
|
exponentTable[i] = 0x80000000 + ((i - 32) << 23);
|
|
}
|
|
return exponentTable;
|
|
}
|
|
/**
|
|
* Computes offset table for casting Float16 to Float32
|
|
* See http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
|
|
*
|
|
* @returns Uint32Array, 6d offset values.
|
|
*/
|
|
function computeFloat16OffsetTable() {
|
|
var offsetTable = new Uint32Array(64);
|
|
for (var i = 0; i < 64; i++) {
|
|
offsetTable[i] = 1024;
|
|
}
|
|
offsetTable[0] = offsetTable[32] = 0;
|
|
return offsetTable;
|
|
}
|
|
/**
|
|
* Retrieve a Float16 decoder which will decode a ByteArray of Float16 values
|
|
* to a Float32Array.
|
|
*
|
|
* @returns Function (buffer: Uint16Array) => Float32Array which decodes
|
|
* the Uint16Array of Float16 bytes to a Float32Array.
|
|
*/
|
|
function getFloat16Decoder() {
|
|
// Algorithm is based off of
|
|
// http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
|
|
// Cache lookup tables
|
|
var mantisaTable = computeFloat16MantisaTable();
|
|
var exponentTable = computeFloat16ExponentTable();
|
|
var offsetTable = computeFloat16OffsetTable();
|
|
return function (quantizedArray) {
|
|
var buffer = new ArrayBuffer(4 * quantizedArray.length);
|
|
var bufferUint32View = new Uint32Array(buffer);
|
|
for (var index = 0; index < quantizedArray.length; index++) {
|
|
var float16Bits = quantizedArray[index];
|
|
var float32Bits = mantisaTable[offsetTable[float16Bits >> 10] + (float16Bits & 0x3ff)] +
|
|
exponentTable[float16Bits >> 10];
|
|
bufferUint32View[index] = float32Bits;
|
|
}
|
|
return new Float32Array(buffer);
|
|
};
|
|
}
|
|
|
|
var TFHUB_SEARCH_PARAM = '?tfjs-format=file';
|
|
var DEFAULT_MODEL_NAME = 'model.json';
|
|
/**
|
|
* A `tf.GraphModel` is a directed, acyclic graph built from a
|
|
* SavedModel GraphDef and allows inference execution.
|
|
*
|
|
* A `tf.GraphModel` can only be created by loading from a model converted from
|
|
* a [TensorFlow SavedModel](https://www.tensorflow.org/guide/saved_model) using
|
|
* the command line converter tool and loaded via `tf.loadGraphModel`.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
var GraphModel = /** @class */ (function () {
|
|
/**
|
|
* @param modelUrl url for the model, or an `io.IOHandler`.
|
|
* @param weightManifestUrl url for the weight file generated by
|
|
* scripts/convert.py script.
|
|
* @param requestOption options for Request, which allows to send credentials
|
|
* and custom headers.
|
|
* @param onProgress Optional, progress callback function, fired periodically
|
|
* before the load is completed.
|
|
*/
|
|
function GraphModel(modelUrl, loadOptions, tfio) {
|
|
if (loadOptions === void 0) { loadOptions = {}; }
|
|
if (tfio === void 0) { tfio = tfc.io; }
|
|
this.modelUrl = modelUrl;
|
|
this.loadOptions = loadOptions;
|
|
this.version = 'n/a';
|
|
this.io = tfio;
|
|
if (loadOptions == null) {
|
|
this.loadOptions = {};
|
|
}
|
|
this.resourceManager = new ResourceManager();
|
|
}
|
|
Object.defineProperty(GraphModel.prototype, "modelVersion", {
|
|
// Returns the version information for the tensorflow model GraphDef.
|
|
get: function () {
|
|
return this.version;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "inputNodes", {
|
|
get: function () {
|
|
return this.executor.inputNodes;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "outputNodes", {
|
|
get: function () {
|
|
return this.executor.outputNodes;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "inputs", {
|
|
get: function () {
|
|
return this.executor.inputs;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "outputs", {
|
|
get: function () {
|
|
return this.executor.outputs;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "weights", {
|
|
get: function () {
|
|
return this.executor.weightMap;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "metadata", {
|
|
get: function () {
|
|
return this.artifacts.userDefinedMetadata;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "modelSignature", {
|
|
get: function () {
|
|
return this.signature;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(GraphModel.prototype, "modelStructuredOutputKeys", {
|
|
get: function () {
|
|
return this.structuredOutputKeys;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
GraphModel.prototype.findIOHandler = function () {
|
|
var path = this.modelUrl;
|
|
if (path.load != null) {
|
|
// Path is an IO Handler.
|
|
this.handler = path;
|
|
}
|
|
else if (this.loadOptions.requestInit != null) {
|
|
this.handler = this.io.browserHTTPRequest(path, this.loadOptions);
|
|
}
|
|
else {
|
|
var handlers = this.io.getLoadHandlers(path, this.loadOptions);
|
|
if (handlers.length === 0) {
|
|
// For backward compatibility: if no load handler can be found,
|
|
// assume it is a relative http path.
|
|
handlers.push(this.io.browserHTTPRequest(path, this.loadOptions));
|
|
}
|
|
else if (handlers.length > 1) {
|
|
throw new Error("Found more than one (".concat(handlers.length, ") load handlers for ") +
|
|
"URL '".concat([path], "'"));
|
|
}
|
|
this.handler = handlers[0];
|
|
}
|
|
};
|
|
/**
|
|
* Loads the model and weight files, construct the in memory weight map and
|
|
* compile the inference graph.
|
|
*/
|
|
GraphModel.prototype.load = function () {
|
|
var _this = this;
|
|
this.findIOHandler();
|
|
if (this.handler.load == null) {
|
|
throw new Error('Cannot proceed with model loading because the IOHandler provided ' +
|
|
'does not have the `load` method implemented.');
|
|
}
|
|
var loadResult = this.handler.load();
|
|
if (tfc.util.isPromise(loadResult)) {
|
|
return loadResult.then(function (artifacts) {
|
|
if (artifacts.getWeightStream == null) {
|
|
return _this.loadSync(artifacts);
|
|
}
|
|
return _this.loadStreaming(artifacts);
|
|
});
|
|
}
|
|
return this.loadSync(loadResult);
|
|
};
|
|
/**
|
|
* Synchronously construct the in memory weight map and
|
|
* compile the inference graph.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes', ignoreCI: true}
|
|
*/
|
|
GraphModel.prototype.loadSync = function (artifacts) {
|
|
var weightMap = this.io.decodeWeights(artifacts.weightData, artifacts.weightSpecs);
|
|
return this.loadWithWeightMap(artifacts, weightMap);
|
|
};
|
|
GraphModel.prototype.loadStreaming = function (artifacts) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var weightMap;
|
|
return __generator(this, function (_d) {
|
|
switch (_d.label) {
|
|
case 0:
|
|
if (artifacts.getWeightStream == null) {
|
|
throw new Error('Model artifacts missing streamWeights function');
|
|
}
|
|
return [4 /*yield*/, decodeWeightsStream(artifacts.getWeightStream(), artifacts.weightSpecs)];
|
|
case 1:
|
|
weightMap = _d.sent();
|
|
return [2 /*return*/, this.loadWithWeightMap(artifacts, weightMap)];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
GraphModel.prototype.loadWithWeightMap = function (artifacts, weightMap) {
|
|
this.artifacts = artifacts;
|
|
var graph = this.artifacts.modelTopology;
|
|
var signature = this.artifacts.signature;
|
|
if (this.artifacts.userDefinedMetadata != null) {
|
|
var metadata = this.artifacts.userDefinedMetadata;
|
|
if (metadata.signature != null) {
|
|
signature = metadata.signature;
|
|
}
|
|
if (metadata.structuredOutputKeys != null) {
|
|
this.structuredOutputKeys = metadata.structuredOutputKeys;
|
|
}
|
|
}
|
|
this.signature = signature;
|
|
this.version = "".concat(graph.versions.producer, ".").concat(graph.versions.minConsumer);
|
|
this.executor = new GraphExecutor(OperationMapper.Instance.transformGraph(graph, this.signature));
|
|
this.executor.weightMap = this.convertTensorMapToTensorsMap(weightMap);
|
|
// Attach a model-level resourceManager to each executor to share resources,
|
|
// such as `HashTable`.
|
|
this.executor.resourceManager = this.resourceManager;
|
|
if (artifacts.modelInitializer != null &&
|
|
artifacts.modelInitializer.node != null) {
|
|
var initializer = OperationMapper.Instance.transformGraph(artifacts.modelInitializer);
|
|
this.initializer = new GraphExecutor(initializer);
|
|
this.initializer.weightMap = this.executor.weightMap;
|
|
// Attach a model-level resourceManager to the initializer, the
|
|
// hashTables created from when executing the initializer will be stored
|
|
// in the resourceManager.
|
|
this.initializer.resourceManager = this.resourceManager;
|
|
this.initializerSignature = artifacts.initializerSignature;
|
|
}
|
|
return true;
|
|
};
|
|
/**
|
|
* Save the configuration and/or weights of the GraphModel.
|
|
*
|
|
* 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 modelUrl =
|
|
* 'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
|
|
* const model = await tf.loadGraphModel(modelUrl);
|
|
* const zeros = tf.zeros([1, 224, 224, 3]);
|
|
* model.predict(zeros).print();
|
|
*
|
|
* const saveResults = await model.save('localstorage://my-model-1');
|
|
*
|
|
* const loadedModel = await tf.loadGraphModel('localstorage://my-model-1');
|
|
* console.log('Prediction from loaded model:');
|
|
* model.predict(zeros).print();
|
|
* ```
|
|
*
|
|
* @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}
|
|
*/
|
|
GraphModel.prototype.save = function (handlerOrURL, config) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var handlers;
|
|
return __generator(this, function (_d) {
|
|
if (typeof handlerOrURL === 'string') {
|
|
handlers = this.io.getSaveHandlers(handlerOrURL);
|
|
if (handlers.length === 0) {
|
|
throw new Error("Cannot find any save handlers for URL '".concat(handlerOrURL, "'"));
|
|
}
|
|
else if (handlers.length > 1) {
|
|
throw new Error("Found more than one (".concat(handlers.length, ") save handlers for ") +
|
|
"URL '".concat(handlerOrURL, "'"));
|
|
}
|
|
handlerOrURL = handlers[0];
|
|
}
|
|
if (handlerOrURL.save == null) {
|
|
throw new Error('GraphModel.save() cannot proceed because the IOHandler ' +
|
|
'provided does not have the `save` attribute defined.');
|
|
}
|
|
return [2 /*return*/, handlerOrURL.save(this.artifacts)];
|
|
});
|
|
});
|
|
};
|
|
GraphModel.prototype.addStructuredOutputNames = function (outputTensors) {
|
|
var _this = this;
|
|
if (this.structuredOutputKeys) {
|
|
var outputTensorsArray = outputTensors instanceof tfc.Tensor ? [outputTensors] : outputTensors;
|
|
var outputTensorMap_1 = {};
|
|
outputTensorsArray.forEach(function (outputTensor, i) { return outputTensorMap_1[_this.structuredOutputKeys[i]] =
|
|
outputTensor; });
|
|
return outputTensorMap_1;
|
|
}
|
|
return outputTensors;
|
|
};
|
|
/**
|
|
* Execute the inference for the input tensors.
|
|
*
|
|
* @param input The input tensors, when there is single input for the model,
|
|
* inputs param should be a `tf.Tensor`. For models with multiple inputs,
|
|
* inputs params should be in either `tf.Tensor`[] if the input order is
|
|
* fixed, or otherwise NamedTensorMap format.
|
|
*
|
|
* For model with multiple inputs, we recommend you use NamedTensorMap as the
|
|
* input type, if you use `tf.Tensor`[], the order of the array needs to
|
|
* follow the
|
|
* order of inputNodes array. @see {@link GraphModel.inputNodes}
|
|
*
|
|
* You can also feed any intermediate nodes using the NamedTensorMap as the
|
|
* input type. For example, given the graph
|
|
* InputNode => Intermediate => OutputNode,
|
|
* you can execute the subgraph Intermediate => OutputNode by calling
|
|
* model.execute('IntermediateNode' : tf.tensor(...));
|
|
*
|
|
* This is useful for models that uses tf.dynamic_rnn, where the intermediate
|
|
* state needs to be fed manually.
|
|
*
|
|
* For batch inference execution, the tensors for each input need to be
|
|
* concatenated together. For example with mobilenet, the required input shape
|
|
* is [1, 244, 244, 3], which represents the [batch, height, width, channel].
|
|
* If we are provide a batched data of 100 images, the input tensor should be
|
|
* in the shape of [100, 244, 244, 3].
|
|
*
|
|
* @param config Prediction configuration for specifying the batch size.
|
|
* Currently the batch size option is ignored for graph model.
|
|
*
|
|
* @returns Inference result tensors. If the model is converted and it
|
|
* originally had structured_outputs in tensorflow, then a NamedTensorMap
|
|
* will be returned matching the structured_outputs. If no structured_outputs
|
|
* are present, the output will be single `tf.Tensor` if the model has single
|
|
* output node, otherwise Tensor[].
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.predict = function (inputs, config) {
|
|
var outputTensors = this.execute(inputs, this.outputNodes);
|
|
return this.addStructuredOutputNames(outputTensors);
|
|
};
|
|
/**
|
|
* Execute the inference for the input tensors in async fashion, use this
|
|
* method when your model contains control flow ops.
|
|
*
|
|
* @param input The input tensors, when there is single input for the model,
|
|
* inputs param should be a `tf.Tensor`. For models with mutliple inputs,
|
|
* inputs params should be in either `tf.Tensor`[] if the input order is
|
|
* fixed, or otherwise NamedTensorMap format.
|
|
*
|
|
* For model with multiple inputs, we recommend you use NamedTensorMap as the
|
|
* input type, if you use `tf.Tensor`[], the order of the array needs to
|
|
* follow the
|
|
* order of inputNodes array. @see {@link GraphModel.inputNodes}
|
|
*
|
|
* You can also feed any intermediate nodes using the NamedTensorMap as the
|
|
* input type. For example, given the graph
|
|
* InputNode => Intermediate => OutputNode,
|
|
* you can execute the subgraph Intermediate => OutputNode by calling
|
|
* model.execute('IntermediateNode' : tf.tensor(...));
|
|
*
|
|
* This is useful for models that uses tf.dynamic_rnn, where the intermediate
|
|
* state needs to be fed manually.
|
|
*
|
|
* For batch inference execution, the tensors for each input need to be
|
|
* concatenated together. For example with mobilenet, the required input shape
|
|
* is [1, 244, 244, 3], which represents the [batch, height, width, channel].
|
|
* If we are provide a batched data of 100 images, the input tensor should be
|
|
* in the shape of [100, 244, 244, 3].
|
|
*
|
|
* @param config Prediction configuration for specifying the batch size.
|
|
* Currently the batch size option is ignored for graph model.
|
|
*
|
|
* @returns A Promise of inference result tensors. If the model is converted
|
|
* and it originally had structured_outputs in tensorflow, then a
|
|
* NamedTensorMap will be returned matching the structured_outputs. If no
|
|
* structured_outputs are present, the output will be single `tf.Tensor` if
|
|
* the model has single output node, otherwise Tensor[].
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.predictAsync = function (inputs, config) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var outputTensors;
|
|
return __generator(this, function (_d) {
|
|
switch (_d.label) {
|
|
case 0: return [4 /*yield*/, this.executeAsync(inputs, this.outputNodes)];
|
|
case 1:
|
|
outputTensors = _d.sent();
|
|
return [2 /*return*/, this.addStructuredOutputNames(outputTensors)];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
GraphModel.prototype.normalizeInputs = function (inputs) {
|
|
var _this = this;
|
|
var _a;
|
|
if (!(inputs instanceof tfc.Tensor) && !Array.isArray(inputs)) {
|
|
// The input is already a NamedTensorMap.
|
|
var signatureInputs = (_a = this.signature) === null || _a === void 0 ? void 0 : _a.inputs;
|
|
if (signatureInputs != null) {
|
|
for (var input in signatureInputs) {
|
|
var tensor = signatureInputs[input];
|
|
if (tensor.resourceId != null) {
|
|
inputs[input] = this.resourceIdToCapturedInput[tensor.resourceId];
|
|
}
|
|
}
|
|
}
|
|
return inputs;
|
|
}
|
|
inputs = Array.isArray(inputs) ? inputs : [inputs];
|
|
var numCapturedInputs = Object.keys(this.resourceIdToCapturedInput).length;
|
|
if (inputs.length + numCapturedInputs !== this.inputNodes.length) {
|
|
throw new Error("Input tensor count mismatch, the graph model has ".concat(this.inputNodes.length -
|
|
numCapturedInputs, " non-resource placeholders, while there are ").concat(inputs.length, " input tensors provided."));
|
|
}
|
|
var inputIndex = 0;
|
|
return this.inputNodes.reduce(function (map, inputName) {
|
|
var _a, _b, _c;
|
|
var resourceId = (_c = (_b = (_a = _this.signature) === null || _a === void 0 ? void 0 : _a.inputs) === null || _b === void 0 ? void 0 : _b[inputName]) === null || _c === void 0 ? void 0 : _c.resourceId;
|
|
if (resourceId != null) {
|
|
map[inputName] = _this.resourceIdToCapturedInput[resourceId];
|
|
}
|
|
else {
|
|
map[inputName] = inputs[inputIndex++];
|
|
}
|
|
return map;
|
|
}, {});
|
|
};
|
|
GraphModel.prototype.normalizeOutputs = function (outputs) {
|
|
outputs = outputs || this.outputNodes;
|
|
return !Array.isArray(outputs) ? [outputs] : outputs;
|
|
};
|
|
GraphModel.prototype.executeInitializerGraph = function () {
|
|
if (this.initializer == null) {
|
|
return [];
|
|
}
|
|
if (this.initializerSignature == null) {
|
|
return this.initializer.execute({}, []);
|
|
}
|
|
else {
|
|
return this.initializer.execute({}, Object.keys(this.initializerSignature.outputs));
|
|
}
|
|
};
|
|
GraphModel.prototype.executeInitializerGraphAsync = function () {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
return __generator(this, function (_d) {
|
|
if (this.initializer == null) {
|
|
return [2 /*return*/, []];
|
|
}
|
|
if (this.initializerSignature == null) {
|
|
return [2 /*return*/, this.initializer.executeAsync({}, [])];
|
|
}
|
|
else {
|
|
return [2 /*return*/, this.initializer.executeAsync({}, Object.keys(this.initializerSignature.outputs))];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
GraphModel.prototype.setResourceIdToCapturedInput = function (outputs) {
|
|
this.resourceIdToCapturedInput = {};
|
|
if (this.initializerSignature) {
|
|
var signatureOutputs = this.initializerSignature.outputs;
|
|
var outputNames = Object.keys(signatureOutputs);
|
|
for (var i = 0; i < outputNames.length; i++) {
|
|
var outputName = outputNames[i];
|
|
var tensorInfo = signatureOutputs[outputName];
|
|
this.resourceIdToCapturedInput[tensorInfo.resourceId] = outputs[i];
|
|
}
|
|
}
|
|
};
|
|
/**
|
|
* Executes inference for the model for given input tensors.
|
|
* @param inputs tensor, tensor array or tensor map of the inputs for the
|
|
* model, keyed by the input node names.
|
|
* @param outputs output node name from the TensorFlow model, if no
|
|
* outputs are specified, the default outputs of the model would be used.
|
|
* You can inspect intermediate nodes of the model by adding them to the
|
|
* outputs array.
|
|
*
|
|
* @returns A single tensor if provided with a single output or no outputs
|
|
* are provided and there is only one default output, otherwise return a
|
|
* tensor array. The order of the tensor array is the same as the outputs
|
|
* if provided, otherwise the order of outputNodes attribute of the model.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.execute = function (inputs, outputs) {
|
|
if (this.resourceIdToCapturedInput == null) {
|
|
this.setResourceIdToCapturedInput(this.executeInitializerGraph());
|
|
}
|
|
inputs = this.normalizeInputs(inputs);
|
|
outputs = this.normalizeOutputs(outputs);
|
|
var result = this.executor.execute(inputs, outputs);
|
|
return result.length > 1 ? result : result[0];
|
|
};
|
|
/**
|
|
* Executes inference for the model for given input tensors in async
|
|
* fashion, use this method when your model contains control flow ops.
|
|
* @param inputs tensor, tensor array or tensor map of the inputs for the
|
|
* model, keyed by the input node names.
|
|
* @param outputs output node name from the TensorFlow model, if no outputs
|
|
* are specified, the default outputs of the model would be used. You can
|
|
* inspect intermediate nodes of the model by adding them to the outputs
|
|
* array.
|
|
*
|
|
* @returns A Promise of single tensor if provided with a single output or
|
|
* no outputs are provided and there is only one default output, otherwise
|
|
* return a tensor map.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.executeAsync = function (inputs, outputs) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var _d, result;
|
|
return __generator(this, function (_e) {
|
|
switch (_e.label) {
|
|
case 0:
|
|
if (!(this.resourceIdToCapturedInput == null)) return [3 /*break*/, 2];
|
|
_d = this.setResourceIdToCapturedInput;
|
|
return [4 /*yield*/, this.executeInitializerGraphAsync()];
|
|
case 1:
|
|
_d.apply(this, [_e.sent()]);
|
|
_e.label = 2;
|
|
case 2:
|
|
inputs = this.normalizeInputs(inputs);
|
|
outputs = this.normalizeOutputs(outputs);
|
|
return [4 /*yield*/, this.executor.executeAsync(inputs, outputs)];
|
|
case 3:
|
|
result = _e.sent();
|
|
return [2 /*return*/, result.length > 1 ? result : result[0]];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* Get intermediate tensors for model debugging mode (flag
|
|
* KEEP_INTERMEDIATE_TENSORS is true).
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.getIntermediateTensors = function () {
|
|
return this.executor.getIntermediateTensors();
|
|
};
|
|
/**
|
|
* Dispose intermediate tensors for model debugging mode (flag
|
|
* KEEP_INTERMEDIATE_TENSORS is true).
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.disposeIntermediateTensors = function () {
|
|
this.executor.disposeIntermediateTensors();
|
|
};
|
|
GraphModel.prototype.convertTensorMapToTensorsMap = function (map) {
|
|
return Object.keys(map).reduce(function (newMap, key) {
|
|
newMap[key] = [map[key]];
|
|
return newMap;
|
|
}, {});
|
|
};
|
|
/**
|
|
* Releases the memory used by the weight tensors and resourceManager.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Classes'}
|
|
*/
|
|
GraphModel.prototype.dispose = function () {
|
|
this.executor.dispose();
|
|
if (this.initializer) {
|
|
this.initializer.dispose();
|
|
if (this.resourceIdToCapturedInput) {
|
|
tfc.dispose(this.resourceIdToCapturedInput);
|
|
}
|
|
}
|
|
this.resourceManager.dispose();
|
|
};
|
|
return GraphModel;
|
|
}());
|
|
/**
|
|
* Load a graph model given a URL to the model definition.
|
|
*
|
|
* Example of loading MobileNetV2 from a URL and making a prediction with a
|
|
* zeros input:
|
|
*
|
|
* ```js
|
|
* const modelUrl =
|
|
* 'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
|
|
* const model = await tf.loadGraphModel(modelUrl);
|
|
* const zeros = tf.zeros([1, 224, 224, 3]);
|
|
* model.predict(zeros).print();
|
|
* ```
|
|
*
|
|
* Example of loading MobileNetV2 from a TF Hub URL and making a prediction
|
|
* with a zeros input:
|
|
*
|
|
* ```js
|
|
* const modelUrl =
|
|
* 'https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/2';
|
|
* const model = await tf.loadGraphModel(modelUrl, {fromTFHub: true});
|
|
* const zeros = tf.zeros([1, 224, 224, 3]);
|
|
* model.predict(zeros).print();
|
|
* ```
|
|
* @param modelUrl The url or an `io.IOHandler` that loads the model.
|
|
* @param options Options for the HTTP request, which allows to send
|
|
* credentials
|
|
* and custom headers.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Loading'}
|
|
*/
|
|
function loadGraphModel(modelUrl, options, tfio) {
|
|
if (options === void 0) { options = {}; }
|
|
if (tfio === void 0) { tfio = tfc.io; }
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var model;
|
|
return __generator(this, function (_d) {
|
|
switch (_d.label) {
|
|
case 0:
|
|
if (modelUrl == null) {
|
|
throw new Error('modelUrl in loadGraphModel() cannot be null. Please provide a url ' +
|
|
'or an IOHandler that loads the model');
|
|
}
|
|
if (options == null) {
|
|
options = {};
|
|
}
|
|
if (options.fromTFHub && typeof modelUrl === 'string') {
|
|
modelUrl = getTFHubUrl(modelUrl);
|
|
}
|
|
model = new GraphModel(modelUrl, options, tfio);
|
|
return [4 /*yield*/, model.load()];
|
|
case 1:
|
|
_d.sent();
|
|
return [2 /*return*/, model];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Load a graph model given a synchronous IO handler with a 'load' method.
|
|
*
|
|
* @param modelSource The `io.IOHandlerSync` that loads the model, or the
|
|
* `io.ModelArtifacts` that encode the model, or a tuple of
|
|
* `[io.ModelJSON, ArrayBuffer]` of which the first element encodes the
|
|
* model and the second contains the weights.
|
|
*
|
|
* @doc {heading: 'Models', subheading: 'Loading'}
|
|
*/
|
|
function loadGraphModelSync(modelSource) {
|
|
if (modelSource == null) {
|
|
throw new Error('modelUrl in loadGraphModelSync() cannot be null. Please provide ' +
|
|
'model artifacts or an IOHandler that loads the model');
|
|
}
|
|
var ioHandler;
|
|
if (modelSource instanceof Array) {
|
|
var _d = __read(modelSource, 2), modelJSON = _d[0], weights = _d[1];
|
|
if (!modelJSON) {
|
|
throw new Error('modelJSON must be the first element of the array');
|
|
}
|
|
if (!weights || !(weights instanceof ArrayBuffer)) {
|
|
throw new Error('An ArrayBuffer of weights must be the second element of' +
|
|
' the array');
|
|
}
|
|
if (!('modelTopology' in modelJSON)) {
|
|
throw new Error('Model JSON is missing \'modelTopology\'');
|
|
}
|
|
if (!('weightsManifest' in modelJSON)) {
|
|
throw new Error('Model JSON is missing \'weightsManifest\'');
|
|
}
|
|
var weightSpecs = tfc.io.getWeightSpecs(modelJSON.weightsManifest);
|
|
var modelArtifacts = tfc.io.getModelArtifactsForJSONSync(modelJSON, weightSpecs, weights);
|
|
ioHandler = tfc.io.fromMemorySync(modelArtifacts);
|
|
}
|
|
else if ('load' in modelSource) {
|
|
// Then modelSource is already an IOHandlerSync.
|
|
ioHandler = modelSource;
|
|
}
|
|
else if ('modelTopology' in modelSource && 'weightSpecs' in modelSource &&
|
|
'weightData' in modelSource) {
|
|
// modelSource is of type ModelArtifacts.
|
|
ioHandler = tfc.io.fromMemorySync(modelSource);
|
|
}
|
|
else {
|
|
throw new Error('Unknown model format');
|
|
}
|
|
var model = new GraphModel(ioHandler);
|
|
model.load();
|
|
return model;
|
|
}
|
|
function getTFHubUrl(modelUrl) {
|
|
if (!modelUrl.endsWith('/')) {
|
|
modelUrl = (modelUrl) + '/';
|
|
}
|
|
return "".concat(modelUrl).concat(DEFAULT_MODEL_NAME).concat(TFHUB_SEARCH_PARAM);
|
|
}
|
|
|
|
/** @license See the LICENSE file. */
|
|
// This code is auto-generated, do not modify this file!
|
|
var version = '4.21.0';
|
|
|
|
exports.GraphModel = GraphModel;
|
|
exports.deregisterOp = deregisterOp;
|
|
exports.loadGraphModel = loadGraphModel;
|
|
exports.loadGraphModelSync = loadGraphModelSync;
|
|
exports.registerOp = registerOp;
|
|
exports.version_converter = version;
|
|
//# sourceMappingURL=tf-converter.node.js.map
|