Defining the model architecture and loading dataset
First, we will define an Optuna-compliant model object. By Optuna-compliant, we mean adding APIs within the model definition code that are provided by Optuna to enable the parameterization of the model hyperparameters. To do this, we'll proceed as follows:
- First, we import the necessary libraries, as follows:
import torch import optuna
The
optuna
library will manage the hyperparameter search for us throughout the exercise. - Next, we define the model architecture. Because we want to be flexible with some of the hyperparameters—such as the number of layers and the number of units in each layer—we need to include some logic in the model definition code. So, first, we have declared that we need anywhere in between
1
to4
convolutional layers and1
to2
fully connected layers thereafter, as illustrated in the following code snippet:class ConvNet(nn.Module): def __init__(self, trial):...