Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Commit 7a64583

Browse files
authored
Centralize precision in tests - remove explicit precision from tests unless needed (#157)
* update tests with precision
1 parent 5fadaaa commit 7a64583

33 files changed

+172
-214
lines changed

src/data/dataset_test.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,49 +55,47 @@ describe('Dataset', () => {
5555

5656
test_util.expectArraysClose(
5757
new Float32Array([0, 0, 0, 1, .25, .75]),
58-
normalizedInputs[0].getValues(), 1e-5);
58+
normalizedInputs[0].getValues());
5959
test_util.expectArraysClose(
6060
new Float32Array([1 / 3, 1 / 3, 1 / 3, 2 / 3, .75, .5]),
61-
normalizedInputs[1].getValues(), 1e-5);
61+
normalizedInputs[1].getValues());
6262
test_util.expectArraysClose(
6363
new Float32Array([2 / 3, 2 / 3, 2 / 3, 1 / 3, 0, 0]),
64-
normalizedInputs[2].getValues(), 1e-5);
64+
normalizedInputs[2].getValues());
6565
test_util.expectArraysClose(
66-
new Float32Array([1, 1, 1, 0, 1, 1]), normalizedInputs[3].getValues(),
67-
1e-5);
66+
new Float32Array([1, 1, 1, 0, 1, 1]), normalizedInputs[3].getValues());
6867

6968
dataset.normalizeWithinBounds(dataIndex, -1, 1);
7069

7170
normalizedInputs = dataset.getData()[0];
7271

7372
test_util.expectArraysClose(
7473
new Float32Array([-1, -1, -1, 1, -.5, .5]),
75-
normalizedInputs[0].getValues(), 1e-5);
74+
normalizedInputs[0].getValues());
7675
test_util.expectArraysClose(
7776
new Float32Array([-1 / 3, -1 / 3, -1 / 3, 1 / 3, .5, .0]),
78-
normalizedInputs[1].getValues(), 1e-5);
77+
normalizedInputs[1].getValues());
7978
test_util.expectArraysClose(
8079
new Float32Array([1 / 3, 1 / 3, 1 / 3, -1 / 3, -1, -1]),
81-
normalizedInputs[2].getValues(), 1e-5);
80+
normalizedInputs[2].getValues());
8281
test_util.expectArraysClose(
83-
new Float32Array([1, 1, 1, -1, 1, 1]), normalizedInputs[3].getValues(),
84-
1e-5);
82+
new Float32Array([1, 1, 1, -1, 1, 1]), normalizedInputs[3].getValues());
8583

8684
dataset.removeNormalization(dataIndex);
8785

8886
normalizedInputs = dataset.getData()[0];
8987

9088
test_util.expectArraysClose(
9189
new Float32Array([1, 2, 10, -1, -2, .75]),
92-
normalizedInputs[0].getValues(), 1e-5);
90+
normalizedInputs[0].getValues());
9391
test_util.expectArraysClose(
9492
new Float32Array([2, 3, 20, -2, 2, .5]),
95-
normalizedInputs[1].getValues(), 1e-5);
93+
normalizedInputs[1].getValues());
9694
test_util.expectArraysClose(
9795
new Float32Array([3, 4, 30, -3, -4, 0]),
98-
normalizedInputs[2].getValues(), 1e-5);
96+
normalizedInputs[2].getValues());
9997
test_util.expectArraysClose(
100-
new Float32Array([4, 5, 40, -4, 4, 1]), normalizedInputs[3].getValues(),
101-
1e-5);
98+
new Float32Array([4, 5, 40, -4, 4, 1]),
99+
normalizedInputs[3].getValues());
102100
});
103101
});

src/graph/ops/max_pool_test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ describe('Max pool', () => {
105105
const y = activations.get(yTensor);
106106
const expectedResult =
107107
Array3D.new([2, 2, 2], [6, 66, 8, 88, 14, 140, 16, 160]);
108-
test_util.expectArraysClose(
109-
y.getValues(), expectedResult.getValues(), 1e-6);
108+
test_util.expectArraysClose(y.getValues(), expectedResult.getValues());
110109
});
111110

