ML (MachineLearning)

ํ‘๋ฐฑ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์…‹์„ AI์—๊ฒŒ ํŒ๋ณ„์‹œ์ผœ, ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ •๋‹ต์„ ๋งž์ถ”๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๊ธฐ๋ฒ•

567Rabbit 2024. 4. 18. 17:29

https://www.tensorflow.org/datasets/catalog/fashion_mnist?hl=ko

 

ํŒจ์…˜_์— ๋‹ˆ์ŠคํŠธ  |  TensorFlow Datasets

์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŒจ์…˜_์— ๋‹ˆ์ŠคํŠธ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”. Fashion-MNIST๋Š” 60,000๊ฐœ์˜ ์˜ˆ์ œ๋กœ ๊ตฌ์„ฑ๋œ

www.tensorflow.org

 

 

 

 

import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist  #fashion_mnist ์‚ฌ์šฉ

(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()

 

 

 

 

์‚ฌ์ง„ ๊ฐ€์ ธ์˜ค๊ธฐ

 

X_train.shape  

(60000, 28, 28)

- ์‚ฌ์ง„ 60000๊ฐœ 28ํ–‰ 28์—ด์ด๋‹ค.

 

 

 

<์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ ๊ฐ€์ ธ์˜ค๊ธฐ>

 

X_train[0 , : , : ]      # 3์ฐจ์›์ด๋ฏ€๋กœ ์ฝค๋งˆ๊ฐ€ 2๊ฐœ์ด๋‹ค

ndarray (28, 28) show data

 

y_train[0]  

9

 

- ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„์˜ ์ •๋‹ต. fashion mnist์˜ label=9๋Š” Ankel boots์ด๋‹ค.

 

 

 

 

 

<๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ ๊ฐ€์ ธ์˜ค๊ธฐ>

 

X_train[1 , : , : ]

 

ndarray (28, 28) show data

 

y_train[1] 

0

 

- ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„์˜ ์ •๋‹ต. fashion mnist์˜ label=0๋Š” T-shirt/top์ด๋‹ค.

 

 

 

 

 

 

ํ”ผ์ฒ˜์Šค์ผ€์ผ๋ง

 

์ด๋ฏธ์ง€๋Š” ๋ฒ”์œ„๊ฐ€ 0~255๊นŒ์ง€ ์ด๊ธฐ๋•Œ๋ฌธ์—, ํ”ผ์ฒ˜์Šค์ผ€์ผ๋ง์€ /255๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค

ํ”ผ์ฒ˜์Šค์ผ€์ผ๋งํ•˜๋ฉด 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค

 

 

X_train = X_train/255.0   #(255๋กœ ๋‚˜๋ˆ„๋ฉด int, 255.0์œผ๋กœ ๋‚˜๋ˆ„๋ฉด float์œผ๋กœ ๋‚˜๋ˆ„๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค)

 

X_test = X_test/255.0

 

 

 

 

 

๋ฐ์ดํ„ฐ์…‹์„ reshapeํ•˜๊ธฐ

 

 

X_train.shape  

(60000, 28, 28)

- ์‚ฌ์ง„ 60000๊ฐœ 28ํ–‰ 28์—ด์ด๋‹ค.

 

 

 

๋ฐ์ดํ„ฐ๋ฅผ reshape ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š”?

 

๋”ฅ๋Ÿฌ๋‹์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ดํ–‰๋˜๋Š”๋ฐ, X_train์˜ ๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์ง„์œผ๋กœ, ๋„ค๋ชจ๋‚œ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค.

๋„ค๋ชจ๋‚œ ํ˜•ํƒœ์˜ ์—ฌ๋Ÿฌ ํ”ฝ์…€๋“ค์„ ๋”ฅ๋Ÿฌ๋‹ํ•˜์—ฌ ๊ฒ€์ •์ƒ‰์€0, ํฐ์ƒ‰์€1๋กœ ํ•˜์—ฌ ์ธ๊ณต์ง€๋Šฅ์ด ์ •๋‹ต์„ ์ฐพ์•„๊ฐ€๋Š” ํ•™์Šต๊ณผ์ •์„ ๋”ฐ๋ฅด๊ธฐ์—,

๋”ฅ๋Ÿฌ๋‹์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ ค๋ฉด flattening์„ ํ†ตํ•ด ํ•˜๋‚˜์˜ ์—ด๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

 

def build_model():
  model = Sequential()
  model.add( Flatten() )   # ์ž…๋ ฅ ๋ ˆ์ด์–ด Flatten์œผ๋กœ, 784๊ฐœ๋กœ ์„ค์ •๋œ๋‹ค (28์—ด*28ํ–‰)
  model.add( Dense(128, 'relu') )  # ํžˆ๋“  ๋ ˆ์ด์–ด๋Š” 128๊ฐœ๋กœ ์„ค์ •ํ•˜์˜€๋‹ค. ์„ค์ •ํ•˜๋Š” ์‚ฌ๋žŒ ๋งˆ์Œ๋Œ€๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค
  model.add( Dense(10, 'softmax'))    # ๋ ˆ์ด๋ธ”์ด 10๊ฐœ(0~9๊นŒ์ง€)์ด๋ฏ€๋กœ, ์ถœ๋ ฅ๋ ˆ์ด์–ด๋Š” 10๊ฐœ๋กœ ์„ค์ •ํ•œ๋‹ค
  model.compile('adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 
  return model
 
 # 3๊ฐœ ์ด์ƒ์˜ ๋ถ„๋ฅ˜๋ฌธ์ œ๋Š” loss function์„ sparse_categorical_crossentropy๋กœ ํ•œ๋‹ค

 

๋”ฅ๋Ÿฌ๋‹์˜ ์ถœ๋ ฅ๊ฐ’์€ ๋‹ค ๋”ํ•ด์„œ 1์ด ๋˜์–ด์•ผ ํ•˜๊ณ , ํ™•๋ฅ ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค 'softmax'๋ผ๋Š” ์•กํ‹ฐ๋ฒ ์ด์…˜ ํŽ‘์…˜์„ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ถœ๋ ฅ๊ฐ’์˜ ํ•ฉ์ด 1๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค

 

 

model = build_model() 

from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(patience=10)

model = build_model()
epoch_history = model.fit(X_train, y_train, epochs= 1000, validation_split=0.2, callbacks=[early_stopping])

 

์‹คํ–‰์‹œํ‚ค๋ฉด loss, accuracy, val_loss, val_accuracy์ด ์—ํฌํฌ๋งˆ๋‹ค ์ถœ๋ ฅ๋œ๋‹ค.

 

 

 

 

 

์–ผ๋ฆฌ์Šคํƒ‘์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๊ธ€์„ ์ฐธ์กฐํ•œ๋‹ค

https://codebunny99.tistory.com/31

 

GridSearch ๋ฅผ ์ด์šฉํ•œ ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฐพ๊ธฐ

! pip install scikeras from scikeras.wrappers import KerasClassifier from sklearn.model_selection import GridSearchCV from keras.models import Sequential from keras.layers import Dense def build_model(optimizer = 'adam') : model = Sequential() model.add(De

codebunny99.tistory.com

 

 

 

 

 

๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ

 

model.evaluate(X_test,y_test)

 

[==============================] - 1s 2ms/step - loss: 0.3772 - accuracy: 0.8818
[0.3771723508834839, 0.8817999958992004]

 

 

# loss์™€ val_loss ๊ทธ๋ž˜ํ”„๋กœ ๋ณด๊ธฐ

 

plt.plot(epoch_history.history['loss'])
plt.plot(epoch_history.history['val_loss'])
plt.legend(['loss','val_loss'])
plt.show()

 

- ๊ณผ๊ฑฐ์— ๋Œ€ํ•œ ํ•™์Šต๋ฐ์ดํ„ฐ์ธ loss๋Š” ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•  ๋•Œ๋งˆ๋‹ค ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๊ฐ€ ๊ฐ์†Œํ•˜์ง€๋งŒ,

val_loss, ์ฆ‰ ๋ฏธ๋ž˜์— ๋“ค์–ด์˜ฌ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๋Š” ๋Š˜์–ด๋‚˜๊ธฐ๋„ ์ค„์–ด๋“ค๊ธฐ๋„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋Œ€์ฒด๋กœ loss๋Š” ์ค„์–ด๋“œ๋Š” ํ˜„์ƒ์„ ๋ณด์ธ๋‹ค.

 

 

# accuracy์™€ val_accuracy ๊ทธ๋ž˜ํ”„๋กœ ๋ณด๊ธฐ

 

plt.plot(epoch_history.history['accuracy'])
plt.plot(epoch_history.history['val_accuracy'])
plt.legend(['accuracy','val_accuracy'])
plt.show()

 

- ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ๊ฐ€ ํ•™์Šต๋ ์ˆ˜๋ก ๋Š˜์–ด๋‚˜์ง€๋งŒ, ๋ฏธ๋ž˜์— ๋Œ€ํ•œ ์˜ˆ์ธก์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ์ƒˆ๋กœ์šด๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก์ด๋ฏ€๋กœ ์ •ํ™•ํ•ด์ง€๊ธฐ๋„, ๋ถ€์ •ํ™•ํ•ด์ง€๊ธฐ๋„ ํ•˜์ง€๋งŒ ๋Œ€์ฒด๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ํ˜„์ƒ์„ ๋ณด์ธ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

confusion matrix

 

 

import seaborn as sb

from sklearn.metrics import confusion_matrix

 

y_pred = np.argmax(model.predict(X_test), axis=1)   

 

# argmax๋ž€ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง„ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

# argmax๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š”, ์ถœ๋ ฅ๊ฐ’ 10๊ฐœ ์ค‘์—์„œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ ธ์˜ค๋Š” max๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฒˆํ˜ธ(์ฆ‰, ๋ผ๋ฒจ ๋ฒˆํ˜ธ)๊ฐ€ ์ •๋‹ต์ธ y_test์™€ ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

cm = confusion_matrix(y_test, y_pred)

cm

[==============================] - 1s 3ms/step
array([[783,   3,  26,  27,   3,   0, 150,   0,   8,   0],
       [  1, 972,   1,  19,   5,   0,   2,   0,   0,   0],
       [ 12,   1, 810,  12, 124,   0,  41,   0,   0,   0],
       [ 15,   5,  10, 904,  29,   0,  31,   0,   5,   1],
       [  0,   0,  94,  33, 845,   0,  26,   0,   2,   0],
       [  0,   0,   0,   1,   0, 946,   0,  23,   3,  27],
       [ 75,   0, 113,  31, 100,   0, 674,   0,   7,   0],
       [  0,   0,   0,   0,   0,  11,   0, 974,   1,  14],
       [  5,   0,  10,   4,   5,   2,  11,   4, 959,   0],
       [  0,   0,   0,   0,   0,   5,   1,  43,   0, 951]])

 

 

np.diagonal(cm).sum() / cm.sum()  # ์ •ํ™•๋„ = ๋Œ€๊ฐ์„ ์˜ ๊ฐ’์˜ ํ•ฉ / ์ „์ฒด ํ•ฉ

0.8818