{"action":"created","issue":{"url":"https://api.github.com/repos/cazala/synaptic/issues/193","repository_url":"https://api.github.com/repos/cazala/synaptic","labels_url":"https://api.github.com/repos/cazala/synaptic/issues/193/labels{/name}","comments_url":"https://api.github.com/repos/cazala/synaptic/issues/193/comments","events_url":"https://api.github.com/repos/cazala/synaptic/issues/193/events","html_url":"https://github.com/cazala/synaptic/issues/193","id":207947592,"number":193,"title":"Best way to merge two networks","user":{"login":"Leekao","id":8443824,"avatar_url":"https://avatars.githubusercontent.com/u/8443824?v=3","gravatar_id":"","url":"https://api.github.com/users/Leekao","html_url":"https://github.com/Leekao","followers_url":"https://api.github.com/users/Leekao/followers","following_url":"https://api.github.com/users/Leekao/following{/other_user}","gists_url":"https://api.github.com/users/Leekao/gists{/gist_id}","starred_url":"https://api.github.com/users/Leekao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Leekao/subscriptions","organizations_url":"https://api.github.com/users/Leekao/orgs","repos_url":"https://api.github.com/users/Leekao/repos","events_url":"https://api.github.com/users/Leekao/events{/privacy}","received_events_url":"https://api.github.com/users/Leekao/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-02-15T22:24:00Z","updated_at":"2017-02-23T09:17:28Z","closed_at":null,"body":"This is a question and not an issue, I couldn't find anywhere else to ask this, sorry if this is the wrong place.\r\nI'm trying to write a function that will take two networks as input and returns a third network that is 1/2 the first network and 1/2 the second network and I wondered what would be the best way to do this? I tried converting the networks to json and messing with it but it didn't really work.\r\nany advice or help will be welcomed."},"comment":{"url":"https://api.github.com/repos/cazala/synaptic/issues/comments/281937964","html_url":"https://github.com/cazala/synaptic/issues/193#issuecomment-281937964","issue_url":"https://api.github.com/repos/cazala/synaptic/issues/193","id":281937964,"user":{"login":"wagenaartje","id":10946322,"avatar_url":"https://avatars.githubusercontent.com/u/10946322?v=3","gravatar_id":"","url":"https://api.github.com/users/wagenaartje","html_url":"https://github.com/wagenaartje","followers_url":"https://api.github.com/users/wagenaartje/followers","following_url":"https://api.github.com/users/wagenaartje/following{/other_user}","gists_url":"https://api.github.com/users/wagenaartje/gists{/gist_id}","starred_url":"https://api.github.com/users/wagenaartje/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wagenaartje/subscriptions","organizations_url":"https://api.github.com/users/wagenaartje/orgs","repos_url":"https://api.github.com/users/wagenaartje/repos","events_url":"https://api.github.com/users/wagenaartje/events{/privacy}","received_events_url":"https://api.github.com/users/wagenaartje/received_events","type":"User","site_admin":false},"created_at":"2017-02-23T09:17:28Z","updated_at":"2017-02-23T09:17:28Z","body":"Well i'm doing the exact same thing at the moment with genetic algorithms, so we are talking about performing the crossover operation on two networks? \r\n\r\nIt's easier to merge two networks that are the **same size**. This will allow you to iterate over both networks at the same time.\r\n\r\n```\r\nconst INPUT = 1;\r\nconst HIDDEN = 4;\r\nconst OUTPUT = 1;\r\n\r\n// Two networks of the same size\r\nvar network1 = new synaptic.Architect.Perceptron(INPUT, HIDDEN, OUTPUT);\r\nvar network2 = new synaptic.Architect.Perceptron(INPUT, HIDDEN, OUTPUT);\r\n\r\ncrossOver(network1, network2);\r\n\r\nfunction crossOver(network1, network2){\r\n  // Converting the networks to JSON makes it all a whole lot easier\r\n  network1 = network1.toJSON();\r\n  network2 = network2.toJSON();\r\n  \r\n  // Create a network, this will be the result of the two networks\r\n  var offspring = new synaptic.Architect.Perceptron(INPUT, HIDDEN, OUTPUT);\r\n  offspring = offspring.toJSON();\r\n  \r\n  var neurons = HIDDEN + OUTPUT; // you only need to crossover the bias of the hidden and output neurson\r\n  var connections = INPUT * HIDDEN + HIDDEN * OUTPUT; // amount of connections of which you can modify the weight\r\n  \r\n  // Let's combine the neuron biases for the offspring\r\n  for(var i = 0; i < neurons; i++){\r\n    var bias1 = network1.neurons[INPUT+i].bias; // get's the bias of neuron i of network1\r\n    var bias2 = network2.neurons[INPUT+i].bias; // get's the bias of neuron i of network2\r\n    \r\n    var new_bias = (bias1 + bias2) / 2; // this is the function that calculates the new bias, do whatever you want here\r\n    \r\n    offspring.neurons[INPUT+i].bias = new_bias; \r\n  }\r\n  \r\n  // Let's combine the neuron conection weights for the offspring\r\n  for(var i = 0; i < connections; i++){\r\n    var weight1 = network1.connections[i].weight; // get's the weight of connection i of network1\r\n    var weight2 = network2.connections[i].weight; // get's the weight of connection i of network2\r\n    \r\n    var new_weight = (weight1 + weight2) / 2; // this is the function that calculates the new bias, do whatever you want here\r\n    \r\n    offspring.connections[i].weight = new_weight; \r\n  }\r\n  \r\n  // Now convert the offspring JSON back to a network and return it\r\n  return synaptic.Network.fromJSON(offspring);\r\n}\r\n```\r\n\r\nThe example I wrote above looks at every neuron bias and connection weight and takes the average of the two networks and returns one 'average' network. There are way more [crossover techniques](https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)) out there.\r\n\r\nFor example, you could also randomly select neuron biases and connection weights of each parent to the offspring. "}}
{"actor":{"display_login":"wagenaartje"}}