Views
No views yet
1from transformers import RobertaForMaskedLM, RobertaTokenizer
2import torch
3
4# Load the fine-tuned RoBERTa model and tokenizer
5model_name = 'roberta_finetuned' # Your fine-tuned RoBERTa model
6model = RobertaForMaskedLM.from_pretrained(model_name)
7tokenizer = RobertaTokenizer.from_pretrained(model_name)
8
9# Move the model to GPU if available
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11model.to(device)
12
13# Quantize the model to FP16
14model = model.half()
15
16# Save the quantized model and tokenizer
17model.save_pretrained("./quantized_roberta_model")
18tokenizer.save_pretrained("./quantized_roberta_model")
19
20# Example input for testing (10 sentences)
21input_texts = [
22 "The sky is <mask> during the night.",
23 "Machine learning is a subset of <mask> intelligence.",
24 "The largest planet in the solar system is <mask>.",
25 "The Eiffel Tower is located in <mask>.",
26 "The sun rises in the <mask>.",
27 "Mount Everest is the highest mountain in the <mask>.",
28 "The capital of Japan is <mask>.",
29 "Shakespeare wrote Romeo and <mask>.",
30 "The currency of the United States is <mask>.",
31 "The fastest land animal is the <mask>."
32]
33
34# Process each input sentence
35for input_text in input_texts:
36 # Tokenize input text
37 inputs = tokenizer(input_text, return_tensors="pt").to(device)
38
39 # Perform inference
40 with torch.no_grad():
41 outputs = model(**inputs)
42 logits = outputs.logits
43
44 # Get the prediction for the masked token
45 masked_index = inputs.input_ids[0].tolist().index(tokenizer.mask_token_id)
46 predicted_token_id = logits[0, masked_index].argmax(axis=-1)
47 predicted_token = tokenizer.decode(predicted_token_id)
48
49 print(f"Input: {input_text}")
50 print(f"Predicted token: {predicted_token}\n").
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safetensors/ # Quantized Model
├── README.md # Model documentation