Пример #1
0
        public void TrainNetworkBySample(DataSamples samples, DataSamples testSamples, BackPropParams param)
        {
            var rnd   = new Random();
            var items = GenerateIndexes(samples);

            //
            infos.InitNeurons(rnd);
            infos.InitNG(Network, rnd);
            var    stop  = false;
            var    step  = 0;
            double error = 0.0;

            while (!stop)
            {
                ShuffleIndexes(items, rnd);
                for (int i = 0; i < items.Count; i++)
                {
                    TrainBySample(samples[items[i]], param.Eta, param.Alpha);
                }
                //
                error = Network.Error(testSamples);
                step++;
                if (param.CallBack != null)
                {
                    param.CallBack(step, error, false);
                }
                stop = (step >= param.MaxSteps) || (error <= param.ErrorStopValue);
            }
            if (param.CallBack != null)
            {
                param.CallBack(step, error, true);
            }
        }
Пример #2
0
        private static List <int> GenerateIndexes(DataSamples samples)
        {
            var items = new List <int>();

            for (int i = 0; i < samples.Count; i++)
            {
                items.Add(i);
            }
            return(items);
        }
Пример #3
0
        public ControllerTestsFixture()
        {
            testServer           = TestServerFactory.GetTestServer();
            using var testClient = testServer.CreateClient();
            _http             = new HttpClient(new TestHttpClientHandler(testServer.CreateHandler()));
            _http.BaseAddress = testClient.BaseAddress;
            dataSamples       = new DataSamples();

            AsyncHelper.RunSync(() => _http.DeleteAsync("api/database"));
            Login();
        }
Пример #4
0
        public void Train(DataSamples samples, DataSamples testSamples, RPropParams param)
        {
            var rnd = new Random();

            infos.InitNeurons(rnd);

            // init deltas
            ProcessSamples(samples);

            while (true)
            {
                infos.UpdateWeights(0.0);

                var error = Network.Error(testSamples);
                if (error < 0.05)
                {
                    break;
                }

                // exchange arrays
                ExchangeDeltas();
                ProcessSamples(samples);
                // calc changes in direction
                foreach (var layerInfo in infos)
                {
                    var layer = layerInfo.Layer;
                    for (int neuronIndex = 0; neuronIndex < layer.Count; neuronIndex++)
                    {
                        for (int weightIndex = 0; weightIndex < layer.Inputs.Length; weightIndex++)
                        {
                            double s0 = layerInfo.PrevDeltas[neuronIndex, weightIndex];
                            double s1 = layerInfo.Deltas[neuronIndex, weightIndex];
                            double sign = s0 > 0 ? -1.0 : 1.0, value = Math.Abs(layerInfo.PrevDeltas[neuronIndex, weightIndex]);
                            if (s0 * s1 > 0.0)
                            {
                                value = Math.Min(50, value * 1.2);
                            }
                            else if (s0 * s1 < 0.0)
                            {
                                value = -sign *Math.Max(1e-30, value *0.5);
                            }
                            else
                            {
                                value = 1e-5;
                            }
                            layerInfo.Deltas[neuronIndex, weightIndex] = sign * value;
                        }
                    }
                }
            }
        }
Пример #5
0
        private void ProcessSamples(DataSamples samples)
        {
            bool first = true;

            foreach (var sample in samples)
            {
                // forward step
                Network.Inputs = sample.Inputs;
                Network.Calc();
                // backward
                infos.ProcessGradients(sample.Outputs);
                infos.ProcessDeltas(1.0, 0, first);
                first = false;
            }
        }
Пример #6
0
        public double Error(DataSamples testSamples)
        {
            double sum = 0.0;

            for (int index = 0; index < testSamples.Count; index++)
            {
                double[] input = testSamples[index].Inputs, output = testSamples[index].Outputs;
                Inputs = input;
                Calc();
                var error = 0.0;
                for (int itemIntex = 0; itemIntex < output.Length; itemIntex++)
                {
                    var diff = output[itemIntex] - Outputs[itemIntex];
                    error += diff * diff;
                }
                error = Math.Sqrt(error) / output.Length;
                sum  += error;
            }
            return(sum / testSamples.Count);
        }
