Пример #1
0
        private void DrawLayerNeurons(bool fullState, NetworkDataModel model, LayerDataModel layer)
        {
            double threshold = model.Layers.First() == layer ? model.InputThreshold : 0;

            Range.ForEach(layer.Neurons, neuron =>
            {
                if (fullState || (neuron.IsBias || neuron.Activation > threshold))
                {
                    using (var brush = Tools.Draw.GetBrush(neuron.Activation))
                    {
                        if (neuron.IsBias)
                        {
                            G.FillEllipse(Brushes.Orange,
                                          LayerX(model, layer) - BIAS_RADIUS,
                                          VERTICAL_OFFSET + VerticalShift(model, layer) + neuron.Id * VerticalDistance(layer.Height) - BIAS_RADIUS,
                                          BIAS_SIZE, BIAS_SIZE);
                        }

                        G.FillEllipse(brush,
                                      LayerX(model, layer) - NEURON_RADIUS,
                                      VERTICAL_OFFSET + VerticalShift(model, layer) + neuron.Id * VerticalDistance(layer.Height) - NEURON_RADIUS,
                                      NEURON_SIZE, NEURON_SIZE);
                    }
                }
            });
        }
Пример #2
0
        public static void GaussHeEtAl(NetworkDataModel network, double?a)
        {
            // He initialization works better for layers with ReLu(s) activation.

            if (!a.HasValue)
            {
                a = 1;
            }

            foreach (var layer in network.Layers)
            {
                foreach (var neuron in layer.Neurons)
                {
                    foreach (var weight in neuron.Weights)
                    {
                        if (layer.Previous == null)
                        {
                            weight.Weight = Rand.GaussianRand.NextGaussian(0, a.Value);
                        }
                        else
                        {
                            weight.Weight = Rand.GaussianRand.NextGaussian(0, a.Value) * Math.Sqrt(2 / (double)layer.Previous.Neurons.Count);
                        }
                    }
                }
            }
        }
Пример #3
0
        private void Draw(bool fullState, NetworkDataModel model, bool isOnlyWeights, bool isOnlyChangedWeights, bool isHighlightChangedWeights)
        {
            if (model == null)
            {
                CtlPresenter.Clear();
                return;
            }

            CtlPresenter.Clear();

            //lock (Main.ApplyChangesLocker)
            {
                if (model.Layers.Count > 0)
                {
                    foreach (var layer in model.Layers)
                    {
                        if (layer == model.Layers.Last())
                        {
                            break;
                        }

                        DrawLayersLinks(fullState, model, layer, layer.Next, isOnlyWeights, isOnlyChangedWeights, isHighlightChangedWeights);
                    }

                    foreach (var layer in model.Layers)
                    {
                        DrawLayerNeurons(fullState, model, layer);
                    }
                }
            }

            CtlPresenter.Update();
        }
Пример #4
0
        public void SetInputDataAndDraw(NetworkDataModel model)
        {
            Threshold = model.InputThreshold;
            var count = model.Layers[0].Neurons.Count(n => !n.IsBias);

            if (Data == null || Data.Length != count)
            {
                Data = new double[count];
                Stat = new double[Data.Length];
            }
            else
            {
                Array.Clear(Data, 0, Data.Length);
            }

            var neuron = model.Layers[0].Neurons[0];

            while (neuron != null)
            {
                if (!neuron.IsBias)
                {
                    Data[neuron.Id] = neuron.Activation;
                }
                neuron = neuron.Next;
            }

            Rearrange(PointsCount);
        }
Пример #5
0
            public void Do(NetworkDataModel model)
            {
                int bound = 10;

                int number = Rand.Flat.Next(bound + 1);

                if (number == 0)
                {
                    return;
                }

                if (!_arrays.ContainsKey(bound))
                {
                    _arrays.Add(bound, new double[bound]);
                }

                for (int i = 0; i < _arrays[bound].Length; ++i)
                {
                    _arrays[bound][i] = i < number ? model.InputInitial1 : model.InputInitial0;
                }

                var shaffle = _arrays[bound].OrderBy(a => Rand.Flat.Next()).ToArray();

                int k = 0;

                Range.ForEach(model.Layers.First().Neurons.Where(n => !n.IsBias), n => n.Activation = shaffle[k++]);
            }
