Exemplo n.º 1
0
        /// <summary>
        /// Processes a double array of data of input and a second array of data for ideals
        /// you must input the input and output size.
        /// this typically builds a supervised IMLDatapair, which you must add to a IMLDataset.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="ideal">The ideal.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataPair ProcessPairs(double[] data, double[] ideal, int _inputWindow, int _predictWindow)
        {
            var result = new BasicMLDataSet();

            for (int i = 0; i < data.Length; i++)
            {
                var inputData = new BasicMLData(_inputWindow);
                var idealData = new BasicMLData(_predictWindow);
                int index     = i;
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }
                index = 0;
                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = ideal[index++];
                }
                IMLDataPair pair = new BasicMLDataPair(inputData, idealData);
                return(pair);
            }
            return(null);
        }
        /// <summary>
        /// Process the array.
        /// </summary>
        ///
        /// <param name="data">The array to process.</param>
        /// <returns>A neural data set that contains the time-series.</returns>
        public IMLDataSet Process(double[] data)
        {
            var result = new BasicMLDataSet();

            int totalWindowSize = _inputWindow + _predictWindow;
            int stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                var inputData = new BasicMLData(_inputWindow);
                var idealData = new BasicMLData(_predictWindow);

                int index = i;

                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }

                var pair = new BasicMLDataPair(inputData, idealData);
                result.Add(pair);
            }

            return(result);
        }
Exemplo n.º 3
0
    public void GetResults()
    {
        foreach (Texture2D texture in OpenFile.Open())
        {
            IMLDataPair pair = new BasicMLDataPair(new BasicMLData(Texture2List(texture).ToArray()));

            IMLData output = basicNetwork.Compute(pair.Input);

            string result = "";
            float  ideal  = 0;

            if (output[0] < 0.25f)
            {
                result = "Normal";
            }
            else if (output[0] > 0.25f && output[0] < 0.75f)
            {
                result = "Esquemico";
                ideal  = 0.5f;
            }
            else
            {
                result = "Hemorragico";
                ideal  = 1.0f;
            }

            Debug.Log("Isso parece ser: " + result + "\nEquivalência{ Imagem Testada: " + output[0] + " Imagem Original: " + ideal + " }");
            debugText.text = ("Isso parece ser: " + result + "\nEquivalência{ Imagem Testada: " + output[0] + " Imagem Original: " + ideal + " }");
        }
    }
