Character-Level Language Models of the Hebrew Torah and the King James Bible
This page presents two language models, TorahGPT and KJVGPT, trained by the author on the Hebrew text of the Five Books of Moses and on the King James Version of the Bible respectively, and on nothing else. Each is a small transformer that assigns a probability to every possible next character, given the 128 characters that precede it, and writes by sampling from those probabilities one character at a time. Both models run entirely in the reader’s browser.
The models are not intended to produce meaningful text, and they do not. Their interest is that of a controlled comparison: two models of identical architecture, trained under identical conditions on corpora that differ in size by an order of magnitude, exhibit in their output and in their measured losses the effect of the quantity of training data. The Hebrew corpus also raises a question that does not arise in ordinary language modeling, namely how a model of the Torah should treat the divine names; the treatment adopted here is described below.
The texts
The Hebrew text is that of the Westminster Leningrad Codex, as published with morphological annotation by the Open Scriptures Hebrew Bible project. In that edition the five books comprise 5,853 verses and 304,850 letters, against the traditional count of 304,805; manuscripts differ slightly in spelling. Vowel points and cantillation marks are removed, leaving the consonantal text as it is written in a Torah scroll, and where the written and the read forms of a word differ, the written form is retained. Words are separated by spaces and verses by line breaks. The resulting text has 381,224 characters, drawn from an alphabet of 31 symbols: the 27 letter forms, the space, the line break, and the two marks used in the substitutes for the divine names.
The English text is the King James Version as compiled in the thiagobodruk/bible collection, comprising 31,102 verses. Remnants of the translators’ marginal notes that had entered the text of some verses are removed, and typographic characters are replaced by their keyboard equivalents, so that text typed by a reader is drawn from the same alphabet as the training text. The resulting text has 4,138,674 characters, drawn from an alphabet of 63 symbols.
The divine names
In Jewish law a divine name, once written, may not be erased, and in writing other than sacred texts it is customary to replace the names with conventional substitutes. TorahGPT is trained on text in which this has been done, so that it never encounters the names themselves. The difficulty is that several names, once the vowels are removed, are spelled exactly as ordinary words: El as the preposition “to,” and Adonai as “my lord” addressed to a person. The two are distinguished by means of the Strong’s numbers with which the Open Scriptures edition annotates every word. The name Ehyeh, which in Exodus 3:14 is annotated as the verb “to be,” is identified by its verse. Each name is written with the substitute customary in Orthodox writing, as follows, with the number of its occurrences in the Torah.
| The Tetragrammaton | ה׳ | 1,820 |
| Elohim, with its inflected forms | אלקים | 812 |
| El | קל | 49 |
| Adonai | אדנ-י | 18 |
| Shaddai | שד-י | 9 |
| Ehyeh | אהי-ה | 3 |
| Eloah | אלוק | 2 |
| Yah | י-ה | 2 |
The page applies the same substitutions to all Hebrew it displays, whether typed by the reader, returned by the translation service, or written by the model. Without the annotation, only those names that cannot be mistaken for ordinary words can be recognized, so this second measure covers the Tetragrammaton and the forms of Elohim and Eloah; the remaining names are excluded by the training text alone.
The models
Each model is a decoder-only transformer (Vaswani et al., 2017) operating on characters. A character is represented by a learned vector of 128 dimensions, to which a learned vector representing its position is added. These representations pass through four blocks, each comprising causal self-attention with four heads and a feed-forward layer, and a final linear map assigns a score to every symbol of the alphabet. Dropout with probability 0.2 is applied throughout. TorahGPT has 817,695 parameters and KJVGPT 825,919, the difference arising from the sizes of their alphabets.
Training
The verses of each text are divided into consecutive blocks of one hundred, and the first block of every ten is withheld from training, so that 10.3 percent of the verses of each text are reserved for evaluation. In the Torah the withheld passages are Genesis 1:1–4:20 and 34:20–37:16, Exodus 18:1–21:22, Leviticus 11:3–13:47, Numbers 10:12–13:24, and Deuteronomy 3:24–6:12. Of the ten possible choices of withheld block, this one minimizes the proportion of withheld text that occurs verbatim in the training text: 2.8 percent of its characters lie within a stretch of thirty characters that also occurs in the training text, against between 3.8 and 9.4 percent for the other choices. The consideration matters in the Torah, whose formulas and parallel passages, such as the offerings of the twelve tribal leaders recorded in nearly identical words in Numbers 7, would otherwise allow memorization to pass for generalization.
Each model was trained with the AdamW optimizer at a learning rate of 0.001, on batches of 32 passages of 128 characters, for 5,000 steps, on the processor of a laptop; neither required more than an hour. The loss on withheld text was estimated every 250 steps, and the parameters with the lowest estimate were retained.
Results
Losses are given in bits per character: the mean, over a text, of −log2 of the probability assigned to each character before it was observed. Every character is scored, in consecutive windows of 128 characters. The counting model predicts each character from the frequencies with which characters followed the same preceding characters in the training text, with add-one smoothing; the figure given is that for the best number of preceding characters, three for the Torah and four for the King James Version.
| TorahGPT | KJVGPT | |
|---|---|---|
| Characters of training text | 342,855 | 3,717,525 |
| Loss on withheld text | 2.494 | 1.698 |
| Loss on training text | 2.113 | 1.669 |
| Counting model, withheld text | 2.876 | 1.962 |
| Uniform distribution | 4.954 | 5.977 |
Both models improve upon the counting model, TorahGPT by 0.38 bits per character and KJVGPT by 0.26. The principal difference between them lies in the gap between their losses on training and on withheld text. TorahGPT predicts the text on which it was trained better by 0.38 bits per character, an indication that it has memorized part of its comparatively small corpus; for KJVGPT, trained on more than ten times as much text, the gap is 0.03. Losses are not comparable between the two languages, since a character of unvocalized Hebrew carries more information than a character of English.
Generation
To write, a model computes the probabilities of the next character, one character is drawn at random according to them and appended to the text, and the process is repeated. The temperature divides the scores before they are converted into probabilities: values below one favor the most probable characters, and values above one give improbable characters a greater chance. The settings offered range from 0.5 to 1.3. A passage continues until the chosen number of characters has been written and the current verse is complete, and it is displayed one word at a time, so that each word can be examined for divine names before it is shown.
The three lengths offered, 248, 365, and 613 characters, are the traditional counts of the positive commandments, the negative commandments, and all the commandments of the Torah. The Talmud (Makkot 23b) relates the 365 prohibitions to the days of the solar year and the 248 positive commandments to the limbs of the human body.
Translation
English typed into TorahGPT is translated into Hebrew before it is given to the model, and each verse the model writes is translated into English once it is complete. Both translations are provided by the free MyMemory service, to which the English typed by the reader and the Hebrew written by the model are sent; nothing else leaves the browser. Machine translation tends to render invented words and ungrammatical sequences as fluent English, and the Hebrew should be regarded as the authoritative text.
Relation to earlier work
Shannon (1951) estimated the information content of English by asking readers to predict text one letter at a time, obtaining between about 0.6 and 1.3 bits per character; the losses above are the corresponding quantity for these models. Character-level language models trained on a single small text were popularized by Karpathy’s char-rnn (2015); the design used here follows the character-level example of his later nanoGPT, with a transformer in place of a recurrent network. The counting of the letters of the Torah is considerably older: the Talmud (Kiddushin 30a) records that the early scribes were called soferim, “counters,” because they counted the letters of the Torah.
Limitations
- The models produce no meaning. They have learned which characters tend to follow which, and nothing of what the texts say.
- A model takes account of only the preceding 128 characters, about two verses of the Torah or one of the King James Version.
- TorahGPT has memorized part of its training text, as the gap between its losses on training and on withheld text shows.
- In the King James Version, 17.5 percent of the withheld text lies within stretches of thirty characters that also occur in the training text, owing to its formulas and parallel passages, so KJVGPT’s loss on withheld text overstates its capacity to generalize.
- The page’s own check recognizes only the divine names that cannot be mistaken for ordinary words; the others are excluded by the training text alone.
- The translations are those of a general-purpose service, which is ill suited to biblical Hebrew.
- The models run in the browser, which must first download about seven megabytes.