Пример #6
0
 public void SetInputDataAndDraw(NetworkDataModel model)
 {
     Threshold = model.InputThreshold;
     Data      = new double[model.Layers.First().Neurons.Where(n => !n.IsBias).Count()];
     Range.ForEach(model.Layers.First().Neurons.Where(n => !n.IsBias), neuron => Data[neuron.Id] = neuron.Activation);
     Rearrange(PointsCount);
 }
Пример #7
0
        public static void He_et_al(NetworkDataModel network, double?a)
        {
            if (!a.HasValue)
            {
                a = 1;
            }

            foreach (var layer in network.Layers)
            {
                foreach (var neuron in layer.Neurons)
                {
                    foreach (var weight in neuron.Weights)
                    {
                        if (layer.Previous == null)
                        {
                            weight.Weight = Rand.GetFlatRandom();
                        }
                        else
                        {
                            weight.Weight = a.Value * Rand.GetFlatRandom() * Math.Sqrt(2 / layer.Previous.Neurons.Count);
                        }
                    }
                }
            }
        }
Пример #8
0
        public NetworkGPU(NetworkDataModel model)
        {
            var layers = GPU.Instance.Accelerator.Allocate <LayerGPU>(model.Layers.Count);

            for (int i = 0; i < model.Layers.Count; ++i)
            {
                layers.CopyFrom(new LayerGPU(model.Layers[i]), i);
            }
            Layers = layers.View;
        }
Пример #9
0
            public double Do(NetworkDataModel model)
            {
                double s      = 0;
                var    neuron = model.Layers.Last().Neurons[0];

                while (neuron != null)
                {
                    s     += Math.Pow(neuron.Target - neuron.Activation, 2);
                    neuron = neuron.Next;
                }
                return(s);
            }
Пример #10
0
 public static void AbsGaussRandom(NetworkDataModel network, double?a)
 {
     foreach (var layer in network.Layers)
     {
         foreach (var neuron in layer.Neurons)
         {
             foreach (var weight in neuron.Weights)
             {
                 weight.Weight = Math.Abs(Rand.GaussianRand.NextGaussian(0, a.HasValue ? a.Value : 1));
             }
         }
     }
 }
Пример #11
0
 public static void FlatRandom(NetworkDataModel network, double?a)
 {
     foreach (var layer in network.Layers)
     {
         foreach (var neuron in layer.Neurons)
         {
             foreach (var weight in neuron.Weights)
             {
                 weight.Weight = Rand.GetFlatRandom(a.HasValue ? a.Value : 1);
             }
         }
     }
 }
Пример #12
0
 public void SetInputStat(NetworkDataModel model)
 {
     if (Stat != null)
     {
         int i      = 0;
         var neuron = model.Layers[0].Neurons[0];
         while (neuron != null)
         {
             if (!neuron.IsBias)
             {
                 Stat[i] += neuron.Activation > Threshold ? neuron.Activation : 0;
             }
             ++i;
             neuron = neuron.Next;
         }
     }
 }
Пример #13
0
        public void FeedForward(NetworkDataModel model)
        {
            for (int i = 0; i < model.Layers.Count - 1; ++i)
            {
                var neurons     = model.Layers[i].GetNeurons();
                var neuronsNext = model.Layers[i + 1].GetNeurons();
                KernelProduct(neuronsNext.Height, neurons, neuronsNext);
                Accelerator.Synchronize();
                for (int n = 0; n < neuronsNext.Height; ++n)
                {
                    neuronsNext.CopyTo(out double activation, new Index2(0, n));

                    model.Layers[i + 1].Neurons[n].Activation = model.Layers[i + 1].Neurons[n].ActivationFunction.Do(activation, model.Layers[i + 1].Neurons[n].ActivationFuncParamA);
                    neuronsNext.CopyFrom(model.Layers[i + 1].Neurons[n].Activation, new Index2(0, n));
                }
            }
        }
