forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatter_nd_test.ts
More file actions
152 lines (138 loc) · 5.98 KB
/
Copy pathscatter_nd_test.ts
File metadata and controls
152 lines (138 loc) · 5.98 KB
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
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '../index';
import {ALL_ENVS, describeWithFlags} from '../jasmine_util';
import {expectArraysClose} from '../test_util';
describeWithFlags('scatterND', ALL_ENVS, () => {
it('should work for 2d', async () => {
const indices = tf.tensor1d([0, 4, 2], 'int32');
const updates = tf.tensor2d(
[100, 101, 102, 777, 778, 779, 1000, 1001, 1002], [3, 3], 'int32');
const shape = [5, 3];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(
await result.data(),
[100, 101, 102, 0, 0, 0, 1000, 1001, 1002, 0, 0, 0, 777, 778, 779]);
});
it('should work for simple 1d', async () => {
const indices = tf.tensor1d([3], 'int32');
const updates = tf.tensor1d([101], 'float32');
const shape = [5];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(await result.data(), [0, 0, 0, 101, 0]);
});
it('should work for multiple 1d', async () => {
const indices = tf.tensor1d([0, 4, 2], 'int32');
const updates = tf.tensor1d([100, 101, 102], 'float32');
const shape = [5];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(await result.data(), [100, 0, 102, 0, 101]);
});
it('should work for high rank updates', async () => {
const indices = tf.tensor2d([0, 2], [2, 1], 'int32');
const updates = tf.tensor3d(
[
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8
],
[2, 4, 4], 'float32');
const shape = [4, 4, 4];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(await result.data(), [
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
});
it('should work for high rank indices', async () => {
const indices = tf.tensor2d([0, 2, 0, 1], [2, 2], 'int32');
const updates = tf.tensor1d([10, 20], 'float32');
const shape = [3, 3];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(await result.data(), [0, 20, 10, 0, 0, 0, 0, 0, 0]);
});
it('should work for high rank indices and update', () => {
const indices = tf.tensor2d([1, 0, 0, 1, 0, 1], [3, 2], 'int32');
const updates = tf.ones([3, 256], 'float32');
const shape = [2, 2, 256];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
});
it('should sum the duplicated indices', async () => {
const indices = tf.tensor1d([0, 4, 2, 1, 3, 0], 'int32');
const updates = tf.tensor1d([10, 20, 30, 40, 50, 60], 'float32');
const shape = [8];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual(updates.dtype);
expectArraysClose(await result.data(), [70, 40, 30, 50, 20, 0, 0, 0]);
});
it('should work for tensorLike input', async () => {
const indices = [0, 4, 2];
const updates = [100, 101, 102];
const shape = [5];
const result = tf.scatterND(indices, updates, shape);
expect(result.shape).toEqual(shape);
expect(result.dtype).toEqual('float32');
expectArraysClose(await result.data(), [100, 0, 102, 0, 101]);
});
it('should throw error when indices type is not int32', () => {
const indices = tf.tensor2d([0, 2, 0, 1], [2, 2], 'float32');
const updates = tf.tensor1d([10, 20], 'float32');
const shape = [3, 3];
expect(() => tf.scatterND(indices, updates, shape)).toThrow();
});
it('should throw error when indices and update mismatch', () => {
const indices = tf.tensor2d([0, 4, 2], [3, 1], 'int32');
const updates = tf.tensor2d(
[100, 101, 102, 103, 777, 778, 779, 780, 10000, 10001, 10002, 10004],
[3, 4], 'float32');
const shape = [5, 3];
expect(() => tf.scatterND(indices, updates, shape)).toThrow();
});
it('should throw error when indices and update count mismatch', () => {
const indices = tf.tensor2d([0, 4, 2], [3, 1], 'int32');
const updates =
tf.tensor2d([100, 101, 102, 10000, 10001, 10002], [2, 3], 'float32');
const shape = [5, 3];
expect(() => tf.scatterND(indices, updates, shape)).toThrow();
});
it('should throw error when indices are scalar', () => {
const indices = tf.scalar(1, 'int32');
const updates =
tf.tensor2d([100, 101, 102, 10000, 10001, 10002], [2, 3], 'float32');
const shape = [5, 3];
expect(() => tf.scatterND(indices, updates, shape)).toThrow();
});
it('should throw error when update is scalar', () => {
const indices = tf.tensor2d([0, 4, 2], [3, 1], 'int32');
const updates = tf.scalar(1, 'float32');
const shape = [5, 3];
expect(() => tf.scatterND(indices, updates, shape)).toThrow();
});
});