Пример #1
0
 public IMLMethod Decode(IGenome genome)
 {
     var result = new RBFNetwork(_inputCount, _rbfCount, _outputCount);
     var dag = (DoubleArrayGenome) genome;
     Array.Copy(dag.Data, 0, result.LongTermMemory, 0, _size);
     return result;
 }
Пример #2
0
        /// <summary>
        ///     Create an initial population.
        /// </summary>
        /// <param name="rnd">Random number generator.</param>
        /// <param name="codec">The codec, the type of network to use.</param>
        /// <returns>The population.</returns>
        public static IPopulation InitPopulation(IGenerateRandom rnd, RBFNetworkGenomeCODEC codec)
        {
            // Create a RBF network to get the length.
            var network = new RBFNetwork(codec.InputCount, codec.RbfCount, codec.OutputCount);
            int size = network.LongTermMemory.Length;

            // Create a new population, use a single species.
            IPopulation result = new BasicPopulation(PopulationSize, new DoubleArrayGenomeFactory(size));
            var defaultSpecies = new BasicSpecies {Population = result};
            result.Species.Add(defaultSpecies);

            // Create a new population of random networks.
            for (int i = 0; i < PopulationSize; i++)
            {
                var genome = new DoubleArrayGenome(size);
                network.Reset(rnd);
                Array.Copy(network.LongTermMemory, 0, genome.Data, 0, size);
                defaultSpecies.Add(genome);
            }

            // Set the genome factory to use the double array genome.
            result.GenomeFactory = new DoubleArrayGenomeFactory(size);

            return result;
        }
Пример #3
0
        public IMLMethod Decode(IGenome genome)
        {
            var result = new RBFNetwork(_inputCount, _rbfCount, _outputCount);
            var dag    = (DoubleArrayGenome)genome;

            Array.Copy(dag.Data, 0, result.LongTermMemory, 0, _size);
            return(result);
        }
Пример #4
0
 public RBFNetworkGenomeCODEC(int inputCount, int rbfCount, int outputCount)
 {
     _inputCount = inputCount;
     _rbfCount = rbfCount;
     _outputCount = outputCount;
     var temp = new RBFNetwork(inputCount, rbfCount, outputCount);
     _size = temp.LongTermMemory.Length;
 }
Пример #5
0
        public RBFNetworkGenomeCODEC(int inputCount, int rbfCount, int outputCount)
        {
            _inputCount  = inputCount;
            _rbfCount    = rbfCount;
            _outputCount = outputCount;
            var temp = new RBFNetwork(inputCount, rbfCount, outputCount);

            _size = temp.LongTermMemory.Length;
        }
