티스토리 뷰

코랩 실습파일)

 

beginner.ipynb

Run, share, and edit Python notebooks

colab.research.google.com

깃허브 소스)

 

GitHub - tensorflow/docs-l10n: Translations of TensorFlow documentation

Translations of TensorFlow documentation. Contribute to tensorflow/docs-l10n development by creating an account on GitHub.

github.com

 

 

* Keras를 사용하여 수행.

  1. 사전에 빌드한 데이터 세트 로드
  2. 이미지를 분류하는 신경망 머신러닝 모델 빌드
  3. 해당 신경망 훈련
  4. 모델의 정확도 평가

 

 

 

 1. TensorFlow 설정하기

import tensorflow as tf
print("TensorFlow version:", tf.__version__)

# colab에서 실행하면 최신버전으로 실행 가능합니다.

 

 

 2. 데이터셋 로드하기

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train[0].shape)
x_train, x_test = x_train / 255.0, x_test / 255.0
>> (28, 28)


import matplotlib.pyplot as plt

idx=5
plt.imshow(x_train[idx])
plt.title(y_train[idx])
>> Text(0.5, 1.0, '2')

# 2차원 데이터는 신경망에 주입을 할 수 없다.
x_train[0].shape
>> (28, 28)

# 신경망에 주입하기 위해 1차원 형태로 변형한다.
# flatten()
x_train[0].flatten().shape
>> (784, )

x_train[0].flatten() 
>> array([0.          , 0.          , 0.          , 0.          , 0.          , ... ])

 

 

 3. 머신러닝 모델 빌드하기

층을 차례대로 쌓아 tf.keras.Sequential 모델을 만듭니다. 훈련에 사용할 옵티마이저(optimizer)와 손실 함수를 선택합니다.

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

각 예시에서 모델은 각 클래스에 대해 하나씩, logits 또는 log-odds 스코어 벡터를 반환합니다.

predictions = model(x_train[:1]).numpy()
predictions
>> array([[0.07470351, 0.08945837, 0.06401815, 0.09958632, 0.06803247,
        0.17370385, 0.10852029, 0.0912534 , 0.09243535, 0.13828829]],
      dtype=float32)

tf.nn.softmax 함수는 다음과 같이 이러한 로짓을 각 클래스에 대한 확률로 변환합니다.

tf.nn.softmax(predictions).numpy()
>> array([[0.09745193, 0.09890047, 0.09641616, 0.09990722, 0.09680399,
        0.10759342, 0.10080379, 0.09907816, 0.09919534, 0.10384963]],
      dtype=float32)
      
tf.nn.softmax(predictions).numpy().sum()
>> 1.0

import numpy as np
np.argmax(tf.nn.softmax(predictions).numpy())
>> 5


참고: tf.nn.softmax 함수를 네트워크의 마지막 레이어에 대한 활성화 함수로 베이킹할 수 있습니다. 
이렇게 하면 모델 출력을 더 직접적으로 해석할 수 있지만 이 접근법은 소프트맥스 출력을 사용할 경우 
모든 모델에 대해 정확하고 수치적으로 안정적인 손실 계산을 제공하는 것이 불가능하므로 권장하지 않습니다.

losses.SparseCategoricalCrossentropy를 사용하여 로짓의 벡터와 True 인덱스를 사용하고 각 예시에 대해 스칼라 손실을 반환하는 훈련용 손실 함수를 정의합니다.

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

이 손실은 실제 클래스의 음의 로그 확률과 같습니다. 모델이 올바른 클래스를 확신하는 경우 손실은 0입니다.

이 훈련되지 않은 모델은 무작위에 가까운 확률(각 클래스에 대해 1/10)을 제공하므로

초기 손실은 -tf.math.log(1/10) ~= 2.3에 근접해야 합니다.

loss_fn(y_train[:1], predictions).numpy()
>> 2.2293959

훈련을 시작하기 전에 Keras Model.compile을 사용하여 모델을 구성하고 컴파일합니다. 

optimizer 클래스를 adam으로 설정하고 loss를 앞에서 정의한 loss_fn 함수로 설정합니다. 

metrics 매개변수를 accuracy로 설정하여 모델에 대해 평가할 메트릭을 지정합니다.

model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])

 

 

 4. 모델 훈련 및 평가하기

model.fit(x_train, y_train, epochs=5)

Model.evaluate 메서드는 일반적으로 "Validation-set" 또는 "Test-set"에서 모델 성능을 확인합니다.

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test,  y_test, verbose=2)

훈련된 이미지 분류기는 이 데이터셋에서 약 98%의 정확도를 달성합니다.

모델이 확률을 반환하도록 하려면 다음과 같이 훈련된 모델을 래핑하고 여기에 소프트맥스를 첨부할 수 있습니다.

probability_model = tf.keras.Sequential([
  model,
  tf.keras.layers.Softmax()
])

probability_model(x_test[:5])
>> <tf.Tensor: shape=(5, 10), dtype=float32, numpy=
array([[0.08533801, 0.08533801, 0.08533802, 0.08534664, 0.08533801,
        0.08533801, 0.08533801, 0.2319492 , 0.08533801, 0.08533803],
       [0.08533675, 0.08533677, 0.23196921, 0.08533676, 0.08533675,
        0.08533678, 0.08533675, 0.08533675, 0.08533675, 0.08533675],
       [0.08534387, 0.23185657, 0.08536338, 0.08534395, 0.08534426,
        0.0853439 , 0.08534405, 0.08536484, 0.0853513 , 0.08534387],
       [0.23196821, 0.08533681, 0.0853372 , 0.08533682, 0.08533681,
        0.08533681, 0.08533683, 0.08533683, 0.08533681, 0.08533685],
       [0.0853756 , 0.08537558, 0.08537596, 0.08537558, 0.23135364,
        0.08537558, 0.08537566, 0.08537634, 0.08537558, 0.08564048]],
      dtype=float32)>

 

'TIL & WIL > Deep Learning' 카테고리의 다른 글

CNN 간단히 알아보기  (0) 2023.04.17
✨ 딥러닝 : 인공신경망 및 과정  (0) 2023.04.11
TensorFlow - Regression 실습 (1) 선형회귀  (0) 2023.04.11
TensorFlow - Classification 실습  (0) 2023.04.11
✨ 딥러닝 기초  (1) 2023.04.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함