Upload 7 files
Browse files- README.md +116 -0
- config.json +48 -0
- model.safetensors +3 -0
- special_tokens_map.json +37 -0
- tokenizer.json +0 -0
- tokenizer_config.json +63 -0
- vocab.txt +0 -0
README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# DistilBERT Fine-Tuned Model for Authorship Attribution on Blog Corpus
|
| 3 |
+
|
| 4 |
+
This repository hosts a fine-tuned DistilBERT model designed for the **authorship attribution** task on the Blog Authorship Corpus dataset. The model is optimized for identifying the author of a given blog post from a subset of top contributors.
|
| 5 |
+
|
| 6 |
+
## Model Details
|
| 7 |
+
|
| 8 |
+
- **Model Architecture:** DistilBERT Base (distilbert-base-uncased)
|
| 9 |
+
- **Task:** Authorship Attribution
|
| 10 |
+
- **Dataset:** Blog Authorship Corpus (Top 10 authors selected)
|
| 11 |
+
- **Quantization:** Float16 (Post-training)
|
| 12 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
| 13 |
+
|
| 14 |
+
## Usage
|
| 15 |
+
|
| 16 |
+
### Installation
|
| 17 |
+
|
| 18 |
+
```sh
|
| 19 |
+
pip install transformers torch
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
### Loading the Model
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizerFast
|
| 26 |
+
import torch
|
| 27 |
+
|
| 28 |
+
# Load fine-tuned model
|
| 29 |
+
model_path = "fine-tuned-model"
|
| 30 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_path)
|
| 31 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(model_path)
|
| 32 |
+
|
| 33 |
+
# Set model to evaluation and convert to half precision
|
| 34 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
+
model.to(device)
|
| 36 |
+
model.eval()
|
| 37 |
+
model.half()
|
| 38 |
+
|
| 39 |
+
# Example input
|
| 40 |
+
blog_post = "Today I went to the beach and had an amazing time with friends. The sunset was breathtaking!"
|
| 41 |
+
|
| 42 |
+
# Tokenize input
|
| 43 |
+
inputs = tokenizer(blog_post, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
|
| 44 |
+
inputs = {k: v.half() if v.dtype == torch.float else v for k, v in inputs.items()}
|
| 45 |
+
|
| 46 |
+
# Make prediction
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
outputs = model(**inputs)
|
| 49 |
+
|
| 50 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 51 |
+
|
| 52 |
+
# Label mapping (example)
|
| 53 |
+
label_mapping = {
|
| 54 |
+
0: "Author_A",
|
| 55 |
+
1: "Author_B",
|
| 56 |
+
2: "Author_C",
|
| 57 |
+
3: "Author_D",
|
| 58 |
+
4: "Author_E",
|
| 59 |
+
5: "Author_F",
|
| 60 |
+
6: "Author_G",
|
| 61 |
+
7: "Author_H",
|
| 62 |
+
8: "Author_I",
|
| 63 |
+
9: "Author_J"
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
predicted_author = label_mapping[predicted_class]
|
| 67 |
+
print(f"Predicted Author: {predicted_author}")
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
## Performance Metrics
|
| 71 |
+
|
| 72 |
+
- **Accuracy:** ~78% (on validation set of top 10 authors)
|
| 73 |
+
- **Precision/Recall/F1:** Vary per class, average F1 around 0.75
|
| 74 |
+
|
| 75 |
+
## Fine-Tuning Details
|
| 76 |
+
|
| 77 |
+
### Dataset
|
| 78 |
+
|
| 79 |
+
The model is trained on a subset of the **Blog Authorship Corpus** containing blogs from the top 10 most prolific authors. Each sample is a blog post with its corresponding author label.
|
| 80 |
+
|
| 81 |
+
### Training
|
| 82 |
+
|
| 83 |
+
- **Epochs:** 3
|
| 84 |
+
- **Batch size:** 8
|
| 85 |
+
- **Evaluation strategy:** Per epoch
|
| 86 |
+
- **Learning rate:** 2e-5
|
| 87 |
+
|
| 88 |
+
### Quantization
|
| 89 |
+
|
| 90 |
+
Post-training dynamic quantization using PyTorch was applied to reduce model size and accelerate inference:
|
| 91 |
+
|
| 92 |
+
```python
|
| 93 |
+
quantized_model = torch.quantization.quantize_dynamic(
|
| 94 |
+
model, {torch.nn.Linear}, dtype=torch.qint8
|
| 95 |
+
)
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
## Repository Structure
|
| 99 |
+
|
| 100 |
+
```
|
| 101 |
+
.
|
| 102 |
+
├── model/ # Contains the fine-tuned and quantized model files
|
| 103 |
+
├── tokenizer_config/ # Tokenizer configuration and vocabulary
|
| 104 |
+
├── model.safensors/ # Safetensors version of model weights
|
| 105 |
+
├── README.md # Documentation
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
## Limitations
|
| 109 |
+
|
| 110 |
+
- The model is limited to the top 10 authors used in fine-tuning.
|
| 111 |
+
- May not generalize well to unseen authors or blogs outside the dataset distribution.
|
| 112 |
+
- Quantization may slightly affect prediction precision.
|
| 113 |
+
|
| 114 |
+
## Contributing
|
| 115 |
+
|
| 116 |
+
Contributions are welcome! If you find bugs or have suggestions for improvements, feel free to open an issue or submit a pull request.
|
config.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"activation": "gelu",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"DistilBertForSequenceClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_dropout": 0.1,
|
| 7 |
+
"dim": 768,
|
| 8 |
+
"dropout": 0.1,
|
| 9 |
+
"hidden_dim": 3072,
|
| 10 |
+
"id2label": {
|
| 11 |
+
"0": 913315,
|
| 12 |
+
"1": 1853281,
|
| 13 |
+
"2": 1904603,
|
| 14 |
+
"3": 1955799,
|
| 15 |
+
"4": 2752410,
|
| 16 |
+
"5": 2781780,
|
| 17 |
+
"6": 3019516,
|
| 18 |
+
"7": 3122872,
|
| 19 |
+
"8": 3346463,
|
| 20 |
+
"9": 3428854
|
| 21 |
+
},
|
| 22 |
+
"initializer_range": 0.02,
|
| 23 |
+
"label2id": {
|
| 24 |
+
"1853281": 1,
|
| 25 |
+
"1904603": 2,
|
| 26 |
+
"1955799": 3,
|
| 27 |
+
"2752410": 4,
|
| 28 |
+
"2781780": 5,
|
| 29 |
+
"3019516": 6,
|
| 30 |
+
"3122872": 7,
|
| 31 |
+
"3346463": 8,
|
| 32 |
+
"3428854": 9,
|
| 33 |
+
"913315": 0
|
| 34 |
+
},
|
| 35 |
+
"max_position_embeddings": 512,
|
| 36 |
+
"model_type": "distilbert",
|
| 37 |
+
"n_heads": 12,
|
| 38 |
+
"n_layers": 6,
|
| 39 |
+
"pad_token_id": 0,
|
| 40 |
+
"problem_type": "single_label_classification",
|
| 41 |
+
"qa_dropout": 0.1,
|
| 42 |
+
"seq_classif_dropout": 0.2,
|
| 43 |
+
"sinusoidal_pos_embds": false,
|
| 44 |
+
"tie_weights_": true,
|
| 45 |
+
"torch_dtype": "float16",
|
| 46 |
+
"transformers_version": "4.51.3",
|
| 47 |
+
"vocab_size": 30522
|
| 48 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:72818d55860500790b1f419b6eedc5dfa39629877d7d736805f9799b688b4b3e
|
| 3 |
+
size 133934740
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": {
|
| 3 |
+
"content": "[CLS]",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"mask_token": {
|
| 10 |
+
"content": "[MASK]",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "[PAD]",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"sep_token": {
|
| 24 |
+
"content": "[SEP]",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"unk_token": {
|
| 31 |
+
"content": "[UNK]",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
}
|
| 37 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": true,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"max_length": 256,
|
| 50 |
+
"model_max_length": 512,
|
| 51 |
+
"pad_to_multiple_of": null,
|
| 52 |
+
"pad_token": "[PAD]",
|
| 53 |
+
"pad_token_type_id": 0,
|
| 54 |
+
"padding_side": "right",
|
| 55 |
+
"sep_token": "[SEP]",
|
| 56 |
+
"stride": 0,
|
| 57 |
+
"strip_accents": null,
|
| 58 |
+
"tokenize_chinese_chars": true,
|
| 59 |
+
"tokenizer_class": "DistilBertTokenizer",
|
| 60 |
+
"truncation_side": "right",
|
| 61 |
+
"truncation_strategy": "longest_first",
|
| 62 |
+
"unk_token": "[UNK]"
|
| 63 |
+
}
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|