|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 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 | + */ |
| 17 | + |
| 18 | +import * as tf from '../index'; |
| 19 | +import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; |
| 20 | +import {Tensor} from '../tensor'; |
| 21 | +import {expectArraysClose} from '../test_util'; |
| 22 | + |
| 23 | +describeWithFlags('booleanMask', ALL_ENVS, () => { |
| 24 | + it('1d array, 1d mask, default axis', async () => { |
| 25 | + const array = tf.tensor1d([1, 2, 3]); |
| 26 | + const mask = tf.tensor1d([1, 0, 1], 'bool'); |
| 27 | + const result = await tf.booleanMask(array, mask); |
| 28 | + expect(result.shape).toEqual([2]); |
| 29 | + expect(result.dtype).toBe('float32'); |
| 30 | + expectArraysClose(await result.data(), [1, 3]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('2d array, 1d mask, default axis', async () => { |
| 34 | + const array = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]); |
| 35 | + const mask = tf.tensor1d([1, 0, 1], 'bool'); |
| 36 | + const result = await tf.booleanMask(array, mask); |
| 37 | + expect(result.shape).toEqual([2, 2]); |
| 38 | + expect(result.dtype).toBe('float32'); |
| 39 | + expectArraysClose(await result.data(), [1, 2, 5, 6]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('2d array, 2d mask, default axis', async () => { |
| 43 | + const array = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]); |
| 44 | + const mask = tf.tensor2d([1, 0, 1, 0, 1, 0], [3, 2], 'bool'); |
| 45 | + const result = await tf.booleanMask(array, mask); |
| 46 | + expect(result.shape).toEqual([3]); |
| 47 | + expect(result.dtype).toBe('float32'); |
| 48 | + expectArraysClose(await result.data(), [1, 3, 5]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('2d array, 1d mask, axis=1', async () => { |
| 52 | + const array = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]); |
| 53 | + const mask = tf.tensor1d([0, 1], 'bool'); |
| 54 | + const axis = 1; |
| 55 | + const result = await tf.booleanMask(array, mask, axis); |
| 56 | + expect(result.shape).toEqual([3, 1]); |
| 57 | + expect(result.dtype).toBe('float32'); |
| 58 | + expectArraysClose(await result.data(), [2, 4, 6]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('accepts tensor-like object as array or mask', async () => { |
| 62 | + const array = [[1, 2], [3, 4], [5, 6]]; |
| 63 | + const mask = [1, 0, 1]; |
| 64 | + const result = await tf.booleanMask(array, mask); |
| 65 | + expect(result.shape).toEqual([2, 2]); |
| 66 | + expect(result.dtype).toBe('float32'); |
| 67 | + expectArraysClose(await result.data(), [1, 2, 5, 6]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('ensure no memory leak', async () => { |
| 71 | + const numTensorsBefore = tf.memory().numTensors; |
| 72 | + |
| 73 | + const array = tf.tensor1d([1, 2, 3]); |
| 74 | + const mask = tf.tensor1d([1, 0, 1], 'bool'); |
| 75 | + let resultPromise: Promise<Tensor> = null; |
| 76 | + |
| 77 | + tf.tidy(() => { |
| 78 | + resultPromise = tf.booleanMask(array, mask); |
| 79 | + }); |
| 80 | + |
| 81 | + const result = await resultPromise; |
| 82 | + expect(result.shape).toEqual([2]); |
| 83 | + expect(result.dtype).toBe('float32'); |
| 84 | + expectArraysClose(await result.data(), [1, 3]); |
| 85 | + array.dispose(); |
| 86 | + mask.dispose(); |
| 87 | + result.dispose(); |
| 88 | + |
| 89 | + const numTensorsAfter = tf.memory().numTensors; |
| 90 | + expect(numTensorsAfter).toBe(numTensorsBefore); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should throw if mask is scalar', async () => { |
| 94 | + const array = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]); |
| 95 | + const mask = tf.scalar(1, 'bool'); |
| 96 | + let errorMessage = 'No error thrown.'; |
| 97 | + try { |
| 98 | + await tf.booleanMask(array, mask); |
| 99 | + } catch (error) { |
| 100 | + errorMessage = error.message; |
| 101 | + } |
| 102 | + expect(errorMessage).toBe('mask cannot be scalar'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should throw if array and mask shape miss match', async () => { |
| 106 | + const array = tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]); |
| 107 | + const mask = tf.tensor2d([1, 0], [1, 2], 'bool'); |
| 108 | + let errorMessage = 'No error thrown.'; |
| 109 | + try { |
| 110 | + await tf.booleanMask(array, mask); |
| 111 | + } catch (error) { |
| 112 | + errorMessage = error.message; |
| 113 | + } |
| 114 | + expect(errorMessage) |
| 115 | + .toBe( |
| 116 | + `mask's shape must match the first K ` + |
| 117 | + `dimensions of tensor's shape, Shapes 3,2 and 1,2 must match`); |
| 118 | + }); |
| 119 | +}); |
0 commit comments