# How to train neural network a math multiplication table?

I am trying to train neural network (brain.js) a multiplication table. It is not going too well: requires lots of hidden layers, iterations and very small error threshold, and the results are still incorrect. What am I doing wrong?

console.clear();
let net = new brain.NeuralNetwork({hiddenLayers: });
let training_data = [
{input:[2,2], output:[4/81]},
{input:[2,3], output:[6/81]},
{input:[2,4], output:[8/81]},
{input:[2,5], output:[10/81]},
];
net.train(training_data, {
errorThresh: 0.0000001,  // error threshold to reach before completion
iterations: 500000,   // maximum training iterations
log: true,           // console.log() progress periodically
logPeriod: 10000,       // number of iterations between logging
learningRate: 0.01    // learning rate
});
for(let i = 2; i < 3; i++){
for(let j = 2; j < 10; j++){
console.log(i + " * " + j + " = " + net.run([i,j])*81);
}
}


The output is:

2 * 2 = 4.265779517591

2 * 3 = 5.825156696140766

2 * 4 = 7.789406754076481

2 * 5 = 10.152319103479385

2 * 6 = 12.860703930258751

2 * 7 = 15.819618478417397

2 * 8 = 18.90912254154682

2 * 9 = 22.006048411130905

After receiving the answer from SRJ I have changed the neural net so, that it now has 4 hidden layers and receives 10 000 inputs:

console.clear();
let net = new brain.NeuralNetwork({hiddenLayers: });
let training_data = [
];

for(let i = 0; i < 100; i++){
for(let j = 0; j < 100; j++){
training_data.push({input:[i,j], output: [i*j/10000]});
}
}
net.train(training_data, {
errorThresh: 0.001,  // error threshold to reach before completion
iterations: 20000,   // maximum training iterations
log: true,           // console.log() progress periodically
logPeriod: 100,       // number of iterations between logging
learningRate: 0.0001    // learning rate
});
let random_numbers = [Math.floor(Math.random()*50), Math.floor(Math.random()*50)]
console.log(random_numbers + " * " + random_numbers + " = " + net.run([random_numbers, random_numbers]) * 10000);


Still does not work. The output: "29 * 42 = 728.9011776447296". (while in reality should be 1218).

• Hi and welcome! Note that programming issues are generally off-topic here. I am not sure if this is a programming issue or not, so I will not close this post as off-topic. I suggest that you read our on-topic page to understand which questions you can ask here. – nbro Jun 18 at 9:41
• Thank you! I know, that my question is more suitable for online forum, but as I know, there is no such of for neural networks and especially brain.js:) – Jaroslav Tavgen Jun 18 at 9:51
• There's Data Science SE and Stack Overflow. Those sites are more suitable to ask questions that involve code or programming issues. – nbro Jun 18 at 9:52