728x90
1. 필요성
- 모델 스스로 학습하지 못하는 부분을 사람이 직접 지정해줌 -> 학습률, 모델 크기, 옵티마이저 등
- 요즘은 필수적이진 않음. 마지막으로 조금이라도 더 모델 성능을 높이기 위해 쓰임. 하이퍼파라미터 튜닝보다는 애초에 좋은 데이터를 쓰는 게 훨씬 좋음
2. 방법
- grid : 점진적으로 수치를 조절하며 튜닝
- random : 말 그대로 랜덤한 값을 주며 튜닝
3. Ray
- multi node multi processing 지원
- ML/DL 병렬 처리 위해 개발됨 -> 현재 거의 ML/DL의 표준 모듈임
- 하이퍼파라미터 서치를 위한 다양한 모듈 제공
- 코드(아래)
def main(num_samples=10, max_num_epochs=10, gpus_per_trial=2):
data_dir = os.path.abspath("./data")
load_data(data_dir)
config = {
"l1": tune.sample_from(lambda _: 2 ** np.random.randint(2, 9)),
"l2": tune.sample_from(lambda _: 2 ** np.random.randint(2, 9)),
"lr": tune.loguniform(1e-4, 1e-1),
"batch_size": tune.choice([2, 4, 8, 16])
}
scheduler = ASHAScheduler(
metric="loss",
mode="min",
max_t=max_num_epochs,
grace_period=1,
reduction_factor=2)
reporter = CLIReporter(
# parameter_columns=["l1", "l2", "lr", "batch_size"],
metric_columns=["loss", "accuracy", "training_iteration"])
result = tune.run(
partial(train_cifar, data_dir=data_dir),
resources_per_trial={"cpu": 2, "gpu": gpus_per_trial},
config=config,
num_samples=num_samples,
scheduler=scheduler,
progress_reporter=reporter)
best_trial = result.get_best_trial("loss", "min", "last")
print("Best trial config: {}".format(best_trial.config))
print("Best trial final validation loss: {}".format(
best_trial.last_result["loss"]))
print("Best trial final validation accuracy: {}".format(
best_trial.last_result["accuracy"]))
best_trained_model = Net(best_trial.config["l1"], best_trial.config["l2"])
device = "cpu"
if torch.cuda.is_available():
device = "cuda:0"
if gpus_per_trial > 1:
best_trained_model = nn.DataParallel(best_trained_model)
best_trained_model.to(device)
best_checkpoint_dir = best_trial.checkpoint.value
model_state, optimizer_state = torch.load(os.path.join(
best_checkpoint_dir, "checkpoint"))
best_trained_model.load_state_dict(model_state)
test_acc = test_accuracy(best_trained_model, device)
print("Best trial test set accuracy: {}".format(test_acc))
if __name__ == "__main__":
# You can change the number of GPUs per trial here:
main(num_samples=10, max_num_epochs=10, gpus_per_trial=0)
* 위의 코드 출처 : 공식 사이트
Hyperparameter tuning with Ray Tune — PyTorch Tutorials 1.10.1+cu102 documentation
Note Click here to download the full example code Hyperparameter tuning with Ray Tune Hyperparameter tuning can make the difference between an average model and a highly accurate one. Often simple things like choosing a different learning rate or changing
pytorch.org
728x90
'AI > AITech 3기' 카테고리의 다른 글
[DataViz] 1-1강 데이터 시각화란 (0) | 2022.02.03 |
---|---|
[PyTorch] 10강 PyTorch Troubleshooting (2) | 2022.01.28 |
[PyTorch] 8강 Multi-GPU 학습 (1) | 2022.01.27 |
[PyTorch] 7강 Monitoring tools for PyTorch (2) | 2022.01.27 |
[PyTorch] 6강 모델 불러오기 (2) | 2022.01.27 |