UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

93 lines 3.94 kB
/** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import * as tf from '../index'; import { ALL_ENVS, describeWithFlags } from '../jasmine_util'; import { expectArraysClose } from '../test_util'; describeWithFlags('cos', ALL_ENVS, () => { it('basic', async () => { // Covers every 1/4pi range from -4pi to 4pi. const values = [1, 3, 4, 6, 7, 9, 10, 12, -1, -3, -4, -6, -7, -9, -10, -12]; const a = tf.tensor1d(values); const result = tf.cos(a); const expected = []; for (let i = 0; i < a.size; i++) { expected[i] = Math.cos(values[i]); } expectArraysClose(await result.data(), expected); }); it('propagates NaNs', async () => { const a = tf.tensor1d([4, NaN, 0]); const res = tf.cos(a); expectArraysClose(await res.data(), [Math.cos(4), NaN, Math.cos(0)]); }); it('gradients: Scalar', async () => { const a = tf.scalar(5); const dy = tf.scalar(8); const gradients = tf.grad(a => tf.cos(a))(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); expectArraysClose(await gradients.data(), [8 * Math.sin(5) * -1]); }); it('gradient with clones', async () => { const a = tf.scalar(5); const dy = tf.scalar(8); const gradients = tf.grad(a => tf.cos(a.clone()).clone())(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); expectArraysClose(await gradients.data(), [8 * Math.sin(5) * -1]); }); it('gradients: Tensor1D', async () => { const a = tf.tensor1d([-1, 2, 3, -5]); const dy = tf.tensor1d([1, 2, 3, 4]); const gradients = tf.grad(a => tf.cos(a))(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); expectArraysClose(await gradients.data(), [ 1 * Math.sin(-1) * -1, 2 * Math.sin(2) * -1, 3 * Math.sin(3) * -1, 4 * Math.sin(-5) * -1 ], 1e-1); }); it('gradients: Tensor2D', async () => { const a = tf.tensor2d([-3, 1, 2, 3], [2, 2]); const dy = tf.tensor2d([1, 2, 3, 4], [2, 2]); const gradients = tf.grad(a => tf.cos(a))(a, dy); expect(gradients.shape).toEqual(a.shape); expect(gradients.dtype).toEqual('float32'); expectArraysClose(await gradients.data(), [ 1 * Math.sin(-3) * -1, 2 * Math.sin(1) * -1, 3 * Math.sin(2) * -1, 4 * Math.sin(3) * -1 ], 1e-1); }); it('throws when passed a non-tensor', () => { expect(() => tf.cos({})) .toThrowError(/Argument 'x' passed to 'cos' must be a Tensor/); }); it('accepts a tensor-like object', async () => { const values = [1, -3, 2, 7, -4]; const result = tf.cos(values); const expected = []; for (let i = 0; i < values.length; i++) { expected[i] = Math.cos(values[i]); } expectArraysClose(await result.data(), expected); }); it('throws for string tensor', () => { expect(() => tf.cos('q')) .toThrowError(/Argument 'x' passed to 'cos' must be numeric/); }); }); //# sourceMappingURL=cos_test.js.map