|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 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 | + */ |
| 17 | + |
| 18 | +import * as tf from '../index'; |
| 19 | +import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; |
| 20 | +import {expectArraysClose} from '../test_util'; |
| 21 | + |
| 22 | +import {tensor1d, tensor2d, tensor3d} from './tensor_ops'; |
| 23 | + |
| 24 | +describeWithFlags('inTopK', ALL_ENVS, async () => { |
| 25 | + it('predictions 2d array, targets 1d array, with default k', async () => { |
| 26 | + const predictions = tensor2d([[20, 10, 40, 30], [30, 50, -20, 10]]); |
| 27 | + const targets = tensor1d([2, 0]); |
| 28 | + const precision = tf.inTopK(predictions, targets); |
| 29 | + expect(precision.shape).toEqual([2]); |
| 30 | + expect(precision.dtype).toBe('bool'); |
| 31 | + expectArraysClose(await precision.data(), [1, 0]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('predictions 2d array, targets 1d array, with k=2', async () => { |
| 35 | + const predictions = tensor2d([[20, 10, 40, 30], [30, 50, -20, 10]]); |
| 36 | + const targets = tensor1d([2, 0]); |
| 37 | + const k = 2; |
| 38 | + const precision = tf.inTopK(predictions, targets, k); |
| 39 | + expect(precision.shape).toEqual([2]); |
| 40 | + expect(precision.dtype).toBe('bool'); |
| 41 | + expectArraysClose(await precision.data(), [1, 1]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('predictions 3d array, targets 2d array, with default k', async () => { |
| 45 | + const predictions = |
| 46 | + tensor3d([[[1, 5, 2], [4, 3, 6]], [[3, 2, 1], [1, 2, 3]]]); |
| 47 | + const targets = tensor2d([[1, 2], [0, 1]]); |
| 48 | + const precision = tf.inTopK(predictions, targets); |
| 49 | + expect(precision.shape).toEqual([2, 2]); |
| 50 | + expect(precision.dtype).toBe('bool'); |
| 51 | + expectArraysClose(await precision.data(), [1, 1, 1, 0]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('predictions 3d array, targets 2d array, with k=2', async () => { |
| 55 | + const predictions = |
| 56 | + tensor3d([[[1, 5, 2], [4, 3, 6]], [[3, 2, 1], [1, 2, 3]]]); |
| 57 | + const targets = tensor2d([[1, 2], [0, 1]]); |
| 58 | + const k = 2; |
| 59 | + const precision = tf.inTopK(predictions, targets, k); |
| 60 | + expect(precision.shape).toEqual([2, 2]); |
| 61 | + expect(precision.dtype).toBe('bool'); |
| 62 | + expectArraysClose(await precision.data(), [1, 1, 1, 1]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('lower-index element count first, with default k', async () => { |
| 66 | + const predictions = tensor2d([[1, 2, 2, 1]]); |
| 67 | + |
| 68 | + const targets1 = tensor1d([1]); |
| 69 | + const precision1 = tf.inTopK(predictions, targets1); |
| 70 | + expect(precision1.shape).toEqual([1]); |
| 71 | + expect(precision1.dtype).toBe('bool'); |
| 72 | + expectArraysClose(await precision1.data(), [1]); |
| 73 | + |
| 74 | + const targets2 = tensor1d([2]); |
| 75 | + const precision2 = tf.inTopK(predictions, targets2); |
| 76 | + expect(precision2.shape).toEqual([1]); |
| 77 | + expect(precision2.dtype).toBe('bool'); |
| 78 | + expectArraysClose(await precision2.data(), [0]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('accept tensor-like object, with default k', async () => { |
| 82 | + const predictions = [[20, 10, 40, 30], [30, 50, -20, 10]]; |
| 83 | + const targets = [2, 0]; |
| 84 | + const precision = tf.inTopK(predictions, targets); |
| 85 | + expect(precision.shape).toEqual([2]); |
| 86 | + expect(precision.dtype).toBe('bool'); |
| 87 | + expectArraysClose(await precision.data(), [1, 0]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('throws when predictions_rank <2', () => { |
| 91 | + const predictions = tensor1d([20, 10, 40, 30]); |
| 92 | + const targets = [2]; |
| 93 | + expect(() => tf.inTopK(predictions, targets)).toThrowError(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('throws when prediction_rank != targets_rank + 1', () => { |
| 97 | + const predictions = tensor2d([[20, 10, 40, 30], [30, 50, -20, 10]]); |
| 98 | + const targets = tensor2d([[0], [0]]); |
| 99 | + expect(() => tf.inTopK(predictions, targets)).toThrowError(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('throws when k > size of last dimension of predictions', () => { |
| 103 | + const predictions = tensor2d([[20, 10, 40, 30], [30, 50, -20, 10]]); |
| 104 | + const targets = tensor1d([2, 0]); |
| 105 | + const k = 5; |
| 106 | + expect(() => tf.inTopK(predictions, targets, k)).toThrowError(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments