Beispiel #1
0
        /// <summary>
        /// Perform either a train or a cross validation.  If the folds property is
        /// greater than 1 then cross validation will be done.  Cross validation does
        /// not produce a usable model, but it does set the error.
        /// If you are cross validating try C and Gamma values until you have a good
        /// error rate.  Then use those values to train, producing the final model.
        /// </summary>
        ///
        public override sealed void Iteration()
        {
            _network.Params.C     = _c;
            _network.Params.gamma = _gamma;

            EncogLogging.Log(EncogLogging.LevelInfo, "Training with parameters C = " + _c + ", gamma = " + _gamma);

            if (_fold > 1)
            {
                // cross validate
                var target = new double[_problem.l];

                svm.svm_cross_validation(_problem, _network.Params,
                                         _fold, target);
                _network.Model = null;

                Error = Evaluate(_network.Params, _problem, target);
            }
            else
            {
                // train
                _network.Model = svm.svm_train(_problem,
                                               _network.Params);

                Error = _network.CalculateError(Training);
            }

            _trainingDone = true;
        }
        /// <summary>
        /// Save the specified object.
        /// </summary>
        ///
        /// <param name="filename">The filename to save to.</param>
        /// <param name="obj">The Object to save.</param>
        public static void SaveObject(FileInfo filename, Object obj)
        {
            FileStream fos = null;

            try
            {
                filename.Delete();
                fos = filename.OpenWrite();
                SaveObject(fos, obj);
            }
            catch (IOException ex)
            {
                throw new PersistError(ex);
            }
            finally
            {
                try
                {
                    if (fos != null)
                    {
                        fos.Close();
                    }
                }
                catch (IOException e)
                {
                    EncogLogging.Log(e);
                }
            }
        }
        /// <summary>
        /// Create a stream to read the resource.
        /// </summary>
        /// <param name="resource">The resource to load.  This should be in the form Encog.Resources.classes.txt</param>
        /// <returns>A stream.</returns>
        public static Stream CreateStream(String resource)
        {
            Stream result = null;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in assemblies)
            {
                if (a.IsDynamic) // this is the fix (https://github.com/encog/encog-dotnet-core/issues/51)
                {
                    continue;
                }
                try
                {
                    result = a.GetManifestResourceStream(resource);
                    if (result != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    // Mostly, ignore the error in case one assembly could not be accessed.
                    // This is probably not necessary given the Dynamic check above.
                    EncogLogging.Log(ex);
                }
            }

            if (result == null)
            {
                throw new EncogError("Cannot create stream");
            }

            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        ///
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.GenerateConfigSourceFile);
            String targetID = Prop.GetPropertyString(
                ScriptProperties.GenerateConfigTargetFile);
            CSVFormat format = Analyst.Script.DetermineInputFormat(
                sourceID);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning generate");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "target file:" + targetID);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);
            FileInfo targetFile = Script.ResolveFilename(targetID);

            // mark generated
            Script.MarkGenerated(targetID);

            // read file
            bool headers    = Script.ExpectInputHeaders(sourceID);
            var  headerList = new CSVHeaders(sourceFile, headers,
                                             format);

            int[] input = DetermineInputFields(headerList);
            int[] ideal = DetermineIdealFields(headerList);

            EncogUtility.ConvertCSV2Binary(sourceFile, format, targetFile, input,
                                           ideal, headers);
            return(false);
        }
