To . . ์ธ๋ ๐ผ ์ ์ค๋๋์ด์ฌํ์ผ์ด์ฌ!!์นญ์ฐฌํด์ฃผ์ธ์ผ!~!~!~!~!โค๏ธ
from keras.datasets.mnist import load_data
# Keras ์ ์ฅ์์์ ๋ฐ์ดํฐ ๋ค์ด
(X_train, y_train), (X_test, y_test) = load_data(path='mnist.npz')
X_train.shape
# ํ๋ จ ๋ฐ์ดํฐ
print(X_train.shape, y_train.shape)
print(y_train)
# ํ
์คํธ ๋ฐ์ดํฐ
print(X_test.shape, y_test.shape)
print(y_test)
import matplotlib.pyplot as plt
import numpy as np
idx = 0
img = X_train[idx, :]
label = y_train[idx]
plt.figure()
plt.imshow(img)
plt.title('%d-th data, label is %d' %(idx, label))
from sklearn.model_selection import train_test_split
# ํ๋ จ/๊ฒ์ฆ ๋ฐ์ดํฐ๋ฅผ 70/30 ๋น์จ๋ก ๋ถ๋ฆฌ
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, random_state=777)
print(X_train.shape) # ํ๋ จ๋ฐ์ดํฐ 42000
print(X_val.shape) # ๊ฒ์ฆ๋ฐ์ดํฐ 18000
๋ชจ๋ธ ์ ๋ ฅ์ ์ํ ํ์ต๋ฐ์ดํฐ(์๊ธ์จ ์ด๋ฏธ์ง ๋ฒกํฐ) ์ ์ฒ๋ฆฌ
- 2์ฐจ์ ๋ฐฐ์ด -> input_dim -> 1์ฐจ์ ๋ฐฐ์ด(28*28=784)
- ์ ๊ทํ -> 0 ~ 255 -> 0 ~ 1
num_x_train = X_train.shape[0] # 42000
num_x_val = X_val.shape[0] # 18000
num_x_test = X_test.shape[0] # 10000
# 1. (28, 28) -> 1์ฐจ์ ๋ฐฐ์ด๋ก ์ฒ๋ฆฌ
X_train = (X_train.reshape(num_x_train, 28*28))
X_val = (X_val.reshape(num_x_val, 28*28))
X_test = (X_test.reshape(num_x_test, 28*28))
print(X_train.shape) -> (42000, 784)
print(X_val.shape) -> (18000, 784)
print(X_test.shape) -> (10000, 784)
์ฌ๋ฌ๊ฐ์ง ์ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ - ์ค์ผ์ผ๋ง
Normalization(MinMax)
Robust Normalization
Standardization
# MinMax 0~255 -> 0~1
X_train = X_train / 255
X_val = X_val / 255
X_test = X_test / 255
print(X_train[0])
from tensorflow.keras.utils import to_categorical
# ์์น ์ ๋ต ๋ฐ์ดํฐ -> ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ก ๋ณ๊ฒฝ
y_train = to_categorical(y_train)
y_val = to_categorical(y_val)
y_test = to_categorical(y_test)
y_train
๋ชจ๋ธ ๋ง์ง๋ง ์ธต์์ ์ํํธ๋งฅ์ค(softmax) ํจ์๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ๋ฒ์ฃผํ ๋ ์ด๋ธ๋ก ๋ณํ
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=784))
model.add(Dense(32, activation='relu'))
# ์ด์ค๋ถ๋ฅ sigmoid ๋ค์ค๋ถ๋ฅ softmax
model.add(Dense(10, activation='softmax')) # 10๊ฐ์ ์ถ๋ ฅ์ ๊ฐ์ง ์ ๊ฒฝ๋ง -> ์ ๋ต์ shape์ ๋์ผ
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['acc'])
history = model.fit(X_train, y_train, epochs=30, batch_size=128, validation_data=(X_val, y_val))
import matplotlib.pyplot as plt
his_dict = history.history
loss = his_dict['loss']
val_loss = his_dict['val_loss'] # ๊ฒ์ฆ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ โval_โ ์์์ด๊ฐ ๋ถ์ต๋๋ค.
epochs = range(1, len(loss) + 1)
fig = plt.figure(figsize = (10, 5))
# ํ๋ จ ๋ฐ ๊ฒ์ฆ ์์ค ๊ทธ๋ฆฌ๊ธฐ
ax1 = fig.add_subplot(1, 2, 1)
ax1.plot(epochs, loss, color = 'blue', label = 'train_loss')
ax1.plot(epochs, val_loss, color = 'orange', label = 'val_loss')
ax1.set_title('train and val loss')
ax1.set_xlabel('epochs')
ax1.set_ylabel('loss')
ax1.legend()
acc = his_dict['acc']
val_acc = his_dict['val_acc']
# ํ๋ จ ๋ฐ ๊ฒ์ฆ ์ ํ๋ ๊ทธ๋ฆฌ๊ธฐ
ax2 = fig.add_subplot(1, 2, 2)
ax2.plot(epochs, acc, color = 'blue', label = 'train_acc')
ax2.plot(epochs, val_acc, color = 'orange', label = 'val_acc')
ax2.set_title('train and val acc')
ax2.set_xlabel('epochs')
ax2.set_ylabel('loss')
ax2.legend()
plt.show()
๋ ๊ทธ๋ํ๊ฐ ์ด๋์๋ถํฐ ๋ฒ์ด์ง๋๊ฐ?
- ๊ณผ๋์ ํฉ ๋ฌธ์ ๊ฐ ๋ํ๋ ๊ฒ
- ๋ฐ์ดํฐ ํน์ฑ, ๋ชจ๋ธ ๊ตฌ์กฐ ๋ฑ์ ์์ ํด๋ณด๊ณ ์ฌํ์ต
- ๋ฒ์ด์ง๊ธฐ ์ ๊น์ง ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๊ณ ์ด๋ฅผ ์ ์ฅ ๋ฐ ๊ธฐ๋ก
model.evaluate(X_test, y_test)
results = model.predict(X_test)
print(results.shape)
print(results[5])
np.argmax(results[5], axis=-1)
idx = 5
arg_results = np.argmax(results, axis=-1)
plt.imshow(X_test[idx].reshape(28,28))
plt.title('predict value of the image : ' + str(arg_results[idx]))
plt.show()
results[9] , idx = 9๋ก ๋ณ๊ฒฝํ๋ฉด
0~9๊น์ง ๊ฐ ์ค 9๊ฐ ๊ฐ์ฅ ํฌ๋ฏ๋ก 9๋ก ์์ธก
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
plt.figure(figsize=(7,7))
cm = confusion_matrix(np.argmax(y_test, axis=-1), np.argmax(results, axis=-1)) # ์ค์ ์ ๋ต๊ณผ ๋น๊ต
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()
๐ 4์ 9๋ฅผ ๋ถ๋ฅํ ๋ ํผ๋์ค๋ฌ์ ํจ
print(classification_report(np.argmax(y_test, axis=-1), np.argmax(results, axis=-1)))
๐ ์ฃผ๋ง๋ชฉํ
๋ฅ๋ฌ๋ ์
๋ฌธ ์ฑ
๋ณด๊ณ ๋ฐฐ์ด๊ณณ๊น์ง ์ ๋ฆฌํด๋ณด๊ฑฐ๋ ๋ชจ๋ฅด๋ ๋ถ๋ถ ์ดํดํ๊ธฐ @ @!
์ด๋ฒ์ฃผ์์ดํด๋ชปํ๋ฉดํ๋ก์ ํธ์ฃฝ์,,,,,,