Пример #6
0
        /// <summary>
        ///     Prepare a Kaggle submission for Titanic.
        /// </summary>
        /// <param name="dataPath">The data path.</param>
        /// <param name="bestNetwork">The best network.</param>
        /// <param name="cross">The cross validated data.</param>
        public void Submit(string dataPath, RBFNetwork bestNetwork, CrossValidate cross)
        {
            String now = new DateTime().ToString("yyyyMMddhhmm");
            string trainingPath = Path.Combine(dataPath, TitanicConfig.TrainingFilename);
            string testPath = Path.Combine(dataPath, TitanicConfig.TestFilename);
            var score = (int) (cross.Score*10000);
            string submitPath = Path.Combine(dataPath, "submit-" + now + "_" + score + ".csv");
            string submitInfoPath = Path.Combine(dataPath, "submit-" + now + ".txt");

            using (var file = new StreamWriter(submitInfoPath))
            {
                file.WriteLine("Crossvalidation stats:");
                for (int i = 0; i < cross.Count; i++)
                {
                    CrossValidateFold fold = cross.Folds[i];
                    file.WriteLine("Fold #" + (i + 1) + " : Score: " + fold.Score);
                }
                file.WriteLine("Average Score: " + cross.Score);
                file.WriteLine();
                file.WriteLine(String.Join(",", bestNetwork.LongTermMemory));
            }

            var stats = new TitanicStats();
            NormalizeTitanic.Analyze(stats, trainingPath);
            NormalizeTitanic.Analyze(stats, testPath);

            var ids = new List<String>();
            IList<BasicData> training = NormalizeTitanic.Normalize(stats, testPath, ids,
                TitanicConfig.InputNormalizeLow,
                TitanicConfig.InputNormalizeHigh,
                TitanicConfig.PredictSurvive,
                TitanicConfig.PredictPerish);

            int idx = 0;
            using (var streamWriter = new StreamWriter(submitPath))
            using (var writer = new CsvWriter(streamWriter))
            {
                writer.WriteField("PassengerId");
                writer.WriteField("Survived");
                writer.NextRecord();

                foreach (BasicData data in training)
                {
                    double[] output = bestNetwork.ComputeRegression(data.Input);
                    int survived = output[0] > 0.5 ? 1 : 0;

                    writer.WriteField(ids[idx]);
                    writer.WriteField(survived);
                    writer.NextRecord();
                    idx++;
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Train one fold.
        /// </summary>
        /// <param name="k">The fold id.</param>
        /// <param name="fold">The fold.</param>
        public void TrainFold(int k, CrossValidateFold fold)
        {
            int noImprove = 0;
            double localBest = 0;

            // Get the training and cross validation sets.
            IList<BasicData> training = fold.TrainingSet;
            IList<BasicData> validation = fold.ValidationSet;

            // Create random particles for the RBF.
            IGenerateRandom rnd = new MersenneTwisterGenerateRandom();
            var particles = new RBFNetwork[TitanicConfig.ParticleCount];
            for (int i = 0; i < particles.Length; i++)
            {
                particles[i] = new RBFNetwork(TitanicConfig.InputFeatureCount, TitanicConfig.RbfCount, 1);
                particles[i].Reset(rnd);
            }

            /**
             * Construct a network to hold the best network.
             */
            if (_bestNetwork == null)
            {
                _bestNetwork = new RBFNetwork(TitanicConfig.InputFeatureCount, TitanicConfig.RbfCount, 1);
            }

            /**
             * Setup the scoring function.
             */
            IScoreFunction score = new ScoreTitanic(training);
            IScoreFunction scoreValidate = new ScoreTitanic(validation);

            /**
             * Setup particle swarm.
             */
            bool done = false;
            var train = new TrainPSO(particles, score);
            int iterationNumber = 0;
            var line = new StringBuilder();

            do
            {
                iterationNumber++;

                train.Iteration();

                var best = (RBFNetwork) train.BestParticle;

                double trainingScore = train.LastError;
                double validationScore = scoreValidate.CalculateScore(best);

                if (validationScore > _bestScore)
                {
                    Array.Copy(best.LongTermMemory, 0, _bestNetwork.LongTermMemory, 0, best.LongTermMemory.Length);
                    _bestScore = validationScore;
                }

                if (validationScore > localBest)
                {
                    noImprove = 0;
                    localBest = validationScore;
                }
                else
                {
                    noImprove++;
                }

                line.Length = 0;
                line.Append("Fold #");
                line.Append(k + 1);
                line.Append(", Iteration #");
                line.Append(iterationNumber);
                line.Append(": training correct: ");
                line.Append(trainingScore);
                line.Append(", validation correct: ");
                line.Append(validationScore);
                line.Append(", no improvement: ");
                line.Append(noImprove);

                if (noImprove > TitanicConfig.AllowNoImprovement)
                {
                    done = true;
                }

                Console.WriteLine(line.ToString());
            } while (!done);

            fold.Score = localBest;
        }
Пример #8
0
        /// <summary>
        ///     Run the example.
        /// </summary>
        public void Process()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream res = assembly.GetManifestResourceStream("AIFH_Vol2.Resources.iris.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();

            IGenerateRandom rnd = new MersenneTwisterGenerateRandom();

            // The following ranges are setup for the Iris data set.  If you wish to normalize other files you will
            // need to modify the below function calls other files.
            ds.NormalizeRange(0, -1, 1);
            ds.NormalizeRange(1, -1, 1);
            ds.NormalizeRange(2, -1, 1);
            ds.NormalizeRange(3, -1, 1);
            IDictionary<string, int> species = ds.EncodeOneOfN(4);

            var particles = new RBFNetwork[ParticleCount];
            for (int i = 0; i < particles.Length; i++)
            {
                particles[i] = new RBFNetwork(4, 4, 3);
                particles[i].Reset(rnd);
            }

            IList<BasicData> trainingData = ds.ExtractSupervised(0, 4, 4, 3);

            IScoreFunction score = new ScoreRegressionData(trainingData);

            var train = new TrainPSO(particles, score);

            PerformIterations(train, 100000, 0.05, true);

            var winner = (RBFNetwork) train.BestParticle;

            QueryOneOfN(winner, trainingData, species);
        }
Пример #9
0
        /// <summary>
        ///     Run the example.
        /// </summary>
        public void Process()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream res = assembly.GetManifestResourceStream("AIFH_Vol2.Resources.iris.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();

            // The following ranges are setup for the Iris data set.  If you wish to normalize other files you will
            // need to modify the below function calls other files.
            ds.NormalizeRange(0, -1, 1);
            ds.NormalizeRange(1, -1, 1);
            ds.NormalizeRange(2, -1, 1);
            ds.NormalizeRange(3, -1, 1);
            IDictionary<string, int> species = ds.EncodeOneOfN(4);
            istream.Close();

            var network = new RBFNetwork(4, 4, 3);

            IList<BasicData> trainingData = ds.ExtractSupervised(0, 4, 4, 3);

            IScoreFunction score = new ScoreRegressionData(trainingData);

            var train = new ContinuousACO(network, score, 30);

            PerformIterations(train, 100000, 0.05, true);

            train.FinishTraining();

            QueryOneOfN(network, trainingData, species);
        }