Exemplo n.º 1
0
 /// <summary>
 /// Construct an RPROP job. For more information on RPROP see the
 /// ResilientPropagation class. 
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data to use.</param>
 /// <param name="loadToMemory">True if binary training data should be loaded to memory.</param>
 /// <param name="localRatio">The local ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="globalRatio">The global ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="segmentationRatio">The segmentation ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="iterationsPer">How many iterations to process per cycle.</param>
 public RPROPJob(BasicNetwork network, INeuralDataSet training,
         bool loadToMemory, double localRatio, int globalRatio, double segmentationRatio, int iterationsPer) :
     this(network, training,
          loadToMemory, RPROPConst.DEFAULT_INITIAL_UPDATE,
          RPROPConst.DEFAULT_MAX_STEP, localRatio, globalRatio, segmentationRatio, iterationsPer)
 {
 }
Exemplo n.º 2
0
 /// <summary>
 /// Construct a cross trainer. 
 /// </summary>
 /// <param name="network">The network.</param>
 /// <param name="training">The training data.</param>
 public CrossTraining(BasicNetwork network,
          FoldedDataSet training)
 {
     this.network = network;
     Training = training;
     this.folded = training;
 }
Exemplo n.º 3
0
 /// <inheritdoc />
 public override void Randomize(BasicNetwork network)
 {
     for (var i = 0; i < network.Weights.Length; i++)
     {
         network.Weights[i] = Rnd.NextDouble(_low, _high);
     }
 }
 /// <summary>
 /// Construct the depth calculation object.
 /// </summary>
 /// <param name="network">The network that we are calculating for.</param>
 public CalculateDepth(BasicNetwork network)
 {
     this.network = network;
     this.outputLayer = network.GetLayer(BasicNetwork.TAG_OUTPUT);
     if( this.outputLayer!=null )
         Calculate(0, this.outputLayer);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Determine if the two neural networks are equal.
        /// </summary>
        ///
        /// <param name="network1">The first network.</param>
        /// <param name="network2">The second network.</param>
        /// <param name="precision">How many decimal places to check.</param>
        /// <returns>True if the two networks are equal.</returns>
        public static bool Equals(BasicNetwork network1,
                                  BasicNetwork network2, int precision)
        {
            double[] array1 = NetworkToArray(network1);
            double[] array2 = NetworkToArray(network2);

            if (array1.Length != array2.Length)
            {
                return false;
            }

            double test = Math.Pow(10.0d, precision);
            if (Double.IsInfinity(test) || (test > Int64.MaxValue))
            {
                throw new NeuralNetworkError("Precision of " + precision
                                             + " decimal places is not supported.");
            }

            for (int i = 0; i < array1.Length; i++)
            {
                var l1 = (long) (array1[i]*test);
                var l2 = (long) (array2[i]*test);
                if (l1 != l2)
                {
                    return false;
                }
            }

            return true;
        }
 /// <summary>
 /// Construct a Manhattan propagation training object.
 /// </summary>
 ///
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data to use.</param>
 /// <param name="learnRate">The learning rate.</param>
 public ManhattanPropagation(BasicNetwork network,
     IMLDataSet training, double learnRate)
     : base(network, training)
 {
     _learningRate = learnRate;
     _zeroTolerance = RPROPConst.DefaultZeroTolerance;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Construct a QPROP trainer for flat networks.
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data.</param>
 /// <param name="learnRate">The learning rate.  2 is a good suggestion as 
 ///            a learning rate to start with.  If it fails to converge, 
 ///            then drop it.  Just like backprop, except QPROP can 
 ///            take higher learning rates.</param>
 public QuickPropagation(BasicNetwork network,
                         IMLDataSet training, double learnRate)
     : base(network, training)
 {
     ValidateNetwork.ValidateMethodToData(network, training);
     LearningRate = learnRate;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Construct a Hopfield training class.
 /// </summary>
 /// <param name="trainingSet">The training set to use.</param>
 /// <param name="network">The network to train.</param>
 public TrainHopfield(INeuralDataSet trainingSet,
          BasicNetwork network)
 {
     this.network = network;
     this.Training = trainingSet;
     this.Error = 0;
 }
Exemplo n.º 9
0
 /// <inheritdoc />
 public override void Randomize(BasicNetwork network)
 {
     for (var i = 0; i < network.Layers.Count - 1; i++)
     {
         RandomizeLayer(network, i);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Construct the LMA object.
        /// </summary>
        /// <param name="network">The network to train. Must have a single output neuron.</param>
        /// <param name="training">The training data to use. Must be indexable.</param>
        public SVDTraining(BasicNetwork network, INeuralDataSet training)
        {
            ILayer outputLayer = network.GetLayer(BasicNetwork.TAG_OUTPUT);

            if (outputLayer == null)
            {
                throw new TrainingError("SVD requires an output layer.");
            }

            if (outputLayer.NeuronCount != 1)
            {
                throw new TrainingError("SVD requires an output layer with a single neuron.");
            }

            if (network.GetLayer(RadialBasisPattern.RBF_LAYER) == null)
                throw new TrainingError("SVD is only tested to work on radial basis function networks.");

            rbfLayer = (RadialBasisFunctionLayer)network.GetLayer(RadialBasisPattern.RBF_LAYER);

            this.Training = training;
            this.network = network;
            this.trainingLength = (int)this.Training.InputSize;

            BasicNeuralData input = new BasicNeuralData(this.Training.InputSize);
            BasicNeuralData ideal = new BasicNeuralData(this.Training.IdealSize);
            this.pair = new BasicNeuralDataPair(input, ideal);
        }
        /// <summary>
        /// Construct a neural genetic algorithm.
        /// </summary>
        ///
        /// <param name="network">The network to base this on.</param>
        /// <param name="randomizer">The randomizer used to create this initial population.</param>
        /// <param name="calculateScore">The score calculation object.</param>
        /// <param name="populationSize">The population size.</param>
        /// <param name="mutationPercent">The percent of offspring to mutate.</param>
        /// <param name="percentToMate">The percent of the population allowed to mate.</param>
        public NeuralGeneticAlgorithm(BasicNetwork network,
            IRandomizer randomizer, ICalculateScore calculateScore,
            int populationSize, double mutationPercent,
            double percentToMate)
            : base(TrainingImplementationType.Iterative)
        {
            Genetic = new NeuralGeneticAlgorithmHelper
                           {
                               CalculateScore = new GeneticScoreAdapter(calculateScore)
                           };
            IPopulation population = new BasicPopulation(populationSize);
            Genetic.MutationPercent = mutationPercent;
            Genetic.MatingPopulation = percentToMate*2;
            Genetic.PercentToMate = percentToMate;
            Genetic.Crossover = new Splice(network.Structure.CalculateSize()/3);
            Genetic.Mutate = new MutatePerturb(4.0d);
            Genetic.Population = population;
            for (int i = 0; i < population.PopulationSize; i++)
            {
                var chromosomeNetwork = (BasicNetwork) (network
                                                           .Clone());
                randomizer.Randomize(chromosomeNetwork);

                var genome = new NeuralGenome(chromosomeNetwork) {GA = Genetic};
                Genetic.PerformCalculateScore(genome);
                Genetic.Population.Add(genome);
            }
            population.Sort();
        }
        /// <summary>
        /// Format the network as a human readable string that lists the 
        /// hidden layers.
        /// </summary>
        /// <param name="network">The network to format.</param>
        /// <returns>A human readable string.</returns>
        public static String NetworkToString(BasicNetwork network)
        {
            StringBuilder result = new StringBuilder();
            int num = 1;

            ILayer layer = network.GetLayer(BasicNetwork.TAG_INPUT);

            // display only hidden layers
            while (layer.Next.Count > 0)
            {
                layer = layer.Next[0].ToLayer;

                if (result.Length > 0)
                {
                    result.Append(",");
                }
                result.Append("H");
                result.Append(num++);
                result.Append("=");
                result.Append(layer.NeuronCount);
            }

            return result.ToString();

        }
Exemplo n.º 13
0
        /// <summary>
        /// Determine the network size.
        /// </summary>
        /// <param name="network">The network to check.</param>
        /// <returns>The size of the network.</returns>
        public static int NetworkSize(BasicNetwork network)
        {

            // see if there is already an up to date flat network
            if (network.Structure.Flat != null
                && (network.Structure.FlatUpdate == FlatUpdateNeeded.None
                || network.Structure.FlatUpdate == FlatUpdateNeeded.Unflatten))
            {
                return network.Structure.Flat.Weights.Length;
            }

            int index = 0;

            // loop over all of the layers, take the output layer first
            foreach (ILayer layer in network.Structure.Layers)
            {

                // see if the previous layer, which is the next layer that the loop will hit,
                // is either a connection to a BasicLayer or a ContextLayer.
                ISynapse synapse = network.Structure
                        .FindPreviousSynapseByLayerType(layer, typeof(BasicLayer));
                ISynapse contextSynapse = network.Structure.FindPreviousSynapseByLayerType(
                        layer, typeof(ContextLayer));

                // get a list of of the previous synapses to this layer
                IList<ISynapse> list = network.Structure.GetPreviousSynapses(layer);

                // If there is not a BasicLayer or contextLayer as the next layer, then
                // just take the first synapse of any type.
                if (synapse == null && contextSynapse == null && list.Count > 0)
                {
                    synapse = list[0];
                }

                // is there any data to record for this synapse?
                if (synapse != null && synapse.WeightMatrix != null)
                {
                    // process each weight matrix
                    for (int x = 0; x < synapse.ToNeuronCount; x++)
                    {

                        index += synapse.FromNeuronCount;


                        if (synapse.ToLayer.HasBias)
                        {
                            index++;
                        }

                        if (contextSynapse != null)
                        {
                            index += contextSynapse.FromNeuronCount;
                        }
                    }
                }
            }

            return index;
        }
Exemplo n.º 14
0
 /// <summary>
 /// Construct the instar training object.
 /// </summary>
 /// <param name="network">The network to be trained.</param>
 /// <param name="training">The training data.</param>
 /// <param name="learningRate">The learning rate.</param>
 public TrainInstar(BasicNetwork network, INeuralDataSet training,
         double learningRate)
 {
     this.network = network;
     this.training = training;
     this.learningRate = learningRate;
     this.parts = new FindCPN(network);
 }
Exemplo n.º 15
0
        /// <summary>
        /// Construct a job definition for RPROP. For more information on backprop,
        /// see the Backpropagation class. 
        /// </summary>
        /// <param name="network">The network to use.</param>
        /// <param name="training">The training data to use.</param>
        /// <param name="loadToMemory">Should binary data be loaded to memory?</param>
        /// <param name="learningRate">THe learning rate to use.</param>
        /// <param name="momentum">The momentum to use.</param>
        public BPROPJob(BasicNetwork network, INeuralDataSet training,
                 bool loadToMemory, double learningRate,
                 double momentum)
            : base(network, training, loadToMemory)
        {

            this.LearningRate = learningRate;
            this.Momentum = momentum;
        }
 /// <summary>
 /// Construct a training class.
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data.</param>
 public ScaledConjugateGradient(BasicNetwork network,
         INeuralDataSet training)
     : base(network, training)
 {
     TrainFlatNetworkSCG rpropFlat = new TrainFlatNetworkSCG(
             network.Structure.Flat,
             this.Training);
     this.FlatTraining = rpropFlat;
 }
 /// <summary>
 /// Construct a QPROP trainer for flat networks.
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data.</param>
 /// <param name="learnRate">The learning rate.  2 is a good suggestion as 
 ///            a learning rate to start with.  If it fails to converge, 
 ///            then drop it.  Just like backprop, except QPROP can 
 ///            take higher learning rates.</param>
 public QuickPropagation(BasicNetwork network,
     IMLDataSet training, double learnRate)
     : base(network, training)
 {
     ValidateNetwork.ValidateMethodToData(network, training);
     LearningRate = learnRate;
     LastDelta = new double[Network.Flat.Weights.Length];
     OutputEpsilon = 1.0;
 }
Exemplo n.º 18
0
        public void Execute(IExampleInterface app)
        {
            GetSunSpots();
            EvaluateEnd = EvaluateStart + 100;
            NormalizeSunspots(-1, 1);
            BasicNetwork network  = (BasicNetwork)CreateElmanNetwork(WindowSize);
            IMLDataSet   training = GenerateTraining();

            Train(network, training);
            Predict(network);
        }
Exemplo n.º 19
0
        public void Execute(int write)
        {
            this.write = write;
            _normalizedTrainingData   = NormalizeData(trainingData);
            _normalizedPredictionData = NormalizeData(predictionData);
            network = CreateNetwork();
            IMLDataSet training = GenerateTraining();

            Train(training);
            Predict();
        }
Exemplo n.º 20
0
        public static BasicNetwork CreateNetwork(int inputSize, int outputSize)
        {
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, inputSize));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, inputSize));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, outputSize));
            network.Structure.FinalizeStructure();
            network.Reset();
            return(network);
        }
