
Getting {"error": null, "iterations": 20000} even though training dataset are just numbers.
When brain.js trains data in my sample website running in Chrome 65 on Mac
Input data: https://gist.github.com/digi0ps/65bebf027876f85b42ab7b4dd2ba49bf
Code:
let net = new brain.NeuralNetwork();
net.train(encoded);
trained = net.toFunction()
Error shouldn't have been NaN. When I run net.run with an input I get
{english: NaN, tanglish: NaN}
instead of values inside them.
Here is the NeuralNetwork object:
https://gist.github.com/digi0ps/53292e8c55c31b7de8de9dcb06ea22e5
Can you help?
Regards,
Sriram.
Reproduced here: https://jsfiddle.net/robertleeplummerjr/jvLw503L/4/
The issue is that your input array lengths are staggered. A simple fix of the following will bring results:
const net = new brain.NeuralNetwork();
net.train(fixLengths(data));
function fixLengths(data) {
let maxLengthInput = -1;
for (let i = 0; i < data.length; i++) {
if (data[i].input.length > maxLengthInput) {
maxLengthInput = data[i].input.length;
}
}
for (let i = 0; i < data.length; i++) {
while (data[i].input.length < maxLengthInput) {
data[i].input.push(0);
}
}
return data;
}
Working example: https://jsfiddle.net/robertleeplummerjr/jvLw503L/10/
Thanks for the quick fix!
@robertleeplummerjr
Sorry to bring this up again.
But running an input against the trained function, gives null back again.
Can you check?
Is the data being sent in after trained the same size as the previously sent in data?
@robertleeplummerjr We could do some validation/handling when the training data and results passed into the train methods are different sizes. The question is which would be the best way to go about it, run the training for what we have or throw and stop execution?
Validating size wouldn't be very costly, and would provide useful feedback, I like the suggestion!
Thanks robertleeplummerjr for the code above. I'm getting back into coding and brushing up on JS. I have some code to contribute. Its like yours but for when you need to size a single array, I thought someone might find it useful. It fills or slices according to the second parameter.
fixLengthSingle(data, 190)
function fixLengthSingle(data, max)
{
var element = [];
var lrg = Math.max(max, data.length);
for (let i = 0; i < lrg; i++)
{
if (element.push(data[i]) > lrg)
{
element.push(i);
}
}
for (let i = 0; i < element.length; i++) {
if (element[i] == undefined) {
element[i] = 0;
}
}
if(element.length > max )
{
element = element.slice(0, max);
}
return element;
}
Most helpful comment
Working example: https://jsfiddle.net/robertleeplummerjr/jvLw503L/10/