딥러닝 Deep learning - Binary Classifier

LSH·2023년 9월 7일
0

교육 정보

  • 교육 명: 경기미래기술학교 AI 교육
  • 교육 기간: 2023.05.08 ~ 2023.10.31
  • 강사: 양정은 강사님
  • 강의 계획:
    1. 딥러닝 소개 및 프레임워크 설치
    2. Artificial Neurons Multilayer Perceptrons
    3. 미분 기초 Backpropagation
    4. MLP 구현하기
    5. MLP로 MNIST 학습시키기(Mini-project)
    6. Sobel Filtering
    7. Convolutional Layers
    8. 이미지 분류를 위한 CNNs
    9. CNN으로 MNIST 학습시키기
    10. VGGNet, ResNet, GoogLeNet
    11. Image Classification 미니 프로젝트
    12. Object Detection 소개
    13. Region Proposal, Selective Search
    14. RCNN
    15. Single Shot Detection

Binary Classifier

Binary Classifier Model을 구현하고 시각화하기

  • 위와 같은 데이터를 만들기 위해 make_blobs를 사용하여 데이터 생성
      n_c_samples = 200
      n_class = 2
      N_SAMPLES = n_class*n_c_samples
      n_features = 2
      X1, y1 = make_blobs(n_samples=n_c_samples, centers=[(-1, -1), (-1, 1)], n_features=n_features, cluster_std=0.2)
      X2, y2 = make_blobs(n_samples=n_c_samples, centers=[(1, 1), (1, -1)], n_features=n_features, cluster_std=0.2)
      X = np.vstack((X1, X2))
      y = np.hstack((y1, y2))
      ```
    ✏️ 동일한 label(실제값)을 주려는 값을 center의 같은 인덱스에 위치시켜야 함 
  • 생성된 데이터의 feature수와 최종 out features에 주의하여 Modeling 생성

    class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.fc1 = nn.Linear(in_features=2, out_features=4)
        self.fc1_act = nn.Sigmoid()
        self.fc2 = nn.Linear(in_features=4, out_features=1)
        self.fc2_act = nn.Sigmoid()
    
      def forward(self, x):
        x = self.fc1.forward(x)  
        x = self.fc1_act.forward(x)
        x = self.fc2.forward(x)  
        x = self.fc2_act.forward(x)
        x = x.view(-1)
        return x
  • Model을 사용하여 학습 진행

    EPOCH = 10
    loss_list = list()
    acc_list = list()
    for epoch in range(EPOCH):
      epoch_loss = 0
      n_correct = 0
    
      for X, y in dataloader:
        X, y = X.to(DEVICE), y.to(DEVICE)
    
        pred = model(X)
        loss = loss_function(pred, y)
    
        # ===
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    
        epoch_loss += loss.item() * len(X)
        pred = (pred > 0.5).type(torch.float)
        n_correct += (pred == y).sum().item()
    
      loss = epoch_loss/N_SAMPLES
      acc = n_correct/N_SAMPLES
    
      if (epoch+1)%10 == 0: print(f"{acc = }, {loss = }")
      loss_list.append(loss)
      acc_list.append(acc)
  • 10회 학습 결과

  • 여러번 학습시킨 모델에서는 아래와 같은 예측값을 얻을수 있었음

profile
:D

0개의 댓글