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|
+
|
| 5 |
+
## Model Overview
|
| 6 |
+
|
| 7 |
+
**Model Name:** BERT-Text2Date
|
| 8 |
+
**Model Type:** BERT (Encoder-only architecture)
|
| 9 |
+
**Language:** Persian
|
| 10 |
+
|
| 11 |
+
**Description:**
|
| 12 |
+
This model is designed to process and generate Persian dates in both formal (YYYY-MM-DD) and informal formats. It utilizes a dataset that includes various representations of dates, allowing for effective training in understanding and predicting Persian date formats.
|
| 13 |
+
|
| 14 |
+
## Dataset
|
| 15 |
+
|
| 16 |
+
**Dataset Description:**
|
| 17 |
+
The dataset consists of two types of dates: formal and informal. It is generated using two main functions:
|
| 18 |
+
|
| 19 |
+
- **`convert_year_to_persian(year)`**: Converts years to Persian format, currently supporting the year 1400.
|
| 20 |
+
- **`generate_date_mappings_with_persian_year(start_year, end_year)`**: Generates dates for a specified range, considering the number of days in each month.
|
| 21 |
+
|
| 22 |
+
**Data Formats:**
|
| 23 |
+
|
| 24 |
+
- **Informal Dates:** Various formats like “روز X ماه سال” and “اول/دوم/… ماه سال”.
|
| 25 |
+
- **Formal Dates:** Stored in YYYY-MM-DD format.
|
| 26 |
+
|
| 27 |
+
**Example Dates:**
|
| 28 |
+
|
| 29 |
+
- بیست و هشتم اسفند هزار و چهار صد و ده, 1410-12-28
|
| 30 |
+
- 1 فروردین 1400, 1400-01-01
|
| 31 |
+
|
| 32 |
+
**Data Split:**
|
| 33 |
+
|
| 34 |
+
- **Training Set:** 80% (19272 samples)
|
| 35 |
+
- **Validation Set:** 10% (2409 samples)
|
| 36 |
+
- **Test Set:** 10% (2409 samples)
|
| 37 |
+
|
| 38 |
+
## Model Architecture
|
| 39 |
+
|
| 40 |
+
**Architecture Details:**
|
| 41 |
+
The model is built using an encoder-only architecture, consisting of:
|
| 42 |
+
|
| 43 |
+
- **Layers:** 4 Encoder layers
|
| 44 |
+
- **Parameters:**
|
| 45 |
+
- `vocab_size`: 25003
|
| 46 |
+
- `context_length`: 32
|
| 47 |
+
- `emb_dim`: 256
|
| 48 |
+
- `n_heads`: 4
|
| 49 |
+
- `drop_rate`: 0.1
|
| 50 |
+
|
| 51 |
+
**Parameter Count:** 14,933,931
|
| 52 |
+
|
| 53 |
+
```
|
| 54 |
+
Transformer( (embedding): Embedding(25003, 256) (positional_encoding): Embedding(32, 256) (en): TransformerEncoder( (layers): ModuleList( (0-3): 4 x TransformerEncoderLayer( (self_attn): MultiheadAttention( (out_proj): NonDynamicallyQuantizableLinear(in_features=256, out_features=256, bias=False) ) (linear1): Linear(in_features=256, out_features=512, bias=False) (dropout): Dropout(p=0.1, inplace=False) (linear2): Linear(in_features=512, out_features=256, bias=False) (norm1): LayerNorm((256,), eps=1e-05, elementwise_affine=True) (norm2): LayerNorm((256,), eps=1e-05, elementwise_affine=True) (dropout1): Dropout(p=0.1, inplace=False) (dropout2): Dropout(p=0.1, inplace=False) ) ) ) (fc_train): Linear(in_features=256, out_features=25003, bias=True) )
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
**Tokenizer:**
|
| 58 |
+
The model uses a Persian tokenizer named “بلبل زبان” available on Hugging Face, with a vocabulary size of 25,000 tokens.
|
| 59 |
+
|
| 60 |
+
## Training
|
| 61 |
+
|
| 62 |
+
**Training Process:**
|
| 63 |
+
|
| 64 |
+
- **Batch Size:** 2048
|
| 65 |
+
- **Epochs:** 60
|
| 66 |
+
- **Learning Rate:** 0.00005
|
| 67 |
+
- **Optimizer:** AdamW
|
| 68 |
+
- **Weight Decay:** 0.2
|
| 69 |
+
- **Masking Technique:** The formal part of the date is masked to facilitate learning.
|
| 70 |
+
|
| 71 |
+
**Performance Metrics:**
|
| 72 |
+
|
| 73 |
+
- **Training Loss:** Reduced from 10.3 to 0.005 over 60 epochs.
|
| 74 |
+
- **Validation Loss:** Reduced from 10.1 to 0.010.
|
| 75 |
+
- **Test Accuracy:** 66% (exact match required).
|
| 76 |
+
- **Perplexity:** 1.01
|
| 77 |
+
|
| 78 |
+
## Inference
|
| 79 |
+
|
| 80 |
+
**Inference Code:**
|
| 81 |
+
The model can be loaded along with the tokenizer using the provided `Inference.ipynb` file. Three functions are implemented:
|
| 82 |
+
|
| 83 |
+
1. **Convert Token IDs to Text**
|
| 84 |
+
```python
|
| 85 |
+
def text_to_token_ids(text, tokenizer):
|
| 86 |
+
|
| 87 |
+
encoded = tokenizer.encode(text)
|
| 88 |
+
|
| 89 |
+
encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
|
| 90 |
+
|
| 91 |
+
return encoded_tensor
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
2. **Convert Text to Token IDs**
|
| 95 |
+
```python
|
| 96 |
+
def token_ids_to_text(token_ids, tokenizer):
|
| 97 |
+
|
| 98 |
+
flat = token_ids.squeeze(0) # remove batch dimension
|
| 99 |
+
|
| 100 |
+
return tokenizer.decode(flat.tolist())
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
3. **`predict_masked(input)`**: Takes an input to predict the masked date.
|
| 104 |
+
```python
|
| 105 |
+
def predict_masked(model,tokenizer,input,deivce):
|
| 106 |
+
|
| 107 |
+
model.eval()
|
| 108 |
+
|
| 109 |
+
inputs_masked = input + " " + "[MASK][MASK][MASK][MASK]-[MASK][MASK]-[MASK][MASK]"
|
| 110 |
+
|
| 111 |
+
input_ids = tokenizer.encode(inputs_masked)
|
| 112 |
+
|
| 113 |
+
input_ids = torch.tensor(input_ids).to(deivce)
|
| 114 |
+
|
| 115 |
+
with torch.no_grad():
|
| 116 |
+
|
| 117 |
+
logits = model(input_ids.unsqueeze(0))
|
| 118 |
+
|
| 119 |
+
logits = logits.flatten(0, 1)
|
| 120 |
+
|
| 121 |
+
probs = torch.argmax(logits,dim=-1,keepdim=True)
|
| 122 |
+
|
| 123 |
+
token_ids = probs.squeeze(1)
|
| 124 |
+
|
| 125 |
+
answer_ids = token_ids[-11:-1]
|
| 126 |
+
|
| 127 |
+
return token_ids_to_text(answer_ids,tokenizer)
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
And use:
|
| 131 |
+
```python
|
| 132 |
+
predict_masked(model,tokenizer,"12 آبان 1402","cuda")
|
| 133 |
+
```
|
| 134 |
+
Output:
|
| 135 |
+
```
|
| 136 |
+
'1402-08-12'
|
| 137 |
+
```
|
| 138 |
+
## Limitations
|
| 139 |
+
|
| 140 |
+
- The model currently only supports Persian dates for the year 1400-1410, with potential for expansion.
|
| 141 |
+
- Performance may vary with dates outside the training dataset.
|
| 142 |
+
|
| 143 |
+
## Intended Use
|
| 144 |
+
|
| 145 |
+
This model is intended for applications requiring date recognition and generation in Persian, such as natural language processing tasks, chatbots, or educational tools.
|
| 146 |
+
|
| 147 |
+
## Acknowledgements
|
| 148 |
+
|
| 149 |
+
- Special thanks to the developers of the “بلبل زبان” tokenizer and the contributors to the dataset.
|