Tonz
Tonz

Reputation: 187

Additional parameters in def__init__ for Optuna Trial

class ConvolutionalNetwork(nn.Module):
    def __init__(self, in_features):
        super().__init__()
        self.in_features = in_features
        # this computes num features outputted from the two conv layers
        c1 = int(((self.in_features - 2)) / 64)  # this is to account for the loss due to conversion to int type
        c2 = int((c1-2)/64)
        self.n_conv = int(c2*16)
        #self.n_conv = int((( ( (self.in_features - 2)/4 ) - 2 )/4 ) * 16) 
        self.conv1 = nn.Conv1d(1, 16, 3, 1)
        self.conv1_bn = nn.BatchNorm1d(16)
        self.conv2 = nn.Conv1d(16, 16, 3, 1)
        self.conv2_bn = nn.BatchNorm1d(16)
        self.dp = nn.Dropout(trial.suggest_uniform('dropout_rate',0,1.0))
        self.fc3 = nn.Linear(self.n_conv, 2)

As you can see, def __init__ already has self and in_features as the variable. I am thinking of adding another variable trial (which is part of the Optuna package) to accommodate

    self.dp = nn.Dropout(trial.suggest_uniform('dropout_rate',0,1.0))

in the above codes. Please advise how, most only sources only has def __init__ (self, trial), which is pretty straightforward, but for my case I have 3 variables to be passed in the objective.

Upvotes: 0

Views: 443

Answers (1)

MSS
MSS

Reputation: 3623

You can do something like this:

class ConvolutionalNetwork(nn.Module):
    def __init__(self, trial, in_features):
        # code for initialization
        ..........

def objective(trial):
    # code to create in_features
    in_features = ......
    #generate the model
    model = ConvolutionalNetwork(trial, in_features)
    ..................
    # code to run the CNN and calculate the accuracy.
    return accuracy

# Create study and optimise the objective function.
study = optuna.create_study()
study.optimize(objective, n_trails=100, timeout=600)

Upvotes: 0

Related Questions