1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import * as tf from '../index';
|
18 | import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
|
19 | import { expectArraysClose } from '../test_util';
|
20 | describeWithFlags('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 |
|
\ | No newline at end of file |