112111
it('MaxPool depth = 2, with some negative numbers', () => {
@@ -133,8 +132,7 @@ describe('Max pool', () => {
133132
const expectedResult =
134133
Array3D.new([2, 2, 2], [6, 55, 8, 88, 14, 140, 16, 150]);
135134

136-
test_util.expectArraysClose(
137-
y.getValues(), expectedResult.getValues(), 1e-6);
135+
test_util.expectArraysClose(y.getValues(), expectedResult.getValues());
138136
});
139137

140138
it('MaxPool downsampling depth is preserved', () => {

src/graph/session_test.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import {InputProvider} from '../data/input_provider';
1919
import {NDArrayMathCPU} from '../math/math_cpu';
2020
import {NDArrayMathGPU} from '../math/math_gpu';
2121
import {Array1D, NDArray, Scalar} from '../math/ndarray';
22+
import * as test_util from '../test_util';
2223

2324
import {Graph, Tensor} from './graph';
2425
import {AdagradOptimizer} from './optimizers/adagrad_optimizer';
2526
import {MomentumOptimizer} from './optimizers/momentum_optimizer';
2627
import {RMSPropOptimizer} from './optimizers/rmsprop_optimizer';
27-
import {FeedDictionary, FeedEntry, Session} from './session';
2828
import {SGDOptimizer} from './optimizers/sgd_optimizer';
29-
import * as test_util from '../test_util';
29+
import {FeedDictionary, FeedEntry, Session} from './session';
3030

3131

3232
describe('FeedDictionary', () => {
@@ -95,7 +95,7 @@ describe('Session', () => {
9595
const session = new Session(g, new NDArrayMathCPU());
9696
const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]);
9797
const expected = new Float32Array([28, 19]);
98-
test_util.expectArraysClose(yVal.getValues(), expected, 1e-5);
98+
test_util.expectArraysClose(yVal.getValues(), expected);
9999
});
100100

101101
it('y=x^2 + 3: GPU', () => {
@@ -107,7 +107,7 @@ describe('Session', () => {
107107
math.scope(() => {
108108
const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]);
109109
const expected = new Float32Array([28, 19]);
110-
test_util.expectArraysClose(yVal.getValues(), expected, 1e-5);
110+
test_util.expectArraysClose(yVal.getValues(), expected);
111111
});
112112
});
113113

@@ -122,7 +122,7 @@ describe('Session', () => {
122122
const yVal =
123123
session.eval(y, [{tensor: xSquared, data: Array1D.new([25, 16])}]);
124124
const expected = new Float32Array([28, 19]);
125-
test_util.expectArraysClose(yVal.getValues(), expected, 1e-5);
125+
test_util.expectArraysClose(yVal.getValues(), expected);
126126
});
127127
});
128128

@@ -139,8 +139,8 @@ describe('Session', () => {
139139
session.evalAll([y, z], [{tensor: x, data: Array1D.new([5, 4])}]);
140140
const expectedY = new Float32Array([28, 19]);
141141
const expectedZ = new Float32Array([27, 18]);
142-
test_util.expectArraysClose(result[0].getValues(), expectedY, 1e-5);
143-
test_util.expectArraysClose(result[1].getValues(), expectedZ, 1e-5);
142+
test_util.expectArraysClose(result[0].getValues(), expectedY);
143+
test_util.expectArraysClose(result[1].getValues(), expectedZ);
144144
});
145145
});
146146

@@ -155,11 +155,11 @@ describe('Session', () => {
155155
math.scope(() => {
156156
const result1 = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]);
157157
const expectedY = new Float32Array([30, 20]);
158-
test_util.expectArraysClose(result1.getValues(), expectedY, 1e-5);
158+
test_util.expectArraysClose(result1.getValues(), expectedY);
159159

160160
const result2 = session.eval(z, [{tensor: x, data: Array1D.new([5, 4])}]);
161161
const expectedZ = new Float32Array([31, 21]);
162-
test_util.expectArraysClose(result2.getValues(), expectedZ, 1e-5);
162+
test_util.expectArraysClose(result2.getValues(), expectedZ);
163163
});
164164
});
165165

@@ -214,7 +214,7 @@ describe('Session', () => {
214214
// dw/dx = [2*x_1 + 1, 2*x_2 + 1]
215215
session.train(w, [{tensor: x, data: inputProvider}], 1, optimizer);
216216
const dwdx = session.gradientArrayMap.get(x).getValues();
217-
test_util.expectArraysClose(dwdx, new Float32Array([5, 9]), 1e-5);
217+
test_util.expectArraysClose(dwdx, new Float32Array([5, 9]));
218218
});
219219

