Views
No views yet
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4# 直接从HuggingFace加载你的模型
5model_name = "yiwenX/Qwen3-0.6B-imdb"
6model = AutoModelForSequenceClassification.from_pretrained(
7 model_name,
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13# 如果tokenizer没有pad_token,设置一下
14if tokenizer.pad_token is None:
15 tokenizer.pad_token = tokenizer.eos_token
16
17# 推理示例
18def predict_sentiment(text):
19 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
20
21 # 移动到正确的设备
22 inputs = {k: v.to(model.device) for k, v in inputs.items()}
23
24 with torch.no_grad():
25 outputs = model(**inputs)
26 prediction = torch.argmax(outputs.logits, dim=-1)
27
28 return "Positive" if prediction.item() == 1 else "Negative"
29
30# 测试示例
31test_texts = [
32 "This movie was absolutely fantastic! Great acting and storyline.",
33 "Terrible movie, waste of time. Bad acting and boring plot.",
34 "One of the best films I've ever seen. Highly recommend!",
35 "I fell asleep halfway through. Very disappointing."
36]
37
38for text in test_texts:
39 sentiment = predict_sentiment(text)
40 print(f"Text: {text[:50]}...")
41 print(f"Sentiment: {sentiment}\n")| Metric | Value |
|---|---|
| Test Accuracy | ~92-94% (expected) |
| F1 Score | ~0.92-0.94 |
| Training Loss (final) | ~0.1-0.2 |
1@misc{qwen3-imdb-lora,
2 author = {yiwenX},
3 title = {Qwen3-0.6B-IMDB: Fine-tuned Sentiment Analysis Model},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/yiwenX/Qwen3-0.6B-imdb}
7}