Пример #14
0
        public static void Centered(NetworkDataModel network, double?a)
        {
            if (!a.HasValue)
            {
                a = 1;
            }

            foreach (var layer in network.Layers)
            {
                foreach (var neuron in layer.Neurons)
                {
                    foreach (var weight in neuron.Weights)
                    {
                        weight.Weight = -a.Value / 2 + a.Value * Rand.GetFlatRandom();
                    }
                }
            }
        }
Пример #15
0
        public static void Constant(NetworkDataModel network, double?a)
        {
            if (!a.HasValue)
            {
                a = 0;
            }

            foreach (var layer in network.Layers)
            {
                foreach (var neuron in layer.Neurons)
                {
                    foreach (var weight in neuron.Weights)
                    {
                        weight.Weight = a.Value;
                    }
                }
            }
        }
Пример #16
0
        public static void WaveProgress(NetworkDataModel network, double?a)
        {
            if (!a.HasValue)
            {
                a = 6;
            }

            foreach (var layer in network.Layers)
            {
                foreach (var neuron in layer.Neurons)
                {
                    foreach (var weight in neuron.Weights)
                    {
                        weight.Weight = InitializeMode.Centered(layer.Id + 1) * Math.Cos(weight.Id / Math.PI) * Math.Cos(neuron.Id / Math.PI);
                    }
                }
            }
        }
Пример #17
0
        private void DrawLayersLinks(bool fullState, NetworkDataModel model, LayerDataModel layer1, LayerDataModel layer2)
        {
            double threshold = model.Layers.First() == layer1 ? model.InputThreshold : 0;

            Range.ForEach(layer1.Neurons, layer2.Neurons, (neuron1, neuron2) =>
            {
                if (!neuron2.IsBias || (neuron1.IsBias && neuron2.IsBiasConnected))
                {
                    if (fullState || ((neuron1.IsBias || neuron1.Activation > threshold) && neuron1.AxW(neuron2) != 0))
                    {
                        using (var pen = Tools.Draw.GetPen(neuron1.AxW(neuron2), 1))
                        {
                            G.DrawLine(pen,
                                       LayerX(model, layer1), VERTICAL_OFFSET + VerticalShift(model, layer1) + neuron1.Id * VerticalDistance(layer1.Height),
                                       LayerX(model, layer2), VERTICAL_OFFSET + VerticalShift(model, layer2) + neuron2.Id * VerticalDistance(layer2.Height));
                        }
                    }
                }
            });
        }
Пример #18
0
        private void DrawLayerNeurons(bool fullState, NetworkDataModel model, LayerDataModel layer)
        {
            double threshold = model.Layers[0] == layer ? model.InputThreshold : 0;

            var biasColor = Tools.Draw.GetPen(Colors.Orange);

            NeuronDataModel prevNeuron = null;

            foreach (var neuron in layer.Neurons)
            {
                if (fullState || neuron.IsBias || neuron.Activation > threshold || layer.Id > 0)
                {
                    if (!Coordinator.ContainsKey(neuron))
                    {
                        Coordinator.Add(neuron, Points.Get(LayerX(model, layer), TOP_OFFSET + VerticalShift(model, layer) + neuron.Id * VerticalDistance(layer.Height)));
                    }

                    // Skip intersected neurons on first layer to improove performance.
                    if (!fullState && !neuron.IsBias && model.Layers[0] == layer && prevNeuron != null)
                    {
                        if (Coordinator[neuron].Y - Coordinator[prevNeuron].Y < NEURON_SIZE)
                        {
                            continue;
                        }
                    }
                    prevNeuron = neuron;

                    var pen   = Tools.Draw.GetPen(neuron.Activation);
                    var brush = pen.Brush;

                    if (neuron.IsBias)
                    {
                        CtlPresenter.DrawEllipse(Brushes.Orange, biasColor, Coordinator[neuron], BIAS_RADIUS, BIAS_RADIUS);
                    }

                    CtlPresenter.DrawEllipse(brush, pen, Coordinator[neuron], NEURON_RADIUS, NEURON_RADIUS);
                }
            }
        }
