@@ -21,11 +21,11 @@ import {expectArraysClose} from '../test_util';
21
21
22
22
import { tensor1d , tensor2d , tensor3d } from './tensor_ops' ;
23
23
24
- describeWithFlags ( 'inTopK ' , ALL_ENVS , async ( ) => {
24
+ describeWithFlags ( 'inTopKAsync ' , ALL_ENVS , async ( ) => {
25
25
it ( 'predictions 2d array, targets 1d array, with default k' , async ( ) => {
26
26
const predictions = tensor2d ( [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ) ;
27
27
const targets = tensor1d ( [ 2 , 0 ] ) ;
28
- const precision = tf . inTopK ( predictions , targets ) ;
28
+ const precision = await tf . inTopKAsync ( predictions , targets ) ;
29
29
expect ( precision . shape ) . toEqual ( [ 2 ] ) ;
30
30
expect ( precision . dtype ) . toBe ( 'bool' ) ;
31
31
expectArraysClose ( await precision . data ( ) , [ 1 , 0 ] ) ;
@@ -35,7 +35,7 @@ describeWithFlags('inTopK', ALL_ENVS, async () => {
35
35
const predictions = tensor2d ( [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ) ;
36
36
const targets = tensor1d ( [ 2 , 0 ] ) ;
37
37
const k = 2 ;
38
- const precision = tf . inTopK ( predictions , targets , k ) ;
38
+ const precision = await tf . inTopKAsync ( predictions , targets , k ) ;
39
39
expect ( precision . shape ) . toEqual ( [ 2 ] ) ;
40
40
expect ( precision . dtype ) . toBe ( 'bool' ) ;
41
41
expectArraysClose ( await precision . data ( ) , [ 1 , 1 ] ) ;
@@ -45,7 +45,7 @@ describeWithFlags('inTopK', ALL_ENVS, async () => {
45
45
const predictions =
46
46
tensor3d ( [ [ [ 1 , 5 , 2 ] , [ 4 , 3 , 6 ] ] , [ [ 3 , 2 , 1 ] , [ 1 , 2 , 3 ] ] ] ) ;
47
47
const targets = tensor2d ( [ [ 1 , 2 ] , [ 0 , 1 ] ] ) ;
48
- const precision = tf . inTopK ( predictions , targets ) ;
48
+ const precision = await tf . inTopKAsync ( predictions , targets ) ;
49
49
expect ( precision . shape ) . toEqual ( [ 2 , 2 ] ) ;
50
50
expect ( precision . dtype ) . toBe ( 'bool' ) ;
51
51
expectArraysClose ( await precision . data ( ) , [ 1 , 1 , 1 , 0 ] ) ;
@@ -56,7 +56,7 @@ describeWithFlags('inTopK', ALL_ENVS, async () => {
56
56
tensor3d ( [ [ [ 1 , 5 , 2 ] , [ 4 , 3 , 6 ] ] , [ [ 3 , 2 , 1 ] , [ 1 , 2 , 3 ] ] ] ) ;
57
57
const targets = tensor2d ( [ [ 1 , 2 ] , [ 0 , 1 ] ] ) ;
58
58
const k = 2 ;
59
- const precision = tf . inTopK ( predictions , targets , k ) ;
59
+ const precision = await tf . inTopKAsync ( predictions , targets , k ) ;
60
60
expect ( precision . shape ) . toEqual ( [ 2 , 2 ] ) ;
61
61
expect ( precision . dtype ) . toBe ( 'bool' ) ;
62
62
expectArraysClose ( await precision . data ( ) , [ 1 , 1 , 1 , 1 ] ) ;
@@ -66,13 +66,13 @@ describeWithFlags('inTopK', ALL_ENVS, async () => {
66
66
const predictions = tensor2d ( [ [ 1 , 2 , 2 , 1 ] ] ) ;
67
67
68
68
const targets1 = tensor1d ( [ 1 ] ) ;
69
- const precision1 = tf . inTopK ( predictions , targets1 ) ;
69
+ const precision1 = await tf . inTopKAsync ( predictions , targets1 ) ;
70
70
expect ( precision1 . shape ) . toEqual ( [ 1 ] ) ;
71
71
expect ( precision1 . dtype ) . toBe ( 'bool' ) ;
72
72
expectArraysClose ( await precision1 . data ( ) , [ 1 ] ) ;
73
73
74
74
const targets2 = tensor1d ( [ 2 ] ) ;
75
- const precision2 = tf . inTopK ( predictions , targets2 ) ;
75
+ const precision2 = await tf . inTopKAsync ( predictions , targets2 ) ;
76
76
expect ( precision2 . shape ) . toEqual ( [ 1 ] ) ;
77
77
expect ( precision2 . dtype ) . toBe ( 'bool' ) ;
78
78
expectArraysClose ( await precision2 . data ( ) , [ 0 ] ) ;
@@ -81,28 +81,72 @@ describeWithFlags('inTopK', ALL_ENVS, async () => {
81
81
it ( 'accept tensor-like object, with default k' , async ( ) => {
82
82
const predictions = [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ;
83
83
const targets = [ 2 , 0 ] ;
84
- const precision = tf . inTopK ( predictions , targets ) ;
84
+ const precision = await tf . inTopKAsync ( predictions , targets ) ;
85
85
expect ( precision . shape ) . toEqual ( [ 2 ] ) ;
86
86
expect ( precision . dtype ) . toBe ( 'bool' ) ;
87
87
expectArraysClose ( await precision . data ( ) , [ 1 , 0 ] ) ;
88
88
} ) ;
89
89
90
- it ( 'throws when predictions_rank <2' , ( ) => {
90
+ it ( 'doesnt leak tensors with tensor-like objects' , async ( ) => {
91
+ const numTensors = tf . memory ( ) . numTensors ;
92
+
93
+ const predictions = [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ;
94
+ const targets = [ 2 , 0 ] ;
95
+ const precision = await tf . inTopKAsync ( predictions , targets ) ;
96
+ precision . dispose ( ) ;
97
+
98
+ expect ( tf . memory ( ) . numTensors ) . toBe ( numTensors ) ;
99
+ } ) ;
100
+
101
+ it ( 'throws when predictions_rank <2' , async ( ) => {
91
102
const predictions = tensor1d ( [ 20 , 10 , 40 , 30 ] ) ;
92
103
const targets = [ 2 ] ;
93
- expect ( ( ) => tf . inTopK ( predictions , targets ) ) . toThrowError ( ) ;
104
+
105
+ // expect(...).toThrowError() does not support async functions.
106
+ // See https://github.com/jasmine/jasmine/issues/1410
107
+ try {
108
+ await tf . inTopKAsync ( predictions , targets ) ;
109
+ throw new Error ( 'The line above should have thrown an error' ) ;
110
+ } catch ( ex ) {
111
+ expect ( ex . message )
112
+ . toEqual (
113
+ 'inTopK() expects the predictions to ' +
114
+ 'be of rank 2 or higher, but got 1' ) ;
115
+ }
94
116
} ) ;
95
117
96
- it ( 'throws when prediction_rank != targets_rank + 1' , ( ) => {
118
+ it ( 'throws when prediction.rank != targets.rank + 1' , async ( ) => {
97
119
const predictions = tensor2d ( [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ) ;
98
120
const targets = tensor2d ( [ [ 0 ] , [ 0 ] ] ) ;
99
- expect ( ( ) => tf . inTopK ( predictions , targets ) ) . toThrowError ( ) ;
121
+
122
+ // expect(...).toThrowError() does not support async functions.
123
+ // See https://github.com/jasmine/jasmine/issues/1410
124
+ try {
125
+ await tf . inTopKAsync ( predictions , targets ) ;
126
+ throw new Error ( 'The line above should have thrown an error' ) ;
127
+ } catch ( ex ) {
128
+ expect ( ex . message )
129
+ . toEqual (
130
+ 'predictions rank should be 1 larger than targets rank,' +
131
+ ' but got predictions rank 2 and targets rank 2' ) ;
132
+ }
100
133
} ) ;
101
134
102
- it ( 'throws when k > size of last dimension of predictions' , ( ) => {
135
+ it ( 'throws when k > size of last dimension of predictions' , async ( ) => {
103
136
const predictions = tensor2d ( [ [ 20 , 10 , 40 , 30 ] , [ 30 , 50 , - 20 , 10 ] ] ) ;
104
137
const targets = tensor1d ( [ 2 , 0 ] ) ;
105
138
const k = 5 ;
106
- expect ( ( ) => tf . inTopK ( predictions , targets , k ) ) . toThrowError ( ) ;
139
+
140
+ // expect(...).toThrowError() does not support async functions.
141
+ // See https://github.com/jasmine/jasmine/issues/1410
142
+ try {
143
+ await tf . inTopKAsync ( predictions , targets , k ) ;
144
+ throw new Error ( 'The line above should have thrown an error' ) ;
145
+ } catch ( ex ) {
146
+ expect ( ex . message )
147
+ . toEqual (
148
+ '\'k\' passed to inTopK() must be > 0 && <= the predictions ' +
149
+ 'last dimension (4), but got 5' ) ;
150
+ }
107
151
} ) ;
108
152
} ) ;
0 commit comments