Views
No views yet
Bamman, D., & Burns, P.J. (2020). Latin BERT: A Contextual Language Model for Classical Philology. arXiv preprint arXiv:2009.10053.
transformers.pip install transformers torch1from transformers import AutoModel, AutoTokenizer
2
3tokenizer = AutoTokenizer.from_pretrained(
4 "latincy/latin-bert", trust_remote_code=True
5)
6model = AutoModel.from_pretrained("latincy/latin-bert")
7
8inputs = tokenizer("Gallia est omnis divisa in partes tres", return_tensors="pt")
9outputs = model(**inputs)
10
11# outputs.last_hidden_state: (batch, seq_len, 768)1from transformers import AutoTokenizer, BertForMaskedLM
2import torch
3
4tokenizer = AutoTokenizer.from_pretrained(
5 "latincy/latin-bert", trust_remote_code=True
6)
7model = BertForMaskedLM.from_pretrained("latincy/latin-bert")
8
9text = "Gallia est omnis [MASK] in partes tres"
10inputs = tokenizer(text, return_tensors="pt")
11mask_idx = (inputs["input_ids"] == tokenizer.mask_token_id).nonzero(as_tuple=True)[1]
12
13with torch.no_grad():
14 logits = model(**inputs).logits
15
16top5 = logits[0, mask_idx, :].topk(5).indices.squeeze()
17for token_id in top5:
18 print(tokenizer.decode([token_id.item()]))SubwordTextEncoder, not standard
WordPiece. This repo includes a faithful reimplementation as a HuggingFace
PreTrainedTokenizer — this is why trust_remote_code=True is required.| Treebank | Accuracy |
|---|---|
| Perseus | 95.2% |
| PROIEL | 98.2% |
| ITTB | 99.2% |
| Metric | Score |
|---|---|
| P@1 | 33.1% |
| P@10 | 62.2% |
| P@50 | 74.0% |
1[components.transformer.model]
2@architectures = "spacy-transformers.TransformerModel.v3"
3name = "latincy/latin-bert"
4
5[components.transformer.model.tokenizer_config]
6trust_remote_code = true
7use_fast = falsetokenizer.encode (and
the GitHub gen_berts.py LatinTokenizer, which splits on whitespace
first), it did not drop inter-word spaces, so it escaped every space to
\32;_ and injected spurious subtokens between every word. This corrupted
masked-word predictions and broke subword→word alignment when calling
tokenizer("full sentence") (the documented usage). Both the slow and fast
tokenizers now whitespace-split first, so the native string API reproduces
the original per-word tokenization with no is_split_into_words needed.
The fast tokenizer also gained the do_lower_case normalizer it was
previously missing (it had ignored the flag, escaping capitals to
codepoints exactly as the slow tokenizer did before v1.1.1). Decode now
renders word boundaries as spaces (BERT-style; "tres." → "tres .").do_lower_case=True to tokenizer.lower() before
tokenizing. The HF PreTrainedTokenizer wrapper was missing this step, causing
uppercase characters to be escaped to their ASCII codepoints (e.g. C →
\67;), inflating token counts ~4x and producing embeddings the model was never
trained on. The tokenizer now lowercases input by default (do_lower_case=True),
matching the original pipeline behavior.SubwordTextEncoder tokenizer and
PyTorch weights as a HuggingFace PreTrainedTokenizer + safetensors model.1@article{bamman2020latin,
2 title={Latin BERT: A Contextual Language Model for Classical Philology},
3 author={Bamman, David and Burns, Patrick J},
4 journal={arXiv preprint arXiv:2009.10053},
5 year={2020}
6}