Exemplo n.º 4
0
        /// <summary>
        /// Load a CSV file into a memory dataset.
        /// </summary>
        ///
        /// <param name="format">The CSV format to use.</param>
        /// <param name="filename">The filename to load.</param>
        /// <param name="headers">True if there is a header line.</param>
        /// <param name="inputSize">The input size.  Input always comes first in a file.</param>
        /// <param name="idealSize">The ideal size, 0 for unsupervised.</param>
        /// <returns>A NeuralDataSet that holds the contents of the CSV file.</returns>
        public static IMLDataSet LoadCSVTOMemory(CSVFormat format, String filename,
                                                 bool headers, int inputSize, int idealSize)
        {
            var result = new BasicMLDataSet();
            var csv    = new ReadCSV(filename, headers, format);

            while (csv.Next())
            {
                BasicMLData ideal = null;
                int         index = 0;

                var input = new BasicMLData(inputSize);
                for (int i = 0; i < inputSize; i++)
                {
                    double d = csv.GetDouble(index++);
                    input[i] = d;
                }

                if (idealSize > 0)
                {
                    ideal = new BasicMLData(idealSize);
                    for (int i = 0; i < idealSize; i++)
                    {
                        double d = csv.GetDouble(index++);
                        ideal[i] = d;
                    }
                }

                IMLDataPair pair = new BasicMLDataPair(input, ideal);
                result.Add(pair);
            }

            return(result);
        }
        /// <summary>
        /// Process the data array and returns an IMLdatapair.
        /// </summary>
        ///
        /// <param name="data">The array to process.</param>
        /// <returns>An IMLDatapair containing data.</returns>
        public IMLDataPair ProcessToPair(double[] data)
        {
            // not sure this method works right: it's only using the last pair?
            IMLDataPair pair            = null;
            int         totalWindowSize = _inputWindow + _predictWindow;
            int         stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                var inputData = new BasicMLData(_inputWindow);
                var idealData = new BasicMLData(_predictWindow);

                int index = i;

                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }

                pair = new BasicMLDataPair(inputData, idealData);
            }
            return(pair);
        }
        /// <summary>
        /// Evaluate memory.
        /// </summary>
        private void EvalMemory()
        {
            BasicMLDataSet training = RandomTrainingFactory.Generate(
                1000, 10000, 10, 10, -1, 1);

            const long stop   = (10 * Evaluate.Milis);
            int        record = 0;

            IMLDataPair pair = BasicMLDataPair.CreatePair(10, 10);

            int iterations = 0;
            var watch      = new Stopwatch();

            watch.Start();
            while (watch.ElapsedMilliseconds < stop)
            {
                iterations++;
                training.GetRecord(record++, pair);
                if (record >= training.Count)
                {
                    record = 0;
                }
            }

            iterations /= 100000;

            _report.Report(Steps, Step2,
                           "Memory dataset, result: " + Format.FormatInteger(iterations));

            _memoryScore = iterations;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Process the data array and returns an IMLdatapair.
        /// </summary>
        ///
        /// <param name="data">The array to process.</param>
        /// <returns>An IMLDatapair containing data.</returns>
        public IMLDataPair ProcessToPair(double[] data)
        {
            IMLDataPair pair            = null;
            int         totalWindowSize = _inputWindow + _predictWindow;
            int         stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                IMLData inputData = new BasicMLData(_inputWindow);
                IMLData idealData = new BasicMLData(_predictWindow);

                int index = i;

                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }

                pair = new BasicMLDataPair(inputData, idealData);
            }
            return(pair);
        }
        /// <summary>
        /// Processes the specified double serie into an IMLDataset.
        /// To use this method, you must provide a formated double array.
        /// The number of points in the input window makes the input array , and the predict window will create the array used in ideal.
        /// Example you have an array with 1, 2, 3 , 4 , 5.
        /// You can use this method to make an IMLDataset 4 inputs and 1 ideal (5).
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataSet ProcessDoubleSerieIntoIMLDataset(double[] data, int _inputWindow, int _predictWindow)
        {
            IMLDataSet result          = new BasicMLDataSet();
            int        totalWindowSize = _inputWindow + _predictWindow;
            int        stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                IMLData inputData = new BasicMLData(_inputWindow);
                IMLData idealData = new BasicMLData(_predictWindow);
                int     index     = i;
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }
                IMLDataPair pair = new BasicMLDataPair(inputData, idealData);
                result.Add(pair);
            }

            return(result);
        }
        /// <summary>
        /// Construct a gradient worker.
        /// </summary>
        ///
        /// <param name="theNetwork">The network to train.</param>
        /// <param name="theOwner">The owner that is doing the training.</param>
        /// <param name="theTraining">The training data.</param>
        /// <param name="theLow">The low index to use in the training data.</param>
        /// <param name="theHigh">The high index to use in the training data.</param>
        /// <param name="theFlatSpots">Holds an array of flat spot constants.</param>
        public GradientWorker(FlatNetwork theNetwork,
                              Propagation theOwner, IMLDataSet theTraining,
                              int theLow, int theHigh, double[] theFlatSpots, IErrorFunction ef)
        {
            _errorCalculation = new ErrorCalculation();
            _network          = theNetwork;
            _training         = theTraining;
            _low      = theLow;
            _high     = theHigh;
            _owner    = theOwner;
            _flatSpot = theFlatSpots;

            _layerDelta = new double[_network.LayerOutput.Length];
            _gradients  = new double[_network.Weights.Length];
            _actual     = new double[_network.OutputCount];

            _weights         = _network.Weights;
            _layerIndex      = _network.LayerIndex;
            _layerCounts     = _network.LayerCounts;
            _weightIndex     = _network.WeightIndex;
            _layerOutput     = _network.LayerOutput;
            _layerSums       = _network.LayerSums;
            _layerFeedCounts = _network.LayerFeedCounts;
            _ef = ef;

            _pair = BasicMLDataPair.CreatePair(_network.InputCount,
                                               _network.OutputCount);
        }
        /// <summary>
        /// Generate a random training set.
        /// </summary>
        /// <param name="seed">The seed value to use, the same seed value will always produce
        /// the same results.</param>
        /// <param name="count">How many training items to generate.</param>
        /// <param name="inputCount">How many input numbers.</param>
        /// <param name="idealCount">How many ideal numbers.</param>
        /// <param name="min">The minimum random number.</param>
        /// <param name="max">The maximum random number.</param>
        /// <returns>The random training set.</returns>
        public static BasicMLDataSet Generate(long seed,
                                              int count, int inputCount,
                                              int idealCount, double min, double max)
        {
            var rand =
                new LinearCongruentialGenerator(seed);

            var result = new BasicMLDataSet();

            for (int i = 0; i < count; i++)
            {
                var inputData = new BasicMLData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData[j] = rand.Range(min, max);
                }

                var idealData = new BasicMLData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                var pair = new BasicMLDataPair(inputData,
                                               idealData);
                result.Add(pair);
            }
            return(result);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Processes the specified double serie into an IMLDataset.
        /// To use this method, you must provide a formated double array with the input data and the ideal data in another double array.
        /// The number of points in the input window makes the input array , and the predict window will create the array used in ideal.
        /// This method will use ALL the data inputs and ideals you have provided.
        /// </summary>
        /// <param name="datainput">The datainput.</param>
        /// <param name="ideals">The ideals.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataSet ProcessDoubleSerieIntoIMLDataset(List <double> datainput, List <double> ideals, int _inputWindow, int _predictWindow)
        {
            var result = new BasicMLDataSet();
            //int count = 0;
            ////lets check if there is a modulo , if so we move forward in the List of doubles in inputs.This is just a check
            ////as the data of inputs should be able to fill without having .
            //while (datainput.Count % _inputWindow !=0)
            //{
            //    count++;
            //}
            var inputData = new BasicMLData(_inputWindow);
            var idealData = new BasicMLData(_predictWindow);

            foreach (double d in datainput)
            {
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = d;
                }
            }
            foreach (double ideal in ideals)
            {
                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = ideal;
                }
            }
            var pair = new BasicMLDataPair(inputData, idealData);

            result.Add(pair);
            return(result);
        }
        /// <summary>
        /// Generate random training into a training set.
        /// </summary>
        /// <param name="training">The training set to generate into.</param>
        /// <param name="seed">The seed to use.</param>
        /// <param name="count">How much data to generate.</param>
        /// <param name="min">The low random value.</param>
        /// <param name="max">The high random value.</param>
        public static void Generate(IMLDataSetAddable training,
                                    long seed,
                                    int count,
                                    double min, double max)
        {
            var rand
                = new LinearCongruentialGenerator(seed);

            int inputCount = training.InputSize;
            int idealCount = training.IdealSize;

            for (int i = 0; i < count; i++)
            {
                var inputData = new BasicMLData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData[j] = rand.Range(min, max);
                }

                var idealData = new BasicMLData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                var pair = new BasicMLDataPair(inputData,
                                               idealData);
                training.Add(pair);
            }
        }