Beispiel #5
0
        /// <inheritdoc />
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.BalanceConfigSourceFile);
            String targetID = Prop.GetPropertyString(
                ScriptProperties.BalanceConfigTargetFile);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning balance");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "target file:" + targetID);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);
            FileInfo targetFile = Script.ResolveFilename(targetID);

            // get other config data
            int countPer = Prop.GetPropertyInt(
                ScriptProperties.BalanceConfigCountPer);
            String targetFieldStr = Prop.GetPropertyString(
                ScriptProperties.BalanceConfigBalanceField);
            DataField targetFieldDf = Analyst.Script.FindDataField(
                targetFieldStr);

            if (targetFieldDf == null)
            {
                throw new AnalystError("Can't find balance target field: "
                                       + targetFieldStr);
            }
            if (!targetFieldDf.Class)
            {
                throw new AnalystError("Can't balance on non-class field: "
                                       + targetFieldStr);
            }

            int targetFieldIndex = Analyst.Script
                                   .FindDataFieldIndex(targetFieldDf);

            // mark generated
            Script.MarkGenerated(targetID);

            // get formats
            CSVFormat inputFormat  = Script.DetermineFormat();
            CSVFormat outputFormat = Script.DetermineFormat();

            // prepare to normalize
            var balance = new BalanceCSV {
                Script = Script
            };

            Analyst.CurrentQuantTask = balance;
            balance.Report           = new AnalystReportBridge(Analyst);

            bool headers = Script.ExpectInputHeaders(sourceID);

            balance.Analyze(sourceFile, headers, inputFormat);
            balance.ProduceOutputHeaders = true;
            balance.Process(targetFile, targetFieldIndex, countPer);
            Analyst.CurrentQuantTask = null;
            return(balance.ShouldStop());
        }
Beispiel #6
0
        /// <summary>
        /// Background thread.
        /// </summary>
        public void Run()
        {
            EncogLogging.Log(EncogLogging.LevelDebug, "Waiting for packets");

            try
            {
                while (!_done)
                {
                    IndicatorPacket packet = Link.ReadPacket();

                    // really do not care if we timeout, just keep listening
                    if (packet == null)
                    {
                        continue;
                    }
                    if (string.Compare(packet.Command,
                                       IndicatorLink.PacketHello, true) == 0)
                    {
                        RemoteType    = packet.Args[0];
                        IndicatorName = packet.Args[1];
                        _listener     = _server
                                        .ResolveIndicator(IndicatorName);
                        _listener.NotifyConnect(Link);
                    }
                    else if (string.Compare(packet.Command,
                                            IndicatorLink.PacketGoodbye, true) == 0)
                    {
                        _done = true;
                    }
                    else
                    {
                        _listener.NotifyPacket(packet);
                    }
                }
            }
            catch (IndicatorError ex)
            {
                EncogLogging.Log(EncogLogging.LevelDebug,
                                 "Client ended connection:" + ex.Message);

                _done = true;
            }
            catch (Exception t)
            {
                EncogLogging.Log(EncogLogging.LevelCritical, t);
            }
            finally
            {
                _done = true;
                _server.Connections.Remove(this);
                if (_listener != null)
                {
                    _listener.NotifyTermination();
                }
                _server.NotifyListenersConnections(Link, false);
                EncogLogging.Log(EncogLogging.LevelDebug,
                                 "Shutting down client handler");
                Link.Close();
            }
        }
Beispiel #7
0
        /// <inheritdoc />
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.RandomizeConfigSourceFile);
            String targetID = Prop.GetPropertyString(
                ScriptProperties.RandomizeConfigTargetFile);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning randomize");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "target file:" + targetID);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);
            FileInfo targetFile = Script.ResolveFilename(targetID);

            // get formats
            CSVFormat format = Script.DetermineFormat();

            // mark generated
            Script.MarkGenerated(targetID);

            // prepare to normalize
            var norm = new ShuffleCSV {
                Script = Script
            };

            Analyst.CurrentQuantTask = norm;
            norm.Report = new AnalystReportBridge(Analyst);
            bool headers = Script.ExpectInputHeaders(sourceID);

            norm.Analyze(sourceFile, headers, format);
            norm.Process(targetFile);
            Analyst.CurrentQuantTask = null;
            return(norm.ShouldStop());
        }
