แกะโครงสร้าง Transformer

Transformer Models ด้วย PyTorch

James Chapman

Curriculum Manager, DataCamp

บทความที่เปลี่ยนแปลงทุกสิ่ง...

 

  • Attention Is All You Need โดย Ashish Vaswani et al. (arXiv:1706.03762)
    • กลไก Attention
    • ปรับให้เหมาะกับการสร้างแบบจำลองข้อความ
    • ใช้ใน Large Language Models (LLMs)

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

บทความที่เปลี่ยนแปลงทุกสิ่ง...

 

  • Attention Is All You Need โดย Ashish Vaswani et al.
    • กลไก Attention
    • ปรับให้เหมาะกับการสร้างแบบจำลองข้อความ
    • ใช้ใน Large Language Models (LLMs)

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

บทความที่เปลี่ยนแปลงทุกสิ่ง...

 

  • Attention Is All You Need โดย Ashish Vaswani et al.
    • กลไก Attention
    • ปรับให้เหมาะกับการสร้างแบบจำลองข้อความ
    • ใช้ใน Large Language Models (LLMs)

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

 

Encoder block
  • มีหลายเลเยอร์ที่เหมือนกัน
  • อ่าน และ ประมวลผล ลำดับ input
  • สร้างการแสดงผลเชิงตัวเลขที่ อุดมด้วยบริบท
  • ใช้ self-attention และ feed-forward networks

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

 

Decoder block
  • ลำดับ input ที่ผ่าน Encoder → ลำดับ output

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

 

Positional encoding
  • เข้ารหัส ตำแหน่ง ของแต่ละ token ในลำดับ
  • ลำดับมีความสำคัญต่อการสร้างแบบจำลองลำดับข้อมูล

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

กลไก Attention
  • จับโฟกัสไปที่ token สำคัญและความสัมพันธ์ระหว่าง token
  • ช่วยปรับปรุงการสร้างข้อความ

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

กลไก Attention
  • จับโฟกัสไปที่ token สำคัญและความสัมพันธ์ระหว่าง token
  • ช่วยปรับปรุงการสร้างข้อความ
Self-attention
  • ถ่วงน้ำหนัก ความสำคัญของ token
  • จับ dependency ระยะไกลได้

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

กลไก Attention
  • จับโฟกัสไปที่ token สำคัญและความสัมพันธ์ระหว่าง token
  • ช่วยปรับปรุงการสร้างข้อความ
Self-attention
  • ถ่วงน้ำหนัก ความสำคัญของ token
  • จับ dependency ระยะไกลได้
Multi-head attention
  • แบ่ง input เป็นหลาย head
  • แต่ละ head จับรูปแบบที่ต่างกัน ทำให้ได้ representation ที่ละเอียดขึ้น

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

เจาะลึก Transformer...

 

Position-wise feed-forward networks
  • โครงข่ายประสาทเทียมขนาดเล็กที่ทำการแปลงข้อมูล
  • แปลงแต่ละ token อย่างอิสระ
  • ไม่ขึ้นกับตำแหน่ง / "position-wise"

สถาปัตยกรรม Transformer ที่ปรากฏในบทความวิชาการ Attention Is All You Need

Transformer Models ด้วย PyTorch

Transformers ใน PyTorch

 

  • d_model: จำนวนมิติของ input โมเดล
  • nheads: จำนวน attention head
  • num_encoder_layers: จำนวนเลเยอร์ของ encoder
  • num_decoder_layers: จำนวนเลเยอร์ของ decoder
import torch.nn as nn


model = nn.Transformer(
d_model=512,
nhead=8,
num_encoder_layers=6,
num_decoder_layers=6
)
print(model)
Transformer Models ด้วย PyTorch
Transformer(
  (encoder): TransformerEncoder(
    (layers): ModuleList(
      (0-5): 6 x TransformerEncoderLayer(
        (self_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (linear1): Linear(in_features=512, out_features=2048, bias=True)
        (dropout): Dropout(p=0.1, inplace=False)
        (linear2): Linear(in_features=2048, out_features=512, bias=True)
        (norm1): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm2): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (dropout1): Dropout(p=0.1, inplace=False)
        (dropout2): Dropout(p=0.1, inplace=False)
      )
    )
    (norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
  )
  ...
Transformer Models ด้วย PyTorch
  (decoder): TransformerDecoder(
    (layers): ModuleList(
      (0-5): 6 x TransformerDecoderLayer(
        (self_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (multihead_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (linear1): Linear(in_features=512, out_features=2048, bias=True)
        (dropout): Dropout(p=0.1, inplace=False)
        (linear2): Linear(in_features=2048, out_features=512, bias=True)
        (norm1): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm2): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm3): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (dropout1): Dropout(p=0.1, inplace=False)
        (dropout2): Dropout(p=0.1, inplace=False)
        (dropout3): Dropout(p=0.1, inplace=False)
      )
    )
    (norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
  )
)
Transformer Models ด้วย PyTorch

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

Transformer Models ด้วย PyTorch

Preparing Video For Download...