Exemplo n.º 13
0
        /// <inheritdoc/>
        public void Write(double[] input, double[] ideal, double significance)
        {
            IMLDataPair pair = BasicMLDataPair.CreatePair(_inputSize,
                                                          _idealSize);

            EngineArray.ArrayCopy(input, pair.Input.Data);
            EngineArray.ArrayCopy(ideal, pair.Ideal.Data);
            pair.Significance = significance;
        }
        /// <summary>
        /// Evaluate disk.
        /// </summary>
        private void EvalBinary()
        {
            FileInfo file = FileUtil.CombinePath(new FileInfo(Path.GetTempPath()), "temp.egb");

            BasicMLDataSet training = RandomTrainingFactory.Generate(
                1000, 10000, 10, 10, -1, 1);

            // create the binary file

            if (file.Exists)
            {
                file.Delete();
            }

            var training2 = new BufferedMLDataSet(file.ToString());

            training2.Load(training);

            const long stop   = (10 * Evaluate.Milis);
            int        record = 0;

            IMLDataPair pair = BasicMLDataPair.CreatePair(10, 10);

            var watch = new Stopwatch();

            watch.Start();

            int iterations = 0;

            while (watch.ElapsedMilliseconds < stop)
            {
                iterations++;
                training2.GetRecord(record++, pair);
                if (record >= training2.Count)
                {
                    record = 0;
                }
            }

            training2.Close();

            iterations /= 100000;

            _report.Report(Steps, Step3,
                           "Disk(binary) dataset, result: "
                           + Format.FormatInteger(iterations));

            if (file.Exists)
            {
                file.Delete();
            }
            _binaryScore = iterations;
        }
 /// <summary>
 /// Move to the next record.
 /// </summary>
 /// <returns>True, if we were able to move to the next record.</returns>
 public bool MoveNext()
 {
     if (HasNext())
     {
         IMLDataPair pair = BasicMLDataPair.CreatePair(
             _owner.InputSize, _owner.IdealSize);
         _owner.GetRecord(_currentIndex++, pair);
         _currentPair = pair;
         return(true);
     }
     _currentPair = null;
     return(false);
 }
        /// <summary>
        /// Get the minimum, over all the data, for the specified index.
        /// </summary>
        ///
        /// <param name="index">An index into the input data.</param>
        /// <returns>The minimum value.</returns>
        private double GetMinValue(int index)
        {
            double      result = Double.MaxValue;
            long        count  = _set.Count;
            IMLDataPair pair   = BasicMLDataPair.CreatePair(
                _set.InputSize, _set.IdealSize);

            for (int i = 0; i < count; i++)
            {
                _set.GetRecord(index, pair);
                result = Math.Min(result, pair.InputArray[index]);
            }
            return(result);
        }
