꺼내먹는지식 준
Pytorchlightning TensorBoard 사용법 쉬운 정리 본문
이전글에서는 tensorboard 를 직접사용해보기 전 단계에서 글을 작성해서 남의 표현을 빌려서 전달했다.
이번에는 tensor board 를 사용해본 입장에서 정리를 해본다.
사실 근데 나는 Wandb (Weight and Bias)로 갈아탈 예정이다. 그래서 이 글을 읽는 사람들에게도 웬만하면 Wandb 사용을 추천한다.
Tensorboard 와 동일한 기능이지만 웹을 통해 접근 가능하고, 프로젝트 별로 저장 된다.
안쓸 이유가 없다.
from pytorch_lightning.loggers import TensorBoardLogger
logger = TensorBoardLogger(config.log_name)
log_name 은 사전에 설정해놓은 tensorboard를 저장할 때 사용할 폴더명이다.
trainer = pl.Trainer(
logger=logger,
max_epochs=config.epoch,
callbacks=callbacks,
auto_lr_find=True,
**config.trainer,
)
간단하게 trainer 안에 logger = logger 선언
끝이다.
엄청 간단하다.
torch lightning 의 강점이자 무서운 점은 너무 많은 것들이 내부적으로 동작한다는 것이다.
이로 인해 쉽게 사용하지만, 정확한 구현 능력이 떨어지기도 하고, 큰 문제는 세부적으로 수정을 하고 싶을 때 못하는 경우가 발생한다.
layer freeze 하려는 경우 torch lightning 으로 가능할까?
가능하다고 한다. 하지만 어떻게 사용해야 할지 명확하게 잘 안보이고, 이로 인해 내부 기능을 살펴봐야한다.
상단의 callbacks 함수 구현을 통해 다음과 같은 기능을 한번에 동작한다.
하단의 코드는 부스트 캠프 팀원 신규범 님이 작성하신 코드이다.
patience 3번까지는 early stop이 참고 멈춘다.
check point 와
loss checkpoint 생성
tqdm 으로 진행률 표시
fold 일 때, no fold 일 때 처리 구분
def callbacks():
early_stopping = EarlyStopping(monitor="val_f1_score", mode='max', patience=3)
lr_monitor = callbacks.LearningRateMonitor()
score_checkpoint = callbacks.ModelCheckpoint(
filename="best_score",
monitor="val_f1_score",
save_top_k=2,
mode="max",
save_last=False,
)
loss_checkpoint = callbacks.ModelCheckpoint(
filename="best_loss",
monitor="val_loss",
save_top_k=1,
mode="min",
save_last=False,
)
tqdm_progressbar = TQDMProgressBar(
refresh_rate=1,
)
if is_fold:
return [early_stopping, lr_monitor, score_checkpoint, loss_checkpoint]
return [lr_monitor, tqdm_progressbar]
'AI > PyTorch' 카테고리의 다른 글
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn 문제 해결 (0) | 2022.03.08 |
---|---|
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same 문제 해결 (0) | 2022.03.08 |
sklearn StratifiedKFold 용례 (0) | 2022.03.05 |
Tensorboard, Weight & Biases 간단 사용 (0) | 2022.01.26 |
Transfer Learning 듀토리얼 - (1) 모델 저장, checkpoint, 불러오기 (0) | 2022.01.26 |