AI Hero Club

Train a Language Model from Scratch · Lesson 1

Marked Complete by You0 / 8

Saved in this browser only. Marking a lesson complete does not mean the project has passed verification.

Lesson Outline 6 subsections
Course Contents 8 lessons
Full course plan.
Train a Language Model from Scratch / Learning Plan ↗

LESSON 01 / 8Suggested study 120 minutes

Define the Corpus and Task Before the Model

How should we split and score the same documents so that held-out loss answers one clearly defined question?

3 Questions for This Lesson
  • Write document, template groups, data division and training targets into language contracts that can be verified item by item
  • Distinguishing document averages, token averages and false generalizations caused by leaks with complete numerical examples
  • Recreate project data summaries from original bytes and group lists and reject cross-assembly repetitions
Course Project ZIP ↓

Task, Starting Point, and Deliverables

This course is about a language model experiment that can begin with the original local text, train all decoder parameters, save restorations and provide recalculating indicators on the text. The training is deliberately small, so the ultimate delivery is credible engineering evidence rather than proof of “He can talk.”’s ability. Readers should be able to read Python functions and dictionaries, calculate the probability of conditions, logarithms and matrix multipliers, and have learned the causes and effects of Transformer’s battles and reverse transmission. This course is repeated with a local copy of the self-written model, followed by a focus on language materials, codes, training and evaluation connections; project compressed kits do not need to visit the catalogue of another course.

This chapter freezes the question: we give a prefix to a short text that predicts its next discrete symbol. The original short is not a token sequence; text and boundary must be saved before coded. data/documents.jsonl of this project contains 32 original template shorts, each with id, group, text, source; data/splits.json assigns 24 trainings, four validations, and four tests. Group is an action template, not a author or unique document number. Train six types of actions, each of which has one set aside for validation and testing. This setting aside only looks at changes within the same manual template system and does not represent natural language distribution.

Define the Statistical Object Clearly

Decrees dd This is a complete document. g(d)g(d) It's the template group. b(d)b(d) is the original byte string that was coded by UTF-8. Use Dtr,Dva,Dte\mathcal D_{tr},\mathcal D_{va},\mathcal D_{te} is a collection of three documents that do not interact with each other. Grouped first, then combined tokenizer in training. TT Converting Documents to [BOS,x1,…,xn,EOS][\mathrm{BOS},x_1,\ldots,x_n,\mathrm{EOS}]; BOS is the given starting condition, not the project ' s projection target, and EOS is the predicted end event. The next chapter of the special number is officially defined by name only and avoids mixing characters or bytes into probabilities in advance.

pθ(x1,…,xn,EOS∣BOS)=∏t=1n+1pθ(xt∣BOS,x1,…,xt−1),xn+1=EOS.p_\theta(x_1,\ldots,x_n,\mathrm{EOS}\mid\mathrm{BOS})=\prod_{t=1}^{n+1}p_\theta(x_t\mid\mathrm{BOS},x_1,\ldots,x_{t-1}),\qquad x_{n+1}=\mathrm{EOS}.
The chain approach is a constant equation; the distribution of limited windows and neural network conditions is the model selection followed.

Here. θ\theta The probability decomposition itself does not assume that it is adjacent to each other; on the contrary, each step can depend on the entire past. The true approximation comes from engineering: the model reads at most one length. CC window. We record this cut-off protocol in the indicator. After a given observation sequence, the multiplier equivalent to maximizes the probability of each target is set at the lowest negative logarithm because the negative logarithm is a strict single-reducing function; the logarithm converts the chain into an added value that avoids spilling over the probability multiplier values of a long series.

The target for a group of actual participants is NN No, no, no. ii The real category of objectives is: yiy_i. The loss of experience is L=−(1/N)∑ilog⁡pθ(yi∣ci)L=-(1/N)\sum_i\log p_\theta(y_i\mid c_i) of which cic_i This average defines the same weight for each goal; it does not automatically equalize the weight of each document. If the business problem is “Average difficulty of randomly drawing a short article”, then it needs to be re-evaluated and evened out first, which is another statistical question. Training and evaluation must clearly choose which issues do not change in curves.

Example 1: From Three Probabilities to the Total

Sets a short lettern with two targets, and the probability of a model giving a true target is... 1/21/2 and 1/41/4; Short B has only one goal and the real probability is 1/81/8 A negative logarithm and log⁡2+log⁡4=3log⁡2\log2+\log4=3\log2 "B" is log⁡8=3log⁡2\log8=3\log2. There are three goals in the whole collection, sum 6log⁡26\log2, by target average 2log⁡2≈1.3862942\log2\approx1.386294 nats. If you want the average document value first, A is 1.5log⁡21.5\log2 "B" is 3log⁡23\log2 We'll get the right to wait. 2.25log⁡2≈1.5595812.25\log2\approx1.559581. Both are right, but the answer is different.

