1. Hyperparameter tuning with caret
Caret makes hyperparameter tuning very easy. By default, it performs automatic tuning for you with every training run. But you can also manually define how you want to tune your models.
2. Automatic hyperparameter tuning in caret
Here you see the random forest model with the `rf` method from before. In the output we see that we only have one hyperparameter to tune:
`mtry` defines the number of variables that are randomly sampled as candidates at each split. caret automatically tried three different `mtry` values and includes the performance of each with the output. The best model is chosen with the metric `accuracy`, which in this case was for mtry = 6.
3. Hyperparameters are specific to model algorithms
Different algorithms have different hyperparameters. You might be wondering how you would know which hyperparameters you can tune with these different `method` in caret.
If you know the model abbreviation, you can use the modellookup function.
But the easiest way is to use the online documentation for caret. Click this link on the slides to go to the page.
There, you will find an overview of the different algorithms you can set as `method` in the train function. This table includes the name of the model, the string you need to put into the train function, whether it can be used for classification or regression and what the original R package of the implementation is.
But most importantly, you will find which hyperparameters can be tuned.
Here, I will not discuss the mathematics behind hyperparameters, instead, I will focus on HOW to perform the hyperparameter tuning.
4. Hyperparameters in Support Vector Machines (SVM)
Let's change things up a bit and build a Support Vector Machine with Polynomial Kernel similar to the Random Forest model from before: this time I am using the `svmPoly` model.
And I am again calculating the training time.
5. Hyperparameters in Support Vector Machines (SVM)
When we examine the model object again,
we see that this time `caret` performed a more complex hyperparameter tuning. If we have more than one hyperparameter to tune, `train` automatically creates a grid of tuning parameters.
By default, caret tries all possible combinations of three hyperparameters, in our model:
- degree being 1, 2, or 3
- scale being 0.001, 0.01 or 0.1
- and c being 0.25, 0.5 or 1
Because the output shows the performance for every possible combination of hyperparameters, the output is too long to fit on this slide and I am only showing the best model with degree of 1, scale of 0.1 and c equal to 1.
6. Defining hyperparameters for automatic tuning
We can also set the option `tuneLength` to specify the number of different values to try for each hyperparameter,
for example 5.
Now, caret tries all possible combinations of five hyperparameters:
- degree being 1, 2, 3, 4 or 5
- scale being 1e-03, 1e-02, 1e-01, 1e+00 and 1e+01
- and c being 0.25, 0.5, 1, 2 or 4
The best model now has degree, scale and c of 1.
7. Manual hyperparameter tuning in caret
Of course, you could also manually try out different hyperparameters.
This, we can do with the option `tuneGrid()`, to which we can feed a grid of hyperparameters. This grid is defined with the `expand.grid()` function.
If we use that function, we need to define all hyperparameters. Let's see what happens if we set the degree to 4 and keep scale and c at 1
and retrain the model.
8. Manual hyperparameter tuning in caret
This time, we only trained with one combination of hyperparameters, so our output gives the performance for these hyperparameters only!
9. It's your turn!
Now it's your turn to apply simple hyperparameter tuning in caret.