UNPKG

3.23 kBMarkdownView Raw
1# TensorFlow.js Layers: High-Level Machine Learning Model API
2
3A part of the TensorFlow.js ecosystem, TensorFlow.js Layers is a high-level
4API built on [TensorFlow.js Core](/tfjs-core),
5enabling users to build, train and execute deep learning models in the browser.
6TensorFlow.js Layers is modeled after
7[Keras](https://keras.io/) and
8[tf.keras](https://www.tensorflow.org/api_docs/python/tf/keras) and can
9load models saved from those libraries.
10
11## Importing
12
13There are three ways to import TensorFlow.js Layers
14
151. You can access TensorFlow.js Layers through the union package
16 between the TensorFlow.js Core and Layers:
17 [@tensorflow/tfjs](https://www.npmjs.com/package/@tensorflow/tfjs)
182. You can get [TensorFlow.js](https://github.com/tensorflow/tfjs) Layers as a module:
19 [@tensorflow/tfjs-layers](https://www.npmjs.com/package/@tensorflow/tfjs-layers).
20 Note that `tfjs-layers` has peer dependency on tfjs-core, so if you import
21 `@tensorflow/tfjs-layers`, you also need to import
22 `@tensorflow/tfjs-core`.
233. As a standalone through [unpkg](https://unpkg.com/).
24
25Option 1 is the most convenient, but leads to a larger bundle size (we will be
26adding more packages to it in the future). Use option 2 if you care about bundle
27size.
28
29## Getting started
30
31### Building, training and executing a model
32
33The following example shows how to build a toy model with only one `dense` layer
34to perform linear regression.
35
36```js
37import * as tf from '@tensorflow/tfjs';
38
39// A sequential model is a container which you can add layers to.
40const model = tf.sequential();
41
42// Add a dense layer with 1 output unit.
43model.add(tf.layers.dense({units: 1, inputShape: [1]}));
44
45// Specify the loss type and optimizer for training.
46model.compile({loss: 'meanSquaredError', optimizer: 'SGD'});
47
48// Generate some synthetic data for training.
49const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
50const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
51
52// Train the model.
53await model.fit(xs, ys, {epochs: 500});
54
55// Ater the training, perform inference.
56const output = model.predict(tf.tensor2d([[5]], [1, 1]));
57output.print();
58```
59
60### Loading a pretrained Keras model
61
62You can also load a model previously trained and saved from elsewhere (e.g.,
63from Python Keras) and use it for inference or transfer learning in the browser.
64
65For example, in Python, save your Keras model using
66[tensorflowjs](https://pypi.org/project/tensorflowjs/),
67which can be installed using `pip install tensorflowjs`.
68
69
70```python
71import tensorflowjs as tfjs
72
73# ... Create and train your Keras model.
74
75# Save your Keras model in TensorFlow.js format.
76tfjs.converters.save_keras_model(model, '/path/to/tfjs_artifacts/')
77
78# Then use your favorite web server to serve the directory at a URL, say
79# http://foo.bar/tfjs_artifacts/model.json
80```
81
82To load the model with TensorFlow.js Layers:
83
84```js
85import * as tf from '@tensorflow/tfjs';
86
87const model = await tf.loadLayersModel('http://foo.bar/tfjs_artifacts/model.json');
88// Now the model is ready for inference, evaluation or re-training.
89```
90
91## For more information
92
93- [TensorFlow.js API documentation](https://js.tensorflow.org/api/latest/)
94- [TensorFlow.js Tutorials](https://js.tensorflow.org/tutorials/)