Upload 7 files
Browse filesFiles changed (7)
- README.mdAdded+99
- config.jsonAdded+39
- merges.txtAdded
- model.safetensorsAdded+3
- special_tokens_map.jsonAdded+51
- tokenizer_config.jsonAdded+57
- vocab.jsonAdded
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899# RoBERTa-Based Topic Classification Model Using AG News Dataset
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.
## Model Details
- **Model Architecture:** RoBERTa (roberta-base)- **Task:** Topic Classification- **Dataset:** AG News (from Hugging Face Datasets)- **Fine-tuning Framework:** Hugging Face Transformers
## Usage
### Installation
```shpip install transformers datasets torch```
### Loading and Predicting
```pythonfrom transformers import RobertaTokenizer, RobertaForSequenceClassificationimport torch
# Load model and tokenizermodel = RobertaForSequenceClassification.from_pretrained("your-saved-model-directory")tokenizer = RobertaTokenizer.from_pretrained("roberta-base")model.eval()
# Sample predictiondef predict_topic(texts, model, tokenizer, device='cpu'): import re if isinstance(texts, str): texts = [texts]
def preprocess(text): text = text.lower() text = re.sub(r"http\S+|www\S+|https\S+", '', text) text = re.sub(r'\@\w+|\#', '', text) text = re.sub(r"[^a-zA-Z0-9\s.,!?']", '', text) text = re.sub(r'\s+', ' ', text).strip() return text
cleaned_texts = [preprocess(t) for t in texts] inputs = tokenizer(cleaned_texts, padding=True, truncation=True, return_tensors="pt").to(device)
model.to(device) model.eval() with torch.no_grad(): outputs = model(**inputs) preds = torch.argmax(outputs.logits, dim=1).tolist()
label_map = {0: "World", 1: "Sports", 2: "Business", 3: "Sci/Tech"} return [label_map[p] for p in preds]
# Examplesample_texts = [ "The stock market witnessed a major crash today due to inflation concerns.", "The new space telescope has captured unprecedented images of distant galaxies."]results = predict_topic(sample_texts, model, tokenizer)for text, label in zip(sample_texts, results): print(f"Text: {text}\nPredicted Topic: {label}\n")```
## Performance Metrics
- **Accuracy:** ~0.96 on AG News test split
## Fine-Tuning Details
### Dataset
The dataset is sourced from the AG News dataset available via Hugging Face Datasets.
### Training
- Number of epochs: 3- Batch size: 8- Evaluation strategy: epoch- Learning rate: 2e-5
## Repository Structure
```.βββ model/ # Contains the fine-tuned model filesβββ tokenizer/ # Tokenizer configuration and vocabβββ README.md # Model documentation```
## Limitations
- The model is trained specifically for AG News-style texts and may not generalize well to informal or unrelated domains.
## Contributing
Contributions are welcome! Please open an issue or submit a PR for suggestions or improvements.123456789101112131415161718192021222324252627282930313233343536373839{ "architectures": [ "RobertaForSequenceClassification" ], "attention_probs_dropout_prob": 0.1, "bos_token_id": 0, "classifier_dropout": null, "eos_token_id": 2, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 768, "id2label": { "0": "LABEL_0", "1": "LABEL_1", "2": "LABEL_2", "3": "LABEL_3" }, "initializer_range": 0.02, "intermediate_size": 3072, "label2id": { "LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2, "LABEL_3": 3 }, "layer_norm_eps": 1e-05, "max_position_embeddings": 514, "model_type": "roberta", "num_attention_heads": 12, "num_hidden_layers": 12, "pad_token_id": 1, "position_embedding_type": "absolute", "problem_type": "single_label_classification", "torch_dtype": "float16", "transformers_version": "4.51.3", "type_vocab_size": 1, "use_cache": true, "vocab_size": 50265}123version https://git-lfs.github.com/spec/v1oid sha256:5f9a9ada76280e737a482c2bc9391d8334408ea67a6a83827faea171d85f1d8esize 249321504123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051{ "bos_token": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "cls_token": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "eos_token": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "mask_token": { "content": "<mask>", "lstrip": true, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<pad>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "sep_token": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "unk_token": { "content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657{ "add_prefix_space": false, "added_tokens_decoder": { "0": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "1": { "content": "<pad>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "2": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "3": { "content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "50264": { "content": "<mask>", "lstrip": true, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": "<s>", "clean_up_tokenization_spaces": false, "cls_token": "<s>", "eos_token": "</s>", "errors": "replace", "extra_special_tokens": {}, "mask_token": "<mask>", "model_max_length": 512, "pad_token": "<pad>", "sep_token": "</s>", "tokenizer_class": "RobertaTokenizer", "unk_token": "<unk>"}README.mdAdded+99
config.jsonAdded+39
merges.txtAdded
model.safetensorsAdded+3
special_tokens_map.jsonAdded+51
tokenizer_config.jsonAdded+57
vocab.jsonAdded