Пример #19
0
        public void Draw(List <NetworkDataModel> models, NetworkDataModel selectedModel)
        {
            StartRender();
            Clear();

            DrawPlotter();

            foreach (var model in models)
            {
                Vanish(model.DynamicStatistic.PercentData, GetPointPercentData);
                Vanish(model.DynamicStatistic.CostData, GetPointCostData);

                DrawData(model.DynamicStatistic.PercentData, model.Color, GetPointPercentData, false);
                DrawData(model.DynamicStatistic.CostData, Color.FromArgb(80, model.Color), GetPointCostData, true);
            }

            if (selectedModel != null)
            {
                DrawLabel(selectedModel.DynamicStatistic.PercentData, selectedModel.Color);
            }

            CtlBox.Invalidate();
        }
Пример #20
0
        private void Draw(bool fullState, NetworkDataModel model)
        {
            StartRender();
            Clear();

            if (model == null)
            {
                CtlBox.Invalidate();
                return;
            }

            lock (Main.ApplyChangesLocker)
            {
                if (model.Layers.Count > 0)
                {
                    Range.ForEachTrimEnd(model.Layers, -1, layer => DrawLayersLinks(fullState, model, layer, layer.Next));
                }

                Range.ForEach(model.Layers, layer => DrawLayerNeurons(fullState, model, layer));
            }

            CtlBox.Invalidate();
        }
        public void Draw(ListX <NetworkDataModel> models, NetworkDataModel selectedModel)
        {
            if (IsBaseRedrawNeeded)
            {
                DrawPlotter();
                IsBaseRedrawNeeded = false;
            }

            CtlPresenter.Clear();

            if (models == null)
            {
                return;
            }

            var model = models[0];

            while (model != null)
            {
                if (!model.IsEnabled)
                {
                    continue;
                }

                DrawData(model.DynamicStatistics.CopyForRender.PercentData, Tools.Draw.GetColor(220, model.Color), GetPointPercentData, false);
                DrawData(model.DynamicStatistics.CopyForRender.CostData, Tools.Draw.GetColor(150, model.Color), GetPointCostData, true);

                model = model.Next;
            }

            if (selectedModel != null && selectedModel.DynamicStatistics.PercentData.Count > 0)
            {
                DrawLabel(selectedModel.DynamicStatistics.PercentData, selectedModel.Color);
            }

            CtlPresenter.Update();
        }
