I am currently working on training a neural network to determine directional decisions based on its inputted life level. The neural network is designed to take in three inputs: [x, y, life]
. If life => 0.2
, it should output the angle from [x, y]
to (1, 1)
. If life < 0.2
, it should output the angle from [x, y]
to (0, 0)
.
Since the inputs and outputs of neurons should fall within the range of 0
to 1
, I normalize the angle by dividing it by 2 * Math.PI
.
Below is the code snippet:
var network = new synaptic.Architect.Perceptron(3,4,1);
for(var i = 0; i < 50000; i++){
var x = Math.random();
var y = Math.random();
var angle1 = angleToPoint(x, y, 0, 0) / (2 * Math.PI);
var angle2 = angleToPoint(x, y, 1, 1) / (2 * Math.PI);
for(var j = 0; j < 100; j++){
network.activate([x,y,j/100]);
if(j < 20){
network.propagate(0.3, [angle1]);
} else {
network.propagate(0.3, [angle2]);
}
}
}
Test it out here: jsfiddle
Now, when I input [0, 1, 0.19]
, I anticipate the neural network to output a value close to [0.75]
(1.5PI / 2PI
). However, the results are inconsistent with no clear correlation to the input provided.
What mistake might I be making in training my Neural network?
Although I have successfully trained a neural network to output
1
when provided with[a, b, c]
wherec => 0.2
and0
whenc < 0.2
, and to output an angle based on input[x, y]
, I am facing challenges in combining the two aspects.
Furthermore, I have developed a code that utilizes two Neural Networks to achieve the desired output. The first neural network converts the life level into 0 or 1, while the second neural network outputs an angle based on the output from the first neural network. Here is the code:
// This network outputs 1 when life => 0.2, otherwise 0
var network1 = new synaptic.Architect.Perceptron(3,3,1);
// This network outputs the angle to a certain point based on life
var network2 = new synaptic.Architect.Perceptron(3,3,1);
for (var i = 0; i < 50000; i++){
var x = Math.random();
var y = Math.random();
var angle1 = angleToPoint(x, y, 0, 0) / (2 * Math.PI);
var angle2 = angleToPoint(x, y, 1, 1) / (2 * Math.PI);
for(var j = 0; j < 100; j++){
network1.activate([x,y,j/100]);
if(j < 20){
network1.propagate(0.1, [0]);
} else {
network1.propagate(0.1, [1]);
}
network2.activate([x,y,0]);
network2.propagate(0.1, [angle1]);
network2.activate([x,y,1]);
network2.propagate(0.1, [angle2]);
}
}
Feel free to try it out here: jsfiddle
As demonstrated in this example, the code manages to achieve the desired output closely. Increasing the number of iterations can improve the accuracy even further.