Created
August 12, 2019 15:45
-
-
Save luiscarbonell/df793cb2371ab3923463cbbbbf1330a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const uid = require("cuid"); | |
| const Group = require("./group"); | |
| function Network(sizes, biases, weights) { | |
| let self = this; | |
| this.id = uid(); | |
| this.groups = sizes == undefined ? [] : sizes.map(function(size, index) { | |
| if(biases == undefined) return new Group(size); | |
| else return new Group(size, biases[index]); | |
| }); | |
| this.groups.slice(0, this.groups.length - 1).forEach(function(group, index) { | |
| if(weights) group.connect(self.groups[index + 1], weights[index]); | |
| else group.connect(self.groups[index + 1]); | |
| }) | |
| this.activate = function(inputs) { | |
| const outputs = this.groups.map(function(group, index) { | |
| if(index === 0) return group.activate(inputs); | |
| else return group.activate(); | |
| }) | |
| return outputs[outputs.length - 1]; | |
| } | |
| this.propagate= function(targets) { | |
| const error = this.groups[this.groups.length - 1].neurons.map(function(neuron, index) { | |
| return 0.5 * Math.pow(targets[index] - neuron.output, 2); | |
| }).reduce((a,b) => a + b); | |
| this.groups.reverse().forEach(function(group, index) { | |
| if(index === 0) group.propagate(targets); | |
| else return group.propagate(); | |
| }); this.groups.reverse(); | |
| return error; | |
| } | |
| } | |
| module.exports = Network; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment