๐ŸงฉTensorflow Certification ์ทจ๋“ํ•˜๊ธฐ - part 4. ์‹ค์ „ (Iris ๊ฝƒ ๋ถ„๋ฅ˜)

vincaยท2023๋…„ 1์›” 2์ผ
0

๐ŸŒ• AI/DL -Tenserflow Certification

๋ชฉ๋ก ๋ณด๊ธฐ
4/11

Iris ๊ฝƒ ๋ถ„๋ฅ˜

  • Fully Connected Layer (Dense)๋ฅผ ํ™œ์šฉํ•œ ๋ถ„๋ฅ˜ ๋ชจ๋ธ (Classification)
  • tensorflow-datasets ๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ

For this task you will train a classifier for Iris flowers using the Iris dataset
The final layer in your neural network should look like: tf.keras.layers.
Dense(3, activation=tf.nn.softmax)
The input layer will expect data in the shape (4,)
We've given you some starter code for preprocessing the data
You'll need to implement the preprocess function for data.map

์ด ์ž‘์—…์—์„œ๋Š” Iris ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Iris ๊ฝƒ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค.
์‹ ๊ฒฝ๋ง์˜ ๋งˆ์ง€๋ง‰ ๊ณ„์ธต์€ tf.keras.layers์™€ ๊ฐ™์•„์•ผํ•ฉ๋‹ˆ๋‹ค.
Dense(3, activation='softmax')
์ž…๋ ฅ ๋ ˆ์ด์–ด๋Š” ๋ชจ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค (4,)
๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ์œ„ํ•œ ์Šคํƒ€ํ„ฐ ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค
data.map์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Solution

์ˆœ์„œ ์š”์•ฝ

  1. import: ํ•„์š”ํ•œ ๋ชจ๋“ˆ import
  2. ์ „์ฒ˜๋ฆฌ: ํ•™์Šต์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ชจ๋ธ๋ง(model): ๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ปดํŒŒ์ผ(compile): ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  5. ํ•™์Šต (fit): ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค.

1. import ํ•˜๊ธฐ

ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ import ํ•ฉ๋‹ˆ๋‹ค.

import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds

from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint

2.1 ์ „์ฒ˜๋ฆฌ (Load dataset)

tensorflow-datasets๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

train_dataset์™€ valid_dataset์„ ๋งŒ๋“ค๊ณ  80% : 20%๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.

train_dataset = tfds.load('iris', split='train[:80%]')
valid_dataset = tfds.load('iris', split='train[80%:]')

2.2 ์ „์ฒ˜๋ฆฌ (tensorflow-datasets)

๋จผ์ €, ์‹œํ—˜์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ „์ฒ˜๋ฆฌ ์š”๊ตฌ ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. label ๊ฐ’์„ one-hot encoding ํ•  ๊ฒƒ
  2. feature (x), label (y)๋ฅผ ๋ถ„ํ• ํ•  ๊ฒƒ
def preprocess(data):
    # ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    x = data['features']
    y = data['label']
    y = tf.one_hot(y, 3)
    return x, y

๋งŒ๋“  ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜(preprocessing)๋ฅผ dataset์— mappingํ•˜๊ณ , batch_size๋„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

batch_size=10
train_data = train_dataset.map(preprocess).batch(batch_size)
valid_data = valid_dataset.map(preprocess).batch(batch_size)
### 3. **์ด๋ฏธ์ง€ ์ •๊ทœํ™” (Normalization)**

3. ๋ชจ๋ธ ์ •์˜ (Sequential)

์ด์ œ Modeling์„ ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.

Sequential ๋ชจ๋ธ ์•ˆ์—์„œ ์ธต์„ ๊นŠ๊ฒŒ ์Œ“์•„ ์˜ฌ๋ ค ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  1. input_shape๋Š” Iris ๊ฝƒ ๋ฐ์ดํ„ฐ์…‹์˜ X์˜ feature ๊ฐฏ์ˆ˜๊ฐ€ 4๊ฐœ ์ด๋ฏ€๋กœ (4, )๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. ๊นŠ์€ ์ถœ๋ ฅ์ธต๊ณผ ๋” ๋งŽ์€ Layer๋ฅผ ์Œ“์Šต๋‹ˆ๋‹ค.
  3. Dense Layer์— activation='relu'๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  4. ๋ถ„๋ฅ˜(Classification)์˜ ๋งˆ์ง€๋ง‰ ์ธต์˜ ์ถœ๋ ฅ ์ˆซ์ž๋Š” ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค ๊ฐฏ์ˆ˜์™€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
