[딥러닝 홀로서기] Lec22. Assignment #4 Review

YJ·2024년 11월 28일
0

딥러닝 홀로서기

목록 보기
22/24
post-thumbnail

이 블로그글은 2019년 조재영(Kevin Jo), 김승수(SeungSu Kim)님의 딥러닝 홀로서기 세미나를 수강하고 작성한 글임을 밝힙니다.

Assignment #4 Review

Goal

  • CNN를 이용하여 Cifar-10 데이터셋을 학습하고 다양한 파라미터와 방법들을 실험하여 모델 성능 높이기

문제점/해결법

  • Adam으로 학습했을 때보다 RMSprop으로 학습했을 때 더 좋은 성과를 보였다. (이떄 동일한 학습률을 사용함)

Adam은 기본적으로 RMSprop보다 낮은 학습률을 사용한다. Adam은 학습률 변화에 더 민감하기 때문에, RMSprop보다 상대적으로 더 낮은 학습률을 설정하는 경우가 많다.

모델 성능 향상을 위한 추가적인 접근법

모델 앙상블 (Ensemble)

  • 앙상블은 여러 모델의 예측을 결합하여 단일 모델보다 더 나은 결과를 도출하는 기법이다.

하드 보팅 (Hard Voting)

from collections import Counter

def hard_voting_ensemble(net_list, partition, args): 
    # 테스트 데이터 로더 생성
    testloader = torch.utils.data.DataLoader(partition['test'],
                                             batch_size=args.test_batch_size,
                                             shuffle=False, num_workers=2) 
    correct = 0
    total = 0

    with torch.no_grad():  
        for images, labels in testloader:
            images, labels = images.cuda(), labels.cuda()
            
            # 모델들의 예측값을 저장할 리스트 초기화
            predictions_list = []

            # 각 모델의 예측값 수집
            for net in net_list:
                net.eval()  # 모델을 평가 모드로 전환
                outputs = net(images)  # 모델의 출력 계산
                predictions = outputs.argmax(dim=1)  # 최종 클래스 예측
                predictions_list.append(predictions.cpu().numpy())  # CPU로 이동 후 리스트에 저장
            
            # 다수결(majority voting)로 최종 예측 계산
            final_predictions = torch.tensor([
                Counter(predictions).most_common(1)[0][0] for predictions in zip(*predictions_list)
            ]).cuda()

            # 정확도 계산
            correct += (final_predictions == labels).sum().item()
            total += labels.size(0)

    # 최종 앙상블 정확도 계산
    test_acc = 100 * correct / total
    print(f"Hard Voting Ensemble test accuracy: {test_acc:.2f}%")
    return test_acc
  • 하드 보팅은 각 모델의 최종 예측값(클래스 레이블)만 고려하여 다수결(majority voting)로 최종 결과를 도출하는 방식이다.
  • 소프트 보팅에 비해 간단하며, 확률 정보가 없는 경우에도 사용할 수 있다.

소프트 보팅 (Soft Voting)

def soft_voting_ensemble(net_list, partition, args): 
    testloader = torch.utils.data.DataLoader(partition['test'],
                                             batch_size=args.test_batch_size,
                                             shuffle=False, num_workers=2) 
    correct = 0
    total = 0

    with torch.no_grad():  # 그래디언트 계산 비활성화 (평가 모드)
        for images, labels in testloader:
            images, labels = images.cuda(), labels.cuda()
            
            # 앙상블 예측을 위한 확률 텐서 초기화
            ensemble_probs = torch.zeros(images.size(0), num_classes).cuda()

            # 모든 모델에서 softmax 확률 수집
            for net in net_list:
                net.eval()  # 모델을 평가 모드로 전환
                outputs = net(images)  # 모델의 출력 계산
                probabilities = torch.nn.functional.softmax(outputs, dim=1)  # softmax 확률 계산
                ensemble_probs += probabilities  # 확률 누적
            
            # 모델 간 확률 평균화
            ensemble_probs /= len(net_list)
            
            # 평균 확률 중 가장 높은 값을 가진 클래스 선택
            final_predictions = ensemble_probs.argmax(dim=1)

            # 정확도 계산 업데이트
            correct += (final_predictions == labels).sum().item()
            total += labels.size(0)

    # 최종 앙상블 정확도 계산
    test_acc = 100 * correct / total
    print(f"Ensemble test accuracy: {test_acc:.2f}%")
    return test_acc
  • 각 모델의 클래스 확률(softmax 출력)을 평균화하여 최종 클래스를 결정하는 방법이다.
  • 딥러닝에서 소프트 보팅이 자주 사용되는 이유는 확률 분포를 활용해 모델의 불확실성을 반영할 수 있기 때문이다.
  • 일반적으로 하드 보팅보다 성능이 뛰어나다.