Пример #7
0
        private static void SampleOne()
        {
            // Create a store:
            var store = new DataSamples(500000);

            // Access:
            var measure = store[3];

            Console.WriteLine(measure.HiTemp);

            // Update:
            store[80] = measure;
            Console.WriteLine(store[80].HiTemp);

            // Load a new page:
            var measure2 = store[2020];

            Console.WriteLine(measure2.LoTemp);

            // Load another new page:
            store[3547] = measure2;
            Console.WriteLine(store[3547].LoTemp);

            // Make sure pages rotate in and out of memory:
            for (int j = 5; j < 500000; j += 1100)
            {
                var item = store[j];
                Console.WriteLine(item.AirPressure);
            }

            // Check for valid arguments:
            try
            {
                var item = store[2000000];
                Console.WriteLine("Didn't check bounds");
            }
            catch (IndexOutOfRangeException)
            {
                // empty catch block. Above code should fail.
            }
        }
Пример #8
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <ClinicContext>();
                    DataSamples.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }
Пример #9
0
        public async Task <string> Seed()
        {
            int recipeCount;

            try
            {
                recipeCount = await _db.Recipes.CountAsync();
            }
            catch (Exception) { return("Database error - can't load entities. Contact an administrator."); }

            if (recipeCount > 0)
            {
                return("Database not empty, cannot seed.");
            }

            try
            {
                await _db.Seed();

                var ds = new DataSamples();
                foreach (var u in ds.Users.Where(up => up.Item1.IsPasswordSet))
                {
                    var us = await _db.Users.SingleOrDefaultAsync(x => x.UserName == u.Item1.UserName);

                    var passwordResult = await _userManager.AddPasswordAsync(us, u.Item2.Password);

                    if (!passwordResult.Succeeded)
                    {
                        throw new Exception("Password set failed");
                    }
                }
            }
            catch (Exception e)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return("Cannot write to database. Contact an administrator.");
            }

            return("Data seeded successfuly.");
        }
Пример #10
0
        public void TrainNetworkByBatch(DataSamples samples, DataSamples testSamples, BackPropParams param)
        {
            var rnd = new Random();

            //
            infos.InitNeurons(rnd);
            infos.InitNG(Network, rnd);
            var    stop  = false;
            var    step  = 0;
            double error = 0.0;
            var    count = samples.Count;

            while (!stop)
            {
                for (int sampleIndex = 0; sampleIndex < count; sampleIndex++)
                {
                    var data = samples[sampleIndex];
                    // forward step
                    Network.Inputs = data.Inputs;
                    Network.Calc();
                    // backward
                    infos.ProcessGradients(data.Outputs /*, sampleIndex == 0*/);
                    infos.ProcessDeltas(param.Eta, param.Alpha, sampleIndex == 0);
                }
                infos.UpdateWeights(param.Alpha);
                error = Network.Error(testSamples);
                step++;
                if (param.CallBack != null)
                {
                    param.CallBack(step, error, false);
                }
                stop = (step >= param.MaxSteps) || (error <= param.ErrorStopValue);
            }
            if (param.CallBack != null)
            {
                param.CallBack(step, error, true);
            }
        }
Пример #11
0
        public GraphControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //InitializeComponent();

            data = new DataSamples();

            minVisibleValue = float.MaxValue;
            maxVisibleValue = float.MinValue;

            absMax = float.MinValue;
            absMin = float.MaxValue;
            lastMin = minVisibleValue;
            lastMax = maxVisibleValue;

            rightBottomMargin = DefaultRightBottomMargin;
            leftTopMargin = DefaultLeftTopMargin;

            verticalLines = DefaultVerticalLines;

            labelVisible = DefaultLabelVisible;
            boxVisible = DefaultBoxVisible;

            axisColor = DefaultAxisColor;
            zeroColor = DefaultZeroColor;
            beginColor = DefaultBeginColor;
            LineColor = DefaultLineColor;
            GraphBackColor = DefaultGraphBackColor;

            verticalLabelFormat = DefaultVerticalLabelFormat;

            startTime = DefaultStartTime;
            Text = DefaultTitle;
            titleFont = DefaultTitleFont;
            titleColor = DefaultTitleColor;
            titleDock = DefaultTitleDock;
            visibleSamples = DefaultVisibleSamples;
            showTooltip = DefaultShowTooltip;

            timeScale = DefaultTimeScale;
            timeFormat = DefaultTimeFormat;

            initView = startTime - visibleSamples;
        }