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/exports" />
|
11 | /**
|
12 | * Exported functions.
|
13 | */
|
14 | import { io } from '@tensorflow/tfjs-core';
|
15 | import { BaseCallbackConstructor } from './base_callbacks';
|
16 | import { ContainerArgs } from './engine/container';
|
17 | import { InputConfig } from './engine/input_layer';
|
18 | import { SymbolicTensor } from './engine/topology';
|
19 | import { LayersModel } from './engine/training';
|
20 | import { Sequential, SequentialArgs } from './models';
|
21 | /**
|
22 | * A model is a data structure that consists of `Layers` and defines inputs
|
23 | * and outputs.
|
24 | *
|
25 | * The key difference between `tf.model` and `tf.sequential` is that
|
26 | * `tf.model` is more generic, supporting an arbitrary graph (without
|
27 | * cycles) of layers. `tf.sequential` is less generic and supports only a linear
|
28 | * stack of layers.
|
29 | *
|
30 | * When creating a `tf.LayersModel`, specify its input(s) and output(s). Layers
|
31 | * are used to wire input(s) to output(s).
|
32 | *
|
33 | * For example, the following code snippet defines a model consisting of
|
34 | * two `dense` layers, with 10 and 4 units, respectively.
|
35 | *
|
36 | * ```js
|
37 | * // Define input, which has a size of 5 (not including batch dimension).
|
38 | * const input = tf.input({shape: [5]});
|
39 | *
|
40 | * // First dense layer uses relu activation.
|
41 | * const denseLayer1 = tf.layers.dense({units: 10, activation: 'relu'});
|
42 | * // Second dense layer uses softmax activation.
|
43 | * const denseLayer2 = tf.layers.dense({units: 4, activation: 'softmax'});
|
44 | *
|
45 | * // Obtain the output symbolic tensor by applying the layers on the input.
|
46 | * const output = denseLayer2.apply(denseLayer1.apply(input));
|
47 | *
|
48 | * // Create the model based on the inputs.
|
49 | * const model = tf.model({inputs: input, outputs: output});
|
50 | *
|
51 | * // The model can be used for training, evaluation and prediction.
|
52 | * // For example, the following line runs prediction with the model on
|
53 | * // some fake data.
|
54 | * model.predict(tf.ones([2, 5])).print();
|
55 | * ```
|
56 | * See also:
|
57 | * `tf.sequential`, `tf.loadLayersModel`.
|
58 | *
|
59 | * @doc {heading: 'Models', subheading: 'Creation'}
|
60 | */
|
61 | export declare function model(args: ContainerArgs): LayersModel;
|
62 | /**
|
63 | * Creates a `tf.Sequential` model. A sequential model is any model where the
|
64 | * outputs of one layer are the inputs to the next layer, i.e. the model
|
65 | * topology is a simple 'stack' of layers, with no branching or skipping.
|
66 | *
|
67 | * This means that the first layer passed to a `tf.Sequential` model should have
|
68 | * a defined input shape. What that means is that it should have received an
|
69 | * `inputShape` or `batchInputShape` argument, or for some type of layers
|
70 | * (recurrent, Dense...) an `inputDim` argument.
|
71 | *
|
72 | * The key difference between `tf.model` and `tf.sequential` is that
|
73 | * `tf.sequential` is less generic, supporting only a linear stack of layers.
|
74 | * `tf.model` is more generic and supports an arbitrary graph (without
|
75 | * cycles) of layers.
|
76 | *
|
77 | * Examples:
|
78 | *
|
79 | * ```js
|
80 | * const model = tf.sequential();
|
81 | *
|
82 | * // First layer must have an input shape defined.
|
83 | * model.add(tf.layers.dense({units: 32, inputShape: [50]}));
|
84 | * // Afterwards, TF.js does automatic shape inference.
|
85 | * model.add(tf.layers.dense({units: 4}));
|
86 | *
|
87 | * // Inspect the inferred shape of the model's output, which equals
|
88 | * // `[null, 4]`. The 1st dimension is the undetermined batch dimension; the
|
89 | * // 2nd is the output size of the model's last layer.
|
90 | * console.log(JSON.stringify(model.outputs[0].shape));
|
91 | * ```
|
92 | *
|
93 | * It is also possible to specify a batch size (with potentially undetermined
|
94 | * batch dimension, denoted by "null") for the first layer using the
|
95 | * `batchInputShape` key. The following example is equivalent to the above:
|
96 | *
|
97 | * ```js
|
98 | * const model = tf.sequential();
|
99 | *
|
100 | * // First layer must have a defined input shape
|
101 | * model.add(tf.layers.dense({units: 32, batchInputShape: [null, 50]}));
|
102 | * // Afterwards, TF.js does automatic shape inference.
|
103 | * model.add(tf.layers.dense({units: 4}));
|
104 | *
|
105 | * // Inspect the inferred shape of the model's output.
|
106 | * console.log(JSON.stringify(model.outputs[0].shape));
|
107 | * ```
|
108 | *
|
109 | * You can also use an `Array` of already-constructed `Layer`s to create
|
110 | * a `tf.Sequential` model:
|
111 | *
|
112 | * ```js
|
113 | * const model = tf.sequential({
|
114 | * layers: [tf.layers.dense({units: 32, inputShape: [50]}),
|
115 | * tf.layers.dense({units: 4})]
|
116 | * });
|
117 | * console.log(JSON.stringify(model.outputs[0].shape));
|
118 | * ```
|
119 | *
|
120 | * @doc {heading: 'Models', subheading: 'Creation'}
|
121 | */
|
122 | export declare function sequential(config?: SequentialArgs): Sequential;
|
123 | /**
|
124 | * Load a model composed of Layer objects, including its topology and optionally
|
125 | * weights. See the Tutorial named "How to import a Keras Model" for usage
|
126 | * examples.
|
127 | *
|
128 | * This method is applicable to:
|
129 | *
|
130 | * 1. Models created with the `tf.layers.*`, `tf.sequential`, and
|
131 | * `tf.model` APIs of TensorFlow.js and later saved with the
|
132 | * `tf.LayersModel.save` method.
|
133 | * 2. Models converted from Keras or TensorFlow tf.keras using the
|
134 | * [tensorflowjs_converter](https://github.com/tensorflow/tfjs/tree/master/tfjs-converter).
|
135 | *
|
136 | * This mode is *not* applicable to TensorFlow `SavedModel`s or their converted
|
137 | * forms. For those models, use `tf.loadGraphModel`.
|
138 | *
|
139 | * Example 1. Load a model from an HTTP server.
|
140 | *
|
141 | * ```js
|
142 | * const model = await tf.loadLayersModel(
|
143 | * 'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json');
|
144 | * model.summary();
|
145 | * ```
|
146 | *
|
147 | * Example 2: Save `model`'s topology and weights to browser [local
|
148 | * storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage);
|
149 | * then load it back.
|
150 | *
|
151 | * ```js
|
152 | * const model = tf.sequential(
|
153 | * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]});
|
154 | * console.log('Prediction from original model:');
|
155 | * model.predict(tf.ones([1, 3])).print();
|
156 | *
|
157 | * const saveResults = await model.save('localstorage://my-model-1');
|
158 | *
|
159 | * const loadedModel = await tf.loadLayersModel('localstorage://my-model-1');
|
160 | * console.log('Prediction from loaded model:');
|
161 | * loadedModel.predict(tf.ones([1, 3])).print();
|
162 | * ```
|
163 | *
|
164 | * Example 3. Saving `model`'s topology and weights to browser
|
165 | * [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API);
|
166 | * then load it back.
|
167 | *
|
168 | * ```js
|
169 | * const model = tf.sequential(
|
170 | * {layers: [tf.layers.dense({units: 1, inputShape: [3]})]});
|
171 | * console.log('Prediction from original model:');
|
172 | * model.predict(tf.ones([1, 3])).print();
|
173 | *
|
174 | * const saveResults = await model.save('indexeddb://my-model-1');
|
175 | *
|
176 | * const loadedModel = await tf.loadLayersModel('indexeddb://my-model-1');
|
177 | * console.log('Prediction from loaded model:');
|
178 | * loadedModel.predict(tf.ones([1, 3])).print();
|
179 | * ```
|
180 | *
|
181 | * Example 4. Load a model from user-selected files from HTML
|
182 | * [file input
|
183 | * elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file).
|
184 | *
|
185 | * ```js
|
186 | * // Note: this code snippet will not work without the HTML elements in the
|
187 | * // page
|
188 | * const jsonUpload = document.getElementById('json-upload');
|
189 | * const weightsUpload = document.getElementById('weights-upload');
|
190 | *
|
191 | * const model = await tf.loadLayersModel(
|
192 | * tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]));
|
193 | * ```
|
194 | *
|
195 | * @param pathOrIOHandler Can be either of the two formats
|
196 | * 1. A string path to the `ModelAndWeightsConfig` JSON describing
|
197 | * the model in the canonical TensorFlow.js format. For file://
|
198 | * (tfjs-node-only), http:// and https:// schemas, the path can be
|
199 | * either absolute or relative.
|
200 | * 2. An `tf.io.IOHandler` object that loads model artifacts with its `load`
|
201 | * method.
|
202 | * @param options Optional configuration arguments for the model loading,
|
203 | * including:
|
204 | * - `strict`: Require that the provided weights exactly match those required
|
205 | * by the layers. Default true. Passing false means that both extra
|
206 | * weights and missing weights will be silently ignored.
|
207 | * - `onProgress`: A function of the signature `(fraction: number) => void',
|
208 | * that can be used as the progress callback for the model loading.
|
209 | * @returns A `Promise` of `tf.LayersModel`, with the topology and weights
|
210 | * loaded.
|
211 | *
|
212 | * @doc {heading: 'Models', subheading: 'Loading'}
|
213 | */
|
214 | export declare function loadLayersModel(pathOrIOHandler: string | io.IOHandler, options?: io.LoadOptions): Promise<LayersModel>;
|
215 | /**
|
216 | * Used to instantiate an input to a model as a `tf.SymbolicTensor`.
|
217 | *
|
218 | * Users should call the `input` factory function for
|
219 | * consistency with other generator functions.
|
220 | *
|
221 | * Example:
|
222 | *
|
223 | * ```js
|
224 | * // Defines a simple logistic regression model with 32 dimensional input
|
225 | * // and 3 dimensional output.
|
226 | * const x = tf.input({shape: [32]});
|
227 | * const y = tf.layers.dense({units: 3, activation: 'softmax'}).apply(x);
|
228 | * const model = tf.model({inputs: x, outputs: y});
|
229 | * model.predict(tf.ones([2, 32])).print();
|
230 | * ```
|
231 | *
|
232 | * Note: `input` is only necessary when using `model`. When using
|
233 | * `sequential`, specify `inputShape` for the first layer or use `inputLayer`
|
234 | * as the first layer.
|
235 | *
|
236 | * @doc {heading: 'Models', subheading: 'Inputs'}
|
237 | */
|
238 | export declare function input(config: InputConfig): SymbolicTensor;
|
239 | export declare function registerCallbackConstructor(verbosityLevel: number, callbackConstructor: BaseCallbackConstructor): void;
|