|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 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 test_util from '../test_util'; |
| 19 | +import {NDArrayMath} from './math'; |
| 20 | +import {NDArrayMathCPU} from './math_cpu'; |
| 21 | +import {NDArrayMathGPU} from './math_gpu'; |
| 22 | +import {Array1D} from './ndarray'; |
| 23 | + |
| 24 | +function executeTests(mathFactory: () => NDArrayMath) { |
| 25 | + let math: NDArrayMath; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + math = mathFactory(); |
| 29 | + math.startScope(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + math.endScope(null); |
| 34 | + math.dispose(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Depth 1 throws error', () => { |
| 38 | + const indices = Array1D.new([0, 0, 0]); |
| 39 | + expect(() => math.oneHot(indices, 1)).toThrowError(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('Depth 2, diagonal', () => { |
| 43 | + const indices = Array1D.new([0, 1]); |
| 44 | + const res = math.oneHot(indices, 2); |
| 45 | + const expected = new Float32Array([1, 0, 0, 1]); |
| 46 | + expect(res.shape).toEqual([2, 2]); |
| 47 | + test_util.expectArraysClose(res.getValues(), expected); |
| 48 | + }); |
| 49 | + |
| 50 | + it('Depth 2, transposed diagonal', () => { |
| 51 | + const indices = Array1D.new([1, 0]); |
| 52 | + const res = math.oneHot(indices, 2); |
| 53 | + const expected = new Float32Array([0, 1, 1, 0]); |
| 54 | + expect(res.shape).toEqual([2, 2]); |
| 55 | + test_util.expectArraysClose(res.getValues(), expected); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Depth 3, 4 events', () => { |
| 59 | + const indices = Array1D.new([2, 1, 2, 0]); |
| 60 | + const res = math.oneHot(indices, 3); |
| 61 | + const expected = new Float32Array([0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0]); |
| 62 | + expect(res.shape).toEqual([4, 3]); |
| 63 | + test_util.expectArraysClose(res.getValues(), expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Depth 2 onValue=3, offValue=-2', () => { |
| 67 | + const indices = Array1D.new([0, 1]); |
| 68 | + const res = math.oneHot(indices, 2, 3, -2); |
| 69 | + const expected = new Float32Array([3, -2, -2, 3]); |
| 70 | + expect(res.shape).toEqual([2, 2]); |
| 71 | + test_util.expectArraysClose(res.getValues(), expected); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +describe('mathCPU oneHot', () => { |
| 76 | + executeTests(() => new NDArrayMathCPU()); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('mathGPU oneHot', () => { |
| 80 | + executeTests(() => new NDArrayMathGPU()); |
| 81 | +}); |
0 commit comments