Пример #22
0
        public void Draw(List <NetworkDataModel> models, NetworkDataModel selectedModel)
        {
            int size = 9;

            StartRender();
            Clear();

            long goodMax    = 1;
            long badMax     = 1;
            long axisOffset = 12;
            long bound      = 60;

            var matrix = selectedModel.ErrorMatrix;

            for (int y = 0; y < matrix.Output.Length; ++y)
            {
                for (int x = 0; x < matrix.Input.Length; ++x)
                {
                    if (x == y)
                    {
                        goodMax = Math.Max(goodMax, matrix.Matrix[x, y]);
                    }
                    else
                    {
                        badMax = Math.Max(badMax, matrix.Matrix[x, y]);
                    }
                }
            }

            for (int y = 0; y < matrix.Output.Length; ++y)
            {
                for (int x = 0; x < matrix.Input.Length; ++x)
                {
                    var value = (double)matrix.Matrix[y, x] / (double)(x == y ? goodMax : badMax);
                    using (var brush = new SolidBrush(Tools.Draw.GetColorDradient(Color.LightGray, x == y ? Color.Green : Color.Red, 255, value)))
                    {
                        G.FillRectangle(brush, axisOffset + x * size, axisOffset + y * size, size, size);
                        G.DrawRectangle(Pens.Silver, axisOffset + x * size, axisOffset + y * size, size, size);
                    }
                }
            }

            long outputMax = Math.Max(matrix.Output.Max(), 1);

            for (int x = 0; x < matrix.Output.Length; ++x)
            {
                using (var brush = new SolidBrush(Tools.Draw.GetColorDradient(Color.White, matrix.Output[x] > matrix.Input[x] ? Color.Red : matrix.Output[x] < matrix.Input[x] ? Color.Blue : Color.Green, 100, (double)matrix.Output[x] / (double)outputMax)))
                {
                    G.FillRectangle(brush, axisOffset + x * size, 2 + axisOffset + (matrix.Input.Length) * size, size, (int)(bound * (double)matrix.Output[x] / (double)outputMax));
                    G.DrawRectangle(Pens.Silver, axisOffset + x * size, 2 + axisOffset + (matrix.Input.Length) * size, size, (int)(bound * (double)matrix.Output[x] / (double)outputMax));
                }
            }

            long inputMax = Math.Max(matrix.Input.Max(), 1);

            for (int y = 0; y < matrix.Input.Length; ++y)
            {
                using (var brush = new SolidBrush(Tools.Draw.GetColorDradient(Color.White, Color.Green, 100, (double)matrix.Input[y] / (double)inputMax)))
                {
                    G.FillRectangle(brush, 2 + axisOffset + (matrix.Output.Length) * size, axisOffset + y * size, (int)(bound * (double)matrix.Input[y] / (double)inputMax), size);
                    G.DrawRectangle(Pens.Silver, 2 + axisOffset + (matrix.Output.Length) * size, axisOffset + y * size, (int)(bound * (double)matrix.Input[y] / (double)inputMax), size);
                }
            }

            G.DrawString("Output", Font, Brushes.Black, axisOffset, axisOffset - Font.Height - 1);
            G.RotateTransform(-90);
            G.DrawString("Input", Font, Brushes.Black, -axisOffset - (matrix.Output.Length) * size, axisOffset - Font.Height - 1);
            G.RotateTransform(90);

            G.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);
            CtlBox.Invalidate();
        }