220220
it('Specify which variables to update (var_list)', () => {
@@ -248,8 +248,8 @@ describe('Session', () => {
248248
const b0After1 = session.activationArrayMap.get(b0).getValues();
249249
const b1After1 = session.activationArrayMap.get(b1).getValues();
250250

251-
test_util.expectArraysClose(b0After1, new Float32Array([-0.8, -1.6]), 1e-5);
252-
test_util.expectArraysClose(b1After1, new Float32Array([0, 0]), 1e-5);
251+
test_util.expectArraysClose(b0After1, new Float32Array([-0.8, -1.6]));
252+
test_util.expectArraysClose(b1After1, new Float32Array([0, 0]));
253253

254254
// Update both b0 and b1
255255
const optimizerAll = new SGDOptimizer(0.1);
@@ -283,7 +283,7 @@ describe('Session', () => {
283283
math.scope(() => {
284284
const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]);
285285
const expected = new Float32Array([25, 16]);
286-
test_util.expectArraysClose(yVal.getValues(), expected, 1e-5);
286+
test_util.expectArraysClose(yVal.getValues(), expected);
287287
});
288288
});
289289

@@ -309,7 +309,7 @@ describe('Session', () => {
309309
// dw/dx = [2*x_1 + 1, 2*x_2 + 1]
310310
session.train(w, [{tensor: x, data: inputProvider}], 1, optimizer);
311311
const dwdx = session.gradientArrayMap.get(x).getValues();
312-
test_util.expectArraysClose(dwdx, new Float32Array([5, 9]), 1e-5);
312+
test_util.expectArraysClose(dwdx, new Float32Array([5, 9]));
313313
});
314314
});
315315

@@ -337,13 +337,13 @@ describe('Session', () => {
337337
// w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.2, -0.4]
338338
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
339339
const dydw = session.activationArrayMap.get(w).getValues();
340-
test_util.expectArraysClose(dydw, new Float32Array([-.2, -0.4]), 1e-5);
340+
test_util.expectArraysClose(dydw, new Float32Array([-.2, -0.4]));
341341
// velocity_w = [momentum* old_vel_w1 + x_1,
342342
// momentum* old_vel_w2 + x_2] = [3,6]
343343
// w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.5, -1.0]
344344
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
345345
const dydw2 = session.activationArrayMap.get(w).getValues();
346-
test_util.expectArraysClose(dydw2, new Float32Array([-.5, -1.0]), 2e-5);
346+
test_util.expectArraysClose(dydw2, new Float32Array([-.5, -1.0]));
347347
});
348348
});
349349

@@ -374,16 +374,15 @@ describe('Session', () => {
374374
// = [-0.1, -0.1]
375375
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
376376
const dydw = session.activationArrayMap.get(w).getValues();
377-
test_util.expectArraysClose(dydw, new Float32Array([-.1, -0.1]), 1e-5);
377+
test_util.expectArraysClose(dydw, new Float32Array([-.1, -0.1]));
378378
// cache = [old_cache_w1 + grad_w1**2,
379379
// old_cache_w2 + grad_w2**2] = [4,16]
380380
// w = [ w1_old - lr*grad_w1/sqrt(cahce_w2 + eps),
381381
// w2_old - lr*grad_w1/sqrt(cahce_w2 + eps)]
382382
// = [-0.1707, -0.1707]
383383
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
384384
const dydw2 = session.activationArrayMap.get(w).getValues();
385-
test_util.expectArraysClose(
386-
dydw2, new Float32Array([-.1707, -.1707]), 2e-5);
385+
test_util.expectArraysClose(dydw2, new Float32Array([-.1707, -.1707]));
387386
});
388387
});
389388

@@ -413,8 +412,7 @@ describe('Session', () => {
413412
// = [-0.2236, -0.2236]
414413
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
415414
const dydw = session.activationArrayMap.get(w).getValues();
416-
test_util.expectArraysClose(
417-
dydw, new Float32Array([-.2236, -0.2236]), 1e-5);
415+
test_util.expectArraysClose(dydw, new Float32Array([-.2236, -0.2236]));
418416
// cache = [gamma*old_cache_w1 + (1-gamma)*grad_w1**2,
419417
// gamma*old_cache_w2 + (1-gamma)*grad_w2**2]
420418
// = [1.44, 5.76]
@@ -423,8 +421,7 @@ describe('Session', () => {
423421
// = [-.39027, -.39027]
424422
session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer);
425423
const dydw2 = session.activationArrayMap.get(w).getValues();
426-
test_util.expectArraysClose(
427-
dydw2, new Float32Array([-.39027, -.39027]), 2e-5);
424+
test_util.expectArraysClose(dydw2, new Float32Array([-.39027, -.39027]));
428425
});
429426
});
430427

src/math/math_cpu_test.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('NDArrayMathCPU slice2D', () => {
8484
const aValues = a.getValues();
8585
const expected =
8686
new Float32Array([aValues[0], aValues[1], aValues[10], aValues[11]]);
87-
test_util.expectArraysClose(b.getValues(), expected, 0);
87+
test_util.expectArraysClose(b.getValues(), expected);
8888
});
8989