Exemplo n.º 21
0
        private void CreateNetwork(FileInfo networkFile)
        {
            network = new BasicNetwork();
            network.AddLayer(new BasicLayer(new ActivationLinear(), true, 22));
            network.AddLayer(new BasicLayer(new ActivationTANH(), true, 6));
            network.AddLayer(new BasicLayer(new ActivationTANH(), false, 1));

            network.Structure.FinalizeStructure();
            network.Reset();
            EncogDirectoryPersistence.SaveObject(networkFile, (BasicNetwork)network);
        }
Exemplo n.º 22
0
        public BasicNetwork Create()
        {
            BasicNetwork network = XOR.CreateTrainedXOR();

            XOR.VerifyXOR(network, 0.1);

            network.SetProperty("test", "test2");


            return(network);
        }
Exemplo n.º 23
0
        public static void Compute <T>(BasicNetwork network, ILearningScenario <T> scenario,
                                       IList <T> dataSource)
        {
            var computingSet = scenario.GetSet(dataSource);

            foreach (var item in computingSet)
            {
                var output = network.Compute(item.Input);
                Console.WriteLine($"Input: {item.Input[0]}, {item.Input[1]} Ideal: {item.Ideal[0]} Actual: {output[0]}");
            }
        }
        protected virtual BasicNetwork BuildNetwork(TimeSeries simulatedData, out NormalizeArray norm)
        {
            double[]     data           = GenerateData(simulatedData);
            double[]     normalizedData = NormalizeData(data, mNormalizedLow, mNormalzedHigh, out norm);
            BasicNetwork network        = CreateNetwork();
            IMLDataSet   training       = GenerateTraining(normalizedData);

            Train(network, training);

            return(network);
        }
