โมเดลที่ผ่านการเทรนมาแล้วสำหรับการสร้างข้อความ

Deep Learning สำหรับข้อความด้วย PyTorch

Shubham Jain

Instructor

ทำไมต้องใช้โมเดลที่ผ่านการเทรนมาแล้ว?

ข้อดี

  1. เทรนบนชุดข้อมูลขนาดใหญ่
  2. ประสิทธิภาพสูงในงานสร้างข้อความหลากหลายรูปแบบ
    • การวิเคราะห์ความรู้สึก
    • การเติมข้อความ
    • การแปลภาษา

ข้อจำกัด

  1. ต้นทุนการประมวลผลสูง
  2. ต้องการพื้นที่จัดเก็บข้อมูลมาก
  3. ปรับแต่งได้จำกัด
Deep Learning สำหรับข้อความด้วย PyTorch

โมเดลที่ผ่านการเทรนมาแล้วใน PyTorch

  • Hugging Face Transformers: ไลบรารีของโมเดลที่ผ่านการเทรนมาแล้ว
  • โมเดลที่ผ่านการเทรนมาแล้ว:
    • GPT-2
    • T5

โลโก้ HuggingFace

Deep Learning สำหรับข้อความด้วย PyTorch

ทำความเข้าใจ GPT-2 Tokenizer และโมเดล

GPT2LMHeadModel:

  • โมเดล GPT-2 จาก HuggingFace
  • ออกแบบมาสำหรับการสร้างข้อความโดยเฉพาะ

GPT2Tokenizer:

  • แปลงข้อความเป็น token
  • รองรับการแบ่ง subword: 'larger' อาจกลายเป็น ['large', 'r']
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ

import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel

tokenizer = GPT2Tokenizer.from_pretrained('gpt2') model = GPT2LMHeadModel.from_pretrained('gpt2')
seed_text = "Once upon a time"
input_ids = tokenizer.encode(seed_text, return_tensors='pt')
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ II

output = model.generate(

                                                           ) 
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ II

output = model.generate(input_ids, max_length=40, 

                                                           ) 
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ II

output = model.generate(input_ids, max_length=40, temperature=0.7,     

                                                           ) 
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ II

output = model.generate(input_ids, max_length=40, temperature=0.7,     
                        no_repeat_ngram_size=2, 
                                                           ) 
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: การนำไปใช้สร้างข้อความ II

output = model.generate(input_ids, max_length=40, temperature=0.7,     
                        no_repeat_ngram_size=2, 
                        pad_token_id=tokenizer.eos_token_id) 
Deep Learning สำหรับข้อความด้วย PyTorch

GPT-2: ผลลัพธ์การสร้างข้อความ

generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)
Generated Text: Once upon a time, the world was a place of great beauty
and great danger. The world of the gods was the place where the great gods were 
born, and where they were to live.
Deep Learning สำหรับข้อความด้วย PyTorch

T5: การนำไปใช้แปลภาษา

  • t5-small: Text-to-Text Transfer Transformer
  • โมเดลที่ผ่านการเทรนมาแล้วสำหรับงานแปลภาษา
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration

tokenizer = T5Tokenizer.from_pretrained("t5-small") model = T5ForConditionalGeneration.from_pretrained("t5-small")
input_prompt = "translate English to French: 'Hello, how are you?'"
input_ids = tokenizer.encode(input_prompt, return_tensors="pt")
output = model.generate(input_ids, max_length=100)
Deep Learning สำหรับข้อความด้วย PyTorch

T5: ผลลัพธ์การแปลภาษา

generated_text = tokenizer.decode(output[0], skip_special_tokens=True)

print("Generated text:",generated_text)
Generated text:
"Bonjour, comment êtes-vous?"
Deep Learning สำหรับข้อความด้วย PyTorch

การเลือกโมเดลที่ผ่านการเทรนมาแล้วให้เหมาะสม

  • มีให้เลือกใช้มากมาย!

 

  • GPT-2: การสร้างข้อความ
  • DistilGPT-2 (GPT-2 เวอร์ชันขนาดเล็ก): การสร้างข้อความ
  • BERT: การจัดประเภทข้อความ, การตอบคำถาม
  • T5 (t5-small คือเวอร์ชันขนาดเล็กของ T5): การแปลภาษา, การสรุปข้อความ

 

  • ค้นหาได้ใน HuggingFace และแหล่งข้อมูลอื่น ๆ
Deep Learning สำหรับข้อความด้วย PyTorch

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

Deep Learning สำหรับข้อความด้วย PyTorch

Preparing Video For Download...