특징소프트 보팅 (Soft Voting)하드 보팅 (Hard Voting)
기본 원리클래스 확률 평균화다수결(최빈값)
정교함확률 정보를 활용하므로 더 정교확률 정보를 사용하지 않음
성능일반적으로 더 나은 성능간단한 문제에서 적합
확률 정보 필요 여부필요필요 없음

학습률 스케줄러 (Learning Rate Scheduler)

  • 딥러닝 모델을 훈련할 때 학습률은 모델 성능에 매우 중요한 역할을 한다.
  • 학습률이 너무 크면 최적점을 지나칠 수 있고, 너무 작으면 학습이 느려지고 최적점을 찾기 어려울 수 있다.
  • 이러한 이유로, 학습률을 동적으로 조정하는 기법을 학습률 스케줄러(Learning Rate Scheduler)라고 한다.

StepLR (스텝 학습률 감소)

from torch.optim.lr_scheduler import StepLR

optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)

for epoch in range(100):
    train(model, optimizer)
    scheduler.step() 
  • 일정 간격마다 학습률을 고정된 비율로 감소시키는 스케줄러 훈련
  • 초기에는 빠르게 학습하고, 후반부에는 학습률을 낮추어 더 정밀하게 학습을 진행한다.
  • 주요 파라미터
    • step_size: 학습률을 감소시킬 주기.
    • gamma: 학습률 감소 비율 (0.1이면 학습률을 0.1배로 감소)

ExponentialLR (지수 학습률 감소)

from torch.optim.lr_scheduler import ExponentialLR

optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
scheduler = ExponentialLR(optimizer, gamma=0.95)

for epoch in range(100):
    train(model, optimizer)
    scheduler.step() 
  • 매 에폭마다 학습률을 지수적으로 감소시키는 방식
  • 학습률이 점차 빠르게 감소하여 후반부에서 정밀한 학습을 유도한다.
  • gamma: 매 에폭마다 학습률을 곱할 비율(0.95이면 매 에폭마다 학습률이 95%로 감소)

ReduceLROnPlateau (성능 향상 없음 시 학습률 감소)

from torch.optim.lr_scheduler import ReduceLROnPlateau

optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
scheduler = ReduceLROnPlateau(optimizer, 'min', patience=5, factor=0.1)

for epoch in range(100):
    train(model, optimizer)
    val_loss = validate(model)
    scheduler.step(val_loss)
  • 검증 손실 또는 정확도가 향상되지 않으면 학습률을 감소시키는 방식
  • 성능 향상이 없을 때만 학습률을 조정하기 때문에 매우 효율적이다.
  • 주요 파라미터
    • mode: 지표의 변화 방향을 지정하는 것
      • 'min' (예: 손실이 줄어들어야 함) 또는 'max' (예: 정확도가 늘어나야 함)
    • patience: 성능 향상이 없는 에폭 수
    • factor: 학습률 감소 비율 (0.1이면 성능 향상이 없을 때 학습률을 0.1배로 감소)
스케줄러 이름설명주요 파라미터장점단점
StepLR일정 에폭마다 학습률을 고정 비율로 감소시킴step_size (학습률 감소 주기), gamma (감소 비율)구현이 간단하고, 과적합을 방지하는 데 유용학습률 감소 시점을 정확히 조절하기 어려움
ExponentialLR매 에폭마다 학습률을 지수적으로 감소시킴gamma (학습률 감소 비율)점진적인 학습률 감소로 후반에 정밀 학습 가능학습률이 너무 빨리 감소하면 학습이 멈출 수 있음
ReduceLROnPlateau성능 향상이 없을 때 학습률을 감소시킴modepatiencefactor성능 향상이 없을 때만 학습률 감소너무 자주 감소할 수 있음, 성능 향상 판별 어려움
profile
제 글이 유익하셨다면 ♡와 팔로우로 응원 부탁드립니다.

0개의 댓글

관련 채용 정보