[DL] 실습 - 다중분류 (손글씨 데이터)

Minjeong Kim·2026년 1월 7일

인공지능

목록 보기
24/50

📢 학습목표

  • 손글씨 데이터를 분류하는 딥러닝 모델링을 진행해보자
  • 다중분류 딥러닝 모델링을 설계하자
  • 이미지 데이터 기본 정보에 대해서 확인하자

code

  1. 라이브러리 불러오기

    # 기본 라이브러리 불러오기
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
  2. 데이터 불러오기

    • keras 에 있는 손글씨 데이터셋 이용
    # 데이터 불러오기
    from tensorflow.keras.datasets import mnist
    
    # 훈련용 데이터, 테스트용 데이터 구분해서 저장해둠 -> 그대로 변수에 담아주기
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    • 데이터 확인
      X_train[0]
      image.png
      • 28*28 가로 28 픽셀, 세로 28 픽셀
      • 흑백 이미지 → 얼마나 검정인지, 흰색인지에 대한 정보를 하나의 픽셀이 가짐
      • 0(검은색), 255(흰색)
      • 데이터 예시
        ndarray (28, 28) hide data
        array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                  0,   0],
               ...
               [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   3,
                 18,  18,  18, 126, 136, 175,  26, 166, 255, 247, 127,   0,   0,
                  0,   0],
               [  0,   0,   0,   0,   0,   0,   0,   0,  30,  36,  94, 154, 170,
                253, 253, 253, 253, 253, 225, 172, 253, 242, 195,  64,   0,   0,
                  0,   0],
               [  0,   0,   0,   0,   0,   0,   0,  49, 238, 253, 253, 253, 253,
                253, 253, 253, 253, 251,  93,  82, 
    • 전체 정답 데이터 확인 (클래스 개수 확인)
      np.unique(y_train)
      # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)
      • 0~9까지 숫자를 가지는 손글씨 데이터 → 클래스 10개 → 다중분류 학습
  3. MLP 모델 설계

    from tensorflow.keras import Sequential
    from tensorflow.keras.layers import Input, Dense, Flatten
    
    # 모델 설계
    model = Sequential()
    
    # input layer
    model.add(Input(shape=(28,28)))
    
    # hidden layer
    model.add(Flatten()) # 2차원 사진 데이터 입력받아 1차원으로 변환
    model.add(Dense(units=16, activation = 'relu'))
    model.add(Dense(units=8, activation = 'relu'))
    model.add(Dense(units=16, activation = 'relu'))
    
    # output layer
    model.add(Dense(units=10, activation = 'softmax'))
    • 다중분류 시, output layer 설계 방법
      • units : 노드 수 = class 종류 개수
      • activation : 활성화 함수는 보통 ‘softmax’ 사용
  4. 학습 방법 및 평가 방법 설계

    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer='SGD',
                  metrics=['accuracy'])
    • loss : sparse_categorical_crossentropy 사용
      • 다중 분류라서 output layer 의 노드 수랑 맞춰줘야 해서
  5. 학습

    model.fit(X_train, y_train, validation_split=0.2, epochs=20, batch_size=64)
  6. 시각화

    # 시각화
    plt.figure(figsize=(5,3))
    plt.plot(model.history.history['loss'], label='loss')
    plt.plot(model.history.history['val_loss'], label='val_loss')
    plt.legend()

    image.png

0개의 댓글