ใน ep นี้เราจะเรียนรู้การสร้าง Toxicity Classifier แบบ Multi-label Classification จำแนกคำพูด Toxic ซึ่งเป็นงานด้าน NLP โดยใช้โมเดลสำเร็จรูป สำหรับภาษา JavaScript ด้วย TensorFlow.js

Toxicity คืออะไร

Man's face screaming/shouting. Stubbly wearing glasses. Credit https://commons.wikimedia.org/wiki/File:Scream_crosathorian.jpg
Man’s face screaming/shouting. Stubbly wearing glasses. Credit https://commons.wikimedia.org/wiki/File:Scream_crosathorian.jpg

Toxicity หมายถึง ความ Toxic มุ่งร้าย คิดร้าย ดูถูก คุกคาม ขู่เข็ญ แดกดัน หยาบคาย สบประมาท อนาจาร คำพูดเป็นพิษ ในเคสนี้ จะแบ่งเป็น 7 หัวข้อ ดังนี้

  1. identity_attack
  2. insult
  3. obscene
  4. severe_toxicity
  5. sexual_explicit
  6. threat
  7. toxicity

โดย 1 ข้อความจะเป็นได้หลายหัวข้อ

TensorFlow.js Code Example

เริ่มต้นด้วยใส่ Code ด้านล่าง ไว้ระหว่าง HTML tag head และ body โค้ดนี้เป็นการ Load TensorFlow.js และ โหลดโมเดลที่ใช้ในการทำ Toxicity Classification

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

กำหนด threshold ของความน่าจะเป็นที่ต้องการ

const threshold = 0.9;

เราจะได้ toxicity ขึ้นมา สั่งให้โหลด model ด้วย threshold ที่เรากำหนดไว้ด้านบน แล้วใช้ model ในการ classify ข้อความที่ต้องการ

toxicity.load(threshold).then(model => {
    model.classify(sentences).then(predictions => {
        ...
    });
});

จะได้ predictions เป็น array ระบุความน่าจะเป็นของ Class ต่าง ๆ รูปแบบด้านล่าง โดยถ้าความน่าจะเป็นมีค่าเกิน threshold ที่ตั้งไว้ match จะเป็น true

    {
      "label": "identity_attack",
      "results": [{
        "probabilities": [0.9659664034843445, 0.03403361141681671],
        "match": false
      }]
    },
    {
      "label": "insult",
      "results": [{
        "probabilities": [0.08124706149101257, 0.9187529683113098],
        "match": true
      }]
    },
    ...

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

10f_toxicity.html

Inference

You are useless.

Result of "You are useless." Toxicity Multi-label Classifier.
Result of “You are useless.” Toxicity Multi-label Classifier.

จะเห็นว่าโมเดล classify ประโยค You are useless. ได้ insult = 97.6% และ toxicity = 98.0%

You are perfect.

Result of "You are perfect." Toxicity Multi-label Classifier.
Result of “You are perfect.” Toxicity Multi-label Classifier.

ส่วนประโยค You are perfect ไม่มีความ Toxicity ทั้ง 7 class เลย

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.