UNPKG

4.38 kBMarkdownView Raw
1# TensorFlow backend for TensorFlow.js via Node.js
2This repository provides native TensorFlow execution in backend JavaScript applications under the Node.js runtime,
3accelerated by the TensorFlow C binary under the hood. It provides the same API as [TensorFlow.js](https://js.tensorflow.org/api/latest/).
4
5This package will work on Linux, Windows, and Mac platforms where TensorFlow is supported.
6
7## Installing
8
9TensorFlow.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
16For GPU support, tfjs-node-gpu@1.2.4 or later requires the following NVIDIA® software installed on your system:
17
18| Name | Version |
19| ------------- | ------------- |
20| [NVIDIA® GPU drivers](https://www.nvidia.com/Download/index.aspx?lang=en-us) | >410.x |
21| [CUDA® Toolkit](https://developer.nvidia.com/cuda-10.0-download-archive) | 10.0 |
22| [cuDNN SDK](https://developer.nvidia.com/rdp/cudnn-download) | >=7.4.1 |
23
24*Other Linux variants might also work but this project matches [core TensorFlow installation requirements](https://www.tensorflow.org/install/install_linux).*
25
26#### Installing CPU TensorFlow.js for Node:
27
28```sh
29npm install @tensorflow/tfjs-node
30(or)
31yarn add @tensorflow/tfjs-node
32```
33
34#### Installing Linux/Windows GPU TensorFlow.js for Node:
35
36```sh
37npm install @tensorflow/tfjs-node-gpu
38(or)
39yarn add @tensorflow/tfjs-node-gpu
40```
41
42#### Windows Requires Python 2.7
43
44Windows 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.
45
46*For more troubleshooting on Windows, check out [WINDOWS_TROUBLESHOOTING.md](./WINDOWS_TROUBLESHOOTING.md).*
47
48#### Mac OS X Requires Xcode
49
50If you do not have Xcode setup on your machine, please run the following commands:
51
52```sh
53$ xcode-select --install
54```
55
56After that operation completes, re-run `yarn add` or `npm install` for the `@tensorflow/tfjs-node` package.
57
58You 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.
59
60## Using the binding
61
62Before executing any TensorFlow.js code, import the node package:
63
64```js
65// Load the binding
66import * as tf from '@tensorflow/tfjs-node';
67
68// Or if running with GPU:
69import * as tf from '@tensorflow/tfjs-node-gpu';
70```
71
72Note: you do not need to add the `@tensorflow/tfjs` package to your dependencies or import it directly.
73
74## Development
75
76```sh
77# Download and install JS dependencies, including libtensorflow 1.8.
78yarn
79
80# Run TFJS tests against Node.js backend:
81yarn test
82```
83
84```sh
85# Switch to GPU for local development:
86yarn enable-gpu
87```
88
89
90## MNIST demo for Node.js
91
92See the [tfjs-examples repository](https://github.com/tensorflow/tfjs-examples/tree/master/mnist-node) for training the MNIST dataset using the Node.js bindings.
93
94### Optional: Build optimal TensorFlow from source
95
96To get the most optimal TensorFlow build that can take advantage of your specific hardware (AVX512, MKL-DNN), you can build the `libtensorflow` library from source:
97- [Install bazel](https://docs.bazel.build/versions/master/install.html)
98- Checkout the [main tensorflow repo](https://github.com/tensorflow/tensorflow) and follow the instructions in [here](https://www.tensorflow.org/install/source) with **one difference**: instead of building the pip package, build `libtensorflow`:
99
100```sh
101./configure
102bazel build --config=opt --config=monolithic //tensorflow/tools/lib_package:libtensorflow
103```
104
105The build might take a while and will produce a `bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz` file, which should be unpacked and replace the files in `deps` folder of `tfjs-node` repo:
106```sh
107cp bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz ~/myproject/node_modules/@tensorflow/tfjs-node/deps
108cd path-to-my-project/node_modules/@tensorflow/tfjs-node/deps
109tar -xf libtensorflow.tar.gz
110```