- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
def autotune(trial):
cfg= { 'device' : "cuda" if torch.cuda.is_available() else "cpu",
# 'train_batch_size' : 64,
# 'test_batch_size' : 1000,
# 'n_epochs' : 1,
# 'seed' : 0,
# 'log_interval' : 100,
# 'save_model' : False,
# 'dropout_rate' : trial.suggest_uniform('dropout_rate',0,1.0),
'lr' : trial.suggest_loguniform('lr', 1e-3, 1e-2),
'momentum' : trial.suggest_uniform('momentum', 0.4, 0.99),
'optimizer': trial.suggest_categorical('optimizer',[torch.optim.Adam,torch.optim.SGD, torch.optim.RMSprop, torch.optim.$
'activation': F.tanh}
optimizer = cfg['optimizer'](model.parameters(), lr=cfg['lr'])
#optimizer = torch.optim.Adam(model.parameters(),lr=0.001
正如您在上面看到的,我正在尝试运行 Optuna 试验来为我的 CNN 模型搜索最佳超参数。
# Train the model
# use small epoch for large dataset
# An epoch is 1 run through all the training data
# losses = [] # use this array for plotting losses
for _ in range(epochs):
# using data_loader
for i, (data, labels) in enumerate(trainloader):
# Forward and get a prediction
# x is the training data which is X_train
if name.lower() == "rnn":
model.hidden = (torch.zeros(1,1,model.hidden_sz),
torch.zeros(1,1,model.hidden_sz))
y_pred = model.forward(data)
# compute loss/error by comparing predicted out vs acutal labels
loss = criterion(y_pred, labels)
#losses.append(loss)
if i%10==0: # print out loss at every 10 epoch
print(f'epoch {i} and loss is: {loss}')
#Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
study = optuna.create_study(sampler=optuna.samplers.TPESampler(), direction='minimize',pruner=optuna.pruners.SuccessiveHalvingPrune$
study.optimize(autotune, n_trials=1)
但是,当我运行上面的代码来调整并找出我的最佳参数时,出现了跟随错误,尽管我仍然得到了 epoch 损失和值,但似乎试验失败了。请指教谢谢!
[W 2020-11-11 13:59:48,000] Trial 0 failed, because the returned value from the objective function cannot be cast to float. Returned value is: None
Traceback (most recent call last):
File "autotune2", line 481, in <module>
n_instances, n_features, scores = run_analysis()
File "autotune2", line 350, in run_analysis
print(study.best_params)
File "/home/shar/anaconda3/lib/python3.7/site-packages/optuna/study.py", line 67, in best_params
return self.best_trial.params
File "/home/shar/anaconda3/lib/python3.7/site-packages/optuna/study.py", line 92, in best_trial
return copy.deepcopy(self._storage.get_best_trial(self._study_id))
File "/home/shar/anaconda3/lib/python3.7/site-packages/optuna/storages/_in_memory.py", line 287, in get_best_trial
raise ValueError("No trials are completed yet.")
ValueError: No trials are completed yet.
最佳答案
引发此异常是因为您研究中的目标函数必须返回浮点数。
在您的情况下,问题出在这一行:
study.optimize(autotune, n_trials=1)
您之前定义的自动调谐函数不返回值且不能用于优化。
def autotune():
cfg= { 'device' : "cuda" if torch.cuda.is_available() else "cpu"
...etc...
}
best_loss = 1e100; # or larger
# Train the model
for _ in range(epochs):
for i, (data, labels) in enumerate(trainloader):
... (train the model) ...
# compute loss/error by comparing predicted out vs actual labels
loss = criterion(y_pred, labels)
best_loss = min(loss,best_loss)
return best_loss
Optuna 存储库中有一个很好的
example with Pythorch,它使用 pythoch 回调来检索准确性(但如果需要,可以轻松更改以使用 RMSE)。它还使用了多个实验并取超参数的中位数。
关于pytorch - Optuna Pytorch : returned value from the objective function cannot be cast to float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64781266/
我正在尝试将 LightGBM 用于回归问题(平均绝对误差/L1 - 或类似 Huber 或 pseud-Huber - 损失),我主要想调整我的超参数。 LightGBMTunerCV在 optun
这个问题的背景是通过运行 study.optimize() 来优化神经网络训练的超参数,启用默认剪枝并将学习率作为优化参数(这个问题可以推广到其他超参数). 高学习率最初可以产生良好的结果,可以在开始
使用 Optuna 执行单目标优化时,研究的最佳参数可使用: import optuna def objective(trial): x = trial.suggest_uniform('x'
使用 Optuna 执行单目标优化时,研究的最佳参数可使用: import optuna def objective(trial): x = trial.suggest_uniform('x'
我在寻找最佳 auc 的 lightgbm 建模中遇到错误。任何帮助将不胜感激。 import optuna from sklearn.model_selection import Stratifi
如何在 Optuna 的 objective 函数中同时优化多个指标。例如,我正在训练一个 LGBM 分类器,并希望为所有常见分类指标(如 F1、精度、召回率、准确率、AUC 等)找到最佳超参数集。
我知道这个问题已经被问过多次,但是有了其他线程的答案,我无法解决我的问题。我正在使用 python3 CTGAN_noscale.py --database_name CTGAN_noshift 在命
由于某种原因,Optuna TPESampler 和 RandomSampler 对任何参数尝试相同的建议整数值(也可能是浮点数和对数)不止一次。我找不到一种方法来阻止它再次建议相同的值。在 100
我正在尝试使用 optuna 搜索超参数空间。 在一个特定场景中,我在一台带有几个 GPU 的机器上训练一个模型。模型和批量大小允许我每 1 个 GPU 运行 1 次训练。所以,理想情况下,我想让 o
我正在使用 optuna 来优化我的自定义模型的参数。 有没有办法对参数进行采样,直到之前未测试当前参数集?我的意思是,如果过去使用相同的参数集进行过一些尝试,请尝试对另一个参数进行采样。 在某些情况
我想在 Optuna 中建议比率。 比率为 X_1, X_2, ..., X_k , 以 ∑X_i = 1 为界和 0 <= X_i <= 1对于所有 i . Optuna 不提供 Dirichlet
我正在使用 Optuna 优化一些目标函数。我想创建“包装”标准 Optuna 代码的自定义类。 例如,这是我的类(class)(它仍在进行中!): class Optimizer(object):
我想获得稍后在笔记本中使用的最佳模型,以使用不同的测试批次进行预测。 可重现的示例(取自 Optuna Github): import lightgbm as lgb import numpy as
我将 sklearn 与 optuna 一起用于 HPO。我想创建一个自定义函数,它将 sklearn 管道作为输入并返回特定于 optuna 的网格。返回 sklearn 特定的参数网格(即字典)似
我在使用 Optuna 调整我的模型时遇到警告。请告诉我如何抑制这些警告? [LightGBM] [Warning] feature_fraction is set=0.2, colsample_by
我想对官方 Optuna 和基于 pytorch 的示例代码 ( https://github.com/optuna/optuna/blob/master/examples/pytorch_simpl
我尝试使用 optuna 来调整超参数。但我的目标函数是有条件的,这会在获得最佳参数方面产生问题。 只有满足条件时我才想获得 cwc,否则继续尝试下一个超参数。但我猜由于条件不满足并且目标函数返回 c
我正在尝试在 Python 中使用 optuna 库来优化推荐系统模型的参数。这些模型是自定义的,看起来像标准的拟合预测 sklearn 模型(使用方法获取/设置参数)。 我所做的:从均匀 int 分
我想将 [OPTUNA][1] 与 sklearn [MLPRegressor][1] 模型一起使用。 对于几乎所有超参数,如何为它们设置 OPTUNA 都非常简单。例如,要设置学习率:learnin
def autotune(trial): cfg= { 'device' : "cuda" if torch.cuda.is_available() else "cpu",
我是一名优秀的程序员,十分优秀!