Exemplo n.º 25
0
        /// <inheritdoc/>
        public virtual void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
        {
            int weightCount = theNetwork.Structure.Flat.Weights.Length;

            flat          = theNetwork.Flat;
            training      = theTraining;
            network       = theNetwork;
            gradients     = new double[weightCount];
            hessianMatrix = new Matrix(weightCount, weightCount);
            hessian       = hessianMatrix.Data;
            derivative    = new double[weightCount];
        }
Exemplo n.º 26
0
 public RedNeuronal()
 {
     trainingSet  = new BasicMLDataSet(neuralInput, neuralOutput);
     this.network = new BasicNetwork();
     network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 72));
     network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 216));
     network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 144));
     network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 12));
     network.Structure.FinalizeStructure();
     network.Reset();
     train = new ResilientPropagation(network, trainingSet);
 }
Exemplo n.º 27
0
        /// <summary>
        /// Initializes this neural network.
        /// </summary>
        private void InitializeNetwork()
        {
            NeuralNetwork = new BasicNetwork();

            foreach (var l in Parameters.Layers)
            {
                NeuralNetwork.AddLayer(new BasicLayer(l.ActivationFunction, l.Bias, l.NueronCount));
            }

            NeuralNetwork.Structure.FinalizeStructure();
            NeuralNetwork.Reset();
        }
