การใช้งาน AutoClasses
เราได้เรียนรู้วิธีการทำงานของ tokenizer และบทบาทในการเตรียมข้อความสำหรับโมเดลไปแล้ว คราวนี้มาต่อยอดด้วยการนำ AutoModels และ AutoTokenizers มาใช้ร่วมกับฟังก์ชัน pipeline() ซึ่งเป็นการผสมผสานระหว่างความยืดหยุ่นและความสะดวกได้อย่างลงตัว
ให้ทำงานวิเคราะห์ความรู้สึกต่อจากเดิม โดยรวม AutoClasses เข้ากับโมดูล pipeline
AutoModelForSequenceClassification, AutoTokenizer และ pipeline จากไลบรารี transformers ได้ถูก import ไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การใช้งาน Hugging Face
คำแนะนำการฝึกหัด
- ดาวน์โหลดโมเดลและ tokenizer แล้วบันทึกเป็น
my_modelและmy_tokenizerตามลำดับ - สร้าง pipeline แล้วบันทึกเป็น
my_pipeline - ทำนายผลลัพธ์โดยใช้
my_pipelineแล้วบันทึกเป็นoutput
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Download the model and tokenizer
my_model = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
my_tokenizer = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
# Create the pipeline
my_pipeline = pipeline(task="sentiment-analysis", ____=____, ____=____)
# Predict the sentiment
output = ____("This course is pretty good, I guess.")
print(f"Sentiment using AutoClasses: {output[0]['label']}")