Beispiel #1
0
        /// <summary>
        /// Classify the input.
        /// </summary>
        /// <param name="input">The input to classify.</param>
        /// <returns>The classification.</returns>
        public int Classify(IMLData input)
        {
            if (_classificationTarget < 0 || _classificationTarget >= _events.Count)
            {
                throw new BayesianError("Must specify classification target by calling setClassificationTarget.");
            }

            int[] d = DetermineClasses(input);

            // properly tag all of the events
            for (int i = 0; i < _events.Count; i++)
            {
                BayesianEvent e = _events[i];
                if (i == _classificationTarget)
                {
                    Query.DefineEventType(e, EventType.Outcome);
                }
                else if (_inputPresent[i])
                {
                    Query.DefineEventType(e, EventType.Evidence);
                    Query.SetEventValue(e, d[i]);
                }
                else
                {
                    Query.DefineEventType(e, EventType.Hidden);
                    Query.SetEventValue(e, d[i]);
                }
            }


            // loop over and try each outcome choice
            BayesianEvent outcomeEvent = _events[_classificationTarget];

            _classificationProbabilities = new double[outcomeEvent.Choices.Count];
            for (int i = 0; i < outcomeEvent.Choices.Count; i++)
            {
                Query.SetEventValue(outcomeEvent, i);
                Query.Execute();
                _classificationProbabilities[i] = Query.Probability;
            }


            return(EngineArray.MaxIndex(_classificationProbabilities));
        }
        /// <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;
                }

                IMLData result = new BasicMLData(1);
                result[0] = EngineArray.MaxIndex(xout);
                return(result);
            }
            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));
        }
Beispiel #3
0
        /// <summary>
        /// Determine the winner for the specified input. This is the number of the
        /// winning neuron.
        /// </summary>
        ///
        /// <param name="input">The input patter to present to the neural network.</param>
        /// <returns>The winning neuron.</returns>
        public int Winner(IMLData input)
        {
            IMLData output = Compute(input);

            return(EngineArray.MaxIndex(output));
        }
        /// <inheritdoc/>
        public int Classify(IMLData input)
        {
            IMLData output = Compute(input);

            return(EngineArray.MaxIndex(output));
        }
Beispiel #5
0
        /// <inheritdoc/>
        public int Classify(IMLData input)
        {
            IMLData result = Compute(input);

            return(EngineArray.MaxIndex(result.Data));
        }