π Deep Learningλ?
νΌμ νΈλ‘ (Perceptron)μ μλ¬Όνμ λ΄λ°μ μνμ μΌλ‘ λͺ¨λΈλ§ν μΈκ³΅ λ΄λ° λͺ¨λΈλ‘,β¦
π§ ꡬ쑰 (Perceptron Structure)
μ
λ ₯(x) β κ°μ€μΉ(w) β κ°μ€ν©(β) β νμ±ν ν¨μ(f) β μΆλ ₯(y)
π οΈ μμ ν λλ ν 리 μμ± λ° νκ²½ μ€μ
# 1. μμ
λλ ν 리 μμ±
mkdir F_MNIST # λλ ν 리 μ΄λ¦: F_MNIST
cd F_MNIST # ν΄λΉ λλ ν λ¦¬λ‘ μ΄λ
# 2. κ°μ νκ²½ μμ± λ° νμ±ν
python3 -m venv .fmnist # κ°μ νκ²½ μμ± (ν΄λ μ΄λ¦: .fmnist)
source .fmnist/bin/activate # κ°μ νκ²½ νμ±ν
# 3. ν¨ν€μ§ μ€μΉ
pip install -U pip # pip μ΅μ λ²μ μΌλ‘ μ
κ·Έλ μ΄λ
pip install tensorflow # TensorFlow (λ₯λ¬λ νλ μμν¬)
pip install matplotlib # Matplotlib (μκ°ν λΌμ΄λΈλ¬λ¦¬)
pip install PyQt5 # PyQt5 (Matplotlib GUI λ°±μλμ©)
pip install scikit_learn # scikit-learn (λ¨Έμ λ¬λ λ° νκ° λꡬ)
# 4. Qt GUI λ°±μλ μ€μ (Wayland νκ²½μμ νμ)
export QT_QPA_PLATFORM=wayland # Qt GUIλ₯Ό Waylandμμ μ μ λμνκ² μ€μ
π¨βπ» μ€μ΅
π‘ Code : Fashion MNIST
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# dataset load
fashion_mnist = keras.datasets.fashion_mnist
# spilt data (train / test)
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
matplotlib.use('Qt5Agg')
NUM=20
plt.figure(figsize=(15,15))
plt.subplots_adjust(hspace=1)
for idx in range(NUM):
sp = plt.subplot(5,5,idx+1)
plt.imshow(train_images[idx])
plt.title(f'{class_names[train_labels[idx]]}')
plt.show()
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
# κ°λ¨ν μ΄λ―Έμ§ μ μ²λ¦¬ (for ANN)
train_images = train_images / 255.0
test_images = test_images / 255.0
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure(figsize=(10,8))
for i in range(20):
plt.subplot(4,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
model = keras.Sequential ([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax'),
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=20)
predictions = model.predict(test_images)
predictions[0]
np.argmax(predictions[0])
test_labels[0]
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
predictions_array, true_label = predictions_array[i], true_label[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions, test_labels)
plt.show()
from sklearn.metrics import accuracy_score
print('accuracy score : ', accuracy_score(tf.math.argmax(predictions, -1), test_labels))
aaaaaa