Package smile.hpo

Class Hyperparameters

java.lang.Object
smile.hpo.Hyperparameters

public class Hyperparameters extends Object
Hyperparameter configuration. A hyperparameter is a parameter whose value is set before the learning process begins. By contrast, the values of other parameters are derived via training.

Hyperparameters can be classified as model hyperparameters, that cannot be inferred while fitting the machine to the training set because they refer to the model selection task, or algorithm hyperparameters, that in principle have no influence on the performance of the model but affect the speed and quality of the learning process. For example, the topology and size of a neural network are model hyperparameters, while learning rate and mini-batch size are algorithm hyperparameters.

The below example shows how to tune the hyperparameters of random forest.

 
    import smile.io.*;
    import smile.data.formula.Formula;
    import smile.validation.*;
    import smile.classification.RandomForest;

    var hp = new Hyperparameters()
        .add("smile.random.forest.trees", 100) // a fixed value
        .add("smile.random.forest.mtry", new int[] {2, 3, 4}) // an array of values to choose
        .add("smile.random.forest.max.nodes", 100, 500, 50); // range [100, 500] with step 50

    var train = Read.arff("data/weka/segment-challenge.arff");
    var test = Read.arff("data/weka/segment-test.arff");
    var formula = Formula.lhs("class");
    var testy = formula.y(test).toIntArray();

    hp.grid().forEach(prop -> {
        var model = RandomForest.fit(formula, train, prop);
        var pred = model.predict(test);
        System.out.println(prop);
        System.out.format("Accuracy = %.2f%%%n", (100.0 * Accuracy.of(testy, pred)));
        System.out.println(ConfusionMatrix.of(testy, pred));
    });
 
 
  • Constructor Details

    • Hyperparameters

      public Hyperparameters()
      Constructor.
  • Method Details

    • add

      public Hyperparameters add(String name, int value)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      value - a fixed value of parameter.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, double value)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      value - a fixed value of parameter.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, String value)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      value - a fixed value of parameter.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, int[] values)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      values - an array of parameter values.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, double[] values)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      values - an array of parameter values.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, String[] values)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      values - an array of parameter values.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, int start, int end)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      start - the start of value range (inclusive).
      end - the end of value range (inclusive).
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, int start, int end, int step)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      start - the start of value range (inclusive).
      end - the end of value range (inclusive).
      step - the step size.
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, double start, double end)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      start - the start of value range (inclusive).
      end - the end of value range (inclusive).
      Returns:
      this object.
    • add

      public Hyperparameters add(String name, double start, double end, double step)
      Adds a parameter.
      Parameters:
      name - the parameter name.
      start - the start of value range (inclusive).
      end - the end of value range (inclusive).
      step - the step size.
      Returns:
      this object.
    • random

      public Stream<Properties> random()
      Generates a stream of hyperparameters for random search.
      Returns:
      the stream of hyperparameters for random search.
    • grid

      public Stream<Properties> grid()
      Generates a stream of hyperparameters for grid search.
      Returns:
      the stream of hyperparameters for grid search.