yadelew commited on
Commit
39ec07b
·
verified ·
1 Parent(s): e6b34e8

Update README

Browse files
Files changed (1) hide show
  1. README.md +86 -109
README.md CHANGED
@@ -1,111 +1,88 @@
 
1
  language:
2
-
3
- amh
4
  tags:
5
- amharic
6
- word-embeddings
7
- skip-gram
8
- natural-language-processing
9
- from-scratch
10
- Amharic Skip-gram Word Embeddings from Scratch
11
-
12
- A small Amharic word-embedding model trained using a manually implemented Skip-gram neural network in Python.
13
-
14
- Overview
15
-
16
- This project implements a Skip-gram model from scratch to learn word representations from a small Amharic text corpus. The goal is to understand and implement the underlying mathematics, including the forward pass, softmax, cross-entropy loss, analytical gradients, and stochastic gradient descent.
17
-
18
- The implementation uses Python lists, loops, math, and random. It does not rely on pretrained embeddings, NumPy, automatic differentiation, or neural-network libraries.
19
-
20
- Model details
21
- Property Value
22
- Language Amharic
23
- Architecture Skip-gram with softmax
24
- Vocabulary size 271
25
- Training pairs 1,976
26
- Embedding dimension 10
27
- Context window 2
28
- Learning rate 0.05
29
- Maximum epochs 50
30
- Random seed 42
31
- Best checkpoint Epoch 49
32
-
33
- The model learns an input embedding matrix E and output weight matrix U. For a center word, it predicts surrounding context words using a softmax distribution over the vocabulary.
34
-
35
- Training and evaluation results
36
- Metric Result
37
- Initial mean training loss 5.602120
38
- Best mean training loss 2.798836
39
- Best epoch 49
40
- Epochs executed 50
41
- Reported maximum absolute gradient-check error 0.00000000
42
-
43
- The best mean training loss was approximately 50% lower than the initial mean training loss. The best checkpoint was restored after epoch 50 produced a slightly higher loss.
44
-
45
- These are training and implementation-validation results. They are not held-out test metrics, and they do not establish generalization to unseen Amharic text.
46
-
47
- Correctness checks
48
-
49
- The implementation passed the following checks:
50
-
51
- Context-window pair generation.
52
- Sentence-boundary protection during pair generation.
53
- Forward scores, softmax probabilities, and cross-entropy loss against a known numerical example.
54
- Analytical gradient values against the assignment's expected values.
55
- One stochastic-gradient-descent update, including verification that unrelated input-embedding rows remain unchanged.
56
- Numerical stability with an extreme score.
57
- Softmax and loss invariance when a constant is added to every score.
58
- Numerical gradient checking using finite differences with epsilon 1e-5.
59
- Cosine similarity handling for zero-norm vectors.
60
- Model save/reload consistency for vocabulary, parameters, probabilities, and nearest-neighbor order.
61
- Example use
62
-
63
- The repository includes a custom Python implementation rather than a Transformers-compatible model. Load the saved JSON with the accompanying model.py implementation:
64
-
65
- from model import SkipGramModel, inverse_vocabulary
66
-
67
- model, word_to_id = SkipGramModel.load_json(
68
- "amharic_skipgram.json"
69
- )
70
- id_to_word = inverse_vocabulary(word_to_id)
71
-
72
- word = "ቴክኖሎጂ"
73
-
74
- if word in word_to_id:
75
- word_id = word_to_id[word]
76
- neighbors = model.nearest_neighbors(word_id, top_k=3)
77
-
78
- for neighbor_id, similarity in neighbors:
79
- print(id_to_word[neighbor_id], round(similarity, 6))
80
- else:
81
- print("Unknown word:", word)
82
-
83
- The similarity values are cosine similarities between learned input embeddings.
84
-
85
- Intended uses
86
- Educational demonstrations of word embeddings and Skip-gram.
87
- Learning about softmax, cross-entropy, gradient calculations, and SGD.
88
- Small experiments with Amharic word-context relationships.
89
- A starting point for further experimentation with Ethiopian-language NLP.
90
- Limitations
91
- The corpus is small, and the vocabulary contains only 271 unique words.
92
- The embedding dimension is only 10.
93
- Nearest neighbors can be noisy and may reflect local context or grammatical patterns rather than semantic similarity.
94
- The model has not been evaluated on a held-out benchmark or downstream NLP task.
95
- This is an educational prototype, not a production-ready or general-purpose Amharic language model.
96
- Words not present in the vocabulary cannot be queried.
97
- Dataset and reproducibility
98
-
99
- The model was trained on a small Amharic corpus organized around multiple everyday topics. The vocabulary, preprocessing, pair-generation logic, model implementation, and training configuration are documented in the project source code.
100
-
101
- Training configuration: window size 2, embedding dimension 10, learning rate 0.05, maximum epochs 50, and random seed 42.
102
-
103
- Future work
104
- Expand the corpus with diverse, properly licensed Amharic text.
105
- Evaluate on held-out text and compare multiple context-window sizes and embedding dimensions.
106
- Compare nearest neighbors with judgments from fluent Amharic speakers.
107
- Add a reproducible training-loss plot and configuration comparison.
108
- Explore subword handling and improved Amharic text normalization.
109
- Acknowledgment
110
-
111
- Built as an educational assignment to understand and implement Skip-gram word embeddings from first principles.
 
