Skip to content

Tensorflow.js

Don Jayamanne edited this page Aug 22, 2021 · 8 revisions

TensorFlow.js is a JavaScript Library for training and deploying machine learning models in the browser and in Node.js.

Features

  • Render plots/tables using the tfjs-vis API (from within node.js)

Notes:

  • Not all of the tfjs-vis is supported.
  • Please file issues for missing/broken features.

Samples

1. Try the sample notebook and view visualizations in the notebook

  • Please download this notebook to get started. Notice how the plots and model summary is displayed in the notebook (this is something that currently works only when using Tensorflow.js in the browser).

https://github.com/DonJayamanne/typescript-notebook/blob/main/resources/docs/tensorflow/sample.nnb

Scatter Plot Model Summary Scatter Plot2

2. Train a model in Tensorflow.js and view the Tensorboard in VS Code

  1. Train a model and generate data using the sample provided here
  2. Create two cell as follows
import * as tf from '@tensorflow/tfjs-node'
import * as path from 'path';
// Constructor a toy multilayer-perceptron regressor for demo purpose.
const model = tf.sequential();
model.add(
    tf.layers.dense({ units: 100, activation: 'relu', inputShape: [200] }));
model.add(tf.layers.dense({ units: 1 }));
model.compile({
    loss: 'meanSquaredError',
    optimizer: 'sgd',
    metrics: ['MAE']
});

// Generate some random fake data for demo purpose.
const xs = tf.randomUniform([10000, 200]);
const ys = tf.randomUniform([10000, 1]);
const valXs = tf.randomUniform([1000, 200]);
const valYs = tf.randomUniform([1000, 1]);

// Start model training process.
await model.fit(xs, ys, {
    epochs: 10,
    validationData: [valXs, valYs],
    // Add the tensorBoard callback here.
    callbacks: tf.node.tensorBoard(path.join(__dirname, 'tmp/fit_logs_1'))
});
  1. Train the model (by running the two code cells).
  2. Use the command Python: Launch TensorBoard to launch the tensorboard

Tensorboard

Home

Clone this wiki locally