UNPKG

3.59 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc. 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('log', ALL_ENVS, () => {
21 it('log', async () => {
22 const a = tf.tensor1d([1, 2]);
23 const r = tf.log(a);
24 expectArraysClose(await r.data(), [Math.log(1), Math.log(2)]);
25 });
26 it('log 6D', async () => {
27 const a = tf.range(1, 65).reshape([2, 2, 2, 2, 2, 2]);
28 const r = tf.log(a);
29 const expectedResult = [];
30 for (let i = 1; i < 65; i++) {
31 expectedResult[i - 1] = Math.log(i);
32 }
33 expectArraysClose(await r.data(), expectedResult);
34 });
35 it('log propagates NaNs', async () => {
36 const a = tf.tensor1d([1, NaN]);
37 const r = tf.log(a);
38 expectArraysClose(await r.data(), [Math.log(1), NaN]);
39 });
40 it('gradients: Scalar', async () => {
41 const a = tf.scalar(5);
42 const dy = tf.scalar(3);
43 const gradients = tf.grad(a => tf.log(a))(a, dy);
44 expect(gradients.shape).toEqual(a.shape);
45 expect(gradients.dtype).toEqual('float32');
46 expectArraysClose(await gradients.data(), [3 / 5]);
47 });
48 it('gradient with clones', async () => {
49 const a = tf.scalar(5);
50 const dy = tf.scalar(3);
51 const gradients = tf.grad(a => tf.log(a.clone()).clone())(a, dy);
52 expect(gradients.shape).toEqual(a.shape);
53 expect(gradients.dtype).toEqual('float32');
54 expectArraysClose(await gradients.data(), [3 / 5]);
55 });
56 it('gradients: Tensor1D', async () => {
57 const a = tf.tensor1d([-1, 2, 3, -5]);
58 const dy = tf.tensor1d([1, 2, 3, 4]);
59 const gradients = tf.grad(a => tf.log(a))(a, dy);
60 expect(gradients.shape).toEqual(a.shape);
61 expect(gradients.dtype).toEqual('float32');
62 expectArraysClose(await gradients.data(), [1 / -1, 2 / 2, 3 / 3, 4 / -5]);
63 });
64 it('gradients: Tensor2D', async () => {
65 const a = tf.tensor2d([-3, 1, 2, 3], [2, 2]);
66 const dy = tf.tensor2d([1, 2, 3, 4], [2, 2]);
67 const gradients = tf.grad(a => tf.log(a))(a, dy);
68 expect(gradients.shape).toEqual(a.shape);
69 expect(gradients.dtype).toEqual('float32');
70 expectArraysClose(await gradients.data(), [1 / -3, 2 / 1, 3 / 2, 4 / 3]);
71 });
72 it('throws when passed a non-tensor', () => {
73 expect(() => tf.log({}))
74 .toThrowError(/Argument 'x' passed to 'log' must be a Tensor/);
75 });
76 it('accepts a tensor-like object', async () => {
77 const r = tf.log([1, 2]);
78 expectArraysClose(await r.data(), [Math.log(1), Math.log(2)]);
79 });
80 it('throws for string tensor', () => {
81 expect(() => tf.log('q'))
82 .toThrowError(/Argument 'x' passed to 'log' must be numeric/);
83 });
84});
85//# sourceMappingURL=log_test.js.map
\No newline at end of file