public void OnTaskChanged(INetworkTask task)
        {
            var controls = NeuronsHolder.Children.OfType <OutputNeuronControl>().ToList();

            for (int i = controls.Count; i < task.GetClasses().Count; ++i)
            {
                AddNeuron();
            }

            for (int i = task.GetClasses().Count; i < controls.Count; ++i)
            {
                NeuronsHolder.Children.RemoveAt(NeuronsHolder.Children.Count - 1);
            }
        }
Beispiel #2
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);
        }