UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

844 lines 35.7 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 reduce_util = require("./reduce_util"); jasmine_util_1.describeWithFlags('min', test_util_1.ALL_ENVS, function () { it('Tensor1D', function () { var a = tf.tensor1d([3, -1, 0, 100, -7, 2]); test_util_1.expectNumbersClose(tf.min(a).get(), -7); }); it('ignores NaNs', function () { var a = tf.tensor1d([3, NaN, 2]); expect(tf.min(a).get()).toEqual(2); }); it('2D', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectNumbersClose(tf.min(a).get(), -7); }); it('2D axis=[0,1]', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectNumbersClose(tf.min(a, [0, 1]).get(), -7); }); it('2D, axis=0', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.min(a, 0); expect(r.shape).toEqual([3]); test_util_1.expectArraysClose(r, [3, -7, 0]); }); it('2D, axis=0, keepDims', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.min(a, 0, true); expect(r.shape).toEqual([1, 3]); test_util_1.expectArraysClose(r, [3, -7, 0]); }); it('2D, axis=1 provided as a number', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.min(a, 1); test_util_1.expectArraysClose(r, [2, -7]); }); it('2D, axis = -1 provided as a number', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.min(a, -1); test_util_1.expectArraysClose(r, [2, -7]); }); it('2D, axis=[1]', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.min(a, [1]); test_util_1.expectArraysClose(r, [2, -7]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.min({}); }) .toThrowError(/Argument 'x' passed to 'min' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('max', test_util_1.ALL_ENVS, function () { it('with one element dominating', function () { var a = tf.tensor1d([3, -1, 0, 100, -7, 2]); var r = tf.max(a); test_util_1.expectNumbersClose(r.get(), 100); }); it('with all elements being the same', function () { var a = tf.tensor1d([3, 3, 3]); var r = tf.max(a); test_util_1.expectNumbersClose(r.get(), 3); }); it('ignores NaNs', function () { expect(tf.max(tf.tensor1d([3, NaN, 2])).get()).toEqual(3); }); it('2D', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectNumbersClose(tf.max(a).get(), 100); }); it('2D axis=[0,1]', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectNumbersClose(tf.max(a, [0, 1]).get(), 100); }); it('2D, axis=0', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.max(a, [0]); expect(r.shape).toEqual([3]); test_util_1.expectArraysClose(r, [100, -1, 2]); }); it('2D, axis=0, keepDims', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.max(a, [0], true); expect(r.shape).toEqual([1, 3]); test_util_1.expectArraysClose(r, [100, -1, 2]); }); it('2D, axis=1 provided as a number', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.max(a, 1); test_util_1.expectArraysClose(r, [5, 100]); }); it('2D, axis = -1 provided as a number', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.max(a, -1); test_util_1.expectArraysClose(r, [5, 100]); }); it('2D, axis=[1]', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.max(a, [1]); test_util_1.expectArraysClose(r, [5, 100]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.max({}); }) .toThrowError(/Argument 'x' passed to 'max' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('argmax', test_util_1.ALL_ENVS, function () { it('Tensor1D', function () { var a = tf.tensor1d([1, 0, 3, 2]); var result = tf.argMax(a); expect(result.dtype).toBe('int32'); expect(result.get()).toBe(2); }); it('one value', function () { var a = tf.tensor1d([10]); var result = tf.argMax(a); expect(result.dtype).toBe('int32'); expect(result.get()).toBe(0); }); it('N > than parallelization threshold', function () { var n = reduce_util.PARALLELIZE_THRESHOLD * 2; var values = new Float32Array(n); for (var i = 0; i < n; i++) { values[i] = i; } var a = tf.tensor1d(values); var result = tf.argMax(a); expect(result.dtype).toBe('int32'); expect(result.get()).toBe(n - 1); }); it('ignores NaNs', function () { var a = tf.tensor1d([0, 3, 5, NaN, 3]); var res = tf.argMax(a); expect(res.dtype).toBe('int32'); expect(res.get()).toBe(2); }); it('2D, no axis specified', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectArraysEqual(tf.argMax(a), [1, 0, 1]); }); it('2D, axis=0', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.argMax(a, 0); expect(r.shape).toEqual([3]); expect(r.dtype).toBe('int32'); test_util_1.expectArraysEqual(r, [1, 0, 1]); }); it('2D, axis=1', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.argMax(a, 1); expect(r.dtype).toBe('int32'); test_util_1.expectArraysEqual(r, [2, 0]); }); it('2D, axis = -1', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]); var r = tf.argMax(a, -1); expect(r.dtype).toBe('int32'); test_util_1.expectArraysEqual(r, [2, 0]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.argMax({}); }) .toThrowError(/Argument 'x' passed to 'argMax' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('argmin', test_util_1.ALL_ENVS, function () { it('Tensor1D', function () { var a = tf.tensor1d([1, 0, 3, 2]); var result = tf.argMin(a); expect(result.get()).toBe(1); }); it('one value', function () { var a = tf.tensor1d([10]); var result = tf.argMin(a); expect(result.get()).toBe(0); }); it('N > than parallelization threshold', function () { var n = reduce_util.PARALLELIZE_THRESHOLD * 2; var values = new Float32Array(n); for (var i = 0; i < n; i++) { values[i] = n - i; } var a = tf.tensor1d(values); var result = tf.argMin(a); expect(result.dtype).toBe('int32'); expect(result.get()).toBe(n - 1); }); it('ignores NaNs', function () { var a = tf.tensor1d([5, 0, NaN, -1, 3]); var res = tf.argMin(a); expect(res.get()).toBe(3); }); it('2D, no axis specified', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); test_util_1.expectArraysEqual(tf.argMin(a), [0, 1, 0]); }); it('2D, axis=0', function () { var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]); var r = tf.argMin(a, 0); expect(r.shape).toEqual([3]); expect(r.dtype).toBe('int32'); test_util_1.expectArraysEqual(r, [0, 1, 0]); }); it('2D, axis=1', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, -8], [2, 3]); var r = tf.argMin(a, 1); test_util_1.expectArraysEqual(r, [1, 2]); }); it('2D, axis = -1', function () { var a = tf.tensor2d([3, 2, 5, 100, -7, -8], [2, 3]); var r = tf.argMin(a, -1); test_util_1.expectArraysEqual(r, [1, 2]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.argMin({}); }) .toThrowError(/Argument 'x' passed to 'argMin' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('logSumExp', test_util_1.ALL_ENVS, function () { it('0', function () { var a = tf.scalar(0); var result = tf.logSumExp(a); test_util_1.expectNumbersClose(result.get(), 0); }); it('basic', function () { var a = tf.tensor1d([1, 2, -3]); var result = tf.logSumExp(a); test_util_1.expectNumbersClose(result.get(), Math.log(Math.exp(1) + Math.exp(2) + Math.exp(-3))); }); it('propagates NaNs', function () { var a = tf.tensor1d([1, 2, NaN]); var result = tf.logSumExp(a); expect(result.get()).toEqual(NaN); }); it('axes=0 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var r = tf.logSumExp(a, [0]); expect(r.shape).toEqual([2]); var expected = [ Math.log(Math.exp(1) + Math.exp(3) + Math.exp(0)), Math.log(Math.exp(2) + Math.exp(0) + Math.exp(1)) ]; test_util_1.expectArraysClose(r, expected); }); it('axes=0 in 2D array, keepDims', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var r = tf.logSumExp(a, [0], true); expect(r.shape).toEqual([1, 2]); var expected = [ Math.log(Math.exp(1) + Math.exp(3) + Math.exp(0)), Math.log(Math.exp(2) + Math.exp(0) + Math.exp(1)) ]; test_util_1.expectArraysClose(r, expected); }); it('axes=1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.logSumExp(a, [1]); expect(res.shape).toEqual([3]); var expected = [ Math.log(Math.exp(1) + Math.exp(2)), Math.log(Math.exp(3) + Math.exp(0)), Math.log(Math.exp(0) + Math.exp(1)), ]; test_util_1.expectArraysClose(res, expected); }); it('axes = -1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.logSumExp(a, -1); expect(res.shape).toEqual([3]); var expected = [ Math.log(Math.exp(1) + Math.exp(2)), Math.log(Math.exp(3) + Math.exp(0)), Math.log(Math.exp(0) + Math.exp(1)), ]; test_util_1.expectArraysClose(res, expected); }); it('2D, axes=1 provided as a single digit', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var res = tf.logSumExp(a, 1); expect(res.shape).toEqual([2]); var expected = [ Math.log(Math.exp(1) + Math.exp(2) + Math.exp(3)), Math.log(Math.exp(0) + Math.exp(0) + Math.exp(1)) ]; test_util_1.expectArraysClose(res, expected); }); it('axes=0,1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.logSumExp(a, [0, 1]); expect(res.shape).toEqual([]); var expected = [Math.log(Math.exp(1) + Math.exp(2) + Math.exp(3) + Math.exp(0) + Math.exp(0) + Math.exp(1))]; test_util_1.expectArraysClose(res, expected); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.logSumExp({}); }) .toThrowError(/Argument 'x' passed to 'logSumExp' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('sum', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var result = tf.sum(a); test_util_1.expectNumbersClose(result.get(), 7); }); it('propagates NaNs', function () { var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]); expect(tf.sum(a).get()).toEqual(NaN); }); it('sum over dtype int32', function () { var a = tf.tensor1d([1, 5, 7, 3], 'int32'); var sum = tf.sum(a); expect(sum.get()).toBe(16); }); it('sum over dtype bool', function () { var a = tf.tensor1d([true, false, false, true, true], 'bool'); var sum = tf.sum(a); expect(sum.get()).toBe(3); }); it('sums all values in 2D array with keep dim', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, null, true); expect(res.shape).toEqual([1, 1]); test_util_1.expectArraysClose(res, [7]); }); it('sums across axis=0 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, [0]); expect(res.shape).toEqual([2]); test_util_1.expectArraysClose(res, [4, 3]); }); it('sums across axis=0 in 2D array, keepDims', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, [0], true); expect(res.shape).toEqual([1, 2]); test_util_1.expectArraysClose(res, [4, 3]); }); it('sums across axis=1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, [1]); expect(res.shape).toEqual([3]); test_util_1.expectArraysClose(res, [3, 3, 1]); }); it('2D, axis=1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var res = tf.sum(a, 1); expect(res.shape).toEqual([2]); test_util_1.expectArraysClose(res, [6, 1]); }); it('2D, axis = -1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var res = tf.sum(a, -1); expect(res.shape).toEqual([2]); test_util_1.expectArraysClose(res, [6, 1]); }); it('sums across axis=0,1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, [0, 1]); expect(res.shape).toEqual([]); test_util_1.expectArraysClose(res, [7]); }); it('2D, axis=[-1,-2] in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.sum(a, [-1, -2]); expect(res.shape).toEqual([]); test_util_1.expectArraysClose(res, [7]); }); it('gradients: sum(2d)', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var dy = tf.scalar(10); var gradients = tf.grad(function (a) { return a.sum(); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [10, 10, 10, 10, 10, 10]); }); it('gradients: sum(2d, axis=0)', function () { var a = tf.tensor2d([[1, 2], [3, 0], [0, 1]], [3, 2]); var dy = tf.tensor1d([10, 20]); var axis = 0; var gradients = tf.grad(function (a) { return a.sum(axis); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [10, 20, 10, 20, 10, 20]); }); it('gradients: sum(2d, axis=1)', function () { var a = tf.tensor2d([[1, 2], [3, 0], [0, 1]], [3, 2]); var dy = tf.tensor1d([10, 20, 30]); var axis = 1; var gradients = tf.grad(function (a) { return a.sum(axis); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [10, 10, 20, 20, 30, 30]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.sum({}); }) .toThrowError(/Argument 'x' passed to 'sum' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('mean', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var r = tf.mean(a); expect(r.dtype).toBe('float32'); test_util_1.expectNumbersClose(r.get(), 7 / 6); }); it('propagates NaNs', function () { var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]); var r = tf.mean(a); expect(r.dtype).toBe('float32'); expect(r.get()).toEqual(NaN); }); it('mean(int32) => float32', function () { var a = tf.tensor1d([1, 5, 7, 3], 'int32'); var r = tf.mean(a); expect(r.dtype).toBe('float32'); test_util_1.expectNumbersClose(r.get(), 4); }); it('mean(bool) => float32', function () { var a = tf.tensor1d([true, false, false, true, true], 'bool'); var r = tf.mean(a); expect(r.dtype).toBe('float32'); test_util_1.expectNumbersClose(r.get(), 3 / 5); }); it('2D array with keep dim', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, null, true); expect(res.shape).toEqual([1, 1]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [7 / 6]); }); it('axis=0 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, [0]); expect(res.shape).toEqual([2]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [4 / 3, 1]); }); it('axis=0 in 2D array, keepDims', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, [0], true); expect(res.shape).toEqual([1, 2]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [4 / 3, 1]); }); it('axis=1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, [1]); expect(res.dtype).toBe('float32'); expect(res.shape).toEqual([3]); test_util_1.expectArraysClose(res, [1.5, 1.5, 0.5]); }); it('axis = -1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, [-1]); expect(res.dtype).toBe('float32'); expect(res.shape).toEqual([3]); test_util_1.expectArraysClose(res, [1.5, 1.5, 0.5]); }); it('2D, axis=1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var res = tf.mean(a, 1); expect(res.shape).toEqual([2]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [2, 1 / 3]); }); it('axis=0,1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var res = tf.mean(a, [0, 1]); expect(res.shape).toEqual([]); expect(res.dtype).toBe('float32'); test_util_1.expectArraysClose(res, [7 / 6]); }); it('gradients', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var dy = tf.scalar(1.5); var da = tf.grad(function (a) { return a.mean(); })(a, dy); expect(da.shape).toEqual(a.shape); test_util_1.expectArraysClose(da, [ dy.get() / a.size, dy.get() / a.size, dy.get() / a.size, dy.get() / a.size, dy.get() / a.size, dy.get() / a.size ]); }); it('gradients throws for defined axis', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var dy = tf.scalar(1.5); expect(function () { return tf.grad(function (a) { return a.mean(1); })(a, dy); }).toThrowError(); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.mean({}); }) .toThrowError(/Argument 'x' passed to 'mean' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('moments', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var _a = tf.moments(a), mean = _a.mean, variance = _a.variance; expect(mean.dtype).toBe('float32'); expect(variance.dtype).toBe('float32'); test_util_1.expectNumbersClose(mean.get(), 7 / 6); test_util_1.expectNumbersClose(variance.get(), 1.1389); }); it('propagates NaNs', function () { var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]); var _a = tf.moments(a), mean = _a.mean, variance = _a.variance; expect(mean.dtype).toBe('float32'); expect(variance.dtype).toBe('float32'); expect(mean.get()).toEqual(NaN); expect(variance.get()).toEqual(NaN); }); it('moments(int32) => float32', function () { var a = tf.tensor1d([1, 5, 7, 3], 'int32'); var _a = tf.moments(a), mean = _a.mean, variance = _a.variance; expect(mean.dtype).toBe('float32'); expect(variance.dtype).toBe('float32'); test_util_1.expectNumbersClose(mean.get(), 4); test_util_1.expectNumbersClose(variance.get(), 5); }); it('moments(bool) => float32', function () { var a = tf.tensor1d([true, false, false, true, true], 'bool'); var _a = tf.moments(a), mean = _a.mean, variance = _a.variance; expect(mean.dtype).toBe('float32'); expect(variance.dtype).toBe('float32'); test_util_1.expectNumbersClose(mean.get(), 3 / 5); test_util_1.expectNumbersClose(variance.get(), 0.23999998); }); it('2D array with keep dim', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var _a = tf.moments(a, null, true), mean = _a.mean, variance = _a.variance; expect(mean.shape).toEqual([1, 1]); expect(mean.dtype).toBe('float32'); expect(variance.shape).toEqual([1, 1]); expect(variance.dtype).toBe('float32'); test_util_1.expectArraysClose(mean, [7 / 6]); test_util_1.expectArraysClose(variance, [1.138889]); }); it('axis=0 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var _a = tf.moments(a, [0]), mean = _a.mean, variance = _a.variance; expect(mean.shape).toEqual([2]); expect(mean.dtype).toBe('float32'); expect(variance.shape).toEqual([2]); expect(variance.dtype).toBe('float32'); test_util_1.expectArraysClose(mean, [4 / 3, 1]); test_util_1.expectArraysClose(variance, [1.556, 2 / 3]); }); it('axis=1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var _a = tf.moments(a, [1]), mean = _a.mean, variance = _a.variance; expect(mean.dtype).toBe('float32'); expect(mean.shape).toEqual([3]); expect(variance.dtype).toBe('float32'); expect(variance.shape).toEqual([3]); test_util_1.expectArraysClose(mean, [1.5, 1.5, 0.5]); test_util_1.expectArraysClose(variance, [0.25, 2.25, 0.25]); }); it('2D, axis=1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var _a = tf.moments(a, 1), mean = _a.mean, variance = _a.variance; expect(mean.shape).toEqual([2]); expect(mean.dtype).toBe('float32'); expect(variance.shape).toEqual([2]); expect(variance.dtype).toBe('float32'); test_util_1.expectArraysClose(mean, [2, 1 / 3]); test_util_1.expectArraysClose(variance, [2 / 3, 0.222]); }); it('2D, axis=-1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var _a = tf.moments(a, -1), mean = _a.mean, variance = _a.variance; expect(mean.shape).toEqual([2]); expect(mean.dtype).toBe('float32'); expect(variance.shape).toEqual([2]); expect(variance.dtype).toBe('float32'); test_util_1.expectArraysClose(mean, [2, 1 / 3]); test_util_1.expectArraysClose(variance, [2 / 3, 0.222]); }); it('axis=0,1 in 2D array', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var _a = tf.moments(a, [0, 1]), mean = _a.mean, variance = _a.variance; expect(mean.shape).toEqual([]); expect(mean.dtype).toBe('float32'); expect(variance.shape).toEqual([]); expect(variance.dtype).toBe('float32'); test_util_1.expectArraysClose(mean, [7 / 6]); test_util_1.expectArraysClose(variance, [1.1389]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.moments({}); }) .toThrowError(/Argument 'x' passed to 'moments' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('norm', test_util_1.ALL_ENVS, function () { it('scalar norm', function () { var a = tf.scalar(-22.0); var norm = tf.norm(a); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 22); }); it('vector inf norm', function () { var a = tf.tensor1d([1, -2, 3, -4]); var norm = tf.norm(a, Infinity); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 4); }); it('vector -inf norm', function () { var a = tf.tensor1d([1, -2, 3, -4]); var norm = tf.norm(a, -Infinity); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 1); }); it('vector 1 norm', function () { var a = tf.tensor1d([1, -2, 3, -4]); var norm = tf.norm(a, 1); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 10); }); it('vector euclidean norm', function () { var a = tf.tensor1d([1, -2, 3, -4]); var norm = tf.norm(a, 'euclidean'); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 5.4772); }); it('vector 2-norm', function () { var a = tf.tensor1d([1, -2, 3, -4]); var norm = tf.norm(a, 2); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 5.4772); }); it('vector >2-norm to throw error', function () { var a = tf.tensor1d([1, -2, 3, -4]); expect(function () { return tf.norm(a, 3); }).toThrowError(); }); it('matrix inf norm', function () { var a = tf.tensor2d([1, 2, -3, 1, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [0, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 4); }); it('matrix -inf norm', function () { var a = tf.tensor2d([1, 2, -3, 1, 0, 1], [3, 2]); var norm = tf.norm(a, -Infinity, [0, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 1); }); it('matrix 1 norm', function () { var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]); var norm = tf.norm(a, 1, [0, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 5); }); it('matrix euclidean norm', function () { var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]); var norm = tf.norm(a, 'euclidean', [0, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 4.123); }); it('matrix fro norm', function () { var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]); var norm = tf.norm(a, 'fro', [0, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectNumbersClose(norm.get(), 4.123); }); it('matrix other norm to throw error', function () { var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]); expect(function () { return tf.norm(a, 2, [0, 1]); }).toThrowError(); }); it('propagates NaNs for norm', function () { var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]); var norm = tf.norm(a); expect(norm.dtype).toBe('float32'); expect(norm.get()).toEqual(NaN); }); it('axis=null in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity); expect(norm.shape).toEqual([]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('2D array norm with keep dim', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, null, true); expect(norm.shape).toEqual([1, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('axis=0 in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [0]); expect(norm.shape).toEqual([2]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3, 2]); }); it('axis=1 in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [1]); expect(norm.dtype).toBe('float32'); expect(norm.shape).toEqual([3]); test_util_1.expectArraysClose(norm, [2, 3, 1]); }); it('axis=1 keepDims in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [1], true); expect(norm.dtype).toBe('float32'); expect(norm.shape).toEqual([3, 1]); test_util_1.expectArraysClose(norm, [2, 3, 1]); }); it('2D norm with axis=1 provided as number', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]); var norm = tf.norm(a, Infinity, 1); expect(norm.shape).toEqual([2]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3, 1]); }); it('axis=0,1 in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [0, 1]); expect(norm.shape).toEqual([]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('axis=0,1 keepDims in 2D array norm', function () { var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]); var norm = tf.norm(a, Infinity, [0, 1], true); expect(norm.shape).toEqual([1, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('3D norm axis=0,1, matrix inf norm', function () { var a = tf.tensor3d([1, 2, -3, 1, 0, 1], [3, 2, 1]); var norm = tf.norm(a, Infinity, [0, 1]); expect(norm.shape).toEqual([1]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [4]); }); it('axis=0,1 keepDims in 3D array norm', function () { var a = tf.tensor3d([1, 2, 3, 0, 0, 1], [3, 2, 1]); var norm = tf.norm(a, Infinity, [0, 1], true); expect(norm.shape).toEqual([1, 1, 1]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('axis=0,1 keepDims in 3D array norm', function () { var a = tf.tensor3d([1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1], [3, 2, 2]); var norm = tf.norm(a, Infinity, [0, 1], true); expect(norm.shape).toEqual([1, 1, 2]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [4, 3]); }); it('axis=null in 3D array norm', function () { var a = tf.tensor3d([1, 2, 3, 0, 0, 1], [3, 2, 1]); var norm = tf.norm(a, Infinity); expect(norm.shape).toEqual([]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('axis=null in 4D array norm', function () { var a = tf.tensor4d([1, 2, 3, 0, 0, 1], [3, 2, 1, 1]); var norm = tf.norm(a, Infinity); expect(norm.shape).toEqual([]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [3]); }); it('axis=0,1 in 4D array norm', function () { var a = tf.tensor4d([ 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1 ], [3, 2, 2, 2]); var norm = tf.norm(a, Infinity, [0, 1]); expect(norm.shape).toEqual([2, 2]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [4, 3, 4, 3]); }); it('axis=0,1 in 4D array norm', function () { var a = tf.tensor4d([ 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1 ], [3, 2, 2, 2]); var norm = tf.norm(a, Infinity, [0, 1], true); expect(norm.shape).toEqual([1, 1, 2, 2]); expect(norm.dtype).toBe('float32'); test_util_1.expectArraysClose(norm, [4, 3, 4, 3]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.norm({}); }) .toThrowError(/Argument 'x' passed to 'norm' must be a Tensor/); }); }); jasmine_util_1.describeWithFlags('unsortedSegmentSum', test_util_1.ALL_ENVS, function () { it('tensor1D', function () { var t = tf.tensor1d([1, 2, 3, 4]); var segmentIds = tf.tensor1d([0, 2, 0, 1], 'int32'); var numSegments = 3; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments); expect(res.shape).toEqual([3]); test_util_1.expectArraysClose(res, [4, 4, 2]); }); it('tensor2D axis=0', function () { var t = tf.tensor2d([1, 2, 3, 4], [2, 2]); var segmentIds = tf.tensor1d([0, 0], 'int32'); var numSegments = 2; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments); expect(res.shape).toEqual([2, 2]); test_util_1.expectArraysClose(res, [4, 6, 0, 0]); }); it('tensor2D axis=11', function () { var t = tf.tensor2d([1, 2, 3, 4], [2, 2]); var segmentIds = tf.tensor1d([0, 0], 'int32'); var numSegments = 2; var axis = 1; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments, axis); expect(res.shape).toEqual([2, 2]); test_util_1.expectArraysClose(res, [3, 0, 7, 0]); }); it('tensor3D axis=0', function () { var t = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [3, 2, 2]); var segmentIds = tf.tensor1d([2, 1, 2], 'int32'); var numSegments = 3; var axis = 0; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments, axis); expect(res.shape).toEqual([3, 2, 2]); test_util_1.expectArraysClose(res, [0, 0, 0, 0, 5, 6, 7, 8, 10, 12, 14, 16]); }); it('tensor3D axis=1', function () { var t = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 2]); var segmentIds = tf.tensor1d([2, 2, 2], 'int32'); var numSegments = 3; var axis = 1; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments, axis); expect(res.shape).toEqual([2, 3, 2]); test_util_1.expectArraysClose(res, [0, 0, 0, 0, 9, 12, 0, 0, 0, 0, 27, 30]); }); it('tensor3D axis=2', function () { var t = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 2]); var segmentIds = tf.tensor1d([0, 0], 'int32'); var numSegments = 2; var axis = 2; var res = tf.unsortedSegmentSum(t, segmentIds, numSegments, axis); expect(res.shape).toEqual([2, 3, 2]); test_util_1.expectArraysClose(res, [3, 0, 7, 0, 11, 0, 15, 0, 19, 0, 23, 0]); }); }); //# sourceMappingURL=reduction_ops_test.js.map