Exemplo n.º 28
0
        /// <summary>
        /// Use an array to populate the memory of the neural network.
        /// </summary>
        /// <param name="array">An array of doubles.</param>
        /// <param name="network">The network to encode.</param>
        public static void ArrayToNetwork(double[] array,
                                          BasicNetwork network)
        {
            int index = 0;

            foreach (ILayer layer in network.Structure.Layers)
            {
                index = NetworkCODEC.ProcessLayer(network, layer, array, index);
            }

            network.Structure.FlatUpdate = FlatUpdateNeeded.Flatten;
        }
Exemplo n.º 29
0
 /// <summary>
 /// Construct a training job.
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data to use.</param>
 /// <param name="loadToMemory">True, if binary data should be loaded to memory.</param>
 public TrainingJob(BasicNetwork network,
                    INeuralDataSet training, bool loadToMemory)
     : base()
 {
     this.Network           = network;
     this.Training          = training;
     this.LoadToMemory      = loadToMemory;
     this.IterationsPer     = 1;
     this.LocalRatio        = 1.0;
     this.GlobalRatio       = 1;
     this.SegmentationRatio = 1.0;
 }
        /// <summary>
        /// Create a feed forward network.
        /// </summary>
        ///
        /// <param name="architecture">The architecture string to use.</param>
        /// <param name="input">The input count.</param>
        /// <param name="output">The output count.</param>
        /// <returns>The feedforward network.</returns>
        public IMLMethod Create(String architecture, int input,
                                int output)
        {
            var                 result = new BasicNetwork();
            IList <String>      layers = ArchitectureParse.ParseLayers(architecture);
            IActivationFunction af     = new ActivationLinear();

            int questionPhase = 0;

            foreach (String layerStr  in  layers)
            {
                // determine default
                int defaultCount = questionPhase == 0 ? input : output;

                ArchitectureLayer layer = ArchitectureParse.ParseLayer(
                    layerStr, defaultCount);
                bool bias = layer.Bias;

                String part = layer.Name;
                part = part != null?part.Trim() : "";

                IActivationFunction lookup = _factory.Create(part);

                if (lookup != null)
                {
                    af = lookup;
                }
                else
                {
                    if (layer.UsedDefault)
                    {
                        questionPhase++;
                        if (questionPhase > 2)
                        {
                            throw new EncogError("Only two ?'s may be used.");
                        }
                    }

                    if (layer.Count == 0)
                    {
                        throw new EncogError("Unknown architecture element: "
                                             + architecture + ", can't parse: " + part);
                    }

                    result.AddLayer(new BasicLayer(af, bias, layer.Count));
                }
            }

            result.Structure.FinalizeStructure();
            result.Reset();

            return(result);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Construct a network analyze class.  Analyze the specified network.
        /// </summary>
        /// <param name="network">The network to analyze.</param>
        public AnalyzeNetwork(BasicNetwork network)
        {
            int            assignDisabled = 0;
            int            assignedTotal  = 0;
            IList <double> biasList       = new List <double>();
            IList <double> weightList     = new List <double>();
            IList <double> allList        = new List <double>();

            foreach (ILayer layer in network.Structure.Layers)
            {
                if (layer.HasBias)
                {
                    for (int i = 0; i < layer.NeuronCount; i++)
                    {
                        biasList.Add(layer.BiasWeights[i]);
                        allList.Add(layer.BiasWeights[i]);
                    }
                }
            }

            foreach (ISynapse synapse in network.Structure.Synapses)
            {
                if (synapse.MatrixSize > 0)
                {
                    for (int from = 0; from < synapse.FromNeuronCount; from++)
                    {
                        for (int to = 0; to < synapse.ToNeuronCount; to++)
                        {
                            if (network.IsConnected(synapse, from, to))
                            {
                                double d = synapse.WeightMatrix[from, to];
                                weightList.Add(d);
                                allList.Add(d);
                            }
                            else
                            {
                                assignDisabled++;
                            }
                            assignedTotal++;
                        }
                    }
                }
            }

            this.disabledConnections = assignDisabled;
            this.totalConnections    = assignedTotal;
            this.weights             = new NumericRange(weightList);
            this.bias           = new NumericRange(biasList);
            this.weightsAndBias = new NumericRange(allList);
            this.weightValues   = EngineArray.ListToDouble(weightList);
            this.allValues      = EngineArray.ListToDouble(allList);
            this.biasValues     = EngineArray.ListToDouble(biasList);
        }
Exemplo n.º 32
0
 /// <summary>
 /// Construct an RPROP job. For more information on RPROP see the
 /// ResilientPropagation class. 
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data to use.</param>
 /// <param name="loadToMemory">True if binary training data should be loaded to memory.</param>
 /// <param name="initialUpdate">The initial update.</param>
 /// <param name="maxStep">The max step.</param>
 /// <param name="localRatio">The local ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="globalRatio">The global ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="segmentationRatio">The segmentation ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="iterationsPer">How many iterations to process per cycle.</param>
 public RPROPJob(BasicNetwork network, INeuralDataSet training,
         bool loadToMemory, double initialUpdate,
         double maxStep, double localRatio, int globalRatio, double segmentationRatio, int iterationsPer) :
     base(network, training, loadToMemory)
 {
     this.InitialUpdate = initialUpdate;
     this.MaxStep = maxStep;
     this.LocalRatio = localRatio;
     this.GlobalRatio = globalRatio;
     this.SegmentationRatio = segmentationRatio;
     this.IterationsPer = iterationsPer;
 }
Exemplo n.º 33
0
        /// <summary>
        /// Determine if the specified neural network can be flat. If it can a null
        /// is returned, otherwise, an error is returned to show why the network
        /// cannot be flattened.
        /// </summary>
        /// <param name="eml">The network to check.</param>
        /// <returns>Null, if the net can not be flattened, an error message
        /// otherwise.</returns>
        public override String IsValid(IEngineMachineLearning eml)
        {
            if (!(eml is BasicNetwork))
            {
                return("Only a BasicNetwork can be converted to a flat network.");
            }

            BasicNetwork network = (BasicNetwork)eml;

            ILayer inputLayer  = network.GetLayer(BasicNetwork.TAG_INPUT);
            ILayer outputLayer = network.GetLayer(BasicNetwork.TAG_OUTPUT);

            if (inputLayer == null)
            {
                return("To convert to a flat network, there must be an input layer.");
            }

            if (outputLayer == null)
            {
                return("To convert to a flat network, there must be an output layer.");
            }

            if (!(network.Logic is FeedforwardLogic) ||
                (network.Logic is ThermalLogic))
            {
                return("To convert to flat, must be using FeedforwardLogic or SimpleRecurrentLogic.");
            }

            foreach (ILayer layer in network.Structure.Layers)
            {
                if (layer.Next.Count > 2)
                {
                    return("To convert to flat a network must have at most two outbound synapses.");
                }

                if (layer.GetType() != typeof(ContextLayer) &&
                    layer.GetType() != typeof(BasicLayer) &&
                    layer.GetType() != typeof(RadialBasisFunctionLayer))
                {
                    return("To convert to flat a network must have only BasicLayer and ContextLayer layers.");
                }
            }

            foreach (ISynapse synapse in network.Structure.Synapses)
            {
                if (synapse is NEATSynapse)
                {
                    return("A NEAT synapse cannot be flattened.");
                }
            }

            return(null);
        }
        public void TestNeuronSignificance()
        {
            BasicNetwork   network   = ObtainNetwork();
            PruneSelective prune     = new PruneSelective(network);
            double         inputSig  = prune.DetermineNeuronSignificance(0, 1);
            double         hiddenSig = prune.DetermineNeuronSignificance(1, 1);
            double         outputSig = prune.DetermineNeuronSignificance(2, 1);

            Assert.AreEqual(63.0, inputSig, 0.01);
            Assert.AreEqual(95.0, hiddenSig, 0.01);
            Assert.AreEqual(26.0, outputSig, 0.01);
        }
Exemplo n.º 35
0
 /// <summary>
 /// Construct an RPROP job. For more information on RPROP see the
 /// ResilientPropagation class.
 /// </summary>
 /// <param name="network">The network to train.</param>
 /// <param name="training">The training data to use.</param>
 /// <param name="loadToMemory">True if binary training data should be loaded to memory.</param>
 /// <param name="initialUpdate">The initial update.</param>
 /// <param name="maxStep">The max step.</param>
 /// <param name="localRatio">The local ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="globalRatio">The global ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="segmentationRatio">The segmentation ratio, used if this job is performed by an OpenCL Device.</param>
 /// <param name="iterationsPer">How many iterations to process per cycle.</param>
 public RPROPJob(BasicNetwork network, INeuralDataSet training,
                 bool loadToMemory, double initialUpdate,
                 double maxStep, double localRatio, int globalRatio, double segmentationRatio, int iterationsPer) :
     base(network, training, loadToMemory)
 {
     this.InitialUpdate     = initialUpdate;
     this.MaxStep           = maxStep;
     this.LocalRatio        = localRatio;
     this.GlobalRatio       = globalRatio;
     this.SegmentationRatio = segmentationRatio;
     this.IterationsPer     = iterationsPer;
 }
Exemplo n.º 36
0
        private static Task <EncogTrainingResponse> GetTrainedNetworkAsync2(TrainingData training, double hiddenMultiplier, double maxError, CancellationToken cancelToken, double?maxSeconds = null)
        {
            return(Task.Run(() =>
            {
                // Create a network, build input/hidden/output layers
                //TODO: Figure out why telling it to use the faster activation functions always causes the trainer to return an error of NaN
                BasicNetwork network = CreateNetwork2(training, hiddenMultiplier, false);

                // Train the network
                return TrainNetwork2(network, training, maxError, cancelToken, maxSeconds);        // lower score is better
            }, cancelToken));
        }
Exemplo n.º 37
0
        /// <summary>
        /// Use an array to populate the memory of the neural network.
        /// </summary>
        /// <param name="array">An array of doubles.</param>
        /// <param name="network">The network to encode.</param>
        public static void ArrayToNetwork(double[] array,
                 BasicNetwork network)
        {
            int index = 0;

            foreach (ILayer layer in network.Structure.Layers)
            {
                index = NetworkCODEC.ProcessLayer(network, layer, array, index);
            }

            network.Structure.FlatUpdate = FlatUpdateNeeded.Flatten;
        }
Exemplo n.º 38
0
 private void init(int layers = 4, int neuronsPerLayer = 15)
 {
     Network = new BasicNetwork();
     Network.AddLayer(new BasicLayer(null, true, inputNumber));
     for (int i = 0; i < layers; i++)
     {
         Network.AddLayer(new BasicLayer(new ActivationElliott(), true, neuronsPerLayer));
     }
     Network.AddLayer(new BasicLayer(new ActivationSoftMax(), false, 6));
     Network.Structure.FinalizeStructure();
     Network.Reset();
 }
Exemplo n.º 39
0
 private GameState GetMinMax(List <MinMaxBranch> Branches, BasicNetwork Network)
 {
     foreach (var b in Branches)
     {
         foreach (var l in b.Leafs)
         {
             var Compute = ((BasicMLData)Network.Compute(l.Head.GetData()));
             l.Score = Compute.Data[0];
         }
     }
     return(Branches.OrderBy(x => x.GetScore()).First().GetFirstChoice());
 }
Exemplo n.º 40
0
        static BasicNetwork UtworzSiecDoRegresji()
        {
            BasicNetwork network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 1));                     //Neurony wejsciowe
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 20)); //Warstwa ukryta
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1)); //Wyjścia
            network.Structure.FinalizeStructure();
            network.Reset();

            return(network);
        }
