Python Optuna试验的def__init__;中的附加参数
如您所见,Python Optuna试验的def__init__;中的附加参数,python,pytorch,cnn,optuna,Python,Pytorch,Cnn,Optuna,如您所见,def\uu init\uu已经将self和in\u功能作为变量。我正在考虑添加另一个变量trial(它是Optuna包的一部分),以适应 class ConvolutionalNetwork(nn.Module): def __init__(self, in_features): super().__init__() self.in_features = in_features # this computes num featu
def\uu init\uu
已经将self
和in\u功能
作为变量。我正在考虑添加另一个变量trial
(它是Optuna包的一部分),以适应
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)
在上述代码中。请给出建议,大多数纯源只有
def\uuu init\uuu(self,trial)
,这非常简单,但对于我的情况,我有3个变量要在目标中传递。您可以这样做:
self.dp = nn.Dropout(trial.suggest_uniform('dropout_rate',0,1.0))
嘿,我很想帮忙,但我一点也不懂这个问题。也许您可以重新表述它?向
\uuuuu init\uuuuu
添加一个关键字参数应该很简单,但我猜您是在试图询问如何使其成为可选的,或者如何在\uuuuu init\uuu
函数中实际使用它。请澄清。在这种情况下,这个目标中有3个变量(self、in_features、trial),如果我将trial添加到init并运行代码,它会说trial未定义。trial.suggest是Optuna包中命令的一部分,我在前面用“import Optuna”导入了该包。其目的是调整参数,运行模型以找出具有最高精度的最佳超参数。
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)