A JavaScript library for training and deploying ML models in the browser and on Node.js.
In order to run tensorflow.js in node.js on Mac, you should install tensorflow.js as following:
$ npm install @tensorflow/tfjs-node > @tensorflow/tfjs-node@0.1.20 install /Users/kurapa/node_modules/@tensorflow/tfjs-node > node scripts/install.js * Downloading libtensorflow [==============================] 5852094/bps 100% 0.0s * Building TensorFlow Node.js bindings > protobufjs@6.8.8 postinstall /Users/kurapa/node_modules/protobufjs > node scripts/postinstall npm WARN saveError ENOENT: no such file or directory, open '/Users/kurapa/package.json' npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open '/Users/kurapa/package.json' npm WARN kurapa No description npm WARN kurapa No repository field. npm WARN kurapa No README data npm WARN kurapa No license field. + @tensorflow/tfjs-node@0.1.20 added 48 packages from 52 contributors and audited 61 packages in 20.808s found 0 vulnerabilities |
If your system NVIDIA GPU, it will be recommended to install below package as well.
$ npm install @tensorflow/tfjs-node-gpu |
The below is the example of tensorflow.js based on node.js
const tf = require('@tensorflow/tfjs'); // Load the binding: require('@tensorflow/tfjs-node'); // Use '@tensorflow/tfjs-node-gpu' if running with GPU. // Train a simple model: const model = tf.sequential(); model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]})); model.add(tf.layers.dense({units: 1, activation: 'linear'})); model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); const xs = tf.randomNormal([100, 10]); const ys = tf.randomNormal([100, 1]); model.fit(xs, ys, { epochs: 100, callbacks: { onEpochEnd: async (epoch, log) => { console.log(`Epoch ${epoch}: loss = ${log.loss}`); } } }); |
Reference URL: https://js.tensorflow.org/