public void AddHiddenNeuronToLayerAndMesh(int layer) { layer += 1; WorkingNeuron neuron = new WorkingNeuron(layer); neurons[layer].Add(neuron); int newIndex = neurons[layer].Count - 1; if (newIndex < neurons[layer - 1].Count) { Neuron oldNeuron = neurons[layer - 1][newIndex]; foreach (WorkingNeuron n in neurons[layer + 1]) { foreach (Connection conn in n.GetConnections()) { if (conn.entryNeuron == oldNeuron) { conn.entryNeuron = neuron; } } } } else { foreach (WorkingNeuron n in neurons[layer + 1]) { n.AddNeuronConnection(new Connection(neuron, Simulation.RandomFloat() * 2 - 1)); } } foreach (Neuron n in neurons[layer - 1]) { neuron.AddNeuronConnection(new Connection(n, Simulation.RandomFloat() * 2 - 1)); } }
public void AddOutputNeuronAndMesh(WorkingNeuron neuron) { OutputNeurons.Add(neuron); foreach (WorkingNeuron wn in LastHiddenLayer) { neuron.AddNeuronConnection(new Connection(wn, 0)); } }
public void AddOutputNeuronAndMesh(WorkingNeuron neuron) { outputNeurons.Add(neuron); foreach (WorkingNeuron wn in hiddenNeurons) { neuron.AddNeuronConnection(new Connection(wn, 0)); } }
public void Deserialize(BinaryReader reader) { fullMeshGenerated = reader.ReadBoolean(); int inputCount = reader.ReadInt32(); for (int currentIndex = 0; currentIndex < inputCount; currentIndex++) { InputNeuron newNeuron = new InputNeuron(); newNeuron.Deserialize(reader); inputNeurons.Add(newNeuron); } int hiddenCount = reader.ReadInt32(); for (int currentIndex = 0; currentIndex < hiddenCount; currentIndex++) { WorkingNeuron newNeuron = new WorkingNeuron(); newNeuron.Deserialize(reader); hiddenNeurons.Add(newNeuron); int connectionCount = reader.ReadInt32(); for (int connectionIndex = 0; connectionIndex < connectionCount; connectionIndex++) { // Todo get rid of this covariance cast Connection connection = Connection.Deserialize(reader, inputNeurons.Cast <Neuron>()); newNeuron.AddNeuronConnection(connection); } } int outputCount = reader.ReadInt32(); for (int currentIndex = 0; currentIndex < outputCount; currentIndex++) { WorkingNeuron newNeuron = new WorkingNeuron(); newNeuron.Deserialize(reader); outputNeurons.Add(newNeuron); int connectionCount = reader.ReadInt32(); for (int connectionIndex = 0; connectionIndex < connectionCount; connectionIndex++) { // Todo get rid of this covariance cast Connection connection = Connection.Deserialize(reader, hiddenNeurons.Cast <Neuron>()); newNeuron.AddNeuronConnection(connection); } } Invalidate(); }
public void CreateHiddenLayer() { List <Neuron> newLayer = new List <Neuron>(); WorkingNeuron newNeuron = new WorkingNeuron(HiddenLayerCount + 2); int layerIndex = neurons.Count - 1; int prevLayerIndex = layerIndex - 1; int connectionCount = 0; while (prevLayerIndex >= 0) { if (layerIndex > 1 && prevLayerIndex == 0) { break; } float weight = 0.0f; if (connectionCount < LastHiddenLayer.Count && connectionCount < ((WorkingNeuron)LastHiddenLayer[connectionCount]).GetConnections().Count) { weight = ((WorkingNeuron)LastHiddenLayer[0]).GetConnections()[connectionCount].weight; } for (int prevNeuronIndex = connectionCount; prevNeuronIndex < neurons[prevLayerIndex].Count && ((!(prevLayerIndex == 0) || connectionCount < FirstHiddenLayer.Count) || layerIndex == 1); ++prevNeuronIndex) { newNeuron.AddNeuronConnection(neurons[prevLayerIndex][prevNeuronIndex], weight); connectionCount++; } --prevLayerIndex; } for (int outputIndex = 0; outputIndex < OutputNeurons.Count; outputIndex++) { WorkingNeuron currentOutputNeuron = OutputNeurons[outputIndex] as WorkingNeuron; for (int connectionIndex = 0; connectionIndex < currentOutputNeuron.GetConnections().Count; connectionIndex++) { if (currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron == LastHiddenLayer[0]) { currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron = newNeuron; } } } newLayer.Add(newNeuron); AddHiddenLayer(newLayer); }
public void RemoveHiddenLayer() { if (HiddenLayerCount > 1) { neurons.RemoveAt(neurons.Count - 2); for (int outNeuronIndex = 0; outNeuronIndex < OutputNeurons.Count; outNeuronIndex++) { WorkingNeuron outputNeuron = OutputNeurons[outNeuronIndex] as WorkingNeuron; if (outputNeuron.GetConnections().Count > LastHiddenLayer.Count) { outputNeuron.GetConnections().RemoveRange(LastHiddenLayer.Count, outputNeuron.GetConnections().Count - LastHiddenLayer.Count); } for (int connectionIndex = 0; connectionIndex < LastHiddenLayer.Count && connectionIndex < outputNeuron.GetConnections().Count; connectionIndex++) { outputNeuron.GetConnections()[connectionIndex].entryNeuron = LastHiddenLayer[connectionIndex]; } for (int connectionIndex = outputNeuron.GetConnections().Count; connectionIndex < LastHiddenLayer.Count; connectionIndex++) { outputNeuron.AddNeuronConnection(LastHiddenLayer[connectionIndex], (Simulation.RandomFloat() * 2 * -1)); } } } }
public NeuronalNetwork CloneFullMesh() { if (!fullMeshGenerated) { throw new NeuronalNetworkNotFullmeshedException(); } NeuronalNetwork copy = new NeuronalNetwork(); Dictionary <Neuron, Neuron> oldToNewMap = new Dictionary <Neuron, Neuron>(); foreach (InputNeuron input in InputNeurons) { Neuron newNeuron = input.NameCopy(); oldToNewMap.Add(input, newNeuron); copy.AddInputNeuron((InputNeuron)newNeuron); } for (int layerIndex = 0; layerIndex < HiddenLayerCount; layerIndex++) { List <Neuron> newLayer = new List <Neuron>(); foreach (WorkingNeuron wn in neurons[layerIndex + 1]) { WorkingNeuron newNeuron = wn.NameCopy() as WorkingNeuron; oldToNewMap.Add(wn, newNeuron); newLayer.Add(newNeuron); foreach (Connection con in wn.GetConnections()) { newNeuron.AddNeuronConnection(oldToNewMap[con.entryNeuron], con.weight); } } copy.AddHiddenLayer(newLayer); } foreach (Neuron output in OutputNeurons) { WorkingNeuron newNeuron = output.NameCopy() as WorkingNeuron; copy.AddOutputNeuron(newNeuron); foreach (Connection con in (output as WorkingNeuron).GetConnections()) { newNeuron.AddNeuronConnection(oldToNewMap[con.entryNeuron], con.weight); } } copy.fullMeshGenerated = true; //copy.GenerateFullMesh(); //for (int layerIndex = 1; layerIndex < neurons.Count; layerIndex++) //{ // for (int neuronIndex = 0; neuronIndex < neurons[layerIndex].Count; neuronIndex++) // { // List<Connection> connectionsOriginal = ((WorkingNeuron)neurons[layerIndex][neuronIndex]).GetConnections(); // List<Connection> connectionsCopy = ((WorkingNeuron)copy.neurons[layerIndex][neuronIndex]).GetConnections(); // if (connectionsOriginal.Count != connectionsCopy.Count) // { // throw new NotSameAmountOfNeuronsException(); // } // for (int k = 0; k < connectionsOriginal.Count; k++) // { // connectionsCopy[k].weight = connectionsOriginal[k].weight; // } // } //} return(copy); }