// Train network using the according settings.
        public static void Train(ref NetworkContainer container, ref EncogTrainSettings settings)
        {
            if (settings.maxTrainingError <= 0)
            {
                throw new EncogException("Maxtrainingerror should be higher than 0");
            }

            // Create training data.
            IMLDataSet trainingSet = new BasicMLDataSet(settings.dataset, settings.answers);

            // Train the neural network.
            IMLTrain train = new ResilientPropagation(container.network, trainingSet);

            uint epoch = 0;

            do
            {
                train.Iteration();
                if (container.verbose)
                {
                    Console.WriteLine("Epoch # " + epoch + " Error: " + train.Error);
                }
                epoch++;
            } while (train.Error > settings.maxTrainingError && (epoch < settings.maxEpochCount || settings.maxEpochCount == 0));
        }
Example #2
0
        // Predict the output of a dataset using an existing network.
        public static bool Predict(ref NetworkContainer container, ref CountNetworkPredictSettings settings)
        {
            if (settings.predictSettings.data == null)
            {
                throw new IncorrectActionOrderException("Prepare predictor before predicting.");
            }

            return(EncogWrapper.Predict(ref container, ref settings.predictSettings));
        }
 public static void SaveNetworkToFS(ref NetworkContainer container, string fileName)
 {
     try
     {
         EncogDirectoryPersistence.SaveObject(new FileInfo(fileName), container.network);
     }
     catch
     {
         throw new EncogException("Failed to save network to file system.");
     }
 }
 public static void LoadNetworkFromFS(ref NetworkContainer container, string fileName)
 {
     try
     {
         container.network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(fileName));
     }
     catch
     {
         throw new EncogException("Failed to load network from file system.");
     }
 }
 public static void FinalizeNetwork(ref NetworkContainer container)
 {
     try
     {
         container.network.Structure.FinalizeStructure();
         container.network.Reset();
     }
     catch
     {
         throw new EncogException("Failed to finalize network.");
     }
 }
        // Predict data using a network.
        public static bool Predict(ref NetworkContainer container, ref EncogPredictSettings settings)
        {
            IMLData output = container.network.Compute(new BasicMLData(settings.data));

            if (container.verbose)
            {
                Console.WriteLine("Raw output: " + output[0]);
            }

            // Return true or false according to threshold.
            return(output[0] >= settings.threshold);
        }
        // Setup a new network.
        public static void Instantiate(ref NetworkContainer container)
        {
            if (container == null)
            {
                container = new NetworkContainer();
            }

            if (container.network != null)
            {
                throw new IncorrectActionOrderException("Network has already been instantiated.");
            }

            container.network = new BasicNetwork();
        }
        public static void PrepareData(ref NetworkContainer container, ref CountNetworkTrainSettings settings)
        {
            if (settings.trainSettings.dataset != null)
            {
                throw new IncorrectActionOrderException("This action has already been completed.");
            }

            TestForErrors(ref settings);

            settings.trainSettings = new EncogTrainSettings
            {
                maxTrainingError = 0.01
            };

            int correctFileCount   = BaseTrainHelper.GetFileCount(settings.correctInputDirectory);
            int incorrectFileCount = BaseTrainHelper.GetFileCount(settings.incorrectInputDirectory);

            if (correctFileCount <= 0)
            {
                throw new FileNotFoundException("correctFiles not found.");
            }

            if (incorrectFileCount <= 0)
            {
                throw new FileNotFoundException("incorretFiles not found.");
            }

            settings.trainSettings.dataset = new double[correctFileCount + incorrectFileCount][];
            settings.trainSettings.answers = new double[correctFileCount + incorrectFileCount][];

            // Compute correct training data.
            ComputeData(
                settings.sampleCount,
                settings.correctInputDirectory,
                ref settings.trainSettings.dataset,
                ref settings.trainSettings.answers,
                1.0,
                0);

            // Compute incorrect training data.
            ComputeData(
                settings.sampleCount,
                settings.incorrectInputDirectory,
                ref settings.trainSettings.dataset,
                ref settings.trainSettings.answers,
                0.0,
                correctFileCount);
        }
        // Prepare input data for prediction.
        public static void PreparePredictor(ref NetworkContainer container, ref ImageNetworkPredictSettings settings)
        {
            TestForErrors(ref settings);

            EncogWrapper.LoadNetworkFromFS(ref container, settings.trainedNetwork);

            List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
            ICSVFilter        quaternions = new CSVEvenColumnFilter();

            baseFilters.Add(quaternions);

            // Setup loader.
            CSVLoaderSettings CSVSettings = new CSVLoaderSettings
            {
                filePath = settings.predictData,
                trimUp   = 1,
                trimDown = 0,
                filters  = baseFilters
            };

            var data = CSVLoader <Vector3> .LoadData(ref CSVSettings);

            // Initialize image Transformer.
            ImageTransformerSettings imageSettings = new ImageTransformerSettings
            {
                focusJoints = (LeapMotionJoint[])Enum.GetValues(typeof(LeapMotionJoint)),
                samples     = data,
                size        = settings.imgSize
            };
            ImageTransformer imageTransformer = new ImageTransformer();


            if (settings.predictSettings.threshold.Equals(null))
            {
                settings.predictSettings = new EncogPredictSettings
                {
                    threshold = 0.9
                };
            }

            settings.predictSettings.data = imageTransformer.GetNeuralInput(imageSettings);

            if (settings.predictSettings.data.Length != container.network.InputCount)
            {
                throw new NoNetworkMatchException("Sample count doesn't match network input count.");
            }
        }
