Skip to content

Latest commit

ย 

History

History
66 lines (57 loc) ยท 1.92 KB

File metadata and controls

66 lines (57 loc) ยท 1.92 KB

2 week

Using Fashion MNIST


import library

import tensorflow as tf
import numpy as np
from tensorflow import keras

import fasion mnist

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

making model

  • 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')
])

model compile

  • optimizer์€ 1 week์˜ 'sgd' ์™€ ๋‹ค๋ฅธ 'adam' ์„ ์‚ฌ์šฉ
model.compile(optimizer='adam', #๋ฐ์ดํ„ฐ์™€ loss ๋ฐ”ํƒ•์œผ๋กœ ๋ชจ๋ธ ์—…๋ฐ์ดํŠธ ๋ฐฉ๋ฒ• ๊ฒฐ์ •
              loss='sparse_categorical_crossentropy',# ๋ชจ๋ธ์˜ ์˜ค์ฐจ ๊ฒฐ์ • 
              metrics=['accuracy']) # ์ด๋ฏธ์ง€ ์ถ”์ • ๋น„์œจ 

training

model.fit(train_images, train_labels,epochs=5) # 0.81์˜ ์ •ํ™•๋„ ๋‚˜์˜ด

model.summary() ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ epochs ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค

Identify the performance of model with a test set

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

Predicting

print('\nํ…Œ์ŠคํŠธ ์ •ํ™•๋„:', test_acc)  # 0.7983 ๋‚˜์˜จ๋‹ค