import tensorflow as tf
import numpy as np
from tensorflow import keras
fashion_mnist๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํ ์ํ๋ก์ฐ์ ์์
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels),(test_images,test_labels) = fashion_mnist.load_data() # 4๊ฐ์ numpy ๋ฐฐ์ด
labels์ 0๋ถํฐ 9๊น์ง์ ์์ด๊ณ ๊ฐ๊ฐ ์๋์ ์์ดํ ์ ๋ํ๋ธ๋ค
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
์๋ ์ฝ๋๋ก ๋ฐ์ดํฐ ํฌ๊ธฐ ํ ์คํธ๊ฐ ๊ฐ๋ฅํ๋ค
#len(train_labels) #60000์ ์ฅ๋์ด์์
#train_labels #array([9, 0, 0, ..., 3, 0, 5], dtype=uint8) ๊ฐ ๋ ์ด๋ธ 0~9 ์ ์
#test_images.shape # (10000, 28, 28) 10000๊ฐ์ ์ด๋ฏธ์ง 28 x 28 pixels
- Flatten : ์ด๋ฏธ์ง 2์ฐจ์ -> 1์ฐจ์์ผ๋ก ๋ณ๊ฒฝ(๋ณํ๋ง ํจ, ํ์ตX)
- Dense : 128๊ฐ์ ๋ด๋ฐ ๊ฐ์ง
- 10๊ฐ์ ๋ ธ๋ (10๊ฐ์ ํ๋ฅ ๋ฐํ)
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
- optimizer์ 1 week์ 'sgd' ์ ๋ค๋ฅธ 'adam' ์ ์ฌ์ฉ
model.compile(optimizer='adam', #๋ฐ์ดํฐ์ loss ๋ฐํ์ผ๋ก ๋ชจ๋ธ ์
๋ฐ์ดํธ ๋ฐฉ๋ฒ ๊ฒฐ์
loss='sparse_categorical_crossentropy',# ๋ชจ๋ธ์ ์ค์ฐจ ๊ฒฐ์
metrics=['accuracy']) # ์ด๋ฏธ์ง ์ถ์ ๋น์จ
model.fit(train_images, train_labels,epochs=5) # 0.81์ ์ ํ๋ ๋์ด
model.summary() ๋ฅผ ์ด์ฉํ๋ฉด ๊ฐ epochs ๊ตฌ์กฐ๋ฅผ ๋ณด์ฌ์ค๋ค
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nํ
์คํธ ์ ํ๋:', test_acc) # 0.7983 ๋์จ๋ค