Пример #23
0
        public NetworkDataModel CreateNetworkDataModel()
        {
            var model = new NetworkDataModel(Id, GetLayersSize())
            {
                Color            = CtlColor.BackColor,
                RandomizeMode    = Randomizer,
                RandomizerParamA = RandomizerParamA,
                LearningRate     = LearningRate,
                InputInitial0    = ActivationFunction.Helper.GetInstance(InputLayer.ActivationFunc).Do(InputLayer.Initial0, null),
                InputInitial1    = ActivationFunction.Helper.GetInstance(InputLayer.ActivationFunc).Do(InputLayer.Initial1, null)
            };

            model.Activate();

            var layers = GetLayersControls();

            for (int ln = 0; ln < layers.Count; ++ln)
            {
                model.Layers[ln].VisualId = layers[ln].Id;

                var neurons = layers[ln].GetNeuronsControls();
                for (int nn = 0; nn < neurons.Count; ++nn)
                {
                    var neuronModel = model.Layers[ln].Neurons[nn];
                    neuronModel.VisualId        = neurons[nn].Id;
                    neuronModel.IsBias          = neurons[nn].IsBias;
                    neuronModel.IsBiasConnected = neurons[nn].IsBiasConnected;

                    neuronModel.ActivationFunction   = ActivationFunction.Helper.GetInstance(neurons[nn].ActivationFunc);
                    neuronModel.ActivationDerivative = ActivationDerivative.Helper.GetInstance(neurons[nn].ActivationFunc);
                    neuronModel.ActivationFuncParamA = neurons[nn].ActivationFuncParamA;

                    neuronModel.WeightsInitializer       = neurons[nn].WeightsInitializer;
                    neuronModel.WeightsInitializerParamA = neurons[nn].WeightsInitializerParamA;
                    double initValue = InitializeMode.Helper.Invoke(neurons[nn].WeightsInitializer, neurons[nn].WeightsInitializerParamA);
                    if (!InitializeMode.Helper.IsSkipValue(initValue))
                    {
                        foreach (var weight in neuronModel.Weights)
                        {
                            weight.Weight = InitializeMode.Helper.Invoke(neurons[nn].WeightsInitializer, neurons[nn].WeightsInitializerParamA);
                        }
                    }

                    if (neuronModel.IsBias)
                    {
                        neuronModel.ActivationInitializer       = neurons[nn].ActivationInitializer;
                        neuronModel.ActivationInitializerParamA = neurons[nn].ActivationInitializerParamA;
                        initValue = InitializeMode.Helper.Invoke(neurons[nn].ActivationInitializer, neurons[nn].ActivationInitializerParamA);
                        if (!InitializeMode.Helper.IsSkipValue(initValue))
                        {
                            neuronModel.Activation = initValue;
                        }
                    }
                }
            }

            model.Layers.Last().VisualId = Const.OutputLayerId;
            {
                var neurons = OutputLayer.GetNeuronsControls();
                for (int i = 0; i < neurons.Count; ++i)
                {
                    model.Layers.Last().Neurons[i].VisualId = neurons[i].Id;
                }
            }

            return(model);
        }
