-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtraining_utils_test.ts
153 lines (133 loc) · 5.97 KB
/
training_utils_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @license
* Copyright 2019 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
import {memory, tensor1d, tensor2d} from '@tensorflow/tfjs-core';
import {describeMathCPU, expectTensorsClose} from '../utils/test_utils';
import {ClassWeight, ClassWeightMap, standardizeClassWeights, standardizeWeights} from './training_utils';
describeMathCPU('standardizeWeights', () => {
it('classWeights with 1D class-index target', async () => {
const y = tensor1d([0, 1, 2, 1, 0]);
const classWeight: ClassWeight = {0: 10, 1: 1, 2: 0.1};
const numTensors0 = memory().numTensors;
const classSampleWeight = await standardizeWeights(y, null, classWeight);
// Assert no memory leak. The extra tensor is `classSampleWeight` itself.
expect(memory().numTensors).toEqual(numTensors0 + 1);
expectTensorsClose(classSampleWeight, tensor1d([10, 1, 0.1, 1, 10]));
expect(y.isDisposed).toEqual(false);
});
it('classWeights with 2D class-index target', async () => {
const y = tensor2d([[3], [2], [0]]);
const classWeight: ClassWeight = {0: 10, 1: 1, 2: 0.1, 3: 0.01};
const numTensors0 = memory().numTensors;
const classSampleWeight = await standardizeWeights(y, null, classWeight);
// Assert no memory leak. The extra tensor is `classSampleWeight` itself.
expect(memory().numTensors).toEqual(numTensors0 + 1);
expectTensorsClose(classSampleWeight, tensor1d([0.01, 0.1, 10]));
expect(y.isDisposed).toEqual(false);
});
it('classWeights with 2D one-hot target', async () => {
const y = tensor2d([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]]);
const classWeight: ClassWeight = {0: 10, 1: 1, 2: 0.1, 3: 0.01};
const numTensors0 = memory().numTensors;
const classSampleWeight = await standardizeWeights(y, null, classWeight);
// Assert no memory leak. The extra tensor is `classSampleWeight` itself.
expect(memory().numTensors).toEqual(numTensors0 + 1);
expectTensorsClose(classSampleWeight, tensor1d([0.01, 0.1, 10]));
expect(y.isDisposed).toEqual(false);
});
it('classWeights with 1D class-index target: Missing class', async () => {
const y = tensor1d([0, 1, 2, 3, 2, 1, 0]);
const classWeight: ClassWeight = {0: 10, 1: 1, 2: 0.1};
let caughtError: Error;
try {
await standardizeWeights(y, null, classWeight);
} catch (error) {
caughtError = error;
}
expect(caughtError.message)
.toMatch(/classWeight must contain all classes.* class 3 .*/);
});
it('classWeights with 2D class-index target: Missing class', async () => {
const y = tensor2d([[3], [2], [0], [4]]);
const classWeight: ClassWeight = {0: 10, 1: 1, 2: 0.1, 3: 0.01};
let caughtError: Error;
try {
await standardizeWeights(y, null, classWeight);
} catch (error) {
caughtError = error;
}
expect(caughtError.message)
.toMatch(/classWeight must contain all classes.* class 4 .*/);
});
it('classWeights with 2D one-hot target: missing weight', async () => {
const y = tensor2d([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]]);
const classWeight: ClassWeight = {0: 10, 1: 1, 3: 0.01};
let caughtError: Error;
try {
await standardizeWeights(y, null, classWeight);
} catch (error) {
caughtError = error;
}
expect(caughtError.message)
.toMatch(/classWeight must contain all classes.* class 2 .*/);
});
});
describe('standardizeClassWeights', () => {
it('One output, ClassWeight singleton', () => {
const outputNames = ['output1'];
const classWeight: ClassWeight = {0: 1, 1: 2};
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}]);
});
it('One output, ClassWeight array', () => {
const outputNames = ['output1'];
const classWeight: ClassWeight[] = [{0: 1, 1: 2}];
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}]);
});
it('One output, ClassWeight dict', () => {
const outputNames = ['output1'];
const classWeight: ClassWeightMap = {'output1': {0: 1, 1: 2}};
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}]);
});
it('Two outputs, ClassWeight array', () => {
const outputNames = ['output1', 'output2'];
const classWeight: ClassWeight[] = [{0: 1, 1: 2}, {0: 10, 1: 20}];
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}, {0: 10, 1: 20}]);
});
it('Two outputs, ClassWeight dict', () => {
const outputNames = ['output1', 'output2'];
const classWeight:
ClassWeightMap = {'output2': {0: 10, 1: 20}, 'output1': {0: 1, 1: 2}};
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}, {0: 10, 1: 20}]);
});
it('Two outputs, ClassWeight singleton leads to Error', () => {
const outputNames = ['output1', 'output2'];
const classWeight: ClassWeight = {0: 10, 1: 20};
expect(() => standardizeClassWeights(classWeight, outputNames))
.toThrowError(/.*has multiple \(2\) outputs.*/);
});
it('Three outputs, ClassWeight array missing element', () => {
const outputNames = ['output1', 'output2', 'output3'];
const classWeight: ClassWeight[] = [{0: 1, 1: 2}, {0: 10, 1: 20}];
expect(() => standardizeClassWeights(classWeight, outputNames))
.toThrowError(
/.*classWeight is an array of 2 element.* model has 3 outputs/);
});
it('Three outputs, ClassWeight dict missing element is okay', () => {
const outputNames = ['output1', 'output2', 'output3'];
const classWeight:
ClassWeightMap = {'output1': {0: 1, 1: 2}, 'output3': {0: 10, 1: 20}};
const output = standardizeClassWeights(classWeight, outputNames);
expect(output).toEqual([{0: 1, 1: 2}, null, {0: 10, 1: 20}]);
});
});