Exemplo n.º 41
0
        private string SaveNetwork(BasicNetwork network)
        {
            double[] data = new double[network.EncodedArrayLength()];
            network.EncodeToArray(data);
            List <byte> netData = new List <byte>();

            foreach (double d in data)
            {
                netData.AddRange(BitConverter.GetBytes(d));
            }
            return(Convert.ToBase64String(netData.ToArray()));
        }
        public double EvaluateRandomizer(IRandomizer randomizer,
                                         BasicNetwork network, IMLDataSet training)
        {
            double total = 0;

            for (int i = 0; i < SAMPLE_SIZE; i++)
            {
                randomizer.Randomize(network);
                total += Evaluate(network, training);
            }
            return(total / SAMPLE_SIZE);
        }
Exemplo n.º 43
0
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            renderer = new Render(spriteBatch, GraphicsDevice);
            graphics.PreferredBackBufferWidth  = 700; // set this value to the desired width of your window
            graphics.PreferredBackBufferHeight = 700; // set this value to the desired height of your window
            graphics.ApplyChanges();

            net       = Trainer.train(renderer);
            debugGame = new SnakeGame();
        }
        private void ProcessNetwork()
        {
            app.WriteLine("Downsampling images...");

            foreach (ImagePair pair in imageList)
            {
                IMLData ideal = new BasicMLData(outputCount);
                int     idx   = pair.Identity;
                for (int i = 0; i < outputCount; i++)
                {
                    if (i == idx)
                    {
                        ideal.Data[i] = 1;
                    }
                    else
                    {
                        ideal.Data[i] = -1;
                    }
                }

                try
                {
                    var img  = new Bitmap(pair.File);
                    var data = new ImageMLData(img);
                    training.Add(data, ideal);
                }
                catch (Exception e)
                {
                    app.WriteLine("Error loading: " + pair.File
                                  + ": " + e.Message);
                }
            }

            String strHidden1 = GetArg("hidden1");
            String strHidden2 = GetArg("hidden2");

            if (training.Count == 0)
            {
                app.WriteLine("No images to create network for.");
                return;
            }

            training.Downsample(downsampleHeight, downsampleWidth);

            int hidden1 = int.Parse(strHidden1);
            int hidden2 = int.Parse(strHidden2);

            network = EncogUtility.SimpleFeedForward(training
                                                     .InputSize, hidden1, hidden2,
                                                     training.IdealSize, true);
            app.WriteLine("Created network: " + network);
        }