Independent inspection is a direct multiplying of the overall probability: (1/2)(1/4)(1/8)=1/64(1/2)(1/4)(1/8)=1/64, the negative logarithm is equal to 6log⁡26\log2, then divides the top by three objectives. If the program merely divides the average loss of two batts by two, it will achieve the wrong weight at the same time in the battling length. We return to the total NLL, the number of valid targets, the original bytes and the document-by-document records in Chapter VII, so that the ledger here can be recalculated. It is more important to save enough molecular denominators than to produce a seemingly accurate decimal.

Split Boundaries Define the Generalization Assumption

Random cutting token regularly allows adjacent segments of the same document to enter training and testing; random cutting files may also allow minor replacements of the same template for template data on both sides. Set up four short messages in a group to change names only, other elements being the same. Three retestings are available for the fourth time, mainly for the replacement of names; leaving the entire action template is subject to actions and objects that are not directly provided by the training. The latter still has a shared sentence, and therefore is not a completely independent new area. Cutting rules change the difficulty of the assessment, and scores have to be explained together with rules.

The project was examined in two hard ways: the same group could not cross a collection and identical UTF-8 text summaries could not cross a pool. id was not enough: copying the text and changing the id was still leaking. Nor was the exact summary sufficient to detect close repetition: changing a space would change the summary. So the rules were only supported by “conflicts that were not captured by these two types of checks”; the template groups defined manually bore part of the nearly repeated isolation. We did not claim to complete copyright review, semantics or privacy filtering of open web-language materials because the project did not do these things.

Example 2: A Bad Test Set That Awards Full Marks

Assumptions for training documents id=a The text of the “Booking.。”, Test File id=z The text is exactly the same, and the group was wrongly filled into another value. Only the id and the group will pass. A system of whole memory maps will have a very low loss on this test text, but it is not supported “No text forecast available” Evidence. Add text. encode("utf-8") It's... SHA-256 Then the two summaries are identical, and the cross-group registration is reported wrong immediately. “Booking.。 ”, the exact summary is different; this reminds us that the summary is a byte-level tool and not a semantic equivalent judge.

The second independent check is to check the collection and merge: the id list of all split can not be repeated; the collection must properly cover the only id in the documents; otherwise the missing document may not be evaluated and the id may be weighted several times. This item should also be counted 32 equals 24 plus 4 plus 4 by hand, training group six, validation group one, testing group one. If a document is filtered out, the list and summary must be regenerated and cannot continue with the old checkpoint language identity.

Standalone ProgramThe standard library is independently validated: faulty material is rejected; two averages can be calculated separately.
import hashlib, math
rows=[{'id':'a','group':'g1','text':'甲整理书。','split':'train'},
      {'id':'z','group':'g2','text':'甲整理书。','split':'test'}]
seen={}; conflicts=[]
for r in rows:
    digest=hashlib.sha256(r['text'].encode('utf-8')).hexdigest()
    if digest in seen and seen[digest]!=r['split']:
        conflicts.append(r['id'])
    seen[digest]=r['split']
assert conflicts==['z']
nlls=[-math.log(.5)-math.log(.25),-math.log(.125)]
weighted=sum(nlls)/3
wrong=(nlls[0]/2+nlls[1])/2
assert abs(weighted+math.log(1/64)/3)<1e-12
print('duplicate rejected:',conflicts,'target mean:',round(weighted,6),'document mean:',round(wrong,6))

From a Research Question to Reproducible Data Identity

The data identity contains content and division, not just file paths. After the project has read two files, the calculation is based on JSON, which means corpus_sha256; the checkpoint simultaneously saves it and tokenizer_sha256. Reread the summary of current language calculations first on recovery, refusing to continue if inconsistent. This summary supports the bytes of the objects read, does not prove the true origin of the data, nor does it replace manual viewing of source fields. This item is documented as a short original template.

The receiving document in this chapter is a fixed document, splets and a data reader that can reject conflicts. Independently run examples show only duplicate detection and average definitions, while project-level checks read the real document directly. The next chapter requires this stable assembly decision. “What's coded?” and “What's the original byte?” and chapter III can only be compiled from training BPE. If you combine three pieces now to learn the usual clips, even if the decoder weight has not seen the test label, it means that the layer design is already influenced by the test distribution. Data disaggregation must precede all the processing steps that need to be prepared.

This item uses the latter, and then the average gradient for valid targets within the batch, so that each updated target weight changes with the length of the batch. The result is that the target weights vary. The separation of the training sample from the assessment of the accurate general ledger explains why the individual catch loss does not amount to a loss of the training set.

Empty files also need to be handled clearly. It has no original bytes in the text, but there are still two boundaries starting and ending after codification, with one end goal; each target loss is defined, and each primary byte indicator is not defined because of zero. The text provided by this project is not empty and data readings are fixed according to the application area of the indicator. Expanding the text should first decide whether to delete the empty document, the single end rate or the byte denominator of the report ' s aggregate, and should not temporarily change the denominator to one when counting zero, which will change statistics quietly.

