Předtrénované modely pro generování textu

Deep Learning for Text with PyTorch

Shubham Jain

Instructor

Proč předtrénované modely?

Výhody

  1. Trénováno na rozsáhlých datasetech
  2. Vysoký výkon při různých úlohách generování textu
    • Analýza sentimentu
    • Doplňování textu
    • Překlad jazyka

Omezení

  1. Vysoké výpočetní nároky na trénování
  2. Velké nároky na úložiště
  3. Omezené možnosti přizpůsobení
Deep Learning for Text with PyTorch

Předtrénované modely v PyTorch

  • Hugging Face Transformers: knihovna předtrénovaných modelů
  • Předtrénované modely:
    • GPT-2
    • T5

Logo HuggingFace

Deep Learning for Text with PyTorch

GPT-2: tokenizer a model

GPT2LMHeadModel:

  • Implementace GPT-2 v HuggingFace
  • Určeno pro generování textu

GPT2Tokenizer:

  • Převádí text na tokeny
  • Zpracovává tokenizaci podslova: 'larger' může být ['large', 'r']
Deep Learning for Text with PyTorch

GPT-2: implementace generování textu

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 for Text with PyTorch

GPT-2: implementace generování textu II

output = model.generate(

                                                           ) 
Deep Learning for Text with PyTorch

GPT-2: implementace generování textu II

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

                                                           ) 
Deep Learning for Text with PyTorch

GPT-2: implementace generování textu II

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

                                                           ) 
Deep Learning for Text with PyTorch

GPT-2: implementace generování textu II

output = model.generate(input_ids, max_length=40, temperature=0.7,     
                        no_repeat_ngram_size=2, 
                                                           ) 
Deep Learning for Text with PyTorch

GPT-2: implementace generování textu 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 for Text with PyTorch

GPT-2: výstup generování textu

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 for Text with PyTorch

T5: implementace překladu jazyka

  • t5-small: Text-to-Text Transfer Transformer
  • Předtrénovaný model pro překlad jazyka
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 for Text with PyTorch

T5: výstup překladu jazyka

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

print("Generated text:",generated_text)
Generated text:
"Bonjour, comment êtes-vous?"
Deep Learning for Text with PyTorch

Výběr správného předtrénovaného modelu

  • Existuje jich mnoho!

 

  • GPT-2: Generování textu
  • DistilGPT-2 (menší verze GPT-2): Generování textu
  • BERT: Klasifikace textu, odpovídání na otázky
  • T5 (t5-small je menší verze T5): Překlad jazyka, sumarizace

 

  • Najdete je na HuggingFace a v dalších repozitářích
Deep Learning for Text with PyTorch

Pojďme si procvičit!

Deep Learning for Text with PyTorch

Preparing Video For Download...