Exemplo n.º 45
0
 /// <summary>
 ///     Construct the backpropagation trainer.
 /// </summary>
 /// <param name="theNetwork">The network to train.</param>
 /// <param name="theTraining">The training data to use.</param>
 /// <param name="theLearningRate">The learning rate.  Can be changed as training runs.</param>
 /// <param name="theMomentum">The momentum.  Can be changed as training runs.</param>
 public BackPropagation(BasicNetwork theNetwork, IList<BasicData> theTraining, double theLearningRate,
     double theMomentum)
 {
     BatchSize = 500;
     Stochastic = new MersenneTwisterGenerateRandom();
     NesterovUpdate = true;
     _network = theNetwork;
     _training = theTraining;
     LearningRate = theLearningRate;
     Momentum = theMomentum;
     _gradients = new GradientCalc(_network, new CrossEntropyErrorFunction(), this);
     _lastDelta = new double[theNetwork.Weights.Length];
 }
Exemplo n.º 46
0
        public void Train(BasicNetwork network, IMLDataSet training)
        {
            ITrain train = new ResilientPropagation(network, training);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > MaxError);
        }
Exemplo n.º 47
0
        /// <summary>
        /// Setup the network logic, read parameters from the network.
        /// </summary>
        /// <param name="network">The network that this logic class belongs to.</param>
        public override void Init(BasicNetwork network)
        {
            base.Init(network);

            ILayer layer = this.Network.GetLayer(BasicNetwork.TAG_INPUT);

            this.on  = new int[layer.NeuronCount];
            this.off = new int[layer.NeuronCount];

            this.temperature  = this.Network.GetPropertyDouble(BoltzmannLogic.PROPERTY_TEMPERATURE);
            this.runCycles    = (int)this.Network.GetPropertyLong(BoltzmannLogic.PROPERTY_RUN_CYCLES);
            this.annealCycles = (int)this.Network.GetPropertyLong(BoltzmannLogic.PROPERTY_ANNEAL_CYCLES);
        }
