/// <summary> /// Randomizes the weights of all connections between all neurons in range of -5.0 to 5.0 /// </summary> public void RandomizeWeights() { MyRandom r = new MyRandom(-0.5, 0.5); //set random weights for output foreach (Neuron itOut in outputLayer) { List <Connection> con = ((WorkingNeuron)(itOut)).connections; foreach (Connection c in con) { c.SetWeight(r.NextDouble()); } } //set random weights for hidden foreach (Neuron itHid in hiddenLayer) { WorkingNeuron wn = itHid as WorkingNeuron; if (wn != null) { List <Connection> con = wn.connections; foreach (Connection c in con) { c.SetWeight(r.NextDouble()); } } } }
/// <summary> /// Creates the mesh of the connection the neurons are maintaining, called after deserialization! /// </summary> public void CreateMesh() { foreach (Neuron n in hiddenLayer) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { foreach (Connection c in wn.connections) { c.sourceNeuron = this.GetNeuronByName(c.sourceNeuronName); } } } foreach (Neuron n in outputLayer) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { foreach (Connection c in wn.connections) { c.sourceNeuron = this.GetNeuronByName(c.sourceNeuronName); } } } }
public override Neuron Copy() { WorkingNeuron n = new WorkingNeuron(); n.name = this.name; n.value = this.value; n.activationfunction = activationfunction; n.parentNetwork = this.parentNetwork; return(n); }
public void Invalidate() { for (int l = layers.Count - 1; l > 0; --l) { foreach (Neuron n in layers.ElementAt(l)) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { wn.Invalidate(); } } } }
public void Invalidate() { foreach (Neuron itOut in outputLayer) { ((WorkingNeuron)(itOut)).Invalidate(); } foreach (Neuron itHid in hiddenLayer) { WorkingNeuron n = itHid as WorkingNeuron; if (n != null) { n.Invalidate(); } } }
/// <summary> /// Creates the mesh of the connection the neurons are maintaining, called after deserialization! /// </summary> public void CreateMesh() { for (int l = layers.Count - 1; l > 0; --l) { foreach (Neuron n in layers.ElementAt(l)) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { foreach (Connection c in wn.connections) { c.sourceNeuron = this.GetNeuronByName(c.sourceNeuronName); } } } } }
/// <summary> /// Function for Network Manipulation /// connect each hidden neuron with each input neuron /// and each output neuron with each hidden neuron /// </summary> public void CreateFullMesh() { //connect each hidden neuron to each input neuron for (int l = layers.Count - 1; l > 0; --l) { foreach (Neuron n in layers.ElementAt(l)) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { foreach (Neuron itIn in layers.ElementAt(l - 1)) { wn.AddConnection(itIn, 1.0); } } } } }
/// <summary> /// Randomizes the weights of all connections between all neurons in range of -5.0 to 5.0 /// </summary> public void RandomizeWeights() { MyRandom r = new MyRandom(-0.5, 0.5); for (int l = layers.Count - 1; l > 0; --l) { foreach (Neuron n in layers.ElementAt(l)) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { List <Connection> con = wn.connections; foreach (Connection c in con) { c.SetWeight(r.NextDouble()); } } } } }
/// <summary> /// Function for Network Manipulation /// connect each hidden neuron with each input neuron /// and each output neuron with each hidden neuron /// </summary> public void CreateFullMesh() { //Connect each output neuron to each hidden layer neuron foreach (Neuron itOut in outputLayer) { foreach (Neuron itHid in hiddenLayer) { ((WorkingNeuron)(itOut)).AddConnection(itHid, 1.0); } } //connect each hidden neuron to each input neuron foreach (Neuron itHid in hiddenLayer) { WorkingNeuron wn = itHid as WorkingNeuron; if (wn != null) { foreach (Neuron itIn in inputLayer) { wn.AddConnection(itIn, 1.0); } } } }
public WorkingNeuron(WorkingNeuron n) : base(n) { valid = n.valid; connections = new List <Connection>(); }
/// <summary> /// (Deep) Copy Constructor for NeuroalNet /// </summary> /// <param name="net"></param> public NeuralNetwork(NeuralNetwork net) : this(net.layers.Count - 2) { settings = net.settings; name = "Copy of " + net.name; score = -1.0f; foreach (Neuron inNeuron in net.layers.ElementAt(0)) { Neuron n; if ((n = inNeuron as InputNeuron) != null) { AddInputNeuron(n.Copy()); } else if ((n = inNeuron as BiasNeuron) != null) { AddInputNeuron(n.Copy()); } } foreach (Neuron outNeuron in net.layers.ElementAt(net.layers.Count - 1)) { Neuron n; if ((n = outNeuron as WorkingNeuron) != null) { AddOutputNeuron(n.Copy()); } } for (int l = 1; l <= net.layers.Count - 2; ++l) { foreach (Neuron hidNeuron in net.layers.ElementAt(l)) { Neuron n; if ((n = hidNeuron as WorkingNeuron) != null) { AddHiddenNeuron(n.Copy(), l); } else if ((n = hidNeuron as BiasNeuron) != null) { AddHiddenNeuron(n.Copy(), l); } } } //generate Full mesh this.CreateFullMesh(); //copy weights //hidden layers for (int l = 1; l < layers.Count - 2; ++l) { for (int i = 0; i < layers.ElementAt(l).Count; ++i) { WorkingNeuron n = layers.ElementAt(l).ElementAt(i) as WorkingNeuron; if (n != null) { for (int c = 0; c < ((WorkingNeuron)net.layers.ElementAt(l).ElementAt(i)).connections.Count; ++c) { n.connections.ElementAt(c).SetWeight(((WorkingNeuron)net.layers.ElementAt(l).ElementAt(i)).connections.ElementAt(c).GetWeight()); } } } } //output layer for (int i = 0; i < layers.ElementAt(layers.Count - 1).Count; ++i) { WorkingNeuron n = layers.ElementAt(layers.Count - 1).ElementAt(i) as WorkingNeuron; if (n != null) { for (int c = 0; c < ((WorkingNeuron)net.layers.ElementAt(layers.Count - 1).ElementAt(i)).connections.Count; ++c) { n.connections.ElementAt(c).SetWeight(((WorkingNeuron)net.layers.ElementAt(layers.Count - 1).ElementAt(i)).connections.ElementAt(c).GetWeight()); } } } }
/// <summary> /// (Deep) Copy Constructor for NeuroalNet /// </summary> /// <param name="net"></param> public NeuralNetwork(NeuralNetwork net) { inputLayer = new List <Neuron>(); outputLayer = new List <Neuron>(); hiddenLayer = new List <Neuron>(); layers = new List <List <Neuron> >(); layers.Add(inputLayer); layers.Add(hiddenLayer); layers.Add(outputLayer); settings = net.settings; name = "Copy of " + net.name; score = -1.0f; foreach (Neuron inNeuron in net.inputLayer) { Neuron n; if ((n = inNeuron as InputNeuron) != null) { AddInputNeuron(n.Copy()); } else if ((n = inNeuron as BiasNeuron) != null) { AddInputNeuron(n.Copy()); } } foreach (Neuron outNeuron in net.outputLayer) { Neuron n; if ((n = outNeuron as WorkingNeuron) != null) { AddOutputNeuron(n.Copy()); } } foreach (Neuron hidNeuron in net.hiddenLayer) { Neuron n; if ((n = hidNeuron as WorkingNeuron) != null) { AddHiddenNeuron(n.Copy()); } else if ((n = hidNeuron as BiasNeuron) != null) { AddHiddenNeuron(n.Copy()); } } //generate Full mesh this.CreateFullMesh(); //copy weights //hidden layer for (int i = 0; i < hiddenLayer.Count; ++i) { WorkingNeuron n = hiddenLayer.ElementAt(i) as WorkingNeuron; if (n != null) { for (int c = 0; c < ((WorkingNeuron)net.hiddenLayer.ElementAt(i)).connections.Count; ++c) { n.connections.ElementAt(c).SetWeight(((WorkingNeuron)net.hiddenLayer.ElementAt(i)).connections.ElementAt(c).GetWeight()); } } } //output layer for (int i = 0; i < outputLayer.Count; ++i) { WorkingNeuron n = outputLayer.ElementAt(i) as WorkingNeuron; if (n != null) { for (int c = 0; c < ((WorkingNeuron)net.outputLayer.ElementAt(i)).connections.Count; ++c) { n.connections.ElementAt(c).SetWeight(((WorkingNeuron)net.outputLayer.ElementAt(i)).connections.ElementAt(c).GetWeight()); } } } }