UNPKG

7.76 kBMarkdownView Raw
1# TensorFlow.js
2
3TensorFlow.js is an open-source hardware-accelerated JavaScript library for
4training and deploying machine learning models.
5
6
7**Develop ML in the Browser** <br/>
8Use flexible and intuitive APIs to build models from scratch using the low-level
9JavaScript linear algebra library or the high-level layers API.
10
11**Develop ML in Node.js** <br/>
12Execute native TensorFlow with the same TensorFlow.js API under the Node.js
13runtime.
14
15**Run Existing models** <br/>
16Use TensorFlow.js model converters to run pre-existing TensorFlow models right
17in the browser.
18
19**Retrain Existing models** <br/>
20Retrain pre-existing ML models using sensor data connected to the browser or
21other client-side data.
22
23## About this repo
24
25This repository contains the logic and scripts that combine
26several packages.
27
28APIs:
29- [TensorFlow.js Core](/tfjs-core),
30 a flexible low-level API for neural networks and numerical computation.
31- [TensorFlow.js Layers](/tfjs-layers),
32 a high-level API which implements functionality similar to
33 [Keras](https://keras.io/).
34- [TensorFlow.js Data](/tfjs-data),
35 a simple API to load and prepare data analogous to
36 [tf.data](https://www.tensorflow.org/guide/datasets).
37- [TensorFlow.js Converter](/tfjs-converter),
38 tools to import a TensorFlow SavedModel to TensorFlow.js
39- [TensorFlow.js Vis](/tfjs-vis),
40 in-browser visualization for TensorFlow.js models
41- [TensorFlow.js AutoML](/tfjs-automl),
42 Set of APIs to load and run models produced by
43 [AutoML Edge](https://cloud.google.com/vision/automl/docs/edge-quickstart).
44
45
46Backends/Platforms:
47- [TensorFlow.js CPU Backend](/tfjs-backend-cpu), pure-JS backend for Node.js and the browser.
48- [TensorFlow.js WebGL Backend](/tfjs-backend-webgl), WebGL backend for the browser.
49- [TensorFlow.js WASM Backend](/tfjs-backend-wasm), WebAssembly backend for the browser.
50- [TensorFlow.js WebGPU](/tfjs-backend-webgpu), WebGPU backend for the browser.
51- [TensorFlow.js Node](/tfjs-node), Node.js platform via TensorFlow C++ adapter.
52- [TensorFlow.js React Native](/tfjs-react-native), React Native platform via expo-gl adapter.
53
54If you care about bundle size, you can import those packages individually.
55
56If you are looking for Node.js support, check out the [TensorFlow.js Node directory](/tfjs-node).
57
58## Examples
59
60Check out our
61[examples repository](https://github.com/tensorflow/tfjs-examples)
62and our [tutorials](https://js.tensorflow.org/tutorials/).
63
64## Gallery
65
66Be sure to check out [the gallery](GALLERY.md) of all projects related to TensorFlow.js.
67
68## Pre-trained models
69
70Be sure to also check out our [models repository](https://github.com/tensorflow/tfjs-models) where we host pre-trained models
71on NPM.
72
73## Benchmarks
74
75* [Local benchmark tool](https://tfjs-benchmarks.web.app/). Use this webpage tool to collect the performance related metrics (speed, memory, etc) of TensorFlow.js models and kernels **on your local device** with CPU, WebGL or WASM backends. You can benchmark custom models by following this [guide](https://github.com/tensorflow/tfjs/blob/master/e2e/benchmarks/local-benchmark/README.md).
76* [Multi-device benchmark tool](https://github.com/tensorflow/tfjs/tree/master/e2e/benchmarks/browserstack-benchmark/README.md). Use this tool to collect the same performance related metrics **on a collection of remote devices**.
77
78## Getting started
79
80There are two main ways to get TensorFlow.js in your JavaScript project:
81via <a href="https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_JavaScript_within_a_webpage" target="_blank">script tags</a> <strong>or</strong> by installing it from <a href="https://www.npmjs.com/" target="_blank">NPM</a>
82and using a build tool like <a href="https://parceljs.org/" target="_blank">Parcel</a>,
83<a href="https://webpack.js.org/" target="_blank">WebPack</a>, or <a href="https://rollupjs.org/guide/en" target="_blank">Rollup</a>.
84
85### via Script Tag
86
87Add the following code to an HTML file:
88
89```html
90<html>
91 <head>
92 <!-- Load TensorFlow.js -->
93 <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
94
95
96 <!-- Place your code in the script tag below. You can also use an external .js file -->
97 <script>
98 // Notice there is no 'import' statement. 'tf' is available on the index-page
99 // because of the script tag above.
100
101 // Define a model for linear regression.
102 const model = tf.sequential();
103 model.add(tf.layers.dense({units: 1, inputShape: [1]}));
104
105 // Prepare the model for training: Specify the loss and the optimizer.
106 model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
107
108 // Generate some synthetic data for training.
109 const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
110 const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
111
112 // Train the model using the data.
113 model.fit(xs, ys).then(() => {
114 // Use the model to do inference on a data point the model hasn't seen before:
115 // Open the browser devtools to see the output
116 model.predict(tf.tensor2d([5], [1, 1])).print();
117 });
118 </script>
119 </head>
120
121 <body>
122 </body>
123</html>
124```
125
126Open up that HTML file in your browser, and the code should run!
127
128### via NPM
129
130Add TensorFlow.js to your project using <a href="https://yarnpkg.com/en/" target="_blank">yarn</a> <em>or</em> <a href="https://docs.npmjs.com/cli/npm" target="_blank">npm</a>. <b>Note:</b> Because
131we use ES2017 syntax (such as `import`), this workflow assumes you are using a modern browser or a bundler/transpiler
132to convert your code to something older browsers understand. See our
133<a href='https://github.com/tensorflow/tfjs-examples' target="_blank">examples</a>
134to see how we use <a href="https://parceljs.org/" target="_blank">Parcel</a> to build
135our code. However, you are free to use any build tool that you prefer.
136
137
138
139```js
140import * as tf from '@tensorflow/tfjs';
141
142// Define a model for linear regression.
143const model = tf.sequential();
144model.add(tf.layers.dense({units: 1, inputShape: [1]}));
145
146// Prepare the model for training: Specify the loss and the optimizer.
147model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
148
149// Generate some synthetic data for training.
150const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
151const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
152
153// Train the model using the data.
154model.fit(xs, ys).then(() => {
155 // Use the model to do inference on a data point the model hasn't seen before:
156 model.predict(tf.tensor2d([5], [1, 1])).print();
157});
158```
159
160See our <a href="https://js.tensorflow.org/tutorials/" target="_blank">tutorials</a>, <a href="https://github.com/tensorflow/tfjs-examples" target="_blank">examples</a>
161and <a href="https://js.tensorflow.org/api/latest/">documentation</a> for more details.
162
163## Importing pre-trained models
164
165We support porting pre-trained models from:
166- [TensorFlow SavedModel](https://www.tensorflow.org/js/tutorials/conversion/import_saved_model)
167- [Keras](https://js.tensorflow.org/tutorials/import-keras.html)
168
169## Various ops supported in different backends
170
171Please refer below :
172- [TFJS Ops Matrix](https://docs.google.com/spreadsheets/d/1D25XtWaBrmUEErbGQB0QmNhH-xtwHo9LDl59w0TbxrI/edit#gid=0)
173
174## Find out more
175
176[TensorFlow.js](https://js.tensorflow.org) is a part of the
177[TensorFlow](https://www.tensorflow.org) ecosystem. For more info:
178- For help from the community, use the `tfjs` tag on the [TensorFlow Forum](https://discuss.tensorflow.org/tag/tfjs).
179- [TensorFlow.js Website](https://js.tensorflow.org)
180- [Tutorials](https://js.tensorflow.org/tutorials)
181- [API reference](https://js.tensorflow.org/api/latest/)
182- [TensorFlow.js Blog](https://blog.tensorflow.org/search?label=TensorFlow.js)
183
184Thanks, <a href="https://www.browserstack.com/">BrowserStack</a>, for providing testing support.