ToyNN Examples
XOR Truth Table
X | Y | Output |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
import toynn from 'toynn';
const X = [ new toynn.NArray([0, 0]).reshape(1, 2), new toynn.NArray([0, 1]).reshape(1, 2), new toynn.NArray([1, 0]).reshape(1, 2), new toynn.NArray([1, 1]).reshape(1, 2),];
const y = [ new toynn.NArray([0]), new toynn.NArray([1]), new toynn.NArray([1]), new toynn.NArray([0]),];
const model = new toynn.NN('xor');
const layer1 = new toynn.Layer(2, 1);layer1.use(toynn.functions.sigmoid);
model.add(layer1);
model.train({ x: X, y, epochs: 1000, alpha: 0.005, loss: toynn.errors.MSE, verbose: true, optimizer: new toynn.optimizers.RMSProp(),});
let newData = new toynn.NArray([1, 0]).reshape(1, 2);// make predictionconsole.log(model.forward(newData).flatten());
Table of 4
import toynn from 'toynn';
const X = [ new toynn.NArray([1, 2, 3, 4]).reshape(1, -1), new toynn.NArray([5, 6, 7, 8]).reshape(1, -1),];
const y = [ new toynn.NArray([4, 8, 12, 16]), new toynn.NArray([20, 24, 28, 32]),];
const model = new toynn.NN('tableOf4');
const layer1 = new toynn.Layer(4, 4);layer1.use(toynn.functions.linear);
model.add(layer1);
model.train({ x: X, y: y, epochs: 500, alpha: 0.005, loss: toynn.errors.MSE, verbose: true,});
const newData = new toynn.NArray([9, 10, 11, 12]).reshape(1, -1);const prediction = model .forward(newData) .map((e) => Math.floor(e)) .flatten();console.log(`Prediction: ${prediction}`);
IRIS Classifier
import toynn from 'toynn';
const classes = { 'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2,};
const datasetURL = 'https://ocw.mit.edu/courses/15-097-prediction-machine-learning-and-statistics-spring-2012/89d88c5528513adc4002a1618ce2efb0_iris.csv';
const irisDataset = await toynn.Dataset.from(datasetURL);
const X = irisDataset.slice(0, -1); // every column except for the last oneconst y = irisDataset.slice(-1); // last column
X.onGet = (element) => { return element.reshape(1, -1);};
y.onGet = (element) => { element = element.flatten()[0]; element = toynn.utils.onehotEncode(classes[element], 3); return new toynn.NArray(element).reshape(-1);};
const [trainX, testX, trainY, testY] = toynn.utils.trainTestSplit(X, y, { testSize: 0.2, shuffle: true,});
const model = new toynn.NN('irisClassifier');
const layer1 = new toynn.Layer(4, 5);layer1.use(toynn.functions.sigmoid);const layer2 = new toynn.Layer(5, 3);layer2.use(toynn.functions.softmax);
model.add(layer1);model.add(layer2);model.train({ x: trainX, y: trainY, epochs: 500, alpha: 0.005, verbose: true, loss: toynn.errors.MSE,});
// get accuracylet accuracy = 0, prediction;
for (let i = 0; i < testX.length; i++) { prediction = model.forward(testX.get(i)).max().index;
if (prediction === testY.get(i).max().index) { accuracy += 1; }}
console.log(`Accuracy: ${accuracy / testX.length}%`);