CodingTrain / Toy-Neural-Network-JS

Neural Network JavaScript library for Coding Train tutorials
MIT License
426 stars 242 forks source link

Training does not reliably converge, even with the simple XOR problem #155

Closed reneheijnemans closed 3 years ago

reneheijnemans commented 3 years ago

I am not sure that this is a known issue, but I cannot get the neural network to converge reliably.

In many instances, it just cannot solve the simple XOR problem.

For instance, I had serialized one of the randomly generated NeuralNetworks that displays this problem:

Is this just an issue with the activation function? Then it would be helpful to explain this in the README. Or is there a bug in the code?

import {NeuralNetwork} from "./nn/nn";

let nn;

const training_data = [
    {
        inputs: [0, 0],
        outputs: [0],
    },
    {
        inputs: [0, 1],
        outputs: [1],
    },
    {
        inputs: [1, 0],
        outputs: [1],
    },
    {
        inputs: [1, 1],
        outputs: [0],
    },
]

function setup() {
    createCanvas(400, 400);

    nn = NeuralNetwork.deserialize({"input_nodes":2,"hidden_nodes":2,"output_nodes":1,"weights_ih":{"rows":2,"cols":2,"data":[[-0.12692590266986858,-0.844955757436316],[-0.9357427469178123,0.8173651578783794]]},"weights_ho":{"rows":1,"cols":2,"data":[[-0.5832662974097391,0.5308947844782579]]},"bias_h":{"rows":2,"cols":1,"data":[[0.39650732687505963],[-0.49808473788143637]]},"bias_o":{"rows":1,"cols":1,"data":[[0.2908941132572971]]},"averageError":0,"learning_rate":0.01,"activation_function":{}});
    global.nn = nn;
}

function draw() {
    background(0);

    for (let i = 0; i < 1000; i++) {
        let data = random(training_data);
        nn.train(data.inputs, data.outputs);
    }

}

global.setup = setup;
global.draw = draw;
global.NeuralNetwork = NeuralNetwork;

The XOR example at https://codingtrain.github.io/Toy-Neural-Network-JS/examples/xor/ also faces this issue ; when refreshing a few times, it will result in the wrong separation visualization.

reneheijnemans commented 3 years ago

Never mind,

The coding challenge in Coding Challenge #92: XOR Problem https://www.youtube.com/watch?v=188B6k_F9jU explains this phenomenon as it is getting stuck due to not enough paths for it to solve the problem. Possibly this could be solved by picking the weights more carefully.

Daniel's solution mentioned in the video by adding additional hidden nodes solves this problem.