Ejemplo n.º 1
0
        private void CtlTabs_SelectedIndexChanged(object sender, EventArgs e)
        {
            // newly selected network must not affect NetworksManager until it saved

            if (NetworksManager != null)
            {
                if (IsRunning)
                {
                    lock (ApplyChangesLocker)
                    {
                        CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models.First());
                        CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel);
                        CtlPlotPresenter.Draw(NetworksManager.Models, NetworksManager.SelectedNetworkModel);
                        CtlStatisticsPresenter.Draw(null);
                    }
                }
                else
                {
                    CtlNetworkPresenter.RenderStanding(NetworksManager.SelectedNetworkModel);
                }
            }
        }
Ejemplo n.º 2
0
        private void CtlTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // newly selected network must not affect NetworksManager until it saved

            if (NetworksManager != null)
            {
                if (IsRunning)
                {
                    lock (ApplyChangesLocker)
                    {
                        CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models[0]);
                        CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel, CtlOnlyWeights.IsOn, CtlOnlyChangedWeights.IsOn, CtlHighlightChangedWeights.IsOn);
                        CtlPlotPresenter.Draw(NetworksManager.Models, NetworksManager.SelectedNetworkModel);
                        CtlStatisticsPresenter.Draw(NetworksManager.SelectedNetworkModel.LastStatistics);
                    }
                }
                else
                {
                    CtlNetworkPresenter.RenderStanding(NetworksManager.SelectedNetworkModel);
                }
            }
        }
Ejemplo n.º 3
0
        private void DrawModels(List <NetworkDataModel> models)
        {
            var renderStart = DateTime.Now;

            CtlNetworkPresenter.RenderRunning(NetworksManager.SelectedNetworkModel);
            CtlInputDataPresenter.SetInputDataAndDraw(NetworksManager.Models.First());

            foreach (var model in models)
            {
                model.DynamicStatistic.Add(model.Statistic.Percent, model.Statistic.AverageCost);
            }

            CtlPlotPresenter.Draw(models, NetworksManager.SelectedNetworkModel);

            var selected = NetworksManager.SelectedNetworkModel;

            if (selected == null)
            {
                CtlStatisticsPresenter.Draw(null);
            }
            else
            {
                var stat = new Dictionary <string, string>();
                var span = DateTime.Now.Subtract(StartTime);
                stat.Add("Time", new DateTime(span.Ticks).ToString(@"HH\:mm\:ss"));

                if (selected.Statistic.Percent > 0)
                {
                    var remains = new DateTime((long)(span.Ticks * 100 / selected.Statistic.Percent) - span.Ticks);
                    stat.Add("Time remaining", new DateTime(remains.Ticks).ToString(@"HH\:mm\:ss"));
                }
                else
                {
                    stat.Add("Time remaining", "N/A");
                }

                if (selected.Statistic.LastGoodOutput > -1)
                {
                    stat.Add("Last good output", $"{selected.Statistic.LastGoodInput}={selected.Statistic.LastGoodOutput} ({Converter.DoubleToText(100 * selected.Statistic.LastGoodOutputActivation, "N6")}%)");
                    stat.Add("Last good cost", Converter.DoubleToText(selected.Statistic.LastGoodCost, "N6"));
                }
                else
                {
                    stat.Add("Last good output", "none");
                    stat.Add("Last good cost", "none");
                }

                if (selected.Statistic.LastBadOutput > -1)
                {
                    stat.Add("Last bad output", $"{selected.Statistic.LastBadInput}={selected.Statistic.LastBadOutput} ({Converter.DoubleToText(100 * selected.Statistic.LastBadOutputActivation, "N6")}%)");
                    stat.Add("Last bad cost", Converter.DoubleToText(selected.Statistic.LastBadCost, "N6"));
                }
                else
                {
                    stat.Add("Last bad output", "none");
                    stat.Add("Last bad cost", "none");
                }

                stat.Add("Average cost", Converter.DoubleToText(selected.Statistic.AverageCost, "N6"));
                stat.Add("Percent", Converter.DoubleToText(selected.Statistic.Percent, "N6") + " %");
                stat.Add("Learning rate", Converter.DoubleToText(selected.LearningRate));
                stat.Add("Rounds", Round.ToString());
                stat.Add("Rounds/sec", ((int)((double)Round / DateTime.Now.Subtract(StartTime).TotalSeconds)).ToString());

                var renderStop = DateTime.Now;

                stat.Add("Render time, msec", ((int)(renderStop.Subtract(renderStart).TotalMilliseconds)).ToString());
                CtlStatisticsPresenter.Draw(stat);
            }

            NetworksManager.ResetModelsStatistic();
        }