The data selection also determines the optimal target distribution of the probability model. Assuming that a type of action template is reproduced ten times as much in training, the empirical risk will be more favourable to the category even if the document is correct; it is not that the optimist itself finds it more important, but rather that the sampling weight changes. If the original source has multiple versions of the same document, it should record them as belonging to the same source group before cutting them in and then decide to retain the version or overall distribution. Otherwise, the training test will be shared with only a few words, with lower losses likely to come from version memory.

Freezing tests does not always mean that the current configuration selection phase does not use test results as feedback; once the data rules or hyper-parameters are adjusted to test failures, the old test is involved in the study process and a new pool should be set aside for an unbiased final evaluation. The current project writes down a fixed configuration before running a test, and the size grid only looks at the verification team. The validation group itself is small, and repeated interrogatories have been tested; so the course shows only the predefined grid and does not generate a good end score by searching repeatedly.

Terminal CommandRuns in the downloading of the root directory of the project, installed in a stationary Python environment; this is the project command and not the Python source code.
python -m unittest -v tests.test_pipeline.PipelineTests.test_group_partition tests.test_pipeline.PipelineTests.test_duplicate_rejected

TRY IT YOURSELF

Practice I: Probability and weight

Each document has one goal and three objectives, each with a real probability of 1/2 and 1/4, respectively. For the target average NLL and the document average NLL, indicate which corresponds to this project.

Hint 1
The probability of negative logarithms per goal in each document is added together before deciding how to qualify.
Hint 2
The average timescale by target is the total number of objectives; the average by document is divided by the current goal, comparing the weights of two.
Compare with the Solution
The first article of the headline NLL is... log⁡2\log2 And the second part is... 3log⁡4=6log⁡23\log4=6\log2 Total 7log⁡27\log2 Four targets, so the average by target is 1.75log⁡2≈1.2130081.75\log2\approx1.213008. Average by document (log⁡2+log⁡4)/2=1.5log⁡2≈1.039721(\log2+\log4)/2=1.5\log2\approx1.039721. This item selects the former, so the second section contributes three target weights; the average value of two battlings cannot be easily averaged.

TRY IT YOURSELF

Practice II: Construct the reverse

Construct an id that is not heavy and the text is not identical, but it can still make the test too easy to cut. Give repair and repair of the boundary.

Hint 1
The id and byte summaries only exclude which repetitions? Try changing only one name in the template.
Hint 2
Allows two different texts to share a generation template, which is split by the template group; indicates which changes are left out of this rule.
Compare with the Solution
train as “The clouds of the morning record.。”, test for “Xiang records the clouds in the morning。”, both id and byte summaries are different, but the action template is shared. “Recording the morning clouds.” The template registers the same group, and the same group can be prevented by dividing it into groups as a group. It only covers groups that are known: rewording or trans-source reproducing may still escape, requiring additional near-repeal rules; and cannot be declared completely undisclosed by this restoration.

TRY IT YOURSELF

Practice III: Design acceptance claims

If there are only ids in 32 in the documents and the sum of the three list lengths is also 32, is it sufficient to prove a valid division? Give complete additional conditions and failures.

Hint 1
The same length does not guarantee the same suming. The list of “Repeat.、One missing.” is constructed first.
Hint 2
Check the collage id without duplication and overlaying the whole set, and then check group against executive summary separately without cross-split.
Compare with the Solution
Not enough. The list may write a twice and miss b. The total length remains 32. It should be asserted that the document is unique, then that the split id is not duplicated and that the grouping is equal to the entire document id; and that the group does not cross the exact summary. The first two steps guarantee partitions, the second two guarantees a specific isolation strategy. All conditions are established to continue to fit tokenizer; any deletion, reordering or group changes require updating of the data identity.

CHECK YOUR UNDERSTANDING

Check Your Understanding

Answer first, then open the explanation to check your reasoning. These questions help reveal gaps in understanding; independent implementation and transfer require separate evaluation.

1. What is the role of this project BOS?

Show ExplanationThe probability of predicting the first text symbol under BOS conditions, EOS is the target of the projection.
2. Combine the three splits first and merge the BPE. What happens?

Show ExplanationBPE is an estimated pre-treatment that combines the whole group to undermine the independence of training; it does not necessarily mean that all test elements are remembered.
3. The two catch valid targets are different. How should NLL be aggregated?

Show ExplanationMolecular denominators are added separately in order to retain the same weight for each target.
4. What can the cross-assembly text summaries prove without repetition?

Show ExplanationThe byte summaries are precise duplicate checks and cannot be determined to be rewritten or nearly repeated.

Manual completion records your progress; it does not certify your project.