Beispiel #8
0
        /// <summary>
        /// Called just after a training iteration.
        /// </summary>
        ///
        public virtual void PostIteration()
        {
            if (_ready)
            {
                double currentError = _mainTrain.Error;
                _lastImprovement = (currentError - _lastError)
                                   / _lastError;
                EncogLogging.Log(EncogLogging.LevelDebug, "Last improvement: "
                                 + _lastImprovement);

                if ((_lastImprovement > 0) ||
                    (Math.Abs(_lastImprovement) < _minImprovement))
                {
                    _lastHybrid++;

                    if (_lastHybrid > _tolerateMinImprovement)
                    {
                        _lastHybrid = 0;

                        EncogLogging.Log(EncogLogging.LevelDebug,
                                         "Performing hybrid cycle");

                        for (int i = 0; i < _alternateCycles; i++)
                        {
                            _altTrain.Iteration();
                        }
                    }
                }
            }
            else
            {
                _ready = true;
            }
        }
        /// <summary>
        /// Load the specified object.
        /// </summary>
        ///
        /// <param name="file">The file to load.</param>
        /// <returns>The loaded object.</returns>
        public static Object LoadObject(FileInfo file)
        {
            FileStream fis = null;

            try
            {
                fis = file.OpenRead();
                Object result = LoadObject(fis);

                return(result);
            }
            catch (IOException ex)
            {
                throw new PersistError(ex);
            }
            finally
            {
                if (fis != null)
                {
                    try
                    {
                        fis.Close();
                    }
                    catch (IOException e)
                    {
                        EncogLogging.Log(e);
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Called just before a training iteration.
        /// </summary>
        ///
        public virtual void PreIteration()
        {
            if (_train.Error > _acceptableThreshold)
            {
                if (!Double.IsNaN(_lastError))
                {
                    double improve = (_lastError - _train.Error);
                    if (improve < _required)
                    {
                        _badCycleCount++;
                        if (_badCycleCount > _cycles)
                        {
                            EncogLogging.Log(EncogLogging.LevelDebug,
                                             "Failed to improve network, resetting.");
                            _method.Reset();
                            _badCycleCount = 0;
                            _lastError     = Double.NaN;
                        }
                    }
                    else
                    {
                        _badCycleCount = 0;
                    }
                }
                else
                {
                    _lastError = _train.Error;
                }
            }

            _lastError = Math.Min(_train.Error, _lastError);
        }
 /// <summary>
 /// Perform one training iteration.
 /// </summary>
 ///
 public override sealed void Iteration()
 {
     EncogLogging.Log(EncogLogging.LevelInfo,
                      "Performing Genetic iteration.");
     PreIteration();
     Genetic.Iteration();
     Error = Genetic.Error;
     PostIteration();
 }
 /// <summary>
 /// Perform one iteration of simulated annealing.
 /// </summary>
 ///
 public override sealed void Iteration()
 {
     EncogLogging.Log(EncogLogging.LevelInfo,
                      "Performing Simulated Annealing iteration.");
     PreIteration();
     _anneal.Iteration();
     Error = _anneal.PerformCalculateScore();
     PostIteration();
 }
 /// <summary>
 /// Initialize this strategy.
 /// </summary>
 ///
 /// <param name="train">The training algorithm.</param>
 public void Init(IMLTrain train)
 {
     _train               = train;
     _ready               = false;
     _setter              = (ILearningRate)train;
     _trainingSize        = train.Training.Count;
     _currentLearningRate = 1.0d / _trainingSize;
     EncogLogging.Log(EncogLogging.LevelDebug, "Starting learning rate: "
                      + _currentLearningRate);
     _setter.LearningRate = _currentLearningRate;
 }
Beispiel #14
0
        /// <summary>
        ///     Execute a task.
        /// </summary>
        /// <param name="name">The name of the task to execute.</param>
        public void ExecuteTask(String name)
        {
            EncogLogging.Log(EncogLogging.LevelInfo, "Analyst execute task:"
                             + name);
            AnalystTask task = _script.GetTask(name);

            if (task == null)
            {
                throw new AnalystError("Can't find task: " + name);
            }

            ExecuteTask(task);
        }
Beispiel #15
0
        /// <summary>
        ///     Execute a task.
        /// </summary>
        /// <param name="task">The task to execute.</param>
        public void ExecuteTask(AnalystTask task)
        {
            int total   = task.Lines.Count;
            int current = 1;

            foreach (String line in task.Lines)
            {
                EncogLogging.Log(EncogLogging.LevelDebug, "Execute analyst line: "
                                 + line);
                ReportCommandBegin(total, current, line);

                bool   canceled = false;
                String command;
                String args;

                String line2 = line.Trim();
                int    index = line2.IndexOf(' ');
                if (index != -1)
                {
                    command = line2.Substring(0, (index) - (0)).ToUpper();
                    args    = line2.Substring(index + 1);
                }
                else
                {
                    command = line2.ToUpper();
                    args    = "";
                }

                Cmd cmd = _commands[command];

                if (cmd != null)
                {
                    canceled = cmd.ExecuteCommand(args);
                }
                else
                {
                    throw new AnalystError("Unknown Command: " + line);
                }

                ReportCommandEnd(canceled);
                CurrentQuantTask = null;
                current++;

                if (ShouldStopAll())
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Perform the specified number of training iterations. This can be more
        /// efficient than single training iterations. This is particularly true if
        /// you are training with a GPU.
        /// </summary>
        public override void Iteration()
        {
            try
            {
                PreIteration();

                RollIteration();

                CalculateGradients();

                if (_flat.Limited)
                {
                    LearnLimited();
                }
                else
                {
                    Learn();
                }


                foreach (GradientWorker worker in _workers)
                {
                    EngineArray.ArrayCopy(_flat.Weights, 0,
                                          worker.Weights, 0, _flat.Weights.Length);
                }

                if (_flat.HasContext)
                {
                    CopyContexts();
                }

                if (_reportedException != null)
                {
                    throw (new EncogError(_reportedException));
                }

                PostIteration();

                EncogLogging.Log(EncogLogging.LevelInfo,
                                 "Training iterations done, error: " + Error);
            }
            catch (IndexOutOfRangeException ex)
            {
                EncogValidate.ValidateNetworkForTraining(_network,
                                                         Training);
                throw new EncogError(ex);
            }
        }
Beispiel #17
0
 /// <summary>
 /// Called just after a training iteration.
 /// </summary>
 ///
 public virtual void PostIteration()
 {
     if (_ready)
     {
         if (_train.Error > _lastError)
         {
             EncogLogging.Log(EncogLogging.LevelDebug,
                              "Greedy strategy dropped last iteration.");
             _train.Error = _lastError;
             _method.DecodeFromArray(_lastNetwork);
         }
     }
     else
     {
         _ready = true;
     }
 }
Beispiel #18
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);
        }
Beispiel #19
0
        /// <inheritdoc />
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String evalID = Prop.GetPropertyString(
                ScriptProperties.MlConfigEvalFile);
            String resourceID = Prop.GetPropertyString(
                ScriptProperties.MlConfigMachineLearningFile);

            String outputID = Prop.GetPropertyString(
                ScriptProperties.MlConfigOutputFile);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning evaluate raw");
            EncogLogging.Log(EncogLogging.LevelDebug, "evaluate file:" + evalID);
            EncogLogging.Log(EncogLogging.LevelDebug, "resource file:"
                             + resourceID);

            FileInfo evalFile     = Script.ResolveFilename(evalID);
            FileInfo resourceFile = Script.ResolveFilename(resourceID);

            FileInfo outputFile = Analyst.Script.ResolveFilename(
                outputID);

            var m = (IMLMethod)EncogDirectoryPersistence.LoadObject(resourceFile);

            if (!(m is IMLRegression))
            {
                throw new AnalystError("The evaluate raw command can only be used with regression.");
            }

            var method = (IMLRegression)m;

            bool headers = Script.ExpectInputHeaders(evalID);

            var eval = new AnalystEvaluateRawCSV {
                Script = Script
            };

            Analyst.CurrentQuantTask = eval;
            eval.Report = new AnalystReportBridge(Analyst);
            eval.Analyze(Analyst, evalFile, headers, Prop
                         .GetPropertyCSVFormat(ScriptProperties.SetupConfigCSVFormat));
            eval.Process(outputFile, method);
            Analyst.CurrentQuantTask = null;
            return(eval.ShouldStop());
        }
        /// <summary>
        /// Background thread.
        /// </summary>
        public void Run()
        {
            try
            {
                _running = true;
                _listenSocket.Listen(5);
                while (_running)
                {
                    try
                    {
                        EncogLogging.Log(EncogLogging.LevelDebug, "Begin listen");
                        Socket connectionSocket = _listenSocket.Accept();
                        EncogLogging.Log(EncogLogging.LevelDebug, "Connection from: " + connectionSocket.RemoteEndPoint);
                        var link = new IndicatorLink(this, connectionSocket);
                        NotifyListenersConnections(link, true);
                        var hc = new HandleClient(this, link);
                        _connections.Add(hc);
                        var t = new Thread(hc.Run);
                        t.Start();
                    }
                    catch (SocketException)
                    {
                        // just accept timing out
                        Thread.Sleep(100);
                    }
                    catch (IOException ex)
                    {
                        throw new IndicatorError(ex);
                    }
                }

                try
                {
                    _listenSocket.Close();
                }
                catch (IOException ex)
                {
                    throw new IndicatorError(ex);
                }
            }
            finally
            {
                _running = false;
            }
        }
 /// <summary>
 /// Called just after a training iteration.
 /// </summary>
 ///
 public void PostIteration()
 {
     if (_ready)
     {
         if (_train.Error > _lastError)
         {
             _currentLearningRate *= LearningDecay;
             _setter.LearningRate  = _currentLearningRate;
             EncogLogging.Log(EncogLogging.LevelDebug,
                              "Adjusting learning rate to {}"
                              + _currentLearningRate);
         }
     }
     else
     {
         _ready = true;
     }
 }
 /// <summary>
 /// Called just before a training iteration.
 /// </summary>
 ///
 public virtual void PreIteration()
 {
     if (_train.Error > _required)
     {
         _badCycleCount++;
         if (_badCycleCount > _cycles)
         {
             EncogLogging.Log(EncogLogging.LevelDebug,
                              "Failed to imrove network, resetting.");
             _method.Reset();
             _badCycleCount = 0;
         }
     }
     else
     {
         _badCycleCount = 0;
     }
 }
        /// <inheritdoc/>
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.SegregateConfigSourceFile);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning segregate");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);

            // get formats
            CSVFormat inputFormat = Script
                                    .DetermineInputFormat(sourceID);
            CSVFormat outputFormat = Script.DetermineOutputFormat();

            // prepare to segregate
            bool headers = Script.ExpectInputHeaders(sourceID);
            var  seg     = new SegregateCSV {
                Script = Script
            };

            Analyst.CurrentQuantTask = seg;

            foreach (AnalystSegregateTarget target  in  Script.Segregate.SegregateTargets)
            {
                FileInfo filename = Script.ResolveFilename(target.File);
                seg.Targets.Add(new SegregateTargetPercent(filename, target.Percent));
                // mark generated
                Script.MarkGenerated(target.File);
                EncogLogging.Log(
                    EncogLogging.LevelDebug,
                    "target file:" + target.File + ", Percent: "
                    + Format.FormatPercent(target.Percent));
            }

            seg.Report = new AnalystReportBridge(Analyst);
            seg.Analyze(sourceFile, headers, inputFormat);
            seg.OutputFormat = outputFormat;

            seg.Process();
            Analyst.CurrentQuantTask = null;
            return(seg.ShouldStop());
        }
        /// <inheritdoc/>
        public override sealed bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.ClusterConfigSourceFile);
            String targetID = Prop.GetPropertyString(
                ScriptProperties.ClusterConfigTargetFile);
            int clusters = Prop.GetPropertyInt(
                ScriptProperties.ClusterConfigClusters);

            Prop.GetPropertyString(ScriptProperties.ClusterConfigType);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning cluster");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "target file:" + targetID);
            EncogLogging.Log(EncogLogging.LevelDebug, "clusters:" + clusters);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);
            FileInfo targetFile = Script.ResolveFilename(targetID);

            // get formats
            CSVFormat inputFormat = Script
                                    .DetermineInputFormat(sourceID);
            CSVFormat outputFormat = Script.DetermineOutputFormat();

            // mark generated
            Script.MarkGenerated(targetID);

            // prepare to normalize
            var cluster = new AnalystClusterCSV {
                Script = Script
            };

            Analyst.CurrentQuantTask = cluster;
            cluster.Report           = new AnalystReportBridge(Analyst);
            bool headers = Script.ExpectInputHeaders(sourceID);

            cluster.Analyze(Analyst, sourceFile, headers, inputFormat);
            cluster.OutputFormat = outputFormat;
            cluster.Process(targetFile, clusters, Analyst, DefaultIterations);
            Analyst.CurrentQuantTask = null;
            return(cluster.ShouldStop());
        }
