Exemplo n.º 1
0
        public void Process(String methodName, String methodArchitecture, String trainerName, String trainerArgs,
                            int outputNeurons)
        {
            // first, create the machine learning method
            var       methodFactory = new MLMethodFactory();
            IMLMethod method        = methodFactory.Create(methodName, methodArchitecture, 2, outputNeurons);

            // second, create the data set
            IMLDataSet dataSet = new BasicMLDataSet(XORInput, XORIdeal);

            // third, create the trainer
            var      trainFactory = new MLTrainFactory();
            IMLTrain train        = trainFactory.Create(method, dataSet, trainerName, trainerArgs);

            // reset if improve is less than 1% over 5 cycles
            if (method is IMLResettable && !(train is ManhattanPropagation))
            {
                train.AddStrategy(new RequiredImprovementStrategy(500));
            }

            // fourth, train and evaluate.
            EncogUtility.TrainToError(train, 0.01);
            method = train.Method;
            EncogUtility.Evaluate((IMLRegression)method, dataSet);

            // finally, write out what we did
            Console.WriteLine(@"Machine Learning Type: " + methodName);
            Console.WriteLine(@"Machine Learning Architecture: " + methodArchitecture);

            Console.WriteLine(@"Training Method: " + trainerName);
            Console.WriteLine(@"Training Args: " + trainerArgs);
        }
        /// <summary>
        /// Perform the training option.
        /// </summary>
        public void Train()
        {
            // first, create the machine learning method
            var       methodFactory = new MLMethodFactory();
            IMLMethod method        = methodFactory.Create(Config.MethodType, Config.MethodArchitecture, Config.InputWindow, 1);

            // second, create the data set
            string     filename = FileUtil.CombinePath(new FileInfo(_path), Config.FilenameTrain).ToString();
            IMLDataSet dataSet  = EncogUtility.LoadEGB2Memory(new FileInfo(filename));

            // third, create the trainer
            var      trainFactory = new MLTrainFactory();
            IMLTrain train        = trainFactory.Create(method, dataSet, Config.TrainType, Config.TrainParams);

            // reset if improve is less than 1% over 5 cycles
            if (method is IMLResettable && !(train is ManhattanPropagation))
            {
                train.AddStrategy(new RequiredImprovementStrategy(500));
            }

            // fourth, train and evaluate.
            EncogUtility.TrainToError(train, Config.TargetError);
            method = train.Method;
            EncogDirectoryPersistence.SaveObject(FileUtil.CombinePath(new FileInfo(_path), Config.MethodName), method);

            // finally, write out what we did
            Console.WriteLine(@"Machine Learning Type: " + Config.MethodType);
            Console.WriteLine(@"Machine Learning Architecture: " + Config.MethodArchitecture);

            Console.WriteLine(@"Training Method: " + Config.TrainType);
            Console.WriteLine(@"Training Args: " + Config.TrainParams);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, String architecture)
        {
            double inputLow   = -1;
            double inputHigh  = 1;
            double outputLow  = -1;
            double outputHigh = 1;

            // Create a basic neural network, just to examine activation functions.
            var methodFactory = new MLMethodFactory();
            var network       = (BasicNetwork)methodFactory.Create(MethodName, architecture, 1, 1);

            if (network.LayerCount < 1)
            {
                throw new EncogError("Neural network does not have an output layer.");
            }

            IActivationFunction outputFunction = network.GetActivation(network.LayerCount - 1);

            double[] d = { -1000, -100, -50 };
            outputFunction.ActivationFunction(d, 0, d.Length);

            if (d[0] > 0 && d[1] > 0 && d[2] > 0)
            {
                inputLow = 0;
            }

            INormalizationStrategy result = new BasicNormalizationStrategy(
                inputLow,
                inputHigh,
                outputLow,
                outputHigh);

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Create the selected method.
        /// </summary>
        /// <returns>The created method.</returns>
        public IMLMethod CreateMethod()
        {
            if (_methodType == null)
            {
                throw new EncogError(
                          "Please call selectMethod first to choose what type of method you wish to use.");
            }
            var       methodFactory = new MLMethodFactory();
            IMLMethod method        = methodFactory.Create(_methodType, _methodArgs, _dataset
                                                           .NormHelper.CalculateNormalizedInputCount(), _config
                                                           .DetermineOutputCount(_dataset));

            return(method);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String trainingID = Prop.GetPropertyString(
                ScriptProperties.MlConfigTrainingFile);
            String resourceID = Prop.GetPropertyString(
                ScriptProperties.MlConfigMachineLearningFile);

            FileInfo trainingFile = Script.ResolveFilename(trainingID);
            FileInfo resourceFile = Script.ResolveFilename(resourceID);

            String type = Prop.GetPropertyString(
                ScriptProperties.MlConfigType);
            String arch = Prop.GetPropertyString(
                ScriptProperties.MlConfigArchitecture);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning create");
            EncogLogging.Log(EncogLogging.LevelDebug, "training file:"
                             + trainingID);
            EncogLogging.Log(EncogLogging.LevelDebug, "resource file:"
                             + resourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "type:" + type);
            EncogLogging.Log(EncogLogging.LevelDebug, "arch:" + arch);

            var egb = new EncogEGBFile(trainingFile.ToString());

            egb.Open();
            int input = egb.InputCount;
            int ideal = egb.IdealCount;

            egb.Close();

            var       factory = new MLMethodFactory();
            IMLMethod obj     = factory.Create(type, arch, input, ideal);

            if (obj is BayesianNetwork)
            {
                string query = Prop.GetPropertyString(ScriptProperties.MLConfigQuery);
                ((BayesianNetwork)obj).DefineClassificationStructure(query);
            }

            EncogDirectoryPersistence.SaveObject(resourceFile, obj);

            return(false);
        }
Exemplo n.º 6
0
        private void CreateCommand()
        {
            if (_cmd.Args.Count != 3)
            {
                Console.WriteLine(@"Must specify method filename, type and args.");
                return;
            }

            String methodFile = _cmd.Args[0];
            String type       = _cmd.Args[1];
            String args       = _cmd.Args[2];

            _sw.Start();
            var       factory = new MLMethodFactory();
            IMLMethod method  = factory.Create(type, args, 0, 0);

            EncogDirectoryPersistence.SaveObject(new FileInfo(methodFile), method);
        }