public void Train()
        {
            log.Info("Test");
            var problem = LoadData();

            parameters.Weights = WeightCalculation.GetWeights(problem.Y);
            model.Train(problem, parameters);
        }
Example #2
0
        public void GetWeights(int[] values, double[] result)
        {
            var weights = WeightCalculation.GetWeights(values);

            for (int i = 0; i < result.Length; i = i + 2)
            {
                Assert.AreEqual(result[i + 1], Math.Round(weights[(int)result[i]], 2));
            }
        }
        public void PerformCrossValidation(bool scaling, double expected)
        {
            log.Info("Test");
            var problem = LoadData(scaling);

            parameters.Weights = WeightCalculation.GetWeights(problem.Y);

            double test = model.PerformCrossValidation(problem, parameters, 5);

            Assert.AreEqual(expected, Math.Round(test, 2));
        }
Example #4
0
        public IParameterSelection Create(TrainingHeader header, IArffDataSet dataset)
        {
            Guard.NotNull(() => header, header);
            Guard.NotNull(() => dataset, dataset);
            Parameter defaultParameter = new Parameter();

            defaultParameter.KernelType = header.Kernel;
            defaultParameter.CacheSize  = 200;
            defaultParameter.SvmType    = header.SvmType;
            var model = new TrainingModel(header);

            if (!header.GridSelection)
            {
                return(new NullParameterSelection(defaultParameter, model));
            }

            GridSearchParameters searchParameters;

            logger.Info("Investigate LibLinear");
            if (header.Kernel == KernelType.Linear)
            {
                var gamma = GetList(1, 1, 1);
                if (dataset.Header.Total > (dataset.TotalDocuments * 10))
                {
                    logger.Info("Selecting Linear features >> instances");
                    defaultParameter.Shrinking = false;
                }
                else
                {
                    logger.Warn("Investigate LibLinear");
                }

                var training = problemFactory.Construct(dataset).GetProblem();
                defaultParameter.Weights = WeightCalculation.GetWeights(training.Y);
                foreach (var classItem in defaultParameter.Weights)
                {
                    logger.Info($"Using class [{classItem.Key}] with weight [{classItem.Value}]");
                }

                searchParameters = new GridSearchParameters(3, GetList(-1, 2, 1), gamma, defaultParameter);
            }
            else
            {
                searchParameters = new GridSearchParameters(3, GetList(-5, 15, 2), GetList(-15, 3, 2), defaultParameter);
            }

            return(new GridParameterSelection(taskFactory, model, searchParameters));
        }
        public async Task Cancel()
        {
            log.Info("Cancel");
            var problem = LoadData();

            parameters.Weights = WeightCalculation.GetWeights(problem.Y);

            CancellationTokenSource source = new CancellationTokenSource();

            parameters.Token = source.Token;
            var task = Task.Run(() => model.PerformCrossValidation(problem, parameters, 5));
            await Task.Delay(100).ConfigureAwait(false);

            source.Cancel();
            try
            {
                task.Wait(5000);
            }
            catch
            {
            }

            Assert.IsTrue(task.IsFaulted);
        }