Exemplo n.º 48
0
        public BasicNetwork GetNetwork()
        {
            var network = new BasicNetwork();

            foreach (NeuralLayerInfo layer in Layers)
            {
                var objLayer = layer.GetLayer();
                network.AddLayer(objLayer);
            }
            network.FinalizeStructure();
            network.Reset();
            return(network);
        }
        public void Execute(IExampleInterface app)
        {
            this.app = app;

            BasicNetwork network = generateNetwork();
            IMLDataSet   data    = generateTraining();

            double rprop  = EvaluateRPROP(network, data);
            double mprop  = EvaluateMPROP(network, data);
            double factor = rprop / mprop;

            Console.WriteLine("Factor improvement:" + factor);
        }
Exemplo n.º 50
0
        /// <summary>
        /// Construct an ADALINE trainer.
        /// </summary>
        ///
        /// <param name="network">The network to train.</param>
        /// <param name="training">The training data.</param>
        /// <param name="learningRate">The learning rate.</param>
        public TrainAdaline(BasicNetwork network, IMLDataSet training,
                            double learningRate) : base(TrainingImplementationType.Iterative)
        {
            if (network.LayerCount > 2)
            {
                throw new NeuralNetworkError(
                    "An ADALINE network only has two layers.");
            }
            _network = network;

            _training = training;
            _learningRate = learningRate;
        }
Exemplo n.º 51
0
 /// <summary>
 ///     Construct the backpropagation trainer.
 /// </summary>
 /// <param name="theNetwork">The network to train.</param>
 /// <param name="theTraining">The training data to use.</param>
 /// <param name="theLearningRate">The learning rate.  Can be changed as training runs.</param>
 /// <param name="theMomentum">The momentum.  Can be changed as training runs.</param>
 public BackPropagation(BasicNetwork theNetwork, IList <BasicData> theTraining, double theLearningRate,
                        double theMomentum)
 {
     BatchSize      = 500;
     Stochastic     = new MersenneTwisterGenerateRandom();
     NesterovUpdate = true;
     _network       = theNetwork;
     _training      = theTraining;
     LearningRate   = theLearningRate;
     Momentum       = theMomentum;
     _gradients     = new GradientCalc(_network, new CrossEntropyErrorFunction(), this);
     _lastDelta     = new double[theNetwork.Weights.Length];
 }
Exemplo n.º 52
0
        /// <summary>
        /// Construct a training job. 
        /// </summary>
        /// <param name="network">The network to train.</param>
        /// <param name="training">The training data to use.</param>
        /// <param name="loadToMemory">True, if binary data should be loaded to memory.</param>
        public TrainingJob(BasicNetwork network,
                 INeuralDataSet training, bool loadToMemory)
            : base()
        {

            this.Network = network;
            this.Training = training;
            this.LoadToMemory = loadToMemory;
            this.IterationsPer = 1;
            this.LocalRatio = 1.0;
            this.GlobalRatio = 1;
            this.SegmentationRatio = 1.0;

        }
 /// <summary>
 /// Construct a simulated annleaing trainer for a feedforward neural network.
 /// </summary>
 ///
 /// <param name="network">The neural network to be trained.</param>
 /// <param name="calculateScore">Used to calculate the score for a neural network.</param>
 /// <param name="startTemp">The starting temperature.</param>
 /// <param name="stopTemp">The ending temperature.</param>
 /// <param name="cycles">The number of cycles in a training iteration.</param>
 public NeuralSimulatedAnnealing(BasicNetwork network,
                                 ICalculateScore calculateScore, double startTemp,
                                 double stopTemp, int cycles) : base(TrainingImplementationType.Iterative)
 {
     _network = network;
     _calculateScore = calculateScore;
     _anneal = new NeuralSimulatedAnnealingHelper(this)
                   {
                       Temperature = startTemp,
                       StartTemperature = startTemp,
                       StopTemperature = stopTemp,
                       Cycles = cycles
                   };
 }
