YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

Vietnamese Sentiment Classifier — LoRA Fine-tuning

Fine-tuned Gemma 2B-IT bằng QLoRA để phân loại cảm xúc (sentiment) tiếng Việt trên dataset UIT-VSFC.

Kết quả

Metric Baseline (zero-shot) Fine-tuned (LoRA) Cải thiện
Weighted F1 10.7% 94.9% +787%
Accuracy 10.0% 95.0% +850%

Chi tiết theo class

Class Precision Recall F1-score
Positive 0.97 0.94 0.96
Negative 0.93 0.99 0.96
Neutral 0.83 0.62 0.71

Neutral F1 thấp hơn do chỉ có 8 samples trong test set, không phải do model kém.

Mô tả dự án

Bài toán

Phân loại câu phản hồi của sinh viên Việt Nam thành 3 loại: positive, negative, neutral.

Dataset

UIT-VSFC — Vietnamese Students' Feedback Corpus, gồm 16,000+ câu tiếng Việt được gán nhãn bởi người.

Split Samples
Train 11,426
Validation 1,583
Test 3,166

Phương pháp

QLoRA (Quantized Low-Rank Adaptation):

  • Base model: google/gemma-2b-it
  • Quantization: 4-bit NF4
  • LoRA rank: 16, alpha: 32
  • Target modules: q_proj, v_proj
  • Trainable parameters: ~0.06% tổng tham số

Training config:

  • Epochs: 3
  • Batch size: 4 (gradient accumulation: 4, effective batch: 16)
  • Learning rate: 2e-4 với cosine scheduler
  • Optimizer: paged AdamW 8-bit
  • Hardware: Google Colab A100 GPU

Training loss

Epoch Training Loss Validation Loss
1 0.9493 0.8839
2 0.8964 0.8458
3 0.8533 0.8422

Cách sử dụng

Cài đặt

pip install transformers peft bitsandbytes accelerate torch

Inference

from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
import torch

# Load base model với 4-bit quantization
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
)

base_model = AutoModelForCausalLM.from_pretrained(
    "google/gemma-2b-it",
    quantization_config=bnb_config,
    device_map="auto",
)

# Load LoRA adapter
model = PeftModel.from_pretrained(base_model, "tung1707/vietnamese-sentiment-lora")
tokenizer = AutoTokenizer.from_pretrained("tung1707/vietnamese-sentiment-lora")

# Predict
def predict_sentiment(text):
    prompt = f"""Phân loại sentiment của câu tiếng Việt sau thành đúng 1 từ: positive, negative, hoặc neutral.

Câu: "{text}"
Sentiment:"""
    
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=10, do_sample=False)
    response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
    return response.strip().lower()

# Test
print(predict_sentiment("Giảng viên dạy rất nhiệt tình"))  # → positive
print(predict_sentiment("Phòng học quá nóng"))              # → negative
print(predict_sentiment("Lịch thi được thông báo đúng hạn")) # → neutral

Cấu trúc repo

├── LoRA_Vietnamese_Sentiment.ipynb   # Notebook training hoàn chỉnh
├── README.md
├── adapter_config.json               # LoRA config
└── adapter_model.safetensors         # LoRA weights (~vài MB)

Kiến thức kỹ thuật

Tại sao dùng LoRA thay vì full fine-tuning?

  • Full fine-tuning Gemma 2B cần ~10GB VRAM và cập nhật toàn bộ 2.5 tỷ tham số
  • LoRA chỉ thêm ~1.5M tham số mới (0.06%), chạy được trên Google Colab free (T4 GPU)
  • Kết quả gần tương đương full fine-tuning cho task đơn giản như classification

Tại sao dùng QLoRA (4-bit)?

  • Giảm VRAM từ ~10GB xuống ~3-4GB bằng cách quantize base model xuống 4-bit
  • Chỉ LoRA adapter được train ở full precision
  • Trade-off: mất rất ít accuracy, tiết kiệm rất nhiều tài nguyên

Tại sao chọn target_modules = ["q_proj", "v_proj"]?

  • Đây là Query và Value projection trong attention mechanism
  • Nghiên cứu gốc của LoRA (Hu et al., 2021) cho thấy fine-tune Q và V cho kết quả tốt nhất
  • Thêm K projection không cải thiện đáng kể nhưng tốn thêm tài nguyên

Tài liệu tham khảo

Tác giả

Bùi Duy Tùng — GitHub | Email

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Papers for tung1707/vietnamese-sentiment-lora