9090
it('returns the rectangle specified', () => {
@@ -863,9 +863,9 @@ describe('NDArrayMathCPU argmin/max, argmaxequals, min/max', () => {
863863
it('topk', () => {
864864
const topk = math.topK(Array1D.new([1, -1, 100, -5, -10.6, 3.3, 5]), 3);
865865
test_util.expectArraysClose(
866-
topk.values.getValues(), new Float32Array([100, 5, 3.3]), 1e-6);
866+
topk.values.getValues(), new Float32Array([100, 5, 3.3]));
867867
test_util.expectArraysClose(
868-
topk.indices.getValues(), new Float32Array([2, 6, 5]), 1e-6);
868+
topk.indices.getValues(), new Float32Array([2, 6, 5]));
869869
});
870870

871871
it('Arg min', () => {
@@ -1560,16 +1560,15 @@ describe('NDArrayMathCPU resizeBilinear', () => {
15601560

15611561
test_util.expectArraysClose(
15621562
output.getValues(),
1563-
new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4]), 1e-4);
1563+
new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4]));
15641564
});
15651565

15661566
it('simple alignCorners=true', () => {
15671567
const input = Array3D.new([2, 2, 1], [2, 2, 4, 4]);
15681568
const output = math.resizeBilinear3D(input, [3, 3], true);
15691569

15701570
test_util.expectArraysClose(
1571-
output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4]),
1572-
1e-4);
1571+
output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4]));
15731572
});
15741573

15751574
it('matches tensorflow w/ random numbers alignCorners=false', () => {
@@ -1589,8 +1588,7 @@ describe('NDArrayMathCPU resizeBilinear', () => {
15891588
0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893,
15901589
0.6183514, 3.49600649, 1.50272655, 1.73724651, 1.68149579,
15911590
0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893
1592-
]),
1593-
1e-4);
1591+
]));
15941592
});
15951593

15961594
it('matches tensorflow w/ random numbers alignCorners=true', () => {
@@ -1610,8 +1608,7 @@ describe('NDArrayMathCPU resizeBilinear', () => {
16101608
1.70539713, 1.3923912, 1.68282723, 1.54382229, 1.66025746,
16111609
1.62451875, 1.83673346, 1.38198328, 1.92833281, 1.13944793,
16121610
2.01993227, 1.57932377, 2.34758639, 2.01919961, 2.67524052
1613-
]),
1614-
1e-4);
1611+
]));
16151612
});
16161613
});
16171614

@@ -1640,8 +1637,7 @@ describe('NDArrayMathCPU batchNorm', () => {
16401637
Math.sqrt(variance.get(0) + varianceEpsilon),
16411638
(x.get(1, 0, 1) - mean.get(1)) * 1 /
16421639
Math.sqrt(variance.get(1) + varianceEpsilon)
1643-
]),
1644-
1e-6);
1640+
]));
16451641
});
16461642

16471643
it('simple batchnorm, no offset, 2x1x2', () => {
@@ -1664,8 +1660,7 @@ describe('NDArrayMathCPU batchNorm', () => {
16641660
Math.sqrt(variance.get(0) + varianceEpsilon),
16651661
(x.get(1, 0, 1) - mean.get(1)) * scale.get(1) /
16661662
Math.sqrt(variance.get(1) + varianceEpsilon)
1667-
]),
1668-
1e-6);
1663+
]));
16691664
});
16701665

16711666
it('simple batchnorm, no scale, 2x1x2', () => {
@@ -1693,8 +1688,7 @@ describe('NDArrayMathCPU batchNorm', () => {
16931688
offset.get(1) +
16941689
(x.get(1, 0, 1) - mean.get(1)) * 1 /
16951690
Math.sqrt(variance.get(1) + varianceEpsilon)
1696-
]),
1697-
1e-6);
1691+
]));
16981692
});
16991693

17001694
it('simple batchnorm, 2x1x2', () => {
@@ -1723,8 +1717,7 @@ describe('NDArrayMathCPU batchNorm', () => {
17231717
offset.get(1) +
17241718
(x.get(1, 0, 1) - mean.get(1)) * scale.get(1) /
17251719
Math.sqrt(variance.get(1) + varianceEpsilon)
1726-
]),
1727-
1e-6);
1720+
]));
17281721
});
17291722

17301723
it('batchnorm matches tensorflow, 2x3x3', () => {
@@ -1750,7 +1743,6 @@ describe('NDArrayMathCPU batchNorm', () => {
17501743
1.52106473, -0.07704776, 0.26144429, 1.28010017, -1.14422404,
17511744
-1.15776136, 1.15425493, 1.82644104, -0.52249442, 1.04803919,
17521745
0.74932291, 0.40568101, 1.2844412
1753-
]),
1754-
1e-5);
1746+
]));
17551747
});
17561748
});

0 commit comments

Comments
 (0)