from tensorflow import keras
import tensorflow as tf
(train_input, train_target), (test_input, test_target) = tf.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,)
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()
print([train_target[i] for i in range(10)])
# [9, 0, 0, 3, 0, 2, 7, 2, 5, 5]
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]))
train_scaled = train_input / 255.0
train_scaled = train_scaled.reshape(-1, 28*28)
print(train_scaled.shape)
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_validate
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.8195666
하나의 층에 모든 노들들이 연결된 모델을 만들어보자.
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,)
10개의 클래스이기에 마지막 층은 10개의 유닛을 두어야 한다.
dense = keras.layers.Dense(10, activation='softmax', input_shape=(784,))
model = keras.Sequential(dense)
이진분류 : binary_crossentropy
다중분류 : categorical_crossentropy
타겟값은 정수로 출력이 된다.
ex) [7 3 5 8 6 9 3 3 9 9]
타겟이 one-hot encoding이 되어 있다면 loss 함수를 categorical_crossentropy으로 사용하지만 타겟값을 정수로 사용하고 싶다면 sparse_categorical_crossentropy로 사용하면 된다.
model.compile(loss = 'sparse_categorical_crossentropy', metrics='accuracy')
model.compile(loss = 'sparse_categorical_crossentropy', metrics='accuracy')
model.fit(train_scaled, train_target, epochs=5)
#
Epoch 1/5
1500/1500 [==============================] - 6s 3ms/step - loss: 0.6080 - accuracy: 0.7934
Epoch 2/5
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4789 - accuracy: 0.8386
Epoch 3/5
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4573 - accuracy: 0.8482
Epoch 4/5
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4441 - accuracy: 0.8529
Epoch 5/5
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4371 - accuracy: 0.8541
<keras.callbacks.History at 0x7f1a0e78f4d0>
model.evaluate(val_scaled, val_target)
# [0.44075122475624084, 0.8517500162124634]
층을 만들고 모델을 설정하는 세분화의 과정