@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
1,134 lines • 59.3 kB
JavaScript
"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('Reduction: 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/);
});
it('accepts a tensor-like object', function () {
test_util_1.expectNumbersClose(tf.min([3, -1, 0, 100, -7, 2]).get(), -7);
});
it('min gradient: Scalar', function () {
var x = tf.scalar(42);
var dy = tf.scalar(-1);
var gradients = tf.grad(function (v) { return tf.min(v); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.scalar(-1));
});
it('min gradient: 1D, ties', function () {
var x = tf.tensor1d([-1, -3, -7, -7]);
var dy = tf.scalar(-1);
var gradients = tf.grad(function (v) { return tf.min(v); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor1d([0, 0, -1, -1]));
});
it('min gradient: 2D, axes=-1, keepDims=false', function () {
var x = tf.tensor2d([[-0, -20, -10], [10, 30, 20]]);
var dy = tf.tensor1d([-1, -1]);
var axis = -1;
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, 0], [-1, 0, 0]]));
});
it('min gradient: ties, 2D, axes=-1, keepDims=false', function () {
var x = tf.tensor2d([[0, -20, -20], [10, 30, 10]]);
var dy = tf.tensor1d([-1, -1]);
var axis = -1;
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, -1], [-1, 0, -1]]));
});
it('min gradient: 2D, axes=0, keepDims=false', function () {
var x = tf.tensor2d([[0, 20, 10], [-10, -30, 20]]);
var dy = tf.tensor1d([-1, -1, -1]);
var axis = 0;
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[-1, -1, 0], [0, 0, -1]]));
});
it('min gradient: 2D, axes=-1, keepDims=true', function () {
var x = tf.tensor2d([[0, -20, -10], [10, 30, 20]]);
var dy = tf.tensor2d([[-1], [-1]]);
var axis = -1;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.min(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, 0], [-1, 0, 0]]));
});
it('min gradient: 2D, axes=0, keepDims=true', function () {
var x = tf.tensor2d([[0, -20, -10], [10, 30, -20]]);
var dy = tf.tensor2d([[-1, -1, -1]]);
var axis = 0;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.min(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[-1, -1, 0], [0, 0, -1]]));
});
it('min gradient: 3D, axes=[1, 2], keepDims=false', function () {
var x = tf.tensor3d([[[0, -20], [-10, -15]], [[10, 30], [20, 15]]]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2];
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, 0]], [[-1, 0], [0, 0]]]));
});
it('min gradient: ties, 3D, axes=[1, 2], keepDims=false', function () {
var x = tf.tensor3d([[[0, -20], [-20, -20]], [[10, 30], [10, 15]]]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2];
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [-1, -1]], [[-1, 0], [-1, 0]]]));
});
it('min gradient: 3D, axes=2, keepDims=false', function () {
var x = tf.tensor3d([[[0, -20], [-10, -15]], [[10, 30], [20, 15]]]);
var dy = tf.tensor2d([[-1, -1], [-1, -1]]);
var axis = 2;
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, -1]], [[-1, 0], [0, -1]]]));
});
it('min gradient: 3D, axes=2, keepDims=true', function () {
var x = tf.tensor3d([[[0, -20], [-10, -15]], [[10, 30], [20, 15]]]);
var dy = tf.tensor3d([[[-1], [-1]], [[-1], [-1]]]);
var axis = 2;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.min(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, -1]], [[-1, 0], [0, -1]]]));
});
it('min gradient: ties, 4D, axes=[1, 2, 3], keepDims=false', function () {
var x = tf.tensor4d([
[[[0, -20], [-20, -20]], [[10, 30], [10, 30]]],
[[[0, 20], [20, 20]], [[-10, -30], [-10, -30]]]
]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2, 3];
var gradients = tf.grad(function (v) { return tf.min(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor4d([
[[[0, -1], [-1, -1]], [[0, 0], [0, 0]]],
[[[0, 0], [0, 0]], [[0, -1], [0, -1]]]
]));
});
it('min gradient: ties, 4D, axes=[2, 3], keepDims=true', function () {
var x = tf.tensor4d([
[[[0, -20], [-20, -20]], [[10, 30], [10, 30]]],
[[[0, 20], [20, 20]], [[-10, -30], [-10, -30]]]
]);
var dy = tf.tensor4d([[[[-1]], [[-2]]], [[[-3]], [[-4]]]]);
var axis = [2, 3];
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.min(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor4d([
[[[0, -1], [-1, -1]], [[-2, 0], [-2, 0]]],
[[[-3, 0], [0, 0]], [[0, -4], [0, -4]]]
]));
});
it('throws error for string tensor', function () {
expect(function () { return tf.min(['a']); })
.toThrowError(/Argument 'x' passed to 'min' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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 () {
test_util_1.expectNumbersClose(tf.max(tf.tensor1d([3, NaN, 2])).get(), 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('6D, axis=[5]', function () {
var a = tf.range(0, 64).reshape([2, 2, 2, 2, 2, 2]);
var r = tf.max(a, [5]);
var expectedResult = [
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63
];
test_util_1.expectArraysClose(r, expectedResult);
});
it('throws when passed a non-tensor', function () {
expect(function () { return tf.max({}); })
.toThrowError(/Argument 'x' passed to 'max' must be a Tensor/);
});
it('accepts a tensor-like object', function () {
var r = tf.max([3, -1, 0, 100, -7, 2]);
test_util_1.expectNumbersClose(r.get(), 100);
});
it('max gradient: Scalar', function () {
var x = tf.scalar(42);
var dy = tf.scalar(-1);
var gradients = tf.grad(function (v) { return tf.max(v); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.scalar(-1));
});
it('max gradient: 1D, ties', function () {
var x = tf.tensor1d([1, 3, 7, 7]);
var dy = tf.scalar(-1);
var gradients = tf.grad(function (v) { return tf.max(v); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor1d([0, 0, -1, -1]));
});
it('max gradient: 2D, axes=-1, keepDims=false', function () {
var x = tf.tensor2d([[0, 20, 10], [-10, -30, -20]]);
var dy = tf.tensor1d([-1, -1]);
var axis = -1;
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, 0], [-1, 0, 0]]));
});
it('max gradient: ties, 2D, axes=-1, keepDims=false', function () {
var x = tf.tensor2d([[0, 20, 20], [-10, -30, -10]]);
var dy = tf.tensor1d([-1, -1]);
var axis = -1;
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, -1], [-1, 0, -1]]));
});
it('max gradient: 2D, axes=0, keepDims=false', function () {
var x = tf.tensor2d([[0, 20, 10], [-10, -30, 20]]);
var dy = tf.tensor1d([-1, -1, -1]);
var axis = 0;
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[-1, -1, 0], [0, 0, -1]]));
});
it('max gradient: 2D, axes=-1, keepDims=true', function () {
var x = tf.tensor2d([[0, 20, 10], [-10, -30, -20]]);
var dy = tf.tensor2d([[-1], [-1]]);
var axis = -1;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.max(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[0, -1, 0], [-1, 0, 0]]));
});
it('max gradient: 2D, axes=0, keepDims=true', function () {
var x = tf.tensor2d([[0, 20, 10], [-10, -30, 20]]);
var dy = tf.tensor2d([[-1, -1, -1]]);
var axis = 0;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.max(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor2d([[-1, -1, 0], [0, 0, -1]]));
});
it('max gradient: 3D, axes=[1, 2], keepDims=false', function () {
var x = tf.tensor3d([[[0, 20], [10, 15]], [[-10, -30], [-20, -15]]]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2];
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, 0]], [[-1, 0], [0, 0]]]));
});
it('max gradient: ties, 3D, axes=[1, 2], keepDims=false', function () {
var x = tf.tensor3d([[[0, 20], [20, 20]], [[-10, -30], [-10, -15]]]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2];
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [-1, -1]], [[-1, 0], [-1, 0]]]));
});
it('max gradient: 3D, axes=2, keepDims=false', function () {
var x = tf.tensor3d([[[0, 20], [10, 15]], [[-10, -30], [-20, -15]]]);
var dy = tf.tensor2d([[-1, -1], [-1, -1]]);
var axis = 2;
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, -1]], [[-1, 0], [0, -1]]]));
});
it('max gradient: 3D, axes=2, keepDims=true', function () {
var x = tf.tensor3d([[[0, 20], [10, 15]], [[-10, -30], [-20, -15]]]);
var dy = tf.tensor3d([[[-1], [-1]], [[-1], [-1]]]);
var axis = 2;
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.max(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor3d([[[0, -1], [0, -1]], [[-1, 0], [0, -1]]]));
});
it('max gradient: ties, 4D, axes=[1, 2, 3], keepDims=false', function () {
var x = tf.tensor4d([
[[[0, 20], [20, 20]], [[-10, -30], [-10, -30]]],
[[[0, -20], [-20, -20]], [[10, 30], [10, 30]]]
]);
var dy = tf.tensor1d([-1, -1]);
var axis = [1, 2, 3];
var gradients = tf.grad(function (v) { return tf.max(v, axis); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor4d([
[[[0, -1], [-1, -1]], [[0, 0], [0, 0]]],
[[[0, 0], [0, 0]], [[0, -1], [0, -1]]]
]));
});
it('max gradient: ties, 4D, axes=[2, 3], keepDims=true', function () {
var x = tf.tensor4d([
[[[0, 20], [20, 20]], [[-10, -30], [-10, -30]]],
[[[0, -20], [-20, -20]], [[10, 30], [10, 30]]]
]);
var dy = tf.tensor4d([[[[-1]], [[-2]]], [[[-3]], [[-4]]]]);
var axis = [2, 3];
var keepDims = true;
var gradients = tf.grad(function (v) { return tf.max(v, axis, keepDims); })(x, dy);
test_util_1.expectArraysClose(gradients, tf.tensor4d([
[[[0, -1], [-1, -1]], [[-2, 0], [-2, 0]]],
[[[-3, 0], [0, 0]], [[0, -4], [0, -4]]]
]));
});
it('throws error for string tensor', function () {
expect(function () { return tf.max(['a']); })
.toThrowError(/Argument 'x' passed to 'max' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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('max index corresponds to start of a non-initial window', function () {
var n = reduce_util.PARALLELIZE_THRESHOLD * 2;
var windowSize = reduce_util.computeOptimalWindowSize(n);
var values = new Float32Array(n);
var index = windowSize * 2;
values[index] = 1;
var a = tf.tensor1d(values);
var result = tf.argMax(a);
expect(result.dtype).toBe('int32');
expect(result.get()).toBe(index);
});
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/);
});
it('accepts a tensor-like object', function () {
var result = tf.argMax([1, 0, 3, 2]);
expect(result.dtype).toBe('int32');
expect(result.get()).toBe(2);
});
it('accepts tensor with bool values', function () {
var t = tf.tensor1d([0, 1], 'bool');
var result = tf.argMax(t);
expect(result.dtype).toBe('int32');
expect(result.get()).toBe(1);
});
it('has gradient', function () {
var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
var dy = tf.ones([3], 'float32');
var da = tf.grad(function (x) { return tf.argMax(x); })(a, dy);
expect(da.dtype).toBe('float32');
expect(da.shape).toEqual([2, 3]);
test_util_1.expectArraysClose(da, [0, 0, 0, 0, 0, 0]);
});
it('throws error for string tensor', function () {
expect(function () { return tf.argMax(['a']); })
.toThrowError(/Argument 'x' passed to 'argMax' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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('min index corresponds to start of a non-initial window', function () {
var n = reduce_util.PARALLELIZE_THRESHOLD * 2;
var windowSize = reduce_util.computeOptimalWindowSize(n);
var values = new Float32Array(n);
var index = windowSize * 2;
values[index] = -1;
var a = tf.tensor1d(values);
var result = tf.argMin(a);
expect(result.dtype).toBe('int32');
expect(result.get()).toBe(index);
});
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/);
});
it('accepts a tensor-like object', function () {
var result = tf.argMin([1, 0, 3, 2]);
expect(result.get()).toBe(1);
});
it('accepts tensor with bool values', function () {
var t = tf.tensor1d([0, 1], 'bool');
var result = tf.argMin(t);
expect(result.dtype).toBe('int32');
expect(result.get()).toBe(0);
});
it('has gradient', function () {
var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
var dy = tf.ones([3], 'float32');
var da = tf.grad(function (x) { return tf.argMin(x); })(a, dy);
expect(da.dtype).toBe('float32');
expect(da.shape).toEqual([2, 3]);
test_util_1.expectArraysClose(da, [0, 0, 0, 0, 0, 0]);
});
it('throws error for string tensor', function () {
expect(function () { return tf.argMin(['a']); })
.toThrowError(/Argument 'x' passed to 'argMin' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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/);
});
it('accepts a tensor-like object', function () {
var result = tf.logSumExp([1, 2, -3]);
test_util_1.expectNumbersClose(result.get(), Math.log(Math.exp(1) + Math.exp(2) + Math.exp(-3)));
});
it('throws error for string tensor', function () {
expect(function () { return tf.logSumExp(['a']); })
.toThrowError(/Argument 'x' passed to 'logSumExp' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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/);
});
it('accepts a tensor-like object', function () {
var result = tf.sum([[1, 2], [3, 0], [0, 1]]);
test_util_1.expectNumbersClose(result.get(), 7);
});
it('throws error for string tensor', function () {
expect(function () { return tf.sum(['a']); })
.toThrowError(/Argument 'x' passed to 'sum' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: prod', test_util_1.ALL_ENVS, function () {
it('basic', function () {
var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
var result = tf.prod(a);
test_util_1.expectNumbersClose(result.get(), 0);
});
it('propagates NaNs', function () {
var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]);
expect(tf.prod(a).get()).toEqual(NaN);
});
it('prod over dtype int32', function () {
var a = tf.tensor1d([1, 5, 7, 3], 'int32');
var prod = tf.prod(a);
expect(prod.get()).toBe(105);
});
it('prod over dtype bool', function () {
var a = tf.tensor1d([true, false, false, true, true], 'bool');
var prod = tf.prod(a);
expect(prod.get()).toBe(0);
});
it('prods all values in 2D array with keep dim', function () {
var a = tf.tensor2d([1, 2, 3, 1, 0, 1], [3, 2]);
var res = tf.prod(a, null, true);
expect(res.shape).toEqual([1, 1]);
test_util_1.expectArraysClose(res, [0]);
});
it('prods across axis=0 in 2D array', function () {
var a = tf.tensor2d([1, 2, 3, 1, 0, 1], [3, 2]);
var res = tf.prod(a, [0]);
expect(res.shape).toEqual([2]);
test_util_1.expectArraysClose(res, [0, 2]);
});
it('prods across axis=0 in 2D array, keepDims', function () {
var a = tf.tensor2d([1, 2, 3, 1, 0, 1], [3, 2]);
var res = tf.prod(a, [0], true);
expect(res.shape).toEqual([1, 2]);
test_util_1.expectArraysClose(res, [0, 2]);
});
it('prods across axis=1 in 2D array', function () {
var a = tf.tensor2d([1, 2, 3, 1, 1, 1], [3, 2]);
var res = tf.prod(a, [1]);
expect(res.shape).toEqual([3]);
test_util_1.expectArraysClose(res, [2, 3, 1]);
});
it('2D, axis=1 provided as number', function () {
var a = tf.tensor2d([1, 2, 3, 1, 1, 1], [2, 3]);
var res = tf.prod(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, 1, 1, 1], [2, 3]);
var res = tf.prod(a, -1);
expect(res.shape).toEqual([2]);
test_util_1.expectArraysClose(res, [6, 1]);
});
it('prods across axis=0,1 in 2D array', function () {
var a = tf.tensor2d([1, 2, 3, 1, 1, 1], [3, 2]);
var res = tf.prod(a, [0, 1]);
expect(res.shape).toEqual([]);
test_util_1.expectArraysClose(res, [6]);
});
it('2D, axis=[-1,-2] in 2D array', function () {
var a = tf.tensor2d([1, 2, 3, 1, 1, 1], [3, 2]);
var res = tf.prod(a, [-1, -2]);
expect(res.shape).toEqual([]);
test_util_1.expectArraysClose(res, [6]);
});
it('throws when passed a non-tensor', function () {
expect(function () { return tf.prod({}); })
.toThrowError(/Argument 'x' passed to 'prod' must be a Tensor/);
});
it('accepts a tensor-like object', function () {
var result = tf.prod([[1, 2], [3, 1], [1, 1]]);
test_util_1.expectNumbersClose(result.get(), 6);
});
it('throws error for string tensor', function () {
expect(function () { return tf.prod(['a']); })
.toThrowError(/Argument 'x' passed to 'prod' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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/);
});
it('accepts a tensor-like object', function () {
var r = tf.mean([[1, 2, 3], [0, 0, 1]]);
expect(r.dtype).toBe('float32');
test_util_1.expectNumbersClose(r.get(), 7 / 6);
});
it('throws error for string tensor', function () {
expect(function () { return tf.mean(['a']); })
.toThrowError(/Argument 'x' passed to 'mean' must be numeric tensor/);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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/);
});
it('accepts a tensor-like object', function () {
var _a = tf.moments([1, 2, 3, 0, 0, 1]), 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);
});
});
jasmine_util_1.describeWithFlags('Reduction: 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('float