Published
Edited
Apr 8, 2022
Insert cell
# Brain.js Issue #778
Insert cell
{
// create a simple recurrent neural network
const net = new brain.recurrent.LSTMTimeStep(netJson);
net.fromJSON(netJson);
function findBounds(data, key) {
let highest = -Infinity;
let lowest = Infinity;
for (let i = 0; i < data.length; i++) {
const io = data[i][key];
for (let j = 0; j < io.length; j++) {
if (io[j] > highest) {
highest = io[j];
}
if (io[j] < lowest) {
lowest = io[j];
}
}
}
return { highest, lowest };
}
function normalize(data, { highest, lowest }, key) {
return data.map((datum) => {
return {
...datum,
[key]: datum[key].map((value) => (value - lowest) / (highest - lowest)),
};
});
}
function denormalize(data, { highest, lowest }, key) {
if (key) {
return data.map((datum) => {
return {
...datum,
[key]: datum[key].map((value) => (highest - lowest) * (value + lowest)),
};
});
}
return data.map((value) => (highest - lowest) * (value + lowest));
}
const trainingDataRaw = [
{
input: [7, 12, 83, 0, 11, 13, 7],
output: [294/*.4*/],
},
{
input: [7, 76, 1, 4, 18, 5, 3],
output: [343/*.9*/],
},
{
input: [9, 0, 103, 22, 10, 15, 5],
output: [339/*.3*/],
},
{
input: [9, 106, 2, 5, 13, 16, 17],
output: [470/*.9*/],
},
{
input: [8, 17, 92, 13, 3, 37, 10],
output: [403/*.4*/],
},
{
input: [7, 4, 4, 77, 3, 22, 6],
output: [347/*.1*/],
},
{
input: [7, 4, 1, 80, 14, 18, 9],
output: [339/*.2*/],
},
{
input: [6, 0, 12, 58, 11, 7, 12],
output: [258/*.9*/],
},
{
input: [5, 10, 48, 2, 0, 11, 13],
output: [231/*.0*/],
},
{
input: [6, 2, 54, 4, 15, 4, 1],
output: [201/*.2*/],
},
{
input: [5, 9, 2, 41, 15, 0, 7],
output: [204/*.5*/],
},
{
input: [7, 76, 0, 6, 6, 20, 1],
output: [399/*.2*/],
},
];
const inputBounds = findBounds(trainingDataRaw, 'input');
const outputBounds = findBounds(trainingDataRaw, 'input');
let trainingData = normalize(normalize(trainingDataRaw, inputBounds, 'input'), outputBounds, 'output');
// For when trained
// net.train(trainingData, {
// iterations: 5000, // the maximum times to iterate the training data --> number greater than 0
// errorThresh: 0.055, // the acceptable error percentage from training data --> number between 0 and 1
// log: true, // true to use console.log, when a function is supplied it is used --> Either true or a function
// logPeriod: 10, // iterations between logging out --> number greater than 0
// learningRate: 0.02, // scales with delta to effect training rate --> number between 0 and 1
// momentum: 0.1, // scales with next layer's change value --> number between 0 and 1
// callback: () => {}, // a periodic call back that can be triggered while training --> null or function
// callbackPeriod: 10, // the number of iterations through the training data between callback calls --> number greater than 0
// timeout: Infinity, // the max number of milliseconds to train for --> number greater than 0
// });
// console.log("Training finished");
// return JSON.stringify(net.toJSON());
console.log(normalize(trainingDataRaw, inputBounds, 'input'));
return normalize(trainingDataRaw, inputBounds, 'input')
.map((datum) => [
...denormalize(datum.input, inputBounds),
...denormalize([net.run(datum.input)], outputBounds),
]);
}
Insert cell
Insert cell
netJson = FileAttachment("net.json").json()
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more