H2O로 그리드/랜덤 서치

R에서 하이퍼파라미터 튜닝

Dr. Shirin Elsinghorst

Senior Data Scientist

H2O 모델의 하이퍼파라미터

  • Gradient Boosting의 하이퍼파라미터:
?h2o.gbm
  • ntrees: 트리 개수. 기본값 50.

  • max_depth: 최대 트리 깊이. 기본값 5.

  • min_rows: 리프의 최소 (가중) 관측치 수. 기본값 10.

  • learn_rate: 학습률(0.0~1.0). 기본값 0.1.

  • learn_rate_annealing: 각 트리 후 학습률에 곱할 계수(예: 0.99, 0.999). 기본값 1.
R에서 하이퍼파라미터 튜닝

H2O로 모델링 준비

  • H2O 프레임으로 변환
seeds_data_hf <- as.h2o(seeds_data)
  • 피처와 타깃 지정
y <- "seed_type"
x <- setdiff(colnames(seeds_data_hf), y)
  • 데이터를 학습/검증/테스트로 분할
sframe <- h2o.splitFrame(data = seeds_data_hf, ratios = c(0.7, 0.15), seed = 42)
train <- sframe[[1]]
valid <- sframe[[2]]
test <- sframe[[3]]
R에서 하이퍼파라미터 튜닝

하이퍼파라미터 그리드 정의

  • GBM 하이퍼파라미터
gbm_params <- list(ntrees = c(100, 150, 200), max_depth = c(3, 5, 7), learn_rate = c(0.001, 0.01, 0.1))
  • h2o.grid 함수
gbm_grid <- h2o.grid("gbm", 
                     grid_id = "gbm_grid",
                     x = x, 
                     y = y,
                     training_frame = train,
                     validation_frame = valid,
                     seed = 42,
                     hyper_params = gbm_params)
  • h2o.getGrid로 결과 확인
R에서 하이퍼파라미터 튜닝

그리드 객체 살펴보기

  • h2o.getGrid로 모델 gbm_grid결과 확인

  • 검증 정확도로 정렬된 그리드 결과 얻기

gbm_gridperf <- h2o.getGrid(grid_id = "gbm_grid", sort_by = "accuracy", decreasing = TRUE)
Grid ID: gbm_grid 
Used hyper parameters: 
  -  learn_rate 
  -  max_depth 
  -  ntrees 
Number of models: 27 
Number of failed models: 0 

Hyper-Parameter Search Summary: ordered by decreasing accuracy
R에서 하이퍼파라미터 튜닝

그리드에서 최적 모델 추출

  • 검증 정확도로 선택된 상위 GBM 모델의 id 위치는 1입니다
best_gbm <- h2o.getModel(gbm_gridperf@model_ids[[1]])
  • 최적 모델의 하이퍼파라미터:
print(best_gbm@model[["model_summary"]])
Model Summary: 
 number_of_trees number_of_internal_trees model_size_in_bytes min_depth
             200                      600              100961         2 
 max_depth mean_depth min_leaves max_leaves mean_leaves
         7    5.22667          3         10     8.38833
R에서 하이퍼파라미터 튜닝

그리드에서 최적 모델 추출

  • best_gbm일반 H2O 모델 객체이며 그대로 사용할 수 있습니다!
h2o.performance(best_gbm, test)
MSE: (Extract with `h2o.mse`) 0.04761904
RMSE: (Extract with `h2o.rmse`) 0.2182179
Logloss: (Extract with `h2o.loglos
R에서 하이퍼파라미터 튜닝

H2O로 랜덤 서치

  • 하이퍼파라미터 그리드에 더해 검색 기준을 추가합니다:
gbm_params <- list(ntrees = c(100, 150, 200),
                   max_depth = c(3, 5, 7),
                   learn_rate = c(0.001, 0.01, 0.1))

search_criteria <- list(strategy = "RandomDiscrete", max_runtime_secs = 60, seed = 42)
gbm_grid <- h2o.grid("gbm", grid_id = "gbm_grid", x = x, y = y, training_frame = train, validation_frame = valid, seed = 42, hyper_params = gbm_params, search_criteria = search_criteria)
R에서 하이퍼파라미터 튜닝
search_criteria <- list(strategy = "RandomDiscrete", 
                        stopping_metric = "mean_per_class_error", 
                        stopping_tolerance = 0.0001, 
                        stopping_rounds = 6)

gbm_grid <- h2o.grid("gbm", x = x, y = y, training_frame = train, validation_frame = valid, seed = 42, hyper_params = gbm_params, search_criteria = search_criteria)
H2O Grid Details
================
Grid ID: gbm_grid 
Used hyper parameters: 
  -  learn_rate 
  -  max_depth 
  -  ntrees 
Number of models: 30 
Number of failed models: 0
R에서 하이퍼파라미터 튜닝

연습해 봅시다!

R에서 하이퍼파라미터 튜닝

Preparing Video For Download...