public string test(double[] testInput)
        {
            loadNetwork("save.eg");
            loadHelper("helper.hp");

            //var csv = new ReadCSV("t.csv", false, CSVFormat.DecimalPoint);
            var     line  = new String[12];
            IMLData input = helper.AllocateInputVector();


            line[0]  = testInput[0].ToString();
            line[1]  = testInput[1].ToString();
            line[2]  = testInput[2].ToString();
            line[3]  = testInput[3].ToString();
            line[4]  = testInput[4].ToString();
            line[5]  = testInput[5].ToString();
            line[6]  = testInput[6].ToString();
            line[7]  = testInput[7].ToString();
            line[8]  = testInput[8].ToString();
            line[9]  = testInput[9].ToString();
            line[10] = testInput[10].ToString();
            line[11] = testInput[11].ToString();
            //String correct = csv.Get(12);
            helper.NormalizeInputVector(line, ((BasicMLData)input).Data, false);
            IMLData output = bestMethod.Compute(input);

            return(helper.DenormalizeOutputVectorToString(output)[0]);
        }
        public double MeasurePerformance(IMLRegression network, BasicNeuralDataSet dataset, IActivationFunction activationFunction)
        {
            int         correctBits = 0;
            const float Threshold   = 0.0f;

            if (!(activationFunction is ActivationTANH))
            {
                throw new ArgumentException("Bad activation function");
            }

            int n = (int)dataset.Count;

            for (int inputIndex = 0; inputIndex < n; inputIndex++)
            {
                var actualOutputs = network.Compute(dataset.Data[inputIndex].Input);
                for (int outputIndex = 0, k = actualOutputs.Count; outputIndex < k; outputIndex++)
                {
                    if (IsBothPositiveBits(actualOutputs[outputIndex], dataset.Data[inputIndex].Ideal[outputIndex], Threshold) ||
                        IsBothNegativeBits(actualOutputs[outputIndex], dataset.Data[inputIndex].Ideal[outputIndex], Threshold))
                    {
                        correctBits++;
                    }
                }
            }

            long totalBitsCount = dataset.Count * dataset.Data[0].Ideal.Count;

            return((double)correctBits / totalBitsCount);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Process the file.
        /// </summary>
        /// <param name="outputFile">The output file.</param>
        /// <param name="method">The method to use.</param>
        public void Process(FileInfo outputFile, IMLRegression method)
        {
            var csv = new ReadCSV(InputFilename.ToString(),
                                  ExpectInputHeaders, Format);

            if (method.InputCount != _inputCount)
            {
                throw new AnalystError("This machine learning method has "
                                       + method.InputCount
                                       + " inputs, however, the data has " + _inputCount
                                       + " inputs.");
            }

            var input = new BasicMLData(method.InputCount);

            StreamWriter tw = AnalystPrepareOutputFile(outputFile);

            ResetStatus();
            while (csv.Next())
            {
                UpdateStatus(false);
                var row = new LoadedRow(csv, _idealCount);

                int dataIndex = 0;
                // load the input data
                for (int i = 0; i < _inputCount; i++)
                {
                    String str = row.Data[i];
                    double d   = Format.Parse(str);
                    input[i] = d;
                    dataIndex++;
                }

                // do we need to skip the ideal values?
                dataIndex += _idealCount;

                // compute the result
                IMLData output = method.Compute(input);

                // display the computed result
                for (int i = 0; i < _outputCount; i++)
                {
                    double d = output[i];
                    row.Data[dataIndex++] = Format.Format(d, Precision);
                }

                WriteRow(tw, row);
            }
            ReportDone(false);
            tw.Close();
            csv.Close();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Evaluate the network and display (to the console) the output for every
 /// value in the training set. Displays ideal and actual.
 /// </summary>
 /// <param name="network">The network to evaluate.</param>
 /// <param name="training">The training set to evaluate.</param>
 public static void Evaluate(IMLRegression network,
                             IMLDataSet training)
 {
     foreach (IMLDataPair pair in training)
     {
         IMLData output = network.Compute(pair.Input);
         Console.WriteLine(@"Input="
                           + FormatNeuralData(pair.Input)
                           + @", Actual=" + FormatNeuralData(output)
                           + @", Ideal="
                           + FormatNeuralData(pair.Ideal));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Calculate a regression error.
        /// </summary>
        /// <param name="method">The method to check.</param>
        /// <param name="data">The data to check.</param>
        /// <returns>The error.</returns>
        public static double CalculateRegressionError(IMLRegression method,
                                                      IMLDataSet data)
        {
            var errorCalculation = new ErrorCalculation();
            if (method is IMLContext)
                ((IMLContext)method).ClearContext();

            foreach (IMLDataPair pair in data)
            {
                IMLData actual = method.Compute(pair.Input);
                errorCalculation.UpdateError(actual, pair.Ideal, pair.Significance);
            }
            return errorCalculation.Calculate();
        }
Ejemplo n.º 6
0
 public static double CalculateRegressionError(IMLRegression method, IMLDataSet data)
 {
     ErrorCalculation calculation = new ErrorCalculation();
     if (method is IMLContext)
     {
         ((IMLContext) method).ClearContext();
     }
     foreach (IMLDataPair pair in data)
     {
         IMLData data2 = method.Compute(pair.Input);
         calculation.UpdateError(data2.Data, pair.Ideal.Data, pair.Significance);
     }
     return calculation.Calculate();
 }
Ejemplo n.º 7
0
        public static bool VerifyXOR(IMLRegression network, double tolerance)
        {
            for (int trainingSet = 0; trainingSet < XORIdeal.Length; trainingSet++)
            {
                var actual = network.Compute(new BasicMLData(XORInput[trainingSet]));

                for (var i = 0; i < XORIdeal[0].Length; i++)
                {
                    double diff = Math.Abs(actual[i] - XORIdeal[trainingSet][i]);
                    if (diff > tolerance)
                        return false;
                }
            }

            return true;
        }
Ejemplo n.º 8
0
        public static bool VerifyXOR(IMLRegression network, double tolerance)
        {
            for (int trainingSet = 0; trainingSet < XORIdeal.Length; trainingSet++)
            {
                var actual = network.Compute(new BasicMLData(XORInput[trainingSet]));

                for (var i = 0; i < XORIdeal[0].Length; i++)
                {
                    double diff = Math.Abs(actual[i] - XORIdeal[trainingSet][i]);
                    if (diff > tolerance)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// Calculate an error for a method that uses regression.
        /// </summary>
        /// <param name="method">The method to evaluate.</param>
        /// <param name="data">The training data to evaluate with.</param>
        /// <returns>The error.</returns>
        public static double CalculateError(IMLRegression method,
                                            IMLDataSet data)
        {
            var errorCalculation = new ErrorCalculation();

            // clear context
            if (method is IMLContext)
            {
                ((IMLContext) method).ClearContext();
            }

            // calculate error
            foreach (IMLDataPair pair  in  data)
            {
                IMLData actual = method.Compute(pair.Input);
                errorCalculation.UpdateError(actual, pair.Ideal, pair.Significance);
            }
            return errorCalculation.Calculate();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Called to notify the indicator that a bar has been received.
        /// </summary>
        /// <param name="packet">The packet received.</param>
        public override void NotifyPacket(IndicatorPacket packet)
        {
            long when = long.Parse(packet.Args[0]);

            if (_method == null)
            {
                if (_holder.Record(when, 2, packet.Args))
                {
                    _rowsDownloaded++;
                }
            }
            else
            {
                var input = new BasicMLData(Config.PredictWindow);

                const int fastIndex = 2;
                const int slowIndex = fastIndex + Config.InputWindow;

                for (int i = 0; i < 3; i++)
                {
                    double fast = CSVFormat.EgFormat.Parse(packet.Args[fastIndex + i]);
                    double slow = CSVFormat.EgFormat.Parse(packet.Args[slowIndex + i]);
                    double diff = _fieldDifference.Normalize((fast - slow) / Config.PipSize);
                    input[i] = _fieldDifference.Normalize(diff);
                }

                IMLData result = _method.Compute(input);

                double d = result[0];
                d = _fieldOutcome.DeNormalize(d);

                String[] args =
                {
                    "?",                                                           // line 1
                    "?",                                                           // line 2
                    CSVFormat.EgFormat.Format(d, EncogFramework.DefaultPrecision), // bar 1
                };                                                                 // arrow 2

                Link.WritePacket(IndicatorLink.PacketInd, args);
            }
        }
        public double[][] ComputeAm(IMLRegression network, List <double[][]> spectralImages, int outputCount)
        {
            int counter    = 0;
            int trackCount = spectralImages.Count;

            double[][] am = new double[trackCount][];
            foreach (double[][] spectralImagesForTrack in spectralImages)
            {
                am[counter] = new double[outputCount];
                foreach (double[] snippet in spectralImagesForTrack)
                {
                    var actualOutput = network.Compute(new BasicMLData(snippet));
                    for (int binOutputIndex = 0; binOutputIndex < outputCount; binOutputIndex++)
                    {
                        am[counter][binOutputIndex] += actualOutput[binOutputIndex] / outputCount;
                    }
                }

                counter++;
            }

            return(am);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Process the file.
        /// </summary>
        ///
        /// <param name="outputFile">The output file.</param>
        /// <param name="method">The method to use.</param>
        public void Process(FileInfo outputFile, IMLRegression method)
        {
            var csv = new ReadCSV(InputFilename.ToString(),
                                  ExpectInputHeaders, Format);

            if (method.InputCount > _inputCount)
            {
                throw new AnalystError("This machine learning method has "
                                       + method.InputCount
                                       + " inputs, however, the data has " + _inputCount
                                       + " inputs.");
            }

            var input = new BasicMLData(method.InputCount);

            StreamWriter tw = AnalystPrepareOutputFile(outputFile);

            ResetStatus();
            while (csv.Next())
            {
                UpdateStatus(false);
                var row = new LoadedRow(csv, _idealCount);

                int dataIndex = 0;
                // load the input data
                for (int i = 0; i < _inputCount; i++)
                {
                    String str = row.Data[i];
                    double d = Format.Parse(str);
                    input[i] = d;
                    dataIndex++;
                }

                // do we need to skip the ideal values?
                dataIndex += _idealCount;

                // compute the result
                IMLData output = method.Compute(input);

                // display the computed result
                for (int i = 0; i < _outputCount; i++)
                {
                    double d = output[i];
                    row.Data[dataIndex++] = Format.Format(d, Precision);
                }

                WriteRow(tw, row);
            }
            ReportDone(false);
            tw.Close();
            csv.Close();
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Evaluate the network and display (to the console) the output for every
 /// value in the training set. Displays ideal and actual.
 /// </summary>
 /// <param name="network">The network to evaluate.</param>
 /// <param name="training">The training set to evaluate.</param>
 public static void Evaluate(IMLRegression network,
                             IMLDataSet training)
 {
     foreach (IMLDataPair pair in training)
     {
         IMLData output = network.Compute(pair.Input);
         Console.WriteLine(@"Input="
                           + FormatNeuralData(pair.Input)
                           + @", Actual=" + FormatNeuralData(output)
                           + @", Ideal="
                           + FormatNeuralData(pair.Ideal));
     }
 }
Ejemplo n.º 14
0
 public void Process(FileInfo outputFile, IMLRegression method)
 {
     IMLData data;
     StreamWriter writer;
     LoadedRow row;
     int num;
     int num2;
     string str;
     double num3;
     IMLData data2;
     int num4;
     double num5;
     object[] objArray;
     ReadCSV csv = new ReadCSV(base.InputFilename.ToString(), base.ExpectInputHeaders, base.InputFormat);
     goto Label_0285;
     Label_006D:
     if (csv.Next())
     {
         base.UpdateStatus(false);
         row = new LoadedRow(csv, this._xb52d4a98fad404da);
         num = 0;
         if ((((uint) num2) | 4) == 0)
         {
             goto Label_011D;
         }
         goto Label_010D;
     }
     if ((((uint) num3) & 0) == 0)
     {
         base.ReportDone(false);
         writer.Close();
         csv.Close();
         if ((((uint) num) + ((uint) num3)) < 0)
         {
             if ((((uint) num) - ((uint) num2)) <= uint.MaxValue)
             {
                 goto Label_010D;
             }
             goto Label_00D5;
         }
     }
     if ((((uint) num) & 0) == 0)
     {
         return;
     }
     goto Label_0165;
     Label_00A6:
     if (((uint) num5) < 0)
     {
         goto Label_01F9;
     }
     num4++;
     Label_00C1:
     if (num4 < this._x98cf41c6b0eaf6ab)
     {
         num5 = data2[num4];
         row.Data[num++] = base.InputFormat.Format(num5, base.Precision);
         goto Label_00A6;
     }
     base.WriteRow(writer, row);
     goto Label_006D;
     Label_00D5:
     data2 = method.Compute(data);
     num4 = 0;
     goto Label_00C1;
     Label_00F6:
     if (num2 < this._x43f451310e815b76)
     {
         str = row.Data[num2];
         goto Label_011D;
     }
     Label_0100:
     num += this._xb52d4a98fad404da;
     goto Label_00D5;
     Label_010D:
     num2 = 0;
     goto Label_00F6;
     Label_011D:
     num3 = base.InputFormat.Parse(str);
     data[num2] = num3;
     num++;
     num2++;
     goto Label_00F6;
     Label_0165:
     if (0 != 0)
     {
         goto Label_00A6;
     }
     goto Label_006D;
     Label_01C8:
     data = new BasicMLData(method.InputCount);
     writer = this.x972236628de6c041(outputFile);
     if ((((uint) num) | 2) == 0)
     {
         goto Label_0100;
     }
     base.ResetStatus();
     goto Label_0165;
     Label_01F9:
     objArray[2] = " inputs, however, the data has ";
     if ((((uint) num4) + ((uint) num)) <= uint.MaxValue)
     {
         objArray[3] = this._x43f451310e815b76;
         objArray[4] = " inputs.";
         throw new AnalystError(string.Concat(objArray));
     }
     Label_0285:
     if (((((uint) num2) | 15) != 0) && (method.InputCount == this._x43f451310e815b76))
     {
         goto Label_01C8;
     }
     objArray = new object[5];
     objArray[0] = "This machine learning method has ";
     if ((((uint) num) - ((uint) num4)) > uint.MaxValue)
     {
         goto Label_01C8;
     }
     objArray[1] = method.InputCount;
     goto Label_01F9;
 }
Ejemplo n.º 15
0
 public static void Evaluate(IMLRegression network, IMLDataSet training)
 {
     using (IEnumerator<IMLDataPair> enumerator = training.GetEnumerator())
     {
         IMLDataPair pair;
         IMLData data;
         string[] strArray;
     Label_0009:
         if (!enumerator.MoveNext())
         {
             if (2 == 0)
             {
                 goto Label_005B;
             }
             if (0 == 0)
             {
                 return;
             }
         }
         goto Label_0095;
     Label_0026:
         strArray[4] = ", Ideal=";
     Label_002E:
         strArray[5] = x8d742ff2b6748ce6(pair.Ideal);
         Console.WriteLine(string.Concat(strArray));
         if (0 == 0)
         {
             goto Label_00A3;
         }
         goto Label_0095;
     Label_004C:
         strArray = new string[6];
         strArray[0] = "Input=";
     Label_005B:
         strArray[1] = x8d742ff2b6748ce6(pair.Input);
         strArray[2] = ", Actual=";
         strArray[3] = x8d742ff2b6748ce6(data);
         goto Label_0026;
     Label_007C:
         if (0 != 0)
         {
             goto Label_002E;
         }
         data = network.Compute(pair.Input);
         if (1 != 0)
         {
             goto Label_004C;
         }
         goto Label_0026;
     Label_0095:
         pair = enumerator.Current;
         goto Label_007C;
     Label_00A3:
         if (0xff != 0)
         {
             goto Label_0009;
         }
     }
 }
Ejemplo n.º 16
0
 public void Process(FileInfo outputFile, IMLRegression method)
 {
     IMLData data;
     StreamWriter writer;
     LoadedRow row;
     int num;
     int num2;
     double num3;
     IMLData data2;
     int num4;
     double num5;
     object[] objArray;
     ReadCSV csv = new ReadCSV(base.InputFilename.ToString(), base.ExpectInputHeaders, base.InputFormat);
     goto Label_028E;
     Label_0022:
     if (csv.Next())
     {
         base.UpdateStatus(false);
         row = new LoadedRow(csv, this._xb52d4a98fad404da);
         num = 0;
         if ((((uint) num3) + ((uint) num4)) < 0)
         {
             goto Label_0208;
         }
         num2 = 0;
     }
     else
     {
         if ((((uint) num5) + ((uint) num2)) < 0)
         {
             goto Label_026E;
         }
         base.ReportDone(false);
         writer.Close();
         csv.Close();
         if (0 == 0)
         {
             return;
         }
         goto Label_028E;
     }
     Label_0125:
     if (num2 < this._x43f451310e815b76)
     {
         string str = row.Data[num2];
         num3 = base.InputFormat.Parse(str);
         data[num2] = num3;
         if ((((uint) num5) + ((uint) num3)) <= uint.MaxValue)
         {
             goto Label_0148;
         }
         goto Label_0196;
     }
     num += this._xb52d4a98fad404da;
     Label_013A:
     data2 = method.Compute(data);
     num4 = 0;
     goto Label_0196;
     Label_0148:
     num++;
     num2++;
     if ((((uint) num) - ((uint) num3)) > uint.MaxValue)
     {
         goto Label_01EC;
     }
     goto Label_0125;
     Label_0196:
     if ((((uint) num3) + ((uint) num3)) >= 0)
     {
     Label_008D:
         if (num4 >= this._x98cf41c6b0eaf6ab)
         {
             base.WriteRow(writer, row);
         }
         else
         {
             num5 = data2[num4];
             if ((((uint) num2) - ((uint) num)) < 0)
             {
                 goto Label_0125;
             }
             if ((((uint) num2) | 15) != 0)
             {
                 row.Data[num++] = base.InputFormat.Format(num5, base.Precision);
                 num4++;
                 goto Label_008D;
             }
         }
         if (((uint) num3) < 0)
         {
             if (((uint) num5) <= uint.MaxValue)
             {
                 goto Label_0125;
             }
             goto Label_0148;
         }
     }
     goto Label_02BE;
     Label_01EC:
     base.ResetStatus();
     goto Label_0022;
     Label_0208:
     data = new BasicMLData(method.InputCount);
     writer = this.x972236628de6c041(outputFile);
     if ((((uint) num5) + ((uint) num4)) >= 0)
     {
         goto Label_01EC;
     }
     goto Label_0125;
     Label_026E:
     objArray[4] = " inputs.";
     if (((uint) num2) < 0)
     {
         goto Label_013A;
     }
     if (((uint) num) >= 0)
     {
         throw new AnalystError(string.Concat(objArray));
     }
     goto Label_02BE;
     Label_028E:
     if (method.InputCount <= this._x43f451310e815b76)
     {
         goto Label_0208;
     }
     objArray = new object[5];
     objArray[0] = "This machine learning method has ";
     objArray[1] = method.InputCount;
     objArray[2] = " inputs, however, the data has ";
     objArray[3] = this._x43f451310e815b76;
     goto Label_026E;
     Label_02BE:
     if ((((uint) num) - ((uint) num2)) <= uint.MaxValue)
     {
         goto Label_0022;
     }
 }