카테고리 없음

딥러닝 4일차

write31991 2026. 9. 10. 16:30

< 복습 >

구분 세부항목 Regression 2-Class Multi-Class
전처리 y 필요시 스케일링
(→ y의 값이 클 때)
0, 1 0, 1, 2, 3, ・・・
Hiddem Layer Activation ReLU ReLU ReLU
Output Layer Activation X Sigmoid Softmax
(오차함수에 포함)
Node 수 1 1 Class 수
Compile optimizer Adam Adam Adam
loss nn.MSELoss nn.BCELoss nn.CrossEntropyLoss
후처리 pred X 0, 1로 변환
(np.where)
.softmax().argmax()

 

< 클래스 선언 방식 >

- Class 방식 모델 설계

    - 클래스란?

       - 객체지향 프로그램의 핵심 개념

       - 데이터(속성)와 기능(메서드 method)을 하나로 묶어 코드를 효율적으로 관리

 

    - 모델을 클래스로 선언하기

       - 딥러닝 모델도 하나의 객체처럼 다룰 수 있음

# 직접 모델 선언 (어제까지 계속 써왔던 방식)

model = nn.Sequential(
    	    nn.Linear(10, 3),
            nn.ReLU(),
            nn.Linear(3, 1)
).to(device)



# 클래스 생성 & 선언

class NeuralNet(nn.Module):
	def __init__(self):
    	    super(NeuralNet, self).__init__()
            self.fc1 = nn.Linear(10, 3)
            self.relu = nn.ReLU()
            self.fc2 = nn.Linear(3, 1)
            
	def forward(self, x):
	    x = self.fc1(x)
            x = self.relu(x)
            x = self.fc2(x)
            return x
            
model = NeuralNet().to(device)

 

    - 모델 선언 방식

       - 기본 모델 설계

         ① NeuralNet 이라는 이름으로 클래스 정의

            · 파이토치가 제공하는 nn.Module 을 상속받음

            · 모든 파이토치 모델은 nn.Module 을 기반으로 생성됨

 

         ② __init__ (초기화 메서드)               ▶ 순서 상관없이 모델에 필요한 층을 정의하면 됨

            · 객체가 만들어질 때 자동 실행됨

            · 여기에 모델에 필요한 층을 정의함

 

        ③ forward (순전파 메서드)                 ▶ 모델이 진행할 순서대로 작성해야 함

            · 입력 데이터가 각 층을 통화하는 순서를 정의함

 

 

- 모델 설계 방법 3가지!!

▶ 다양한 방식은 코드를 얼마나 간결하고 효율적으로 쓰느냐의 차이일 뿐, 성능의 차이는 없음

① 기본형

class NeuralNet(nn.Module):
	def __init__(self, n_feature, n_claass):
    	super(NeuralNet, self).__init__()
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(n_feature, 256)
        self.fc2 = nn.Linear(256, 64)
        self.fc3 = nn.Linear(64, n_class)
        self.relu = nn.ReLU()
        
	def forward(self, x):
    	x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        x = self.relu(x)
        x = self.fc3
        return x

 

② 중첩형

class NeuralNet(nn.Module):
	def __init__(self, n_feature, n_class):
    	super(NeuralNet, self).__init__()		# super().__init__() 으로 써도 됨
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(n_feature, 256)
        self.fc2 = nn.Linear(256, 64)
        self.fc3 = nn.Linear(64, n_class)
        self.relu = nn.ReLU()
        
	def forward(self, x):
    	x = self.flatten(x)
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.fc3(x)
        return x

 

③ 모듈형

class NeuralNet(nn.Module):
	def __init__(self, n_feature, n_class):
    	super(NeuralNet, self).__init__()
        self.flatten = nn.Flatten()
        self.fc1 = nn.Sequential(nn.Linear(n_feature, 256), nn.ReLU())
        self.fc2 = nn.Sequential(nn.Linear(256, 64), nn.ReLU())
        self.fc3 = nn.Linear(64, n_class)
        
	def forward(self, x)
    	x = self.flatten(x)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x