kd13 commited on
Commit
664d6f8
·
verified ·
1 Parent(s): c600bf2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +75 -3
README.md CHANGED
@@ -1,3 +1,75 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ datasets:
4
+ - kd13/bookcorpus-clean
5
+ language:
6
+ - en
7
+ metrics:
8
+ - perplexity
9
+ pipeline_tag: fill-mask
10
+ library_name: transformers
11
+ tags:
12
+ - mlm
13
+ ---
14
+
15
+ # BERTsmall — Custom BERT with RoPE & Pre-LN Trained from Scratch
16
+
17
+ A compact BERT-style masked language model trained entirely from scratch on BookCorpus. The architecture replaces the canonical absolute positional embeddings with **Rotary Position Embeddings (RoPE)** and adopts a **Pre-Layer Normalization** (Pre-LN) residual layout, both of which have become standard practice in modern transformer training.
18
+
19
+ ---
20
+
21
+ ### Architecture Design Choices
22
+
23
+ **RoPE instead of learned absolute position embeddings.** Rotary embeddings encode positional information directly into the query–key dot product, enabling length generalisation beyond the training window and eliminating a separate learnable parameter table.
24
+
25
+ **Pre-LN residual stream.** Layer normalisation is applied to the *input* of each sub-layer rather than the output. This stabilises gradient flow during early training and generally makes the loss curve smoother, at the cost of requiring an explicit final encoder normalisation before the prediction head.
26
+
27
+ **Embedding tying.** The MLM decoder projection matrix shares weights with the token embedding table, which reduces parameter count and typically improves token prediction quality.
28
+
29
+ ---
30
+
31
+ ## Training Details
32
+
33
+ ### Dataset
34
+
35
+ | Split | Source | Packing |
36
+ |---|---|---|
37
+ | Train | BookCorpus | Fixed-length packed sequences (128 tokens) |
38
+ | Eval | BookCorpus (held-out) | Same packing strategy |
39
+
40
+ Sequences were packed (not padded) to maximise token utilisation per batch, following the approach used in the original BERT paper.
41
+
42
+ ### Pre-training Objective
43
+
44
+ Masked Language Modelling (MLM) with a masking probability of **30 %**. The standard 80/10/10 mask/replace/keep strategy is applied by `DataCollatorForLanguageModeling`.
45
+
46
+ ## Evalution Details
47
+
48
+ Perplexity : 7.63
49
+
50
+ ---
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ import torch
56
+ from transformers import AutoTokenizer, AutoModelForMaskedLM
57
+
58
+ model_name = "kd13/RoPERT-MLM-small"
59
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
60
+ model = AutoModelForMaskedLM.from_pretrained(model_name, trust_remote_code=True)
61
+
62
+ text = "i don't have much [MASK]."
63
+ inputs = tokenizer(text, return_tensors="pt")
64
+
65
+ with torch.no_grad():
66
+ logits = model(**inputs).logits
67
+
68
+ mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
69
+ mask_token_logits = logits[0, mask_token_index, :]
70
+ top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist()
71
+
72
+ for token in top_5_tokens:
73
+ print(f">>> {text.replace('[MASK]', tokenizer.decode([token]))}")
74
+
75
+ ```