-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathvariables.ts
359 lines (329 loc) · 11.5 KB
/
variables.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
import * as tfc from '@tensorflow/tfjs-core';
import {DataType, Tensor, variableGrads} from '@tensorflow/tfjs-core';
import {getNextUniqueTensorId} from './backend/state';
import {getScopedTensorName, getUniqueTensorName} from './common';
import {Constraint} from './constraints';
import {NotImplementedError} from './errors';
import {Shape} from './keras_format/common';
import {HasShape} from './types';
const DEFAULT_VARIABLE_NAME_PREFIX = 'Variable';
/**
* A `tf.layers.LayerVariable` is similar to a `tf.Tensor` in that it has a
* dtype and shape, but its value is mutable. The value is itself represented
* as a`tf.Tensor`, and can be read with the `read()` method and updated with
* the `write()` method.
*/
export class LayerVariable {
readonly dtype: DataType;
readonly shape: Shape;
readonly id: number;
// The fully scoped name of this Variable, including a unique suffix if needed
readonly name: string;
// The originally requested fully scoped name of this Variable, not including
// any unique suffix. This may be needed when restoring weights because this
// original name is used as a key.
readonly originalName: string;
private trainable_: boolean;
protected readonly val: tfc.Variable;
readonly constraint: Constraint;
/**
* Construct Variable from a `tf.Tensor`.
*
* If not explicitly named, the Variable will be given a name with the
* prefix 'Variable'. Variable names are unique. In the case of name
* collision, suffixies '_<num>' will be added to the name.
*
* @param val Initial value of the Variable.
* @param name Name of the variable. If `null` or `undefined` is provided, it
* will default a name with the prefix 'Variable'.
* @param constraint Optional, projection function to be applied to the
* variable after optimize updates
* @throws ValueError if `name` is `null` or `undefined`.
*/
constructor(
val: Tensor, dtype: DataType = 'float32',
name = DEFAULT_VARIABLE_NAME_PREFIX, trainable = true,
constraint: Constraint = null) {
this.dtype = dtype == null ? 'float32' : dtype;
this.shape = val.shape;
this.id = getNextUniqueTensorId();
name = name == null ? DEFAULT_VARIABLE_NAME_PREFIX : name;
this.originalName = getScopedTensorName(name);
this.name = getUniqueTensorName(this.originalName);
this.trainable_ = trainable;
this.constraint = constraint;
this.val = tfc.variable(val, this.trainable_, this.name, this.dtype);
}
/**
* Get a snapshot of the Variable's value.
*
* The returned value is a snapshot of the Variable's value at the time of
* the invocation. Future mutations in the value of the tensor will only
* be reflected by future calls to this method.
*/
read(): Tensor {
this.assertNotDisposed();
return this.val;
}
/**
* Update the value of the Variable.
*
* @param newVal: The new value to update to. Must be consistent with the
* dtype and shape of the Variable.
* @return This Variable.
*/
write(newVal: Tensor) {
// TODO(cais): Once TF.js Core supports Tensor.dtype, check dtype match.
this.assertNotDisposed();
checkShapesMatch(this.val, newVal);
// Skip updating if this is the exact same tensor.
if (this.val.id !== newVal.id) {
this.val.assign(newVal);
if (this.constraint != null) {
this.val.assign(this.constraint.apply(this.val));
}
}
return this;
}
/**
* Dispose this LayersVariable instance from memory.
*/
dispose(): void {
this.assertNotDisposed();
this.val.dispose();
}
protected assertNotDisposed(): void {
if (this.val.isDisposed) {
throw new Error(`LayersVariable ${this.name} is already disposed.`);
}
}
get trainable(): boolean {
return this.trainable_;
}
set trainable(trainable: boolean) {
this.trainable_ = trainable;
this.val.trainable = trainable;
}
}
function checkShapesMatch(x: HasShape, y: HasShape): void {
if (x.shape.toString() !== y.shape.toString()) {
throw new Error(
'Shape mismatch: ' + JSON.stringify(x.shape) + ' vs. ' +
JSON.stringify(y.shape));
}
}
/**
* Create a Variable.
* @param x The initial value of the `Variable`.
* @param dtype optional, the type of the variable.
* @param name optional, the name of the variable, default provided by
* Variable.
* @param constraint optional, a constraint to be applied after every update.
* @return The newly instantiated `Variable`.
*/
export function variable(
x: Tensor, dtype?: DataType, name?: string,
constraint?: Constraint): LayerVariable {
return new LayerVariable(x, dtype, name, true, constraint);
}
/**
* Instantiates an all-zeros Variable and returns it.
*
* @param shape Shape of the tensor.
* @param dtype DType of the tensor.
* @param name Name of the tensor.
* @return An all-zero Variable.
*/
export function zerosVariable(
shape: Shape, dtype?: DataType, name?: string): LayerVariable {
// TODO(cais): Implement logic for dtype.
return new LayerVariable(tfc.zeros(shape), dtype, name);
}
/**
* Instantiates an all-zeros tensor of the same shape as another tensor.
*
* @param x The other tensor.
* @param dtype DType of the tensor.
* @param name Name of the tensor.
* @return A newly instantiated Variable.
*/
export function zerosLike(
x: Tensor, dtype?: DataType, name?: string): LayerVariable {
return new LayerVariable(tfc.zerosLike(x), dtype, name);
}
/**
* Instantiates an all-ones tensor and returns it.
*
* @param shape Shape of the tensor.
* @param dtype DType of the tensor.
* @param name Name of the tensor.
* @return An all-ones Variable.
*/
export function onesVariable(
shape: Shape, dtype?: DataType, name?: string): LayerVariable {
// TODO(cais): Implement logic for dtype.
const allocated = tfc.ones(shape);
return new LayerVariable(allocated, dtype, name);
}
/**
* Instantiates an all-ones tensor of the same shape as another tensor.
*
* @param x The other tensor.
* @param dtype DType of the tensor.
* @param name Name of the tensor.
* @return A newly instantiated Variable.
*/
export function onesLike(
x: Tensor, dtype?: DataType, name?: string): LayerVariable {
const allocated = tfc.onesLike(x);
return new LayerVariable(allocated, dtype, name);
}
/**
* Instantiate an identity matrix and returns it, as a Variable
*
* @param size Number of rows/columns.
* @param dtype Data type of returned Variable.
* @param name Name of returned Variable.
* @return A Variable, an identity matrix.
*/
export function eyeVariable(
size: number, dtype?: DataType, name?: string): LayerVariable {
return new LayerVariable(tfc.eye(size), dtype, name);
}
/**
* Get a Variable with uniform distribution of values.
* @param shape Shape of the tensor.
* @param minval Lower bound of the uniform distribution.
* @param maxval Upper bound of the uniform distribution.
* @param dtype
* @param seed
* @param name Optional name.
* @return The uniform-random Variable.
*/
export function randomUniformVariable(
shape: Shape, minval: number, maxval: number, dtype?: DataType,
seed?: number, name = 'randomUniform'): LayerVariable {
return new LayerVariable(
tfc.randomUniform(shape, minval, maxval, dtype), dtype, name);
}
/**
* Get a Variable with truncated-normal distribution of values.
* @param shape Shape of the tensor.
* @param mean mean value of the normal distribution.
* @param stddev standard deviation of the normal distribution.
* @param dtype
* @param seed
* @param name Optional name.
* @return The truncated-normal-random Variable.
*/
export function truncatedNormalVariable(
shape: Shape, mean = 0.0, stddev = 1.0, dtype?: DataType, seed?: number,
name = 'truncatedNormal'): LayerVariable {
// TODO(cais): Implement logic for dtype and seed once they are supported
// by deeplearn.js.
dtype = dtype || 'float32';
if (dtype !== 'float32' && dtype !== 'int32') {
throw new NotImplementedError(
`randomNormal does not support dType ${dtype}.`);
}
return new LayerVariable(
tfc.truncatedNormal(shape, mean, stddev, dtype, seed), dtype, name);
}
/**
* Get a Variable with normal distribution of values.
* @param shape Shape of the tensor.
* @param mean mean value of the normal distribution.
* @param stddev standard deviation of the normal distribution.
* @param dtype
* @param seed
* @param name Optional name.
* @return The truncated-normal-random Variable.
*/
export function randomNormalVariable(
shape: Shape, mean = 0.0, stddev = 1.0, dtype?: DataType, seed?: number,
name = 'randomNormal'): LayerVariable {
dtype = dtype || 'float32';
if (dtype !== 'float32' && dtype !== 'int32') {
throw new NotImplementedError(
`randomNormalVariable does not support dType ${dtype}.`);
}
return new LayerVariable(
tfc.randomNormal(shape, mean, stddev, dtype, seed), dtype, name);
}
/**
* Update the value of a Variable.
* @param x The Variable to be updated.
* @param xNew The new value to update to.
* @return The Variable updated.
*/
export function update(x: LayerVariable, xNew: Tensor): LayerVariable {
return x.write(xNew);
}
/**
* Update the value of a Variable by adding an increment.
* @param x The Variable to be updated.
* @param increment The incrment to add to `x`.
* @return The Variable updated.
*/
export function updateAdd(x: LayerVariable, increment: Tensor): LayerVariable {
return x.write(tfc.add(x.read(), increment));
}
/**
* Update the value of a Variable by subtracting a decrement.
* @param x The Variable to be updated.
* @param decrement The decrement to subtract from `x`.
* @return The Variable updated.
*/
export function updateSub(x: LayerVariable, decrement: Tensor): LayerVariable {
return x.write(tfc.sub(x.read(), decrement));
}
/**
* Get the values of an array of Variables.
*
* @param tensors An `Array` of `Variable`s to get the values of.
* @return The values of the inputs, as an `Array` of`tf.Tensor`s.
*/
export function batchGetValue(xs: LayerVariable[]): Tensor[] {
return xs.map(x => x.read());
}
/**
* Update the value of multiple Variables at once.
*
* @param variablesAndValues An `Array`, each element is of type
* [Variable, Tensor]. The first item is the
* `Variable` of which the value is to be updated. The second item
* carries the new value.
*/
export function batchSetValue(
variablesAndValues: Array<[LayerVariable, Tensor]>): void {
variablesAndValues.forEach(variableAndValue => {
const variable: LayerVariable = variableAndValue[0];
variable.write(variableAndValue[1]);
});
}
/**
* Returns the gradients of `variables` w.r.t. the return value of `lossFn`.
* @param lossFn A function which returns a Scalar to be used as the function
* value (i.e., numerator) for differentiation.
* @param variables List of variables to be used as the independent variables
* (i.e., denominator) for differentiation.
* @returns An Array of gradients tensors.
*/
export function gradients(
lossFn: () => tfc.Scalar, variables: LayerVariable[]): Tensor[] {
// TODO(cais): The return type signature can be simplified if deeplearn makes
// the corresponding type public.
const variableList =
variables.map(variable => variable.read() as tfc.Variable);
const valudAndGrads = variableGrads(lossFn, variableList);
return variables.map(variable => valudAndGrads.grads[variable.name]);
}