UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

1,144 lines 106 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var environment_1 = require("../environment"); var tf = require("../index"); var jasmine_util_1 = require("../jasmine_util"); var test_util_1 = require("../test_util"); var util = require("../util"); var selu_util = require("./selu_util"); jasmine_util_1.describeWithFlags('relu', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1, -2, 0, 3, -0.1]); var result = tf.relu(a); test_util_1.expectArraysClose(result, [1, 0, 0, 3, 0]); }); it('5D', function () { var a = tf.tensor5d([1, -2, 5, -3], [1, 2, 2, 1, 1]); var result = tf.relu(a); test_util_1.expectArraysClose(result, [1, 0, 5, 0]); }); it('6D', function () { var a = tf.tensor6d([1, -2, 5, -3, -1, 4, 7, 8], [1, 2, 2, 2, 1, 1]); var result = tf.relu(a); test_util_1.expectArraysClose(result, [1, 0, 5, 0, 0, 4, 7, 8]); }); it('does nothing to positive values', function () { var a = tf.scalar(1); var result = tf.relu(a); test_util_1.expectNumbersClose(result.get(), 1); }); it('sets negative values to 0', function () { var a = tf.scalar(-1); var result = tf.relu(a); test_util_1.expectNumbersClose(result.get(), 0); }); it('preserves zero values', function () { var a = tf.scalar(0); var result = tf.relu(a); test_util_1.expectNumbersClose(result.get(), 0); }); it('propagates NaNs, float32', function () { var a = tf.tensor1d([1, -2, 0, 3, -0.1, NaN]); var result = tf.relu(a); expect(result.dtype).toBe('float32'); test_util_1.expectArraysClose(result, [1, 0, 0, 3, 0, NaN]); }); it('gradients: positive scalar', function () { var a = tf.scalar(3); var dy = tf.scalar(5); var grad = tf.grad(function (a) { return tf.relu(a); }); var da = grad(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [5]); }); it('gradients: negative scalar', function () { var a = tf.scalar(-3); var dy = tf.scalar(5); var grad = tf.grad(function (a) { return tf.relu(a); }); var da = grad(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [0]); }); it('gradients: array', function () { var a = tf.tensor2d([1, -1, 0, .1], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var grad = tf.grad(function (a) { return tf.relu(a); }); var da = grad(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [1, 0, 0, 4]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.relu({}); }) .toThrowError(/Argument 'x' passed to 'relu' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.relu([1, -2, 0, 3, -0.1]); test_util_1.expectArraysClose(result, [1, 0, 0, 3, 0]); }); it('throws for string tensor', function () { expect(function () { return tf.relu('q'); }) .toThrowError(/Argument 'x' passed to 'relu' must be numeric/); }); }); jasmine_util_1.describeWithFlags('abs', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1, -2, 0, 3, -0.1]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [1, 2, 0, 3, 0.1]); }); it('5D', function () { var a = tf.tensor5d([1, -2, 0, -3], [1, 2, 2, 1, 1]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [1, 2, 0, 3]); }); it('6D', function () { var a = tf.tensor6d([1, -2, 5, -3, -1, 4, 7, 8], [1, 2, 2, 2, 1, 1]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [1, 2, 5, 3, 1, 4, 7, 8]); }); it('complex64 rank-1', function () { var a = tf.complex([-2, -1, 0, 1, 2], [1, 2, 3, 0, -1]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [ Math.sqrt(-2 * -2 + 1 * 1), Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3), Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1) ]); expect(result.shape).toEqual([5]); }); it('complex64 rank-2', function () { var a = tf.complex([[-3, -2, -1], [0, 1, 2]], [[4, 1, 2], [3, 0, -1]]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [ Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1), Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3), Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1) ]); expect(result.shape).toEqual([2, 3]); }); it('complex64 rank-3', function () { var a = tf.complex([[[-3, -2], [-1, 0]], [[1, 2], [3, 4]]], [[[4, 1], [2, 3]], [[0, -1], [-3, -4]]]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [ Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1), Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3), Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1), Math.sqrt(3 * 3 + -3 * -3), Math.sqrt(4 * 4 + -4 * -4) ]); expect(result.shape).toEqual([2, 2, 2]); }); it('is underflow-safe for complex64', function () { var floatBits = environment_1.ENV.backend.floatPrecision(); var small; switch (floatBits) { case 32: small = 1e-30; break; case 16: small = 1e-4; break; default: throw new Error("Test not implemented for ENV.engine.floatPrecision()=" + floatBits + "."); } var a = tf.complex([small, 0, small, 0], [small, small, 0, 0]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [ Math.hypot(small, small), Math.hypot(0, small), Math.hypot(small, 0), Math.hypot(0, 0) ], small / 100); expect(result.shape).toEqual([4]); }); it('propagates NaNs', function () { var a = tf.tensor1d([1, -2, 0, 3, -0.1, NaN]); var result = tf.abs(a); test_util_1.expectArraysClose(result, [1, 2, 0, 3, 0.1, NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(4); var dy = tf.scalar(8); var da = tf.grad(function (a) { return tf.abs(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [8 * 1]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var da = tf.grad(function (a) { return tf.abs(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [1 * 1, 2 * 1, 3 * -1, 4 * 1]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([3, -1, -2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var da = tf.grad(function (a) { return tf.abs(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [1 * 1, 2 * -1, 3 * -1, 4 * 1]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.abs({}); }) .toThrowError(/Argument 'x' passed to 'abs' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.abs([1, -2, 0, 3, -0.1]); test_util_1.expectArraysClose(result, [1, 2, 0, 3, 0.1]); }); it('throws for string tensor', function () { expect(function () { return tf.abs('q'); }) .toThrowError(/Argument 'x' passed to 'abs' must be numeric/); }); }); jasmine_util_1.describeWithFlags('step', test_util_1.ALL_ENVS, function () { it('with 1d tensor', function () { var a = tf.tensor1d([1, -2, -.01, 3, -0.1]); var result = tf.step(a); test_util_1.expectArraysClose(result, [1, 0, 0, 1, 0]); }); it('with 1d tensor and alpha', function () { var a = tf.tensor1d([1, -2, -.01, 3, NaN]); var result = tf.step(a, 0.1); test_util_1.expectArraysClose(result, [1, 0.1, 0.1, 1, NaN]); }); it('with 2d tensor', function () { var a = tf.tensor2d([1, -5, -3, 4], [2, 2]); var result = tf.step(a); expect(result.shape).toEqual([2, 2]); test_util_1.expectArraysClose(result, [1, 0, 0, 1]); }); it('propagates NaNs', function () { var a = tf.tensor1d([1, -2, -.01, 3, NaN]); var result = tf.step(a); test_util_1.expectArraysClose(result, [1, 0, 0, 1, NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(-4); var dy = tf.scalar(8); var gradients = tf.grad(function (a) { return tf.step(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.step(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([3, -1, -2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.step(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.step({}); }) .toThrowError(/Argument 'x' passed to 'step' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.step([1, -2, -.01, 3, -0.1]); test_util_1.expectArraysClose(result, [1, 0, 0, 1, 0]); }); it('throws for string tensor', function () { expect(function () { return tf.step('q'); }) .toThrowError(/Argument 'x' passed to 'step' must be numeric/); }); }); jasmine_util_1.describeWithFlags('neg', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1, -3, 2, 7, -4]); var result = tf.neg(a); test_util_1.expectArraysClose(result, [-1, 3, -2, -7, 4]); }); it('propagate NaNs', function () { var a = tf.tensor1d([1, -3, 2, 7, NaN]); var result = tf.neg(a); var expected = [-1, 3, -2, -7, NaN]; test_util_1.expectArraysClose(result, expected); }); it('gradients: Scalar', function () { var a = tf.scalar(4); var dy = tf.scalar(8); var da = tf.grad(function (a) { return tf.neg(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [8 * -1]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var da = tf.grad(function (a) { return tf.neg(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [1 * -1, 2 * -1, 3 * -1, 4 * -1]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([3, -1, -2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var da = tf.grad(function (a) { return tf.neg(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [1 * -1, 2 * -1, 3 * -1, 4 * -1]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.neg({}); }) .toThrowError(/Argument 'x' passed to 'neg' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.neg([1, -3, 2, 7, -4]); test_util_1.expectArraysClose(result, [-1, 3, -2, -7, 4]); }); it('throws for string tensor', function () { expect(function () { return tf.neg('q'); }) .toThrowError(/Argument 'x' passed to 'neg' must be numeric/); }); }); jasmine_util_1.describeWithFlags('sigmoid', test_util_1.ALL_ENVS, function () { it('basic', function () { var values = [1, -3, 2, 7, -4]; var a = tf.tensor1d(values); var result = tf.sigmoid(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = 1 / (1 + Math.exp(-values[i])); } test_util_1.expectArraysClose(result, expected); }); it('6D', function () { var a = tf.ones([2, 2, 2, 2, 2, 2]); var result = tf.sigmoid(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = 1 / (1 + Math.exp(-1.0)); } test_util_1.expectArraysClose(result, expected); }); it('propagates NaNs', function () { var a = tf.tensor1d([3, NaN]); var res = tf.sigmoid(a); test_util_1.expectArraysClose(res, [1 / (1 + Math.exp(-3)), NaN]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var da = tf.grad(function (a) { return tf.sigmoid(a); })(a, dy); var expected = []; for (var i = 0; i < a.size; i++) { var y = 1 / (1 + Math.exp(-a.get(i))); expected[i] = dy.get(i) * y * (1 - y); } test_util_1.expectArraysClose(da, expected); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.sigmoid({}); }) .toThrowError(/Argument 'x' passed to 'sigmoid' must be a Tensor/); }); it('accepts a tensor-like object', function () { var values = [1, -3, 2, 7, -4]; var result = tf.sigmoid(values); var expected = []; for (var i = 0; i < values.length; i++) { expected[i] = 1 / (1 + Math.exp(-values[i])); } test_util_1.expectArraysClose(result, expected); }); it('throws for string tensor', function () { expect(function () { return tf.sigmoid('q'); }) .toThrowError(/Argument 'x' passed to 'sigmoid' must be numeric/); }); }); jasmine_util_1.describeWithFlags('logSigmoid', test_util_1.ALL_ENVS, function () { it('basic', function () { var values = [1, -3, 2, 7, -4]; var a = tf.tensor1d(values); var result = tf.logSigmoid(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = Math.log(1 / (1 + Math.exp(-values[i]))); } test_util_1.expectArraysClose(result, expected); }); it('scalar', function () { var a = tf.scalar(-2); var result = tf.logSigmoid(a); var expected = [Math.log(1 / (1 + Math.exp(2)))]; test_util_1.expectArraysClose(result, expected); }); it('tensor2D', function () { var values = [1, 2, -3, 5]; var a = tf.tensor2d(values, [2, 2]); var result = tf.logSigmoid(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = Math.log(1 / (1 + Math.exp(-values[i]))); } test_util_1.expectArraysClose(result, expected); }); it('larger magnitude negative inputs', function () { var values = [-100, -200, -3000]; var a = tf.tensor1d(values); var result = tf.logSigmoid(a); var expected = [-100, -200, -3000]; test_util_1.expectArraysClose(result, expected); }); it('larger magnitude positive inputs', function () { var values = [100, 200, 3000, 50000]; var a = tf.tensor1d(values); var result = tf.logSigmoid(a); var expected = [0, 0, 0, 0]; test_util_1.expectArraysClose(result, expected); }); it('propagates NaNs', function () { var a = tf.tensor1d([3, NaN]); var res = tf.logSigmoid(a); test_util_1.expectArraysClose(res, [Math.log(1 / (1 + Math.exp(-3))), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(3); var dy = tf.scalar(4); var da = tf.grad(function (a) { return tf.logSigmoid(a); })(a, dy).get(); var y = 1 / (1 + Math.exp(a.get())); test_util_1.expectNumbersClose(da, dy.get() * y); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var da = tf.grad(function (a) { return tf.logSigmoid(a); })(a, dy); var expected = []; for (var i = 0; i < a.size; i++) { var y = 1 / (1 + Math.exp(a.get(i))); expected[i] = dy.get(i) * y; } test_util_1.expectArraysClose(da, expected); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([1, 2, -3, 5], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var da = tf.grad(function (a) { return tf.logSigmoid(a); })(a, dy); var expected = []; var aVals = a.dataSync(); var dyVals = dy.dataSync(); for (var i = 0; i < a.size; i++) { var y = 1 / (1 + Math.exp(aVals[i])); expected[i] = dyVals[i] * y; } test_util_1.expectArraysClose(da, expected); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.logSigmoid({}); }) .toThrowError(/Argument 'x' passed to 'logSigmoid' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.logSigmoid(-2); var expected = [Math.log(1 / (1 + Math.exp(2)))]; test_util_1.expectArraysClose(result, expected); }); it('throws for string tensor', function () { expect(function () { return tf.logSigmoid('q'); }) .toThrowError(/Argument 'x' passed to 'logSigmoid' must be numeric/); }); }); jasmine_util_1.describeWithFlags('softplus', test_util_1.ALL_ENVS, function () { it('basic', function () { var values = [1, -3, 2, 7, -4]; var a = tf.tensor1d(values); var result = tf.softplus(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = Math.log((1 + Math.exp(values[i]))); } test_util_1.expectArraysClose(result, expected); }); it('scalar', function () { var a = tf.scalar(-2); var result = tf.softplus(a); var expected = [Math.log((1 + Math.exp(-2)))]; test_util_1.expectArraysClose(result, expected); }); it('tensor2D', function () { var values = [1, 2, -3, 5]; var a = tf.tensor2d(values, [2, 2]); var result = tf.softplus(a); var expected = []; for (var i = 0; i < a.size; i++) { expected[i] = Math.log((1 + Math.exp(values[i]))); } test_util_1.expectArraysClose(result, expected); }); it('larger magnitude negative inputs', function () { var values = [-100, -200, -3000, -50000]; var a = tf.tensor1d(values); var result = tf.softplus(a); var expected = [0, 0, 0, 0]; test_util_1.expectArraysClose(result, expected); }); it('larger magnitude positive inputs', function () { var values = [100, 200, 3000]; var a = tf.tensor1d(values); var result = tf.softplus(a); var expected = [100, 200, 3000]; test_util_1.expectArraysClose(result, expected); }); it('propagates NaNs', function () { var a = tf.tensor1d([3, NaN]); var res = tf.softplus(a); test_util_1.expectArraysClose(res, [Math.log((1 + Math.exp(3))), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(3); var dy = tf.scalar(4); var da = tf.grad(function (a) { return tf.softplus(a); })(a, dy); var y = 1 / (1 + Math.exp(-a.get())); test_util_1.expectNumbersClose(da.get(), dy.get() * y); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, -3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var da = tf.grad(function (a) { return tf.softplus(a); })(a, dy); var expected = []; for (var i = 0; i < a.size; i++) { var y = 1 / (1 + Math.exp(-a.get(i))); expected[i] = dy.get(i) * y; } test_util_1.expectArraysClose(da, expected); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([1, 2, -3, 5], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var da = tf.grad(function (a) { return tf.softplus(a); })(a, dy); var expected = []; var aVals = a.dataSync(); var dyVals = dy.dataSync(); for (var i = 0; i < a.size; i++) { var y = 1 / (1 + Math.exp(-aVals[i])); expected[i] = dyVals[i] * y; } test_util_1.expectArraysClose(da, expected); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.softplus({}); }) .toThrowError(/Argument 'x' passed to 'softplus' must be a Tensor/); }); it('accepts a tensor-like object', function () { var result = tf.softplus(-2); var expected = [Math.log((1 + Math.exp(-2)))]; test_util_1.expectArraysClose(result, expected); }); it('throws for string tensor', function () { expect(function () { return tf.softplus('q'); }) .toThrowError(/Argument 'x' passed to 'softplus' must be numeric/); }); }); jasmine_util_1.describeWithFlags('sqrt', test_util_1.ALL_ENVS, function () { it('sqrt', function () { var a = tf.tensor1d([2, 4]); var r = tf.sqrt(a); test_util_1.expectNumbersClose(r.get(0), Math.sqrt(2)); test_util_1.expectNumbersClose(r.get(1), Math.sqrt(4)); }); it('sqrt propagates NaNs', function () { var a = tf.tensor1d([1, NaN]); var r = tf.sqrt(a); test_util_1.expectArraysClose(r, [Math.sqrt(1), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(4); var dy = tf.scalar(8); var da = tf.grad(function (a) { return tf.sqrt(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [8 / (2 * Math.sqrt(4))]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, 3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.sqrt(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ 1 / (2 * Math.sqrt(1)), 2 / (2 * Math.sqrt(2)), 3 / (2 * Math.sqrt(3)), 4 / (2 * Math.sqrt(5)) ], 1e-1); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.sqrt(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ 1 / (2 * Math.sqrt(3)), 2 / (2 * Math.sqrt(1)), 3 / (2 * Math.sqrt(2)), 4 / (2 * Math.sqrt(3)) ], 1e-1); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.sqrt({}); }) .toThrowError(/Argument 'x' passed to 'sqrt' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.sqrt([2, 4]); test_util_1.expectNumbersClose(r.get(0), Math.sqrt(2)); test_util_1.expectNumbersClose(r.get(1), Math.sqrt(4)); }); it('throws for string tensor', function () { expect(function () { return tf.sqrt('q'); }) .toThrowError(/Argument 'x' passed to 'sqrt' must be numeric/); }); }); jasmine_util_1.describeWithFlags('rsqrt', test_util_1.ALL_ENVS, function () { it('rsqrt', function () { var a = tf.tensor1d([2, 4]); var r = tf.rsqrt(a); test_util_1.expectNumbersClose(r.get(0), 1 / Math.sqrt(2)); test_util_1.expectNumbersClose(r.get(1), 1 / Math.sqrt(4)); }); it('rsqrt propagates NaNs', function () { var a = tf.tensor1d([1, NaN]); var r = tf.rsqrt(a); test_util_1.expectArraysClose(r, [1 / Math.sqrt(1), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(4); var dy = tf.scalar(8); var da = tf.grad(function (a) { return tf.rsqrt(a); })(a, dy); expect(da.shape).toEqual(a.shape); expect(da.dtype).toEqual('float32'); test_util_1.expectArraysClose(da, [(-1 * 8) / (2 * Math.pow(4, 1.5))]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([1, 2, 3, 5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.rsqrt(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ -1 * 1 / (2 * Math.pow(1, 1.5)), -1 * 2 / (2 * Math.pow(2, 1.5)), -1 * 3 / (2 * Math.pow(3, 1.5)), -1 * 4 / (2 * Math.pow(5, 1.5)) ], 1e-1); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.rsqrt(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ -1 * 1 / (2 * Math.pow(3, 1.5)), -1 * 2 / (2 * Math.pow(1, 1.5)), -1 * 3 / (2 * Math.pow(2, 1.5)), -1 * 4 / (2 * Math.pow(3, 1.5)) ], 1e-1); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.rsqrt({}); }) .toThrowError(/Argument 'x' passed to 'rsqrt' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.rsqrt([2, 4]); test_util_1.expectNumbersClose(r.get(0), 1 / Math.sqrt(2)); test_util_1.expectNumbersClose(r.get(1), 1 / Math.sqrt(4)); }); it('throws for string tensor', function () { expect(function () { return tf.rsqrt('q'); }) .toThrowError(/Argument 'x' passed to 'rsqrt' must be numeric/); }); }); jasmine_util_1.describeWithFlags('square', test_util_1.ALL_ENVS, function () { it('1D array', function () { var a = tf.tensor1d([2, 4, Math.sqrt(2)]); var r = tf.square(a); test_util_1.expectArraysClose(r, [4, 16, 2]); }); it('2D array', function () { var a = tf.tensor2d([1, 2, Math.sqrt(2), Math.sqrt(3)], [2, 2]); var r = tf.square(a); expect(r.shape).toEqual([2, 2]); test_util_1.expectArraysClose(r, [1, 4, 2, 3]); }); it('5D array', function () { var a = tf.tensor5d([1, 2, Math.sqrt(2), Math.sqrt(3)], [1, 1, 2, 2, 1]); var r = tf.square(a); expect(r.shape).toEqual([1, 1, 2, 2, 1]); test_util_1.expectArraysClose(r, [1, 4, 2, 3]); }); it('6D array', function () { var a = tf.tensor6d([1, 2, Math.sqrt(2), Math.sqrt(3), 3, 4, Math.sqrt(7), Math.sqrt(13)], [1, 1, 2, 2, 2, 1]); var r = tf.square(a); expect(r.shape).toEqual(a.shape); test_util_1.expectArraysClose(r, [1, 4, 2, 3, 9, 16, 7, 13]); }); it('square propagates NaNs', function () { var a = tf.tensor1d([1.5, NaN]); var r = tf.square(a); test_util_1.expectArraysClose(r, [2.25, NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(5); var dy = tf.scalar(8); var gradients = tf.grad(function (a) { return tf.square(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [2 * 5 * 8]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.square(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [-2, 4 * 2, 6 * 3, -10 * 4]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.square(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [-6 * 1, 2 * 2, 4 * 3, 6 * 4]); }); it('gradients: Tensor5D', function () { var a = tf.tensor5d([-3, 1, 2, 3], [1, 1, 1, 2, 2]); var dy = tf.tensor5d([1, 2, 3, 4], [1, 1, 1, 2, 2]); var gradients = tf.grad(function (a) { return tf.square(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [-6 * 1, 2 * 2, 4 * 3, 6 * 4]); }); it('gradients: Tensor6D', function () { var a = tf.tensor6d([-3, 1, 2, 3, -4, 5, 12, 3], [1, 1, 1, 2, 2, 2]); var dy = tf.tensor6d([1, 2, 3, 4, 5, 6, 7, 8], [1, 1, 1, 2, 2, 2]); var gradients = tf.grad(function (a) { return tf.square(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [-6 * 1, 2 * 2, 4 * 3, 6 * 4, -8 * 5, 10 * 6, 24 * 7, 6 * 8]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.square({}); }) .toThrowError(/Argument 'x' passed to 'square' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.square([2, 4, Math.sqrt(2)]); test_util_1.expectArraysClose(r, [4, 16, 2]); }); it('throws for string tensor', function () { expect(function () { return tf.square('q'); }) .toThrowError(/Argument 'x' passed to 'square' must be numeric/); }); }); jasmine_util_1.describeWithFlags('reciprocal', test_util_1.ALL_ENVS, function () { it('1D array', function () { var a = tf.tensor1d([2, 3, 0, NaN]); var r = tf.reciprocal(a); test_util_1.expectArraysClose(r, [1 / 2, 1 / 3, Infinity, NaN]); }); it('2D array', function () { var a = tf.tensor2d([1, Infinity, 0, NaN], [2, 2]); var r = tf.reciprocal(a); expect(r.shape).toEqual([2, 2]); test_util_1.expectArraysClose(r, [1 / 1, 0, Infinity, NaN]); }); it('reciprocal propagates NaNs', function () { var a = tf.tensor1d([1.5, NaN]); var r = tf.reciprocal(a); test_util_1.expectArraysClose(r, [1 / 1.5, NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(5); var dy = tf.scalar(8); var gradients = tf.grad(function (a) { return tf.reciprocal(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [-1 * 8 * (1 / (5 * 5))]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.reciprocal(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ -1 * 1 * (1 / (-1 * -1)), -1 * 2 * (1 / (2 * 2)), -1 * 3 * (1 / (3 * 3)), -1 * 4 * (1 / (-5 * -5)) ]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-1, 2, 3, -5], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.reciprocal(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [ -1 * 1 * (1 / (-1 * -1)), -1 * 2 * (1 / (2 * 2)), -1 * 3 * (1 / (3 * 3)), -1 * 4 * (1 / (-5 * -5)) ]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.reciprocal({}); }) .toThrowError(/Argument 'x' passed to 'reciprocal' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.reciprocal([2, 3, 0, NaN]); test_util_1.expectArraysClose(r, [1 / 2, 1 / 3, Infinity, NaN]); }); it('throws for string tensor', function () { expect(function () { return tf.reciprocal('q'); }) .toThrowError(/Argument 'x' passed to 'reciprocal' must be numeric/); }); }); jasmine_util_1.describeWithFlags('log', test_util_1.ALL_ENVS, function () { it('log', function () { var a = tf.tensor1d([1, 2]); var r = tf.log(a); test_util_1.expectNumbersClose(r.get(0), Math.log(1)); test_util_1.expectNumbersClose(r.get(1), Math.log(2)); }); it('log 6D', function () { var a = tf.range(1, 65).reshape([2, 2, 2, 2, 2, 2]); var r = tf.log(a); var expectedResult = []; for (var i = 1; i < 65; i++) { expectedResult[i - 1] = Math.log(i); } test_util_1.expectArraysClose(r, expectedResult); }); it('log propagates NaNs', function () { var a = tf.tensor1d([1, NaN]); var r = tf.log(a); test_util_1.expectArraysClose(r, [Math.log(1), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(5); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.log(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [3 / 5]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.log(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 / -1, 2 / 2, 3 / 3, 4 / -5]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.log(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 / -3, 2 / 1, 3 / 2, 4 / 3]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.log({}); }) .toThrowError(/Argument 'x' passed to 'log' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.log([1, 2]); test_util_1.expectNumbersClose(r.get(0), Math.log(1)); test_util_1.expectNumbersClose(r.get(1), Math.log(2)); }); it('throws for string tensor', function () { expect(function () { return tf.log('q'); }) .toThrowError(/Argument 'x' passed to 'log' must be numeric/); }); }); jasmine_util_1.describeWithFlags('log1p', test_util_1.ALL_ENVS, function () { it('log1p', function () { var a = tf.tensor1d([1, 2]); var r = tf.log1p(a); test_util_1.expectNumbersClose(r.get(0), Math.log1p(1)); test_util_1.expectNumbersClose(r.get(1), Math.log1p(2)); }); it('log1p propagates NaNs', function () { var a = tf.tensor1d([1, NaN]); var r = tf.log1p(a); test_util_1.expectArraysClose(r, [Math.log1p(1), NaN]); }); it('gradients: Scalar', function () { var a = tf.scalar(5); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.log1p(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [3 / (1 + 5)]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.log1p(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [Infinity, 2 / (1 + 2), 3 / (1 + 3), 4 / (1 + -5)]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.log1p(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 / (1 + -3), 2 / (1 + 1), 3 / (1 + 2), 4 / (1 + 3)]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.log1p({}); }) .toThrowError(/Argument 'x' passed to 'log1p' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.log1p([1, 2]); test_util_1.expectNumbersClose(r.get(0), Math.log1p(1)); test_util_1.expectNumbersClose(r.get(1), Math.log1p(2)); }); it('throws for string tensor', function () { expect(function () { return tf.log1p('q'); }) .toThrowError(/Argument 'x' passed to 'log1p' must be numeric/); }); }); jasmine_util_1.describeWithFlags('ceil', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1.5, 2.1, -1.4]); var r = tf.ceil(a); test_util_1.expectNumbersClose(r.get(0), 2); test_util_1.expectNumbersClose(r.get(1), 3); test_util_1.expectNumbersClose(r.get(2), -1); }); it('propagates NaNs', function () { var a = tf.tensor1d([1.5, NaN, -1.4]); var r = tf.ceil(a); test_util_1.expectArraysClose(r, [2, NaN, -1]); }); it('gradients: Scalar', function () { var a = tf.scalar(5.2); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.ceil(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1.1, 2.6, 3, -5.9]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.ceil(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2.2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.ceil(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.ceil({}); }) .toThrowError(/Argument 'x' passed to 'ceil' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.ceil([1.5, 2.1, -1.4]); test_util_1.expectNumbersClose(r.get(0), 2); test_util_1.expectNumbersClose(r.get(1), 3); test_util_1.expectNumbersClose(r.get(2), -1); }); it('throws for string tensor', function () { expect(function () { return tf.ceil('q'); }) .toThrowError(/Argument 'x' passed to 'ceil' must be numeric/); }); }); jasmine_util_1.describeWithFlags('floor', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1.5, 2.1, -1.4]); var r = tf.floor(a); test_util_1.expectNumbersClose(r.get(0), 1); test_util_1.expectNumbersClose(r.get(1), 2); test_util_1.expectNumbersClose(r.get(2), -2); }); it('propagates NaNs', function () { var a = tf.tensor1d([1.5, NaN, -1.4]); var r = tf.floor(a); test_util_1.expectArraysClose(r, [1, NaN, -2]); }); it('gradients: Scalar', function () { var a = tf.scalar(5.2); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.floor(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1.1, 2.6, 3, -5.9]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.floor(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2.2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.floor(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.floor({}); }) .toThrowError(/Argument 'x' passed to 'floor' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.floor([1.5, 2.1, -1.4]); test_util_1.expectNumbersClose(r.get(0), 1); test_util_1.expectNumbersClose(r.get(1), 2); test_util_1.expectNumbersClose(r.get(2), -2); }); it('throws for string tensor', function () { expect(function () { return tf.floor('q'); }) .toThrowError(/Argument 'x' passed to 'floor' must be numeric/); }); }); jasmine_util_1.describeWithFlags('sign', test_util_1.ALL_ENVS, function () { it('basic', function () { var a = tf.tensor1d([1.5, 0, NaN, -1.4]); var r = tf.sign(a); test_util_1.expectNumbersClose(r.get(0), 1); test_util_1.expectNumbersClose(r.get(1), 0); test_util_1.expectNumbersClose(r.get(2), 0); test_util_1.expectNumbersClose(r.get(3), -1); }); it('propagates NaNs', function () { var a = tf.tensor1d([1.5, NaN, -1.4]); var r = tf.sign(a); test_util_1.expectArraysClose(r, [1, 0, -1]); }); it('gradients: Scalar', function () { var a = tf.scalar(5.2); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.sign(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1.1, 2.6, 3, -5.9]); var dy = tf.tensor1d([-1, 1, 1, -1]); var gradients = tf.grad(function (a) { return tf.sign(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2.2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.sign(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [0, 0, 0, 0]); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.sign({}); }) .toThrowError(/Argument 'x' passed to 'sign' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.sign([1.5, 0, NaN, -1.4]); test_util_1.expectNumbersClose(r.get(0), 1); test_util_1.expectNumbersClose(r.get(1), 0); test_util_1.expectNumbersClose(r.get(2), 0); test_util_1.expectNumbersClose(r.get(3), -1); }); it('throws for string tensor', function () { expect(function () { return tf.sign('q'); }) .toThrowError(/Argument 'x' passed to 'sign' must be numeric/); }); }); jasmine_util_1.describeWithFlags('exp', test_util_1.ALL_ENVS, function () { it('exp', function () { var a = tf.tensor1d([1, 2, 0]); var r = tf.exp(a); test_util_1.expectNumbersClose(r.get(0), Math.exp(1)); test_util_1.expectNumbersClose(r.get(1), Math.exp(2)); test_util_1.expectNumbersClose(r.get(2), 1); }); it('exp propagates NaNs', function () { var a = tf.tensor1d([1, NaN, 0]); var r = tf.exp(a); test_util_1.expectArraysClose(r, [Math.exp(1), NaN, 1]); }); it('gradients: Scalar', function () { var a = tf.scalar(0.5); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.exp(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [3 * Math.exp(0.5)]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.exp(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 * Math.exp(-1), 2 * Math.exp(2), 3 * Math.exp(3), 4 * Math.exp(-5)], 1e-1); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); var gradients = tf.grad(function (a) { return tf.exp(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 * Math.exp(-3), 2 * Math.exp(1), 3 * Math.exp(2), 4 * Math.exp(3)], 1e-1); }); it('throws when passed a non-tensor', function () { expect(function () { return tf.exp({}); }) .toThrowError(/Argument 'x' passed to 'exp' must be a Tensor/); }); it('accepts a tensor-like object', function () { var r = tf.exp([1, 2, 0]); test_util_1.expectNumbersClose(r.get(0), Math.exp(1)); test_util_1.expectNumbersClose(r.get(1), Math.exp(2)); test_util_1.expectNumbersClose(r.get(2), 1); }); it('throws for string tensor', function () { expect(function () { return tf.exp('q'); }) .toThrowError(/Argument 'x' passed to 'exp' must be numeric/); }); }); jasmine_util_1.describeWithFlags('expm1', test_util_1.ALL_ENVS, function () { it('expm1', function () { var a = tf.tensor1d([1, 2, 0]); var r = tf.expm1(a); test_util_1.expectNumbersClose(r.get(0), Math.expm1(1)); test_util_1.expectNumbersClose(r.get(1), Math.expm1(2)); test_util_1.expectNumbersClose(r.get(2), Math.expm1(0)); }); it('expm1 propagates NaNs', function () { var a = tf.tensor1d([1, NaN, 0]); var r = tf.expm1(a); test_util_1.expectArraysClose(r, [Math.expm1(1), NaN, Math.expm1(0)]); }); it('gradients: Scalar', function () { var a = tf.scalar(0.5); var dy = tf.scalar(3); var gradients = tf.grad(function (a) { return tf.expm1(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [3 * Math.exp(0.5)]); }); it('gradients: Tensor1D', function () { var a = tf.tensor1d([-1, 2, 3, -5]); var dy = tf.tensor1d([1, 2, 3, 4]); var gradients = tf.grad(function (a) { return tf.expm1(a); })(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); test_util_1.expectArraysClose(gradients, [1 * Math.exp(-1), 2 * Math.exp(2), 3 * Math.exp(3), 4 * Math.exp(-5)], 1e-1); }); it('gradients: Tensor2D', function () { var a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); var dy = tf.tensor2d