我使用Toshihiko Yanase的代码对我的超参数优化器与Optuna进行交叉验证。下面是我使用的代码:
def objective(trial, train_loader, valid_loader):
# Remove the following line.
# train_loader, valid_loader = get_mnist()
...
return accuracy
def objective_cv(trial):
# Get the MNIST dataset.
dataset = datasets.MNIST(DIR, train=True, download=True, transform=transforms.ToTensor())
fold = KFold(n_splits=3, shuffle=True, random_state=0)
scores = []
for fold_idx, (train_idx, valid_idx) in enumerate(fold.split(range(len(dataset)))):
train_data = torch.utils.data.Subset(dataset, train_idx)
valid_data = torch.utils.data.Subset(dataset, valid_idx)
train_loader = torch.utils.data.DataLoader(
train_data,
batch_size=BATCHSIZE,
shuffle=True,
)
valid_loader = torch.utils.data.DataLoader(
valid_data,
batch_size=BATCHSIZE,
shuffle=True,
)
accuracy = objective(trial, train_loader, valid_loader)
scores.append(accuracy)
return np.mean(scores)
study = optuna.create_study(direction="maximize")
study.optimize(objective_cv, n_trials=20, timeout=600)
不幸的是,使用这种方式的代码,它不会记录每个折叠瓦尔损失到Optuna Jmeter 板。有没有办法记录每个折叠val损失到Optuna Jmeter 板?
1条答案
按热度按时间dojqjjoe1#
每个拆分验证损失都可以记录在当前试验的试验对象的
system_attrs
中。system_attrs
可以根据需要在相应试验下的控制面板中查看。具有所需功能的修改代码是:
PS: Unfortunatly, Optuna developers have indicated that they will remove the
system_attrs
in the future which I think will be a loss.