@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
1,049 lines • 127 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");
jasmine_util_1.describeWithFlags('div', jasmine_util_1.ALL_ENVS, function () {
it('same shape', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, 3, 4, 5, 6], [2, 3]);
c = tf.tensor2d([1, 2, 3, 4, 2, 5], [2, 3]);
r = tf.div(a, c);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1, 1, 1, 1, 2.5, 6 / 5]]);
return [2 /*return*/];
}
});
}); });
it('TensorLike', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = [0, 1, -2, -4, 4, -4];
b = [0.15, 0.2, 0.25, 0.5, 0.7, 1.2];
result = tf.div(a, b);
expect(result.shape).toEqual([6]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(),
[0, 5.0, -8.0, -8.0, 5.714285850524902, -3.3333332538604736]]);
return [2 /*return*/];
}
});
}); });
it('TensorLike chained', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([0, 1, -2, -4, 4, -4]);
b = [0.15, 0.2, 0.25, 0.5, 0.7, 1.2];
result = a.div(b);
expect(result.shape).toEqual(a.shape);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(),
[0, 5.0, -8.0, -8.0, 5.714285850524902, -3.3333332538604736]]);
return [2 /*return*/];
}
});
}); });
it('division by zero results in infinity', function () { return __awaiter(_this, void 0, void 0, function () {
var r, rData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
r = tf.div(1, 0);
return [4 /*yield*/, r.data()];
case 1:
rData = _a.sent();
expect(Array.from(rData)).toEqual([Infinity]);
return [2 /*return*/];
}
});
}); });
it('integer division implements floor divide', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([-6, -6, -5, -4, -3, -3, 3, 3, 2], 'int32');
c = tf.tensor1d([-2, 2, 3, 2, -3, 3, 2, 3, 2], 'int32');
r = tf.div(a, c);
expect(r.dtype).toEqual('int32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [3, -3, -2, -2, 1, -1, 1, 1, 1]]);
return [2 /*return*/];
}
});
}); });
it('integer division broadcasts', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor1d([-5, -4, 3, 2], 'int32');
c = tf.scalar(2, 'int32');
r = tf.div(a, c);
expect(r.dtype).toEqual('int32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [-3, -2, 1, 1]]);
return [2 /*return*/];
}
});
}); });
it('propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2], [2, 1]);
c = tf.tensor2d([3, NaN], [2, 1]);
r = tf.div(a, c);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 / 3, NaN]]);
return [2 /*return*/];
}
});
}); });
it('broadcasting same rank Tensors different shape', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor2d([2, 3], [2, 1]);
result = tf.div(a, b);
expect(result.shape).toEqual([2, 2]);
expected = [1 / 2, 1, -1, -4 / 3];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcast scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, 3, 4], [2, 2]);
b = [2];
result = tf.div(a, b);
expect(result.shape).toEqual([2, 2]);
expected = [0.5, 1, 1.5, 2];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcast 2D + 1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor1d([1, 2]);
result = tf.div(a, b);
expect(result.shape).toEqual([2, 2]);
expected = [1, 1, -3, -2];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('upcasts when dtypes dont match', function () { return __awaiter(_this, void 0, void 0, function () {
var res, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
res = tf.div(tf.scalar(6, 'int32'), tf.scalar(3, 'float32'));
expect(res.dtype).toBe('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, res.data()];
case 1:
_a.apply(void 0, [_c.sent(), [2]]);
res = tf.div(tf.scalar(6, 'int32'), tf.scalar(true, 'bool'));
expect(res.dtype).toBe('int32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, res.data()];
case 2:
_b.apply(void 0, [_c.sent(), [6]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed tensors of different shapes', function () {
var a = tf.tensor2d([1, 2, -3, -4, 5, 6], [2, 3]);
var b = tf.tensor2d([5, 3, 4, -7], [2, 2]);
expect(function () { return tf.div(a, b); }).toThrowError();
expect(function () { return tf.div(b, a); }).toThrowError();
});
it('scalar divided by array', function () { return __awaiter(_this, void 0, void 0, function () {
var c, a, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
c = tf.scalar(2);
a = tf.tensor2d([1, 2, 3, 4, 5, 6], [2, 3]);
r = tf.div(c, a);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [2 / 1, 2 / 2, 2 / 3, 2 / 4, 2 / 5, 2 / 6]]);
return [2 /*return*/];
}
});
}); });
it('scalar divided by array propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var c, a, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
c = tf.scalar(NaN);
a = tf.tensor2d([1, 2, 3], [1, 3]);
r = tf.div(c, a);
_a = test_util_1.expectArraysEqual;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [NaN, NaN, NaN]]);
return [2 /*return*/];
}
});
}); });
it('array divided by scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, 3, 4, 5, 6], [2, 3]);
c = tf.scalar(2);
r = tf.div(a, c);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 / 2, 2 / 2, 3 / 2, 4 / 2, 5 / 2, 6 / 2]]);
return [2 /*return*/];
}
});
}); });
it('array divided by scalar propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, c, r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, NaN], [1, 3]);
c = tf.scalar(2);
r = tf.div(a, c);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 / 2, 2 / 2, NaN]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, before, grads, _a, da, db, now, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.scalar(5);
b = tf.scalar(2);
dy = tf.scalar(4);
before = tf.memory().numTensors;
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
now = tf.memory().numTensors;
expect(now).toBe(before + 2);
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [4 / 2]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-4 * 5 / (2 * 2)]]);
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
var grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
grads = tf.grads(function (a, b) { return tf.div(a.clone(), b.clone()).clone(); });
_a = grads([5, 2]), da = _a[0], db = _a[1];
expect(da.shape).toEqual([]);
expect(db.shape).toEqual([]);
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1 / 2]]);
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-5 / 4]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor1d([1, 2, 3]);
b = tf.tensor1d([3, 4, 5]);
dy = tf.tensor1d([1, 10, 20]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(db.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1 / 3, 10 / 4, 20 / 5]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-1 * 1 / 9, -10 * 2 / 16, -20 * 3 / 25]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor1D with int32', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor1d([1, 2, 3], 'int32');
b = tf.tensor1d([3, 4, 5], 'int32');
dy = tf.tensor1d([1, 10, 20]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(db.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1 / 3, 10 / 4, 20 / 5]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-1 * 1 / 9, -10 * 2 / 16, -20 * 3 / 25]]);
return [2 /*return*/];
}
});
}); });
it('gradient: 1d<int32> with 1d<bool> ', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor1d([true, false, true], 'bool');
b = tf.tensor1d([1, 2, 3], 'int32');
dy = tf.tensor1d([1, 19, 20]);
grads = tf.grads(function (a, b) { return tf.div(a.toInt(), b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(db.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1, 19 / 2, 20 / 3]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-1 / 1, 0, -20 / 9]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([3, 1, 2, 3], [2, 2]);
b = tf.tensor2d([1, 3, 4, 5], [2, 2]);
dy = tf.tensor2d([1, 10, 15, 20], [2, 2]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1 / 1, 10 / 3, 15 / 4, 20 / 5]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-1 * 3 / 1, -10 * 1 / 9, -15 * 2 / 16, -20 * 3 / 25]]);
return [2 /*return*/];
}
});
}); });
it('gradient: scalar / Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.scalar(2);
b = tf.tensor1d([3, 4, 5]);
dy = tf.tensor1d([6, 7, 8]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [6 / 3 + 7 / 4 + 8 / 5]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-6 * 2 / 9, -7 * 2 / 16, -8 * 2 / 25]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D / scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([[2, 3], [4, 5]], [2, 2]);
b = tf.scalar(2);
dy = tf.tensor2d([[6, 7], [8, 9]], [2, 2]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [6 / 2, 7 / 2, 8 / 2, 9 / 2]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-6 * 2 / 4 + -7 * 3 / 4 + -8 * 4 / 4 + -9 * 5 / 4]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D / Tensor2D w/ broadcast', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([3, 4], [2, 1]);
b = tf.tensor2d([[2, 3], [4, 5]], [2, 2]);
dy = tf.tensor2d([[6, 7], [8, 9]], [2, 2]);
grads = tf.grads(function (a, b) { return tf.div(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [6 / 2 + 7 / 3, 8 / 4 + 9 / 5]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [-6 * 3 / 4, -7 * 3 / 9, -8 * 4 / 16, -9 * 4 / 25]]);
return [2 /*return*/];
}
});
}); });
it('throws when passed a as a non-tensor', function () {
expect(function () { return tf.div({}, tf.scalar(1)); })
.toThrowError(/Argument 'a' passed to 'div' must be a Tensor/);
});
it('throws when passed b as a non-tensor', function () {
expect(function () { return tf.div(tf.scalar(1), {}); })
.toThrowError(/Argument 'b' passed to 'div' must be a Tensor/);
});
it('accepts a tensor-like object', function () { return __awaiter(_this, void 0, void 0, function () {
var r, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
r = tf.div([[1, 2, 3], [4, 5, 6]], 2);
expect(r.shape).toEqual([2, 3]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, r.data()];
case 1:
_a.apply(void 0, [_b.sent(), [1 / 2, 2 / 2, 3 / 2, 4 / 2, 5 / 2, 6 / 2]]);
return [2 /*return*/];
}
});
}); });
});
jasmine_util_1.describeWithFlags('mul', jasmine_util_1.ALL_ENVS, function () {
it('strict same-shaped tensors', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, expected, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor2d([5, 3, 4, -7], [2, 2]);
expected = [5, 6, -12, 28];
result = tf.mulStrict(a, b);
expect(result.shape).toEqual([2, 2]);
expect(result.dtype).toBe('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('strict propagates NaNs', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 3, 4, 0], [2, 2]);
b = tf.tensor2d([NaN, 3, NaN, 3], [2, 2]);
result = tf.mulStrict(a, b);
expect(result.dtype).toBe('float32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [NaN, 9, NaN, 0]]);
return [2 /*return*/];
}
});
}); });
it('strict throws when passed tensors of different shapes', function () {
var a = tf.tensor2d([1, 2, -3, -4, 5, 6], [2, 3]);
var b = tf.tensor2d([5, 3, 4, -7], [2, 2]);
expect(function () { return tf.mulStrict(a, b); }).toThrowError();
expect(function () { return tf.mulStrict(b, a); }).toThrowError();
});
it('strict throws when dtypes do not match', function () {
var a = tf.tensor2d([1, 2, -3, -4, 5, 6], [2, 3], 'float32');
var b = tf.tensor2d([5, 3, 4, -7], [2, 2], 'int32');
expect(function () { return tf.mulStrict(a, b); }).toThrowError();
expect(function () { return tf.mulStrict(b, a); }).toThrowError();
});
it('strict int32 * int32', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, res, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2], 'int32');
b = tf.tensor2d([2, 1, 3, -4], [2, 2], 'int32');
res = tf.mulStrict(a, b);
expect(res.dtype).toBe('int32');
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, res.data()];
case 1:
_a.apply(void 0, [_b.sent(), [2, 2, -9, 16]]);
return [2 /*return*/];
}
});
}); });
it('same-shaped tensors', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, expected, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor2d([5, 3, 4, -7], [2, 2]);
expected = [5, 6, -12, 28];
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('TensorLike', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, expected, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = [[1, 2], [-3, -4]];
b = [[5, 3], [4, -7]];
expected = [5, 6, -12, 28];
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('TensorLike chained', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, expected, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = [[5, 3], [4, -7]];
expected = [5, 6, -12, 28];
result = a.mul(b);
expect(result.shape).toEqual([2, 2]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcasting tensors', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, expected, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.scalar(2);
expected = [2, 4, -6, -8];
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcasting same rank Tensors different shape', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor2d([2, 3], [2, 1]);
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2]);
expected = [2, 4, -9, -12];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcast 2D + 1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.tensor2d([1, 2, -3, -4], [2, 2]);
b = tf.tensor1d([1, 2]);
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2]);
expected = [1, 4, -3, -8];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcast 5D + 2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.range(1, 33).reshape([2, 2, 2, 2, 2]);
b = tf.tensor([2, 3], [2, 1]);
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2, 2, 2, 2]);
expected = [
2, 4, 9, 12, 10, 12, 21, 24, 18, 20, 33, 36, 26, 28, 45, 48,
34, 36, 57, 60, 42, 44, 69, 72, 50, 52, 81, 84, 58, 60, 93, 96
];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('broadcast 6D + 2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, result, expected, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
a = tf.range(1, 65).reshape([2, 2, 2, 2, 2, 2]);
b = tf.tensor([2, 3], [2, 1]);
result = tf.mul(a, b);
expect(result.shape).toEqual([2, 2, 2, 2, 2, 2]);
expected = [
2, 4, 9, 12, 10, 12, 21, 24, 18, 20, 33, 36, 26,
28, 45, 48, 34, 36, 57, 60, 42, 44, 69, 72, 50, 52,
81, 84, 58, 60, 93, 96, 66, 68, 105, 108, 74, 76, 117,
120, 82, 84, 129, 132, 90, 92, 141, 144, 98, 100, 153, 156,
106, 108, 165, 168, 114, 116, 177, 180, 122, 124, 189, 192
];
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), expected]);
return [2 /*return*/];
}
});
}); });
it('gradient: Scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c, _d, _e;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
a = tf.scalar(5);
b = tf.scalar(2);
dy = tf.scalar(4);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_c = [_f.sent()];
return [4 /*yield*/, b.mul(dy).data()];
case 2:
_b.apply(void 0, _c.concat([_f.sent()]));
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_d = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 3:
_e = [_f.sent()];
return [4 /*yield*/, a.mul(dy).data()];
case 4:
_d.apply(void 0, _e.concat([_f.sent()]));
return [2 /*return*/];
}
});
}); });
it('gradient with clones', function () { return __awaiter(_this, void 0, void 0, function () {
var grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
grads = tf.grads(function (a, b) { return tf.mul(a.clone(), b.clone()).clone(); });
_a = grads([4, 2]), da = _a[0], db = _a[1];
expect(da.shape).toEqual([]);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), 2]);
expect(db.shape).toEqual([]);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), 4]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor1d([1, 2, 3]);
b = tf.tensor1d([3, 4, 5]);
dy = tf.tensor1d([1, 10, 20]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [3 * 1, 4 * 10, 5 * 20]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [1 * 1, 2 * 10, 3 * 20]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor1D with dtype int32', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor1d([1, 2, 3], 'int32');
b = tf.tensor1d([3, 4, 5], 'int32');
dy = tf.tensor1d([1, 10, 20]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(db.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [3 * 1, 4 * 10, 5 * 20]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [1 * 1, 2 * 10, 3 * 20]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([3, 1, 2, 3], [2, 2]);
b = tf.tensor2d([1, 3, 4, 5], [2, 2]);
dy = tf.tensor2d([1, 10, 15, 20], [2, 2]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [1 * 1, 3 * 10, 4 * 15, 5 * 20]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [3 * 1, 1 * 10, 2 * 15, 3 * 20]]);
return [2 /*return*/];
}
});
}); });
it('gradient: scalar * Tensor1D', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.scalar(2);
b = tf.tensor1d([3, 4, 5]);
dy = tf.tensor1d([6, 7, 8]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [3 * 6 + 4 * 7 + 5 * 8]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [2 * 6, 2 * 7, 2 * 8]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D * scalar', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([[2, 3], [4, 5]], [2, 2]);
b = tf.scalar(2);
dy = tf.tensor2d([[6, 7], [8, 9]], [2, 2]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [2 * 6, 2 * 7, 2 * 8, 2 * 9]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [2 * 6 + 3 * 7 + 4 * 8 + 5 * 9]]);
return [2 /*return*/];
}
});
}); });
it('gradient: Tensor2D * Tensor2D w/ broadcast', function () { return __awaiter(_this, void 0, void 0, function () {
var a, b, dy, grads, _a, da, db, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
a = tf.tensor2d([3, 4], [2, 1]);
b = tf.tensor2d([[2, 3], [4, 5]], [2, 2]);
dy = tf.tensor2d([[6, 7], [8, 9]], [2, 2]);
grads = tf.grads(function (a, b) { return tf.mul(a, b); });
_a = grads([a, b], dy), da = _a[0], db = _a[1];
expect(da.shape).toEqual(a.shape);
expect(da.dtype).toEqual('float32');
_b = test_util_1.expectArraysClose;
return [4 /*yield*/, da.data()];
case 1:
_b.apply(void 0, [_d.sent(), [2 * 6 + 3 * 7, 4 * 8 + 5 * 9]]);
expect(db.shape).toEqual(b.shape);
expect(db.dtype).toEqual('float32');
_c = test_util_1.expectArraysClose;
return [4 /*yield*/, db.data()];
case 2:
_c.apply(void 0, [_d.sent(), [6 * 3, 7 * 3, 8 * 4, 9 * 4]]);
return [2 /*return*/];
}
});
}); });
it('complex number multiplication', function () { return __awaiter(_this, void 0, void 0, function () {
var real1, imag1, complex1, real2, imag2, complex2, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
real1 = tf.tensor1d([2]);
imag1 = tf.tensor1d([3]);
complex1 = tf.complex(real1, imag1);
real2 = tf.tensor1d([4]);
imag2 = tf.tensor1d([5]);
complex2 = tf.complex(real2, imag2);
result = complex1.mul(complex2);
expect(result.dtype).toBe('complex64');
expect(result.shape).toEqual([1]);
_a = test_util_1.expectArraysClose;
return [4 /*yield*/, result.data()];
case 1:
_a.apply(void 0, [_b.sent(), [2 * 4 - 3 * 5, 2 * 5 + 3 * 4]]);
return [2 /*return*/];
}
});
}); });
it('complex number broadcasting multiplication', function () { return __awaiter(_this, void 0, void 0, function () {
var real1, imag1, complex1, real2, imag2, complex2, result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
real1 = tf.tensor2d([1, 2, -3, -4], [2, 2]);
imag1 = tf.tensor2d([10, 20, -30, -40], [2, 2]);
complex1 = tf.complex(real1, imag1);
real2 = tf.tensor1d([4]);
imag2 = tf.tensor1d([5]);
complex2 = tf.complex(real2, imag2);
result = tf.mul(complex1, complex2);
expect(result.dtype).toEqual('complex64');
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 * 4 - 10 * 5, 1 * 5 + 10 * 4, 2 * 4 - 20 * 5, 2 * 5 + 20 * 4,
-3 * 4 + 30 * 5, -3 * 5 + -30 * 4, -4 * 4 + 40 * 5, -4 * 5 + -40 * 4
]]);
return [2 /*retu