ONE is preparing: 01 / INITIALIZING MODEL ROUTER. Evaluating routing across providers.
Current state
01 / INITIALIZING MODEL ROUTER.

Evaluating routing across providers.

Start learning
← NLP & Multimodal

AI Researcher

Seq2Seq - Chuỗi sang chuỗi

Vietnamese onlyThe learning content is available in Vietnamese only. The surrounding interface follows your language.

Kiến trúc mã hóa-giải mã chuyển đổi chuỗi đầu vào thành chuỗi đầu ra, nền tảng cho dịch máy và tóm tắt văn bản.

lesson102 minNLP & MultimodalAI Researcher

Thử thách

Bạn là phiên dịch viên. Nghe câu "Tôi yêu Việt Nam" xong, bạn dịch sang tiếng Anh. Bạn phải nghe HẾT câu trước rồi mới dịch, hay dịch từng từ ngay lập tức?

Bạn phải nghe hết "Tôi yêu Việt Nam" (MÃ HÓA toàn bộ ý nghĩa) rồi mới bắt đầu dịch "I love Vietnam" (GIẢI MÃ từng từ). Seq2Seq hoạt động đúng như phiên dịch viên: Encoder nghe → nén thành "ý nghĩa" → Decoder dịch!

Khoảnh khắc A-ha

Seq2Seq = Encoder (mã hóa input → context vector) + Decoder (giải mã context → output). Giống phiên dịch viên: nghe hết → nhớ ý chính → dịch từng từ!

Nhưng có vấn đề: context vector có kích thước cố định. Câu dài 100 từ bị nén vào cùng kích thước với câu 5 từ — như nhét cả quyển sách vào 1 tờ giấy nhớ!

Thử thách nhanh

Seq2Seq dịch câu 5 từ tốt, nhưng câu 50 từ lại kém. Tại sao?

Context vector = 1 vector cố định phải chứa ý nghĩa toàn bộ câu. Câu càng dài, thông tin bị nén càng nhiều → mất mát. Attention sẽ giải quyết vấn đề này!

Vấn đề bottleneck

Độ dài câu nguồnContext vectorHệ quả
Ngắn (5 từ)256 chiềuĐủ chỗ chứa
Dài (50 từ)256 chiều (cùng kích thước)Mất thông tin

Lý thuyết

Sequence-to-Sequence (Sutskever et al., Google 2014) gồm Encoder nén input và Decoder sinh output, thường dùng RNN hoặc LSTM làm khối cơ sở, và sau đó được tăng cường bằng attention để khắc phục bottleneck.

seq2seq_concept.py

import torch
import torch.nn as nn

class Encoder(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.rnn = nn.LSTM(embed_dim, hidden_dim, batch_first=True)

    def forward(self, x):
        # x: [batch, seq_len] → "Tôi yêu Việt Nam"
        embedded = self.embedding(x)
        outputs, (hidden, cell) = self.rnn(embedded)
        return hidden, cell  # Context vector!

class Decoder(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.rnn = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, vocab_size)

    def forward(self, x, hidden, cell):
        # x: token trước đó → "I", "love", ...
        embedded = self.embedding(x.unsqueeze(1))
        output, (hidden, cell) = self.rnn(embedded, (hidden, cell))
        prediction = self.fc(output.squeeze(1))
        return prediction, hidden, cell  # Từ tiếp theo

Tóm tắt

  • Seq2Seq = Encoder (nén input → context vector) + Decoder (giải mã → output).
  • Decoder sinh từng token một (autoregressive): output t-1 → input t.
  • Bottleneck: context vector cố định không đủ chứa thông tin câu dài.
  • Attention giải quyết bottleneck bằng cách cho decoder nhìn mọi vị trí encoder.
  • Ứng dụng: dịch máy, tóm tắt, chatbot — tiền thân của kiến trúc Transformer.

Continue the module

Lesson 4 of 13