UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

1,134 lines 89.5 kB
"use strict"; 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('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('throws when passed a non-tensor', function () { expect(function () { return tf.zerosLike({}); }) .toThrowError(/Argument 'x' passed to 'zerosLike' must be a Tensor/); }); }); 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('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('throws when passed a non-tensor', function () { expect(function () { return tf.onesLike({}); }) .toThrowError(/Argument 'x' passed to 'onesLike' must be a Tensor/); }); }); 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('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); }); }); 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); }); }); jasmine_util_1.describeWithFlags('fromPixels', test_util_1.WEBGL_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]); expect(res.dtype).toBe('int32'); test_util_1.expectArraysClose(res, [4, 5, 6]); }); it('fromPixels + fromPixels', function () { var pixelsA = new ImageData(1, 1); pixelsA.data[0] = 255; pixelsA.data[1] = 3; pixelsA.data[2] = 4; pixelsA.data[3] = 255; var pixelsB = new ImageData(1, 1); pixelsB.data[0] = 5; pixelsB.data[1] = 6; pixelsB.data[2] = 7; pixelsB.data[3] = 255; var a = tf.fromPixels(pixelsA, 3).toFloat(); var b = tf.fromPixels(pixelsB, 3).toFloat(); var res = a.add(b); expect(res.shape).toEqual([1, 1, 3]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [260, 9, 11]); }); }); jasmine_util_1.describeWithFlags('toPixels no canvas', test_util_1.ALL_ENVS, function () { it('draws a rank-2 float32 tensor', function (done) { var x = tf.tensor2d([.1, .2], [2, 1], 'float32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.1 * 255), Math.round(.1 * 255), Math.round(.1 * 255), 255, Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255 ]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-2 int32 tensor', function (done) { var x = tf.tensor2d([10, 20], [2, 1], 'int32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 1 channel', function (done) { var x = tf.tensor3d([.1, .2], [2, 1, 1], 'float32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.1 * 255), Math.round(.1 * 255), Math.round(.1 * 255), 255, Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255 ]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 1 channel', function (done) { var x = tf.tensor3d([10, 20], [2, 1, 1], 'int32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 3 channel', function (done) { var x = tf.tensor3d([.05, .1, .15, .2, .25, .3], [2, 1, 3], 'float32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.05 * 255), Math.round(.1 * 255), Math.round(.15 * 255), 255, Math.round(.2 * 255), Math.round(.25 * 255), Math.round(.30 * 255), 255 ]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 3 channel', function (done) { var x = tf.tensor3d([10, 20, 30, 40, 50, 60], [2, 1, 3], 'int32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([10, 20, 30, 255, 40, 50, 60, 255]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 4 channel', function (done) { var x = tf.tensor3d([.05, .1, .15, .2, .25, .3, .35, .4], [2, 1, 4], 'float32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.05 * 255), Math.round(.1 * 255), Math.round(.15 * 255), Math.round(.20 * 255), Math.round(.25 * 255), Math.round(.30 * 255), Math.round(.35 * 255), Math.round(.4 * 255) ]); expect(data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 4 channel', function (done) { var x = tf.tensor3d([10, 20, 30, 40, 50, 60, 70, 80], [2, 1, 4], 'int32'); tf.toPixels(x).then(function (data) { var expected = new Uint8ClampedArray([10, 20, 30, 40, 50, 60, 70, 80]); expect(data).toEqual(expected); done(); }); }); it('throws for scalars', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.scalar(1)); }, done); }); it('throws for rank-1 tensors', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor1d([1])); }, done); }); it('throws for rank-4 tensors', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor4d([1], [1, 1, 1, 1])); }, done); }); it('throws for bool dtype', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor2d([1], [1, 1], 'bool')); }, done); }); it('throws for rank-3 depth = 2', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor3d([1, 2], [1, 1, 2])); }, done); }); it('throws for rank-3 depth = 5', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor3d([1, 2, 3, 4, 5], [1, 1, 5])); }, done); }); it('throws for float32 tensor with values not in [0 - 1]', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor2d([-1, .5], [1, 2])); }, done); }); it('throws for int32 tensor with values not in [0 - 255]', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels(tf.tensor2d([-1, 100], [1, 2], 'int32')); }, done); }); it('throws when passed a non-tensor', function (done) { test_util_1.expectPromiseToFail(function () { return tf.toPixels({}); }, done); }); }); jasmine_util_1.describeWithFlags('toPixels', test_util_1.WEBGL_ENVS, function () { it('draws a rank-2 float32 tensor, canvas', function (done) { var x = tf.tensor2d([.1, .2], [2, 1], 'float32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.1 * 255), Math.round(.1 * 255), Math.round(.1 * 255), 255, Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255 ]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-2 int32 tensor, canvas', function (done) { var x = tf.tensor2d([10, 20], [2, 1], 'int32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 1 channel, canvas', function (done) { var x = tf.tensor3d([.1, .2], [2, 1, 1], 'float32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.1 * 255), Math.round(.1 * 255), Math.round(.1 * 255), 255, Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255 ]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 1 channel, canvas', function (done) { var x = tf.tensor3d([10, 20], [2, 1, 1], 'int32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 3 channel, canvas', function (done) { var x = tf.tensor3d([.05, .1, .15, .20, .25, .30], [2, 1, 3], 'float32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.05 * 255), Math.round(.1 * 255), Math.round(.15 * 255), 255, Math.round(.2 * 255), Math.round(.25 * 255), Math.round(.3 * 255), 255 ]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 3 channel, canvas', function (done) { var x = tf.tensor3d([10, 20, 30, 40, 50, 60], [2, 1, 3], 'int32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([10, 20, 30, 255, 40, 50, 60, 255]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 float32 tensor, 4 channel, canvas', function (done) { var x = tf.tensor3d([.05, .1, .15, 1, .20, .25, .30, 1], [2, 1, 4], 'float32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([ Math.round(.05 * 255), Math.round(.1 * 255), Math.round(.15 * 255), 255, Math.round(.20 * 255), Math.round(.25 * 255), Math.round(.30 * 255), 255 ]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); it('draws a rank-3 int32 tensor, 4 channel, canvas', function (done) { var x = tf.tensor3d([10, 20, 30, 255, 50, 60, 70, 255], [2, 1, 4], 'int32'); var canvas = document.createElement('canvas'); tf.toPixels(x, canvas).then(function (data) { var expected = new Uint8ClampedArray([10, 20, 30, 255, 50, 60, 70, 255]); expect(data).toEqual(expected); var ctx = canvas.getContext('2d'); var imgData = ctx.getImageData(0, 0, 1, 2); expect(imgData.data).toEqual(expected); done(); }); }); }); jasmine_util_1.describeWithFlags('clone', test_util_1.ALL_ENVS, function () { it('1D default dtype', function () { var a = tf.tensor1d([1, 2, 3]); var b = tf.clone(a); expect(b.dtype).toBe('float32'); expect(b.shape).toEqual([3]); test_util_1.expectArraysClose(b, [1, 2, 3]); }); it('1D float32 dtype', function () { var a = tf.tensor1d([1, 2, 3], 'float32'); var b = tf.clone(a); expect(b.dtype).toBe('float32'); expect(b.shape).toEqual([3]); test_util_1.expectArraysClose(b, [1, 2, 3]); }); it('1D int32 dtype', function () { var a = tf.tensor1d([1, 2