Home Manual Reference Source Test

Overview

All of the model building classes are structured similarly to ml.js models. Each model has three shared components regardless if it's a regression, classification or time series model.

This library was built to quickly implement Tensorflow and Keras models in JavaScript.

Configuration

All of the model configuration is managed during creating an instance of the class. All models have fit and compile properties that are assigned to the instance settings property.

You can also pass the Tensorflow reference to be used, as a configuration property if you want to use the compiled C++ or GPU via CUDA versions on the tf property;

Model constructor example

import { MultipleLinearRegression, } from 'tensorscript';
import tf from '@tensorflow/tfjs-node-gpu';

const MLR = new MultipleLinearRegression({
  fit:{
    epochs:500,
    batchSize:5,
  },
  compile:{
    loss: 'meanSquaredError',
    optimizer: 'adam',
  },
},{
  tf,
});

Each constructor takes two parameters settings and properties. As in the example above, settings are used to configure tensorflow objects and properties are used to configure tensorscript (like which version of tensorflow to use).

Training

All tensorflow models train asynchronously, therefore all tensorscript model train functions are all asynchronous. Always pass dependent and independent variables as matrix inputs.

Time series models can be trained with only one input matrix.

Training example


import { MultipleLinearRegression, } from 'tensorscript';

async function main(){
  const MLR = new MultipleLinearRegression();
  const x = [ [1], [2] ];
  const y = [ [3], [4] ];
  await MLR.train(x,y);
}

Predicting

All prediction inputs must be the same shape as training inputs. TensorScript provides an asynchronous predict method that converts tensor values to javascript objects. If you want the tensor from tensorflow, use the calculate method instead.

Predicting example

import { MultipleLinearRegression, } from 'tensorscript';

async function main(){
  const MLR = new MultipleLinearRegression();
  const x = [ [1], [2] ];
  const y = [ [3], [4] ];
  await MLR.train(x,y);

  await MLR.predict([[3]]) //=> [[5]];
  const tensorPrediction = await MLR.calculate([[3]]) //=> [[5]];
  tensorPrediction.data()
    .then(tensors=> /*Typed Array*/)
}

Examples

For more examples on how to do feature scaling and more, check out the Examples folder.