Skip to content
import warnings

from rich import print
from rich.pretty import pprint
from sklearn.model_selection import train_test_split

from pytorch_tabular.utils import make_mixed_dataset
data, cat_col_names, num_col_names = make_mixed_dataset(
    task="classification", n_samples=10000, n_features=20, n_categories=4
)

导入库

from pytorch_tabular import TabularModel
from pytorch_tabular.models import (
    CategoryEmbeddingModelConfig,
)
from pytorch_tabular.config import DataConfig, OptimizerConfig, TrainerConfig, ExperimentConfig
from pytorch_tabular.models.common.heads import LinearHeadConfig

超参数调优

train, test = train_test_split(data, random_state=42)
train, val = train_test_split(train, random_state=42)
data_config = DataConfig(
    target=[
        "target"
    ],  # 目标应始终为一个列表。仅在回归任务中支持多目标设置。多任务分类功能尚未实现。
    continuous_cols=num_col_names,
    categorical_cols=cat_col_names,
)
trainer_config = TrainerConfig(
    batch_size=1024,
    max_epochs=100,
    early_stopping="valid_loss",  # 监控有效损失以提前停止训练
    early_stopping_mode="min",  # 将模式设置为“min”,因为在验证损失中,数值越低越好。
    early_stopping_patience=5,  # 在终止之前,降级训练的轮数等待时间
    checkpoints="valid_loss",  # 保存最佳检查点监控验证损失
    load_best=True,  # 训练后,加载最佳检查点
    progress_bar="none",  # 关闭进度条
    trainer_kwargs=dict(enable_model_summary=False),  # 关闭模型摘要
)
optimizer_config = OptimizerConfig()

head_config = LinearHeadConfig(
    layers="", dropout=0.1, initialization="kaiming"  # 头部没有额外的层,仅包含一个映射层,输出维度为output_dim。
).__dict__  # 转换为字典以传递给模型配置(OmegaConf不接受对象)

model_config = CategoryEmbeddingModelConfig(
    task="classification",
    layers="1024-512-512",  # 每层节点数
    activation="LeakyReLU",  # 每层之间的激活
    learning_rate=1e-3,
    head="LinearHead",  # 线性磁头
    head_config=head_config,  # 线性磁头配置
)

网格搜索

注意:为了演示,我们使用测试集进行调优,但在实际问题中,请使用单独的验证集进行调优。否则,您将会过拟合于测试集,并且获得虚高的性能估计。

定义超参数空间

超参数空间定义为字典。键是超参数名称,值是要尝试的值列表。超参数名称遵循以下约定: - model_config__<hyperparameter_name> 用于模型超参数 - model_config.head_config__<hyperparameter_name> 用于头部超参数 - optimizer_config__<hyperparameter_name> 用于优化器超参数 - 我们无法使用数据模块的超参数进行调优,因为数据模块已经拟合,并且我们不能更改其超参数。

search_space = {
    "model_config__layers": ["1024-512-512", "1024-512-256", "1024-512-128"],
    "model_config.head_config__dropout": [0.1, 0.2, 0.3],
    "optimizer_config__optimizer": ["RAdam", "AdamW"],
}
# 在搜索过程中,任何不属于搜索空间的参数都将保持不变。
from pytorch_tabular.tabular_model_tuner import TabularModelTuner
tuner = TabularModelTuner(
    data_config=data_config,
    model_config=model_config,
    optimizer_config=optimizer_config,
    trainer_config=trainer_config
)
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    result = tuner.tune(
        train=train,
        validation=test,
        search_space=search_space,
        strategy="grid_search",
        # cv=5,取消注释此项以进行五折交叉验证
        metric="accuracy",
        mode="max",
        progress_bar=True,
        verbose=False # 如果你想每迭代一次就记录一次指标和参数,请设置为 True。
    )
Output()


LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]



