Class Hyperparameters
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 using grid search.
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}) // discrete choices
.add("smile.random.forest.max.nodes", 100, 500, 50); // range [100, 500] 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));
});
// Random search — remember to limit the infinite stream:
hp.random().limit(50).forEach(prop -> { ... });
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a parameter with a fixed double value.Adds a parameter with a discrete set of double choices.Adds a double parameter with an auto-stepped range.Adds a double parameter with an explicit step range.Adds a parameter with a fixed integer value.Adds a parameter with a discrete set of integer choices.Adds an integer parameter with an auto-stepped range.Adds an integer parameter with an explicit step range.Adds a parameter with a fixed string value.Adds a parameter with a discrete set of string choices.clear()Removes all registered parameters.grid()Generates a finite stream of all hyperparameter configurations for grid (exhaustive) search.random()Generates an infinite stream of randomly sampled hyperparameter configurations.random(int n) Generates a finite stream ofnrandomly sampled hyperparameter configurations.Removes a previously registered parameter.intsize()Returns the number of registered parameters.
-
Constructor Details
-
Hyperparameters
public Hyperparameters()Constructor.
-
-
Method Details
-
add
Adds a parameter with a fixed integer value.- Parameters:
name- the parameter name.value- a fixed value of the parameter.- Returns:
- this object.
-
add
Adds a parameter with a fixed double value.- Parameters:
name- the parameter name.value- a fixed value of the parameter.- Returns:
- this object.
-
add
Adds a parameter with a fixed string value.- Parameters:
name- the parameter name.value- a fixed value of the parameter.- Returns:
- this object.
-
add
Adds a parameter with a discrete set of integer choices.- Parameters:
name- the parameter name.values- a non-empty array of candidate values.- Returns:
- this object.
- Throws:
IllegalArgumentException- ifvaluesis empty.
-
add
Adds a parameter with a discrete set of double choices.- Parameters:
name- the parameter name.values- a non-empty array of candidate values.- Returns:
- this object.
- Throws:
IllegalArgumentException- ifvaluesis empty.
-
add
Adds a parameter with a discrete set of string choices.- Parameters:
name- the parameter name.values- a non-empty array of candidate values.- Returns:
- this object.
- Throws:
IllegalArgumentException- ifvaluesis empty.
-
add
Adds an integer parameter with an auto-stepped range. The step defaults tomax(1, (end-start)/10).- Parameters:
name- the parameter name.start- the start of value range (inclusive).end- the end of value range (inclusive upper bound for steps).- Returns:
- this object.
-
add
Adds an integer parameter with an explicit step range.- Parameters:
name- the parameter name.start- the start of value range (inclusive).end- the end of value range (inclusive upper bound for steps).step- the step size (must be positive).- Returns:
- this object.
-
add
Adds a double parameter with an auto-stepped range. The step defaults to(end-start)/10.- Parameters:
name- the parameter name.start- the start of value range (inclusive).end- the end of value range (inclusive upper bound for steps).- Returns:
- this object.
-
add
Adds a double parameter with an explicit step range.- Parameters:
name- the parameter name.start- the start of value range (inclusive).end- the end of value range (inclusive upper bound for steps).step- the step size (must be positive).- Returns:
- this object.
-
remove
Removes a previously registered parameter. Has no effect if the parameter was not registered.- Parameters:
name- the parameter name.- Returns:
- this object.
-
clear
-
size
public int size()Returns the number of registered parameters.- Returns:
- the number of registered parameters.
-
random
Generates an infinite stream of randomly sampled hyperparameter configurations. The caller must applyStream.limit(long)before collecting, e.g.:hp.random().limit(100).forEach(this::evaluate);For
Hyperparameters.IntRangeandHyperparameters.DoubleRangeparameters the step field is ignored; values are sampled uniformly from[start, end).- Returns:
- an infinite stream of hyperparameter configurations.
- Throws:
IllegalStateException- if no parameters have been registered.
-
random
Generates a finite stream ofnrandomly sampled hyperparameter configurations. This is a convenience alternative torandom().limit(n).- Parameters:
n- the number of configurations to sample (must be positive).- Returns:
- a stream of
nrandomly sampled configurations. - Throws:
IllegalArgumentException- ifn <= 0.IllegalStateException- if no parameters have been registered.
-
grid
Generates a finite stream of all hyperparameter configurations for grid (exhaustive) search. The total number of configurations is the Cartesian product of all registered parameter value lists — be cautious of combinatorial explosion.Parameters are enumerated in insertion order.
- Returns:
- a finite stream of all grid-search configurations.
- Throws:
IllegalStateException- if no parameters have been registered.
-