/// <summary>
        /// Обучает переданную модель искусственной нейронной сети.
        /// </summary>
        /// <param name="neuralNerwork">Модель обучаемой искусственной нейронной сети.</param>
        /// <param name="trainingSet">Тренировочная выборка вида (входной вектор, желаемый выходной вектор).</param>
        /// <param name="validationSet">Проверочная выборка вида (входной вектор, желаемый выходной вектор).</param>
        public INeuralNetwork Learn(INeuralNetwork neuralNetwork, IEnumerable <Tuple <IList <double>, IList <double> > > trainingSet, IEnumerable <Tuple <IList <double>, IList <double> > > validationSet)
        {
            Validate(neuralNetwork, trainingSet, validationSet);

            var normalizedTrainingSet   = NormilizeSet(neuralNetwork, trainingSet);
            var normalizedValidationSet = NormilizeSet(neuralNetwork, validationSet);

            var currentNetworkErrorValue = CalculateNetworkErrorValue(neuralNetwork, normalizedValidationSet);
            var minNetworkErrorValue     = currentNetworkErrorValue;
            var bestModel = (INeuralNetwork)neuralNetwork.Clone();

            for (var i = 0; i < LearningEpochMaxAmount; i++)
            {
                PerformEpoch(neuralNetwork, normalizedTrainingSet);

                currentNetworkErrorValue = CalculateNetworkErrorValue(neuralNetwork, normalizedValidationSet);

                if (currentNetworkErrorValue < minNetworkErrorValue && i + 1 >= _learningEpochMinAmount)
                {
                    minNetworkErrorValue = currentNetworkErrorValue;
                    bestModel            = (INeuralNetwork)neuralNetwork.Clone();
                }
                else
                {
                    break;
                }
            }

            return(bestModel);
        }
Beispiel #2
0
        public void TrainOnMemory(IConcurrentMemory <TData> memory)
        {
            var trainingData = _dataBuilder.BuildDataset(memory);

            if (trainingData == null || trainingData.Count == 0)
            {
                return;
            }

            var clonedInstance = _network.Clone();
            // Train the network
            var result = NetworkManager.TrainNetwork(clonedInstance,
                                                     trainingData,
                                                     TrainingAlgorithms.AdaDelta(),
                                                     _configuration.Epochs, 0.5f,
                                                     TrackBatchProgress,
                                                     TrainingProgress);

            Console.WriteLine("\nTraining session completed, moving to next one");

            var backupName = $"backup-network-{DateTime.Now:yyyyMMdd-HH-mm-ss-fff}.modl";

            _network.Save(File.Create(backupName));
            Console.WriteLine($"Backup model {backupName} saved");
            _network = clonedInstance;
        }
        public void JsonMetadataSerialization()
        {
            INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Rgb24>(120, 120),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationType.AbsoluteReLU),
                                                                  NetworkLayers.Convolutional((5, 5), 20, ActivationType.ELU),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationType.Identity),
                                                                  NetworkLayers.Pooling(ActivationType.ReLU),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationType.Identity),
                                                                  NetworkLayers.Pooling(ActivationType.Identity),
                                                                  NetworkLayers.BatchNormalization(NormalizationMode.Spatial, ActivationType.ReLU),
                                                                  NetworkLayers.FullyConnected(125, ActivationType.Tanh),
                                                                  NetworkLayers.Softmax(133));
            string metadata1 = network.SerializeMetadataAsJson();

            Assert.IsTrue(metadata1.Length > 0);
            Assert.IsTrue(metadata1.Equals(network.Clone().SerializeMetadataAsJson()));
            network.Layers.First().To <INetworkLayer, ConvolutionalLayer>().Weights[0] += 0.1f;
            Assert.IsFalse(metadata1.Equals(network.SerializeMetadataAsJson()));
        }