Beispiel #25
0
        /// <summary>
        /// Called just after a training iteration.
        /// </summary>
        ///
        public void PostIteration()
        {
            if (_ready)
            {
                double currentError = _train.Error;
                _lastImprovement = (currentError - _lastError)
                                   / _lastError;
                EncogLogging.Log(EncogLogging.LevelDebug, "Last improvement: "
                                 + _lastImprovement);

                if ((_lastImprovement > 0) ||
                    (Math.Abs(_lastImprovement) < MinImprovement))
                {
                    _lastMomentum++;

                    if (_lastMomentum > MomentumCycles)
                    {
                        _lastMomentum = 0;
                        if (((int)_currentMomentum) == 0)
                        {
                            _currentMomentum = StartMomentum;
                        }
                        _currentMomentum *= (1.0d + MomentumIncrease);
                        _setter.Momentum  = _currentMomentum;
                        EncogLogging.Log(EncogLogging.LevelDebug,
                                         "Adjusting momentum: " + _currentMomentum);
                    }
                }
                else
                {
                    EncogLogging.Log(EncogLogging.LevelDebug,
                                     "Setting momentum back to zero.");

                    _currentMomentum = 0;
                    _setter.Momentum = 0;
                }
            }
            else
            {
                _ready = true;
            }
        }
