Tinh chỉnh mô hình với Trainer

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Dennis Lee

Data Engineer, Amazon

Chuẩn bị dữ liệu

 

Sơ đồ huấn luyện phân tán với nhân bản mô hình và chia nhỏ dữ liệu.

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Huấn luyện phân tán

 

 

Lưu đồ minh họa các chủ đề: chuẩn bị dữ liệu, huấn luyện phân tán, huấn luyện hiệu quả và bộ tối ưu.

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Trainer và Accelerator

Biểu đồ so sánh dễ dùng và khả năng tùy biến cho Accelerator và Trainer.

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Trainer và Accelerator

Biểu đồ so sánh dễ dùng và khả năng tùy biến cho Accelerator và Trainer.

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Tăng tốc huấn luyện với Trainer

  • Thư viện Trainer

    from transformers import Trainer
    
  • Chạy mô hình song song trên mỗi thiết bị

  • Tăng tốc huấn luyện, như dây chuyền lắp ráp
  • Ôn lại đầu vào: dataset, model, metrics
  • Xây dựng phân tích cảm xúc cho thương mại điện tử

Hình ảnh dây chuyền lắp ráp ô tô, minh họa xử lý song song.

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Bộ dữ liệu cảm xúc đánh giá sản phẩm

print(dataset)
DatasetDict({
    train: Dataset({
        features: ['Text', 'Label'],
        num_rows: 1000
    }), ...})
print(f'"{dataset["train"]["Text"][0]}": {dataset["train"]["Label"][0]}')
"I love this product!": positive
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Chuyển nhãn thành số nguyên

def map_labels(example):
    if example["Label"] == "negative":
        return {"labels": 0}

else: return {"labels": 1} dataset = dataset.map(map_labels)
print(f'First label: {dataset["train"]["labels"][0]}')
First label: 1
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Định nghĩa tokenizer và mô hình

  • Tải mô hình và tokenizer tiền huấn luyện:
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", 
                                                           num_labels=2)

tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
  • Áp dụng tokenizer cho trường text:
def encode(examples):

return tokenizer(examples["Text"], padding="max_length", truncation=True, return_tensors="pt")
dataset = dataset.map(encode, batched=True)
print(f'The first tokenized review is {dataset["train"]["input_ids"][0]}.')
The first tokenized review is [101, 1045, 2293, 2023, 4031, 999, 102].
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Định nghĩa chỉ số đánh giá

import evaluate


def compute_metrics(eval_predictions):
load_accuracy = evaluate.load("accuracy") load_f1 = evaluate.load("f1")
logits, labels = eval_predictions
predictions = np.argmax(logits, axis=-1)
accuracy = load_accuracy.compute(predictions=predictions, references=labels)[ "accuracy" ]
f1 = load_f1.compute(predictions=predictions, references=labels)["f1"]
return {"accuracy": accuracy, "f1": f1}
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Tham số huấn luyện

  • output_dir: Nơi lưu mô hình
  • Chỉ định siêu tham số (vd: learning_rate, weight_decay)
  • save_strategy: Lưu sau mỗi epoch
  • evaluation_strategy: Đánh giá sau mỗi epoch
from transformers import (
    TrainingArguments)

training_args = TrainingArguments(
    output_dir="output_folder",

learning_rate=2e-5, per_device_train_batch_size=16, per_device_eval_batch_size=16, num_train_epochs=2, weight_decay=0.01,
save_strategy="epoch", evaluation_strategy="epoch", )
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Thiết lập Trainer

from transformers import Trainer

trainer = Trainer(model=model,

args=training_args,
train_dataset=dataset["train"], eval_dataset=dataset["validation"],
compute_metrics=compute_metrics)
trainer.train()
{'epoch': 1.0, 'eval_loss': 0.79, 'eval_accuracy': 0.00, 'eval_f1': 0.00}
{'epoch': 2.0, 'eval_loss': 0.65, 'eval_accuracy': 0.11, 'eval_f1': 0.15}
print(trainer.args.device)
cpu
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Chạy phân tích cảm xúc cho thương mại điện tử

sample_review = "This product is amazing!"

input_ids = tokenizer.encode(sample_review, return_tensors='pt') print(f"Tokenized review: {input_ids}")
Tokenized review: tensor([[ 101, 2023, 4031, 2003, 6429,  999,  102 ]])
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Chạy phân tích cảm xúc cho thương mại điện tử

output = model(input_ids)
print(f"Output logits: {output.logits}")
Output logits: tensor([[ -0.0538, 0.1300 ]])
predicted_label = torch.argmax(output.logits, dim=1).item()
print(f"Predicted label: {predicted_label}")
Predicted label: 1
sentiment = "Negative" if predicted_label == 0 else "Positive"
print(f'The sentiment of the product review is "{sentiment}."')
The sentiment of the product review is "Positive."
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Checkpoint với Trainer

  • Tiếp tục từ checkpoint mới nhất, như tạm dừng phim
trainer.train(resume_from_checkpoint=True)
{'epoch': 3.0, 'eval_loss': 0.29, 'eval_accuracy': 0.37, 'eval_f1': 0.51}
{'epoch': 4.0, 'eval_loss': 0.23, 'eval_accuracy': 0.46, 'eval_f1': 0.58}
  • Tiếp tục từ checkpoint cụ thể trong thư mục output
trainer.train(resume_from_checkpoint="model/checkpoint-1000")
Huấn luyện Mô hình AI Hiệu quả với PyTorch

Passons à la pratique !

Huấn luyện Mô hình AI Hiệu quả với PyTorch

Preparing Video For Download...