Exemplo n.º 1
0
        public static NS.Tuple <Machine, Solver, LibSvmModel> train(Problem prob, Parameters parameters)
        {
            double[] w;
            double   Cp = parameters.Complexity;
            double   Cn = parameters.Complexity;

            if (parameters.ClassWeights != null)
            {
                for (int i = 0; i < parameters.ClassLabels.Count; i++)
                {
                    if (parameters.ClassLabels[i] == -1)
                    {
                        Cn *= parameters.ClassWeights[i];
                    }

                    else if (parameters.ClassLabels[i] == +1)
                    {
                        Cn *= parameters.ClassWeights[i];
                    }
                }
            }

            NS.Tuple <Machine, Solver> result = train_one(prob, parameters, out w, Cp, Cn);

            return(NS.Tuple.Create(result.Item1, result.Item2, new LibSvmModel()
            {
                Dimension = prob.Dimensions,
                Classes = 2,
                Labels = new[] { +1, -1 },
                Solver = parameters.Solver,
                Weights = w,
                Bias = 0
            }));
        }
Exemplo n.º 2
0
        public void Main(params string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            if (args.Length == 0)
            {
                return;
            }

            if (args[0] == "--self")
            {
                ExecuteTestAndExit(args);
            }


            string input_file_name;
            string model_file_name;

            // The parameters object contains information about which parameters we passed to
            // the command line and that should be used to control the training of the machine,
            // such as which solver to use, the model complexity C, etc.
            this.Parameters = parse_command_line(args, out input_file_name, out model_file_name);

            // The problem object contains the input and output data
            this.Problem = read_problem(input_file_name, Parameters.Bias);

            this.ErrorMessage = check_parameter(Problem, Parameters);

            if (!String.IsNullOrEmpty(ErrorMessage))
            {
                Console.WriteLine("ERROR:" + ErrorMessage);
                return;
            }

            // Learn the specified problem and register steps and obtained results
            NS.Tuple <Machine, Solver, LibSvmModel> result = train(Problem, Parameters);
            this.Machine = result.Item1; // The SVM actually created by Accord.NET
            this.Solver  = result.Item2; // The Accord.NET learning algorithm used.
            this.Model   = result.Item3; // LIBLINEAR's definition of what a SVM is

            try
            {
                // Save the model to disk
                Model.Save(model_file_name);
            }
            catch (Exception ex)
            {
                this.ErrorMessage = "can't save model to file " + model_file_name;
                Console.WriteLine(this.ErrorMessage);
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Reads a problem specified in LibSVM's sparse format.
        /// </summary>
        ///
        public static Problem read_problem(string filename, double bias)
        {
            // Create a LibSVM's sparse data reader
            var reader = new SparseReader(filename);

            if (bias > 0)
            {
                reader.Intercept = bias;
            }

            NS.Tuple <Sparse <double>[], double[]> r = reader.ReadSparseToEnd();
            Sparse <double>[] x = r.Item1;
            double[]          y = r.Item2;

            return(new Problem()
            {
                Dimensions = reader.Dimensions,
                Inputs = x,
                Outputs = y,
            });
        }