【问题标题】:R H2O grid search: how to train top model on new data?R H2O 网格搜索:如何在新数据上训练顶级模型?
【发布时间】:2018-06-20 18:48:41
【问题描述】:

在运行超参数搜索并从网格中提取最佳模型后,是否可以使用模型对象在新数据集上进行训练?我现在看到的唯一方法是使用最佳模型的参数手动创建对训练函数(例如 h2o.gbm())的调用,但这非常麻烦。

【问题讨论】:

    标签: r h2o


    【解决方案1】:

    checkpoint 参数可以满足您的需求,它可以从原始模型进一步训练模型。

    此功能适用于h2o 包中的gbmrandom forestdeep learning

    下面的示例代码复制自:http://s3.amazonaws.com/h2o-release/h2o/master/3689/docs-website/h2o-docs/data-science/algo-params/checkpoint.html

    library(h2o)
    h2o.init()
    
    # import the cars dataset:
    # this dataset is used to classify whether or not a car is economical based on
    # the car's displacement, power, weight, and acceleration, and the year it was made
    cars <- h2o.importFile("https://s3.amazonaws.com/h2o-public-test-data/smalldata/junit/cars_20mpg.csv")
    
    # convert response column to a factor
    cars["economy_20mpg"] <- as.factor(cars["economy_20mpg"])
    
    # set the predictor names and the response column name
    predictors <- c("displacement","power","weight","acceleration","year")
    response <- "economy_20mpg"
    
    # split into train and validation sets
    cars.split <- h2o.splitFrame(data = cars,ratios = 0.8, seed = 1234)
    train <- cars.split[[1]]
    valid <- cars.split[[2]]
    
    # build a GBM with 1 tree (ntrees = 1) for the first model:
    cars_gbm <- h2o.gbm(x = predictors, y = response, training_frame = train,
                        validation_frame = valid, ntrees = 1, seed = 1234)
    
    # print the auc for the validation data
    print(h2o.auc(cars_gbm, valid = TRUE))
    
    # re-start the training process on a saved GBM model using the ‘checkpoint‘ argument:
    # the checkpoint argument requires the model id of the model on which you wish to continue building
    # get the model's id from "cars_gbm" model using `cars_gbm@model_id`
    # the first model has 1 tree, let's continue building the GBM with an additional 49 more trees, so set ntrees = 50
    
    # to see how many trees the original model built you can look at the `ntrees` attribute
    print(paste("Number of trees built for cars_gbm model:", cars_gbm@allparameters$ntrees))
    
    # build and train model with 49 additional trees for a total of 50 trees:
    cars_gbm_continued <- h2o.gbm(x = predictors, y = response, training_frame = train,
                        validation_frame = valid, checkpoint = cars_gbm@model_id, ntrees = 50, seed = 1234)
    
    # print the auc for the validation data
    print(h2o.auc(cars_gbm_continued, valid = TRUE))
    
    # you can also use checkpointing to pass in a new dataset (see options above for parameters you cannot change)
    # simply change out the training and validation frames with your new dataset
    

    编辑(基于下面@Edward 的评论:)

    h2o.grid会返回一系列模型,你可以得到最好的模型处理。所有参数都保存在模型手柄中,然后您可以将参数应用到新模型中。

    grid <- h2o.getGrid(h2o.grid@grid_id,sort_by = "auc",decreasing=TRUE)
    model.h2o <- h2o.getModel(grid@model_ids[[1]])
    

    model@allparameters 包含所有使用的参数,您可以使用这些参数来创建新模型和新数据。

    【讨论】:

    • 是的,我知道检查点,但希望有另一种方法来提取模型参数并在全新的数据集上进行训练。本质上,我只是想基于一些数据集建立一个模型结构,然后将其应用于不同的集合。因此,例如,在运行 GBM 网格搜索后,我可以获得一个对象“best_gbm_params”,然后稍后执行类似 h2o.gbm(x, y, train, valid, best_gbm_params, ...) 的操作。希望这是有道理的。
    • @Edward,根据您给定的场景更新答案。
    • 这就是我过去一直在做的事情,训练一个网格,然后从@allparameters 获取参数并手动创建对 h2o.gbm() 的调用。我想做的是类似 h2o.gbm(training_frame = new_train_data, best_model@allparameters),或者有一些其他的方法来自动从最佳模型中提取所有参数并生成对 h2o train 函数之一的新调用。我不喜欢手动方法,因为它很乏味,而且还因为将来可能会更改或添加某些参数,从而增加维护开销。
    • @Edward,我现在明白你的意思了。这将需要 h2o 向输出和模型添加一个函数以接受那些最佳估计参数。另一种方法是编写一个函数:基于h2o训练模型的args输出,然后构造字符表达式(通过匹配参数)。使用 do.call 运行此表达式。这样你就不用担心参数的变化,但仍然需要创建函数。
    • 好的,感谢您的帮助,考虑到 H2O 中没有对此类功能的原生支持,这听起来是一个合理的解决方案。
    猜你喜欢
    • 2018-12-22
    • 2020-08-11
    • 2016-06-10
    • 2018-06-16
    • 1970-01-01
    • 2018-02-17
    • 2020-05-21
    • 2020-10-22
    • 2018-06-21
    相关资源
    最近更新 更多