@@ -19,14 +19,14 @@ import {InputProvider} from '../data/input_provider';
19
19
import { NDArrayMathCPU } from '../math/math_cpu' ;
20
20
import { NDArrayMathGPU } from '../math/math_gpu' ;
21
21
import { Array1D , NDArray , Scalar } from '../math/ndarray' ;
22
+ import * as test_util from '../test_util' ;
22
23
23
24
import { Graph , Tensor } from './graph' ;
24
25
import { AdagradOptimizer } from './optimizers/adagrad_optimizer' ;
25
26
import { MomentumOptimizer } from './optimizers/momentum_optimizer' ;
26
27
import { RMSPropOptimizer } from './optimizers/rmsprop_optimizer' ;
27
- import { FeedDictionary , FeedEntry , Session } from './session' ;
28
28
import { SGDOptimizer } from './optimizers/sgd_optimizer' ;
29
- import * as test_util from '../test_util ' ;
29
+ import { FeedDictionary , FeedEntry , Session } from './session ' ;
30
30
31
31
32
32
describe ( 'FeedDictionary' , ( ) => {
@@ -95,7 +95,7 @@ describe('Session', () => {
95
95
const session = new Session ( g , new NDArrayMathCPU ( ) ) ;
96
96
const yVal = session . eval ( y , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
97
97
const expected = new Float32Array ( [ 28 , 19 ] ) ;
98
- test_util . expectArraysClose ( yVal . getValues ( ) , expected , 1e-5 ) ;
98
+ test_util . expectArraysClose ( yVal . getValues ( ) , expected ) ;
99
99
} ) ;
100
100
101
101
it ( 'y=x^2 + 3: GPU' , ( ) => {
@@ -107,7 +107,7 @@ describe('Session', () => {
107
107
math . scope ( ( ) => {
108
108
const yVal = session . eval ( y , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
109
109
const expected = new Float32Array ( [ 28 , 19 ] ) ;
110
- test_util . expectArraysClose ( yVal . getValues ( ) , expected , 1e-5 ) ;
110
+ test_util . expectArraysClose ( yVal . getValues ( ) , expected ) ;
111
111
} ) ;
112
112
} ) ;
113
113
@@ -122,7 +122,7 @@ describe('Session', () => {
122
122
const yVal =
123
123
session . eval ( y , [ { tensor : xSquared , data : Array1D . new ( [ 25 , 16 ] ) } ] ) ;
124
124
const expected = new Float32Array ( [ 28 , 19 ] ) ;
125
- test_util . expectArraysClose ( yVal . getValues ( ) , expected , 1e-5 ) ;
125
+ test_util . expectArraysClose ( yVal . getValues ( ) , expected ) ;
126
126
} ) ;
127
127
} ) ;
128
128
@@ -139,8 +139,8 @@ describe('Session', () => {
139
139
session . evalAll ( [ y , z ] , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
140
140
const expectedY = new Float32Array ( [ 28 , 19 ] ) ;
141
141
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 ) ;
144
144
} ) ;
145
145
} ) ;
146
146
@@ -155,11 +155,11 @@ describe('Session', () => {
155
155
math . scope ( ( ) => {
156
156
const result1 = session . eval ( y , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
157
157
const expectedY = new Float32Array ( [ 30 , 20 ] ) ;
158
- test_util . expectArraysClose ( result1 . getValues ( ) , expectedY , 1e-5 ) ;
158
+ test_util . expectArraysClose ( result1 . getValues ( ) , expectedY ) ;
159
159
160
160
const result2 = session . eval ( z , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
161
161
const expectedZ = new Float32Array ( [ 31 , 21 ] ) ;
162
- test_util . expectArraysClose ( result2 . getValues ( ) , expectedZ , 1e-5 ) ;
162
+ test_util . expectArraysClose ( result2 . getValues ( ) , expectedZ ) ;
163
163
} ) ;
164
164
} ) ;
165
165
@@ -214,7 +214,7 @@ describe('Session', () => {
214
214
// dw/dx = [2*x_1 + 1, 2*x_2 + 1]
215
215
session . train ( w , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
216
216
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 ] ) ) ;
218
218
} ) ;
219
219
220
220
it ( 'Specify which variables to update (var_list)' , ( ) => {
@@ -248,8 +248,8 @@ describe('Session', () => {
248
248
const b0After1 = session . activationArrayMap . get ( b0 ) . getValues ( ) ;
249
249
const b1After1 = session . activationArrayMap . get ( b1 ) . getValues ( ) ;
250
250
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 ] ) ) ;
253
253
254
254
// Update both b0 and b1
255
255
const optimizerAll = new SGDOptimizer ( 0.1 ) ;
@@ -283,7 +283,7 @@ describe('Session', () => {
283
283
math . scope ( ( ) => {
284
284
const yVal = session . eval ( y , [ { tensor : x , data : Array1D . new ( [ 5 , 4 ] ) } ] ) ;
285
285
const expected = new Float32Array ( [ 25 , 16 ] ) ;
286
- test_util . expectArraysClose ( yVal . getValues ( ) , expected , 1e-5 ) ;
286
+ test_util . expectArraysClose ( yVal . getValues ( ) , expected ) ;
287
287
} ) ;
288
288
} ) ;
289
289
@@ -309,7 +309,7 @@ describe('Session', () => {
309
309
// dw/dx = [2*x_1 + 1, 2*x_2 + 1]
310
310
session . train ( w , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
311
311
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 ] ) ) ;
313
313
} ) ;
314
314
} ) ;
315
315
@@ -337,13 +337,13 @@ describe('Session', () => {
337
337
// w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.2, -0.4]
338
338
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
339
339
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 ] ) ) ;
341
341
// velocity_w = [momentum* old_vel_w1 + x_1,
342
342
// momentum* old_vel_w2 + x_2] = [3,6]
343
343
// w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.5, -1.0]
344
344
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
345
345
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 ] ) ) ;
347
347
} ) ;
348
348
} ) ;
349
349
@@ -374,16 +374,15 @@ describe('Session', () => {
374
374
// = [-0.1, -0.1]
375
375
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
376
376
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 ] ) ) ;
378
378
// cache = [old_cache_w1 + grad_w1**2,
379
379
// old_cache_w2 + grad_w2**2] = [4,16]
380
380
// w = [ w1_old - lr*grad_w1/sqrt(cahce_w2 + eps),
381
381
// w2_old - lr*grad_w1/sqrt(cahce_w2 + eps)]
382
382
// = [-0.1707, -0.1707]
383
383
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
384
384
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 ] ) ) ;
387
386
} ) ;
388
387
} ) ;
389
388
@@ -413,8 +412,7 @@ describe('Session', () => {
413
412
// = [-0.2236, -0.2236]
414
413
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
415
414
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 ] ) ) ;
418
416
// cache = [gamma*old_cache_w1 + (1-gamma)*grad_w1**2,
419
417
// gamma*old_cache_w2 + (1-gamma)*grad_w2**2]
420
418
// = [1.44, 5.76]
@@ -423,8 +421,7 @@ describe('Session', () => {
423
421
// = [-.39027, -.39027]
424
422
session . train ( y , [ { tensor : x , data : inputProvider } ] , 1 , optimizer ) ;
425
423
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 ] ) ) ;
428
425
} ) ;
429
426
} ) ;
430
427
0 commit comments