Ejemplo n.º 4
0
        unsafe private void RunNetwork(object args)
        {
            var arr       = (object[])args;
            var processor = (Processor)arr[0];

            SetProcessorAffinity(processor);
            SetThreadPriority(ThreadPriorityLevel.Highest);

            var forLimit = new List <ForLimit>
            {
                new ForLimit(Settings.SkipRoundsToDrawErrorMatrix),
                new ForLimit(Settings.SkipRoundsToDrawNetworks),
                new ForLimit(Settings.SkipRoundsToDrawStatistics)
            };

            forLimit = forLimit.OrderBy(fl => fl.Current).ToList();

            bool IsErrorMatrixRendering = false;
            bool IsNetworkRendering     = false;
            bool IsStatisticsRendering  = false;

            var currentForLimit = forLimit[0];

            var swPureSpeed = new Stopwatch();
            var swLock      = new Stopwatch();

            double k1 = 0.5;
            double k2 = 0.5;

            while (!CancellationToken.IsCancellationRequested)
            {
                lock (ApplyChangesLocker)
                {
                    swPureSpeed.Start();
                    for (int i = 0; i < currentForLimit.Current; ++i)
                    {
                        NetworksManager.PrepareModelsForRound();

                        var model = NetworksManager.Models[0];
                        while (model != null)
                        {
                            if (!model.IsEnabled)
                            {
                                continue;
                            }

                            model.FeedForward();

                            var output   = model.GetMaxActivatedOutputNeuron();
                            var outputId = output.Id;
                            var input    = model.TargetOutput;
                            var cost     = model.CostFunction.Do(model);
                            if (input == outputId)
                            {
                                ++model.Statistics.CorrectRoundsTotal;
                                ++model.Statistics.CorrectRounds;

                                model.Statistics.LastGoodInput            = model.Classes[input];
                                model.Statistics.LastGoodOutput           = model.Classes[outputId];
                                model.Statistics.LastGoodOutputActivation = output.Activation;
                                model.Statistics.LastGoodCost             = cost;
                            }
                            else
                            {
                                model.Statistics.LastBadInput            = model.Classes[input];
                                model.Statistics.LastBadOutput           = model.Classes[outputId];
                                model.Statistics.LastBadOutputActivation = output.Activation;
                                model.Statistics.LastBadCost             = cost;
                                model.Statistics.LastBadTick             = StartTime.Elapsed.Duration().Ticks;
                            }

                            model.Statistics.CostSum += cost;
                            model.ErrorMatrix.AddData(input, outputId);

                            model.BackPropagation();

                            model = model.Next;
                        }
                    }
                    swPureSpeed.Stop();

                    Round += currentForLimit.Current;

                    if (Round % Settings.SkipRoundsToDrawStatistics == 0)
                    {
                        var pureSpeedElapsedSeconds = swPureSpeed.Elapsed.Duration().TotalSeconds;
                        var totalTicksElapsed       = StartTime.Elapsed.Duration().Ticks;

                        var m = NetworksManager.Models[0];
                        while (m != null)
                        {
                            m.Statistics.Rounds            = Round;
                            m.Statistics.TotalTicksElapsed = totalTicksElapsed;
                            m.Statistics.CostSumTotal     += m.Statistics.CostSum;

                            m.Statistics.PureRoundsPerSecond = Round / pureSpeedElapsedSeconds;

                            var percent      = 100 * (double)m.Statistics.CorrectRounds / Settings.SkipRoundsToDrawStatistics;
                            var percentTotal = 100 * (double)m.Statistics.CorrectRoundsTotal / Round;

                            /*
                             * k1 = (totalTicksElapsed - m.Statistics.LastBadTick) / m.Statistics.LastBadTick;
                             * if (k1 > 1)
                             * {
                             *  k1 = 1;
                             * }
                             *
                             * k2 = 1 - k1;
                             */
                            k1 = 1;
                            k2 = 0;
                            m.Statistics.Percent = percent * k1 + percentTotal * k2;

                            var costAvg      = m.Statistics.CostSum / Settings.SkipRoundsToDrawStatistics;
                            var costAvgTotal = m.Statistics.CostSumTotal / Round;
                            m.Statistics.CostAvg = costAvg * k1 + costAvgTotal * k2;

                            m.DynamicStatistics.Add(m.Statistics.Percent, m.Statistics.CostAvg);

                            m.Statistics.CostSum       = 0;
                            m.Statistics.CorrectRounds = 0;

                            m = m.Next;
                        }
                    }

                    if (forLimit.Count > 1)
                    {
                        for (int i = 1; i < forLimit.Count; ++i)
                        {
                            forLimit[i].Current -= currentForLimit.Current;
                        }
                        currentForLimit.Current = currentForLimit.Original;

                        forLimit.RemoveAll(fl => fl.Current == 0);
                        forLimit        = forLimit.OrderBy(fl => fl.Current).ToList();
                        currentForLimit = forLimit[0];
                    }
                }

                var matrixNeeded     = !IsErrorMatrixRendering && Round % Settings.SkipRoundsToDrawErrorMatrix == 0;
                var networkNeeded    = !IsNetworkRendering && Round % Settings.SkipRoundsToDrawNetworks == 0;
                var statisticsNeeded = !IsStatisticsRendering && Round % Settings.SkipRoundsToDrawStatistics == 0;
                var anyNeeded        = matrixNeeded || networkNeeded || statisticsNeeded;

                if (anyNeeded)
                {
                    if (matrixNeeded)
                    {
                        IsErrorMatrixRendering = true;
                    }

                    if (networkNeeded)
                    {
                        IsNetworkRendering = true;
                    }

                    if (statisticsNeeded)
                    {
                        IsStatisticsRendering = true;
                    }

                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        if (matrixNeeded)
                        {
                            ErrorMatrix errorMatrix;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                errorMatrix = NetworksManager.SelectedNetworkModel.ErrorMatrix;
                                NetworksManager.SelectedNetworkModel.ErrorMatrix = errorMatrix.Next;

                                swLock.Stop();
                                RenderTime.ErrorMatrix = swLock.Elapsed.Ticks;
                            }

                            CtlMatrixPresenter.Draw(errorMatrix);
                            errorMatrix.ClearData();
                            IsErrorMatrixRendering = false;
                        }

                        if (networkNeeded)
                        {
                            NetworkDataModel modelCopy;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                modelCopy = NetworksManager.SelectedNetworkModel.GetCopyForRender();
                                CtlInputDataPresenter.SetInputStat(NetworksManager.Models[0]);

                                swLock.Stop();
                                RenderTime.Network = swLock.Elapsed.Ticks;
                            }

                            DrawNetwork(modelCopy, CtlOnlyWeights.IsOn, CtlOnlyChangedWeights.IsOn, CtlHighlightChangedWeights.IsOn);

                            IsNetworkRendering = false;
                        }

                        if (statisticsNeeded)
                        {
                            NetworkDataModel selectedModel;
                            Statistics statistics;
                            double learningRate;

                            lock (ApplyChangesLocker)
                            {
                                swLock.Restart();

                                CtlPlotPresenter.Vanish(NetworksManager.Models);

                                selectedModel = NetworksManager.SelectedNetworkModel;
                                statistics = selectedModel?.Statistics.Copy();
                                learningRate = selectedModel == null ? 0 : selectedModel.LearningRate;

                                swLock.Stop();
                                RenderTime.Statistics = swLock.Elapsed.Ticks;
                            }

                            CtlPlotPresenter.Draw(NetworksManager.Models, selectedModel);

                            var lastStats = DrawStatistics(statistics, learningRate);
                            if (selectedModel != null)
                            {
                                selectedModel.LastStatistics = lastStats;
                            }

                            IsStatisticsRendering = false;
                        }
                    }), DispatcherPriority.Render);//  .Send);
                }
            }

            StartTime.Stop();
        }