BEncoderRT commited on
Commit
4b2884f
·
verified ·
1 Parent(s): 1a636fa

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +136 -3
README.md CHANGED
@@ -1,3 +1,136 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ base_model:
6
+ - TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
7
+ pipeline_tag: text-classification
8
+ tags:
9
+ - LORA
10
+ - IMDB
11
+ - Sentiment
12
+ ---
13
+ # IMDB-Sentiment-LoRA-TinyLlama-1.1B
14
+
15
+ A lightweight LoRA adapter fine-tuned on the **TinyLlama-1.1B** base model for **IMDB movie review sentiment analysis** (binary classification: Positive or Negative).
16
+
17
+ 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.
18
+
19
+ ## Model Details
20
+
21
+ - **Base Model**: `TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T`
22
+ - **LoRA Configuration**:
23
+ - Rank (`r`): 8
24
+ - Scaling (`lora_alpha`): 16
25
+ - Target modules: `["q_proj", "v_proj"]`
26
+ - Dropout: 0.05
27
+ - Bias: "none"
28
+ - Task type: `CAUSAL_LM`
29
+ - **Training Data**: 8,000 labeled samples from IMDB (balanced)
30
+ - **Training Method**: Instruction-tuned SFT using PEFT + TRL
31
+
32
+ ## Usage Example
33
+
34
+ ```python
35
+ from transformers import AutoTokenizer, AutoModelForCausalLM
36
+ from peft import PeftModel
37
+ import torch
38
+
39
+ # 定义基础模型和 LoRA 模型仓库
40
+ base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
41
+ repo_id_sentiment = "BEncoderRT/IMDB-Sentiment-LoRA-TinyLlama-1.1B" # 请确认此 repo 是否存在,若不存在请替换为正确 ID
42
+
43
+
44
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
45
+ if tokenizer.pad_token is None:
46
+ tokenizer.pad_token = tokenizer.eos_token
47
+
48
+ base_model = AutoModelForCausalLM.from_pretrained(
49
+ base_model_name,
50
+ device_map="auto", # 自动分配到 GPU/CPU
51
+ torch_dtype=torch.float16 # 推荐使用 half precision 节省显存
52
+ )
53
+
54
+ # 单独加载 sentiment LoRA adapter
55
+ sentiment_model = PeftModel.from_pretrained(
56
+ base_model,
57
+ repo_id_sentiment,
58
+ adapter_name="sentiment" # 可选,默认为 default
59
+ )
60
+
61
+ sentiment_model.eval() # 设置为评估模式
62
+ print("Sentiment LoRA 模型加载完成。")
63
+
64
+ # 推理函数(仅针对 sentiment 任务)
65
+ def sentiment_inference(model, tokenizer, review_text, max_new_tokens=50):
66
+ # 设置 adapter(如果有多个,这里确保使用 sentiment)
67
+ if hasattr(model, "set_adapter"):
68
+ model.set_adapter("sentiment")
69
+
70
+ # 构造 prompt
71
+ formatted_prompt = (
72
+ "### Task: Sentiment Analysis\n"
73
+ "### Review:\n"
74
+ f"{review_text}\n"
75
+ "### Answer:\n"
76
+ )
77
+
78
+ inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
79
+
80
+ with torch.no_grad():
81
+ outputs = model.generate(
82
+ **inputs,
83
+ max_new_tokens=max_new_tokens,
84
+ do_sample=True,
85
+ temperature=0.7,
86
+ top_k=50,
87
+ top_p=0.95,
88
+ eos_token_id=tokenizer.eos_token_id,
89
+ pad_token_id=tokenizer.pad_token_id
90
+ )
91
+
92
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
93
+
94
+ # 提取答案
95
+ answer_start = generated_text.find("### Answer:\n")
96
+ if answer_start != -1:
97
+ extracted = generated_text[answer_start + len("### Answer:\n"):].strip()
98
+ # 简单判断 positive/negative
99
+ if 'positive' in extracted.lower():
100
+ return 'positive'
101
+ elif 'negative' in extracted.lower():
102
+ return 'negative'
103
+ return extracted.split('\n')[0].strip()
104
+
105
+ return generated_text
106
+
107
+ # --- 测试用例 ---
108
+ print("\n测试 Sentiment Analysis:")
109
+
110
+ positive_review = "This movie was absolutely fantastic! The acting was superb and the story was captivating."
111
+ print(f"Review: {positive_review}")
112
+ print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, positive_review)}\n")
113
+
114
+ negative_review = "I found this film to be incredibly boring and predictable. A complete waste of time."
115
+ print(f"Review: {negative_review}")
116
+ print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, negative_review)}\n")
117
+
118
+ another_review = "An okay movie, nothing special but not bad either."
119
+ print(f"Review: {another_review}")
120
+ print(f"Sentiment: {sentiment_inference(sentiment_model, tokenizer, another_review)}")
121
+ ```
122
+
123
+ ```
124
+ Sentiment LoRA 模型加载完成。
125
+
126
+ 测试 Sentiment Analysis:
127
+ Review: This movie was absolutely fantastic! The acting was superb and the story was captivating.
128
+ Sentiment: positive
129
+
130
+ Review: I found this film to be incredibly boring and predictable. A complete waste of time.
131
+ Sentiment: negative
132
+
133
+ Review: An okay movie, nothing special but not bad either.
134
+ Sentiment: positive
135
+ ```
136
+