본문 바로가기
공부기록/[PyTorch]

[PyTorch] - Deep Learning Hyperparameter Tuning in PyTorch | Making the Best Possible ML Model (240414, 공부기록 3일차)

by RiverWon 2024. 4. 14.

사실 어제가 3일차 였어야 하지만 공부를 너무 대충 하고 올렸기에.. 3일차는 이것으로 하는것으로하도록하겠습니다

 

그 지난번 CNN on Fashion MNIST 에 있는 코드로 진행 예정!

2024.04.12 - [공부기록/[PyTorch]] - [PyTorch] - CNN on Fashion MNIST(240411 , 공부기록 1일차)

 

[PyTorch] - CNN on Fashion MNIST(240411 , 공부기록 1일차)

Colab https://colab.research.google.com/drive/1PoE9q4uWwayPGQLB7aClKecpuYRsudn2?usp=sharing PyTorch Tutorial1 : Training a CNN Classifier on Fashion MNIST.ipynb Colab notebook colab.research.google.com 어제 학습했던 LSTM stock forecasting 내용이

riverfromscratch.tistory.com

 

reference : https://www.youtube.com/watch?v=xP9l9MptIZo&list=PLKYEe2WisBTHmsTdhjaQydI58tI7pFCDT&index=2


Model Overview

import torch.nn as nn
import torch.nn.functional as F

class NeuralNet(nn.Module):
    def __init__(self):
        super().__init__()

        self.conv1 = nn.Conv2d(in_channels=1, out_channels=256, kernel_size=3)
        self.batch_norm1 = nn.BatchNorm2d(256)
        self.pool1 = nn.MaxPool2d(2, 2)
        
        self.conv2 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3)
        self.batch_norm2 = nn.BatchNorm2d(512)
        self.pool2 = nn.MaxPool2d(2, 2)

        self.avg_pool = nn.AvgPool2d(5, 5)
        self.flatten = nn.Flatten()
        
        self.fc1 = nn.Linear(in_features=512, out_features=512)
        self.drop1 = nn.Dropout(p=0.1)
        
        self.out = nn.Linear(in_features=512, out_features=10)

    
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.batch_norm1(x)
        x = self.pool1(x)
        
        x = F.relu(self.conv2(x))
        x = self.batch_norm2(x)
        x = self.pool2(x)
        
        #print(f'Shape before pool: {x.shape}')
        x = self.avg_pool(x)
        #print(f'Shape after pool: {x.shape}')
        
        x = self.flatten(x)
        
        x = F.relu(self.fc1(x))
    
        x = self.out(x)

        return x

overfitting problem : training accuracy >> validation accuracy (bad performance on unseen data)

 


Learning Rate

learning_rate : rate at which the model is learning

how bigger step to take to right direction

 

learning했을 때 accuracy가 너무 낮다

   - model doesn't fit to the problem (model can't capture the pattern)

   - learning rate is too high


Batch Size

batch_size : at a time, gpu take {batch_size} images, to update the model

- The smaller the batch_size

   - less computation needs(pros)

   - faster the model update (pros)

   - worse estimate gradient (cons)

- The highter the batch_size

   - the more accurate (pros)


Adding Complexity

model에 parameter더해주는 것과 동일

model overview부분에 있는 코드에서, out_channel, in_channel을 크게 해 주면 됨

or

linear layer을 더 쌓아주면 됨

 

학습가능한 파라미터 자체를 늘려주면 된다는 이야기

더 general하게 representation을 학습하고 각각이 패턴을 잘 저장할 수 있어서 그렇지 않을까 싶음

 


공부한 내용

Parameter vs HyperParameter?

Parameter

  • 모델 내부에서 결정되는 변수
  • 특정 집단에 대해 정규분포를 그린다 → 집단에 따라 평균, 표준편자 자동결정 : parameter
  • 데이터를 통해 구해지며, 모델 내부적으로 결정되는 값
  • 사용자에 의해 조정되지 않음

HyperParameter

A model hyperparameter is a configuration that is external to the model and whose value cannot be estimated from data.

  • 모델링할 때 사용자가 직접 설정하는 값
  • batch size, learning rate, KNN algorithm에서의 K, ...
  • 최적값이 정해져 있지 않으므로 heuristic하거나 emprical하게 설정
  • 베이지안 옵티미제이션같이 최적값 찾아주는 라이브러리 있음

BO, Bayesian optimization

는 다음 포스팅으로 바로 적어야지!