인공 신경망

패션 MNIST

In [1]:
from tensorflow import keras

(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
In [2]:
print(train_input.shape, train_target.shape)
(60000, 28, 28) (60000,)
In [3]:
print(test_input.shape, test_target.shape)
(10000, 28, 28) (10000,)
In [4]:
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()
In [5]:
print([train_target[i] for i in range(10)])
[9, 0, 0, 3, 0, 2, 7, 2, 5, 5]
In [6]:
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]))

로지스틱 회귀로 패션 아이템 분류하기

In [7]:
train_scaled = train_input / 255.0
train_scaled = train_scaled.reshape(-1, 28*28)
In [8]:
print(train_scaled.shape)
(60000, 784)
In [9]:
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.8195666666666668

인공신경망

텐서플로와 케라스

In [10]:
import tensorflow as tf
In [11]:
from tensorflow import keras

인공신경망으로 모델 만들기

In [12]:
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)
In [13]:
print(train_scaled.shape, train_target.shape)
(48000, 784) (48000,)
In [14]:
print(val_scaled.shape, val_target.shape)
(12000, 784) (12000,)
In [15]:
dense = keras.layers.Dense(10, activation='softmax', input_shape=(784,))
In [16]:
model = keras.Sequential(dense)

인공신경망으로 패션 아이템 분류하기

In [17]:
model.compile(loss='sparse_categorical_crossentropy', metrics='accuracy')
In [18]:
print(train_target[:10])
[7 3 5 8 6 9 3 3 9 9]
In [19]:
model.fit(train_scaled, train_target, epochs=5)
Epoch 1/5
1500/1500 [==============================] - 5s 2ms/step - loss: 0.6094 - accuracy: 0.7943
Epoch 2/5
1500/1500 [==============================] - 2s 2ms/step - loss: 0.4794 - accuracy: 0.8385
Epoch 3/5
1500/1500 [==============================] - 2s 2ms/step - loss: 0.4553 - accuracy: 0.8483
Epoch 4/5
1500/1500 [==============================] - 2s 2ms/step - loss: 0.4444 - accuracy: 0.8521
Epoch 5/5
1500/1500 [==============================] - 2s 2ms/step - loss: 0.4376 - accuracy: 0.8551
Out[19]:
<tensorflow.python.keras.callbacks.History at 0x7ff144158610>
In [20]:
model.evaluate(val_scaled, val_target)
375/375 [==============================] - 1s 1ms/step - loss: 0.4555 - accuracy: 0.8508
Out[20]:
[0.45545145869255066, 0.8507500290870667]