Auto Models และ Tokenizers

การใช้งาน Hugging Face

Jacob H. Marquez

Lead Data Engineer

Pipeline: รวดเร็วและใช้งานง่าย

from transformers import pipeline  

my_pipeline = pipeline(
    "text-classification",
    model="distilbert-base-uncased-finetuned-sst-2-english"))

print(my_pipeline("Wi-Fi is slower than a snail today!"))
[{'label': 'NEGATIVE', 'score': 0.99}]
การใช้งาน Hugging Face

Auto Classes: ยืดหยุ่นและทรงพลัง

$$

  • Auto classes: เข้าถึงโมเดลและ tokenizer ได้อย่างยืดหยุ่น
  • ควบคุมได้มากขึ้น ทั้งพฤติกรรมและผลลัพธ์ของโมเดล
  • เหมาะกับงานขั้นสูง

$$

  • Pipeline = รวดเร็ว; Auto classes = ยืดหยุ่น

แถบเลื่อนสามอันพร้อมปุ่มโยก และมือกำลังปรับอันหนึ่ง แสดงถึงการควบคุมที่มากขึ้น

การใช้งาน Hugging Face

AutoModels

  • เลือก AutoModel class เพื่อดาวน์โหลดโมเดลโดยตรง

$$

from transformers import AutoModelForSequenceClassification

# Download a pre-trained text classification model model = AutoModelForSequenceClassification.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english" )
การใช้งาน Hugging Face

AutoTokenizers

  • เตรียมข้อมูลข้อความสำหรับนำเข้าโมเดล
  • แนะนำให้ใช้ tokenizer ที่จับคู่กับโมเดล

$$

from transformers import AutoTokenizer


# Retrieve the tokenizer paired with the model tokenizer = AutoTokenizer.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english" )
การใช้งาน Hugging Face

การ tokenize ข้อความด้วย AutoTokenizer

  • Tokenizer ทำความสะอาดข้อมูลและแบ่งข้อความออกเป็น token

$$

tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

# Tokenize input text tokens = tokenizer.tokenize("AI: Helping robots think and humans overthink:)") print(tokens)
['ai', ':', 'helping', 'robots', 'think', 'and', 
 'humans', 'over', '##thi', '##nk', ':', ')']
การใช้งาน Hugging Face

โมเดลต่างกัน tokenizer ก็ต่างกัน

  • โมเดลของเรา (distilbert-base-uncased):

    ['ai', ':', 'helping', 'robots', 'think', 'and', 'humans', 'over', '##thi',
    '##nk', ':', ')']
    
  • BERT-Base-Cased Tokenizer:

    ['AI', ':', 'Help', '##ing', 'robots', 'think', 'and', 'humans', 'over',
    '##thin', '##k', ':', ')']
    
การใช้งาน Hugging Face

สร้าง Pipeline ด้วย Auto Classes

from transformers import AutoModelForSequenceClassification,
AutoTokenizer, pipeline

# Download the model and tokenizer my_model = AutoModelForSequenceClassification.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english") my_tokenizer = AutoTokenizer.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english")
# Create the custom pipeline my_pipeline = pipeline( task="sentiment-analysis", model=my_model, tokenizer=my_tokenizer)
การใช้งาน Hugging Face

กรณีการใช้งาน AutoModels และ AutoTokenizers

$$

  • 🔧 ใช้เมื่อต้องการควบคุมและปรับแต่งมากขึ้น

  • 📝 การเตรียมข้อความ: ทำความสะอาดและ tokenize สำหรับงานเฉพาะ

  • 🏆 การกำหนดเกณฑ์: จัดลำดับความสำคัญของหมวดหมู่ในงาน classification
  • 🚀 Workflow ซับซ้อน: ควบคุมการประมวลผลหลายขั้นตอนและการเชื่อมต่อ

$$ การควบคุมและปรับแต่งที่มากขึ้น

การใช้งาน Hugging Face

มาฝึกกันเถอะ!

การใช้งาน Hugging Face

Preparing Video For Download...