UNPKG

7.97 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC
4 *
5 * Use of this source code is governed by an MIT-style
6 * license that can be found in the LICENSE file or at
7 * https://opensource.org/licenses/MIT.
8 * =============================================================================
9 */
10/// <amd-module name="@tensorflow/tfjs-layers/dist/variables" />
11import * as tfc from '@tensorflow/tfjs-core';
12import { DataType, Tensor } from '@tensorflow/tfjs-core';
13import { Constraint } from './constraints';
14import { Shape } from './keras_format/common';
15/**
16 * A `tf.layers.LayerVariable` is similar to a `tf.Tensor` in that it has a
17 * dtype and shape, but its value is mutable. The value is itself represented
18 * as a`tf.Tensor`, and can be read with the `read()` method and updated with
19 * the `write()` method.
20 */
21export declare class LayerVariable {
22 readonly dtype: DataType;
23 readonly shape: Shape;
24 readonly id: number;
25 readonly name: string;
26 readonly originalName: string;
27 private trainable_;
28 protected readonly val: tfc.Variable;
29 readonly constraint: Constraint;
30 /**
31 * Construct Variable from a `tf.Tensor`.
32 *
33 * If not explicitly named, the Variable will be given a name with the
34 * prefix 'Variable'. Variable names are unique. In the case of name
35 * collision, suffixies '_<num>' will be added to the name.
36 *
37 * @param val Initial value of the Variable.
38 * @param name Name of the variable. If `null` or `undefined` is provided, it
39 * will default a name with the prefix 'Variable'.
40 * @param constraint Optional, projection function to be applied to the
41 * variable after optimize updates
42 * @throws ValueError if `name` is `null` or `undefined`.
43 */
44 constructor(val: Tensor, dtype?: DataType, name?: string, trainable?: boolean, constraint?: Constraint);
45 /**
46 * Get a snapshot of the Variable's value.
47 *
48 * The returned value is a snapshot of the Variable's value at the time of
49 * the invocation. Future mutations in the value of the tensor will only
50 * be reflected by future calls to this method.
51 */
52 read(): Tensor;
53 /**
54 * Update the value of the Variable.
55 *
56 * @param newVal: The new value to update to. Must be consistent with the
57 * dtype and shape of the Variable.
58 * @return This Variable.
59 */
60 write(newVal: Tensor): this;
61 /**
62 * Dispose this LayersVariable instance from memory.
63 */
64 dispose(): void;
65 protected assertNotDisposed(): void;
66 trainable: boolean;
67}
68/**
69 * Create a Variable.
70 * @param x The initial value of the `Variable`.
71 * @param dtype optional, the type of the variable.
72 * @param name optional, the name of the variable, default provided by
73 * Variable.
74 * @param constraint optional, a constraint to be applied after every update.
75 * @return The newly instantiated `Variable`.
76 */
77export declare function variable(x: Tensor, dtype?: DataType, name?: string, constraint?: Constraint): LayerVariable;
78/**
79 * Instantiates an all-zeros Variable and returns it.
80 *
81 * @param shape Shape of the tensor.
82 * @param dtype DType of the tensor.
83 * @param name Name of the tensor.
84 * @return An all-zero Variable.
85 */
86export declare function zerosVariable(shape: Shape, dtype?: DataType, name?: string): LayerVariable;
87/**
88 * Instantiates an all-zeros tensor of the same shape as another tensor.
89 *
90 * @param x The other tensor.
91 * @param dtype DType of the tensor.
92 * @param name Name of the tensor.
93 * @return A newly instantiated Variable.
94 */
95export declare function zerosLike(x: Tensor, dtype?: DataType, name?: string): LayerVariable;
96/**
97 * Instantiates an all-ones tensor and returns it.
98 *
99 * @param shape Shape of the tensor.
100 * @param dtype DType of the tensor.
101 * @param name Name of the tensor.
102 * @return An all-ones Variable.
103 */
104export declare function onesVariable(shape: Shape, dtype?: DataType, name?: string): LayerVariable;
105/**
106 * Instantiates an all-ones tensor of the same shape as another tensor.
107 *
108 * @param x The other tensor.
109 * @param dtype DType of the tensor.
110 * @param name Name of the tensor.
111 * @return A newly instantiated Variable.
112 */
113export declare function onesLike(x: Tensor, dtype?: DataType, name?: string): LayerVariable;
114/**
115 * Instantiate an identity matrix and returns it, as a Variable
116 *
117 * @param size Number of rows/columns.
118 * @param dtype Data type of returned Variable.
119 * @param name Name of returned Variable.
120 * @return A Variable, an identity matrix.
121 */
122export declare function eyeVariable(size: number, dtype?: DataType, name?: string): LayerVariable;
123/**
124 * Get a Variable with uniform distribution of values.
125 * @param shape Shape of the tensor.
126 * @param minval Lower bound of the uniform distribution.
127 * @param maxval Upper bound of the uniform distribution.
128 * @param dtype
129 * @param seed
130 * @param name Optional name.
131 * @return The uniform-random Variable.
132 */
133export declare function randomUniformVariable(shape: Shape, minval: number, maxval: number, dtype?: DataType, seed?: number, name?: string): LayerVariable;
134/**
135 * Get a Variable with truncated-normal distribution of values.
136 * @param shape Shape of the tensor.
137 * @param mean mean value of the normal distribution.
138 * @param stddev standard deviation of the normal distribution.
139 * @param dtype
140 * @param seed
141 * @param name Optional name.
142 * @return The truncated-normal-random Variable.
143 */
144export declare function truncatedNormalVariable(shape: Shape, mean?: number, stddev?: number, dtype?: DataType, seed?: number, name?: string): LayerVariable;
145/**
146 * Get a Variable with normal distribution of values.
147 * @param shape Shape of the tensor.
148 * @param mean mean value of the normal distribution.
149 * @param stddev standard deviation of the normal distribution.
150 * @param dtype
151 * @param seed
152 * @param name Optional name.
153 * @return The truncated-normal-random Variable.
154 */
155export declare function randomNormalVariable(shape: Shape, mean?: number, stddev?: number, dtype?: DataType, seed?: number, name?: string): LayerVariable;
156/**
157 * Update the value of a Variable.
158 * @param x The Variable to be updated.
159 * @param xNew The new value to update to.
160 * @return The Variable updated.
161 */
162export declare function update(x: LayerVariable, xNew: Tensor): LayerVariable;
163/**
164 * Update the value of a Variable by adding an increment.
165 * @param x The Variable to be updated.
166 * @param increment The incrment to add to `x`.
167 * @return The Variable updated.
168 */
169export declare function updateAdd(x: LayerVariable, increment: Tensor): LayerVariable;
170/**
171 * Update the value of a Variable by subtracting a decrement.
172 * @param x The Variable to be updated.
173 * @param decrement The decrement to subtract from `x`.
174 * @return The Variable updated.
175 */
176export declare function updateSub(x: LayerVariable, decrement: Tensor): LayerVariable;
177/**
178 * Get the values of an array of Variables.
179 *
180 * @param tensors An `Array` of `Variable`s to get the values of.
181 * @return The values of the inputs, as an `Array` of`tf.Tensor`s.
182 */
183export declare function batchGetValue(xs: LayerVariable[]): Tensor[];
184/**
185 * Update the value of multiple Variables at once.
186 *
187 * @param variablesAndValues An `Array`, each element is of type
188 * [Variable, Tensor]. The first item is the
189 * `Variable` of which the value is to be updated. The second item
190 * carries the new value.
191 */
192export declare function batchSetValue(variablesAndValues: Array<[LayerVariable, Tensor]>): void;
193/**
194 * Returns the gradients of `variables` w.r.t. the return value of `lossFn`.
195 * @param lossFn A function which returns a Scalar to be used as the function
196 * value (i.e., numerator) for differentiation.
197 * @param variables List of variables to be used as the independent variables
198 * (i.e., denominator) for differentiation.
199 * @returns An Array of gradients tensors.
200 */
201export declare function gradients(lossFn: () => tfc.Scalar, variables: LayerVariable[]): Tensor[];