Views
No views yet
q_proj, k_proj, v_proj, o_projstanfordnlp/imdb), 25,000 labeled movie reviews in the original train splitassistant_only_loss=True); system and user tokens are masked| Metric | Value |
|---|---|
| Accuracy | 0.9176 |
| Precision | 0.9314 |
| Recall | 0.9014 |
| F1 | 0.9162 |
| Predicted: negative | Predicted: positive | |
|---|---|---|
| True: negative | 465 | 33 |
| True: positive | 49 | 448 |
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3from peft import PeftModel
4
5BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
6ADAPTER_REPO = "Carlo88/qwen05b-sentiment-lora-imdb"
7
8tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
9
10bnb_config = BitsAndBytesConfig(
11 load_in_4bit=True,
12 bnb_4bit_quant_type="nf4",
13 bnb_4bit_compute_dtype=torch.float16,
14 bnb_4bit_use_double_quant=True,
15)
16
17base_model = AutoModelForCausalLM.from_pretrained(
18 BASE_MODEL,
19 quantization_config=bnb_config,
20 device_map="auto",
21 dtype=torch.float16,
22)
23
24model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)
25model.eval()
26
27review = "This movie was a complete waste of time, the plot made no sense."
28messages = [
29 {"role": "system", "content": "Sei un classificatore di sentiment. Rispondi solo con 'positive' o 'negative'."},
30 {"role": "user", "content": review},
31]
32prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34
35output = model.generate(**inputs, max_new_tokens=5, do_sample=False)
36response = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
37print(response) # -> "negative"<br /><br />) which were not cleaned during preprocessing. This did not appear to harm performance but is worth noting for anyone extending this work.transformers, peft, trl, bitsandbytes