ในการสร้างโมเดล Machine Learning ที่สามารถ Predict ได้อย่างถูกต้อง แม่นยำ อีกเรื่องที่เราควรคำนึงถึงคือ สุดท้ายแล้วจุดหมายปลายทางนั้น โมเดลจะถูกนำไป Deploy ที่ไหน ไม่ว่าจะเป็นบน Web Browser, Server, มือถือ, Edge Device, IoT, etc.

ใน ep นี้ เราจะมาเรียนรู้ TensorFlow.js ซึ่งเป็น Machine Learning Framework สำหรับภาษา JavaScript จาก Google ที่สามารถรันได้ทั้งใน Web Browser และ บน Server ผ่าน Node.js

TensorFlow.js คืออะไร

Teach a model to classify images using files or your webcam. TensorFlow.js demos.  Credit https://teachablemachine.withgoogle.com/
Teach a model to classify images using files or your webcam. TensorFlow.js demos. Credit https://teachablemachine.withgoogle.com/

TensorFlow.js คือ Library สำหรับงาน Machine Learning ในภาษา JavaScript จาก Google / TensorFlow ใช้ในการสร้างโมเดลด้วย JavaScript และใช้ Machine Learning โดยตรงจากภายใน Web Browser หรือใน Node.js

TensorFlow.js คือ Library JavaScript ที่พัฒนาโดยชุมชน Open Source รองรับ Hardware-Accelerated (GPU) สำหรับเทรน และ Deploy โมเดล Machine Learning

ใช้ TensorFlow.js พัฒนา Machine Learning ใน Web Browser

เราสามารสร้างโมเดลโดยใช้ Linear Algegra จาก Low-level API หรือ หรือ ใช้ Layers จาก High-level API ก็ได้

ใช้ TensorFlow.js พัฒนา Machine Learning ใน Node.js

รัน Native TensorFlow ด้วย API เดียวกับ TenslowFlow.js ภายใต้ Node.js Runtime

ใช้ TensorFlow.js Run โมเดลที่มีอยู่แล้ว

เราสามารถใช้ TensorFlow.js model converters ในการแปลง โมเดล TensorFlow ที่เรามีอยู่แล้ว ให้รันบน Web Browser ได้

ใช้ TensorFlow.js Retrain โมเดลที่มีอยู่แล้ว

ไม่ใช่แค่รันเท่านั้น เราสามารถ Retrain โมเดลที่เรามีอยู่แล้ว Fine-tuned โมเดล ด้วยข้อมูลที่อยู่ปลายทาง จากใน Web Browser ได้เลย ไม่ต้องส่งกลับไปเทรนบน Server

TensorFlow.js Code Example

เริ่มต้นด้วยใส่ Code ด้านล่าง ไว้ระหว่าง HTML tag head และ body

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>

สร้างโมเดล Neural Network แบบ Sequential ความลึก 1 Dense Layer ขนาด 1 Neuron โดยไม่ต้องมี Activation Function

        const model = tf.sequential();
        model.add(tf.layers.dense({units: 1, inputShape: [1]}))

กำหนด Training Loop ในเคสนี้ เราจะเทรน Linear Regression เพื่อประมาณสมการเส้นตรง y = 5x + 0 กับ + Noise อีกนิดหน่อย โดยใน ทุก Epoch จะให้ Print เลขที่ Epoch และค่า Loss ใน Console

        async function doTraining(model) {
            const history = await model.fit(xs, ys, {epochs: 500, callbacks:{
                onEpochEnd: async(epoch, logs) => {
                    console.log("Epochs:" + epoch + " Loss:" + logs.loss);
                    }
            }});
        }

ใช้ Mean Squared Error Loss Function และ Optimize ด้วย SGD Optimizer

        model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

ข้อมูล Training Set

        const xs = tf.tensor2d([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], [6, 1]);
        const ys = tf.tensor2d([-5.0, 0.0, 5.1, 10.2, 14.7, 19.9], [6, 1]);

เรียกฟังก์ชัน เทรนโมเดล เมื่อเทรนจบ ให้ใช้โมเดลที่เทรนเสร็จแล้ว ในการ Predict ค่า y ถ้า x=10

        doTraining(model).then(() => {
            alert(model.predict(tf.tensor2d([10], [1, 1])))
        })

เรามาเริ่มกันเลยดีกว่า

Training

เริ่มต้นเทรน เมื่อเปิดหน้าเว็บ ให้เรากด F12 เปิด Console ของ Web Browser ขึ้นมาจะเห็น ดังนี้

  • Model Architecture สังเกตว่า มีแค่ 1 Neuron มี 2 Param คือ Weight และ Bias
  • log จะมี เลขที่ Epoch 0-299 และ ค่า Loss ที่ลดลงเรื่อย ๆ
_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense1 (Dense)         [null,1]                  2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________

Epochs:0 Loss:236.11563110351562
Epochs:1 Loss:186.18048095703125
Epochs:2 Loss:146.88548278808594
Epochs:3 Loss:115.96178436279297
Epochs:4 Loss:91.6244125366211
Epochs:5 Loss:72.468994140625
Epochs:6 Loss:57.39065170288086
Epochs:7 Loss:45.520118713378906
Epochs:8 Loss:36.17348861694336
Epochs:9 Loss:28.812698364257812
Epochs:10 Loss:23.014423370361328
Epochs:20 Loss:3.362860679626465
Epochs:30 Loss:1.3424725532531738
Epochs:40 Loss:0.9680830240249634
Epochs:50 Loss:0.7790122032165527
...
Epochs:291 Loss:0.025788750499486923
Epochs:292 Loss:0.025684181600809097
Epochs:293 Loss:0.025581758469343185
Epochs:294 Loss:0.025481462478637695
Epochs:295 Loss:0.025383224710822105
Epochs:296 Loss:0.02528700977563858
Epochs:297 Loss:0.02519271895289421
Epochs:298 Loss:0.025100406259298325
Epochs:299 Loss:0.02501003071665764

Inference

เมื่อสิ้นสุด ระบบจะ Alert ค่า y ที่โมเดล Predict จาก x=10

Alert Result of TensorFlow.js Model Inference x=10
Alert Result of TensorFlow.js Model Inference x=10

Credit

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.