CodingTrain / Toy-Neural-Network-JS

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

Multilayer Neural Network #42

Closed MkLahane closed 6 years ago

MkLahane commented 6 years ago

Created a multilayer neural network class which uses the matrix library in this repository for matrix operations. Some functionality is changed like you need to pass only 2 parameters(number of input nodes, number of output nodes) to the constructor of the neural network and for adding a hidden layer you need to call the function addHiddenLayer(number of neurons) for adding a hiddenLayer to your neural network and before training and after calling all addHiddenLayer function calls you need to call the config() function.

xxMrPHDxx commented 6 years ago

You may be interested on this

MkLahane commented 6 years ago

Wow, it is really great. Your code is quite optimised and is much more short and precise than mine.

xxMrPHDxx commented 6 years ago

Thank you. But I'm not really sure that it will produce the expected result. Yours seems much better and promising though.

AR-234 commented 6 years ago

Haven't looked in here before i've wrote my Version, i've merged it into the nn.js file, so it's not breaking the examples, but i like your approach. And i've broke the serialize and have no clue how to fix it.. Maybe somebody of you know a way to fix it? link

@xxMrPHDxx yours looks really awesome!

xxMrPHDxx commented 6 years ago

You can try to use this serialize function instead

serialize() {
   let cache = [];
   return JSON.stringify(this,(key, value) => {
      if (typeof value === 'object' && value !== null) {
         if (cache.indexOf(value) !== -1) {
            // Circular reference found, discard key
            return;
         }
         // Store value in our collection
         cache.push(value);
      }
      return value;
   });
   cache = null;
}

and for the deserialize function use

    static deserialize(data) {
        if(typeof data == 'string')
        {
            data = JSON.parse(data);
        }
        let nn = new NeuralNetwork(data.input_nodes, data.hidden_nodes, data.output_nodes);
        nn.layer.map(obj => {
            let nnlayer = new NeuralNetworkLayer(nn,obj.weights.cols,obj.weights.rows);
            nnlayer.weights = obj.weights;
            nnlayer.bias = obj.bias;
        });
        return nn;
    }
shiffman commented 6 years ago

Thank you so much for this contribution and discussion! At the moment I prefer #61 and keeping just a single NeuralNetwork class as well as adding a Layer class.