@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
1,135 lines • 271 kB
JavaScript
"use strict";
/**
* @license
* Copyright 2017 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.
* =============================================================================
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (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 };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tf = require("../index");
var jasmine_util_1 = require("../jasmine_util");
var test_util_1 = require("../test_util");
var util = require("../util");
var selu_util = require("./selu_util");
jasmine_util_1.describeWithFlags('relu', jasmine_util_1.ALL_ENVS, function () {
it('basic', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, 0, 3, -0.1]);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 3, 0]]);
return [2 /*return*/];
}
});
}); });
it('basic relu6', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, 0, 8, -0.1]);
result = tf.relu6(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 6, 0]]);
return [2 /*return*/];
}
});
}); });
it('5D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor5d([1, -2, 5, -3], [1, 2, 2, 1, 1]);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 5, 0]]);
return [2 /*return*/];
}
});
}); });
it('6D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor6d([1, -2, 5, -3, -1, 4, 7, 8], [1, 2, 2, 2, 1, 1]);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 5, 0, 0, 4, 7, 8]]);
return [2 /*return*/];
}
});
}); });
it('does nothing to positive values', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(1);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1]]);
return [2 /*return*/];
}
});
}); });
it('sets negative values to 0', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(-1);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('preserves zero values', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(0);
result = tf.relu(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs, float32', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, 0, 3, -0.1, NaN]);
result = tf.relu(a);
expect(result.dtype).toBe('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 3, 0, NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradients: positive scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(3);
dy = tf.scalar(5);
grad = tf.grad(function (a) { return tf.relu(a); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [5]]);
return [2 /*return*/];
}
});
}); });
it('gradients: relu6', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(8);
dy = tf.scalar(5);
grad = tf.grad(function (a) { return tf.relu6(a); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(3);
dy = tf.scalar(5);
grad = tf.grad(function (a) { return tf.relu(a.clone()).clone(); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [5]]);
return [2 /*return*/];
}
});
}); });
it('gradients: negative scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(-3);
dy = tf.scalar(5);
grad = tf.grad(function (a) { return tf.relu(a); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('gradients: array', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, -1, 0, .1], [2, 2]);
dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
grad = tf.grad(function (a) { return tf.relu(a); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 4]]);
return [2 /*return*/];
}
});
}); });
it('gradients: relu6 array', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, grad, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([8, -1, 0, .1], [2, 2]);
dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
grad = tf.grad(function (a) { return tf.relu6(a); });
da = grad(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0, 0, 0, 4]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a non-tensor', function () {
expect(function () { return tf.relu({}); })
.toThrowError(/Argument 'x' passed to 'relu' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = tf.relu([1, -2, 0, 3, -0.1]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 3, 0]]);
return [2 /*return*/];
}
});
}); });
it('throws for string tensor', function () {
expect(function () { return tf.relu('q'); })
.toThrowError(/Argument 'x' passed to 'relu' must be numeric/);
});
});
jasmine_util_1.describeWithFlags('abs', jasmine_util_1.ALL_ENVS, function () {
it('basic', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, 0, 3, -0.1]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 2, 0, 3, 0.1]]);
return [2 /*return*/];
}
});
}); });
it('5D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor5d([1, -2, 0, -3], [1, 2, 2, 1, 1]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 2, 0, 3]]);
return [2 /*return*/];
}
});
}); });
it('6D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor6d([1, -2, 5, -3, -1, 4, 7, 8], [1, 2, 2, 2, 1, 1]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 2, 5, 3, 1, 4, 7, 8]]);
return [2 /*return*/];
}
});
}); });
it('complex64 rank-1', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.complex([-2, -1, 0, 1, 2], [1, 2, 3, 0, -1]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [
Math.sqrt(-2 * -2 + 1 * 1), Math.sqrt(-1 * -1 + 2 * 2),
Math.sqrt(0 * 0 + 3 * 3), Math.sqrt(1 * 1 + 0 * 0),
Math.sqrt(2 * 2 + -1 * -1)
]]);
expect(result.shape).toEqual([5]);
return [2 /*return*/];
}
});
}); });
it('complex64 rank-2', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.complex([[-3, -2, -1], [0, 1, 2]], [[4, 1, 2], [3, 0, -1]]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [
Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1),
Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3),
Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1)
]]);
expect(result.shape).toEqual([2, 3]);
return [2 /*return*/];
}
});
}); });
it('complex64 rank-3', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.complex([[[-3, -2], [-1, 0]], [[1, 2], [3, 4]]], [[[4, 1], [2, 3]], [[0, -1], [-3, -4]]]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [
Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1),
Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3),
Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1),
Math.sqrt(3 * 3 + -3 * -3), Math.sqrt(4 * 4 + -4 * -4)
]]);
expect(result.shape).toEqual([2, 2, 2]);
return [2 /*return*/];
}
});
}); });
it('is underflow-safe for complex64', function () { return __awaiter(_this, void 0, void 0, function () {
var floatBits, small, a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
floatBits = tf.backend().floatPrecision();
switch (floatBits) {
case 32:
small = 1e-30;
break;
case 16:
small = 1e-4;
break;
default:
throw new Error("Test not implemented for ENV.engine.floatPrecision()=" + floatBits + ".");
}
a = tf.complex([small, 0, small, 0], [small, small, 0, 0]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(),
[
Math.hypot(small, small), Math.hypot(0, small), Math.hypot(small, 0),
Math.hypot(0, 0)
],
/*tolerance=*/ small / 100]);
expect(result.shape).toEqual([4]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, 0, 3, -0.1, NaN]);
result = tf.abs(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 2, 0, 3, 0.1, NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(4);
dy = tf.scalar(8);
da = tf.grad(function (a) { return tf.abs(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [8 * 1]]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () {
var a = tf.scalar(4);
var dy = tf.scalar(8);
var da = tf.grad(function (a) { return a.clone().abs().clone(); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
});
it('gradients: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
dy = tf.tensor1d([1, 2, 3, 4]);
da = tf.grad(function (a) { return tf.abs(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 * 1, 2 * 1, 3 * -1, 4 * 1]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([3, -1, -2, 3], [2, 2]);
dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
da = tf.grad(function (a) { return tf.abs(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 * 1, 2 * -1, 3 * -1, 4 * 1]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a non-tensor', function () {
expect(function () { return tf.abs({}); })
.toThrowError(/Argument 'x' passed to 'abs' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = tf.abs([1, -2, 0, 3, -0.1]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 2, 0, 3, 0.1]]);
return [2 /*return*/];
}
});
}); });
it('throws for string tensor', function () {
expect(function () { return tf.abs('q'); })
.toThrowError(/Argument 'x' passed to 'abs' must be numeric/);
});
});
jasmine_util_1.describeWithFlags('step', jasmine_util_1.ALL_ENVS, function () {
it('with 1d tensor', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, -.01, 3, -0.1]);
result = tf.step(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 1, 0]]);
return [2 /*return*/];
}
});
}); });
it('with 1d tensor and alpha', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, -.01, 3, NaN]);
result = tf.step(a, 0.1);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0.1, 0.1, 1, NaN]]);
return [2 /*return*/];
}
});
}); });
it('with 2d tensor', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, -5, -3, 4], [2, 2]);
result = tf.step(a);
expect(result.shape).toEqual([2, 2]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 1]]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -2, -.01, 3, NaN]);
result = tf.step(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 1, NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, gradients, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(-4);
dy = tf.scalar(8);
gradients = tf.grad(function (a) { return tf.step(a); })(a, dy);
expect(gradients.shape).toEqual(a.shape);
expect(gradients.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, gradients.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, gradients, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(-4);
dy = tf.scalar(8);
gradients = tf.grad(function (a) { return tf.step(a.clone()).clone(); })(a, dy);
expect(gradients.shape).toEqual(a.shape);
expect(gradients.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, gradients.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, gradients, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
dy = tf.tensor1d([1, 2, 3, 4]);
gradients = tf.grad(function (a) { return tf.step(a); })(a, dy);
expect(gradients.shape).toEqual(a.shape);
expect(gradients.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, gradients.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0, 0, 0, 0]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, gradients, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([3, -1, -2, 3], [2, 2]);
dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
gradients = tf.grad(function (a) { return tf.step(a); })(a, dy);
expect(gradients.shape).toEqual(a.shape);
expect(gradients.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, gradients.data()];
case 1:
_a.apply(void 0, [_b.sent(), [0, 0, 0, 0]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a non-tensor', function () {
expect(function () { return tf.step({}); })
.toThrowError(/Argument 'x' passed to 'step' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = tf.step([1, -2, -.01, 3, -0.1]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 0, 0, 1, 0]]);
return [2 /*return*/];
}
});
}); });
it('throws for string tensor', function () {
expect(function () { return tf.step('q'); })
.toThrowError(/Argument 'x' passed to 'step' must be numeric/);
});
});
jasmine_util_1.describeWithFlags('neg', jasmine_util_1.ALL_ENVS, function () {
it('basic', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -3, 2, 7, -4]);
result = tf.neg(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [-1, 3, -2, -7, 4]]);
return [2 /*return*/];
}
});
}); });
it('propagate NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, -3, 2, 7, NaN]);
result = tf.neg(a);
expected = [-1, 3, -2, -7, NaN];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('gradients: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(4);
dy = tf.scalar(8);
da = tf.grad(function (a) { return tf.neg(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [8 * -1]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(4);
dy = tf.scalar(8);
da = tf.grad(function (a) { return tf.neg(a.clone()).clone(); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [8 * -1]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
dy = tf.tensor1d([1, 2, 3, 4]);
da = tf.grad(function (a) { return tf.neg(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 * -1, 2 * -1, 3 * -1, 4 * -1]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([3, -1, -2, 3], [2, 2]);
dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
da = tf.grad(function (a) { return tf.neg(a); })(a, dy);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 * -1, 2 * -1, 3 * -1, 4 * -1]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a non-tensor', function () {
expect(function () { return tf.neg({}); })
.toThrowError(/Argument 'x' passed to 'neg' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = tf.neg([1, -3, 2, 7, -4]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [-1, 3, -2, -7, 4]]);
return [2 /*return*/];
}
});
}); });
it('throws for string tensor', function () {
expect(function () { return tf.neg('q'); })
.toThrowError(/Argument 'x' passed to 'neg' must be numeric/);
});
});
jasmine_util_1.describeWithFlags('sigmoid', jasmine_util_1.ALL_ENVS, function () {
it('basic', function () { return __awaiter(_this, void 0, void 0, function () {
var values, a, result, expected, i, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [1, -3, 2, 7, -4];
a = tf.tensor1d(values);
result = tf.sigmoid(a);
expected = [];
for (i = 0; i < a.size; i++) {
expected[i] = 1 / (1 + Math.exp(-values[i]));
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('6D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, expected, i, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.ones([2, 2, 2, 2, 2, 2]);
result = tf.sigmoid(a);
expected = [];
for (i = 0; i < a.size; i++) {
expected[i] = 1 / (1 + Math.exp(-1.0));
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, res, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([3, NaN]);
res = tf.sigmoid(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, res.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 / (1 + Math.exp(-3)), NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, aVals, dyVals, expected, i, y, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
dy = tf.tensor1d([1, 2, 3, 4]);
da = tf.grad(function (a) { return tf.sigmoid(a); })(a, dy);
return [4 /*yield*/, a.array()];
case 1:
aVals = _b.sent();
return [4 /*yield*/, dy.array()];
case 2:
dyVals = _b.sent();
expected = [];
for (i = 0; i < a.size; i++) {
y = 1 / (1 + Math.exp(-aVals[i]));
expected[i] = dyVals[i] * y * (1 - y);
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 3:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, da, aVals, dyVals, expected, i, y, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
dy = tf.tensor1d([1, 2, 3, 4]);
da = tf.grad(function (a) { return tf.sigmoid(a.clone()).clone(); })(a, dy);
return [4 /*yield*/, a.array()];
case 1:
aVals = _b.sent();
return [4 /*yield*/, dy.array()];
case 2:
dyVals = _b.sent();
expected = [];
for (i = 0; i < a.size; i++) {
y = 1 / (1 + Math.exp(-aVals[i]));
expected[i] = dyVals[i] * y * (1 - y);
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 3:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a non-tensor', function () {
expect(function () { return tf.sigmoid({}); })
.toThrowError(/Argument 'x' passed to 'sigmoid' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var values, result, expected, i, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [1, -3, 2, 7, -4];
result = tf.sigmoid(values);
expected = [];
for (i = 0; i < values.length; i++) {
expected[i] = 1 / (1 + Math.exp(-values[i]));
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('throws for string tensor', function () {
expect(function () { return tf.sigmoid('q'); })
.toThrowError(/Argument 'x' passed to 'sigmoid' must be numeric/);
});
});
jasmine_util_1.describeWithFlags('logSigmoid', jasmine_util_1.ALL_ENVS, function () {
it('basic', function () { return __awaiter(_this, void 0, void 0, function () {
var values, a, result, expected, i, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [1, -3, 2, 7, -4];
a = tf.tensor1d(values);
result = tf.logSigmoid(a);
expected = [];
for (i = 0; i < a.size; i++) {
expected[i] = Math.log(1 / (1 + Math.exp(-values[i])));
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(-2);
result = tf.logSigmoid(a);
expected = [Math.log(1 / (1 + Math.exp(2)))];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var values, a, result, expected, i, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [1, 2, -3, 5];
a = tf.tensor2d(values, [2, 2]);
result = tf.logSigmoid(a);
expected = [];
for (i = 0; i < a.size; i++) {
expected[i] = Math.log(1 / (1 + Math.exp(-values[i])));
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('larger magnitude negative inputs', function () { return __awaiter(_this, void 0, void 0, function () {
var values, a, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [-100, -200, -3000];
a = tf.tensor1d(values);
result = tf.logSigmoid(a);
expected = [-100, -200, -3000];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('larger magnitude positive inputs', function () { return __awaiter(_this, void 0, void 0, function () {
var values, a, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
values = [100, 200, 3000, 50000];
a = tf.tensor1d(values);
result = tf.logSigmoid(a);
expected = [0, 0, 0, 0];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, res, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([3, NaN]);
res = tf.logSigmoid(a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, res.data()];
case 1:
_a.apply(void 0, [_b.sent(), [Math.log(1 / (1 + Math.exp(-3))), NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, dy, dyVal, da, aVal, y, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.scalar(3);
dy = tf.scalar(4);
return [4 /*yield*/, dy.array()];
case 1:
dyVal = _b.sent();
da = tf.grad(function (a) { return tf.logSigmoid(a); })(a, dy);
return [4 /*yield*/, a.array()];
case 2:
aVal = _b.sent();
y = 1 / (1 + Math.exp(aVal));
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 3:
_a.apply(void 0, [_b.sent(), [dyVal * y]]);
return [2 /*return*/];
}
});
}); });
it('gradients: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, aVals, dy, dyVals, da, expected, i, y, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([1, 2, -3, 5]);
return [4 /*yield*/, a.array()];
case 1:
aVals = _b.sent();
dy = tf.tensor1d([1, 2, 3, 4]);
return [4 /*yield*/, dy.array()];
case 2:
dyVals = _b.sent();
da = tf.grad(function (a) { return tf.logSigmoid(a); })(a, dy);
expected = [];
for (i = 0; i < a.size; i++) {
y = 1 / (1 + Math.exp(aVals[i]));
expected[i] = dyVals[i] * y;
}
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 3:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
va