File size: 4,595 Bytes
c6ee1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2850336
c6ee1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2850336
c6ee1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Alpha Transformer

A custom **Encoder-Decoder Transformer model trained from scratch** for English β†’ French translation.

Alpha is a 51M parameter Transformer implemented entirely in PyTorch, including:
- Multi-Head Attention
- Rotary Positional Embeddings (RoPE)
- Encoder-Decoder architecture
- Cross Attention
- Custom tokenizer
- Mixed precision training
- Checkpointing system

The model was trained on the **OPUS-100 English-French translation dataset**.

---

## Model Overview

| Feature | Value |
|---|---|
| Architecture | Encoder-Decoder Transformer |
| Parameters | 51.26M |
| Framework | PyTorch |
| Task | English β†’ French Translation |
| Dataset | OPUS-100 |
| Training Samples | ~1M sentences |
| Embedding Dimension | 256 |
| Attention Heads | 8 |
| Feed Forward Dimension | 1024 |
| Encoder Layers | 3 |
| Decoder Layers | 3 |
| Maximum Sequence Length | 64 tokens |
| Positional Encoding | RoPE |

---

# Performance

Evaluation on the OPUS-100 test set:

| Metric | Result |
|---|---|
| Token Accuracy | 62.8% |

The model was trained from scratch without using pretrained weights.

---

# Architecture

## Encoder

Each encoder block contains:

Input Embeddings
|
v
LayerNorm
|
Multi-Head Self Attention + RoPE
|
Residual Connection
|
LayerNorm
|
Feed Forward Network
|
Residual Connection

## Decoder

Each decoder block contains:

Target Embeddings
|
LayerNorm
|
Masked Self Attention + RoPE
|
Residual Connection
|
LayerNorm
|
Cross Attention
|
Residual Connection
|
LayerNorm
|
Feed Forward Network
|
Residual Connection

---

# Files


Alpha/
β”‚
β”œβ”€β”€ config.json
β”‚ └── Model architecture configuration
β”‚
β”œβ”€β”€ model.safetensors
β”‚ └── Trained model weights
β”‚
β”œβ”€β”€ src_vocab.json
β”‚ └── English vocabulary
β”‚
β”œβ”€β”€ tgt_vocab.json
β”‚ └── French vocabulary
β”‚
β”œβ”€β”€ tokenizer.py
β”‚ └── Custom tokenizer implementation
β”‚
β”œβ”€β”€ main.py
β”‚ └── Transformer architecture
β”‚
β”œβ”€β”€ inference.py
β”‚ └── Translation script
β”‚
└── README.md

---

# Installation

Clone the repository:

```bash
git clone https://github.com/MehdiSkilll24/Alpha.git
cd Alpha

Install dependencies:

pip install torch safetensors

Usage

Run inference:

python inference.py

Example:

Input:
> hello, how are you?

Output:
> bonjour, comment allez-vous ?



Loading the Model Manually
import torch
import json
from safetensors.torch import load_file
from main import Transformer

with open("config.json") as f:
    config = json.load(f)

weights = load_file("model.safetensors")

model = Transformer(
    src_vocab_size=config["src_vocab_size"],
    tgt_vocab_size=config["tgt_vocab_size"],
    d_model=config["d_model"],
    num_heads=config["num_heads"],
    d_ff=config["d_ff"]
)

model.load_state_dict(weights)
model.eval()


Training

The model was trained using:

AdamW optimizer
Mixed precision training (FP16)
ReduceLROnPlateau scheduler
Custom PyTorch training loop

Training configuration:

Learning Rate: 3e-4
Batch Size: 32
Epochs: 18
Dataset

Training data:

OPUS-100 English-French translation dataset.

The dataset contains parallel English/French sentence pairs collected from various sources.

Limitations

This model is designed as an educational and experimental Transformer implementation.

Limitations:

Vocabulary is based on a custom word-level tokenizer
Limited context length
No pretrained initialization
Smaller dataset compared to production translation systems
Token accuracy does not directly represent BLEU score
Future Improvements

Possible improvements:

Replace word tokenizer with BPE/SentencePiece
Increase model depth
Train on larger datasets
Add beam search decoding
Evaluate with BLEU / COMET
Implement KV-cache for faster inference
Motivation

Alpha was built to understand and implement the Transformer architecture from first principles.

Rather than using existing libraries, the project implements the core components manually:

Attention calculations
Positional embeddings
Encoder/decoder blocks
Training pipeline
Model serialization

The goal is to better understand how modern language models are built internally.

Source Code

GitHub:

https://github.com/MehdiSkilll24/Alpha

Model weights:

https://huggingface.co/MehdiSkilll24/Alpha

License

MIT License


One suggestion before you upload: add **3-5 real translation examples** from your model. That will make the HF page look 10x more alive. A model card with "62.8% accuracy" is okay; a model card with:


English:
"The weather is beautiful today."

Alpha:
"Le temps est magnifique aujourd'hui."