001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.libsvm;
018
019import com.oracle.labs.mlrg.olcut.config.Option;
020import org.tribuo.classification.ClassificationOptions;
021import org.tribuo.classification.Label;
022import org.tribuo.classification.libsvm.SVMClassificationType.SVMMode;
023import org.tribuo.common.libsvm.KernelType;
024import org.tribuo.common.libsvm.SVMParameters;
025
026/**
027 * CLI options for training a LibSVM classification model.
028 */
029public class LibSVMOptions implements ClassificationOptions<LibSVMClassificationTrainer> {
030
031    @Override
032    public String getOptionsDescription() {
033        return "Options for parameterising a LibSVM classification trainer.";
034    }
035
036    /**
037     * Intercept in kernel function. Defaults to 0.0.
038     */
039    @Option(longName = "svm-coefficient", usage = "Intercept in kernel function. Defaults to 0.0.")
040    public double svmCoefficient = 0.0;  //TODO should this be 1.0?
041    /**
042     * Degree in polynomial kernel. Defaults to 3.
043     */
044    @Option(longName = "svm-degree", usage = "Degree in polynomial kernel. Defaults to 3.")
045    public int svmDegree = 3;
046    /**
047     * Gamma value in kernel function. Defaults to 0.0.
048     */
049    @Option(longName = "svm-gamma", usage = "Gamma value in kernel function. Defaults to 0.0.")
050    public double svmGamma = 0.0;  //TODO should the default be 0.1
051    /**
052     * Type of SVM kernel. Defaults to LINEAR.
053     */
054    @Option(longName = "svm-kernel", usage = "Type of SVM kernel. Defaults to LINEAR.")
055    public KernelType svmKernel = KernelType.LINEAR;
056    /**
057     * Type of SVM. Defaults to C_SVC.
058     */
059    @Option(longName = "svm-type", usage = "Type of SVM. Defaults to C_SVC.")
060    public SVMClassificationType.SVMMode svmType = SVMMode.C_SVC;
061
062    @Override
063    public LibSVMClassificationTrainer getTrainer() {
064        SVMParameters<Label> parameters = new SVMParameters<>(new SVMClassificationType(svmType), svmKernel);
065        parameters.setGamma(svmGamma);
066        parameters.setCoeff(svmCoefficient);
067        parameters.setDegree(svmDegree);
068        return new LibSVMClassificationTrainer(parameters);
069    }
070
071}