Beispiel #26
0
        /// <summary>
        /// Read a packet.
        /// </summary>
        /// <returns>The packet we read.</returns>
        public IndicatorPacket ReadPacket()
        {
            try
            {
                var line = new StringBuilder();

                for (;;)
                {
                    char ch = ReadNextChar();

                    if (ch != '\n' && ch != '\r')
                    {
                        line.Append(ch);
                    }
                    else
                    {
                        break;
                    }
                }

                IList <String> list = _parseLine.Parse(line.ToString());
                _packets++;

                if (list.Count > 0)
                {
                    list[0] = list[0].ToUpper();
                }

                EncogLogging.Log(EncogLogging.LevelDebug, "Received Packet: " + line);
                return(new IndicatorPacket(list));
            }
            catch (SocketException ex)
            {
                // was it just a timeout?
                if (ex.ErrorCode == 10060)
                {
                    return(null);
                }
                throw new IndicatorError(ex);
            }
        }
        /// <summary>
        /// Perform one training iteration.
        /// </summary>
        ///
        public override sealed void Iteration()
        {
            try
            {
                PreIteration();

                _flatTraining.Iteration();
                Error = _flatTraining.Error;

                PostIteration();

                EncogLogging.Log(EncogLogging.LevelInfo,
                                 "Training iteration done, error: " + Error);
            }
            catch (IndexOutOfRangeException ex)
            {
                EncogValidate.ValidateNetworkForTraining(_network,
                                                         Training);
                throw new EncogError(ex);
            }
        }
