ในบทความนี้เราจะได้เรียนรู้เกี่ยวกับการสร้างและเทรนโมเดล Machine Learning สำหรับงาน Classification โดยใช้ TensorFlow และ Keras ใน Python โดยเราจะใช้ชุดข้อมูล Iris dataset ที่เป็นที่นิยมและเป็นที่รู้จักในวงการ Machine Learning มาเป็นตัวอย่าง
ชุดข้อมูล Iris dataset ประกอบไปด้วย:
150 ตัวอย่างของ Iris
มีทั้งหมด 3 ประเภทของ Iris โดยแต่ละประเภทมี 50 ตัวอย่าง
แต่ละตัวอย่างประกอบด้วยค่าความยาวและความกว้างของกลีบเลี้ยง (sepal) และกลีบดอก (petal)
เราจะใช้ข้อมูลนี้เพื่อสร้างและเทรนโมเดลที่สามารถจำแนกประเภทของ Iris ได้อย่างแม่นยำ
การสร้างและเทรนโมเดล Classification ด้วย TensorFlow/Keras
ขั้นตอนที่ 1 : เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็นในการทำงาน:
import tensorflow as tf
from tensorflow import keras
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
ขั้นตอนที่ 2 : หลังจากนั้น เราจะโหลดและเตรียมข้อมูล Iris dataset สำหรับการใช้งาน:
# โหลดข้อมูล Iris dataset
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
# แบ่งข้อมูลเป็นชุด train และ test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# มาตราส่วนข้อมูล
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
ขั้นตอนที่ 3 : จากนั้น เราจะสร้างโมเดล Neural Network สำหรับ Classification ด้วย TensorFlow/Keras:
model = keras.Sequential([
keras.layers.Dense(10, input_shape=(4,), activation='relu'),
keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
โดยในโมเดลของเรามี:
- Layer แรก: Dense layer ที่มี 10 units, ใช้ activation function เป็น 'relu'
- Layer ที่สอง: Dense layer สุดท้ายที่มี 3 units (ตรงกับจำนวนประเภทของ Iris), ใช้ activation function เป็น 'softmax' เพื่อให้ได้ค่าความน่าจะเป็นของแต่ละประเภท
ขั้นตอนที่ 4 : เราจะทำการเทรนโมเดลของเราด้วยข้อมูลที่มีประกอบด้วย X_train_scaled และ y_train:
model.fit(X_train_scaled, y_train, epochs=50, batch_size=2, verbose=1)
X_train_scaled : เป็นชุดข้อมูลฟีเจอร์ (features) ของข้อมูลที่ใช้ในการเทรนโมเดล ในกรณีของ Iris dataset คือข้อมูลที่ถูกมาตราส่วนและเตรียมไว้แล้วใน X_train_scaled
y_train : เป็นชุดข้อมูลเป้าหมาย (target) หรือคำตอบที่ต้องการให้โมเดลทำนาย
epochs=50 : จำนวนรอบการเทรนทั้งหมดที่โมเดลจะถูกฝึกด้วยชุดข้อมูลใน
X_train_scaled ซึ่งกล่าวคือ โมเดลจะมองข้อมูลทั้งหมดใน X_train_scaled 50 ครั้ง
batch_size=2 : กำหนดจำนวนตัวอย่างที่ใช้ในการอัปเดตค่า weight ในโมเดลในแต่ละรอบการเทรน ในที่นี้เราใช้ขนาด batch เท่ากับ 2 หมายถึงการอัปเดตค่า weight ทุก 2 ตัวอย่าง
verbose=1 : กำหนดว่าในการเทรนโมเดลจะแสดงข้อมูลเพิ่มเติมหรือไม่ ในกรณีที่เป็น 1 จะแสดงผลลัพท์ในแต่ละรอบการเทรน เช่น loss และ accuracy ถ้าเป็น 0 จะไม่แสดงผลลัพท์ในแต่ละรอบ ในกรณีที่เป็น 2 จะแสดงผลสรุปหลังจากการเทรนเสร็จสิ้นเท่านั้น
ผลลัพท์ที่ได้จาก Code :
Epoch 1/50
60/60 [==============================] - 2s 2ms/step - loss: 1.0986 - accuracy: 0.2833
Epoch 2/50
60/60 [==============================] - 0s 2ms/step - loss: 0.9626 - accuracy: 0.5083
Epoch 3/50
60/60 [==============================] - 0s 4ms/step - loss: 0.8513 - accuracy: 0.7833
Epoch 4/50
60/60 [==============================] - 0s 5ms/step - loss: 0.7422 - accuracy: 0.8250
Epoch 5/50
60/60 [==============================] - 0s 4ms/step - loss: 0.6388 - accuracy: 0.8500
Epoch 6/50
60/60 [==============================] - 0s 4ms/step - loss: 0.5465 - accuracy: 0.8750
Epoch 7/50
60/60 [==============================] - 0s 4ms/step - loss: 0.4753 - accuracy: 0.8417
Epoch 8/50
60/60 [==============================] - 0s 6ms/step - loss: 0.4187 - accuracy: 0.8750
Epoch 9/50
60/60 [==============================] - 0s 5ms/step - loss: 0.3823 - accuracy: 0.8917
Epoch 10/50
60/60 [==============================] - 0s 4ms/step - loss: 0.3506 - accuracy: 0.8750
Epoch 11/50
60/60 [==============================] - 0s 5ms/step - loss: 0.3288 - accuracy: 0.8917
Epoch 12/50
60/60 [==============================] - 0s 3ms/step - loss: 0.3090 - accuracy: 0.9083
Epoch 13/50
60/60 [==============================] - 0s 4ms/step - loss: 0.2928 - accuracy: 0.9083
Epoch 14/50
60/60 [==============================] - 0s 6ms/step - loss: 0.2793 - accuracy: 0.9083
Epoch 15/50
60/60 [==============================] - 0s 4ms/step - loss: 0.2684 - accuracy: 0.9333
Epoch 16/50
60/60 [==============================] - 0s 5ms/step - loss: 0.2568 - accuracy: 0.9333
Epoch 17/50
60/60 [==============================] - 0s 4ms/step - loss: 0.2478 - accuracy: 0.9250
Epoch 18/50
60/60 [==============================] - 0s 6ms/step - loss: 0.2391 - accuracy: 0.9333
Epoch 19/50
60/60 [==============================] - 0s 6ms/step - loss: 0.2304 - accuracy: 0.9417
Epoch 20/50
60/60 [==============================] - 0s 7ms/step - loss: 0.2232 - accuracy: 0.9333
Epoch 21/50
60/60 [==============================] - 0s 7ms/step - loss: 0.2159 - accuracy: 0.9417
Epoch 22/50
60/60 [==============================] - 0s 6ms/step - loss: 0.2083 - accuracy: 0.9500
Epoch 23/50
60/60 [==============================] - 0s 7ms/step - loss: 0.2023 - accuracy: 0.9417
Epoch 24/50
60/60 [==============================] - 0s 7ms/step - loss: 0.1956 - accuracy: 0.9500
Epoch 25/50
60/60 [==============================] - 0s 4ms/step - loss: 0.1906 - accuracy: 0.9500
Epoch 26/50
60/60 [==============================] - 0s 3ms/step - loss: 0.1836 - accuracy: 0.9500
Epoch 27/50
60/60 [==============================] - 0s 4ms/step - loss: 0.1784 - accuracy: 0.9500
Epoch 28/50
60/60 [==============================] - 0s 4ms/step - loss: 0.1729 - accuracy: 0.9583
Epoch 29/50
60/60 [==============================] - 0s 4ms/step - loss: 0.1676 - accuracy: 0.9667
Epoch 30/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1630 - accuracy: 0.9583
Epoch 31/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1586 - accuracy: 0.9500
Epoch 32/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1543 - accuracy: 0.9667
Epoch 33/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1502 - accuracy: 0.9667
Epoch 34/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1459 - accuracy: 0.9667
Epoch 35/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1426 - accuracy: 0.9583
Epoch 36/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1398 - accuracy: 0.9750
Epoch 37/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1359 - accuracy: 0.9583
Epoch 38/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1333 - accuracy: 0.9750
Epoch 39/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1301 - accuracy: 0.9667
Epoch 40/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1266 - accuracy: 0.9750
Epoch 41/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1254 - accuracy: 0.9667
Epoch 42/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1222 - accuracy: 0.9750
Epoch 43/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1189 - accuracy: 0.9750
Epoch 44/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1167 - accuracy: 0.9583
Epoch 45/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1132 - accuracy: 0.9750
Epoch 46/50
60/60 [==============================] - 0s 3ms/step - loss: 0.1109 - accuracy: 0.9750
Epoch 47/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1100 - accuracy: 0.9667
Epoch 48/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1071 - accuracy: 0.9750
Epoch 49/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1055 - accuracy: 0.9583
Epoch 50/50
60/60 [==============================] - 0s 2ms/step - loss: 0.1054 - accuracy: 0.9667
ขั้นตอนที่ 5 : เมื่อเทรนเสร็จสิ้น เราจะทำการประเมินโมเดลบนชุดข้อมูลทดสอบ:
test_loss, test_acc = model.evaluate(X_test_scaled, y_test, verbose=2)
print('\nTest accuracy:', test_acc)
ผลลัพท์ที่ได้จาก Code :
1/1 - 0s - loss: 0.0794 - accuracy: 1.0000 - 341ms/epoch - 341ms/step
Test accuracy: 1.0
loss: 0.0794: ค่า loss หรือค่าความสูญเสียที่ได้จากการทดสอบโมเดลบนชุดข้อมูลทดสอบ (Test dataset) มีค่าอยู่ที่ 0.0794 ซึ่งหมายถึงโมเดลทำนายข้อมูลในชุดทดสอบได้ดีและความคลาดเคลื่อนของการทำนายน้อย
accuracy: 1.0000: ค่า accuracy หรือความแม่นยำของโมเดลบนชุดข้อมูลทดสอบ มีค่าเท่ากับ 1.0 หรือ 100% ซึ่งหมายถึงโมเดลทำนายข้อมูลในชุดทดสอบทุกตัวอย่างได้อย่างถูกต้อง โมเดลไม่มีการทำนายผิดพลาดใด ๆ เลย
ผลลัพท์ที่ได้จะแสดงค่า loss และ accuracy บนชุดข้อมูลทดสอบ เราจะใช้ค่า accuracy เพื่อประเมินประสิทธิภาพของโมเดลในการจำแนกประเภทข้อมูลของ Iris dataset
หลังจากที่ทำการประเมินโมเดลเสร็จสิ้น เราจะได้ผลลัพท์ออกมาเป็นค่า accuracy บนชุดข้อมูลทดสอบ ซึ่งมีค่าอยู่ในช่วงระหว่าง 0 ถึง 1 โดยค่าที่ใกล้เคียงกับ 1 แสดงถึงความแม่นยำของโมเดลที่ดี
นั่นคือขั้นตอนการสร้างและเทรนโมเดล Classification ด้วย TensorFlow/Keras สำหรับงาน Classification โดยใช้ Iris dataset ใน Python ของเรา
สรุป
ในบทความนี้เราได้เรียนรู้เกี่ยวกับขั้นตอนการสร้างและเทรนโมเดล Machine Learning สำหรับงาน Classification ด้วย TensorFlow/Keras ใน Python โดยใช้ชุดข้อมูลที่นิยมใช้ในการศึกษา Iris dataset โมเดลที่ได้มีความแม่นยำสูงในการจำแนกประเภทของ Iris ทำให้เราสามารถนำไปใช้ในงานจริงได้อย่างมีประสิทธิภาพ
Reference:
TensorFlow: https://www.tensorflow.org/
Keras: https://keras.io/
Iris dataset: https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html
Top comments (0)