Exemplo n.º 17
0
            public void Update(State s, double value)
            {
                var pair        = new BasicMLDataPair(new BasicMLData(s.AsDoubles()), new BasicMLData(new double[] { value }));
                var trainingSet = new BasicMLDataSet(new[] { pair });

                var train = new Encog.Neural.Networks.Training.Propagation.Back.Backpropagation(_network, trainingSet);

                train.BatchSize = 1;

                train.Iteration(2);
                if (train.Error > 0.01)
                {
                    train.Iteration(3);
                }
            }
Exemplo n.º 18
0
        /// <summary>
        /// Calculate the error for this neural network. The error is calculated
        /// using root-mean-square(RMS).
        /// </summary>
        ///
        /// <param name="data">The training set.</param>
        /// <returns>The error percentage.</returns>
        public double CalculateError(IMLDataSet data)
        {
            var errorCalculation = new ErrorCalculation();

            var         actual = new double[_outputCount];
            IMLDataPair pair   = BasicMLDataPair.CreatePair(data.InputSize,
                                                            data.IdealSize);

            for (int i = 0; i < data.Count; i++)
            {
                data.GetRecord(i, pair);
                Compute(pair.InputArray, actual);
                errorCalculation.UpdateError(actual, pair.IdealArray, pair.Significance);
            }
            return(errorCalculation.Calculate());
        }
Exemplo n.º 19
0
        /// <summary>
        /// Convert an external file format, such as CSV, to an Encog memory training set.
        /// </summary>
        public IMLDataSet External2Memory()
        {
            Status.Report(0, 0, "Importing to memory");

            if (Result == null)
            {
                Result = new BasicMLDataSet();
            }

            var input = new double[_codec.InputSize];
            var ideal = new double[_codec.IdealSize];

            _codec.PrepareRead();

            int    currentRecord = 0;
            int    lastUpdate    = 0;
            double significance  = 1.0;

            while (_codec.Read(input, ideal, ref significance))
            {
                IMLData b = null;

                IMLData a = new BasicMLData(input);

                if (_codec.IdealSize > 0)
                {
                    b = new BasicMLData(ideal);
                }

                IMLDataPair pair = new BasicMLDataPair(a, b);
                pair.Significance = significance;
                Result.Add(pair);

                currentRecord++;
                lastUpdate++;
                if (lastUpdate >= 10000)
                {
                    lastUpdate = 0;
                    Status.Report(0, currentRecord, "Importing...");
                }
            }

            _codec.Close();
            Status.Report(0, 0, "Done importing to memory");
            return(Result);
        }
Exemplo n.º 20
0
        public static IMLDataSet CreateNoisyXORDataSet(int count)
        {
            var result = new BasicMLDataSet();

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    var inputData = new BasicMLData(XORInput[j]);
                    var idealData = new BasicMLData(XORIdeal[j]);
                    var pair      = new BasicMLDataPair(inputData, idealData);
                    inputData[0] = inputData[0] + RangeRandomizer.Randomize(-0.1, 0.1);
                    inputData[1] = inputData[1] + RangeRandomizer.Randomize(-0.1, 0.1);
                    result.Add(pair);
                }
            }
            return(result);
        }
Exemplo n.º 21
0
        /// <inheritdoc/>
        public IMLDataPair this[int x]
        {
            get
            {
                var input = new double[InputSize];
                var ideal = new double[IdealSize];

                _egb.SetLocation(x);
                _egb.Read(input);
                _egb.Read(ideal);

                var inputData = new BasicMLData(input, false);
                var idealData = new BasicMLData(ideal, false);

                var result = new BasicMLDataPair(inputData, idealData);
                result.Significance = _egb.Read();
                return(result);
            }
        }
        /// <summary>
        /// Move to the next element.
        /// </summary>
        /// <returns>True if there are more elements to read.</returns>
        public bool MoveNext()
        {
            try
            {
                if (_current >= _data.Count)
                {
                    return(false);
                }

                _currentRecord = BasicMLDataPair.CreatePair(_data
                                                            .InputSize, _data.IdealSize);
                _data.GetRecord(_current++, _currentRecord);
                return(true);
            }
            catch (EndOfStreamException)
            {
                return(false);
            }
        }
