Mô hình tiền huấn luyện cho sinh văn bản

Deep Learning cho Văn bản với PyTorch

Shubham Jain

Instructor

Vì sao dùng mô hình tiền huấn luyện?

Lợi ích

  1. Huấn luyện trên bộ dữ liệu rất lớn
  2. Hiệu suất cao cho nhiều tác vụ sinh văn bản
    • Phân tích cảm xúc
    • Hoàn thành văn bản
    • Dịch ngôn ngữ

Hạn chế

  1. Chi phí tính toán huấn luyện cao
  2. Yêu cầu lưu trữ lớn
  3. Tùy biến hạn chế
Deep Learning cho Văn bản với PyTorch

Mô hình tiền huấn luyện trong PyTorch

  • Hugging Face Transformers: thư viện mô hình tiền huấn luyện
  • Mô hình tiền huấn luyện:
    • GPT-2
    • T5

Logo HuggingFace

Deep Learning cho Văn bản với PyTorch

Hiểu Tokenizer và Model của GPT-2

GPT2LMHeadModel:

  • Phiên bản GPT-2 của HuggingFace
  • Tối ưu cho sinh văn bản

GPT2Tokenizer:

  • Chuyển văn bản thành token
  • Xử lý tách từ con: 'larger' có thể thành ['large', 'r']
Deep Learning cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản

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 cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản II

output = model.generate(

                                                           ) 
Deep Learning cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản II

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

                                                           ) 
Deep Learning cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản II

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

                                                           ) 
Deep Learning cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản II

output = model.generate(input_ids, max_length=40, temperature=0.7,     
                        no_repeat_ngram_size=2, 
                                                           ) 
Deep Learning cho Văn bản với PyTorch

GPT-2: triển khai sinh văn bản 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 cho Văn bản với PyTorch

GPT-2: đầu ra sinh văn bản

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 cho Văn bản với PyTorch

T5: triển khai dịch ngôn ngữ

  • t5-small: Text-to-Text Transfer Transformer
  • Mô hình tiền huấn luyện cho dịch ngôn ngữ
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 cho Văn bản với PyTorch

T5: đầu ra dịch ngôn ngữ

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

print("Generated text:",generated_text)
Generated text:
"Bonjour, comment êtes-vous?"
Deep Learning cho Văn bản với PyTorch

Chọn mô hình tiền huấn luyện phù hợp

  • Có rất nhiều!

 

  • GPT-2: Sinh văn bản
  • DistilGPT-2 (phiên bản nhỏ hơn của GPT-2): Sinh văn bản
  • BERT: Phân loại văn bản, hỏi–đáp
  • T5 (t5-small là bản nhỏ hơn của T5): Dịch, tóm tắt

 

  • Tìm trên HuggingFace và các kho khác
Deep Learning cho Văn bản với PyTorch

Ayo berlatih!

Deep Learning cho Văn bản với PyTorch

Preparing Video For Download...