I wanted to make a neural network in node.js with brain.js. It should raise to the power some number. Yes, I know, that I can do it without using the neural network. But I am learning.
I just haven't an idea what to do
var brain = require('brainjs');
var net = new brain.NeuralNetwork();
function norm (inp){
var istr = inp.toString(2);
var out = [];
for (let i = 0;i <= istr.length;i++) {
out[i] = +istr.charAt(i);
}
return out;
}
net.train([
{input: norm(3), output: norm(9)},
{input: norm(9), output: norm(81)},
{input: norm(6), output: norm(36)},
{input: norm(8), output: norm(64)}
]);
var input = norm(6);
console.log(input);
var output = net.run(input);
console.log(parseInt(output,2));
I waited for output about [1,0,0,1,0,0](second output). But I got this:
[1,1,0,0]
NaN
What is the problem?
The use case is a little tricky, because norm
in this case is returning different size arrays ( NeuralNetwork
, and feedforward neural networks in general, doesn't support that), and console.log(parseInt(output,2))
is casting an array ( output
) to an integer. Here is a working example, the key is normalizing inputs (here is a live version: https://jsfiddle.net/robertleeplummerjr/8kh0wurg/2/ ):
const brain = require('./src');
const net = new brain.NeuralNetwork();
const lookupValueTable = {
3: normKey(3).join(','),
6: normKey(6).join(','),
8: normKey(8).join(','),
9: normKey(9).join(','),
36: normKey(36).join(','),
64: normKey(64).join(','),
81: normKey(81).join(',')
};
const lookupKeyTable = {
[normKey(3).join(',')]: 3,
[normKey(6).join(',')]: 6,
[normKey(8).join(',')]: 8,
[normKey(9).join(',')]: 9,
[normKey(36).join(',')]: 36,
[normKey(64).join(',')]: 64,
[normKey(81).join(',')]: 81
};
const trainingData = [
{ input: norm(3), output: norm(9) },
{ input: norm(9), output: norm(81) },
{ input: norm(6), output: norm(36) },
{ input: norm(8), output: norm(64) }
];
net.train(trainingData, { errorThresh: 0.0015 });
printResults(3, 9);
printResults(9, 81);
printResults(6, 36);
printResults(8, 64);
function normKey (inp) {
const limitNumber = 100;
const limit = limitNumber.toString(2).length;
const istr = inp.toString(2);
if (istr.length > limit) throw new Error('Normalizing too large of a value for this neural network');
const out = [];
for (let i = 0; i < limit; i++) {
if (i < istr.length) {
out[i] = istr[i] === '0' ? '.5' : istr[i];
} else {
out[i] = '-';
}
}
return out;
}
function norm (inp) {
const limitNumber = 100;
const limit = limitNumber.toString(2).length;
const istr = inp.toString(2);
if (istr.length > limit) throw new Error('Normalizing too large of a value for this neural network');
const out = [];
for (let i = 0; i < limit; i++) {
if (i < istr.length) {
out[i] = istr[i] === '0' ? .5 : +istr[i];
} else {
out[i] = 0;
}
}
return out;
}
function keyFromArray(output) {
return Array.from(output).map(v => {
if (v > .8) return '1';
if (v < .6 && v > .4) return '.5';
return '-';
}).join(',');
}
function printResults(inputRaw, outputRaw) {
const input = norm(inputRaw);
const output = net.run(input);
const key = keyFromArray(output);
console.log(`input for ${inputRaw}:`, input);
console.log(`output for ${inputRaw}:`, output);
console.log(`lookup key for ${inputRaw}:`, key);
console.log(`lookup value, should be ${outputRaw}:`, lookupKeyTable[key]);
}
A variation on this would be to simply use the value of the original norm
function like this:
const trainingData = [
{ input: { [norm(3)]: 1 }, output: { [norm(9)]: 1 } },
{ input: { [norm(9)]: 1 }, output: { [norm(81)]: 1 } },
{ input: { [norm(6)]: 1 }, output: { [norm(36)]: 1 } },
{ input: { [norm(8)]: 1 }, output: { [norm(64)]: 1 } }
];
You could even use a LSTM recurrent neural network. Here is an example of a bunch of math learned by one: https://github.com/BrainJS/brain.js/blob/master/examples/javascript/learn-math.js
You are not doing anything wrong. The neural network is not able to approximate square operation correctly. As shown in the readme of brainjs , it's not able to XOR also correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.