๐ง ๋ฅ๋ฌ๋ ๊ฐ์
๋ฅ๋ฌ๋์ ์ธ๊ฐ์ ๋๋ฅผ ๋ชจ๋ฐฉํ ์ธ๊ณต์ ๊ฒฝ๋ง(ANN)์ ๊ธฐ๋ฐ์ผ๋ก ํ ๊ธฐ๊ณํ์ต ๊ธฐ์ ๋ก, ๋ค์ธต ๊ตฌ์กฐ์ ์ ๊ฒฝ๋ง์ ํตํด ๋ณต์กํ ํจํด์ ํ์ตํ๊ณ ์์ธกํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
๐ ์ธ๊ณต์ ๊ฒฝ๋ง(ANN)์ ๊ฐ๋
- ์๋ฌผํ์ ๋ด๋ฐ ๊ตฌ์กฐ์์ ์ฐฉ์.
- ์ ๋ ฅ โ ๊ฐ์ค์น โ ํ์ฑํ ํจ์ โ ์ถ๋ ฅ ํ๋ฆ์ผ๋ก ๋์.
- ๊ฐ ์ ํธ์ ๊ฐ๋๋ ๊ฐ์ค์น(Weight)๋ก ํํ๋จ.
๐งฌ ๋ฅ๋ฌ๋(Deep Learning)์ด๋?
- ์๋์ธต์ด ์ฌ๋ฌ ๊ฐ์ธ ์ฌ์ธต ์ ๊ฒฝ๋ง(Deep Neural Network, DNN)์ ํตํด ํ์ตํ๋ ๋ฐฉ์.
- ์ฌ์ธต ํ์ต(Deep Learning)์ด๋ผ๊ณ ๋ ํจ.
๐ ๏ธ ์ ๊ฒฝ๋ง ๊ตฌ์ฑ ์์
๊ตฌ์ฑ ์์ | ์ค๋ช |
---|---|
์ ๋ ฅ์ธต | ํ์ต ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ค๋ ์ธต |
์๋์ธต | ๊ฐ์คํฉ ๊ณ์ฐ ๋ฐ ๋น์ ํ ๋ณํ ์ํ |
์ถ๋ ฅ์ธต | ์ต์ข ์์ธก๊ฐ์ ์ถ๋ ฅ |
๊ฐ์ค์น | ์ ๋ ฅ์ ์ค์๋๋ฅผ ๊ฒฐ์ |
ํธํฅ | ๊ฐ์คํฉ์ ๋ํด์ง๋ ์์๋ก ์ถ๋ ฅ ์กฐ์ |
โ ๊ฐ์คํฉ (Weighted Sum)
- ๊ฐ ์ ๋ ฅ๊ฐ ร ๊ฐ์ค์น + ํธํฅ
- ์์: z = wโxโ + wโxโ + โฆ + b
โ๏ธ ํ์ฑํ ํจ์ (Activation Function)
ํจ์๋ช | ํน์ง |
---|---|
Sigmoid | S์ ํํ, ์ถ๋ ฅ๊ฐ [0, 1], ๊ธฐ์ธ๊ธฐ ์์ค ๋ฌธ์ |
Tanh | ์ถ๋ ฅ [-1, 1], ํ๊ท 0, sigmoid๋ณด๋ค ์ฐ์ |
ReLU | 0 ์ดํ โ 0, 0 ์ด๊ณผ โ ๊ทธ๋๋ก ์ถ๋ ฅ, ๋น ๋ฅธ ํ์ต |
LeakyReLU | ReLU์ ์์ ์ ๋ ฅ ๋ฌด๋ฐ์ ๋ฌธ์ ํด๊ฒฐ |
Softmax | ํ๋ฅ ๋ถํฌ ์ถ๋ ฅ, ๋ค์ค ํด๋์ค ๋ถ๋ฅ์ ์ฌ์ฉ |
๐งญ ํ์ต ๊ณผ์ (Training Flow)
1๏ธโฃ ์์ ํ (Forward Propagation)
- ์ ๋ ฅ โ ์๋์ธต โ ์ถ๋ ฅ์ธต์ผ๋ก ์์ธก๊ฐ ๋์ถ
2๏ธโฃ ์์ค ํจ์ (Loss Function)
- ์์ธก๊ฐ๊ณผ ์ค์ ๊ฐ์ ์ฐจ์ด๋ฅผ ๊ณ์ฐ
- ํ๊ท: MSE
- ๋ถ๋ฅ: Cross Entropy
3๏ธโฃ ์ตํฐ๋ง์ด์ (Optimizer)
- ๊ฒฝ์ฌํ๊ฐ๋ฒ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ค์น ์ต์ ํ
- ์ ์ฒด/๋ฏธ๋ ๋ฐฐ์น ๋ฐฉ์ ์ฌ์ฉ
4๏ธโฃ ์ญ์ ํ (Backpropagation)
- ์ค์ฐจ๋ฅผ ์ญ๋ฐฉํฅ ์ ํํด ๊ฐ์ค์น ์ ๋ฐ์ดํธ
- ๊ฐ ์ธต์ ๊ฐ์ค์น์ ๋ํด ๋ฏธ๋ถ๊ฐ ๊ธฐ๋ฐ ๋ณด์
๐งฑ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ์ ํ
์ ํ | ์ค๋ช |
---|---|
DFN (์๋ฐฉํฅ ์ ๊ฒฝ๋ง) | ๊ธฐ๋ณธ ๊ตฌ์กฐ, ๊ณ ์ ์ ๋ ฅ ์ฒ๋ฆฌ |
RNN (์ํ ์ ๊ฒฝ๋ง) | ์๊ณ์ด ๋ฐ์ดํฐ ์ฒ๋ฆฌ, ๊ณผ๊ฑฐ ์ ๋ณด ๋ฐ์ |
LSTM | RNN ๊ฐ์ , ์ฅ๊ธฐ ๊ธฐ์ต ์ ์ง |
CNN | ์ด๋ฏธ์ง ๋ถ์ ํนํ, ํฉ์ฑ๊ณฑ ๋ฐ ํ๋ง ํ์ฉ |
๐ง CNN์ ๊ตฌ์กฐ
- ํฉ์ฑ๊ณฑ์ธต: ํํฐ๋ฅผ ํตํด ํน์ง ์ถ์ถ
- ํ๋ง์ธต: ๋ฐ์ดํฐ ํฌ๊ธฐ ์ถ์, ํต์ฌ ์ ๋ณด ๋ณด์กด
- ์์ ์ฐ๊ฒฐ์ธต: ์ต์ข ๋ถ๋ฅ ์ํ
๐ ๋น๊ต ์์ฝ (DFN vs RNN vs CNN)
ํญ๋ชฉ | DFN | RNN | CNN |
---|---|---|---|
์ ๋ ฅ | ์ ์ | ์๊ณ์ด | ์ด๋ฏธ์ง/์๊ณ์ด |
ํน์ง | ๋จ๋ฐฉํฅ | ์ํ ์ฐ๊ฒฐ | ์ง์ญ์ ํน์ง |
ํ์ต | ์ฌ์ | ์ด๋ ค์ | ์ค๊ฐ |
ํจ์จ | ๋ฎ์ | ๋ฎ์ | ๋์ |
๐ฌ ์๋ ์๋ฒ ๋ฉ (Word Embedding)
๋ฐฉ์ | ์ค๋ช |
---|---|
One-hot Encoding | ํฌ์ ๋ฒกํฐ, ๋จ์ ๊ตฌ์กฐ |
Word2Vec | ์ฃผ๋ณ ๋ฌธ๋งฅ โ ์ค์ฌ ๋จ์ด ์์ธก (CBOW/Skip-gram) |
TF-IDF | ๋จ์ด ์ค์๋ ๊ฐ์ค์น ๋ถ์ฌ |
FastText | ๋ถ๋ถ ๋จ์ด ๊ธฐ๋ฐ, OOV ๋ฌธ์ ํด๊ฒฐ |
GloVe | ๋จ์ด ๋์ ๋ฑ์ฅ ํต๊ณ ๊ธฐ๋ฐ |
ELMo | ๋ฌธ๋งฅ์ ๋ฐ๋ผ ๋ฒกํฐ๊ฐ ๋ฌ๋ผ์ง๋ ๋์ ์๋ฒ ๋ฉ |
๐จ ์ ๋์ ์์ฑ ์ ๊ฒฝ๋ง (GAN)
- ๋ ๋คํธ์ํฌ๊ฐ ๊ฒฝ์:
- Generator: ์ง์ง ๊ฐ์ ๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ
- Discriminator: ์ง์ง์ ๊ฐ์ง ๊ตฌ๋ณ
- ์์ , ์ด๋ฏธ์ง ์์ฑ ๋ฑ์์ ๊ฐ๋ ฅํ ์ฑ๋ฅ
โ ์์ฝ
- ๋ฅ๋ฌ๋์ ์ธ๊ณต์ ๊ฒฝ๋ง์ ํ์ฅํ ๊ตฌ์กฐ๋ก, ๋ค์ํ ๋ฌธ์ ํด๊ฒฐ์ ์ ์ฉ ๊ฐ๋ฅ
- ํ์ฑํ ํจ์, ํ์ต ์๊ณ ๋ฆฌ์ฆ, ๋ชจ๋ธ ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ฑ๋ฅ์ด ์ข์ฐ๋จ
- CNN, RNN, GAN, Word Embedding ๋ฑ์ ์ค์ ๋ฌธ์ ์ ๋ง๋ ๋ฅ๋ฌ๋ ๊ธฐ๋ฒ ์ ํ์ ๊ธฐ์ค์ด ๋๋ค.
๐ ๏ธ ์์ ํ ๋๋ ํ ๋ฆฌ ์์ฑ ๋ฐ ํ๊ฒฝ ์ค์
# 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))