새소식

부스트캠프 AI Tech 4기

[PyTorch] 23. Hyperparameter Tuning

  • -

hyperparameter tuning

  • 모델이 스스로 학습하지 않는 요소들을 사람이 지정
    (learning rate, optimizer, batch size 등)
  • 성능에 영향을 끼치는 정도는 데이터 > 모델 > hyperparameter 순이다.
    hyperparameter tuning은 성능을 쥐어짤 때 사용해볼만하지만 시간대비 효율은 좋지 않다.
  • 과거엔 튜닝에 의해 값이 크게 좌우되기도 했었지만 요새는 점점 중요성이 떨어지는 추세
  • 가장 기본적인 방법: grid search(일정한 간격)vs random search → 요새는 잘 안쓴다.
    최근에는 베이지안 방법론(e.g. BOHB(Bayesian Optimization Hyper Band, 2018))이 사용되고 있다.

https://i.stack.imgur.com/JcaO2.png

 

Ray

  • multi-node multi preprocessing 지원 모듈이다.
  • ML/DL의 병렬처리를 위해 개발된  모듈이다.
  • Hyperparameter Search를 위한 다양한 모듈을 제공해주고 있다.

https://pytorch.org/tutorials/beginner/hyperparameter_tuning_tutorial.html

 

+ colab에서는 따로 설치가 필요하다.

!pip uninstall -y -q pyarrow
!pip install -q -U ray[tune]
!pip install -q ray[debug]

 

from ray.tune.suggest.bayesopt import BayesOptSearch
from ray.tune.suggest.hyperopt import HyperOptSearch

def main(num_samples=10, max_num_epochs=10, gpus_per_trial=2):
    
    data_dir = os.path.abspath("./data")
    load_data(data_dir)
    
    #################################################################################
    #### 1.search space 설정####
    config = {
        "lr": tune.loguniform(1e-4, 1e-1),
        "batch_size": tune.choice([2, 4, 8, 16])
    }
    
    #### 2.scheduler 지정 ####
    scheduler = ASHAScheduler(  # ASHA는 가능성 없는 후보를 제거하는 방식
        metric="loss",
        mode="min",
        max_t=max_num_epochs,
        grace_period=1,
        reduction_factor=2)
        
    #### 3.결과 출력 양식 지정 ####
    reporter = CLIReporter(
        metric_columns=["loss", "accuracy", "training_iteration"])
    
    #### 4. RunRay ####
    result = tune.run(
        partial(train_cifar, data_dir=data_dir), # train_cifar : 학습을 할 때 실행할 부분을 함수 형태로 만들어서 넣어주어야 함
        resources_per_trial={"cpu": 2, "gpu": gpus_per_trial},
        config=config,
        num_samples=num_samples,
        scheduler=scheduler,
        progress_reporter=reporter)
    #################################################################################

 

▮ WandB와 Ray Tune Documentation

https://docs.wandb.ai/v/ko/integrations/ray-tune


부스트캠프 AI Tech 교육 자료를 참고하였습니다.

728x90

'부스트캠프 AI Tech 4기' 카테고리의 다른 글

[WEEK02] 회고  (1) 2022.09.29
[PyTorch] 24. Troubleshooting  (0) 2022.09.29
[PyTorch] 22. Multi-GPU  (0) 2022.09.29
[PyTorch] 21. Monitoring tools  (0) 2022.09.28
[PyTorch] 20. 모델 저장 및 불러오기  (0) 2022.09.28
Contents