เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การแปลภาษาด้วยโมเดล PyTorch ที่ผ่านการเทรนมาแล้ว

ทีมของคุณที่ PyBooks กำลังพัฒนาโปรเจกต์ AI ที่เกี่ยวข้องกับการแปลภาษา พวกเขาต้องการใช้ pre-trained model สำหรับงานนี้ ซึ่งช่วยประหยัดเวลาและทรัพยากรในการเทรนได้มาก โจทย์ของแบบฝึกหัดนี้คือการตั้งค่าโมเดลแปลภาษาจากไลบรารี Transformers ของ HuggingFace โดยเฉพาะโมเดล T5 (Text-To-Text Transfer Transformer) แล้วใช้แปลประโยคภาษาอังกฤษเป็นภาษาฝรั่งเศส

ได้โหลด T5Tokenizer และ T5ForConditionalGeneration ไว้ให้แล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Initialize tokenizer และ model จาก pretrained model "t5-small"
  • เข้ารหัส input prompt โดยใช้ tokenizer และระบุให้คืนค่าเป็น PyTorch tensor
  • แปล input prompt โดยใช้ model และสร้างผลลัพธ์ที่แปลแล้ว

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Initalize tokenizer and model
tokenizer = ____.from_pretrained("t5-small")
model = ____.from_pretrained("t5-small")

input_prompt = "translate English to French: 'Hello, how are you?'"

# Encode the input prompt using the tokenizer
input_ids = ____.____(input_prompt, return_tensors="____")

# Generate the translated ouput
output = model.____(input_ids, max_length=50)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("Generated text:",generated_text)
แก้ไขและรันโค้ด