Exemplo n.º 54
0
        /// <summary>
        /// Construct he ADALINE trainer.
        /// </summary>
        /// <param name="network">The network to train.</param>
        /// <param name="training">The training set.</param>
        /// <param name="learningRate">The learning rate.</param>
        public TrainAdaline(BasicNetwork network, INeuralDataSet training,
                double learningRate)
        {
            if (network.Structure.Layers.Count > 2)
                throw new NeuralNetworkError(
                        "An ADALINE network only has two layers.");
            this.network = network;

            ILayer input = network.GetLayer(BasicNetwork.TAG_INPUT);

            this.synapse = input.Next[0];
            this.training = training;
            this.learningRate = learningRate;
        }
Exemplo n.º 55
0
        /// <summary>
        ///     The Xaiver initialization works layer by layer.
        /// </summary>
        /// <param name="network">The network.</param>
        /// <param name="fromLayer">The source layer.</param>
        private void RandomizeLayer(BasicNetwork network, int fromLayer)
        {
            var fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            var toCount = network.Layers[fromLayer + 1].Count;

            for (var fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
            {
                for (var toNeuron = 0; toNeuron < toCount; toNeuron++)
                {
                    var sigma = Math.Sqrt(2.0/(fromCount + toCount));
                    var w = Rnd.NextGaussian()*sigma;
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, w);
                }
            }
        }
Exemplo n.º 56
0
        /// <summary>
        /// Construct a neural genome.
        /// </summary>
        ///
        /// <param name="network">The network to use.</param>
        public NeuralGenome(BasicNetwork network)
        {
            Organism = network;

            _networkChromosome = new Chromosome();

            // create an array of "double genes"
            int size = network.Structure.CalculateSize();
            for (int i = 0; i < size; i++)
            {
                IGene gene = new DoubleGene();
                _networkChromosome.Genes.Add(gene);
            }

            Chromosomes.Add(_networkChromosome);

            Encode();
        }
Exemplo n.º 57
0
        /// <summary>
        /// Construct a neural network genome.
        /// </summary>
        /// <param name="nga">The neural genetic algorithm.</param>
        /// <param name="network">The network.</param>
        public NeuralGenome(NeuralGeneticAlgorithm nga, BasicNetwork network)
            : base(nga.Helper)
        {
            this.Organism = network;
            this.networkChromosome = new Chromosome();

            // create an array of "double genes"
            int size = network.Structure.CalculateSize();
            for (int i = 0; i < size; i++)
            {
                IGene gene = new DoubleGene();
                this.networkChromosome.Genes.Add(gene);
            }

            this.Chromosomes.Add(this.networkChromosome);

            Encode();
        }
        /// <summary>
        /// Construct a Manhattan propagation training object. 
        /// </summary>
        /// <param name="network">The network to train.</param>
        /// <param name="training">The training data to use.</param>
        /// <param name="profile">The learning rate.</param>
        /// <param name="learnRate">The OpenCL profile to use, null for CPU.</param>
        public ManhattanPropagation(BasicNetwork network,
                 INeuralDataSet training, OpenCLTrainingProfile profile, double learnRate)
            : base(network, training)
        {

            if (profile == null)
            {
                FlatTraining = new TrainFlatNetworkManhattan(
                        network.Structure.Flat,
                        this.Training,
                        learnRate);
            }
#if !SILVERLIGHT
            else
            {
                TrainFlatNetworkOpenCL rpropFlat = new TrainFlatNetworkOpenCL(
                        network.Structure.Flat, this.Training,
                        profile);
                rpropFlat.LearnManhattan(learnRate);
                this.FlatTraining = rpropFlat;
            }
#endif
        }
Exemplo n.º 59
0
        /// <summary>
        /// Generate the Elman neural network.
        /// </summary>
        /// <returns>The Elman neural network.</returns>
        public BasicNetwork Generate()
        {
            int y = PatternConst.START_Y;
            ILayer input = new BasicLayer(this.activation, false,
                   this.inputNeurons);

            BasicNetwork result = new BasicNetwork();
            result.AddLayer(input);

            input.X = PatternConst.START_X;
            input.Y = y;
            y += PatternConst.INC_Y;

            foreach (int count in this.hidden)
            {

                ILayer hidden = new BasicLayer(
                       this.activation, true, count);

                result.AddLayer(hidden);
                hidden.X = PatternConst.START_X;
                hidden.Y = y;
                y += PatternConst.INC_Y;
            }

            ILayer output = new BasicLayer(this.activation, true,
                   this.outputNeurons);
            result.AddLayer(output);
            output.X = PatternConst.START_X;
            output.Y = y;
            y += PatternConst.INC_Y;

            result.Structure.FinalizeStructure();
            result.Reset();

            return result;
        }
Exemplo n.º 60
0
 /// <summary>
 /// Setup the network logic, read parameters from the network.
 /// </summary>
 /// <param name="network">The network that this logic class belongs to.</param>
 public virtual void Init(BasicNetwork network)
 {
     this.network = network;
 }