Implementation:666ghj BettaFish Qwen3LoRAUniversal train
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Sentiment_Analysis, Parameter_Efficient_Finetuning |
| Last Updated | 2026-10-08 00:00 GMT |
Overview
Concrete LoRA fine-tuning class for Qwen3 models (0.6B/4B/8B) that uses the PEFT library to adapt the base model for Chinese sentiment classification with minimal trainable parameters.
Description
The Qwen3LoRAUniversal class implements parameter-efficient fine-tuning of Qwen3 language models for sentiment analysis. It: 1. Loads a Qwen3 base model (0.6B, 4B, or 8B) from local cache or HuggingFace 2. Applies LoRA adapters via `peft.LoraConfig` and `peft.get_peft_model` 3. Uses HuggingFace `Trainer` and `TrainingArguments` for training 4. Supports configurable LoRA rank (r) and alpha per model size 5. Saves the LoRA-adapted model for later prediction
The class auto-selects the device (CUDA or CPU), handles tokenizer setup (pad_token), and saves models to local directories for offline reuse.
Usage
Import this class when you need to fine-tune Qwen3 for sentiment analysis with LoRA. It is part of the SentimentAnalysisModel/WeiboSentiment_SmallQwen package.
Code Reference
Source Location
- Repository: BettaFish
- File: `SentimentAnalysisModel/WeiboSentiment_SmallQwen/qwen3_lora_universal.py:L28-468`
Signature
class Qwen3LoRAUniversal(BaseQwenModel):
def __init__(self, model_size: str = "0.6B"):
"""
Args:
model_size: "0.6B", "4B", or "8B"
"""
def _load_base_model(self):
"""Load Qwen3 base model from local dir, HF cache, or download."""
def train(
self,
train_data: List[Tuple[str, int]],
eval_data: List[Tuple[str, int]] = None,
epochs: int = 3,
batch_size: int = None, # defaults to config recommended_batch_size
learning_rate: float = None, # defaults to config recommended_lr
) -> None:
"""
Fine-tune with LoRA adapters.
Uses peft.LoraConfig with:
r = config["lora_r"] (16/32/64 by model size)
lora_alpha = config["lora_alpha"] (32/64/128)
task_type = TaskType.CAUSAL_LM
"""
Import
from SentimentAnalysisModel.WeiboSentiment_SmallQwen.qwen3_lora_universal import Qwen3LoRAUniversal
from SentimentAnalysisModel.WeiboSentiment_SmallQwen.models_config import QWEN3_MODELS, MODEL_PATHS
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_size | str | Yes | "0.6B", "4B", or "8B" |
| train_data | List[Tuple[str, int]] | Yes | (text, label) training pairs |
| epochs | int | No | Training epochs (default: 3) |
| batch_size | int | No | Batch size (default: from config) |
| learning_rate | float | No | LR (default: from config) |
Outputs
| Name | Type | Description |
|---|---|---|
| model | PEFT model | LoRA-adapted model saved to disk |
Usage Examples
Fine-tune Qwen3-0.6B with LoRA
from SentimentAnalysisModel.WeiboSentiment_SmallQwen.qwen3_lora_universal import Qwen3LoRAUniversal
from SentimentAnalysisModel.WeiboSentiment_MachineLearning.utils import load_corpus
# Load data
train_data = load_corpus("data/weibo2018/train.txt")
eval_data = load_corpus("data/weibo2018/test.txt")
# Initialize and train
model = Qwen3LoRAUniversal(model_size="0.6B")
model.train(
train_data=train_data,
eval_data=eval_data,
epochs=3,
)
# Model is saved to ./models/qwen3_lora_0.6b_final
Related Pages
Implements Principle
- Principle:666ghj_BettaFish_LoRA_Fine_Tuning
- Principle:666ghj_BettaFish_Multilingual_Sentiment_Classification
- Principle:666ghj_BettaFish_Sentiment_Model_Deployment