wandb sweep 사용법

MostlyFor·2023년 4월 23일

참고 자료
https://docs.wandb.ai/guides/sweeps

Sweep을 쓰는 이유

  1. hyperparam search의 자동화

  2. the space of possible model 탐색

Sweep의 구성

1. a controller

controller는 hyperparameter 조합을 생성합니다.
(the controller lives on the Weights & Biases server)

2. agents

controller가 생성한 hyperparameter 조합을 이용하여 model을 학습시킨 뒤 결과를 Sweep server에 기록합니다.
( Agents live on your machine(s).)

  • agents는 병렬화가 가능하다고 합니다.

wandb sweep 실행법

1. add a couple of code to log

이 부분은 기존 code에 wandb를 추가하라는 내용인데 구체적으로는 다음 내용들을 추가해야합니다.

  1. import wandb
  2. sweep configuration을 담고 있는 객체 생성
  • a. dictionary structure
  • b. YAML file 이용

여기 가면 좀 더 자세한 sweep config를 확인할 수 있습니다!
https://docs.wandb.ai/guides/sweeps/define-sweep-configuration

  1. wandb sweep에 sweep configration 정보 넘기기
  • 이때 wandb.sweep()이 id를 반환합니다.
sweep_id = wandb.sweep(
  sweep=sweep_configuration, 
  project='my-first-sweep'
  )
  1. wandb.init()으로 api 실행 -> wandb에 기록 시작
    + wandb.config를 설정된 값을 가져올 수 있습니다. ex) lr = wandb.config.lr

  2. 기록할 정보 설정 wandb.log()

      wandb.log({
        'epoch': epoch, 
        'train_acc': train_acc,
        'train_loss': train_loss, 
        'val_acc': val_acc, 
        'val_loss': val_loss
      })
  1. wandb.agent()을 이용하여 에이전트 이용하기

2. define the sweep configuration

여기서 우리가 탐색하고자 하는 the space of possible model과 탐색 전략(random, grid, bayes)과 metric을 설정할 수 있습니다.

# 2: Define the search space
sweep_configuration = {
    'method': 'random',
    'metric': {'goal': 'minimize', 'name': 'score'},
    'parameters': 
    {
        'x': {'max': 0.1, 'min': 0.01},
        'y': {'values': [1, 3, 7]},
     }
}

wandb가 제공하는 hyperparam search 전략이 3가지가 있는데 그 중 하나를 선택할 수 있습니다.

3. initialize sweep

Sweep Server 시작 (글에서 Sweep Server를 central controller로 표현했습니다.)

sweep_id = wandb.sweep(
    sweep=sweep_configuration, 
    project='my-first-sweep'
    )

4. start sweep

the agent가 the central sweep server에서 이번에 실험할 하이퍼파라미터를 가져오고 실험합니다.

이때 function에는 training func이 들어갑니다!
여기서 실행할 횟수도 정할 수 있습니다.

wandb.agent(sweep_id, function=main, count=10)

5. visualize results

이건 wandb에 접속해서 확인할 수 있습니다.

주의사항

Both the W&B Sweep and the W&B Run must be in the same project. Therefore, the name you provide when you initialize Weights & Biases (wandb.init) must match the name of the project you provide when you initialize a W&B Sweep (wandb.sweep).
-> init에서의 프로젝트 이름과 sweep_Id에서의 프로젝트 이름은 항상 같아야 한다!

0개의 댓글