结果是一个命名元组,包含 trials_df、best_params、best_score 和 best_model。

  • trials_df:一个包含所有超参数组合及其对应分数的数据框
  • best_params:最佳超参数组合
  • best_score:最佳分数
  • best_model:如果 return_best_model 为 True,则返回 best_model, 否则返回 None
result.trials_df.head()
trial_id model_config.head_config__dropout model_config__layers optimizer_config__optimizer loss accuracy
0 0 0.1 1024-512-512 RAdam 0.219196 0.9152
1 1 0.1 1024-512-512 AdamW 0.197452 0.9288
2 2 0.1 1024-512-256 RAdam 0.210085 0.9180
3 3 0.1 1024-512-256 AdamW 0.202238 0.9272
4 4 0.1 1024-512-128 RAdam 0.220526 0.9132
print("Best Score: ", result.best_score)
pprint(result.best_params)
Best Score:  0.9308000206947327
{
'model_config.head_config__dropout': 0.3,
'model_config__layers': '1024-512-512',
'optimizer_config__optimizer': 'AdamW',
'loss': 0.19081245362758636
}

随机搜索

注意:为了演示,我们使用测试集进行调优,但在实际问题中,请使用单独的验证集进行调优。否则,您将对测试集过拟合,并且会得到虚高的性能估计。

定义超参数空间

超参数空间被定义为一个字典。键是超参数的名称,值是类别的值列表和连续的分布。超参数名称遵循以下约定: - model_config__<hyperparameter_name> 表示模型超参数 - model_config.head_config__<hyperparameter_name> 表示头部超参数 - optimizer_config__<hyperparameter_name> 表示优化器超参数 - 我们不能使用数据模块的超参数进行调优,因为数据模块已经拟合,我们无法更改它的超参数。

from scipy.stats import uniform, randint, loguniform
search_space = {
    "model_config__layers": ["1024-512-512", "1024-512-256", "1024-512-128"],
    "model_config.head_config__dropout": uniform(0, 0.5),
    "optimizer_config__optimizer": ["RAdam", "AdamW"],
}
# 任何不属于搜索空间的参数,在搜索过程中将保持不变。
from pytorch_tabular.tabular_model_tuner import TabularModelTuner
tuner = TabularModelTuner(
    data_config=data_config,
    model_config=model_config,
    optimizer_config=optimizer_config,
    trainer_config=trainer_config
)
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    result = tuner.tune(
        train=train,
        validation=test, # 无需验证的是我们使用CV。
        search_space=search_space,
        n_trials=10,
        strategy="random_search",
        # cv=5,取消注释此项以进行5折交叉验证
        metric="accuracy",
        mode="max",
        progress_bar=True,
        verbose=False # 如果你希望在每次迭代时记录指标和参数,请设置为True。
    )
Output()


LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]

LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]



结果是一个命名元组,包含 trials_df、best_params、best_score 和 best_model。

  • trials_df: 一个包含所有超参数组合及其对应分数的数据框
  • best_params: 最佳超参数组合
  • best_score: 最佳分数
  • best_model: 如果 return_best_model 为 True,返回 best_model,否则返回 None
result.trials_df.head()
trial_id model_config.head_config__dropout model_config__layers optimizer_config__optimizer loss accuracy
0 0 0.187270 1024-512-512 RAdam 0.218989 0.9184
1 1 0.365997 1024-512-512 RAdam 0.215875 0.9216
2 2 0.078009 1024-512-128 RAdam 0.226962 0.9104
3 3 0.029042 1024-512-512 AdamW 0.206726 0.9224
4 4 0.071433 1024-512-128 AdamW 0.211062 0.9200
print("Best Score: ", result.best_score)
pprint(result.best_params)
Best Score:  0.9247999787330627
{
'model_config.head_config__dropout': 0.028205789513550128,
'model_config__layers': '1024-512-256',
'optimizer_config__optimizer': 'AdamW',
'loss': 0.18974390625953674
}