1
+ ---
2
  language:
3
+ - am
4
+ license: mit
5
  tags:
6
+ - amharic
7
+ - word-embeddings
8
+ - skip-gram
9
+ - natural-language-processing
10
+ - from-scratch
11
+ - custom-implementation
12
+ metrics:
13
+ - loss
14
+ library_name: custom
15
+ ---
16
+
17
+ # Amharic Skip-gram Word Embeddings from Scratch
18
+
19
+ A lightweight, pure-Python implementation of a Skip-gram word embedding model trained on a small Amharic corpus. Built entirely from first principles without external numerical or machine learning libraries (no NumPy, PyTorch, or TensorFlow).
20
+
21
+ ## Overview
22
+
23
+ This project implements a Skip-gram neural network to learn vector representations (word embeddings) for Amharic words. The primary goal is educational: to understand and verify the core mathematical foundations of word embeddings, including:
24
+ - Forward propagation and softmax distribution calculation
25
+ - Cross-entropy loss formulation
26
+ - Analytical gradient derivation for input and output weight matrices
27
+ - Stochastic Gradient Descent (SGD) optimization from scratch
28
+
29
+ The implementation relies strictly on Python standard library modules (`math`, `random`, lists, and loops).
30
+
31
+ ---
32
+
33
+ ## Model Architecture & Training Parameters
34
+
35
+ | Hyperparameter | Value |
36
+ | :--- | :--- |
37
+ | **Language** | Amharic (`am`) |
38
+ | **Architecture** | Skip-gram with Softmax |
39
+ | **Vocabulary Size** | 271 unique tokens |
40
+ | **Training Pairs** | 1,976 |
41
+ | **Embedding Dimension** | 10 |
42
+ | **Context Window Size** | 2 |
43
+ | **Learning Rate** | 0.05 |
44
+ | **Maximum Epochs** | 50 |
45
+ | **Random Seed** | 42 |
46
+ | **Best Checkpoint** | Epoch 49 |
47
+
48
+ The model optimizes two matrices: an input embedding matrix $E$ and an output context weight matrix $U$. For a given center word, it predicts surrounding context words via a softmax probability distribution over the vocabulary.
49
+
50
+ ---
51
+
52
+ ## Training & Validation Results
53
+
54
+ | Metric | Result |
55
+ | :--- | :--- |
56
+ | **Initial Mean Training Loss** | 5.602120 |
57
+ | **Best Mean Training Loss** | 2.798836 |
58
+ | **Best Epoch** | 49 |
59
+ | **Epochs Executed** | 50 |
60
+ | **Max Absolute Gradient-Check Error** | 0.00000000 |
61
+
62
+ > **Note:** The best mean training loss achieved a ~50% reduction from the initial loss. Epoch 49 was selected via early checkpointing as epoch 50 yielded a slightly higher loss value. These metrics validate implementation correctness on the training split and do not reflect generalization performance on unseen text.
63
+
64
+ ---
65
+
66
+ ## Implementation & Correctness Checks
67
+
68
+ The model passed a rigorous suite of mathematical and algorithmic unit tests:
69
+
70
+ - [x] **Data Processing:** Correct context-window pair generation with strict sentence-boundary protection.
71
+ - [x] **Forward & Loss Verification:** Forward scores, softmax probabilities, and cross-entropy loss validated against manual reference calculations.
72
+ - [x] **Analytical Gradients:** Exact match with expected symbolic gradients.
73
+ - [x] **SGD Updates:** Verified parameter updates, ensuring non-context input embedding rows remain unmodified during sparse updates.
74
+ - [x] **Numerical Stability:** Verified stability under extreme score magnitudes and shift invariance ($\text{softmax}(z) = \text{softmax}(z + c)$).
75
+ - [x] **Gradient Checking:** Finite-difference numerical gradient verification ($\epsilon = 10^{-5}$).
76
+ - [x] **Edge Cases:** Safe cosine similarity evaluation for zero-norm vectors.
77
+ - [x] **Serialization:** Full JSON round-trip consistency for vocabulary mapping, weight arrays, and nearest-neighbor outputs.
78
+
79
+ ---
80
+
81
+ ## Usage
82
+
83
+ This model uses a custom Python execution format rather than Hugging Face `transformers`. Download `amharic_skipgram.json` alongside your `model.py` script to run inference.
84
+
85
+ ### Python Code Example
86
+
87
+ ```python
88
+ from model import SkipGramModel