1 | # TensorFlow backend for TensorFlow.js via Node.js
|
2 | This repository provides native TensorFlow execution in backend JavaScript applications under the Node.js runtime,
|
3 | accelerated by the TensorFlow C binary under the hood. It provides the same API as [TensorFlow.js](https://js.tensorflow.org/api/latest/).
|
4 |
|
5 | This package will work on Linux, Windows, and Mac platforms where TensorFlow is supported.
|
6 |
|
7 | ## Installing
|
8 |
|
9 | TensorFlow.js for Node currently supports the following platforms:
|
10 | - Mac OS X CPU (10.12.6 Siera or higher)
|
11 | - Linux CPU (Ubuntu 14.04 or higher)
|
12 | - Linux GPU (Ubuntu 14.04 or higher and Cuda 10.0 w/ CUDNN v7) ([see installation instructions](https://www.tensorflow.org/install/gpu#software_requirements))
|
13 | - Windows CPU (Win 7 or higher)
|
14 | - Windows GPU (Win 7 or higher and Cuda 10.0 w/ CUDNN v7) ([see installation instructions](https://www.tensorflow.org/install/gpu#windows_setup))
|
15 |
|
16 | *Other Linux variants might also work but this project matches [core TensorFlow installation requirements](https://www.tensorflow.org/install/install_linux).*
|
17 |
|
18 | #### Installing CPU TensorFlow.js for Node:
|
19 |
|
20 | ```sh
|
21 | npm install @tensorflow/tfjs-node
|
22 | (or)
|
23 | yarn add @tensorflow/tfjs-node
|
24 | ```
|
25 |
|
26 | #### Installing Linux/Windows GPU TensorFlow.js for Node:
|
27 |
|
28 | ```sh
|
29 | npm install @tensorflow/tfjs-node-gpu
|
30 | (or)
|
31 | yarn add @tensorflow/tfjs-node-gpu
|
32 | ```
|
33 |
|
34 | #### Windows Requires Python 2.7
|
35 |
|
36 | Windows build support for `node-gyp` requires Python 2.7. Be sure to have this version before installing `@tensorflow/tfjs-node` or `@tensorflow/tfjs-node-gpu`. Machines with Python 3.x will not install the bindings properly.
|
37 |
|
38 | *For more troubleshooting on Windows, check out [WINDOWS_TROUBLESHOOTING.md](./WINDOWS_TROUBLESHOOTING.md).*
|
39 |
|
40 | #### Mac OS X Requires Xcode
|
41 |
|
42 | If you do not have Xcode setup on your machine, please run the following commands:
|
43 |
|
44 | ```sh
|
45 | $ xcode-select --install
|
46 | ```
|
47 |
|
48 | After that operation completes, re-run `yarn add` or `npm install` for the `@tensorflow/tfjs-node` package.
|
49 |
|
50 | You only need to include `@tensorflow/tfjs-node` or `@tensorflow/tfjs-node-gpu` in the package.json file, since those packages ship with `@tensorflow/tfjs` already.
|
51 |
|
52 | ## Using the binding
|
53 |
|
54 | Before executing any TensorFlow.js code, import the node package:
|
55 |
|
56 | ```js
|
57 | // Load the binding
|
58 | import * as tf from '@tensorflow/tfjs-node';
|
59 |
|
60 | // Or if running with GPU:
|
61 | import * as tf from '@tensorflow/tfjs-node-gpu';
|
62 | ```
|
63 |
|
64 | Note: you do not need to add the `@tensorflow/tfjs` package to your dependencies or import it directly.
|
65 |
|
66 | ## Development
|
67 |
|
68 | ```sh
|
69 | # Download and install JS dependencies, including libtensorflow 1.8.
|
70 | yarn
|
71 |
|
72 | # Run TFJS tests against Node.js backend:
|
73 | yarn test
|
74 | ```
|
75 |
|
76 | ```sh
|
77 | # Switch to GPU for local development:
|
78 | yarn enable-gpu
|
79 | ```
|
80 |
|
81 |
|
82 | ## MNIST demo for Node.js
|
83 |
|
84 | See the [tfjs-examples repository](https://github.com/tensorflow/tfjs-examples/tree/master/mnist-node) for training the MNIST dataset using the Node.js bindings.
|
85 |
|
86 | ### Optional: Build libtensorflow From TensorFlow source
|
87 |
|
88 | This requires installing bazel first.
|
89 |
|
90 | ```sh
|
91 | bazel build --config=monolithic //tensorflow/tools/lib_package:libtensorflow
|
92 | ```
|