We provide an API that closely mirrors the TensorFlow eager API. deeplearn.js was originally developed by the Google Brain PAIR team to build powerful interactive machine learning tools for the browser, but it can be used for everything from education, to model understanding, to art projects.
You can install this library via yarn/npm:
yarn add deeplearn
npm install deeplearn
Alternatively you can use a script tag. Here we load it from a CDN.
In this case it will be available as a global variable named
You can replace also specify which version to load replacing
@latest with a specific
version string (e.g.
<!-- or -->
Let's add a scalar value to a 1D Tensor. Deeplearn js supports broadcasting the value of scalar over all the elements in the tensor.
; // If not loading the script as a globalconst a = dl;const b = dl;const result = a; // a is not modified, result is a new tensorresultdata; // Float32Array([3, 4, 5]// Alternatively you can use a blocking call to get the data.// However this might slow your program down if called repeatedly.console; // Float32Array([3, 4, 5]
var a = dl;var b = dl;var result = a; // a is not modified, result is a new tensor// Option 1: With a Promise.resultdata; // Float32Array([3, 4, 5])// Option 2: Synchronous download of data. Blocks the UI.console;
To build deeplearn.js from source, we need to clone the project and prepare the dev environment:
$ git clone https://github.com/PAIR-code/deeplearnjs.git$ cd deeplearnjs$ yarn prep # Installs dependencies.
Yarn vs NPM
Generally speaking it's up to you. Yarn is fully interoperable with npm. You can either do
npm install. However we use yarn, and if you are adding or removing dependencies you should use yarn to keep the
yarn.lock file up to date.
To interactively develop any of the demos (e.g.
$ ./scripts/watch-demo demos/nn-art>> Starting up http-server, serving ./>> Available on:>> http://127.0.0.1:8080>> Hit CTRL-C to stop the server>> 1357589 bytes written to dist/demos/nn-art/bundle.js at 10:34:45 AM
watch-demo script monitors for changes of typescript code and does
incremental compilation (~200-400ms), so users can have a fast edit-refresh
cycle when developing apps.
Before submitting a pull request, make sure the code passes all the tests and is clean of lint errors:
$ yarn test$ yarn lint
To run a subset of tests and/or on a specific browser:
$ yarn test --browsers=Chrome --grep='multinomial'> ...> Chrome 62.0.3202 : Executed 28 of 1891 SUCCESS
To run the tests once and exit the karma process (helpful on Windows):
$ yarn test --single-run
Packaging (browser and npm)
To build a standalone ES5 library that can be imported in the browser with a
$ ./scripts/build-standalone.sh # Builds standalone library.>> Stored standalone library at dist/deeplearn.js
To build an npm package:
$ ./scripts/build-npm.sh...Stored standalone library at dist/deeplearn.jsdeeplearn-VERSION.tgz
To install it locally, run
npm install ./deeplearn-VERSION.tgz.
On Windows, use bash (available through git) to use the scripts above.
Looking to contribute, and don't know where to start? Check out our "help wanted" issues.
deeplearn.js targets environments with WebGL 1.0 or WebGL 2.0. For devices
OES_texture_float extension, we fall back to fixed precision
floats backed by a
gl.UNSIGNED_BYTE texture. For platforms without WebGL,
we provide CPU fallbacks.