UNPKG

4.05 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import * as tf from '../index';
18import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
19import { expectArraysClose } from '../test_util';
20describeWithFlags('sinh', ALL_ENVS, () => {
21 it('basic', async () => {
22 const values = [1, -3, 2, -1, -4];
23 const a = tf.tensor1d(values);
24 const result = tf.sinh(a);
25 const expected = [];
26 for (let i = 0; i < a.size; i++) {
27 expected[i] = Math.sinh(values[i]);
28 }
29 expectArraysClose(await result.data(), expected);
30 });
31 it('propagates NaNs', async () => {
32 const a = tf.tensor1d([4, NaN, 0]);
33 const res = tf.sinh(a);
34 expectArraysClose(await res.data(), [Math.sinh(4), NaN, Math.sinh(0)]);
35 });
36 it('gradients: Scalar', async () => {
37 const a = tf.scalar(0.5);
38 const dy = tf.scalar(8);
39 const gradients = tf.grad(a => tf.sinh(a))(a, dy);
40 expect(gradients.shape).toEqual(a.shape);
41 expect(gradients.dtype).toEqual('float32');
42 expectArraysClose(await gradients.data(), [8 * Math.cosh(0.5)]);
43 });
44 it('gradient with clones', async () => {
45 const a = tf.scalar(0.5);
46 const dy = tf.scalar(8);
47 const gradients = tf.grad(a => tf.sinh(a.clone()).clone())(a, dy);
48 expect(gradients.shape).toEqual(a.shape);
49 expect(gradients.dtype).toEqual('float32');
50 expectArraysClose(await gradients.data(), [8 * Math.cosh(0.5)]);
51 });
52 it('gradients: Tensor1D', async () => {
53 const aValues = [-1, 2, 3, -5];
54 const dyValues = [1, 2, 3, 4];
55 const a = tf.tensor1d(aValues);
56 const dy = tf.tensor1d(dyValues);
57 const gradients = tf.grad(a => tf.sinh(a))(a, dy);
58 const expected = [];
59 for (let i = 0; i < a.size; i++) {
60 expected[i] = dyValues[i] * Math.cosh(aValues[i]);
61 }
62 expect(gradients.shape).toEqual(a.shape);
63 expect(gradients.dtype).toEqual('float32');
64 expectArraysClose(await gradients.data(), expected);
65 });
66 it('gradients: Tensor2D', async () => {
67 const aValues = [-3, 1, 2, 3];
68 const dyValues = [1, 2, 3, 4];
69 const a = tf.tensor2d(aValues, [2, 2]);
70 const dy = tf.tensor2d(dyValues, [2, 2]);
71 const gradients = tf.grad(a => tf.sinh(a))(a, dy);
72 const expected = [];
73 for (let i = 0; i < a.size; i++) {
74 expected[i] = dyValues[i] * Math.cosh(aValues[i]);
75 }
76 expect(gradients.shape).toEqual(a.shape);
77 expect(gradients.dtype).toEqual('float32');
78 expectArraysClose(await gradients.data(), expected);
79 });
80 it('throws when passed a non-tensor', () => {
81 expect(() => tf.sinh({}))
82 .toThrowError(/Argument 'x' passed to 'sinh' must be a Tensor/);
83 });
84 it('accepts a tensor-like object', async () => {
85 const values = [1, -3, 2, -1, -4];
86 const result = tf.sinh(values);
87 const expected = [];
88 for (let i = 0; i < values.length; i++) {
89 expected[i] = Math.sinh(values[i]);
90 }
91 expectArraysClose(await result.data(), expected);
92 });
93 it('throws for string tensor', () => {
94 expect(() => tf.sinh('q'))
95 .toThrowError(/Argument 'x' passed to 'sinh' must be numeric/);
96 });
97});
98//# sourceMappingURL=sinh_test.js.map
\No newline at end of file