Views
No views yet
pip install transformers torch1from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
2import torch
3
4# Load quantized model
5quantized_model_path = "/kaggle/working/distilbert_finetuned_fp16"
6quantized_model = DistilBertForSequenceClassification.from_pretrained(quantized_model_path)
7quantized_model.eval() # Set to evaluation mode
8quantized_model.half() # Convert model to FP16
9
10# Load tokenizer
11tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
12
13# Define a test sentence
14test_sentence = "This update has received great engagement!"
15
16# Tokenize input
17inputs = tokenizer(test_sentence, return_tensors="pt", padding=True, truncation=True, max_length=128)
18
19# Ensure input tensors are in correct dtype
20inputs["input_ids"] = inputs["input_ids"].long() # Convert to long type
21inputs["attention_mask"] = inputs["attention_mask"].long() # Convert to long type
22
23# Make prediction
24with torch.no_grad():
25 outputs = quantized_model(**inputs)
26
27# Get predicted class
28predicted_class = torch.argmax(outputs.logits, dim=1).item()
29print(f"Predicted Class: {predicted_class}")
30
31label_mapping = {0: "very_negative", 1: "negative", 2: "neutral", 3: "positive", 4: "very_positive"} # Example
32
33predicted_label = label_mapping[predicted_class]
34print(f"Predicted Label: {predicted_label}").
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safensors/ # Fine Tuned Model
├── README.md # Model documentation