void AddDendrite(Neuron neuron) { // See if we have to make a new dendrite foreach (var a in dendrites) { if (a.neuron == this) { return; } } foreach (var a in dendrites) { if (a.neuron == null) { a.neuron = this; return; } } Dendrite d = new Dendrite(); d.type = DendriteType.Excitatory; d.neuron = this; dendrites.Add(d); }
public void Process() { value = State; Profiler.BeginSample("Dendrites"); if (Dendrites0 != null) { var AnyDendriteLoose = Dendrites0.Any(d => Mathf.RoundToInt(d.Strength) == 0); var AllDendritesLoose = Dendrites0.All(d => Mathf.RoundToInt(d.Strength) == 0); foreach (var Dendrite in Dendrites0) { Dendrite.Process(this, AnyDendriteLoose, AllDendritesLoose); } } if (Dendrites1 != null) { var AnyDendriteLoose = Dendrites1.Any(d => Mathf.RoundToInt(d.Strength) == 0); var AllDendritesLoose = Dendrites1.All(d => Mathf.RoundToInt(d.Strength) == 0); foreach (var Dendrite in Dendrites1) { Dendrite.Process(this, AnyDendriteLoose, AllDendritesLoose); } } Profiler.EndSample(); Profiler.BeginSample("State rule"); State = ProcessStateRule(); Profiler.EndSample(); State = Mathf.Clamp(State, 0, 255); Profiler.BeginSample("Leakage"); State = ProcessLeakage(); Profiler.EndSample(); }
public void TestIfNeuronCalculatesCorrectOutput() { double weight1 = 0.5; double weight2 = 2.5; Dendrite dendrite1 = new Dendrite(weight1); Dendrite dendrite2 = new Dendrite(weight2); // Give input to inputneurons InputNeuron n1 = new InputNeuron(); InputNeuron n2 = new InputNeuron(); n1.Output = 5; n2.Output = 2; dendrite1.CalculateValue(n1.Output); dendrite2.CalculateValue(n2.Output); // Make hidden neuron, assign dendrites HiddenNeuron n3 = new HiddenNeuron(); n3.Dendrites.Add(dendrite1); n3.Dendrites.Add(dendrite2); n3.CalculateOutput(); double expectedValue = 7.5; double actualValue = n3.Output; Assert.AreEqual(expectedValue, actualValue); }
public void CanConnectNeurons() { var in1 = new Input(); var in2 = new Input(); var in3 = new Input(); var dendrite1 = new Dendrite(); dendrite1.SetConnection(in1); var dendrite2 = new Dendrite(); dendrite2.SetConnection(in2); var dendrite3 = new Dendrite(); dendrite3.SetConnection(in3); var neuron = new StepNeuron(); neuron.Connect(dendrite1); neuron.Connect(dendrite2); neuron.Connect(dendrite3); var dendrite4 = new Dendrite(); dendrite4.SetConnection(neuron); Assert.AreEqual(0d, neuron.Output()); }
public void DendriteMultipliesInputByWeight() { var input = new Input(1.1); var dendrite = new Dendrite(10); dendrite.SetConnection(input); Assert.AreEqual(11, dendrite.Output()); }
private Dendrite CreateDendrite() { var dendrite = new Dendrite { Weight = new Random().NextDouble() }; return(dendrite); }
public void CalculateValue_CalledWithOutput_HasCorrectValue() { double weight = 0.5; double output = 4; Dendrite d = new Dendrite(weight); d.CalculateValue(output); double expectedResult = 2; double actualResult = d.Value; Assert.AreEqual(expectedResult, actualResult); }
private void Write(string fname, Dendrite dendrite) { sbmain = new StringBuilder(); tmp = new StringBuilder(); tmpsb = new StringBuilder(); // VTKフォーマットの仕様書はこちら http://www.vtk.org/VTK/img/file-formats.pdf // このあたりの情報良さそう http://www.cacr.caltech.edu/~slombey/asci/vtk/vtk_formats.simple.html sbmain.AppendLine("# vtk DataFile Version 2.0"); // おまじない sbmain.AppendLine("Colored Dendrite"); // タイトル sbmain.AppendLine("ASCII"); // テキストで記述する (VTKはバイナリでも記述できる) sbmain.AppendLine("DATASET POLYDATA"); // lineで記述したい // 点データの記述 sbmain.AppendLine("POINTS " + dendrite.GetDendriteNodeNum().ToString() + " float"); OutputVTKPointsRec(dendrite.root, null); // 線データの記述 nlines = 0; // 線の本数 nlpoint = 0; // 1本の線を構成する点の個数 ndata = 0; // 書き込むデータの個数 flag = false; // 書き込みの途中であるか? OutputVTKLinesRec(dendrite.root, null); sbmain.AppendLine("LINES " + nlines.ToString() + " " + ndata.ToString()); sbmain.Append(tmpsb.ToString()); sbmain.AppendLine("POINT_DATA " + dendrite.GetDendriteNodeNum().ToString()); sbmain.AppendLine("SCALARS distance float 1"); sbmain.AppendLine("LOOKUP_TABLE default"); OutputVTKDistanceRec(dendrite.root, null); sbmain.AppendLine("SCALARS radius float 1"); sbmain.AppendLine("LOOKUP_TABLE radius"); OutputVTKRadiusRec(dendrite.root, null); sbmain.AppendLine("SCALARS functionaldistance float 1"); sbmain.AppendLine("LOOKUP_TABLE functionaldistance"); OutputVTKFDistanceRec(dendrite.root, null); sbmain.AppendLine("SCALARS degree int 1"); sbmain.AppendLine("LOOKUP_TABLE degree"); OutputVTKDegreeRec(dendrite.root, null); using (StreamWriter sw = new StreamWriter(Path.ChangeExtension(fname, ".vtk"))) { sw.Write(sbmain.ToString()); } }
public FTPerceptron(INeuronFactory neuronFactory) : base(neuronFactory) { // this is the unit input which will handle any bias var simpleInput = new Input(1.0); // dendrite has weight var dendrite = new Dendrite(); // connect to the input dendrite.SetConnection(simpleInput); // connect to the neuron _neuron.Connect(dendrite); }
public void AddInput(Input simpleInput) { // dendrite has weight var dendrite = new Dendrite(); // keep ref for classifying _inputs.Add(simpleInput); // connect to the input dendrite.SetConnection(simpleInput); // connect to the neuron _neuron.Connect(dendrite); }
public void NeuronFiresRelativeToThreshold(double val, double weight, double threshold, double output) { var input1 = new Input(val); var dendrite1 = new Dendrite(weight); dendrite1.SetConnection(input1); var input2 = new Input(val); var dendrite2 = new Dendrite(weight); dendrite2.SetConnection(input2); var neuron = new StepNeuron(threshold); neuron.Connect(dendrite1); neuron.Connect(dendrite2); Assert.AreEqual(output, neuron.Output()); }
public void TrainNatural(List <double[]> inputTrainSet, List <double[]> outputTarget, int iterationsNumber) { for (int i = 0; i < iterationsNumber; i++) { _network.ForwardPropagate(); if (i % 10000 == 0) { Console.WriteLine(_network.GetError(inputTrainSet, outputTarget).ToString(CultureInfo.InvariantCulture)); } Dendrite dendrite = _network.GetRandomLayer().GetRandomNeuron().GetRandomDendrite(); double saveDendriteWeight = dendrite.Weight; //save dendrite weight if (outputTarget[1].Length == _network.GetLastLayer().Neurons.Count) //outputTarget lengh must be the same number as the number of neurons in the last layer { TryMoreLess(inputTrainSet, outputTarget, dendrite, saveDendriteWeight); } } _network.ForwardPropagate(); Console.WriteLine(_network.GetError(inputTrainSet, outputTarget).ToString(CultureInfo.InvariantCulture)); }
private void RemoveDendriteAsync(Dendrite dendrite) { MainViewModel.ExecuteCommand( this, async() => { await neuronApplicationService.RemoveTerminalsFromNeuron( dendrite.Id, new Terminal[] { new Terminal { TargetId = this.neuronId } }, dendrite.Version ); return(true); }, async() => await this.LoadAsync(), this.messageService, this.DialogService, "Extension removed.", $"An error occurred while removing Extension '{dendrite.Data}'" ); }
public override double Calculate() { return(ScalingFactor * Dendrite.CalculateEffective()); }
private async Task NavigateDendriteAsync(Dendrite dendrite) { await NavigationService.NavigateToAsync <MainViewModel>(new NeuronParameter { NeuronId = dendrite.Id }); }
public static void SaveToVTKLegacy(string fname, Dendrite dendrite) { var writer = new VtkWriter(); writer.Write(fname, dendrite); }
private void TryMoreLess(List <double[]> inputTrainSet, List <double[]> outputTarget, Dendrite dendrite, double saveDendriteWeight) { double networkError = _network.GetError(inputTrainSet, outputTarget); //try more dendrite.Weight = saveDendriteWeight + weightVariationRate; _network.ForwardPropagate(); double tryMoreNetworkError = _network.GetError(inputTrainSet, outputTarget); //try less dendrite.Weight = saveDendriteWeight - weightVariationRate; _network.ForwardPropagate(); double tryLessNetworkError = _network.GetError(inputTrainSet, outputTarget); if (tryLessNetworkError < tryMoreNetworkError) //Less is better { if (tryLessNetworkError < networkError) //Improve! { dendrite.Weight = saveDendriteWeight - weightVariationRate; //weightVariationRate = weightVariationRate * 0.9; } else //F**k this shit! { dendrite.Weight = saveDendriteWeight; //weightVariationRate = weightVariationRate*1.1; } } else //More is better { if (tryMoreNetworkError < networkError) //Improve! { dendrite.Weight = saveDendriteWeight + weightVariationRate; //weightVariationRate = weightVariationRate * 0.9; } else //F**k this shit! { dendrite.Weight = saveDendriteWeight; //weightVariationRate = weightVariationRate * 1.1; } } }
//СОЕДИНЕНИЕ С ИСТОЧНИКАМИ СИГНАЛОВ //пересоединяет нейрон с указанными источниками сигнала public void Reconnect(Signal[] signals_) { Dendrites = new Dendrite[signals_.Length]; var weightMagnitude = 10f / (signals_.Length + 1); //соединяем с небольшими случайными весами for (int i = 0, c = signals_.Length; i < c; i++) { Dendrites[i] = new Dendrite() { Signal = signals_[i], Weight = GetRandomWeight(weightMagnitude) }; } BiasDendrite = new Dendrite() { Signal = NNHelper.UnitSignal, Weight = GetRandomWeight(weightMagnitude) }; }
public void Fire() { Axon.Set(Dendrite.Get() * Weight); }
void UpdateLines(bool forceUpdate, bool onValidate = false) { if (lines == null) { lines = new List <Line>(); } LineRenderer[] lr = GetComponentsInChildren <LineRenderer>(); foreach (var l1 in lr) { bool found = false; foreach (var l2 in lines) { if (l2.lineRenderer == l1) { found = true; break; } } if (!found) { Line l = new Line(); l.lineRenderer = l1; l.prevPos = new Vector3(-999999.0f, 0, 0); lines.Add(l); } } if (Brain.Get() == null) { return; } LineRenderer prefab = Brain.Get().lineRendererPrefab; if (prefab == null) { return; } int nSegs = 20; for (int i = lines.Count; i < dendrites.Count; i++) { var l = new Line(); l.lineRenderer = Instantiate(prefab, transform); l.lineRenderer.transform.position = transform.position + Vector3.left * radius; l.lineRenderer.positionCount = nSegs; l.prevPos = new Vector3(-999999.0f, 0, 0); lines.Add(l); } for (int i = dendrites.Count; i < lines.Count; i++) { if (onValidate) { Destroy(lines[i].lineRenderer); } else { DestroyImmediate(lines[i].lineRenderer); } } lines.RemoveAll((x) => x.lineRenderer == null); float tangentOffset = 2f; float aInc = 20.0f; float angle = 180.0f - (dendrites.Count - 1) * 0.5f * aInc; for (int i = 0; i < dendrites.Count; i++) { Dendrite d = dendrites[i]; if (i >= lines.Count) { break; } if (d.neuron == null) { continue; } Vector3 p1 = d.neuron.transform.position + Vector3.right * d.neuron.radius; Vector3 p2 = p1 + Vector3.right * tangentOffset; Vector3 dir = new Vector3(Mathf.Cos(Mathf.Deg2Rad * angle), Mathf.Sin(Mathf.Deg2Rad * angle), 0); Vector3 p4 = transform.position + dir * radius; Vector3 p3 = p4 + dir * tangentOffset; if (d.neuron == this) { p2 += Vector3.up * tangentOffset; p3 += Vector3.up * tangentOffset; } angle += aInc; if ((Vector3.Distance(lines[i].prevPos, p4) > 0.0005f) || (forceUpdate)) { for (int j = 0; j < nSegs; j++) { float t = (float)j / (float)(nSegs - 1); lines[i].lineRenderer.SetPosition(j, EvaluateBezier(t, p1, p2, p3, p4)); } lines[i].prevPos = p1; } lines[i].lineRenderer.colorGradient = (d.type == DendriteType.Excitatory) ? (Brain.Get().excitatoryGradient) : (Brain.Get().inhibitoryGradient); } }
public MultiLayerPerceptron BuildMultiLayerPerceptron() { // 4 inputs, 3 hidden neurons, 2 outputs, fully connected var factory = new SigmoidNeuronFactory(); var network = new MultiLayerPerceptron(factory); // input values var i1 = new Input(); var i2 = new Input(); var i3 = new Input(); var i4 = new Input(); // hidden layer var hn1 = factory.Create(); var hn2 = factory.Create(); var hn3 = factory.Create(); //var hiddenLayer = new Layer(new List<INeuron> { hn1, hn2, hn3 }); // output neurons var on1 = factory.Create(); var on2 = factory.Create(); //var outputLayer = new Layer(new List<INeuron> {on1, on2}); network.AddInput(i1); network.AddInput(i2); network.AddInput(i3); network.AddInput(i4); // hidden layer network.AddHiddenNeuron(hn1); network.AddHiddenNeuron(hn2); network.AddHiddenNeuron(hn3); // output layer network.AddOutput(on1); network.AddOutput(on2); // hidden connections var dh11 = new Dendrite(learningRate: 1); var dh12 = new Dendrite(learningRate: 1); var dh21 = new Dendrite(learningRate: 1); var dh22 = new Dendrite(learningRate: 1); var dh31 = new Dendrite(learningRate: 1); var dh32 = new Dendrite(learningRate: 1); dh11.SetConnection(hn1); dh12.SetConnection(hn1); dh21.SetConnection(hn2); dh22.SetConnection(hn2); dh31.SetConnection(hn3); dh32.SetConnection(hn3); on1.Connect(dh11); on1.Connect(dh21); on1.Connect(dh31); on2.Connect(dh12); on2.Connect(dh22); on2.Connect(dh32); // input dendrite connections var d11 = new Dendrite(learningRate: 1); var d12 = new Dendrite(learningRate: 1); var d13 = new Dendrite(learningRate: 1); var d21 = new Dendrite(learningRate: 1); var d22 = new Dendrite(learningRate: 1); var d23 = new Dendrite(learningRate: 1); var d31 = new Dendrite(learningRate: 1); var d32 = new Dendrite(learningRate: 1); var d33 = new Dendrite(learningRate: 1); d11.SetConnection(i1); d12.SetConnection(i1); d13.SetConnection(i1); d21.SetConnection(i2); d22.SetConnection(i2); d23.SetConnection(i2); d31.SetConnection(i3); d32.SetConnection(i3); d33.SetConnection(i3); hn1.Connect(d11); hn1.Connect(d21); hn1.Connect(d31); hn2.Connect(d12); hn2.Connect(d22); hn2.Connect(d32); hn3.Connect(d13); hn3.Connect(d23); hn3.Connect(d33); return(network); }
void PaintNetwork(object sender, PaintEventArgs e) { if (network == null) { return; } Graphics panel = networkPanel.CreateGraphics(); Pen p = new Pen(Color.Black); SolidBrush sb = new SolidBrush(Color.Black); float layerSpacing = networkPanel.Width / network.LayerCount; float prevNeuronSpacing = 0; int prevNeuronCount = 0; int prevNeuronPerSpace = 1; for (int i = 0; i < network.LayerCount; i++) { Layer layer = network.Layers[i]; float x = (i + 0.5f) * layerSpacing; int neuronCount = layer.NeuronCount > 20 ? 20 : layer.NeuronCount; float neuronSpacing = networkPanel.Height / neuronCount; for (int j = 0; j < neuronCount; j++) { Neuron n = layer.Neurons[j]; float y = (j + 0.5f) * neuronSpacing; panel.DrawEllipse(p, x - neuronWidth / 2f, y - neuronHeight / 2f, neuronWidth, neuronHeight); panel.FillEllipse(sb, x - neuronWidth / 2f, y - neuronHeight / 2f, neuronWidth, neuronHeight); if (i > 0) { int l = 0; double weight = 0; for (int k = 0; k < n.Dendrites.Count; k++) { l++; Dendrite d = n.Dendrites[k]; Pen dendritePen; weight += d.Weight; if (l >= prevNeuronPerSpace) { weight /= l; if (weight == 0) { return; } if (weight > 0) { dendritePen = new Pen(Color.Black, (float)NeuralNetUtils.Sigmoid(weight) * 3f); } else { dendritePen = new Pen(Color.Red, (float)NeuralNetUtils.Sigmoid(-weight) * 3f); } int neuronNum = (int)((float)k / network.Layers[i - 1].NeuronCount * prevNeuronCount); panel.DrawLine(dendritePen, new Point((int)(layerSpacing * (i - 0.5f)), (int)(prevNeuronSpacing * (neuronNum + 0.5f))), new Point((int)x, (int)y)); weight = 0; l = 0; } } } } prevNeuronPerSpace = (int)(layer.NeuronCount / neuronSpacing); prevNeuronCount = neuronCount; prevNeuronSpacing = neuronSpacing; } }