public int DetermineTreeType(OutputEquilateral eqField, IMLData output)
        {
            int result;

            if (eqField != null)
            {
                result = eqField.Equilateral.Decode(output.Data);
            }
            else
            {
                double maxOutput = Double.NegativeInfinity;
                result = -1;

                for (int i = 0; i < output.Count; i++)
                {
                    if (output[i] > maxOutput)
                    {
                        maxOutput = output[i];
                        result = i;
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Calculate the best matching unit (BMU). This is the output neuron that
        /// has the lowest Euclidean distance to the input vector.
        /// </summary>
        ///
        /// <param name="input">The input vector.</param>
        /// <returns>The output neuron number that is the BMU.</returns>
        public int CalculateBMU(IMLData input)
        {
            int result = 0;

            // Track the lowest distance so far.
            double lowestDistance = Double.MaxValue;

            for (int i = 0; i < _som.OutputCount; i++)
            {
                double distance = CalculateEuclideanDistance(
                    _som.Weights, input, i);

                // Track the lowest distance, this is the BMU.
                if (distance < lowestDistance)
                {
                    lowestDistance = distance;
                    result = i;
                }
            }

            // Track the worst distance, this is the error for the entire network.
            if (lowestDistance > _worstDistance)
            {
                _worstDistance = lowestDistance;
            }

            return result;
        }
 /// <inheritdoc/>
 public void CalculateError(IMLData ideal, double[] actual, double[] error)
 {
     for (int i = 0; i < actual.Length; i++)
     {
         error[i] = ideal[i] - actual[i];
     }
 }
Exemple #4
0
        /// <summary>
        /// Classify the input into one of the output clusters.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The cluster it was clasified into.</returns>
        public int Classify(IMLData input)
        {
            if (input.Count > InputCount)
            {
                throw new NeuralNetworkError(
                    "Can't classify SOM with input size of " + InputCount
                    + " with input data of count " + input.Count);
            }

            double[][] m = _weights.Data;
            double minDist = Double.PositiveInfinity;
            int result = -1;

            for (int i = 0; i < OutputCount; i++)
            {
                double dist = EngineArray.EuclideanDistance(input, m[i]);
                if (dist < minDist)
                {
                    minDist = dist;
                    result = i;
                }
            }

            return result;
        }
Exemple #5
0
 public override sealed IMLData Compute(IMLData input)
 {
     int num;
     BiPolarMLData data = new BiPolarMLData(input.Count);
     if (0 == 0)
     {
         if (((uint) num) <= uint.MaxValue)
         {
             if (3 == 0)
             {
                 return data;
             }
             goto Label_0053;
         }
     }
     else
     {
         goto Label_0053;
     }
     Label_003B:
     EngineArray.ArrayCopy(base.CurrentState.Data, data.Data);
     return data;
     Label_0053:
     EngineArray.ArrayCopy(input.Data, base.CurrentState.Data);
     this.Run();
     for (num = 0; num < base.CurrentState.Count; num++)
     {
         data.SetBoolean(num, BiPolarUtil.Double2bipolar(base.CurrentState[num]));
     }
     goto Label_003B;
 }
 private void x3342cd5bc15ae07b(int x7079b5ea66d0bae1, IMLData xcdaeea7afaf570ff)
 {
     for (int i = 0; i < this._x87a7fc6a72741c2e.InputCount; i++)
     {
         this._x87a7fc6a72741c2e.Weights[i, x7079b5ea66d0bae1] = xcdaeea7afaf570ff[i];
     }
 }
 /// <summary>
 /// Copy the specified input pattern to the weight matrix. This causes an
 /// output neuron to learn this pattern "exactly". This is useful when a
 /// winner is to be forced.
 /// </summary>
 ///
 /// <param name="outputNeuron">The output neuron to set.</param>
 /// <param name="input">The input pattern to copy.</param>
 private void CopyInputPattern(int outputNeuron, IMLData input)
 {
     for (int inputNeuron = 0; inputNeuron < _network.InputCount; inputNeuron++)
     {
         _network.Weights[inputNeuron, outputNeuron] = input[inputNeuron];
     }
 }
Exemple #8
0
 public IMLData Compute(IMLData input)
 {
     int num;
     Matrix col;
     Matrix matrix2;
     IMLData data = new BasicMLData(this.OutputCount);
     if (0 == 0)
     {
         goto Label_003F;
     }
     Label_000F:
     matrix2 = Matrix.CreateRowMatrix(input.Data);
     if (3 == 0)
     {
         goto Label_003F;
     }
     data[num] = MatrixMath.DotProduct(matrix2, col);
     num++;
     Label_0034:
     if (num < this.OutputCount)
     {
         col = this._weights.GetCol(num);
         goto Label_000F;
     }
     return data;
     Label_003F:
     num = 0;
     goto Label_0034;
 }
        /// <summary>
        /// Train the neural network for the specified pattern. The neural network
        /// can be trained for more than one pattern. To do this simply call the
        /// train method more than once.
        /// </summary>
        ///
        /// <param name="pattern">The pattern to train for.</param>
        public void AddPattern(IMLData pattern)
        {
            if (pattern.Count != NeuronCount)
            {
                throw new NeuralNetworkError("Network with " + NeuronCount
                                             + " neurons, cannot learn a pattern of size "
                                             + pattern.Count);
            }

            // Create a row matrix from the input, convert boolean to bipolar
            Matrix m2 = Matrix.CreateRowMatrix(pattern.Data);
            // Transpose the matrix and multiply by the original input matrix
            Matrix m1 = MatrixMath.Transpose(m2);
            Matrix m3 = MatrixMath.Multiply(m1, m2);

            // matrix 3 should be square by now, so create an identity
            // matrix of the same size.
            Matrix identity = MatrixMath.Identity(m3.Rows);

            // subtract the identity matrix
            Matrix m4 = MatrixMath.Subtract(m3, identity);

            // now add the calculated matrix, for this pattern, to the
            // existing weight matrix.
            ConvertHopfieldMatrix(m4);
        }
Exemple #10
0
 public BasicMLDataPair(IMLData input, IMLData ideal, IMLData calced, IMLData error)
 {
     this._significance = 1.0;
     this._input = input;
     this._ideal = ideal;
     this._calced = calced;
     this._error = error;
 }
Exemple #11
0
 public override void Add(IMLData data)
 {
     if (!(data is ImageMLData))
     {
         throw new NeuralNetworkError("This data set only supports ImageNeuralData or Image objects.");
     }
     base.Add(data);
 }
Exemple #12
0
 public BasicMLDataPair(IMLData input, IMLData ideal)
 {
     this._significance = 1.0;
     this._input = input;
     this._ideal = ideal;
     this._calced = null;
     this._error = null;
 }
Exemple #13
0
 public void UpdateError(double[] actual, IMLData ideal)
 {
     for (int i = 0; i < actual.Length; i++)
     {
         double delta = ideal[i] - actual[i];
         _globalError += delta * delta;
     }
     _setSize += ideal.Count;
 }
Exemple #14
0
 /// <summary>
 ///     Construct a loaded row from an IMLData.
 /// </summary>
 /// <param name="format">The format to store the numbers in.</param>
 /// <param name="data">The data to use.</param>
 /// <param name="extra">The extra positions to allocate.</param>
 public LoadedRow(CSVFormat format, IMLData data, int extra)
 {
     int count = data.Count;
     _data = new String[count + extra];
     for (int i = 0; i < count; i++)
     {
         _data[i] = format.Format(data[i], 5);
     }
 }
Exemple #15
0
        /// <summary>
        /// Add the specified input and ideal object to the collection.
        /// </summary>
        /// <param name="inputData">The image to train with.</param>
        /// <param name="idealData">The expected otuput form this image.</param>
        public override void Add(IMLData inputData, IMLData idealData)
        {
            if (!(inputData is ImageMLData))
            {
                throw new NeuralNetworkError(MUST_USE_IMAGE);
            }

            base.Add(inputData, idealData);
        }
Exemple #16
0
 /// <summary>
 /// Subtract two vectors.
 /// </summary>
 /// <param name="a">First vector.</param>
 /// <param name="b">Second vector.</param>
 /// <returns>Result vector.</returns>
 public static double[] Subtract(IMLData a, double[] b)
 {
     double[] result = new double[a.Count];
     for (int i = 0; i < a.Count; i++)
     {
         result[i] = a[i] - b[i];
     }
     return(result);
 }
Exemple #17
0
 public void CalculateError(IActivationFunction af, double[] b, double[] a,
                            IMLData ideal, double[] actual, double[] error, double derivShift,
                            double significance)
 {
     for (int i = 0; i < actual.Length; i++)
     {
         error[i] = (ideal[i] - actual[i]) * significance;
     }
 }
Exemple #18
0
        /// <summary>
        /// Add the specified data, must be an ImageNeuralData class.
        /// </summary>
        /// <param name="data">The data The object to add.</param>
        public override void Add(IMLData data)
        {
            if (!(data is ImageMLData))
            {
                throw new NeuralNetworkError(MUST_USE_IMAGE);
            }

            base.Add(data);
        }
 /// <summary>
 /// Get the specified weight.
 /// </summary>
 ///
 /// <param name="matrix">The matrix to use.</param>
 /// <param name="input">The input, to obtain the size from.</param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns>The value from the matrix.</returns>
 private static double GetWeight(Matrix matrix, IMLData input,
                                 int x, int y)
 {
     if (matrix.Rows != input.Count)
     {
         return(matrix[x, y]);
     }
     return(matrix[y, x]);
 }
Exemple #20
0
        public int CalculateScore(IEnumerable <int> customerOrder, bool showResults)
        {
            var input = new BasicMLData(1);

            input[0] = 1;

            IMLData output    = _network.Compute(input);
            var     logOutput = new List <LogisticSimulatorOutput>();

            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Retailer_Min", Value = Convert.ToInt32(min.DeNormalize(output[0]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Retailer_Max", Value = Convert.ToInt32(max.DeNormalize(output[1]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "WholeSaler_Min", Value = Convert.ToInt32(min.DeNormalize(output[2]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "WholeSaler_Max", Value = Convert.ToInt32(max.DeNormalize(output[3]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Distributor_Min", Value = Convert.ToInt32(min.DeNormalize(output[4]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Distributor_Max", Value = Convert.ToInt32(max.DeNormalize(output[5]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Factory_Min", Value = Convert.ToInt32(min.DeNormalize(output[6]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Factory_Max", Value = Convert.ToInt32(max.DeNormalize(output[7]))
            });
            logOutput.Add(new LogisticSimulatorOutput()
            {
                Name = "Factory_Units_Per_Day", Value = Convert.ToInt32(units.DeNormalize(output[8]))
            });

            sim.Start(logOutput, customOrders: customerOrder);

            if (showResults)
            {
                ShowResults(logOutput);
            }

            int score = Convert.ToInt32(sim.SumAllCosts());

            return(score);
        }
        private char Denormalize(IMLData value)
        {
            var query = new double[value.Count];

            value.CopyTo(query, 0, value.Count);
            var index = query.ToList().IndexOf(1D);

            return(_alphas[index]);
        }
Exemple #22
0
        /// <summary>
        /// Note: for Boltzmann networks, you will usually want to call the "run"
        /// method to compute the output.
        /// This method can be used to copy the input data to the current state. A
        /// single iteration is then run, and the new current state is returned.
        /// </summary>
        ///
        /// <param name="input">The input pattern.</param>
        /// <returns>The new current state.</returns>
        public override sealed IMLData Compute(IMLData input)
        {
            var result = new BiPolarMLData(input.Count);

            input.CopyTo(CurrentState.Data, 0, input.Count);
            Run();
            EngineArray.ArrayCopy(CurrentState.Data, result.Data);
            return(result);
        }
        public virtual void Compute(IMLData input, double[] output)
        {
            int sourceIndex = _layerOutput.Length
                              - _layerCounts[_layerCounts.Length - 1];

            input.CopyTo(_layerOutput, sourceIndex, _inputCount);

            InnerCompute(output);
        }
Exemple #24
0
        static void Main(string[] args)
        {
            // used for prediction of time series
            // sin(x) in theory
            int DEGREES     = 360;
            int WINDOW_SIZE = 16;

            double[][] Input = new double[DEGREES][];
            double[][] Ideal = new double[DEGREES][];

            // Create array of sin signals
            for (int i = 0; i < DEGREES; i++)
            {
                Input[i] = new double[WINDOW_SIZE];
                Ideal[i] = new double[] { Math.Sin(DegreeToRad(i + WINDOW_SIZE)) };
                for (int j = 0; j < WINDOW_SIZE; j++)
                {
                    Input[i][j] = Math.Sin(DegreeToRad(i + j));
                }
            }
            // construct training set
            IMLDataSet trainingSet = new BasicMLDataSet(Input, Ideal);

            // construct an Elman type network
            // simple recurrent network
            ElmanPattern pattern = new ElmanPattern
            {
                InputNeurons       = WINDOW_SIZE,
                ActivationFunction = new ActivationSigmoid(),
                OutputNeurons      = 1
            };

            pattern.AddHiddenLayer(WINDOW_SIZE);
            IMLMethod    method  = pattern.Generate();
            BasicNetwork network = (BasicNetwork)method;
            // Train network
            IMLTrain train = new Backpropagation(network, trainingSet);
            var      stop  = new StopTrainingStrategy();

            train.AddStrategy(new Greedy());
            train.AddStrategy(stop);
            int epoch = 0;

            while (!stop.ShouldStop())
            {
                train.Iteration();
                Console.WriteLine($"Training Epoch #{epoch} Error:{train.Error}");
                epoch++;
            }
            // Test network
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine($"actual={output[0]}, ideal={pair.Ideal[0]}");
            }
        }
        /// <summary>
        /// Add only input data, for an unsupervised dataset.
        /// </summary>
        /// <param name="data1">The data to be added.</param>
        public void Add(IMLData data1)
        {
            if (!_loading)
            {
                throw new IMLDataError(ErrorAdd);
            }

            _egb.Write(data1);
            _egb.Write(1.0);
        }
 public void CalculateError(IActivationFunction af, double[] b, double[] a,
                            IMLData ideal, double[] actual, double[] error, double derivShift,
                            double significance)
 {
     for (int i = 0; i < actual.Length; i++)
     {
         double deriv = af.DerivativeFunction(b[i], a[i]);
         error[i] = (Math.Atan(ideal[i] - actual[i]) * significance) * deriv;
     }
 }
        private double[] IMDataToDoubleArray(IMLData d)
        {
            double[] array = new double[d.Count];
            for (int i = 0; i < d.Count; i++)
            {
                array[i] = d[i];
            }

            return(array);
        }
Exemple #28
0
        /// <summary>
        /// Construct a loaded row from an IMLData.
        /// </summary>
        /// <param name="format">The format to store the numbers in.</param>
        /// <param name="data">The data to use.</param>
        /// <param name="extra">The extra positions to allocate.</param>
        public LoadedRow(CSVFormat format, IMLData data, int extra)
        {
            int count = data.Count;

            _data = new String[count + extra];
            for (int i = 0; i < count; i++)
            {
                _data[i] = format.Format(data[i], 5);
            }
        }
        /// <summary>
        /// Add only input data, for an unsupervised dataset.
        /// </summary>
        /// <param name="data1">The data to be added.</param>
        public void Add(IMLData data1)
        {
            if (!loading)
            {
                throw new IMLDataError(ERROR_ADD);
            }

            egb.Write(data1.Data);
            egb.Write(1.0);
        }
        /// <summary>
        /// Add both the input and ideal data.
        /// </summary>
        /// <param name="inputData">The input data.</param>
        /// <param name="idealData">The ideal data.</param>
        public void Add(IMLData inputData, IMLData idealData)
        {
            if (!loading)
            {
                throw new IMLDataError(ERROR_ADD);
            }

            egb.Write(inputData.Data);
            egb.Write(idealData.Data);
            egb.Write(1.0);
        }
        /// <summary>
        /// Get the Euclidean distance between the specified data and the set number.
        /// </summary>
        /// <param name="data">The data to check.</param>
        /// <param name="set">The set to check.</param>
        /// <returns>The distance.</returns>
        public double GetDistance(IMLData data, int set)
        {
            double result = 0;

            for (int i = 0; i < data.Count; i++)
            {
                var val = data[i] - _matrix[set][i];
                result += val * val;
            }
            return(Math.Sqrt(result));
        }
        /// <summary>
        /// Add both the input and ideal data.
        /// </summary>
        /// <param name="inputData">The input data.</param>
        /// <param name="idealData">The ideal data.</param>
        public void Add(IMLData inputData, IMLData idealData)
        {
            if (!_loading)
            {
                throw new IMLDataError(ErrorAdd);
            }

            _egb.Write(inputData);
            _egb.Write(idealData);
            _egb.Write(1.0);
        }
        public double PredictNext(double[] inputData)
        {
            double[] newNormalizedData = normalizeArray.Process(inputData);
            IMLData  input             = new BasicMLData(WindowSize);

            input.Data = newNormalizedData;
            IMLData output1            = network.Compute(input);
            double  outputDenormalized = normalizeArray.Stats.DeNormalize(output1.Data[0]);

            return(outputDenormalized);
        }
Exemple #34
0
        /// <summary>
        /// Called to update for each number that should be checked.
        /// </summary>
        public void UpdateError(IMLData actual, double[] ideal, double significance)
        {
            for (int i = 0; i < actual.Count; i++)
            {
                double delta = ideal[i] - actual[i];

                _globalError += delta * delta;
            }

            _setSize += ideal.Length;
        }
        public double DetermineAngle(IMLData angle)
        {
            double result = (Math.Atan2(angle[0], angle[1]) / Math.PI) * 180;

            if (result < 0)
            {
                result += 360;
            }

            return(result);
        }
Exemple #36
0
        /// <summary>
        /// Calculate the Euclidean distance between two vectors.
        /// </summary>
        /// <param name="p1">The first vector.</param>
        /// <param name="p2">The second vector.</param>
        /// <returns>The distance.</returns>
        public static double EuclideanDistance(IMLData p1, double[] p2)
        {
            double sum = 0;

            for (int i = 0; i < p1.Count; i++)
            {
                double d = p1[i] - p2[i];
                sum += d * d;
            }
            return(Math.Sqrt(sum));
        }
        private string GetOutput(IMLData lst)
        {
            string ret = " [Ouput: ";

            for (int i = 0; i < lst.Count; i++)
            {
                ret += lst[i] + ",";
            }
            ret  = ret.TrimEnd(',');
            ret += "]";
            return(ret);
        }
Exemple #38
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var     input  = debugGame.inputs();
            IMLData output = net.Compute(input);

            debugGame.update(output[0], output[1]);
        }
        // Predict data using a network.
        public static bool Predict(ref NetworkContainer container, ref EncogPredictSettings settings)
        {
            IMLData output = container.network.Compute(new BasicMLData(settings.data));

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

            // Return true or false according to threshold.
            return(output[0] >= settings.threshold);
        }
Exemple #40
0
        /// <inheritdoc/>
        public double Distance(IMLDataPair d)
        {
            IMLData diff = _value.Minus(d.Input);
            double  sum  = 0.0;

            for (int i = 0; i < diff.Count; i++)
            {
                sum += diff[i] * diff[i];
            }

            return(Math.Sqrt(sum));
        }
        /// <inheritdoc/>
        public int Classify(IMLData input)
        {
            if (_model == null)
            {
                throw new EncogError(
                          "Can't use the SVM yet, it has not been trained, "
                          + "and no model exists.");
            }

            svm_node[] formattedInput = MakeSparse(input);
            return((int)svm.svm_predict(_model, formattedInput));
        }
Exemple #42
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();
        }
 /// <summary>
 /// Add a pattern to the neural network.
 /// </summary>
 ///
 /// <param name="inputPattern">The input pattern.</param>
 /// <param name="outputPattern">The output pattern(for this input).</param>
 public void AddPattern(IMLData inputPattern,
                        IMLData outputPattern)
 {
     for (int i = 0; i < _f1Count; i++)
     {
         for (int j = 0; j < _f2Count; j++)
         {
             var weight = (int)(inputPattern[i] * outputPattern[j]);
             _weightsF1ToF2.Add(i, j, weight);
             _weightsF2ToF1.Add(j, i, weight);
         }
     }
 }
        /// <inheritdoc />
        public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
            int dataColumn)
        {
            double value = data[dataColumn];
            double result = ((colDef.Low - colDef.High)*value
                             - _normalizedHigh*colDef.Low + colDef.High
                             *_normalizedLow)
                            /(_normalizedLow - _normalizedHigh);

            // typically caused by a number that should not have been normalized
            // (i.e. normalization or actual range is infinitely small.
            if (Double.IsNaN(result))
            {
                return "" + (((_normalizedHigh - _normalizedLow)/2) + _normalizedLow);
            }
            return "" + result;
        }
        /// <inheritdoc />
        public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
            int dataColumn)
        {
            double bestValue = Double.NegativeInfinity;
            int bestIndex = 0;

            for (int i = 0; i < data.Count; i++)
            {
                double d = data[dataColumn + i];
                if (d > bestValue)
                {
                    bestValue = d;
                    bestIndex = i;
                }
            }

            return colDef.Classes[bestIndex];
        }
 /// <summary>
 /// Construct a new BasicMLData object from an existing one. This makes a
 /// copy of an array. If MLData is not complex, then only reals will be 
 /// created. 
 /// </summary>
 /// <param name="d">The object to be copied.</param>
 public BasicMLComplexData(IMLData d)
 {
     if (d is IMLComplexData)
     {
         var c = (IMLComplexData) d;
         for (int i = 0; i < d.Count; i++)
         {
             _data[i] = new ComplexNumber(c.GetComplexData(i));
         }
     }
     else
     {
         for (int i = 0; i < d.Count; i++)
         {
             _data[i] = new ComplexNumber(d[i], 0);
         }
     }
 }
        /// <inheritdoc />
        public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
            int dataColumn)
        {
            double high = colDef.Classes.Count;
            double low = 0;

            double value = data[dataColumn];
            double result = ((low - high)*value - _normalizedHigh*low + high
                             *_normalizedLow)
                            /(_normalizedLow - _normalizedHigh);

            // typically caused by a number that should not have been normalized
            // (i.e. normalization or actual range is infinitely small.
            if (Double.IsNaN(result))
            {
                return colDef.Classes[0];
            }
            return colDef.Classes[(int) result];
        }
Exemple #48
0
 public void AddPattern(IMLData pattern)
 {
     object[] objArray;
     if (pattern.Count != base.NeuronCount)
     {
         objArray = new object[4];
         if (0 == 0)
         {
             objArray[0] = "Network with ";
             do
             {
                 if (0 != 0)
                 {
                     return;
                 }
             }
             while (0 != 0);
             objArray[1] = base.NeuronCount;
             objArray[2] = " neurons, cannot learn a pattern of size ";
             objArray[3] = pattern.Count;
         }
     }
     else
     {
         Matrix b = Matrix.CreateRowMatrix(pattern.Data);
         Matrix a = MatrixMath.Multiply(MatrixMath.Transpose(b), b);
         Matrix matrix4 = MatrixMath.Identity(a.Rows);
         Matrix delta = MatrixMath.Subtract(a, matrix4);
         this.ConvertHopfieldMatrix(delta);
         if (0 == 0)
         {
             return;
         }
     }
     throw new NeuralNetworkError(string.Concat(objArray));
 }
        private double MaxInArray(IMLData result)
        {
            var array = new double[Network.OutputSize];
            result.CopyTo(array, 0, Network.OutputSize - 1);

            var maxValue = array.Max();
            var maxIndex = array.ToList().IndexOf(maxValue);

            return maxIndex + 1;
        }
        /// <summary>
        /// Compute the output from this synapse.
        /// </summary>
        /// <param name="input">The input to this synapse.</param>
        /// <returns>The output from this synapse.</returns>
        public IMLData Compute(IMLData input)
        {
            var result = new double[_outputCount];

            // clear from previous
            EngineArray.Fill(_preActivation, 0.0);
            EngineArray.Fill(_postActivation, 0.0);
            _postActivation[0] = 1.0;

            // copy input
            for (int i = 0; i < _inputCount; i++)
            {
                _postActivation[i + 1] = input[i];
            }

            // iterate through the network activationCycles times
            for (int i = 0; i < ActivationCycles; ++i)
            {
                InternalCompute();
            }

            // copy output
            for (int i = 0; i < _outputCount; i++)
            {
                result[i] = _postActivation[_outputIndex + i];
            }

            return new BasicMLData(result);
        }
        /// <summary>
        /// Create a matrix that is a single row.
        /// </summary>
        /// <param name="input">A 1D array to make the matrix from.</param>
        /// <returns>A matrix that contans a single row.</returns>
        public static Matrix CreateRowMatrix(IMLData input)
        {
            var d = new double[1][];

            d[0] = new double[input.Count];

            for(int i = 0; i < input.Count; i++)
            {
                d[0][i] = input[i];
            }

            return new Matrix(d);
        }
Exemple #52
0
 /// <summary>
 /// Not supported.
 /// </summary>
 /// <param name="inputData">Not used.</param>
 /// <param name="idealData">Not used.</param>
 public void Add(IMLData inputData, IMLData idealData)
 {
     throw new TrainingError(AddNotSupported);
 }
Exemple #53
0
 /// <summary>
 /// Not supported.
 /// </summary>
 /// <param name="data1">Not used.</param>
 public void Add(IMLData data1)
 {
     throw new TrainingError(AddNotSupported);
 }
Exemple #54
0
        /// <summary>
        ///     Determines the angle on IMLData.
        /// </summary>
        /// <param name="angle">The angle.</param>
        /// <returns></returns>
        public static double DetermineAngle(IMLData angle)
        {
            double result = (Math.Atan2(angle[0], angle[1])/Math.PI)*180;
            if (result < 0)
                result += 360;

            return result;
        }
Exemple #55
0
        /// <summary>
        /// Compute the output from this network.
        /// </summary>
        ///
        /// <param name="input">The input to the network.</param>
        /// <returns>The output from the network.</returns>
        public override sealed IMLData Compute(IMLData input)
        {
            var xout = new double[OutputCount];

            double psum = 0.0d;

            int r = -1;

            foreach (IMLDataPair pair  in  _samples)
            {
                r++;

                if (r == Exclude)
                {
                    continue;
                }

                double dist = 0.0d;
                for (int i = 0; i < InputCount; i++)
                {
                    double diff = input[i] - pair.Input[i];
                    diff /= _sigma[i];
                    dist += diff*diff;
                }

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

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

                if (OutputMode == PNNOutputMode.Classification)
                {
                    var pop = (int) pair.Ideal[0];
                    xout[pop] += dist;
                }
                else if (OutputMode == PNNOutputMode.Unsupervised)
                {
                    for (int i = 0; i < InputCount; i++)
                    {
                        xout[i] += dist*pair.Input[i];
                    }
                    psum += dist;
                }
                else if (OutputMode == PNNOutputMode.Regression)
                {
                    for (int i = 0; i < OutputCount; i++)
                    {
                        xout[i] += dist*pair.Ideal[i];
                    }

                    psum += dist;
                }
            }

            if (OutputMode == PNNOutputMode.Classification)
            {
                psum = 0.0d;
                for (int i = 0; i < OutputCount; i++)
                {
                    if (_priors[i] >= 0.0d)
                    {
                        xout[i] *= _priors[i]/_countPer[i];
                    }
                    psum += xout[i];
                }

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

                for (int i = 0; i < OutputCount; i++)
                {
                    xout[i] /= psum;
                }
            }
            else if (OutputMode == PNNOutputMode.Unsupervised)
            {
                for (int i = 0; i < InputCount; i++)
                {
                    xout[i] /= psum;
                }
            }
            else if (OutputMode == PNNOutputMode.Regression)
            {
                for (int i = 0; i < OutputCount; i++)
                {
                    xout[i] /= psum;
                }
            }

            return new BasicMLData(xout);
        }
Exemple #56
0
 /// <inheritdoc/>
 public int Classify(IMLData input)
 {
     IMLData output = Compute(input);
     return EngineArray.MaxIndex(output.Data);
 }
        /// <summary>
        /// Called to update for each number that should be checked.
        /// </summary>
        public void UpdateError(IMLData actual, double[] ideal, double significance)
        {
            for(int i = 0; i < actual.Count; i++)
            {
                double delta = ideal[i] - actual[i];

                _globalError += delta * delta;
            }

            _setSize += ideal.Length;
        }
        /// <summary>
        ///     Compute the output from the input MLData. The individual values of the
        ///     input will be mapped to the variables defined in the context. The order
        ///     is the same between the input and the defined variables. The input will
        ///     be mapped to the appropriate types. Enums will use their ordinal number.
        ///     The result will be a single number MLData.
        /// </summary>
        /// <param name="input">The input to the program.</param>
        /// <returns>A single numer MLData.</returns>
        public IMLData Compute(IMLData input)
        {
            if (input.Count != InputCount)
            {
                throw new EACompileError("Invalid input count.");
            }

            for (int i = 0; i < input.Count; i++)
            {
                _variables.SetVariable(i, input[i]);
            }

            ExpressionValue v = RootNode.Evaluate();
            VariableMapping resultMapping = ResultType;

            var result = new BasicMLData(1);
            bool success = false;

            switch (resultMapping.VariableType)
            {
                case EPLValueType.FloatingType:
                    if (v.IsNumeric)
                    {
                        result.Data[0] = v.ToFloatValue();
                        success = true;
                    }
                    break;
                case EPLValueType.StringType:
                    result.Data[0] = v.ToFloatValue();
                    success = true;
                    break;
                case EPLValueType.BooleanType:
                    if (v.IsBoolean)
                    {
                        result.Data[0] = v.ToBooleanValue() ? 1.0 : 0.0;
                        success = true;
                    }
                    break;
                case EPLValueType.IntType:
                    if (v.IsNumeric)
                    {
                        result[0] = v.ToIntValue();
                        success = true;
                    }
                    break;
                case EPLValueType.EnumType:
                    if (v.IsEnum)
                    {
                        result.Data[0] = v.ToIntValue();
                        success = true;
                    }
                    break;
            }

            if (!success)
            {
                throw new EARuntimeError("EncogProgram produced "
                                         + v.ExprType.ToString() + " but "
                                         + resultMapping.VariableType.ToString()
                                         + " was expected.");
            }

            return result;
        }
 /// <summary>
 /// Write the data from an IMLData
 /// </summary>
 /// <param name="v">The array to write.</param>
 public void Write(IMLData v)
 {
     try
     {
         for(int i = 0; i < v.Count; i++)
         {
             _binaryWriter.Write(v[i]);
         }
     }
     catch(IOException ex)
     {
         throw new BufferedDataError(ex);
     }
 }
        /// <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;

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

                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;
                }
            }

            return new BasicMLData(xout);
        }