Exemplo n.º 23
0
        /// <inheritdoc />
        public IMLDataPair this[int index]
        {
            get
            {
                if (index > Count)
                {
                    return(null);
                }

                var input = new BasicMLData(
                    CalculatedInputSize * CalculateLagCount());
                var ideal = new BasicMLData(
                    CalculatedIdealSize * CalculateLeadCount());
                IMLDataPair pair = new BasicMLDataPair(input, ideal);

                GetRecord(index, pair);

                return(pair);
            }
        }
        /// <summary>
        /// Construct the chain rule worker.
        /// </summary>
        /// <param name="theNetwork">The network to calculate a Hessian for.</param>
        /// <param name="theTraining">The training data.</param>
        /// <param name="theLow">The low range.</param>
        /// <param name="theHigh">The high range.</param>
        public ChainRuleWorker(FlatNetwork theNetwork, IMLDataSet theTraining, int theLow, int theHigh)
        {
            int weightCount = theNetwork.Weights.Length;

            _training = theTraining;
            _flat     = theNetwork;

            _layerDelta = new double[_flat.LayerOutput.Length];
            _actual     = new double[_flat.OutputCount];
            _derivative = new double[weightCount];
            _totDeriv   = new double[weightCount];
            _gradients  = new double[weightCount];

            _weights         = _flat.Weights;
            _layerIndex      = _flat.LayerIndex;
            _layerCounts     = _flat.LayerCounts;
            _weightIndex     = _flat.WeightIndex;
            _layerOutput     = _flat.LayerOutput;
            _layerSums       = _flat.LayerSums;
            _layerFeedCounts = _flat.LayerFeedCounts;
            _low             = theLow;
            _high            = theHigh;
            _pair            = BasicMLDataPair.CreatePair(_flat.InputCount, _flat.OutputCount);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Loads variables inputs and one ideal double series into an imldataset.
        /// </summary>
        /// <param name="idealsinputs"></param>
        /// <param name="WindoSize"></param>
        /// <param name="pparamInputs"></param>
        /// <returns></returns>
        public static Tuple <IMLDataSet, NormalizeArray> Load(double[] idealsinputs, int WindoSize, params double[][] pparamInputs)
        {
            try
            {
                var finalSet = new BasicMLDataSet();
                //We make a dic with the count of inputs being the number of double series we are sending in.
                Dictionary <int, double[]> inputsDics = new Dictionary <int, double[]>(pparamInputs.Length);
                int indexS = 0;
                //We make a normalizeArray which we will return as a tuple ready for denormalization.
                NormalizeArray Normer = new NormalizeArray(-1, 1);
                //Process each inputs.
                foreach (double[] doubleSeries in pparamInputs)
                {
                    inputsDics.Add(indexS++, Normer.Process(doubleSeries));
                }
                //Process the ideals.
                var idealNormed = Normer.Process(idealsinputs);

                //Make a list which will hold the inputs one after the others
                List <double> dtda         = new List <double>();
                int           listindex    = 0;
                int           currentindex = 0;
                //starts from zero so count -1..
                int dicinputsCount = inputsDics.Keys.Count - 1;
                //Process the input normed.
                foreach (double d in inputsDics[0])
                {
                    if (currentindex++ < WindoSize)
                    {
                        dtda.Add(d);
                        //we put all the fields which are in the dic.
                        while (dicinputsCount > 0)
                        {
                            dtda.Add(inputsDics[dicinputsCount--][listindex]);
                        }
                        //We reset the field count for a later pass.
                        dicinputsCount = inputsDics.Keys.Count - 1;
                    }

                    if (currentindex == WindoSize)
                    {
                        //Make an imldata pair, and add it to the imldataset...reset the temp list of inputs...
                        var pair = new BasicMLDataPair(
                            new BasicMLData(dtda.ToArray()),
                            new BasicMLData(new double[] { idealNormed[listindex] }));
                        currentindex = 0;
                        dtda.Clear();
                        finalSet.Add(pair);
                    }
                    //Lets increment the indexes..
                    listindex++;
                }
                //Return the dataset and the normalization array..
                return(new Tuple <IMLDataSet, NormalizeArray>(finalSet, Normer));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Got an error : ", ex);
                throw new Exception("Error parsing points....");
            }
        }
        /// <summary>
        /// Compute the derivative for target data.
        /// </summary>
        ///
        /// <param name="input">The input.</param>
        /// <param name="target">The target data.</param>
        /// <returns>The output.</returns>
        public IMLData ComputeDeriv(IMLData input, IMLData target)
        {
            int    pop, ivar;
            int    ibest = 0;
            int    outvar;
            double dist, truedist;
            double vtot, wtot;
            double temp, der1, der2, psum;
            int    vptr, wptr, vsptr = 0, wsptr = 0;

            var xout = new double[_network.OutputCount];

            for (pop = 0; pop < _network.OutputCount; pop++)
            {
                xout[pop] = 0.0d;
                for (ivar = 0; ivar < _network.InputCount; ivar++)
                {
                    _v[pop * _network.InputCount + ivar] = 0.0d;
                    _w[pop * _network.InputCount + ivar] = 0.0d;
                }
            }

            psum = 0.0d;

            if (_network.OutputMode != PNNOutputMode.Classification)
            {
                vsptr = _network.OutputCount
                        * _network.InputCount;
                wsptr = _network.OutputCount
                        * _network.InputCount;
                for (ivar = 0; ivar < _network.InputCount; ivar++)
                {
                    _v[vsptr + ivar] = 0.0d;
                    _w[wsptr + ivar] = 0.0d;
                }
            }

            IMLDataPair pair = BasicMLDataPair.CreatePair(_network.Samples.InputSize, _network.Samples.IdealSize);

            for (int r = 0; r < _network.Samples.Count; r++)
            {
                _network.Samples.GetRecord(r, pair);

                if (r == _network.Exclude)
                {
                    continue;
                }

                dist = 0.0d;
                for (ivar = 0; ivar < _network.InputCount; ivar++)
                {
                    double diff = input[ivar] - pair.Input[ivar];
                    diff       /= _network.Sigma[ivar];
                    _dsqr[ivar] = diff * diff;
                    dist       += _dsqr[ivar];
                }

                if (_network.Kernel == PNNKernelType.Gaussian)
                {
                    dist = Math.Exp(-dist);
                }
                else if (_network.Kernel == PNNKernelType.Reciprocal)
                {
                    dist = 1.0d / (1.0d + dist);
                }

                truedist = dist;
                if (dist < 1.0e-40d)
                {
                    dist = 1.0e-40d;
                }

                if (_network.OutputMode == PNNOutputMode.Classification)
                {
                    pop        = (int)pair.Ideal[0];
                    xout[pop] += dist;
                    vptr       = pop * _network.InputCount;
                    wptr       = pop * _network.InputCount;
                    for (ivar = 0; ivar < _network.InputCount; ivar++)
                    {
                        temp             = truedist * _dsqr[ivar];
                        _v[vptr + ivar] += temp;
                        _w[wptr + ivar] += temp * (2.0d * _dsqr[ivar] - 3.0d);
                    }
                }

                else if (_network.OutputMode == PNNOutputMode.Unsupervised)
                {
                    for (ivar = 0; ivar < _network.InputCount; ivar++)
                    {
                        xout[ivar]       += dist * pair.Input[ivar];
                        temp              = truedist * _dsqr[ivar];
                        _v[vsptr + ivar] += temp;
                        _w[wsptr + ivar] += temp
                                            * (2.0d * _dsqr[ivar] - 3.0d);
                    }
                    vptr = 0;
                    wptr = 0;
                    for (outvar = 0; outvar < _network.OutputCount; outvar++)
                    {
                        for (ivar = 0; ivar < _network.InputCount; ivar++)
                        {
                            temp = truedist * _dsqr[ivar]
                                   * pair.Input[ivar];
                            _v[vptr++] += temp;
                            _w[wptr++] += temp * (2.0d * _dsqr[ivar] - 3.0d);
                        }
                    }
                    psum += dist;
                }
                else if (_network.OutputMode == PNNOutputMode.Regression)
                {
                    for (ivar = 0; ivar < _network.OutputCount; ivar++)
                    {
                        xout[ivar] += dist * pair.Ideal[ivar];
                    }
                    vptr = 0;
                    wptr = 0;
                    for (outvar = 0; outvar < _network.OutputCount; outvar++)
                    {
                        for (ivar = 0; ivar < _network.InputCount; ivar++)
                        {
                            temp = truedist * _dsqr[ivar]
                                   * pair.Ideal[outvar];
                            _v[vptr++] += temp;
                            _w[wptr++] += temp * (2.0d * _dsqr[ivar] - 3.0d);
                        }
                    }
                    for (ivar = 0; ivar < _network.InputCount; ivar++)
                    {
                        temp              = truedist * _dsqr[ivar];
                        _v[vsptr + ivar] += temp;
                        _w[wsptr + ivar] += temp
                                            * (2.0d * _dsqr[ivar] - 3.0d);
                    }
                    psum += dist;
                }
            }

            if (_network.OutputMode == PNNOutputMode.Classification)
            {
                psum = 0.0d;
                for (pop = 0; pop < _network.OutputCount; pop++)
                {
                    if (_network.Priors[pop] >= 0.0d)
                    {
                        xout[pop] *= _network.Priors[pop]
                                     / _network.CountPer[pop];
                    }
                    psum += xout[pop];
                }

                if (psum < 1.0e-40d)
                {
                    psum = 1.0e-40d;
                }
            }

            for (pop = 0; pop < _network.OutputCount; pop++)
            {
                xout[pop] /= psum;
            }

            for (ivar = 0; ivar < _network.InputCount; ivar++)
            {
                if (_network.OutputMode == PNNOutputMode.Classification)
                {
                    vtot = wtot = 0.0d;
                }
                else
                {
                    vtot = _v[vsptr + ivar] * 2.0d
                           / (psum * _network.Sigma[ivar]);
                    wtot = _w[wsptr + ivar]
                           * 2.0d
                           / (psum * _network.Sigma[ivar] * _network.Sigma[ivar]);
                }

                for (outvar = 0; outvar < _network.OutputCount; outvar++)
                {
                    if ((_network.OutputMode == PNNOutputMode.Classification) &&
                        (_network.Priors[outvar] >= 0.0d))
                    {
                        _v[outvar * _network.InputCount + ivar] *= _network.Priors[outvar]
                                                                   / _network.CountPer[outvar];
                        _w[outvar * _network.InputCount + ivar] *= _network.Priors[outvar]
                                                                   / _network.CountPer[outvar];
                    }
                    _v[outvar * _network.InputCount + ivar] *= 2.0d / (psum * _network.Sigma[ivar]);

                    _w[outvar * _network.InputCount + ivar] *= 2.0d / (psum
                                                                       * _network.Sigma[ivar] * _network.Sigma[ivar]);
                    if (_network.OutputMode == PNNOutputMode.Classification)
                    {
                        vtot += _v[outvar * _network.InputCount + ivar];
                        wtot += _w[outvar * _network.InputCount + ivar];
                    }
                }

                for (outvar = 0; outvar < _network.OutputCount; outvar++)
                {
                    der1 = _v[outvar * _network.InputCount + ivar]
                           - xout[outvar] * vtot;
                    der2 = _w[outvar * _network.InputCount + ivar]
                           + 2.0d * xout[outvar] * vtot * vtot - 2.0d
                           * _v[outvar * _network.InputCount + ivar]
                           * vtot - xout[outvar] * wtot;
                    if (_network.OutputMode == PNNOutputMode.Classification)
                    {
                        if (outvar == (int)target[0])
                        {
                            temp = 2.0d * (xout[outvar] - 1.0d);
                        }
                        else
                        {
                            temp = 2.0d * xout[outvar];
                        }
                    }
                    else
                    {
                        temp = 2.0d * (xout[outvar] - target[outvar]);
                    }
                    _network.Deriv[ivar]  += temp * der1;
                    _network.Deriv2[ivar] += temp * der2 + 2.0d * der1
                                             * der1;
                }
            }

            if (_network.OutputMode == PNNOutputMode.Classification)
            {
                IMLData result = new BasicMLData(1);
                result[0] = ibest;
                return(result);
            }

            return(null);
        }
        /// <summary>
        /// Calculate the error for the entire training set.
        /// </summary>
        ///
        /// <param name="training">Training set to use.</param>
        /// <param name="deriv">Should we find the derivative.</param>
        /// <returns>The error.</returns>
        public double CalculateError(IMLDataSet training,
                                     bool deriv)
        {
            double totErr;
            double diff;

            totErr = 0.0d;

            if (deriv)
            {
                int num = (_network.SeparateClass)
                              ? _network.InputCount * _network.OutputCount
                              : _network.InputCount;
                for (int i = 0; i < num; i++)
                {
                    _network.Deriv[i]  = 0.0d;
                    _network.Deriv2[i] = 0.0d;
                }
            }

            _network.Exclude = (int)training.Count;

            IMLDataPair pair = BasicMLDataPair.CreatePair(
                training.InputSize, training.IdealSize);

            var xout = new double[_network.OutputCount];

            for (int r = 0; r < training.Count; r++)
            {
                training.GetRecord(r, pair);
                _network.Exclude = _network.Exclude - 1;

                double err = 0.0d;

                IMLData input  = pair.Input;
                IMLData target = pair.Ideal;

                if (_network.OutputMode == PNNOutputMode.Unsupervised)
                {
                    if (deriv)
                    {
                        IMLData output = ComputeDeriv(input, target);
                        for (int z = 0; z < _network.OutputCount; z++)
                        {
                            xout[z] = output[z];
                        }
                    }
                    else
                    {
                        IMLData output = _network.Compute(input);
                        for (int z = 0; z < _network.OutputCount; z++)
                        {
                            xout[z] = output[z];
                        }
                    }
                    for (int i = 0; i < _network.OutputCount; i++)
                    {
                        diff = input[i] - xout[i];
                        err += diff * diff;
                    }
                }
                else if (_network.OutputMode == PNNOutputMode.Classification)
                {
                    var     tclass = (int)target[0];
                    IMLData output;

                    if (deriv)
                    {
                        output = ComputeDeriv(input, pair.Ideal);
                        //output_4.GetData(0); //**FIX**?
                    }
                    else
                    {
                        output = _network.Compute(input);
                        //output_4.GetData(0); **FIX**?
                    }

                    xout[0] = output[0];

                    for (int i = 0; i < xout.Length; i++)
                    {
                        if (i == tclass)
                        {
                            diff = 1.0d - xout[i];
                            err += diff * diff;
                        }
                        else
                        {
                            err += xout[i] * xout[i];
                        }
                    }
                }

                else if (_network.OutputMode == PNNOutputMode.Regression)
                {
                    if (deriv)
                    {
                        IMLData output = _network.Compute(input);
                        for (int z = 0; z < _network.OutputCount; z++)
                        {
                            xout[z] = output[z];
                        }
                    }
                    else
                    {
                        IMLData output = _network.Compute(input);
                        for (int z = 0; z < _network.OutputCount; z++)
                        {
                            xout[z] = output[z];
                        }
                    }
                    for (int i = 0; i < _network.OutputCount; i++)
                    {
                        diff = target[i] - xout[i];
                        err += diff * diff;
                    }
                }

                totErr += err;
            }

            _network.Exclude = -1;

            _network.Error = totErr / training.Count;
            if (deriv)
            {
                for (int i = 0; i < _network.Deriv.Length; i++)
                {
                    _network.Deriv[i]  /= training.Count;
                    _network.Deriv2[i] /= training.Count;
                }
            }

            if ((_network.OutputMode == PNNOutputMode.Unsupervised) ||
                (_network.OutputMode == PNNOutputMode.Regression))
            {
                _network.Error = _network.Error
                                 / _network.OutputCount;
                if (deriv)
                {
                    for (int i = 0; i < _network.InputCount; i++)
                    {
                        _network.Deriv[i]  /= _network.OutputCount;
                        _network.Deriv2[i] /= _network.OutputCount;
                    }
                }
            }

            return(_network.Error);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Read an object.
        /// </summary>
        public Object Read(Stream mask0)
        {
            var ins0 = new EncogReadHelper(mask0);
            EncogFileSection section;
            var samples = new BasicMLDataSet();
            IDictionary <String, String> networkParams = null;
            PNNKernelType kernel      = default(PNNKernelType) /* was: null */;
            PNNOutputMode outmodel    = default(PNNOutputMode) /* was: null */;
            int           inputCount  = 0;
            int           outputCount = 0;
            double        error       = 0;

            double[] sigma = null;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    networkParams = section.ParseParams();
                }
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    inputCount = EncogFileSection.ParseInt(paras,
                                                           PersistConst.InputCount);
                    outputCount = EncogFileSection.ParseInt(paras,
                                                            PersistConst.OutputCount);
                    kernel   = StringToKernel(paras[PersistConst.Kernel]);
                    outmodel = StringToOutputMode(paras[PropertyOutputMode]);
                    error    = EncogFileSection
                               .ParseDouble(paras, PersistConst.Error);
                    sigma = section.ParseDoubleArray(paras, PersistConst.Sigma);
                }
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("SAMPLES"))
                {
                    foreach (String line in section.Lines)
                    {
                        IList <String> cols = EncogFileSection
                                              .SplitColumns(line);
                        int index     = 0;
                        var inputData = new BasicMLData(inputCount);
                        for (int i = 0; i < inputCount; i++)
                        {
                            inputData[i] =
                                CSVFormat.EgFormat.Parse(cols[index++]);
                        }
                        var idealData = new BasicMLData(inputCount);

                        idealData[0] = CSVFormat.EgFormat.Parse(cols[index++]);

                        IMLDataPair pair = new BasicMLDataPair(inputData,
                                                               idealData);
                        samples.Add(pair);
                    }
                }
            }

            var result = new BasicPNN(kernel, outmodel, inputCount,
                                      outputCount);

            if (networkParams != null)
            {
                EngineArray.PutAll(networkParams, result.Properties);
            }
            result.Samples = samples;
            result.Error   = error;
            if (sigma != null)
            {
                EngineArray.ArrayCopy(sigma, result.Sigma);
            }

            return(result);
        }