Beispiel #28
0
        /// <summary>
        /// </summary>
        public override sealed bool ExecuteCommand(String args)
        {
            _kfold = ObtainCross();
            IMLDataSet trainingSet = ObtainTrainingSet();
            IMLMethod  method      = ObtainMethod();
            IMLTrain   trainer     = CreateTrainer(method, trainingSet);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning training");

            PerformTraining(trainer, method, trainingSet);

            String resourceID = Prop.GetPropertyString(
                ScriptProperties.MlConfigMachineLearningFile);
            FileInfo resourceFile = Analyst.Script.ResolveFilename(
                resourceID);

            method = trainer.Method;
            EncogDirectoryPersistence.SaveObject(resourceFile, method);
            EncogLogging.Log(EncogLogging.LevelDebug, "save to:" + resourceID);

            return(Analyst.ShouldStopCommand());
        }
Beispiel #29
0
        /// <inheritdoc />
        public override bool ExecuteCommand(String args)
        {
            // get filenames
            String sourceID = Prop.GetPropertyString(
                ScriptProperties.PROCESS_CONFIG_SOURCE_FILE);
            String targetID = Prop.GetPropertyString(
                ScriptProperties.PROCESS_CONFIG_TARGET_FILE);

            int forwardSize = Prop.GetPropertyInt(
                ScriptProperties.PROCESS_CONFIG_FORWARD_SIZE);
            int backwardSize = Prop.GetPropertyInt(
                ScriptProperties.PROCESS_CONFIG_BACKWARD_SIZE);

            EncogLogging.Log(EncogLogging.LevelDebug, "Beginning randomize");
            EncogLogging.Log(EncogLogging.LevelDebug, "source file:" + sourceID);
            EncogLogging.Log(EncogLogging.LevelDebug, "target file:" + targetID);

            FileInfo sourceFile = Script.ResolveFilename(sourceID);
            FileInfo targetFile = Script.ResolveFilename(targetID);

            // get formats
            CSVFormat format = Script.DetermineFormat();

            // mark generated
            Script.MarkGenerated(targetID);

            // prepare to transform
            var process = new AnalystProcess(Analyst, backwardSize, forwardSize);

            process.Script           = Script;
            Analyst.CurrentQuantTask = process;
            process.Report           = new AnalystReportBridge(Analyst);
            bool headers = Script.ExpectInputHeaders(sourceID);

            process.Analyze(sourceFile, headers, format);
            process.Process(targetFile);
            Analyst.CurrentQuantTask = null;
            return(process.ShouldStop());
        }
Beispiel #30
0
        /// <summary>
        ///     Create a trainer, use cross validation if enabled.
        /// </summary>
        /// <param name="method">The method to use.</param>
        /// <param name="trainingSet">The training set to use.</param>
        /// <returns>The trainer.</returns>
        private IMLTrain CreateTrainer(IMLMethod method,
                                       IMLDataSet trainingSet)
        {
            var factory = new MLTrainFactory();

            String type = Prop.GetPropertyString(
                ScriptProperties.MlTrainType);
            String args = Prop.GetPropertyString(
                ScriptProperties.MlTrainArguments);

            EncogLogging.Log(EncogLogging.LevelDebug, "training type:" + type);
            EncogLogging.Log(EncogLogging.LevelDebug, "training args:" + args);

            IMLTrain train = factory.Create(method, trainingSet, type, args);

            if (_kfold > 0)
            {
                train = new CrossValidationKFold(train, _kfold);
            }

            return(train);
        }