Пример #24
0
        public NetworkDataModel CreateNetworkDataModel(INetworkTask task, bool isCopy)
        {
            ErrorMatrix em1 = null;

            if (task != null)
            {
                em1 = new ErrorMatrix(task.GetClasses());
                var em2 = new ErrorMatrix(task.GetClasses());
                em1.Next = em2;
                em2.Next = em1;
            }

            var model = new NetworkDataModel(Id, GetLayersSize())
            {
                ErrorMatrix               = em1,
                Classes                   = task?.GetClasses(),
                IsEnabled                 = CtlIsNetworkEnabled.IsOn,
                Color                     = CtlColor.Foreground.GetColor(),
                RandomizeMode             = RandomizeMode,
                RandomizerParamA          = RandomizerParamA,
                LearningRate              = LearningRate,
                InputInitial0             = ActivationFunction.Helper.GetInstance(InputLayer.ActivationFunc).Do(InputLayer.Initial0, InputLayer.ActivationFuncParamA),
                InputInitial1             = ActivationFunction.Helper.GetInstance(InputLayer.ActivationFunc).Do(InputLayer.Initial1, InputLayer.ActivationFuncParamA),
                CostFunction              = CostFunction.Helper.GetInstance(CtlCostFunction.SelectedValue.ToString()),
                IsAdjustFirstLayerWeights = InputLayer.IsAdjustFirstLayerWeights
            };

            model.ActivateNetwork();

            LayerDataModel prevLayer = null;

            var layers = GetLayersControls();

            for (int ln = 0; ln < layers.Count; ++ln)
            {
                if (ln > 0)
                {
                    prevLayer = model.Layers[ln - 1];
                }

                model.Layers[ln].VisualId = layers[ln].Id;

                var neurons = layers[ln].GetNeuronsControls();

                for (int nn = 0; nn < neurons.Count; ++nn)
                {
                    var neuronModel = model.Layers[ln].Neurons[nn];
                    neuronModel.VisualId        = neurons[nn].Id;
                    neuronModel.IsBias          = neurons[nn].IsBias;
                    neuronModel.IsBiasConnected = neurons[nn].IsBiasConnected;

                    neuronModel.ActivationFunction   = ActivationFunction.Helper.GetInstance(neurons[nn].ActivationFunc);
                    neuronModel.ActivationFuncParamA = neurons[nn].ActivationFuncParamA;


                    if (ln == 0 && !neuronModel.IsBias)
                    {
                        neuronModel.WeightsInitializer       = InputLayer.WeightsInitializer;
                        neuronModel.WeightsInitializerParamA = InputLayer.WeightsInitializerParamA;
                        double initValue = InitializeMode.Helper.Invoke(neuronModel.WeightsInitializer, neuronModel.WeightsInitializerParamA);
                        if (!InitializeMode.Helper.IsSkipValue(initValue))
                        {
                            neuronModel.Weights.ForEach(w => w.Weight = InitializeMode.Helper.Invoke(neuronModel.WeightsInitializer, neuronModel.WeightsInitializerParamA));
                        }
                    }
                    else
                    {
                        neuronModel.WeightsInitializer       = neurons[nn].WeightsInitializer;
                        neuronModel.WeightsInitializerParamA = neurons[nn].WeightsInitializerParamA;
                        double initValue = InitializeMode.Helper.Invoke(neuronModel.WeightsInitializer, neuronModel.WeightsInitializerParamA);
                        if (!InitializeMode.Helper.IsSkipValue(initValue))
                        {
                            neuronModel.Weights.ForEach(w => w.Weight = InitializeMode.Helper.Invoke(neuronModel.WeightsInitializer, neuronModel.WeightsInitializerParamA));
                        }
                    }

                    if (neuronModel.IsBias)
                    {
                        neuronModel.ActivationInitializer       = neurons[nn].ActivationInitializer;
                        neuronModel.ActivationInitializerParamA = neurons[nn].ActivationInitializerParamA;
                        double initValue = InitializeMode.Helper.Invoke(neurons[nn].ActivationInitializer, neurons[nn].ActivationInitializerParamA);
                        if (!InitializeMode.Helper.IsSkipValue(initValue))
                        {
                            neuronModel.Activation = initValue;
                        }
                    }

                    if (!isCopy && prevLayer != null && prevLayer.Height > 0)
                    {
                        neuronModel.ForwardHelper = new ListX <ForwardNeuron>(prevLayer.Height);

                        var prevNeuron = prevLayer.Neurons[0];
                        while (prevNeuron != null)
                        {
                            if (!neuronModel.IsBias || (neuronModel.IsBiasConnected && prevNeuron.IsBias))
                            {
                                neuronModel.ForwardHelper.Add(new ForwardNeuron(prevNeuron, prevNeuron.WeightTo(neuronModel)));
                            }

                            prevNeuron = prevNeuron.Next;
                        }
                    }
                }
            }

            model.Layers.Last().VisualId = Const.OutputLayerId;
            {
                var neurons = OutputLayer.GetNeuronsControls();
                for (int i = 0; i < neurons.Count; ++i)
                {
                    model.Layers.Last().Neurons[i].VisualId = neurons[i].Id;
                }
            }

            if (!isCopy)
            {
                model.Copy = CreateNetworkDataModel(task, true);
            }

            return(model);
        }
