An optimized implementation of the backpropagation algorithm in Typescript
import { TrainingData, Net } from "./Net";
//example dataset
const XOR: TrainingData = [
[[0, 0], [0]],
[[0, 1], [1]],
[[1, 0], [1]],
[[1, 1], [0]],
];
//create a net with random weights/biases
const net = Net.create(2, 1, 3, 1);
//train the net
const trainConfig = {
gamma: 0.1, //learning rate of weights
gamma_b: 0.1, //learning rate of biases
momentum: 0.1, //momentum of weights/biases
batchSize: 1, //batch size
};
console.log(net.errorDataset(XOR)); //print the average error of the net
net.train(XOR, 100000, trainConfig); //train the net for 100000 epochs
console.log(net.errorDataset(XOR)); //print the new average error of the net
- Net.prototype.eval
- Net.prototype.gradient
- Net.prototype.apply
- Net.prototype.train
- Net.prototype.error
- Net.prototype.errorDataset
- Net.prototype.debug
source | TOC
Constructor for the Net class.
options
{NetOptions} - The weights and biases of the net.
Net.create(inputs, outputs, hidden, hiddenLayers)
source | TOC
Creates a new net with the given dimensions.
inputs
{number
}: The size of the input layer.outputs
{number
}: The size of the output layer.hidden
{number
}: The size of the hidden layer.hiddenLayers
{number
}: The number of hidden layers.
{Net
}: An instance of the Net
class, with random weights and biases.
source | TOC
Average several gradients into one.
gradients
{Gradient[]
}: The gradients to average.
{Gradient}: The averaged gradient.
source | TOC
Run a forward pass of the net using the given input.
input
{number[]
}: The input to the net.
{number[][]
}: The state of the net after the input has been passed.
source | TOC
Calculate the required gradient for a given test case
input
{number[]
}: The input to the net.expected
{number[]
}: The expected output of the net.
{Gradient}: The gradient of the net.
source | TOC
Apply a gradient to the net.
gradient
{Gradient[]
}: The gradient to apply.gamma
{number
}: [Optional] The learning rate of the weights.gamma_b
{number
}: [Optional] The learning rate of the biases.
source | TOC
Train the net using the given dataset.
dataset
{TrainingData[]
}: The dataset to train the net on.epochs
{number
}: The number of epochs to train the net for.options
{TrainingOptions}: The options for training.
source | TOC
Calculate the error of the net for a given test case using MSE
input
{number[]
}: The input to the net.expected
{number[]
}: The expected output of the net.
{number
}: The error of the net.
source | TOC
Calculate the average error of the net for a given dataset.
dataset
{TrainingData[]
}: The dataset to use.
{number
}: The average error of the net.
source | TOC
Prints the state of the net to the console.
Weights:
<Layer0 Weight0>, <Layer0 Weight1>, ..., <Layer0 WeightN>
<Layer1 Weight0>, <Layer1 Weight1>, ..., <Layer1 WeightN>
...
<LayerM Weight0>, <LayerM Weight1>, ..., <LayerM WeightN>
Biases:
<Layer0 Bias0>, <Layer0 Bias1>, ..., <Layer0 BiasN>
<Layer1 Bias0>, <Layer1 Bias1>, ..., <Layer1 BiasN>
...
<LayerM Bias0>, <LayerM Bias1>, ..., <LayerM BiasN>
{
weights: number[][];
biases: number[][];
dimensions: number[];
};
An object describing the weights and biases of a net.
[
number[][], //bias gradient
number[][] //weight gradient
]
An object describing the gradient of a net.
[
[number[], number[]], //input, expected output
[number[], number[]],
[number[], number[]],
...
]
An array of training data.
{
gamma?: number; //learning rate of the weights
gamma_b?: number; //learning rate of the biases
momentum?: number; //momentum of the weights/biases
batchSize?: number; //the size of the batch
}
Weights are internally stored as a 1D array, grouped by origin node. This is so the weights can be read sequentially, increasing the performance of the network.
For this net, the weights would be stored as such:
[
[0.4, 0.8, 0.52, 0.3], //Layer 0
[0.1, 0.63, 0.48, 0.2], //Layer 1
];