model = tf.keras.models.Sequential([
    # input_shape๋Š” X์˜ feature ๊ฐฏ์ˆ˜๊ฐ€ 4๊ฐœ ์ด๋ฏ€๋กœ (4, )๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    Dense(512, activation='relu', input_shape=(4,)),
    Dense(256, activation='relu'),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(32, activation='relu'),
    # Classification์„ ์œ„ํ•œ Softmax, ํด๋ž˜์Šค ๊ฐฏ์ˆ˜ = 3๊ฐœ
    Dense(3, activation='softmax'),
])
model.summary()

4. ์ปดํŒŒ์ผ (compile)

  1. optimizer๋Š” ๊ฐ€์žฅ ์ตœ์ ํ™”๊ฐ€ ์ž˜๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ 'adam'์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. loss์„ค์ •
  • ์ถœ๋ ฅ์ธต activation์ด sigmoid ์ธ ๊ฒฝ์šฐ: binary_crossentropy
  • ์ถœ๋ ฅ์ธต activation์ด softmax ์ธ ๊ฒฝ์šฐ:
    • ์›ํ•ซ์ธ์ฝ”๋”ฉ(O): categorical_crossentropy
    • ์›ํ•ซ์ธ์ฝ”๋”ฉ(X): sparse_categorical_crossentropy)
  1. metrics๋ฅผ 'acc' ํ˜น์€ 'accuracy'๋กœ ์ง€์ •ํ•˜๋ฉด, ํ•™์Šต์‹œ ์ •ํ™•๋„๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ one-hot encoding ์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, categorical_crossentropy๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

ModelCheckpoint: ์ฒดํฌํฌ์ธํŠธ ์ƒ์„ฑ

val_loss ๊ธฐ์ค€์œผ๋กœ epoch ๋งˆ๋‹ค ์ตœ์ ์˜ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, ModelCheckpoint๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

  • checkpoint_path๋Š” ๋ชจ๋ธ์ด ์ €์žฅ๋  ํŒŒ์ผ ๋ช…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ModelCheckpoint์„ ์„ ์–ธํ•˜๊ณ , ์ ์ ˆํ•œ ์˜ต์…˜ ๊ฐ’์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
checkpoint_path = "my_checkpoint.ckpt"
checkpoint = ModelCheckpoint(filepath=checkpoint_path, 
                             save_weights_only=True, 
                             save_best_only=True, 
                             monitor='val_loss', 
                             verbose=1)

5. ํ•™์Šต (fit)

  1. validation_data๋ฅผ ๋ฐ˜๋“œ์‹œ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. epochs์„ ์ ์ ˆํ•˜๊ฒŒ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  3. callbacks์— ๋ฐ”๋กœ ์œ„์—์„œ ๋งŒ๋“  checkpoint๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
history = model.fit(train_data,
                    validation_data=(valid_data),
                    epochs=20,
                    callbacks=[checkpoint],
                   )

ํ•™์Šต ์™„๋ฃŒ ํ›„ Load Weights (ModelCheckpoint)

ํ•™์Šต์ด ์™„๋ฃŒ๋œ ํ›„์—๋Š” ๋ฐ˜๋“œ์‹œ load_weights๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ์—ด์‹ฌํžˆ ModelCheckpoint๋ฅผ ๋งŒ๋“  ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

# checkpoint ๋ฅผ ์ €์žฅํ•œ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
model.load_weights(checkpoint_path)

์˜ค์ฐจ ๋ฐ ์ •ํ™•๋„ ์‹œ๊ฐํ™”

ํ•™์Šต Loss (์˜ค์ฐจ)์— ๋Œ€ํ•œ ์‹œ๊ฐํ™”

import matplotlib.pyplot as plt
plt.figure(figsize=(12, 9))
plt.plot(np.arange(1, 21), history.history['loss'])
plt.plot(np.arange(1, 21), history.history['val_loss'])
plt.title('Loss / Val Loss', fontsize=20)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['loss', 'val_loss'], fontsize=15)
plt.show()

ํ•™์Šต accuracy (์ •ํ™•๋„)์— ๋Œ€ํ•œ ์‹œ๊ฐํ™”

plt.figure(figsize=(12, 9))
plt.plot(np.arange(1, 21), history.history['acc'])
plt.plot(np.arange(1, 21), history.history['val_acc'])
plt.title('Acc / Val Acc', fontsize=20)
plt.xlabel('Epochs')
plt.ylabel('Acc')
plt.legend(['acc', 'val_acc'], fontsize=15)
plt.show()

๊ฒ€์ฆ์ด ํ•˜๊ณ ์‹ถ๋‹ค๋ฉด..

model.evaluate(x_valid, y_valid)
profile
๋ถ‰์€ ๋ฐฐ ์˜ค์ƒ‰ ๋”ฑ๋‹ค๊ตฌ๋ฆฌ ๊ฐœ๋ฐœ์ž ๐ŸฆƒCloud & DevOps

0๊ฐœ์˜ ๋Œ“๊ธ€