Пример #25
0
        private void DrawLayersLinks(bool fullState, NetworkDataModel model, LayerDataModel layer1, LayerDataModel layer2, bool isOnlyWeights, bool isOnlyChangedWeights, bool isHighlightChangedWeights)
        {
            double threshold = model.Layers[0] == layer1 ? model.InputThreshold : 0;

            NeuronDataModel prevNeuron = null;

            foreach (var neuron1 in layer1.Neurons)
            {
                if (!Coordinator.ContainsKey(neuron1))
                {
                    Coordinator.Add(neuron1, Points.Get(LayerX(model, layer1), TOP_OFFSET + VerticalShift(model, layer1) + neuron1.Id * VerticalDistance(layer1.Height)));
                }

                // Skip intersected neurons on first layer to improove performance.
                if (!fullState && !neuron1.IsBias && model.Layers[0] == layer1 && prevNeuron != null)
                {
                    if (Coordinator[neuron1].Y - Coordinator[prevNeuron].Y < NEURON_SIZE)
                    {
                        continue;
                    }
                }

                if (fullState || neuron1.IsBias || neuron1.Activation > threshold)
                {
                    foreach (var neuron2 in layer2.Neurons)
                    {
                        if (!neuron2.IsBias || (neuron2.IsBiasConnected && neuron1.IsBias))
                        {
                            if (fullState || ((neuron1.IsBias || neuron1.Activation > threshold) && neuron1.AxW(neuron2) != 0))
                            {
                                Pen  pen             = null;
                                Pen  penChange       = null;
                                bool isWeightChanged = false;
                                var  weight          = neuron1.WeightTo(neuron2);

                                if (WeightsData.TryGetValue(weight, out double prevWeight))
                                {
                                    if (prevWeight != weight.Weight)
                                    {
                                        isWeightChanged     = true;
                                        WeightsData[weight] = weight.Weight;
                                    }
                                }
                                else
                                {
                                    prevWeight      = 0;
                                    isWeightChanged = true;
                                    WeightsData.Add(weight, weight.Weight);
                                }

                                double fraction = Math.Min(1, Math.Abs((prevWeight - weight.Weight) / prevWeight));
                                if (fraction <= 0.001)
                                {
                                    isWeightChanged = false;
                                }

                                if (isWeightChanged && isHighlightChangedWeights)
                                {
                                    penChange = Tools.Draw.GetPen(Colors.Lime);
                                }

                                if (isOnlyWeights)
                                {
                                    if ((isWeightChanged && isOnlyChangedWeights) || !isOnlyChangedWeights)
                                    {
                                        pen = Tools.Draw.GetPen(weight.Weight, 1);
                                    }
                                }
                                else
                                {
                                    pen = Tools.Draw.GetPen(neuron1.AxW(neuron2), 1);
                                }

                                if (!isOnlyChangedWeights && isHighlightChangedWeights && isWeightChanged)
                                {
                                    pen       = penChange;
                                    penChange = null;
                                }

                                if (pen != null)
                                {
                                    if (!Coordinator.ContainsKey(neuron2))
                                    {
                                        Coordinator.Add(neuron2, Points.Get(LayerX(model, layer2), TOP_OFFSET + VerticalShift(model, layer2) + neuron2.Id * VerticalDistance(layer2.Height)));
                                    }

                                    CtlPresenter.DrawLine(pen, Coordinator[neuron1], Coordinator[neuron2]);
                                    prevNeuron = neuron1;
                                }

                                if (penChange != null)
                                {
                                    CtlPresenter.DrawLine(penChange, Coordinator[neuron1], Coordinator[neuron2]);
                                    prevNeuron = neuron1;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #26
0
 private float VerticalShift(NetworkDataModel model, LayerDataModel layer)
 {
     return((MaxHeight(model) - layer.Height * VerticalDistance(layer.Height)) / 2);
 }
Пример #27
0
 private int LayerX(NetworkDataModel model, LayerDataModel layer)
 {
     return(HORIZONTAL_OFFSET + LayerDistance(model) * layer.Id);
 }
Пример #28
0
 public int LayerDistance(NetworkDataModel model)
 {
     return((int)(ActualWidth - 2 * HORIZONTAL_OFFSET) / (model.Layers.Count - 1));
 }
Пример #29
0
 public void Do(NetworkDataModel model)
 {
     Range.For(Rand.Flat.Next(11), i => model.Layers.First().Neurons.RandomElementTrimEnd(model.Layers.First().BiasCount).Activation = model.InputInitial1);
 }
Пример #30
0
 private new float MaxHeight(NetworkDataModel model)
 {
     return(model.Layers.Max(layer => layer.Height * VerticalDistance(layer.Height)));
 }