Example #10
0
        public static void PreparePredictor(ref NetworkContainer container, ref CountNetworkPredictSettings settings)
        {
            TestForErrors(ref settings);

            EncogWrapper.LoadNetworkFromFS(ref container, settings.trainedNetwork);

            List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
            ICSVFilter        quaternions = new CSVEvenColumnFilter();

            baseFilters.Add(quaternions);

            // Setup loader.
            CSVLoaderSettings CSVSettings = new CSVLoaderSettings
            {
                filePath = settings.predictData,
                trimUp   = 1,
                trimDown = 0,
                filters  = baseFilters
            };

            var data = CSVLoader <Vector3> .LoadData(ref CSVSettings);

            // Initialize CountBased Transformer settings.
            IntervalBasedTransformerSettings countSettings = new IntervalBasedTransformerSettings
            {
                sampleList = data,
                count      = settings.sampleCount
            };
            CountBasedTransformer countTransformer = new CountBasedTransformer();

            if (settings.predictSettings.threshold.Equals(null))
            {
                settings.predictSettings = new EncogPredictSettings
                {
                    threshold = 0.9
                };
            }

            settings.predictSettings.data = countTransformer.GetNeuralInput(countSettings);

            if (settings.predictSettings.data.Length != container.network.InputCount)
            {
                throw new NoNetworkMatchException("Sample count doesn't match network input count.");
            }
        }
        // Add a custom layer to the network.
        public static void AddLayer(ref NetworkContainer container, ref EncogLayerSettings settings)
        {
            if (settings.neuronCount <= 0)
            {
                throw new InvalidNeuronCountException("Neuroncount should be higher than 0.");
            }

            try
            {
                container.network.AddLayer(new BasicLayer(
                                               settings.activationFunction,
                                               settings.hasBias,
                                               settings.neuronCount));
            }
            catch
            {
                throw new EncogException("Adding layer failed.");
            }
        }
        public static void Train(ref NetworkContainer container, ref CountNetworkTrainSettings settings)
        {
            if (settings.trainSettings.dataset == null)
            {
                throw new IncorrectActionOrderException("Prepare data before training network.");
            }

            if (container == null)
            {
                throw new IncorrectActionOrderException("Prepare network before training network.");
            }

            EncogWrapper.Train(ref container, ref settings.trainSettings);
            if (settings.outputDirectory[settings.outputDirectory.Length - 1].Equals("/"))
            {
                EncogWrapper.SaveNetworkToFS(ref container, settings.outputDirectory + settings.outputName + ".eg");
                return;
            }

            EncogWrapper.SaveNetworkToFS(ref container, settings.outputDirectory + "/" + settings.outputName + ".eg");
        }
Example #13
0
        // Prepare network for training, this is mainly setting up the layers and activation functions.
        public static void PrepareNetwork(ref NetworkContainer container, ref ImageNetworkTrainSettings settings)
        {
            if (settings.trainSettings.dataset == null)
            {
                throw new IncorrectActionOrderException("Prepare data before preparing network.");
            }

            EncogWrapper.Instantiate(ref container);

            EncogLayerSettings inputLayerSettings = new EncogLayerSettings
            {
                activationFunction = null,
                hasBias            = true,
                neuronCount        = settings.trainSettings.dataset[0].Length
            };

            EncogWrapper.AddLayer(ref container, ref inputLayerSettings);

            EncogLayerSettings hiddenLayerOneSettings = new EncogLayerSettings
            {
                activationFunction = new ActivationElliott(),
                hasBias            = true,
                neuronCount        = 100
            };

            EncogWrapper.AddLayer(ref container, ref hiddenLayerOneSettings);

            EncogLayerSettings outputLayerSettings = new EncogLayerSettings
            {
                activationFunction = new ActivationElliott(),
                hasBias            = false,
                neuronCount        = 1
            };

            EncogWrapper.AddLayer(ref container, ref outputLayerSettings);
            EncogWrapper.FinalizeNetwork(ref container);
        }