Upload 7 files
Browse files- README.md +99 -0
- config.json +39 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- special_tokens_map.json +51 -0
- tokenizer_config.json +57 -0
- vocab.json +0 -0
README.md
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# RoBERTa-Based Topic Classification Model Using AG News Dataset
|
2 |
+
|
3 |
+
This repository hosts a RoBERTa-based transformer model fine-tuned for topic classification on the AG News dataset. The model identifies topics such as World, Sports, Business, and Science/Technology in a given news text.
|
4 |
+
|
5 |
+
## Model Details
|
6 |
+
|
7 |
+
- **Model Architecture:** RoBERTa (roberta-base)
|
8 |
+
- **Task:** Topic Classification
|
9 |
+
- **Dataset:** AG News (from Hugging Face Datasets)
|
10 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
11 |
+
|
12 |
+
## Usage
|
13 |
+
|
14 |
+
### Installation
|
15 |
+
|
16 |
+
```sh
|
17 |
+
pip install transformers datasets torch
|
18 |
+
```
|
19 |
+
|
20 |
+
### Loading and Predicting
|
21 |
+
|
22 |
+
```python
|
23 |
+
from transformers import RobertaTokenizer, RobertaForSequenceClassification
|
24 |
+
import torch
|
25 |
+
|
26 |
+
# Load model and tokenizer
|
27 |
+
model = RobertaForSequenceClassification.from_pretrained("your-saved-model-directory")
|
28 |
+
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
|
29 |
+
model.eval()
|
30 |
+
|
31 |
+
# Sample prediction
|
32 |
+
def predict_topic(texts, model, tokenizer, device='cpu'):
|
33 |
+
import re
|
34 |
+
if isinstance(texts, str):
|
35 |
+
texts = [texts]
|
36 |
+
|
37 |
+
def preprocess(text):
|
38 |
+
text = text.lower()
|
39 |
+
text = re.sub(r"http\S+|www\S+|https\S+", '', text)
|
40 |
+
text = re.sub(r'\@\w+|\#', '', text)
|
41 |
+
text = re.sub(r"[^a-zA-Z0-9\s.,!?']", '', text)
|
42 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
43 |
+
return text
|
44 |
+
|
45 |
+
cleaned_texts = [preprocess(t) for t in texts]
|
46 |
+
inputs = tokenizer(cleaned_texts, padding=True, truncation=True, return_tensors="pt").to(device)
|
47 |
+
|
48 |
+
model.to(device)
|
49 |
+
model.eval()
|
50 |
+
with torch.no_grad():
|
51 |
+
outputs = model(**inputs)
|
52 |
+
preds = torch.argmax(outputs.logits, dim=1).tolist()
|
53 |
+
|
54 |
+
label_map = {0: "World", 1: "Sports", 2: "Business", 3: "Sci/Tech"}
|
55 |
+
return [label_map[p] for p in preds]
|
56 |
+
|
57 |
+
# Example
|
58 |
+
sample_texts = [
|
59 |
+
"The stock market witnessed a major crash today due to inflation concerns.",
|
60 |
+
"The new space telescope has captured unprecedented images of distant galaxies."
|
61 |
+
]
|
62 |
+
results = predict_topic(sample_texts, model, tokenizer)
|
63 |
+
for text, label in zip(sample_texts, results):
|
64 |
+
print(f"Text: {text}\nPredicted Topic: {label}\n")
|
65 |
+
```
|
66 |
+
|
67 |
+
## Performance Metrics
|
68 |
+
|
69 |
+
- **Accuracy:** ~0.96 on AG News test split
|
70 |
+
|
71 |
+
## Fine-Tuning Details
|
72 |
+
|
73 |
+
### Dataset
|
74 |
+
|
75 |
+
The dataset is sourced from the AG News dataset available via Hugging Face Datasets.
|
76 |
+
|
77 |
+
### Training
|
78 |
+
|
79 |
+
- Number of epochs: 3
|
80 |
+
- Batch size: 8
|
81 |
+
- Evaluation strategy: epoch
|
82 |
+
- Learning rate: 2e-5
|
83 |
+
|
84 |
+
## Repository Structure
|
85 |
+
|
86 |
+
```
|
87 |
+
.
|
88 |
+
├── model/ # Contains the fine-tuned model files
|
89 |
+
├── tokenizer/ # Tokenizer configuration and vocab
|
90 |
+
├── README.md # Model documentation
|
91 |
+
```
|
92 |
+
|
93 |
+
## Limitations
|
94 |
+
|
95 |
+
- The model is trained specifically for AG News-style texts and may not generalize well to informal or unrelated domains.
|
96 |
+
|
97 |
+
## Contributing
|
98 |
+
|
99 |
+
Contributions are welcome! Please open an issue or submit a PR for suggestions or improvements.
|
config.json
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"RobertaForSequenceClassification"
|
4 |
+
],
|
5 |
+
"attention_probs_dropout_prob": 0.1,
|
6 |
+
"bos_token_id": 0,
|
7 |
+
"classifier_dropout": null,
|
8 |
+
"eos_token_id": 2,
|
9 |
+
"hidden_act": "gelu",
|
10 |
+
"hidden_dropout_prob": 0.1,
|
11 |
+
"hidden_size": 768,
|
12 |
+
"id2label": {
|
13 |
+
"0": "LABEL_0",
|
14 |
+
"1": "LABEL_1",
|
15 |
+
"2": "LABEL_2",
|
16 |
+
"3": "LABEL_3"
|
17 |
+
},
|
18 |
+
"initializer_range": 0.02,
|
19 |
+
"intermediate_size": 3072,
|
20 |
+
"label2id": {
|
21 |
+
"LABEL_0": 0,
|
22 |
+
"LABEL_1": 1,
|
23 |
+
"LABEL_2": 2,
|
24 |
+
"LABEL_3": 3
|
25 |
+
},
|
26 |
+
"layer_norm_eps": 1e-05,
|
27 |
+
"max_position_embeddings": 514,
|
28 |
+
"model_type": "roberta",
|
29 |
+
"num_attention_heads": 12,
|
30 |
+
"num_hidden_layers": 12,
|
31 |
+
"pad_token_id": 1,
|
32 |
+
"position_embedding_type": "absolute",
|
33 |
+
"problem_type": "single_label_classification",
|
34 |
+
"torch_dtype": "float16",
|
35 |
+
"transformers_version": "4.51.3",
|
36 |
+
"type_vocab_size": 1,
|
37 |
+
"use_cache": true,
|
38 |
+
"vocab_size": 50265
|
39 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5f9a9ada76280e737a482c2bc9391d8334408ea67a6a83827faea171d85f1d8e
|
3 |
+
size 249321504
|
special_tokens_map.json
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": true,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"cls_token": {
|
10 |
+
"content": "<s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": true,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"eos_token": {
|
17 |
+
"content": "</s>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": true,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"mask_token": {
|
24 |
+
"content": "<mask>",
|
25 |
+
"lstrip": true,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"pad_token": {
|
31 |
+
"content": "<pad>",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": true,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
},
|
37 |
+
"sep_token": {
|
38 |
+
"content": "</s>",
|
39 |
+
"lstrip": false,
|
40 |
+
"normalized": true,
|
41 |
+
"rstrip": false,
|
42 |
+
"single_word": false
|
43 |
+
},
|
44 |
+
"unk_token": {
|
45 |
+
"content": "<unk>",
|
46 |
+
"lstrip": false,
|
47 |
+
"normalized": true,
|
48 |
+
"rstrip": false,
|
49 |
+
"single_word": false
|
50 |
+
}
|
51 |
+
}
|
tokenizer_config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"added_tokens_decoder": {
|
4 |
+
"0": {
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false,
|
10 |
+
"special": true
|
11 |
+
},
|
12 |
+
"1": {
|
13 |
+
"content": "<pad>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false,
|
18 |
+
"special": true
|
19 |
+
},
|
20 |
+
"2": {
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false,
|
26 |
+
"special": true
|
27 |
+
},
|
28 |
+
"3": {
|
29 |
+
"content": "<unk>",
|
30 |
+
"lstrip": false,
|
31 |
+
"normalized": true,
|
32 |
+
"rstrip": false,
|
33 |
+
"single_word": false,
|
34 |
+
"special": true
|
35 |
+
},
|
36 |
+
"50264": {
|
37 |
+
"content": "<mask>",
|
38 |
+
"lstrip": true,
|
39 |
+
"normalized": false,
|
40 |
+
"rstrip": false,
|
41 |
+
"single_word": false,
|
42 |
+
"special": true
|
43 |
+
}
|
44 |
+
},
|
45 |
+
"bos_token": "<s>",
|
46 |
+
"clean_up_tokenization_spaces": false,
|
47 |
+
"cls_token": "<s>",
|
48 |
+
"eos_token": "</s>",
|
49 |
+
"errors": "replace",
|
50 |
+
"extra_special_tokens": {},
|
51 |
+
"mask_token": "<mask>",
|
52 |
+
"model_max_length": 512,
|
53 |
+
"pad_token": "<pad>",
|
54 |
+
"sep_token": "</s>",
|
55 |
+
"tokenizer_class": "RobertaTokenizer",
|
56 |
+
"unk_token": "<unk>"
|
57 |
+
}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|