Published
Edited
Mar 14, 2020
Insert cell
Insert cell
Insert cell
Insert cell
{
// first training
const net1 = new brain.NeuralNetwork();
net1.train(trainingData);
const net1Result1 = net1.run({
a: 0,
b: 1,
});
const net1Result2 = net1.run({
a: 1,
b: 0,
});
const stringifiedNet = JSON.stringify(net1.toJSON());

// second training
const net2 = new brain.NeuralNetwork();
net2.fromJSON(JSON.parse(stringifiedNet));
const net2Result1 = net2.run({
a: 0,
b: 1,
});
const net2Result2 = net2.run({
a: 1,
b: 0,
});
if (net1Result1.good !== net2Result1.good) throw new Error('net2Result1.good changed!');
if (net1Result1.bad !== net2Result1.bad) throw new Error('net2Result1.bad changed!');
if (net2Result2.good !== net2Result2.good) throw new Error('net2Result2.good changed!');
if (net2Result2.bad !== net2Result2.bad) throw new Error('net2Result2.bad changed!');
// third training
net2.train(trainingData2);
const stringifiedNet2 = JSON.stringify(net2.toJSON());

const net2RetrainResult1 = net2.run({
a: 0,
b: 1,
});
const net2RetrainResult2 = net2.run({
a: 1,
b: 0,
});

// prediction
const net3 = new brain.NeuralNetwork();
net3.fromJSON(JSON.parse(stringifiedNet2));

const net3Result1 = net3.run({
a: 0,
b: 1,
});

const net3Result2 = net3.run({
a: 1,
b: 0,
});

if (net2RetrainResult1.good !== net3Result1.good) throw new Error('net3Result1.good changed!');
if (net2RetrainResult1.bad !== net3Result1.bad) throw new Error('net3Result1.bad changed!');
if (net2RetrainResult2.good !== net3Result2.good) throw new Error('net3Result2.good changed!');
if (net2RetrainResult2.bad !== net3Result2.bad) throw new Error('net3Result2.bad changed!');
const net4 = new brain.NeuralNetwork();
net4.train(trainingData2);
const net4Result1 = net4.run({
a: 0,
b: 1,
});
const net4Result2 = net4.run({
a: 1,
b: 0,
});
return [
net1Result1,
net2Result2,
net2RetrainResult1,
net2RetrainResult2,
net3Result1,
net3Result2,
net4Result1,
net4Result2,
];
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more