deeplearning-js is an open source JavaScript library for deep learning. deeplearning-js provides all JavaScript developers a new way to play around with deep learning models without learning unfamiliar Python, statistics or calculus knowledge.
npm install deeplearning-js
yarn add deeplearning-js
Normalize 1D Array data set.
Support normalization method:
- minmax: (num - min) / (max - min)
- zscore: (num - mean) / std
import { Normalization } from 'deeplearning-js';
expect(Normalization.zscore([1, 2, 3])).toEqual([-1.224744871391589, 0, 1.224744871391589]);
expect(Normalization.minmax([1, 2, 3])).toEqual([0, 0.5, 1]);
Return initial parameters according to model structure.
Support activation functions:
- linear
- relu
- sigmoid
- softmax
const initialParameters = initializeParameters(
[{
size: trainingSet.input.shape[0], // input layer nerouns
}, {
size: 56, // hidden layer nerouns
activationFunc: 'relu', // hidden layer activation function
}, {
... // more hidden layers
}, {
size: trainingSet.output.shape[0], // output layer nerouns
activationFunc: 'softmax', // output layer activation function
}],
0, // mean (default: 0)
1, // variance (default: 1)
0.01, // scale (default: 0.01)
);
{
W1: number[][],
b1: number[][],
...
Wl: number[][],
bl: number[][],
}
Return parameters and cost after training for 1 epoch.
Support cost functions:
- quadratic
- cross-entropy
train(
input: number[][],
output: number[][],
parameters: any,
costFunc: 'quadratic' | 'cross-entropy',
learningRate: number,
)
{
parameters: {
W1: number[][],
b1: number[][],
...
Wl: number[][],
bl: number[][],
},
cost: number,
}
Return parameters and costs after multiple batches of epochs training.
batchTrain(
currentBatch: number,
totalBatch: number,
batchSize: number,
input: number[][],
output: number[][],
parameters: any,
learningRate: number,
costFunc: 'quadratic' | 'cross-entropy',
onBatchTrainEnd: (ro: { // invoke when each batch training ends
costs: number[],
parameters: any
}, currentBatch: number) => any,
onTrainEnd: (ro: { // invoke when all batches training ends
costs: number[],
parameters: any,
}) => any,
costs?: number[] = [],
disableRaf?: boolean = false,
)
batchTrain is a recursive function so please handle intermediate training results in onBatchTrainEnd callback and final training results in onTrainEnd callback.
Return predict values based on input data and model parameters.
const forwardResults = forwardPropagation(input, parameters);
const predict = forwardResults.yHat;
{
yHat: number[][], // predict values
caches: Cache[], // for backPropagation
activationFuncs: string[], // for backPropagation
}