Example #1
0
        public void Sgd(List <DigitImageForStuding> trainingData, Parametrs param, List <DigitImageForEvaluating> testData)
        {
            var fileDiscryption = FileDescryption(param);
            var fileNameResult  = "Result with " + fileDiscryption + ".txt";
            var fileNameNetwork = "Network with " + fileDiscryption + ".bin";

            var nTest = 0;

            if (testData != null)
            {
                nTest = testData.Count;
            }

            using (var file = new StreamWriter(_folderForResult + fileNameResult))
            {
                for (var i = 0; i < param.Epochs; i++)
                {
                    trainingData.Shuffle();
                    List <List <DigitImageForStuding> > miniBatches = MakeMiniBatches(trainingData, param.MiniBatchSize);



                    // miniBatches.ForEach(x => UpdateMiniBatch(x, param.Eta, param.Beta));

                    foreach (List <DigitImageForStuding> miniBatch in miniBatches)
                    {
                        var tempPrevoiusWeights = _weights.ConvertAll(m => m.Clone()); //For momentum

                        var tempNet = _builder.Build(this);

                        UpdateMiniBatch(tempNet, miniBatch, param);

                        _updater.Update(this, tempNet);

                        _previousWeights = tempPrevoiusWeights; //For momentum
                    }

                    if (testData != null)
                    {
                        var currentEfficiency = Evaluate(testData);

                        if (_bestEfficiency < currentEfficiency)
                        {
                            SaveToBinary(_folderForNetwork + fileNameNetwork);
                            _bestEfficiency = currentEfficiency;
                        }

                        if (_dropoutOn)
                        {
                            _weights = _weights.Select(w => w * 0.5).ToList();
                            _biases  = _biases.Select(b => b * 0.5).ToList();
                        }

                        var agregateCostValidationData = AgregateCost(testData.Select(x => x.Pixels).ToList(),
                                                                      testData.Select(x => _helpers.VectorizedResult(x.Label)).ToList());
                        var agregateCostStudyData = AgregateCost(trainingData.Select(x => x.Pixels).ToList(),
                                                                 trainingData.Select(x => x.Label).ToList());

                        Console.WriteLine("Epoch {0}: {1} / {2}", i, currentEfficiency, nTest);
                        Console.WriteLine("Cost on training set: {0}", agregateCostStudyData);
                        Console.WriteLine("Cost on validation set {0}", agregateCostValidationData);

                        file.WriteLine("Epoch {0}: {1} / {2}", i, currentEfficiency, nTest);
                        file.WriteLine("Cost on training set: {0}", agregateCostStudyData);
                        file.WriteLine("Cost on validation set {0}", agregateCostValidationData);

                        if (_dropoutOn)
                        {
                            _weights = _weights.Select(w => w * 2).ToList();
                            _biases  = _biases.Select(b => b * 2).ToList();
                        }
                    }
                    else
                    {
                        Console.WriteLine("Epoch {0} complete", i);
                        file.WriteLine("Epoch {0} complete", i);
                    }
                }
            }
        }
Example #2
0
 private void LateUpdate()
 {
     NetworkUpdater.Update();
 }