Esempio n. 1
0
        private TreeModel[] GetTreeModelsInParallel(byte[][] points, byte[] targets, Random[] randoms)
        {
            TreeModel[] treeModels = new TreeModel[TreeCount];

            int treesTrainedCount = 0;
            var rangePartitioner  = Partitioner.Create(0, TreeCount);

            ParallelOptions parallelOptions = new ParallelOptions();

            parallelOptions.CancellationToken = _cancellationToken;

            Parallel.ForEach(rangePartitioner, parallelOptions, range =>
            {
                for (int i = range.Item1; i < range.Item2; ++i)
                {
                    TreeTrainer trainer = new TreeTrainer(BandCountPerSplit, MaxTreeHeight, MinNodeSize, BootstrappingRatio, randoms[i]);
                    treeModels[i]       = trainer.Train(points, targets, _cancellationToken);

                    Interlocked.Increment(ref treesTrainedCount);
                    _progress.Report(ClusteringProgress.StepTaken(treesTrainedCount));
                    parallelOptions.CancellationToken.ThrowIfCancellationRequested();
                }
            });

            return(treeModels);
        }
Esempio n. 2
0
        private RandomForestModel GetModel(byte[][] points, byte[] targets, int bandCount)
        {
            TreeModel[] treeModels = new TreeModel[TreeCount];

            TreeTrainer trainer = new TreeTrainer(BandCountPerSplit, MaxTreeHeight, MinNodeSize, BootstrappingRatio, new Random(_random.Next()));

            for (int i = 0; i < TreeCount; ++i)
            {
                treeModels[i] = trainer.Train(points, targets, _cancellationToken);

                _progress.Report(ClusteringProgress.StepTaken(i + 1));
            }

            return(new RandomForestModel(treeModels, bandCount));
        }