딥러닝을 시작합니다

1. 인공 신경망

패션 MNIST

레이블 0 1 2 3 4 5 6 7 8 9
패션 아이템 티셔츠 바지 스웨터 드레스 코트 샌달 셔츠 스니커즈 가방 앵클 부츠
# 패션 MNIST 데이터 불러오기
from tensorflow import keras
(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()
print(train_input.shape, train_target.shape)
print(test_input.shape, test_target.shape)

# 몇 개의 샘플 이미지 출력하기
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,10,figsize=(10,10))
for i in range(10):
  axs[i].imshow(train_input[i], cmap='gray_r')
  axs[i].axis('off')
plt.show()

# 처음 10개 샘플의 타깃값을 리스트로 만든 후 출력
print([train_target[i] for i in range(10)])

# 레이블당 샘플 개수 확인
import numpy as np
print(np.unique(train_target, return_counts=True))

1.png

※ TensorFlow : 딥러닝 라이브러리

로지스틱 회귀로 패션 아이템 분류하기

# 정규화 & 2차원 배열 -> 1차원 배열로 만들기
train_scaled = train_input / 255.0
train_scaled = train_scaled.reshape(-1,28*28)
print(train_scaled.shape)

# 교차 검증으로 성능 확인
from sklearn.model_selection import cross_validate
from sklearn.linear_model import SGDClassifier
sc = SGDClassifier(loss='log', max_iter=5, random_state=42)
scores = cross_validate(sc, train_scaled, train_target, n_jobs=-1)
print(np.mean(scores['test_score']))

⇒ 이런 식으로 10개의 클래스에 대한 선형 방정식을 모두 계산 → 소프트맥스 함수를 통하여 각 클래스에 대한 확률을 얻을 수 있음