1 | # TensorFlow.js Layers: High-Level Machine Learning Model API
|
2 |
|
3 | A part of the TensorFlow.js ecosystem, TensorFlow.js Layers is a high-level
|
4 | API built on [TensorFlow.js Core](/tfjs-core),
|
5 | enabling users to build, train and execute deep learning models in the browser.
|
6 | TensorFlow.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
|
9 | load models saved from those libraries.
|
10 |
|
11 | ## Importing
|
12 |
|
13 | There are three ways to import TensorFlow.js Layers
|
14 |
|
15 | 1. 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)
|
18 | 2. 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`.
|
23 | 3. As a standalone through [unpkg](https://unpkg.com/).
|
24 |
|
25 | Option 1 is the most convenient, but leads to a larger bundle size (we will be
|
26 | adding more packages to it in the future). Use option 2 if you care about bundle
|
27 | size.
|
28 |
|
29 | ## Getting started
|
30 |
|
31 | ### Building, training and executing a model
|
32 |
|
33 | The following example shows how to build a toy model with only one `dense` layer
|
34 | to perform linear regression.
|
35 |
|
36 | ```js
|
37 | import * as tf from '@tensorflow/tfjs';
|
38 |
|
39 | // A sequential model is a container which you can add layers to.
|
40 | const model = tf.sequential();
|
41 |
|
42 | // Add a dense layer with 1 output unit.
|
43 | model.add(tf.layers.dense({units: 1, inputShape: [1]}));
|
44 |
|
45 | // Specify the loss type and optimizer for training.
|
46 | model.compile({loss: 'meanSquaredError', optimizer: 'SGD'});
|
47 |
|
48 | // Generate some synthetic data for training.
|
49 | const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
|
50 | const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
|
51 |
|
52 | // Train the model.
|
53 | await model.fit(xs, ys, {epochs: 500});
|
54 |
|
55 | // After the training, perform inference.
|
56 | const output = model.predict(tf.tensor2d([[5]], [1, 1]));
|
57 | output.print();
|
58 | ```
|
59 |
|
60 | ### Loading a pretrained Keras model
|
61 |
|
62 | You can also load a model previously trained and saved from elsewhere (e.g.,
|
63 | from Python Keras) and use it for inference or transfer learning in the browser.
|
64 |
|
65 | For example, in Python, save your Keras model using
|
66 | [tensorflowjs](https://pypi.org/project/tensorflowjs/),
|
67 | which can be installed using `pip install tensorflowjs`.
|
68 |
|
69 |
|
70 | ```python
|
71 | import tensorflowjs as tfjs
|
72 |
|
73 | # ... Create and train your Keras model.
|
74 |
|
75 | # Save your Keras model in TensorFlow.js format.
|
76 | tfjs.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 |
|
82 | To load the model with TensorFlow.js Layers:
|
83 |
|
84 | ```js
|
85 | import * as tf from '@tensorflow/tfjs';
|
86 |
|
87 | const 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/)
|