forked from tensorflow/tfjs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable_test.ts
173 lines (141 loc) · 5.19 KB
/
variable_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @license
* Copyright 2017 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';
// tslint:disable-next-line:max-line-length
import {Scalar, Tensor, Tensor1D, Tensor2D, Tensor3D, Tensor4D, variable, Variable} from './tensor';
import {ALL_ENVS, expectArraysClose} from './test_util';
import {describeWithFlags} from './jasmine_util';
import {Rank} from './types';
describeWithFlags('variable', ALL_ENVS, () => {
it('simple assign', () => {
const v = variable(tf.tensor1d([1, 2, 3]));
expectArraysClose(v, [1, 2, 3]);
v.assign(tf.tensor1d([4, 5, 6]));
expectArraysClose(v, [4, 5, 6]);
});
it('simple chain assign', () => {
const v = tf.tensor1d([1, 2, 3]).variable();
expectArraysClose(v, [1, 2, 3]);
v.assign(tf.tensor1d([4, 5, 6]));
expectArraysClose(v, [4, 5, 6]);
});
it('default names are unique', () => {
const v = variable(tf.tensor1d([1, 2, 3]));
expect(v.name).not.toBeNull();
const v2 = variable(tf.tensor1d([1, 2, 3]));
expect(v2.name).not.toBeNull();
expect(v.name).not.toBe(v2.name);
});
it('user provided name', () => {
const v = variable(tf.tensor1d([1, 2, 3]), true, 'myName');
expect(v.name).toBe('myName');
});
it('if name already used, throw error', () => {
variable(tf.tensor1d([1, 2, 3]), true, 'myName');
expect(() => variable(tf.tensor1d([1, 2, 3]), true, 'myName'))
.toThrowError();
});
it('ops can take variables', () => {
const value = tf.tensor1d([1, 2, 3]);
const v = variable(value);
const res = tf.sum(v);
expectArraysClose(res, [6]);
});
it('chained variables works', () => {
const v = tf.tensor1d([1, 2, 3]).variable();
const res = tf.sum(v);
expectArraysClose(res, [6]);
});
it('variables are not affected by tidy', () => {
let v: Variable<Rank.R1>;
expect(tf.memory().numTensors).toBe(0);
tf.tidy(() => {
const value = tf.tensor1d([1, 2, 3], 'float32');
expect(tf.memory().numTensors).toBe(1);
v = variable(value);
expect(tf.memory().numTensors).toBe(2);
});
expect(tf.memory().numTensors).toBe(1);
expectArraysClose(v, [1, 2, 3]);
v.dispose();
expect(tf.memory().numTensors).toBe(0);
});
it('constructor does not dispose', () => {
const a = tf.scalar(2);
const v = tf.variable(a);
expect(tf.memory().numTensors).toBe(2);
expect(tf.memory().numDataBuffers).toBe(1);
expectArraysClose(v, [2]);
expectArraysClose(a, [2]);
});
it('variables are assignable to tensors', () => {
// This test asserts compilation, not doing any run-time assertion.
const x0: Variable<Rank.R0> = null;
const y0: Scalar = x0;
expect(y0).toBeNull();
const x1: Variable<Rank.R1> = null;
const y1: Tensor1D = x1;
expect(y1).toBeNull();
const x2: Variable<Rank.R2> = null;
const y2: Tensor2D = x2;
expect(y2).toBeNull();
const x3: Variable<Rank.R3> = null;
const y3: Tensor3D = x3;
expect(y3).toBeNull();
const x4: Variable<Rank.R4> = null;
const y4: Tensor4D = x4;
expect(y4).toBeNull();
const xh: Variable = null;
const yh: Tensor = xh;
expect(yh).toBeNull();
});
it('assign does not dispose old data', () => {
let v: Variable<Rank.R1>;
v = variable(tf.tensor1d([1, 2, 3]));
expect(tf.memory().numTensors).toBe(2);
expect(tf.memory().numDataBuffers).toBe(1);
expectArraysClose(v, [1, 2, 3]);
const secondArray = tf.tensor1d([4, 5, 6]);
expect(tf.memory().numTensors).toBe(3);
expect(tf.memory().numDataBuffers).toBe(2);
v.assign(secondArray);
expectArraysClose(v, [4, 5, 6]);
// Assign doesn't dispose the 1st array.
expect(tf.memory().numTensors).toBe(3);
expect(tf.memory().numDataBuffers).toBe(2);
v.dispose();
// Disposing the variable disposes itself. The input to variable and
// secondArray are the only remaining tensors.
expect(tf.memory().numTensors).toBe(2);
expect(tf.memory().numDataBuffers).toBe(2);
});
it('shape must match', () => {
const v = variable(tf.tensor1d([1, 2, 3]));
expect(() => v.assign(tf.tensor1d([1, 2]))).toThrowError();
// tslint:disable-next-line:no-any
expect(() => v.assign(tf.tensor2d([3, 4], [1, 2]) as any)).toThrowError();
});
it('dtype must match', () => {
const v = variable(tf.tensor1d([1, 2, 3]));
// tslint:disable-next-line:no-any
expect(() => v.assign(tf.tensor1d([1, 1, 1], 'int32') as any))
.toThrowError();
// tslint:disable-next-line:no-any
expect(() => v.assign(tf.tensor1d([true, false, true], 'bool') as any))
.toThrowError();
});
});