IMDB-Sentiment-LoRA-TinyLlama-1.1B

A lightweight LoRA adapter fine-tuned on the TinyLlama-1.1B base model for IMDB movie review sentiment analysis (binary classification: Positive or Negative).

Trained with supervised fine-tuning (SFT) on only 8,000 examples from the Hugging Face imdb dataset, formatted as instruction prompts. Despite the limited data, it achieves solid performance with very low memory usage and fast inference.

Model Details

  • Base Model: TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
  • LoRA Configuration:
    • Rank (r): 8
    • Scaling (lora_alpha): 16
    • Target modules: ["q_proj", "v_proj"]
    • Dropout: 0.05
    • Bias: "none"
    • Task type: CAUSAL_LM
  • Training Data: 8,000 labeled samples from IMDB (balanced)
  • Training Method: Instruction-tuned SFT using PEFT + TRL

Usage Example

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

# 定义基础模型和 LoRA 模型仓库
base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
repo_id_sentiment = "BEncoderRT/IMDB-Sentiment-LoRA-TinyLlama-1.1B"  # 请确认此 repo 是否存在,若不存在请替换为正确 ID


tokenizer = AutoTokenizer.from_pretrained(base_model_name)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

base_model = AutoModelForCausalLM.from_pretrained(
    base_model_name,
    device_map="auto",  # 自动分配到 GPU/CPU
    torch_dtype=torch.float16  # 推荐使用 half precision 节省显存
)

# 单独加载 sentiment LoRA adapter
sentiment_model = PeftModel.from_pretrained(
    base_model,
    repo_id_sentiment,
    adapter_name="sentiment"  # 可选,默认为 default
)

sentiment_model.eval()  # 设置为评估模式
print("Sentiment LoRA 模型加载完成。")

# 推理函数(仅针对 sentiment 任务)
def sentiment_inference(model, tokenizer, review_text, max_new_tokens=50):
    # 设置 adapter(如果有多个,这里确保使用 sentiment)
    if hasattr(model, "set_adapter"):
        model.set_adapter("sentiment")
    
    # 构造 prompt
    formatted_prompt = (
        "### Task: Sentiment Analysis\n"
        "### Review:\n"
        f"{review_text}\n"
        "### Answer:\n"
    )
    
    inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=max_new_tokens,
            do_sample=True,
            temperature=0.7,
            top_k=50,
            top_p=0.95,
            eos_token_id=tokenizer.eos_token_id,
            pad_token_id=tokenizer.pad_token_id
        )
    
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # 提取答案
    answer_start = generated_text.find("### Answer:\n")
    if answer_start != -1:
        extracted = generated_text[answer_start + len("### Answer:\n"):].strip()
        # 简单判断 positive/negative
        if 'positive' in extracted.lower():
            return 'positive'
        elif 'negative' in extracted.lower():
            return 'negative'
        return extracted.split('\n')[0].strip()
    
    return generated_text

# --- 测试用例 ---
print("\n测试 Sentiment Analysis:")

positive_review = "This movie was absolutely fantastic! The acting was superb and the story was captivating."
print(f"Review: {positive_review}")
print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, positive_review)}\n")

negative_review = "I found this film to be incredibly boring and predictable. A complete waste of time."
print(f"Review: {negative_review}")
print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, negative_review)}\n")

another_review = "An okay movie, nothing special but not bad either."
print(f"Review: {another_review}")
print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, another_review)}")
Sentiment LoRA 模型加载完成。

测试 Sentiment Analysis:
Review: This movie was absolutely fantastic! The acting was superb and the story was captivating.
Sentiment: positive

Review: I found this film to be incredibly boring and predictable. A complete waste of time.
Sentiment: negative

Review: An okay movie, nothing special but not bad either.
Sentiment: positive
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

Model tree for BEncoderRT/IMDB-Sentiment-LoRA-TinyLlama-1.1B

Finetuned
(102)
this model