@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
1,148 lines (1,147 loc) • 150 kB
JavaScript
"use strict";
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 rand_util_1 = require("./rand_util");
jasmine_util_1.describeWithFlags('zeros', test_util_1.ALL_ENVS, function () {
it('1D default dtype', function () {
var a = tf.zeros([3]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysClose(a, [0, 0, 0]);
});
it('1D float32 dtype', function () {
var a = tf.zeros([3], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysClose(a, [0, 0, 0]);
});
it('1D int32 dtype', function () {
var a = tf.zeros([3], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysEqual(a, [0, 0, 0]);
});
it('1D bool dtype', function () {
var a = tf.zeros([3], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysEqual(a, [0, 0, 0]);
});
it('2D default dtype', function () {
var a = tf.zeros([3, 2]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0]);
});
it('2D float32 dtype', function () {
var a = tf.zeros([3, 2], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0]);
});
it('2D int32 dtype', function () {
var a = tf.zeros([3, 2], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0]);
});
it('2D bool dtype', function () {
var a = tf.zeros([3, 2], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0]);
});
it('3D default dtype', function () {
var a = tf.zeros([2, 2, 2]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0, 0, 0]);
});
it('3D float32 dtype', function () {
var a = tf.zeros([2, 2, 2], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0, 0, 0]);
});
it('3D int32 dtype', function () {
var a = tf.zeros([2, 2, 2], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0, 0, 0]);
});
it('3D bool dtype', function () {
var a = tf.zeros([2, 2, 2], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0, 0, 0]);
});
it('4D default dtype', function () {
var a = tf.zeros([3, 2, 1, 1]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0]);
});
it('4D float32 dtype', function () {
var a = tf.zeros([3, 2, 1, 1], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysClose(a, [0, 0, 0, 0, 0, 0]);
});
it('4D int32 dtype', function () {
var a = tf.zeros([3, 2, 1, 1], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0]);
});
it('4D bool dtype', function () {
var a = tf.zeros([3, 2, 1, 1], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysEqual(a, [0, 0, 0, 0, 0, 0]);
});
});
jasmine_util_1.describeWithFlags('ones', test_util_1.ALL_ENVS, function () {
it('1D default dtype', function () {
var a = tf.ones([3]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysClose(a, [1, 1, 1]);
});
it('1D float32 dtype', function () {
var a = tf.ones([3], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysClose(a, [1, 1, 1]);
});
it('1D int32 dtype', function () {
var a = tf.ones([3], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysEqual(a, [1, 1, 1]);
});
it('1D bool dtype', function () {
var a = tf.ones([3], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3]);
test_util_1.expectArraysEqual(a, [1, 1, 1]);
});
it('2D default dtype', function () {
var a = tf.ones([3, 2]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1]);
});
it('2D float32 dtype', function () {
var a = tf.ones([3, 2], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1]);
});
it('2D int32 dtype', function () {
var a = tf.ones([3, 2], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1]);
});
it('2D bool dtype', function () {
var a = tf.ones([3, 2], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3, 2]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1]);
});
it('3D default dtype', function () {
var a = tf.ones([2, 2, 2]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1, 1, 1]);
});
it('3D float32 dtype', function () {
var a = tf.ones([2, 2, 2], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1, 1, 1]);
});
it('3D int32 dtype', function () {
var a = tf.ones([2, 2, 2], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1, 1, 1]);
});
it('3D bool dtype', function () {
var a = tf.ones([2, 2, 2], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([2, 2, 2]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1, 1, 1]);
});
it('4D default dtype', function () {
var a = tf.ones([3, 2, 1, 1]);
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1]);
});
it('4D float32 dtype', function () {
var a = tf.ones([3, 2, 1, 1], 'float32');
expect(a.dtype).toBe('float32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysClose(a, [1, 1, 1, 1, 1, 1]);
});
it('4D int32 dtype', function () {
var a = tf.ones([3, 2, 1, 1], 'int32');
expect(a.dtype).toBe('int32');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1]);
});
it('4D bool dtype', function () {
var a = tf.ones([3, 2, 1, 1], 'bool');
expect(a.dtype).toBe('bool');
expect(a.shape).toEqual([3, 2, 1, 1]);
test_util_1.expectArraysEqual(a, [1, 1, 1, 1, 1, 1]);
});
});
jasmine_util_1.describeWithFlags('zerosLike', test_util_1.ALL_ENVS, function () {
it('1D default dtype', function () {
var a = tf.tensor1d([1, 2, 3]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [0, 0, 0]);
});
it('chainable 1D default dtype', function () {
var a = tf.tensor1d([1, 2, 3]);
var b = a.zerosLike();
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [0, 0, 0]);
});
it('1D float32 dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [0, 0, 0]);
});
it('1D int32 dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysEqual(b, [0, 0, 0]);
});
it('1D bool dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysEqual(b, [0, 0, 0]);
});
it('2D default dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('2D float32 dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('2D int32 dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('2D bool dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('3D default dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('3D float32 dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('3D int32 dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('3D bool dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('4D default dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('4D float32 dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('4D int32 dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('4D bool dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('4D default dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('5D float32 dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('5D int32 dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('5D bool dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('5D default dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('6D float32 dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'float32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([1, 2, 2, 1, 1, 1]);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('6D int32 dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'int32');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('6D bool dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'bool');
var b = tf.zerosLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysEqual(b, [0, 0, 0, 0]);
});
it('6D default dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1]);
var b = tf.zerosLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysClose(b, [0, 0, 0, 0]);
});
it('throws when passed a non-tensor', function () {
expect(function () { return tf.zerosLike({}); })
.toThrowError(/Argument 'x' passed to 'zerosLike' must be a Tensor/);
});
it('accepts a tensor-like object', function () {
var res = tf.zerosLike([[1, 2], [3, 4]]);
expect(res.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(res, [0, 0, 0, 0]);
});
});
jasmine_util_1.describeWithFlags('onesLike', test_util_1.ALL_ENVS, function () {
it('1D default dtype', function () {
var a = tf.tensor1d([1, 2, 3]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [1, 1, 1]);
});
it('chainable 1D default dtype', function () {
var a = tf.tensor1d([1, 2, 3]);
var b = a.onesLike();
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [1, 1, 1]);
});
it('1D float32 dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysClose(b, [1, 1, 1]);
});
it('1D int32 dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysEqual(b, [1, 1, 1]);
});
it('1D bool dtype', function () {
var a = tf.tensor1d([1, 2, 3], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([3]);
test_util_1.expectArraysEqual(b, [1, 1, 1]);
});
it('2D default dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('2D float32 dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('2D int32 dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('2D bool dtype', function () {
var a = tf.tensor2d([1, 2, 3, 4], [2, 2], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('3D default dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('3D float32 dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('3D int32 dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('3D bool dtype', function () {
var a = tf.tensor3d([1, 2, 3, 4], [2, 2, 1], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('4D default dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('4D float32 dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('4D int32 dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('4D bool dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('4D default dtype', function () {
var a = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('5D float32 dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('5D int32 dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('5D bool dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('5D default dtype', function () {
var a = tf.tensor5d([1, 2, 3, 4], [1, 2, 2, 1, 1]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual([1, 2, 2, 1, 1]);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('6D int32 dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'int32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('int32');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('6D bool dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'bool');
var b = tf.onesLike(a);
expect(b.dtype).toBe('bool');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysEqual(b, [1, 1, 1, 1]);
});
it('6D default dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1]);
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('6D float32 dtype', function () {
var a = tf.tensor6d([1, 2, 3, 4], [1, 2, 2, 1, 1, 1], 'float32');
var b = tf.onesLike(a);
expect(b.dtype).toBe('float32');
expect(b.shape).toEqual(a.shape);
test_util_1.expectArraysClose(b, [1, 1, 1, 1]);
});
it('throws when passed a non-tensor', function () {
expect(function () { return tf.onesLike({}); })
.toThrowError(/Argument 'x' passed to 'onesLike' must be a Tensor/);
});
it('accepts a tensor-like object', function () {
var res = tf.onesLike([[1, 2], [3, 4]]);
expect(res.shape).toEqual([2, 2]);
test_util_1.expectArraysEqual(res, [1, 1, 1, 1]);
});
});
jasmine_util_1.describeWithFlags('rand', test_util_1.ALL_ENVS, function () {
it('should return a random 1D float32 array', function () {
var shape = [10];
var result = tf.rand(shape, function () { return util.randUniform(0, 2); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2);
result = tf.rand(shape, function () { return util.randUniform(0, 1.5); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 1D int32 array', function () {
var shape = [10];
var result = tf.rand(shape, function () { return util.randUniform(0, 2); }, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 1D bool array', function () {
var shape = [10];
var result = tf.rand(shape, function () { return util.randUniform(0, 1); }, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 2D float32 array', function () {
var shape = [3, 4];
var result = tf.rand(shape, function () { return util.randUniform(0, 2.5); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.rand(shape, function () { return util.randUniform(0, 1.5); }, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 2D int32 array', function () {
var shape = [3, 4];
var result = tf.rand(shape, function () { return util.randUniform(0, 2); }, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 2D bool array', function () {
var shape = [3, 4];
var result = tf.rand(shape, function () { return util.randUniform(0, 1); }, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 3D float32 array', function () {
var shape = [3, 4, 5];
var result = tf.rand(shape, function () { return util.randUniform(0, 2.5); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.rand(shape, function () { return util.randUniform(0, 1.5); }, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 3D int32 array', function () {
var shape = [3, 4, 5];
var result = tf.rand(shape, function () { return util.randUniform(0, 2); }, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 3D bool array', function () {
var shape = [3, 4, 5];
var result = tf.rand(shape, function () { return util.randUniform(0, 1); }, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 4D float32 array', function () {
var shape = [3, 4, 5, 6];
var result = tf.rand(shape, function () { return util.randUniform(0, 2.5); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.rand(shape, function () { return util.randUniform(0, 1.5); });
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 4D int32 array', function () {
var shape = [3, 4, 5, 6];
var result = tf.rand(shape, function () { return util.randUniform(0, 2); }, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 4D bool array', function () {
var shape = [3, 4, 5, 6];
var result = tf.rand(shape, function () { return util.randUniform(0, 1); }, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
});
jasmine_util_1.describeWithFlags('eye', test_util_1.ALL_ENVS, function () {
it('1x1', function () {
test_util_1.expectArraysClose(tf.eye(1), tf.tensor2d([[1]]));
});
it('2x2', function () {
test_util_1.expectArraysClose(tf.eye(2), tf.tensor2d([[1, 0], [0, 1]]));
});
it('3x3', function () {
test_util_1.expectArraysClose(tf.eye(3), tf.tensor2d([[1, 0, 0], [0, 1, 0], [0, 0, 1]]));
});
it('3x4', function () {
test_util_1.expectArraysClose(tf.eye(3, 4), tf.tensor2d([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]));
});
it('4x3', function () {
test_util_1.expectArraysClose(tf.eye(4, 3), tf.tensor2d([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]));
});
it('with 1D batchShape', function () {
test_util_1.expectArraysClose(tf.eye(2, 2, [3]), tf.tensor3d([[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]]));
});
it('with 2D batchShape', function () {
test_util_1.expectArraysClose(tf.eye(2, 2, [2, 3]), tf.tensor4d([
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]],
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]]
]));
});
it('with 3D batchShape', function () {
test_util_1.expectArraysClose(tf.eye(2, 2, [2, 2, 3]), tf.tensor5d([
[
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]],
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]]
],
[
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]],
[[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 0], [0, 1]]]
]
]));
});
it('3x3, int32', function () {
test_util_1.expectArraysClose(tf.eye(3, 3, null, 'int32'), tf.tensor2d([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [3, 3], 'int32'));
});
it('3x3, bool', function () {
test_util_1.expectArraysClose(tf.eye(3, 3, null, 'bool'), tf.tensor2d([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [3, 3], 'bool'));
});
});
jasmine_util_1.describeWithFlags('randomNormal', test_util_1.ALL_ENVS, function () {
var SEED = 2002;
var EPSILON = 0.05;
it('should return a float32 1D of random normal values', function () {
var SAMPLES = 10000;
var result = tf.randomNormal([SAMPLES], 0, 0.5, null, SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual([SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 0.5, EPSILON);
result = tf.randomNormal([SAMPLES], 0, 1.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual([SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 1.5, EPSILON);
});
it('should return a int32 1D of random normal values', function () {
var SAMPLES = 10000;
var result = tf.randomNormal([SAMPLES], 0, 2, 'int32', SEED);
expect(result.dtype).toBe('int32');
expect(result.shape).toEqual([SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2, EPSILON);
});
it('should return a float32 2D of random normal values', function () {
var SAMPLES = 100;
var result = tf.randomNormal([SAMPLES, SAMPLES], 0, 2.5, null, SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual([SAMPLES, SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2.5, EPSILON);
result = tf.randomNormal([SAMPLES, SAMPLES], 0, 3.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual([SAMPLES, SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 3.5, EPSILON);
});
it('should return a int32 2D of random normal values', function () {
var SAMPLES = 100;
var result = tf.randomNormal([SAMPLES, SAMPLES], 0, 2, 'int32', SEED);
expect(result.dtype).toBe('int32');
expect(result.shape).toEqual([SAMPLES, SAMPLES]);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2, EPSILON);
});
it('should return a float32 3D of random normal values', function () {
var SAMPLES_SHAPE = [20, 20, 20];
var result = tf.randomNormal(SAMPLES_SHAPE, 0, 0.5, null, SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 0.5, EPSILON);
result = tf.randomNormal(SAMPLES_SHAPE, 0, 1.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 1.5, EPSILON);
});
it('should return a int32 3D of random normal values', function () {
var SAMPLES_SHAPE = [20, 20, 20];
var result = tf.randomNormal(SAMPLES_SHAPE, 0, 2, 'int32', SEED);
expect(result.dtype).toBe('int32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2, EPSILON);
});
it('should return a float32 4D of random normal values', function () {
var SAMPLES_SHAPE = [10, 10, 10, 10];
var result = tf.randomNormal(SAMPLES_SHAPE, 0, 0.5, null, SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 0.5, EPSILON);
result = tf.randomNormal(SAMPLES_SHAPE, 0, 1.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 1.5, EPSILON);
});
it('should return a int32 4D of random normal values', function () {
var SAMPLES_SHAPE = [10, 10, 10, 10];
var result = tf.randomNormal(SAMPLES_SHAPE, 0, 2, 'int32', SEED);
expect(result.dtype).toBe('int32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2, EPSILON);
});
it('should return a int32 5D of random normal values', function () {
var SAMPLES_SHAPE = [10, 10, 10, 10, 10];
var result = tf.randomNormal(SAMPLES_SHAPE, 0, 2, 'int32', SEED);
expect(result.dtype).toBe('int32');
expect(result.shape).toEqual(SAMPLES_SHAPE);
rand_util_1.jarqueBeraNormalityTest(result);
rand_util_1.expectArrayInMeanStdRange(result, 0, 2, EPSILON);
});
});
jasmine_util_1.describeWithFlags('truncatedNormal', test_util_1.ALL_ENVS, function () {
var EPSILON = 0.60;
var SEED = 2002;
function assertTruncatedValues(array, mean, stdv) {
var bounds = mean + stdv * 2;
var values = array.dataSync();
for (var i = 0; i < values.length; i++) {
expect(Math.abs(values[i])).toBeLessThanOrEqual(bounds);
}
}
it('should return a random 1D float32 array', function () {
var shape = [1000];
var result = tf.truncatedNormal(shape, 0, 3.5, null, SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 3.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 3.5, EPSILON);
result = tf.truncatedNormal(shape, 0, 4.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 4.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 4.5, EPSILON);
});
it('should return a randon 1D int32 array', function () {
var shape = [1000];
var result = tf.truncatedNormal(shape, 0, 5, 'int32', SEED);
expect(result.dtype).toBe('int32');
assertTruncatedValues(result, 0, 5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 5, EPSILON);
});
it('should return a 2D float32 array', function () {
var shape = [50, 50];
var result = tf.truncatedNormal(shape, 0, 3.5, null, SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 3.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 3.5, EPSILON);
result = tf.truncatedNormal(shape, 0, 4.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 4.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 4.5, EPSILON);
});
it('should return a 2D int32 array', function () {
var shape = [50, 50];
var result = tf.truncatedNormal(shape, 0, 5, 'int32', SEED);
expect(result.dtype).toBe('int32');
assertTruncatedValues(result, 0, 5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 5, EPSILON);
});
it('should return a 3D float32 array', function () {
var shape = [10, 10, 10];
var result = tf.truncatedNormal(shape, 0, 3.5, null, SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 3.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 3.5, EPSILON);
result = tf.truncatedNormal(shape, 0, 4.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 4.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 4.5, EPSILON);
});
it('should return a 3D int32 array', function () {
var shape = [10, 10, 10];
var result = tf.truncatedNormal(shape, 0, 5, 'int32', SEED);
expect(result.dtype).toBe('int32');
assertTruncatedValues(result, 0, 5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 5, EPSILON);
});
it('should return a 4D float32 array', function () {
var shape = [5, 5, 5, 5];
var result = tf.truncatedNormal(shape, 0, 3.5, null, SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 3.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 3.5, EPSILON);
result = tf.truncatedNormal(shape, 0, 4.5, 'float32', SEED);
expect(result.dtype).toBe('float32');
assertTruncatedValues(result, 0, 4.5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 4.5, EPSILON);
});
it('should return a 4D int32 array', function () {
var shape = [5, 5, 5, 5];
var result = tf.truncatedNormal(shape, 0, 5, 'int32', SEED);
expect(result.dtype).toBe('int32');
assertTruncatedValues(result, 0, 5);
rand_util_1.expectArrayInMeanStdRange(result, 0, 5, EPSILON);
});
});
jasmine_util_1.describeWithFlags('randomUniform', test_util_1.ALL_ENVS, function () {
it('should return a random 1D float32 array', function () {
var shape = [10];
var result = tf.randomUniform(shape, 0, 2.5);
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.randomUniform(shape, 0, 1.5, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 1D int32 array', function () {
var shape = [10];
var result = tf.randomUniform(shape, 0, 2, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 1D bool array', function () {
var shape = [10];
var result = tf.randomUniform(shape, 0, 1, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 2D float32 array', function () {
var shape = [3, 4];
var result = tf.randomUniform(shape, 0, 2.5);
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.randomUniform(shape, 0, 1.5, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 2D int32 array', function () {
var shape = [3, 4];
var result = tf.randomUniform(shape, 0, 2, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 2D bool array', function () {
var shape = [3, 4];
var result = tf.randomUniform(shape, 0, 1, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 3D float32 array', function () {
var shape = [3, 4, 5];
var result = tf.randomUniform(shape, 0, 2.5);
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.randomUniform(shape, 0, 1.5, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 3D int32 array', function () {
var shape = [3, 4, 5];
var result = tf.randomUniform(shape, 0, 2, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 3D bool array', function () {
var shape = [3, 4, 5];
var result = tf.randomUniform(shape, 0, 1, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 4D float32 array', function () {
var shape = [3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 2.5);
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.randomUniform(shape, 0, 1.5, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 4D int32 array', function () {
var shape = [3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 2, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 4D bool array', function () {
var shape = [3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 1, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
it('should return a random 5D float32 array', function () {
var shape = [2, 3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 2.5);
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 2.5);
result = tf.randomUniform(shape, 0, 1.5, 'float32');
expect(result.dtype).toBe('float32');
test_util_1.expectValuesInRange(result, 0, 1.5);
});
it('should return a random 5D int32 array', function () {
var shape = [2, 3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 2, 'int32');
expect(result.dtype).toBe('int32');
test_util_1.expectValuesInRange(result, 0, 2);
});
it('should return a random 5D bool array', function () {
var shape = [2, 3, 4, 5, 6];
var result = tf.randomUniform(shape, 0, 1, 'bool');
expect(result.dtype).toBe('bool');
test_util_1.expectValuesInRange(result, 0, 1);
});
});
var MockContext = (function () {
function MockContext() {
}
MockContext.prototype.getImageData = function (x, y, width, height) {
var data = new Uint8ClampedArray(width * height * 4);
for (var i = 0; i < data.length; ++i) {
data[i] = i + 1;
}
return { data: data };
};
return MockContext;
}());
var MockCanvas = (function () {
function MockCanvas(width, height) {
this.width = width;
this.height = height;
}
MockCanvas.prototype.getContext = function (type) {
return new MockContext();
};
return MockCanvas;
}());
jasmine_util_1.describeWithFlags('fromPixels, mock canvas', test_util_1.NODE_ENVS, function () {
it('accepts a canvas-like element', function () {
var c = new MockCanvas(2, 2);
var t = tf.fromPixels(c);
expect(t.dtype).toBe('int32');
expect(t.shape).toEqual([2, 2, 3]);
tf.test_util.expectArraysEqual(t, [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15]);
});
it('accepts a canvas-like element, numChannels=4', function () {
var c = new MockCanvas(2, 2);
var t = tf.fromPixels(c, 4);
expect(t.dtype).toBe('int32');
expect(t.shape).toEqual([2, 2, 4]);
tf.test_util.expectArraysEqual(t, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
});
it('errors when passed a non-canvas object', function () {
expect(function () { return tf.fromPixels(5); }).toThrowError();
});
});
jasmine_util_1.describeWithFlags('fromPixels', test_util_1.BROWSER_ENVS, function () {
it('ImageData 1x1x3', function () {
var pixels = new ImageData(1, 1);
pixels.data[0] = 0;
pixels.data[1] = 80;
pixels.data[2] = 160;
pixels.data[3] = 240;
var array = tf.fromPixels(pixels, 3);
test_util_1.expectArraysEqual(array, [0, 80, 160]);
});
it('ImageData 1x1x4', function () {
var pixels = new ImageData(1, 1);
pixels.data[0] = 0;
pixels.data[1] = 80;
pixels.data[2] = 160;
pixels.data[3] = 240;
var array = tf.fromPixels(pixels, 4);
test_util_1.expectArraysEqual(array, [0, 80, 160, 240]);
});
it('ImageData 2x2x3', function () {
var pixels = new ImageData(2, 2);
for (var i = 0; i < 8; i++) {
pixels.data[i] = i * 2;
}
for (var i = 8; i < 16; i++) {
pixels.data[i] = i * 2;
}
var array = tf.fromPixels(pixels, 3);
test_util_1.expectArraysEqual(array, [0, 2, 4, 8, 10, 12, 16, 18, 20, 24, 26, 28]);
});
it('ImageData 2x2x4', function () {
var pixels = new ImageData(2, 2);
for (var i = 0; i < 8; i++) {
pixels.data[i] = i * 2;
}
for (var i = 8; i < 16; i++) {
pixels.data[i] = i * 2;
}
var array = tf.fromPixels(pixels, 4);
test_util_1.expectArraysClose(array, new Int32Array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]));
});
it('fromPixels, 3 channels', function () {
var pixels = new ImageData(1, 2);
pixels.data[0] = 2;
pixels.data[1] = 3;
pixels.data[2] = 4;
pixels.data[3] = 255;
pixels.data[4] = 5;
pixels.data[5] = 6;
pixels.data[6] = 7;
pixels.data[7] = 255;
var res = tf.fromPixels(pixels, 3);
expect(res.shape).toEqual([2, 1, 3]);
expect(res.dtype).toBe('int32');
test_util_1.expectArraysClose(res, [2, 3, 4, 5, 6, 7]);
});
it('fromPixels, reshape, then do tf.add()', function () {
var pixels = new ImageData(1, 1);
pixels.data[0] = 2;
pixels.data[1] = 3;
pixels.data[2] = 4;
pixels.data[3] = 255;
var a = tf.fromPixels(pixels, 3).reshape([1, 1, 1, 3]);
var res = a.add(tf.scalar(2, 'int32'));
expect(res.shape).toEqual([1, 1, 1, 3]);