public void CachingCanBeConfiguredOnConstruction() { neuron=new Neuron(); Assert.AreEqual(neuron.IsCachingActivationResults, false); neuron = new Neuron(true); Assert.AreEqual(neuron.IsCachingActivationResults, true); }
public override void AddNeuron(Neuron NeuronToAdd) { if( NeuronToAdd is InputNeuron ) this.NeuronsOwned.Add(NeuronToAdd); else throw new Exception("Only InputNeurons can be added to an InputLayer"); }
public void LastLayerGivesDeltasAndWeightsToTheOneBefore() { Neuron n31 = new Neuron(); Neuron n32 = new Neuron(); BiasNeuron bias2 = new BiasNeuron(); ; Neuron n21 = new Neuron(); Neuron n22 = new Neuron(); n31.Connect(bias2); n31.Connect(n21); n31.Connect(n22); n32.Connect(bias2); n32.Connect(n21); n32.Connect(n22); InputNeuron input = new InputNeuron(); BiasNeuron bias1 = new BiasNeuron(); n21.Connect(bias1); n21.Connect(input); n22.Connect(bias1); n22.Connect(input); input.Input = 1; n31.SetAnswer(0.9); n32.SetAnswer(0.1); n31.PropagateBackwards(); n32.PropagateBackwards(); double delta31 = n31.GetDelta(); double delta32 = n32.GetDelta(); double n21_n31 = n31.Weights[1]; double n21_n32 = n32.Weights[1]; n21.PropagateBackwards(); double desired_delta_for_n21 = n21_n31*delta31 + n21_n32*delta32; Assert.AreEqual(desired_delta_for_n21, n21.GetDelta()); }
public void TestAccumulatesWeightShift() { Neuron n31 = new Neuron(); BiasNeuron bias2 = new BiasNeuron(); ; Neuron n21 = new Neuron(); n31.Connect(bias2); n31.Connect(n21); InputNeuron input = new InputNeuron(); BiasNeuron bias1 = new BiasNeuron(); n21.Connect(bias1); n21.Connect(input); input.Input = 1; n31.SetAnswer(0.9); double[] ws = n31.GetWeightShifts(); double acc = ws[1]; Assert.AreEqual(acc, 0); n31.PropagateBackwards(); ws = n31.GetWeightShifts(); acc = ws[1]; Assert.AreNotEqual(acc, 0); n31.ApplyTraining(0, 1); ws = n31.GetWeightShifts(); acc = ws[1]; Assert.AreEqual(acc, 0); }
public void Think_SetsValuesInOutputSynapses() { Neuron neuron = new Neuron(); // Reset the bias, since we know the ctor provides a random bias. neuron.Bias = 0; // Add two inputs to the neuron. neuron.Inputs.Add(new Synapse { Value = 1, Weight = 4 }); neuron.Inputs.Add(new Synapse { Value = 3, Weight = 2 }); // Add two outputs to the neuron. // Causing the neuron to think should set the outputs values. neuron.Outputs.Add(new Synapse()); neuron.Outputs.Add(new Synapse()); // Execute the code to test. neuron.Think(); //// Think sums the neurons bias (which we set to 0 to disregard) //// with each inputs weight times value. //// This means sum = bias + (1 * 4) + (2 * 3) = 10. //// It then runs the total through a sigmoid function. //// 1 / (1 + Math.Pow(Math.E, -sum)) //// = 1 / (1 + e^-10) //// = 0.999954 double expected = 1 / (1 + Math.Pow(Math.E, -10)); // Validate that the outputs now have values. Assert.AreEqual(expected, neuron.Outputs[0].Value); Assert.AreEqual(expected, neuron.Outputs[1].Value); }
static void Main(string[] args) { int[,] inputPatterns = new int[,] { {0,0,0,0}, {0,0,0,1}, {0,0,1,0}, {0,0,1,1}, {0,1,0,0}, {0,1,0,1}, {0,1,1,0}, {0,1,1,1}, {1,0,0,0}, {1,0,0,1}, {1,0,1,0}, {1,0,1,1}, {1,1,0,0}, {1,1,0,1}, {1,1,1,0}, {1,1,1,1} }; int[,] outputPatterns = new int[,] { {1,1,1,1}, {1,0,0,1}, {0,1,1,0}, {0,0,1,1}, {0,1,1,0}, {0,1,0,1}, {0,1,1,0}, {1,1,1,1}, {1,0,0,1}, {1,0,0,1}, {1,0,1,0}, {1,1,1,1}, {1,1,0,0}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1} }; Neuron inputNeuron1 = new Neuron(); inputNeuron1.Weights = new double[]{1}; Neuron inputNeuron2 = new Neuron(); inputNeuron2.Weights = new double[] { 1 }; Neuron inputNeuron3 = new Neuron(); inputNeuron3.Weights = new double[] { 1 }; Neuron inputNeuron4 = new Neuron(); inputNeuron4.Weights = new double[] { 1 }; Neuron hiddenNeuron1 = new Neuron(); hiddenNeuron1.Inputs = new double[] { inputNeuron1.Output(), inputNeuron2.Output(), inputNeuron3.Output(), inputNeuron4.Output() }; }
public void AddOutboundConnection_ValidatesArgs() { // Setup var neuron = new Neuron(0.0d, new Mock<IActivationFunction>().Object); // Execute/Verify neuron.AddOutboundConnection(null); }
public Brain() { anchor_neurons = new List<Neuron>(); neurons = new NeuronPool( this ); synapses = new SynapsePool( this ); _default_neuron = new Neuron(); _default_synapse = new Synapse(); }
public Connection(Neuron neuron, double weight) { if(neuron == null) { throw (new NetworkException()); } this._neuron = neuron; this._weight = weight; }
public NeuralNet(int inputs, int outputs, int[] layers, float[] weights, float bias) { //string weightString = ""; //foreach (var w in weights) //{ // weightString += w + " "; //} //Debug.Log("Initialized net with bias: " + bias + " and weights: " + weightString); NumWeights = GetNumWeights(inputs, outputs, layers); if (weights.Length != NumWeights) { throw new System.Exception("Incorrect number of weights - wanted " + NumWeights + " got: " + weights.Length); } Weights = weights; Bias = bias; Net = new Neuron[layers.Length + 2][]; //setup arrays Net[0] = new Neuron[inputs]; Net[Net.Length - 1] = new Neuron[outputs]; for (int i = 0; i < layers.Length; i++) { Net[i + 1] = new Neuron[layers[i]]; } //setup input neurons for (int i = 0; i < Net[0].Length; i++) { Net[0][i] = new Neuron(Bias); } //setup other neurons int weightIndex = 0; for (int layer = 1; layer < Net.Length; layer++) { for (int i = 0; i < Net[layer].Length; i++) { Net[layer][i] = new Neuron(Bias); for (int p = 0; p < Net[layer - 1].Length; p++) { Net[layer][i].Connections.Add( new Connection() { neuron = Net[layer - 1][p], weight = weights[weightIndex] } ); weightIndex++; } } } }
public NeuronNet(int sizeX, int size) { this.sizeX = sizeX; this.size = size; this.levelOne = new Neuron[this.sizeX]; for (int i = 0; i < this.sizeX; i++) this.levelOne[i] = new Neuron(); this.levelTwo = new Neuron(this.sizeX); }
void OnGUI() { if (editorWindow) editorWindow.minSize = new Vector2(512, 512); else ShowEditor(); if (brain == null) StatusMsg = "nothing loaded!"; else StatusMsg = "loaded " + brain.name; GUILayout.Label("Status: " + StatusMsg); assetName = GUILayout.TextField(assetName, 32, GUILayout.ExpandWidth(true)); if (GUILayout.Button("Create")) { var asset = CreateInstance<Brain>(); DataAssetCreator.CreateDataAsset(assetName, asset); if (asset != null) { //asset.Name = assetName; brain = asset; //StatusMsg = "created " + Graph.Name; } } GUI.enabled = assetName != ""; if (GUILayout.Button("Load")) { Brain tmp = Resources.Load<Brain>(assetName); if (tmp != null) { //StatusMsg = "loaded " + tmp.Name; //Graph = tmp; brain = tmp; } } GUI.enabled = brain != null; if (GUILayout.Button("add neuron")) { var neuron = new Neuron(); neuron.count = 1; brain.network.Add(neuron); } if (GUI.changed) { EditorUtility.SetDirty(brain); } }
public LinkWeight(INeuralNode from, Neuron to) { this.Weight = RandomGenerator.Generate(); this.From = @from; this.To = to; this.From.AddOutput(this); this.PreviousDelta = 0; }
public NeuronLayer(int numNeurons, int numInputs) { this.numNeurons = numNeurons; neurons = new Neuron[numNeurons]; //Initialize each neuron in the layer. for(int currNeuron = 0; currNeuron < numNeurons; ++currNeuron) { neurons[currNeuron] = new Neuron(numInputs); } }
public int this[Neuron n] { get { if( n.Equals( to ) ) { _cycle = n.Cycle; return result; } return 0; } }
public double UpdateWeights(double[] pattern, Neuron winner, int it) { double sum = 0; for (var i = 0; i < Weights.Length; i++) { var delta = LearningRate(it) * Gauss(winner, it) * (pattern[i] - Weights[i]); Weights[i] += delta; sum += delta; } return sum / Weights.Length; }
public NeuralNetworks(int inputNumber, int outputNumber, int hiddenLayerWidth, int hiddenLayerHeight) { HiddenLayerWeights = new Tensor(hiddenLayerWidth, hiddenLayerHeight, hiddenLayerHeight); InputLayerWeights = new Matrix(hiddenLayerHeight, inputNumber); OutputLayerWeights = new Matrix(outputNumber, hiddenLayerHeight); HiddenLayerNeuron = new Neuron[hiddenLayerWidth, hiddenLayerHeight]; OutputLayerNeuron = new Neuron[outputNumber]; InputNumber = inputNumber; OutputNumber = outputNumber; HiddenLayerWidth = hiddenLayerWidth; HiddenLayerHeight = hiddenLayerHeight; Random random = new Random(); //Initialize weights randomly from -1 to 1 for (int i = 0; i < HiddenLayerWeights.Width; i++) { for (int j = 0; j < HiddenLayerWeights.Height; j++) { for (int k = 0; k < HiddenLayerWeights.Depth; k++) { if (random.Next() % 2 == 0) HiddenLayerWeights[i, j, k] = random.NextDouble(); else HiddenLayerWeights[i, j, k] = -random.NextDouble(); } } } for (int i = 0; i < hiddenLayerHeight; i++) { for (int j = 0; j < inputNumber; j++) InputLayerWeights[i, j] = random.NextDouble(); } for (int i = 0; i < outputNumber; i++) { for (int j = 0; j < hiddenLayerHeight; j++) OutputLayerWeights[i, j] = random.NextDouble(); } //Initialize neurons for (int i = 0; i < HiddenLayerWidth; i++) { for (int j = 0; j < hiddenLayerHeight; j++) HiddenLayerNeuron[i, j] = new Neuron(); } for (int i = 0; i < outputNumber; i++) OutputLayerNeuron[i] = new Neuron(); //Initizlize input as a 0 vector LastInput = new Vector(InputNumber); for (int i = 0; i < inputNumber; i++) LastInput[i] = 0; }
public NeuralNetwork(RoomControl rc, FrogMove fm) {//Inputs are fed to the hidden layer which are in turn fed into the visible layer hiddenLayer = new Neuron[hiddenSize]; this.rc = rc; this.fm = fm; for (int i = 0; i < hiddenSize; i++) { hiddenLayer [i] = new Neuron(this, numInputs, 1); } topLayer = new Neuron[numOutputs]; topLayer [0] = new Neuron(this, hiddenSize, 1); topLayer [1] = new Neuron(this, hiddenSize, 1); topLayer [2] = new Neuron(this, hiddenSize, 0); topLayer [3] = new Neuron(this, hiddenSize, 2); }
public NeuralNetwork(RoomControl rc, FrogMove fm, Neuron[] neurons) {//We already have the whole neuron array (called in generations after the 1st) hiddenLayer = new Neuron[hiddenSize]; this.rc = rc; this.fm = fm; int i = 0; for (; i < hiddenSize; i++) { hiddenLayer [i] = neurons [i]; hiddenLayer [i].SetParent(this); } topLayer = new Neuron[numOutputs]; for (int x=0; x<numOutputs; x++) { topLayer [x] = neurons [i + x]; topLayer [x].SetParent(this); } }
public Brain() { // INITIALIZATION Inputs = new List<NeuronInput>(); Outputs = new List<Neuron>(); MainColumn = new List<Neuron>(); // REFERENCE GRABS AHK = AHKController.instance.gameObject; Controller = AHKController.instance; // INPUTS for (int i = 0; i < InputCount; i++) { Inputs.Add(new NeuronInput()); } // MAIN COLUMN for (int i = 0; i < InputCount + 1; i++) { Neuron tempNeuron = new Neuron(); for (int j = 0; j < InputCount; j++) { tempNeuron.InputSynapses.Add(new Synapse(Inputs[j])); } MainColumn.Add(tempNeuron); } // OUTPUTS for (int i = 0; i < OutputCount; i++) { Neuron tempNeuron = new Neuron(); for (int j = 0; j < InputCount + 1; j++) { tempNeuron.InputSynapses.Add(new Synapse(MainColumn[j])); } Outputs.Add(tempNeuron); } // TRAINERS TrainingSet = new List<ANNTrainer>(); TrainingSet.Add(new TrainerBaseline(this)); TrainingSet.Add(new TrainerCorrelation(this)); TrainingSet.Add(new TrainerGeneticHover(this)); }
public void AddInboundConnection_WillNotAddDuplicates() { // Setup var mockConnection1 = new Mock<INeuralConnection>(); var mockConnection2 = new Mock<INeuralConnection>(); var neuron = new Neuron(0.0d, new Mock<IActivationFunction>().Object); // Execute neuron.AddInboundConnection(mockConnection1.Object); neuron.AddInboundConnection(mockConnection2.Object); neuron.AddInboundConnection(mockConnection1.Object); // Verify Assert.AreEqual(2, neuron.InboundConnections.Count); Assert.IsTrue(neuron.InboundConnections.Contains(mockConnection1.Object)); Assert.IsTrue(neuron.InboundConnections.Contains(mockConnection2.Object)); }
public static Neuron[] Breed(Neuron[] lnn, Neuron[] rnn) { //Mutation happens under CopyWeight Neuron[] baby = new Neuron[numOutputs + hiddenSize]; for (int i=0; i<baby.Length; i++) { //For every Neuron, cross lnn and rnn and add it to the array int length = lnn [i].WeightsLength(); int index = Random.Range(0, length); float[] weights = new float[length]; lnn [i].CopyWeights(weights, 0, index); rnn [i].CopyWeights(weights, index, weights.Length - index); if (i == baby.Length - 2) { //Making the jump neuron baby [i] = new Neuron(weights, 0); } else if (i == baby.Length - 1) { //Making the speed neuron baby [i] = new Neuron(weights, 2); } else { baby [i] = new Neuron(weights, 1); } } return baby; }
public void Both_inputs_low() { var neuronA = new InputNeuron(0, 1); var neuronB = new InputNeuron(0, 1); var hiddenLayerA = new Neuron(1.5, -1); var hiddenLayerB = new Neuron(.5, 1); var result = new OutputNeuron(.5, 1, 0); var network = new Network(); network.Connect(neuronA, hiddenLayerA); network.Connect(neuronA, hiddenLayerB); network.Connect(neuronB, hiddenLayerA); network.Connect(neuronB, hiddenLayerB); network.Connect(hiddenLayerA, result); network.Connect(hiddenLayerB, result); Assert.IsFalse(network.Execute()); }
public override void Fire() { Init(); //be sure to leave this here if (synth == null) { return; } bool paused = true; for (int i = 1; i < na.NeuronCount; i++) { Neuron n = na.GetNeuronAt(i); if (n.Fired()) { if (n.Label.Length == 1) { phraseToSpeak += n.Label; paused = false; } if (n.Synapses.Count == 0) { //if a neuron fired and it has no connection, connect it to the knowledge store //connection to KB ModuleUKSN nmKB = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN)); if (nmKB != null) { string label = "pn" + n.Label; List <Thing> phonemes = nmKB.Labeled("Phoneme").Children; Thing pn = nmKB.Labeled(label, phonemes); if (pn == null) //this should always be null { pn = nmKB.AddThing(label, new Thing[] { nmKB.Labeled("Phoneme") }, pn); } Neuron n1 = nmKB.GetNeuron(pn); if (n1 != null) { n.AddSynapse(n1.Id, 1); n1.SetValue(1); } } } } } if (phonemesToFire != "") { char c = phonemesToFire[0]; bool fired = false; for (int i = 0; i < na.NeuronCount; i++) { Neuron n = na.GetNeuronAt(i); if (n.Label == c.ToString()) { n.SetValue(1); fired = true; break; } } if (!fired) { Utils.Noop(); } phonemesToFire = phonemesToFire.Substring(1); } if (paused && phraseToSpeak != "") { if (dlg != null) { ((ModuleSpeakPhonemesDlg)dlg).SetLabel(phraseToSpeak); } if (na.GetNeuronAt("Enable").Fired()) { ModuleSpeechIn msi = (ModuleSpeechIn)FindModuleByType(typeof(ModuleSpeechIn)); if (msi != null) { msi.PauseRecognition(); //if there is a recognizer active } //synth.SpeakAsync(phraseToSpeak + "."); //phraseToSpeak = ""; PromptBuilder pb1 = new PromptBuilder(); if (typedIn) { pb1.StartVoice("Microsoft David Desktop"); pb1.StartStyle(new PromptStyle(PromptRate.Medium)); } else { pb1.StartVoice("Microsoft Zira Desktop"); pb1.StartStyle(new PromptStyle(PromptRate.ExtraSlow)); } pb1.AppendTextWithPronunciation("not used", phraseToSpeak); pb1.EndStyle(); pb1.EndVoice(); string x = pb1.ToXml(); Debug.WriteLine(debugString(phraseToSpeak)); //synth.Speak(pb1); synth.SpeakAsync(pb1); } //string heard = GetPronunciationFromText("", phraseToSpeak); //it would be nice to hear what was said but it doesn't work with this engine phraseToSpeak = ""; typedIn = false; } }
public void NeuronTestInitialize() { neuron = new Neuron(null, null, 0, new Random()); }
public override void Fire() { Init(); //be sure to leave this here if (synth == null) { return; } if (GetNeuronValue("Cancel") == 1) { synth.SpeakAsyncCancelAll(); } if (GetNeuronValue("Validate") == 1) { if (!validating) { hitWords.Clear(); missWords.Clear(); missPhrase.Clear(); hit = 0; miss = 0; } validating = true; } else { if (validating) { if (hit + miss == 0) { Debug.WriteLine("No Validation Data"); } else { Debug.WriteLine("Validation: " + hit + " / " + miss + " = " + 100 * hit / (hit + miss)); Debug.WriteLine("Validation: " + hitWords.Count + " / " + missWords.Count + " = " + 100 * hitWords.Count / (hitWords.Count + missWords.Count)); } } validating = false; } bool paused = true; for (int i = 3; i < na.NeuronCount; i++) { Neuron n = na.GetNeuronAt(i); if (n.Fired()) { if (n.Label.Length == 1) { phraseToSpeak += n.Label; paused = false; } if (n.Synapses.Count == 0) { //connect it to the knowledge store //connection to KB //ModuleUKS2 nmKB = (ModuleUKS2)FindModuleByName("AudibleUKS"); if (FindModuleByName("AudibleUKS") is ModuleUKS2 UKS) { string label = "pn" + n.Label; List <Thing> phonemes = UKS.Labeled("Phoneme").Children; Thing pn = UKS.Labeled(label, phonemes); if (pn == null) //this should always be null { pn = UKS.AddThing(label, new Thing[] { UKS.Labeled("Phoneme") }, pn); } Neuron n1 = UKS.GetNeuron(pn); Neuron n2 = UKS.GetNeuron(pn, false); if (n1 != null) { n.AddSynapse(n1.Id, 1); n1.SetValue(1); n2.AddSynapse(n.Id, 1); } } } } } if (phonemesToFire != "") { char c = phonemesToFire[0]; bool fired = false; if (c != ' ') { for (int i = 0; i < na.NeuronCount; i++) { Neuron n = na.GetNeuronAt(i); if (n.Label == c.ToString()) { n.SetValue(1); fired = true; break; } } if (!fired) { Neuron n = AddLabel(c.ToString()); //connect it to the knowledge store //connection to KB //ModuleUKS2 nmKB = (ModuleUKS2)FindModuleByName("AudibleUKS"); if (FindModuleByName("AudibleUKS") is ModuleUKS2 UKS) { string label = "pn" + n.Label; List <Thing> phonemes = UKS.Labeled("Phoneme").Children; Thing pn = UKS.Labeled(label, phonemes); if (pn == null) //this should always be null { pn = UKS.AddThing(label, new Thing[] { UKS.Labeled("Phoneme") }, pn); } Neuron n1 = UKS.GetNeuron(pn); Neuron n2 = UKS.GetNeuron(pn, false); if (n1 != null) { n.AddSynapse(n1.Id, 1); n2.AddSynapse(n.Id, 1); n.SetValue(1); } } } } phonemesToFire = phonemesToFire.Substring(1); } if (paused && phraseToSpeak != "") { if (dlg != null) { ((ModuleSpeakPhonemes2Dlg)dlg).SetLabel(phraseToSpeak); } if (na.GetNeuronAt("Enable").Fired()) { ModuleSpeechIn msi = (ModuleSpeechIn)FindModuleByType(typeof(ModuleSpeechIn)); if (msi != null) { msi.PauseRecognition(); //if there is a recognizer active } //synth.SpeakAsync(phraseToSpeak + "."); //phraseToSpeak = ""; PromptBuilder pb1 = new PromptBuilder(); if (typedIn) { pb1.StartVoice("Microsoft David Desktop"); pb1.StartStyle(new PromptStyle(PromptRate.Medium)); } else { pb1.StartVoice("Microsoft Zira Desktop"); pb1.StartStyle(new PromptStyle(PromptRate.Slow)); } pb1.AppendTextWithPronunciation("not used", phraseToSpeak.Trim()); pb1.EndStyle(); pb1.EndVoice(); string x = pb1.ToXml(); Debug.WriteLine(debugString(phraseToSpeak)); //synth.Speak(pb1); synth.SpeakAsync(pb1); } //string heard = GetPronunciationFromText("", phraseToSpeak); //it would be nice to hear what was said but it doesn't work with this engine phraseToSpeak = ""; typedIn = false; } }
public void AddConnection(Neuron from, Neuron to) { from.AddConnection(to, Innovation); Innovation++; }
public void initNN() { try { //Randomize the weights Random r = new Random(); for (int i = 0; i <= depth; i++) { if (i == 0) { for (int ii = 0; ii <= count - 1; ii++) { //Foreach space in the l1 weight array, randomize it double[,] temps = new double[, ] { { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) }, { Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)), Sigmoid.sigmoid(r.Next(-9, 9)) } }; Neuron n = new Neuron(this, temps, 0, 0); } } if (i >= 1 && i <= depth - 1) { for (int ii = 0; ii <= count - 1; ii++) { Neuron n = new Neuron(this, new Dictionary <Neuron, double>(), 0, i); n.layWeights.Clear(); foreach (Neuron neu in Neurons) { if (neu.layer == n.layer - 1) { if (!n.layWeights.ContainsKey(neu)) { //Make a connection of random weight to each neuron with a layer n - 1 lower n.layWeights.Add(neu, /* Weight for the neuron */ Sigmoid.sigmoid(r.Next(0, 999) / 100)); } } } } } if (i == depth) { //Make the final neuron (output) Neuron n = new Neuron(this, new Dictionary <Neuron, double>(), 0, i); n.layWeights.Clear(); foreach (Neuron neu in Neurons) { if (neu.layer == n.layer - 1) { if (!n.layWeights.ContainsKey(neu)) { //Make a connection of random weight to each neuron with a layer n - 1 lower n.layWeights.Add(neu, /* Weight for the neuron */ Sigmoid.sigmoid(r.Next(0, 999) / 100)); } } } //Output of the NN is this neuron Output = n; } } } //If it fails, print the error out catch (Exception ex) { Console.WriteLine(ex); } }
private static Network breed(int parentA, int parentB) { List<Edge> childEdges = new List<Edge> (); int AIndex = 0; int BIndex = 0; //merge genomes while (AIndex < networks[parentA].edges.Count && BIndex < networks[parentB].edges.Count) { if (networks [parentA].edges [AIndex].innovation == networks [parentB].edges [BIndex].innovation) { if (rand.Next (0, 2) == 0) { Edge e = new Edge(networks[parentA].edges[AIndex].inNeuron,networks[parentA].edges[AIndex].outNeuron,networks[parentA].edges[AIndex].weight, networks[parentA].edges[AIndex].isEnabled,networks[parentA].edges[AIndex].innovation); childEdges.Add (e); } else { Edge e = new Edge(networks[parentB].edges[BIndex].inNeuron,networks[parentB].edges[BIndex].outNeuron,networks[parentB].edges[BIndex].weight, networks[parentB].edges[BIndex].isEnabled,networks[parentB].edges[BIndex].innovation); childEdges.Add (e); } AIndex++; BIndex++; } else if (networks [parentA].edges [AIndex].innovation > networks [parentB].edges [BIndex].innovation) { BIndex++; } else { Edge e = new Edge(networks[parentA].edges[AIndex].inNeuron,networks[parentA].edges[AIndex].outNeuron,networks[parentA].edges[AIndex].weight, networks[parentA].edges[AIndex].isEnabled,networks[parentA].edges[AIndex].innovation); childEdges.Add (e); AIndex++; } } //excess edges left over from dominant parent if (BIndex == networks [parentB].edges.Count) { for (int i=AIndex; i<networks[parentA].edges.Count; i++) { Edge e = new Edge(networks[parentA].edges[i].inNeuron,networks[parentA].edges[i].outNeuron,networks[parentA].edges[i].weight, networks[parentA].edges[i].isEnabled,networks[parentA].edges[i].innovation); childEdges.Add (e); } } //perturb it double rand01 = rand.NextDouble (); if (rand01 < perturbChance) { double perturbAmount = (rand.NextDouble () * perturbPercent * 2.0 - perturbPercent); for (int i=0; i<childEdges.Count; i++) { //perturb it! sore aru if(rand.NextDouble() < 0.9){ childEdges [i].weight = childEdges [i].weight + perturbAmount; } else{ //give weight a new value; childEdges [i].weight = randomEdgeWeight(); } if (!childEdges [i].isEnabled) { rand01 = rand.NextDouble (); if (rand01 < enableMutationChance) { childEdges [i].isEnabled = true; } } } } //create the child network Network net = new Network (); //add inputs for(int j=0;j<inputN;j++){ Neuron n = new Neuron(); net.addNeuron(n); net.addInput(j); } //add outputs for(int j=0;j<outputN;j++){ Neuron n = new Neuron(); net.addNeuron(n); net.addOutput(inputN + j); } for (int j=0; j<(neuronN - inputN - outputN); j++) { Neuron n = new Neuron (); net.addNeuron (n); } for(int i=0;i<childEdges.Count;i++){ bool flag = net.addEdge(childEdges[i].inNeuron, childEdges[i].outNeuron, childEdges[i].weight, childEdges[i].innovation); if(flag==false){ //Console.WriteLine("wtf add edge failed"); } } //new links double rand2 = rand.NextDouble(); if (rand2 < linkMutationChance) { int tCount = 0; while(tCount <30){ int inputNode = rand.Next(0,neuronN - outputN); if(inputNode >= inputN){ inputNode += outputN; } int outputNode = rand.Next (inputN, neuronN); if(net.checkEdge(inputNode, outputNode)){ //add edge if the edge is connected to the input. if(net.neurons[inputNode].inputEdges.Count > 0 || inputNode < inputN){ bool tb = net.addEdge(inputNode, outputNode, randomEdgeWeight(), getInnovation(inputNode, outputNode)); if(tb){ tCount = 20; } } } tCount++; } } //new node rand2 = rand.NextDouble (); if (rand2 < nodeMutationChance) { int tCount = 0; while(tCount < 10){ int randomEdge = rand.Next(0,net.edges.Count); bool flag = true; int randomEdgeIn = net.edges[randomEdge].inNeuron; int randomEdgeOut = net.edges[randomEdge].outNeuron; //check if there already exists a node that does the same thing for(int i=0;i<innovationInputs.Count;i++){ if(innovationInputs[i] == randomEdgeIn){ for(int j=0;j<innovationInputs.Count;j++){ if(innovationInputs[j] == innovationOutputs[i]){ if(innovationOutputs[j] == randomEdgeOut){ flag = false; } } } } } //create new node if(flag){ net.edges[randomEdge].isEnabled = false; Neuron tN = new Neuron(); net.addNeuron(tN); bool tb = net.addEdge(net.edges[randomEdge].inNeuron, neuronN, randomEdgeWeight(), getInnovation(net.edges[randomEdge].inNeuron, neuronN)); bool tc = net.addEdge(neuronN, net.edges[randomEdge].outNeuron, randomEdgeWeight(), getInnovation(neuronN, net.edges[randomEdge].outNeuron)); if(!tb || !tc){ //Console.WriteLine("add Edge failed"); } neuronN++; tCount = 20; net.printNetwork(); } tCount++; } } return net; }
public Synapse(Neuron inputNeuron) { InputNeuron = inputNeuron; Weight = 1m; }
public void Mutate() { if (Random.value <= MutationRate) { float temp = Random.value; if (temp >= 0.66f) { //Remove neuron if (Random.value >= 0.5f) { int index = Random.Range(0, DecisionHiddenLayer.Count); DecisionHiddenLayer.RemoveAt(index); for (int i = 0; i < DecisionOutputLayer.Count; i++) { DecisionOutputLayer[i].weights.RemoveAt(index); } } else { int index = Random.Range(0, DirectionHiddenLayer.Count); DirectionHiddenLayer.RemoveAt(index); for (int i = 0; i < DirectionOutputLayer.Count; i++) { DirectionOutputLayer[i].weights.RemoveAt(index); } } } else if (temp >= 0.33f) { //Add neuron if (Random.value >= 0.5f) { List <float> l = new List <float>(); for (int i = 0; i < InputLayer.Count; i++) { l.Add(Random.value); } Neuron neur = new Neuron(InputLayer, l); DecisionHiddenLayer.Add(neur); foreach (Neuron n in DecisionOutputLayer) { n.weights.Add(Random.value); } } else { List <float> l = new List <float>(); for (int i = 0; i < InputLayer.Count; i++) { l.Add(Random.value); } Neuron neur = new Neuron(InputLayer, l); DirectionHiddenLayer.Add(neur); foreach (Neuron n in DirectionOutputLayer) { n.weights.Add(Random.value); } } } else { //weights random change for random neuron if (Random.value >= 0.5f) { int index = Random.Range(0, DecisionHiddenLayer.Count); for (int i = 0; i < DecisionHiddenLayer[index].weights.Count; i++) { DecisionHiddenLayer[index].weights[i] = Random.value; } } else { int index = Random.Range(0, DirectionHiddenLayer.Count); for (int i = 0; i < DirectionHiddenLayer[index].weights.Count; i++) { DirectionHiddenLayer[index].weights[i] = Random.value; } } } } }
/// <summary> /// Perform VERY FAST checks here /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="neuron">The neuron context to check</param> /// <returns></returns> protected virtual bool PreCheckLocation(int x, int y, Neuron neuron) { return(true); }
private XorTrResult trainByTwoNeuronsBySigmoid() { Debug.WriteLine("Działa"); float[,] inputs = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }; float[] results = { 0, 1, 1, 0 }; Neuron hiddenNeuron1 = new Neuron(); Neuron hiddenNeuron2 = new Neuron(); Neuron outputNeuron = new Neuron(); List <String> raport = new List <String>(); hiddenNeuron1.randomizeWeights(); raport.Add("Inicjalizacja: "); raport.Add("hiddenNeuron1.weights[0]: " + hiddenNeuron1.weights[0]); raport.Add("hiddenNeuron1.weights[1]: " + hiddenNeuron1.weights[1]); hiddenNeuron2.randomizeWeights(); raport.Add("hiddenNeuron2.weights[0]: " + hiddenNeuron2.weights[0]); raport.Add("hiddenNeuron2.weights[1]: " + hiddenNeuron2.weights[1]); outputNeuron.randomizeWeights(); raport.Add("outputNeuron.weights[0]: " + outputNeuron.weights[0]); raport.Add("outputNeuron.weights[1]: " + outputNeuron.weights[1]); bool isChanged = true; int epochs = 0; while (isChanged) { isChanged = false; for (int i = 0; i < 4; i++) { // 1) forward propagation (calculates output) hiddenNeuron1.inputs = new float[] { inputs[i, 0], inputs[i, 1] }; hiddenNeuron2.inputs = new float[] { inputs[i, 0], inputs[i, 1] }; outputNeuron.inputs = new float[] { hiddenNeuron1.output(), hiddenNeuron2.output() }; float result = outputNeuron.output(); Debug.WriteLine("{0} xor {1} = {2}", inputs[i, 0], inputs[i, 1], result); // 2) back propagation (adjusts weights) // adjusts the weight of the output neuron, based on its error result = activationFunctions.correctionForSigmoid(result); Debug.WriteLine("output po korekcji: " + result); if (result != results[i]) { isChanged = true; outputNeuron.error = Sigmoid.derivative(outputNeuron.output()) * (results[i] - outputNeuron.output()); outputNeuron.adjustWeights(trainingSetings.learningRate); // then adjusts the hidden neurons' weights, based on their errors hiddenNeuron1.error = Sigmoid.derivative(hiddenNeuron1.output()) * outputNeuron.error * outputNeuron.weights[0]; hiddenNeuron2.error = Sigmoid.derivative(hiddenNeuron2.output()) * outputNeuron.error * outputNeuron.weights[1]; hiddenNeuron1.adjustWeights(trainingSetings.learningRate); hiddenNeuron2.adjustWeights(trainingSetings.learningRate); raport.Add("Zmiana wag: "); raport.Add("hiddenNeuron1.weights[0]: " + hiddenNeuron1.weights[0]); raport.Add("hiddenNeuron1.weights[1]: " + hiddenNeuron1.weights[1]); raport.Add("hiddenNeuron2.weights[0]: " + hiddenNeuron2.weights[0]); raport.Add("hiddenNeuron2.weights[1]: " + hiddenNeuron2.weights[1]); raport.Add("outputNeuron.weights[0]: " + outputNeuron.weights[0]); raport.Add("outputNeuron.weights[1]: " + outputNeuron.weights[1]); } } epochs++; } raport.Add("Ilość epochs: " + epochs); return(new XorTrResult(hiddenNeuron1, hiddenNeuron2, outputNeuron, raport)); }
public void init() { neuron = new Neuron(); }
public Synapse(Neuron neuron, float weight) { this.neuron = neuron; this.weight = weight; }
public PackedNeuron(Neuron neuron) { innovationNb = neuron.InnovationNb; type = neuron.Type; }
void Setup() { Connection input1_hidden1 = new Connection (); Connection input1_hidden2 = new Connection (); Connection input1_hidden3 = new Connection (); Connection input1_hidden4 = new Connection (); Connection input1_hidden5 = new Connection (); Connection input2_hidden1 = new Connection (); Connection input2_hidden2 = new Connection (); Connection input2_hidden3 = new Connection (); Connection input2_hidden4 = new Connection (); Connection input2_hidden5 = new Connection (); Connection input3_hidden1 = new Connection (); Connection input3_hidden2 = new Connection (); Connection input3_hidden3 = new Connection (); Connection input3_hidden4 = new Connection (); Connection input3_hidden5 = new Connection (); Connection hidden1_output1 = new Connection (); Connection hidden2_output1 = new Connection (); Connection hidden3_output1 = new Connection (); Connection hidden4_output1 = new Connection (); Connection hidden5_output1 = new Connection (); Connection hidden1_output2 = new Connection (); Connection hidden2_output2 = new Connection (); Connection hidden3_output2 = new Connection (); Connection hidden4_output2 = new Connection (); Connection hidden5_output2 = new Connection (); Connection bias1_input1 = new Connection (); Connection bias2_input2 = new Connection (); Connection bias3_input3 = new Connection (); Neuron input1 = new Neuron (); Neuron bias1 = new Neuron (); Neuron input2 = new Neuron (); Neuron bias2 = new Neuron (); Neuron input3 = new Neuron (); Neuron bias3 = new Neuron (); Neuron hidden1 = new Neuron (); Neuron hidden2 = new Neuron (); Neuron hidden3 = new Neuron (); Neuron hidden4 = new Neuron (); Neuron hidden5 = new Neuron (); Neuron output1 = new Neuron (); Neuron output2 = new Neuron (); input1_hidden1.SetConnections (input1, hidden1); input1_hidden2.SetConnections (input1, hidden2); input1_hidden3.SetConnections (input1, hidden3); input1_hidden4.SetConnections (input1, hidden4); input1_hidden5.SetConnections (input1, hidden5); input2_hidden1.SetConnections (input2, hidden1); input2_hidden2.SetConnections (input2, hidden2); input2_hidden3.SetConnections (input2, hidden3); input2_hidden4.SetConnections (input2, hidden4); input2_hidden5.SetConnections (input2, hidden5); input3_hidden1.SetConnections (input3, hidden1); input3_hidden2.SetConnections (input3, hidden2); input3_hidden3.SetConnections (input3, hidden3); input3_hidden4.SetConnections (input3, hidden4); input3_hidden5.SetConnections (input3, hidden5); hidden1_output1.SetConnections (hidden1, output1); hidden2_output1.SetConnections (hidden2, output1); hidden3_output1.SetConnections (hidden3, output1); hidden4_output1.SetConnections (hidden4, output1); hidden5_output1.SetConnections (hidden5, output1); hidden1_output2.SetConnections (hidden1, output2); hidden2_output2.SetConnections (hidden2, output2); hidden3_output2.SetConnections (hidden3, output2); hidden4_output2.SetConnections (hidden4, output2); hidden5_output2.SetConnections (hidden5, output2); bias1_input1.SetConnections (bias1, input1); bias2_input2.SetConnections (bias2, input2); bias3_input3.SetConnections (bias3, input3); bias1.SetPlace (NeuronPlace.bias); bias1.AddOutputConnection (bias1_input1); bias2.SetPlace (NeuronPlace.bias); bias2.AddOutputConnection (bias2_input2); bias3.SetPlace (NeuronPlace.bias); bias3.AddOutputConnection (bias3_input3); input1.SetPlace (NeuronPlace.input); input1.AddOutputConnection (input1_hidden1); input1.AddOutputConnection (input1_hidden2); input1.AddOutputConnection (input1_hidden3); input1.AddOutputConnection (input1_hidden4); input1.AddOutputConnection (input1_hidden5); input2.SetPlace (NeuronPlace.input); input2.AddOutputConnection (input2_hidden1); input2.AddOutputConnection (input2_hidden2); input2.AddOutputConnection (input2_hidden3); input2.AddOutputConnection (input2_hidden4); input2.AddOutputConnection (input2_hidden5); input3.SetPlace (NeuronPlace.input); input3.AddOutputConnection (input3_hidden1); input3.AddOutputConnection (input3_hidden2); input3.AddOutputConnection (input3_hidden3); input3.AddOutputConnection (input3_hidden4); input3.AddOutputConnection (input3_hidden5); hidden1.SetPlace (NeuronPlace.hidden); hidden1.AddInputConnection (input1_hidden1); hidden1.AddInputConnection (input2_hidden1); hidden1.AddInputConnection (input3_hidden1); hidden1.AddOutputConnection (hidden1_output1); hidden1.AddOutputConnection (hidden1_output2); hidden2.SetPlace (NeuronPlace.hidden); hidden2.AddInputConnection (input1_hidden2); hidden2.AddInputConnection (input2_hidden2); hidden2.AddInputConnection (input3_hidden2); hidden2.AddOutputConnection (hidden2_output1); hidden2.AddOutputConnection (hidden2_output2); hidden3.SetPlace (NeuronPlace.hidden); hidden3.AddInputConnection (input1_hidden3); hidden3.AddInputConnection (input2_hidden3); hidden2.AddInputConnection (input3_hidden3); hidden3.AddOutputConnection (hidden3_output1); hidden3.AddOutputConnection (hidden3_output2); hidden4.SetPlace (NeuronPlace.hidden); hidden4.AddInputConnection (input1_hidden4); hidden4.AddInputConnection (input2_hidden4); hidden4.AddInputConnection (input3_hidden4); hidden4.AddOutputConnection (hidden4_output1); hidden4.AddOutputConnection (hidden4_output2); hidden5.SetPlace (NeuronPlace.hidden); hidden5.AddInputConnection (input1_hidden5); hidden5.AddInputConnection (input2_hidden5); hidden5.AddInputConnection (input3_hidden5); hidden5.AddOutputConnection (hidden5_output1); hidden5.AddOutputConnection (hidden5_output2); output1.SetPlace (NeuronPlace.output); output1.AddInputConnection (hidden1_output1); output1.AddInputConnection (hidden2_output1); output1.AddInputConnection (hidden3_output1); output1.AddInputConnection (hidden4_output1); output1.AddInputConnection (hidden5_output1); output2.SetPlace (NeuronPlace.output); output2.AddInputConnection (hidden1_output2); output2.AddInputConnection (hidden2_output2); output2.AddInputConnection (hidden3_output2); output2.AddInputConnection (hidden4_output2); output2.AddInputConnection (hidden5_output2); connections.Add (input1_hidden1); connections.Add (input1_hidden2); connections.Add (input1_hidden3); connections.Add (input1_hidden4); connections.Add (input1_hidden5); connections.Add (input2_hidden1); connections.Add (input2_hidden2); connections.Add (input2_hidden3); connections.Add (input2_hidden4); connections.Add (input2_hidden5); connections.Add (input3_hidden1); connections.Add (input3_hidden2); connections.Add (input3_hidden3); connections.Add (input3_hidden4); connections.Add (input3_hidden5); connections.Add (bias1_input1); connections.Add (bias2_input2); connections.Add (bias3_input3); connections.Add (hidden1_output1); connections.Add (hidden2_output1); connections.Add (hidden3_output1); connections.Add (hidden4_output1); connections.Add (hidden5_output1); connections.Add (hidden1_output2); connections.Add (hidden2_output2); connections.Add (hidden3_output2); connections.Add (hidden4_output2); connections.Add (hidden5_output2); neurons.Add (input1); neurons.Add (bias1); neurons.Add (input2); neurons.Add (bias2); neurons.Add (input3); neurons.Add (bias3); neurons.Add (hidden1); neurons.Add (hidden2); neurons.Add (hidden3); neurons.Add (hidden4); neurons.Add (hidden5); neurons.Add (output1); neurons.Add (output2); canRun = true; }
private void RunDynamicsBtn_Click(object sender, EventArgs e) { Bitmap img = new Bitmap(distImg); List <Neuron> initialState = new List <Neuron>(NN.N); startScanAnim = true; scanLineLoc = 0; scanLineDir = true; for (int i = 0; i < imageDim; i++) { for (int j = 0; j < imageDim; j++) { Neuron neuron = new Neuron(); int currentPixel = img.GetPixel(i, j).ToArgb(); if (currentPixel == Color.Black.ToArgb()) { neuron.State = NeuronStates.AgainstField; current.SetPixel(i, j, Color.Black); } else if (currentPixel == Color.White.ToArgb()) { neuron.State = NeuronStates.AlongField; current.SetPixel(i, j, Color.White); } initialState.Add(neuron); } } NN.Run(initialState); //lblEnergy.Text = NN.Energy.ToString(); startScanAnim = false; currentState.Image = current; int nearest = NearestPattern(initialState); Console.WriteLine(nearest); int k = 0; Bitmap nPattern = new Bitmap(imageDim, imageDim); for (int i = 0; i < imageDim; i++) { for (int j = 0; j < imageDim; j++) { if (patternList[nearest][k++].State == -1) { nPattern.SetPixel(i, j, Color.Black); } else { nPattern.SetPixel(i, j, Color.White); } } } k = 0; for (int i = 0; i < imageDim; i++) { for (int j = 0; j < imageDim; j++) { if (initialState[k++].State == -1) { current.SetPixel(i, j, Color.Black); } else { current.SetPixel(i, j, Color.White); } } } label4.Text = "This looks like the " + classifier[nearest] + " word:"; nearPat.Image = nPattern; }
public void SetConnections(Neuron outp) { end = outp; }
public override void Initialize() { Neuron nEnable = na.GetNeuronAt(0); nEnable.Label = "Enable"; Neuron nDisable = na.GetNeuronAt(1); nDisable.Label = "Disable"; nEnable.AddSynapse(nDisable.Id, -1); nDisable.AddSynapse(nDisable.Id, 1); //nDisable.CurrentCharge = 1; for (int i = 2; i < na.NeuronCount; i++) { Neuron n = na.GetNeuronAt(i); nDisable.AddSynapse(n.Id, -1); } Neuron nStop = na.GetNeuronAt(2); nStop.Label = "Stop"; Neuron nGo = na.GetNeuronAt(3); nGo.Label = "Go"; Neuron nLeft = na.GetNeuronAt(4); nLeft.Label = "Left"; Neuron nRight = na.GetNeuronAt(5); nRight.Label = "Right"; nStop.AddSynapse(nGo.Id, -1); nGo.AddSynapse(nGo.Id, 1); Neuron nFwd = GetNeuron("ModuleMove", "^"); if (nFwd != null) { nGo.AddSynapse(nFwd.Id, 1); } Neuron nKBGo = GetNeuron("KBOut", "Go"); if (nKBGo != null) { nKBGo.AddSynapse(nGo.Id, 1); } Neuron nKBStop = GetNeuron("KBOut", "Stop"); if (nKBStop != null) { nKBStop.AddSynapse(nStop.Id, 1); } Neuron nL = GetNeuron("ModuleTurn", "<"); Neuron nR = GetNeuron("ModuleTurn", ">"); if (nL != null) { nLeft.AddSynapse(nL.Id, 1); } if (nR != null) { nRight.AddSynapse(nR.Id, 1); } Neuron nLTurn = GetNeuron("KBOut", "LTurn"); if (nLTurn != null) { nLTurn.AddSynapse(nLeft.Id, 1); } Neuron nRTurn = GetNeuron("KBOut", "RTurn"); if (nRTurn != null) { nRTurn.AddSynapse(nRight.Id, 1); } nStop.AddSynapse(nLeft.Id, -1); nStop.AddSynapse(nRight.Id, -1); nLeft.AddSynapse(nLeft.Id, 1); nRight.AddSynapse(nRight.Id, 1); }
public NeuronChangedEventArgs(Neuron neuron) { Contract.Requires <ArgumentNullException>(neuron != null); Neuron = neuron; }
public Synapse(Neuron inputNeuron, decimal weight) { InputNeuron = inputNeuron; Weight = weight; }
public static void populationSetup() { finished = 0; networks = new List<Network> (); innovationInputs = new List<int> (); innovationOutputs = new List<int> (); for(int i=0;i<populationSize;i++){ Network net = new Network(); //add xorFitnessinputs for(int j=0;j<inputN;j++){ Neuron n = new Neuron(); net.addNeuron(n); net.addInput(j); } //add outputs for(int j=0;j<outputN;j++){ Neuron n = new Neuron(); net.addNeuron(n); net.addOutput(inputN + j); } neuronN = inputN + outputN; //add edges to outputs for(int j=0;j<inputN;j++){ for(int k=0;k<outputN;k++){ //add a random weight between -2 and 2 bool temp = net.addEdge(j, k+inputN, randomEdgeWeight(), getInnovation(j, k+inputN)); } } networks.Add(net); } startingIndexOfNewGenomes = 0; //initialize species species = new List<List<int>> (); speciesLastMaxFitness = new List<double> (); speciesLastFitnessImprovement = new List<int> (); }
private void button1_Click(object sender, EventArgs e) { neuron = new Neuron("AND-Neuron", 0, 2); neuron.addNewInput("input1", 0, 0); neuron.addNewInput("input2", 0, 0); PerceptronNetwork pn = new PerceptronNetwork(neuron); TrainingTemplate andTemplate = new TrainingTemplate("AND Template"); andTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 0 }, new List <double> { 0 })); andTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 1 }, new List <double> { 0 })); andTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 0 }, new List <double> { 0 })); andTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 1 }, new List <double> { 1 })); TrainingTemplate orTemplate = new TrainingTemplate("OR Template"); orTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 0 }, new List <double> { 0 })); orTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 1 }, new List <double> { 1 })); orTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 0 }, new List <double> { 1 })); orTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 1 }, new List <double> { 1 })); TrainingTemplate xorTemplate = new TrainingTemplate("XOR Template"); xorTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 0 }, new List <double> { 0 })); xorTemplate.addTrainingRow(new TrainingRow(new List <double> { 0, 1 }, new List <double> { 1 })); xorTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 0 }, new List <double> { 1 })); xorTemplate.addTrainingRow(new TrainingRow(new List <double> { 1, 1 }, new List <double> { 0 })); templatesList = new List <TrainingTemplate>(); ErrorHistory errorProg = new ErrorHistory(); double error = pn.train(xorTemplate, 100, errorProg); labelWeight1.Text = neuron.inputs[0].weight.ToString("N3"); labelWeight2.Text = neuron.inputs[1].weight.ToString("N3"); labelError.Text = error.ToString("N3"); for (int X = 0; X < errorProg.errorPoints.Count; X++) { chart1.Series["Error"].Points.AddXY(X, errorProg.errorPoints[X]); } //chart1.DataBind(errorProg); }
public Synapse(Neuron inputNeuron, Neuron outputNeuron) { InputNeuron = inputNeuron; OutputNeuron = outputNeuron; Weight = Network.GetRandom(); }
/// <summary> /// Constructs a Connection with the provided source and target neurons, and connection weight. /// </summary> public Connection(Neuron srcNeuron, Neuron tgtNeuron, double weight) { _tgtNeuron = tgtNeuron; _srcNeuron = srcNeuron; _weight = weight; }
public Connection(Neuron n, float wei) { Wei = wei; EntNeur = n; }
// We can connection two Neurons void Connect(Neuron a, Neuron b, float weight) { Connection c = CreateConnection(a, b, weight); a.AddConnection(c); //b.AddConnection(c); // Also add the Connection here MyConnections.Add(c); }
public void GenerateFromConnections(List <Connection> connList) { Clear(); foreach (var conn in connList) { Neuron sourceNeuron = FindNeuron(conn.Source.Index); Neuron targetNeuron = FindNeuron(conn.Target.Index); if (targetNeuron == null) { targetNeuron = conn.Target.Copy(); List <Neuron> sourceLayer = NeuronLayer(sourceNeuron); if (IsInput(sourceLayer)) { if (HiddenLayer.Count == 0) { List <Neuron> targetLayer = new List <Neuron>(); targetLayer.Add(targetNeuron); HiddenLayer.Add(targetLayer); } else { List <Neuron> targetLayer = HiddenLayer[0]; targetLayer.Add(targetNeuron); } } else { int hiddenPos = HiddenLayer.IndexOf(sourceLayer); if (hiddenPos == HiddenLayer.Count - 1) { List <Neuron> targetLayer = new List <Neuron>(); targetLayer.Add(targetNeuron); HiddenLayer.Add(targetLayer); } else { List <Neuron> targetLayer = HiddenLayer[hiddenPos + 1]; targetLayer.Add(targetNeuron); } } sourceNeuron.AddConnection(targetNeuron, conn); } else { List <Neuron> sourceLayer = NeuronLayer(sourceNeuron); List <Neuron> targetLayer = NeuronLayer(targetNeuron); if (sourceLayer == targetLayer) { int hiddenPos = HiddenLayer.IndexOf(sourceLayer); if (hiddenPos == HiddenLayer.Count - 1) { targetLayer = new List <Neuron>(); targetLayer.Add(targetNeuron); HiddenLayer.Add(targetLayer); } else { targetLayer = HiddenLayer[hiddenPos + 1]; targetLayer.Add(targetNeuron); } } else if (!IsInput(sourceNeuron) && !IsOutput(targetNeuron)) { int sourceHiddenPos = HiddenLayer.IndexOf(sourceLayer); int targetHiddenPos = HiddenLayer.IndexOf(targetLayer); if (targetHiddenPos < sourceHiddenPos) { targetLayer.Remove(targetNeuron); if (sourceHiddenPos == HiddenLayer.Count - 1) { List <Neuron> newLayer = new List <Neuron>(); newLayer.Add(targetNeuron); HiddenLayer.Add(newLayer); } else { List <Neuron> newLayer = HiddenLayer[sourceHiddenPos + 1]; newLayer.Add(targetNeuron); HiddenLayer.Add(newLayer); } if (targetLayer.Count == 0) { HiddenLayer.RemoveAt(targetHiddenPos); } } } sourceNeuron.AddConnection(targetNeuron, conn); } } ResetInnovation(); ResetNodeIndex(); }
public override void Fire() { Init(); //be sure to leave this here ModuleView mv = MainWindow.theNeuronArray.FindModuleByLabel("Robot"); if (mv == null) { return; } //a little bit of initializeaion if (previousPositionValues == null) { previousPositionValues = new float[mv.Height]; for (int i = 0; i < mv.Height; i++) { previousPositionValues[i] = mv.GetNeuronAt(0, i).LastCharge; } } //see if a pose request has been fired for (int i = 2; i < base.mv.NeuronCount; i++) { Neuron n = base.mv.GetNeuronAt(i); if (n == null || n.Label == "") { break; } //update internal labels in case they are changed poses[i - 2].name = base.mv.GetNeuronAt(i).Label; if (n.Fired()) { Pose thePose = poses[i - 2]; mv.GetNeuronAt("Timing").SetValue(thePose.timing); for (int j = 0; j < thePose.actuators.Count; j++) { Neuron n1 = mv.GetNeuronAt(thePose.actuators[j]); if (!thePose.isRelative) { n1.SetValue(thePose.positions[j]); } else { n1.SetValue(thePose.positions[j] + n1.LastCharge); } } } } //save a new pose if (GetNeuron("Save").Fired() || GetNeuron("Move").Fired()) { Neuron n = null; foreach (Neuron n1 in base.mv.Neurons) { if (n1.Label == "") { n = n1; break; } } if (n == null) { return; //TODO: add something here to expand the array? } Pose theNewPose = new Pose() { timing = mv.GetNeuronAt(0, 0).LastCharge, isRelative = GetNeuron("Move").Fired(), }; n.Label = theNewPose.isRelative ? "M" + poses.Count : "P" + poses.Count; for (int i = 1; i < previousPositionValues.Length; i++) { if (previousPositionValues[i] != mv.GetNeuronAt(0, i).LastCharge) { theNewPose.actuators.Add(mv.GetNeuronAt(0, i).Label); if (!theNewPose.isRelative) { theNewPose.positions.Add(mv.GetNeuronAt(0, i).LastCharge); } else { theNewPose.positions.Add(mv.GetNeuronAt(0, i).LastCharge - previousPositionValues[i]); } } } poses.Add(theNewPose); MainWindow.Update(); } }
//fill this method in with code which will execute once //when the module is added, when "initialize" is selected from the context menu, //or when the engine restart button is pressed public override void Initialize() { base.Initialize(); foreach (Neuron n in na.Neurons()) { n.DeleteAllSynapes(); } ModuleView na1 = theNeuronArray.FindAreaByLabel("KBOut"); if (na1 != null) { foreach (Neuron n in na1.Neurons()) { n.DeleteAllSynapes(); } } //since we are inserting at 0, these are in reverse order UKS.Insert(0, new Thing { Label = "Sleep" }); UKS.Insert(0, new Thing { Label = "Max" }); UKS.Insert(0, new Thing { Label = "Ref" }); UKS.Insert(0, new Thing { Label = "Parent" }); firstThing = 4; EventCount = 0; phraseCount = 0; //add connections from speakPhonemes to this module ModuleView naPhonemes = theNeuronArray.FindAreaByLabel("ModuleSpeakPhonemes"); if (naPhonemes != null) { Neuron n2 = GetNeuron(Labeled("SayRnd")); Neuron n3 = naPhonemes.GetNeuronAt("BabyTalk"); if (n2 != null && n3 != null) { n3.AddSynapse(n2.Id, 1); } Thing parent = Labeled("Vowel"); foreach (Neuron n in naPhonemes.Neurons()) { if (n.Label == "Conson.") { parent = Labeled("Consonant"); } if (n.Label.Length == 1) { string label = "spk" + n.Label; Thing pn = AddThing(label, new Thing[] { parent }); Neuron n1 = GetNeuron(pn, false); if (n1 != null) { n1.AddSynapse(n.Id, 1); } } } } UpdateNeuronLabels(); }
/// <summary> /// Adds a child element of type node for the given neuron </summary> /// <param name="neuron"> </param> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: private void addNode(final org.neuroph.core.Neuron neuron) private void addNode(Neuron neuron) { appendChild(new Node(neuron.Label)); }
//Randomize to, or load weights from, weigths.txt file private void Start() { inputAmount = p.gridSize * p.gridSize; //Load weights from file if it exists weightValues = LoadWeights(); if (randomize) { weightValues = new float[layer1NeuronAmount * inputAmount + layer2NeuronAmount * layer1NeuronAmount + 10 * layer2NeuronAmount]; } int lineIndex = 0; //Layer 1 Neuron[] layer1Neurons = new Neuron[layer1NeuronAmount]; for (int i = 0; i < layer1NeuronAmount; i++) { float[] weights = new float[inputAmount]; for (int w = 0; w < inputAmount; w++) { if (randomize) { weightValues[lineIndex] = UnityEngine.Random.Range(0f, 1f); } weights[w] = weightValues[lineIndex]; lineIndex++; } layer1Neurons[i] = new Neuron(0f, weights, 0f, 0f); } hiddenLayer1 = new Layer(layer1Neurons); //Layer 2 Neuron[] layer2Neurons = new Neuron[layer2NeuronAmount]; for (int i = 0; i < layer2NeuronAmount; i++) { float[] weights = new float[layer1NeuronAmount]; for (int w = 0; w < layer1NeuronAmount; w++) { if (randomize) { weightValues[lineIndex] = UnityEngine.Random.Range(0f, 1f); } weights[w] = weightValues[lineIndex]; lineIndex++; } layer2Neurons[i] = new Neuron(0f, weights, 0f, 0f); } hiddenLayer2 = new Layer(layer2Neurons); //Output Neuron[] output = new Neuron[10]; for (int i = 0; i < 10; i++) { float[] weights = new float[layer2NeuronAmount]; for (int w = 0; w < layer2NeuronAmount; w++) { if (randomize) { weightValues[lineIndex] = UnityEngine.Random.Range(0f, 1f); } weights[w] = weightValues[lineIndex]; lineIndex++; } output[i] = new Neuron(0f, weights, 0f, 0f); } outputLayer = new Layer(output); if (randomize) { SaveWeights(false); } if (trainNetwork) { trainingValues = new float[10 * imageAmount]; //Output * imageAmount labels = new int[imageAmount]; //Load labels string path = Application.dataPath + "/Scenes/NN/Training Data/labels.txt"; string text = File.ReadAllText(path); for (int i = 0; i < imageAmount; i++) { labels[i] = int.Parse(text[i].ToString()); } StartCoroutine(TrainNetwork()); } }
public NeuronConnection(Neuron outputNeuron, float weight) { this.OutputNeuron = outputNeuron; this.Weight = weight; }
public void SetConnections(Neuron inp, Neuron outp) { start = inp; end = outp; }
public void AddConnection(Neuron OutputNeuron, float Weight) { neuronConnections.Add(new NeuronConnection(OutputNeuron, Weight)); }
public abstract double ComputeScoreAtLocation(int x, int y, Neuron neuron);
public override double ComputeScoreAtLocation(int x, int y, Neuron neuron) { return(Network.NeuronDesirabilityMap[y, x]); }
private double Gauss(Neuron win, int it) { var distance = System.Math.Sqrt(System.Math.Pow(win.X - X, 2) + System.Math.Pow(win.Y - Y, 2)); return System.Math.Exp(-System.Math.Pow(distance, 2) / (System.Math.Pow(Strength(it), 2))); }
public Dendrite(Neuron originNeuron, double weight) { OriginNeuron = originNeuron; Weight = weight; }