16. Keras

akanana·2023년 1월 18일
0

개인공부

목록 보기
28/30
post-thumbnail
post-custom-banner

옷을 구분하자

먼저 fashion minist 데이터를 받아와보자

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) # (60000, 28, 28) (60000,)
print(test_input.shape, test_target.shape) # (10000, 28, 28) (10000,)

훈련세트는 60000개의 28*28의 이미지, 테스트세트는 12000개의 28*28이미지를 가진다

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()

이미지를 추력하면 다음과 같은 이미지가 출력된다

import numpy as np
print(np.unique(train_target, return_counts=True))
"""
(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8), 
array([6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000],
dtype=int64))
"""

6000개씩 10가지 타겟이 들어있다

로지스틱 회귀

train_scaled = train_input / 255.0
train_scaled = train_scaled.reshape(-1,28*28)

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'])) # 0.819...

이제 로지스틱 회귀를 통해 모델을 훈련시켜보자
82%정도로 만족스러운 결과는 아니다

어떻게 동작중이지?

z티셔츠=w1p1+w2p2...w784p784+bz_{티셔츠} = w_1*p_1 + w_2*p_2 ...w_{784}*p_{784} + b
...
z바지=w1p1+w2p2...w784p784+bz_{바지} = w_1*p_1 + w_2*p_2 ...w_{784}*p_{784} + b

로지스틱 회귀 식을 바탕으로 각각의 픽셀을 계수와 곱하여 값을 도출하였을것이다

인공신경망 사용하기

from sklearn.model_selection import train_test_split
train_scaled, val_scaled, train_target, val_target = \
    train_test_split(train_scaled, train_target, test_size=0.2, random_state=42)
    
print(train_scaled.shape, train_target.shape) # (48000, 784) (48000,)
print(val_scaled.shape, val_target.shape) # (12000, 784) (12000,)

훈련세트와 검증세트를 만들었다

밀집층

...

dense = keras.layers.Dense(10,activation='softmax', input_shape=(784,))
model = keras.Sequential(dense)
model.compile(loss='sparse_categorical_crossentropy', metrics='accuracy')
print(train_target[:10]) # [7 3 5 8 6 9 3 3 9 9]

이제 ephoc이 늘어감에 따른 정확도를 출력해보자

model.fit(train_scaled, train_target, epochs=5)
model.evaluate(val_scaled, val_target)
Epoch 1/5
1500/1500 [==============================] - 2s 1ms/step - loss: 0.6066 - 
accuracy: 0.7940
Epoch 2/5
1500/1500 [==============================] - 2s 1ms/step - loss: 0.4745 - 
accuracy: 0.8399
Epoch 3/5
1500/1500 [==============================] - 2s 1ms/step - loss: 0.4489 - 
accuracy: 0.8470
Epoch 4/5
1500/1500 [==============================] - 2s 1ms/step - loss: 0.4378 - 
accuracy: 0.8511
Epoch 5/5
1500/1500 [==============================] - 2s 1ms/step - loss: 0.4288 - 
accuracy: 0.8540
375/375 [==============================] - 0s 981us/step - loss: 0.4418 - 
accuracy: 0.8507
post-custom-banner

0개의 댓글