AI Hero Club

Transformer in Practice · Lesson 12

Marked Complete by You0 / 12

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

Lesson Outline 6 subsections
Course Contents 12 lessons
Full course plan.
Transformer in Practice / Learning Plan ↗

LESSON 12 / 12Suggested study 240 minutes

Final Project: Deliver a Reproducible Transformer

Given only the project archive, how can someone independently reproduce training, recovery, evaluation and generation for the same build?

4 Questions for This Lesson
  • Independent assembly of data, models, optimization, restoration, evaluation and generation complete chain
  • Check correctness with a clear acceptance matrix, not just a demonstration.
  • Report the benefits, failures and data boundaries of the reference operation
  • Project packages with environment, Hash, original results and recovery orders
Course Project ZIP ↓

Final Deliverables and Completion Criteria

The final delivery is the same character Transformer that has been continuously constructed in the first 11 chapters: starting with a document number and code, with self-decoding blocks, full parameter training, preservation restoration, independent rating and cache generation. You should be able to repeat from a new depressed directory, explain the meaning of each major load, and define the results using a failure check. If you can only display a single piece of text or run another person’s saved weight, but cannot re-establish the training and evaluation, this course will not be completed.

The final package is divided into two parts: a self-fulfilling and an experimental report. This should include data, configuration, source code, testing and necessary instructions. The report should contain the original indicator, the failure sample and the identity information of the corresponding check point. The course provides a complete reference project to help you match formulas and interfaces, but the reference achievement cannot be understood as running. It is recommended that you complete your own module by chapter, then send the same input to the reference page for comparison, save the first failed intermediate, rather than simply pasting a passable result at the end.

The course's default readers are already writing Python functions, lists, dictionaries and running local programs; linear algebras, probability and guide numbers are already in front. The project relies on PyTorch's volume operations and automatic guidance engines, but attention, fruit masks, decoding blocks and training organizations are prepared by us without pre-training models or n. Transformer. This is a small-scale realization of the complete architecture and engineering chain, not pre-training Chinese models using thousands of words.

Check the Environment and Trace Where Data Enters the System

Depress the project package into the directory where README is located. First, confirm that the interpreter version is at least Python3. 10; the course actually validates the use of Python 3. 12.14 and PyTorch2. 6.0. If the system defaults that python3 is too old to create an environment with an already compliant interpreter, then apply the interpretor within the environment in a uniform manner. Do not command Python or another part to use a new environment and then miscalculate the missing packages to model problems. The following commands do not download pre-training weights and rely on installation to complete, the sample data and all calculations are local.

Terminal CommandExecutes the item directory after the pressure is lifted. If a version check fails, replaces the Python 3.10 or more interpreter first.
python3 -c 'import sys; assert sys.version_info >= (3,10), sys.version'
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python -m tests.run_tests --output evidence/my-tests.json

The project example contains 24 new short Chinese documents for the course, with stable numbers ranging from doc001 to doc024; titles are described only and the body of the document is trained. Distinguished files assign 18 trainings, three validations, three tests, and then only the text of the training constructs a sorting of the vocabulary. Do not treat the words " First create all the words and then split the document" as the same operation. For new language materials, the source and range are written, then the document identity is fixed, the division is checked and duplicated; different filenames do not preclude duplication of text.

Read the data and check the number of documents in the list, content and vocabulary to unknown characters. Each code is BOS, real character, EOS; the document breaks window, never starting with the end of the previous document. Each true character and each EOS is counted once in a full evaluation and the available context at the beginning of the window may be shorter. The window length and replacement are protocols that cannot be changed quietly after selecting the best result. The same agreement in Chapters VIII and IX remains valid in the final project.

PhaseInputs to readEvidence of necessity
DataDocument, Split, Encoding protocolID quarantine, double check, vocabulary, unknown rate
ModelFree Config with Parameter InitialisationShape, causality, parameter books, gradients
TrainingTraining window and sampler statuslost logs, updates, complete checkpoint
EvaluationSaved vocabulary and unupdated documentEffective token volume, loss and rating agreement
GenerateSame checkpoint and hintNumbering series, stop rules, cache cross-reference
ReportThe same version of the above-mentioned documentCommands, environment, Hash, original results and failure.
The output of each phase is a clear input for the next stage; a missing item cannot be filled with a screenshot.

Validation Stage 1: Each Component Computes What It Claims

Starts longer training by running numerical and behavioural tests. The focus tests not only check output shapes, but also check weights and weights for the future with a manual score, zero weights and value aggregations; the causal tests change future numbers and require prefix output to remain constant; each layer of the gradient tests receives a limited gradient compared to each length of the parameter, and the parameters are selected with a limited score at the float64 centre. “Received gradient” Only part of the connection is demonstrated and additional checks are required for the optimizers to actually update them.

The length, accuracy, and tolerance of the limited differentials should be recorded. The length of steps includes a clear cut-off error, and too small increases the reduced rounding effect; looking at several steps is more reliable than choosing just one pass. If the entire wrong target has been counted in the forward direction, automatic guidance and limited points may also pass together. Therefore, they must be taken back to the right goal in chapter II and the hand count in chapter III to prevent “The two procedures are identical.” from replacing “They're all solving the right problems.”.

Cache tests compare each step of the same prefix, covering word characters, multi-tokens, position deviations, window ceilings and slide windows. Saving recovery tests compares continuous training with a back-to-back training: not only to look at final loss, but also to compare parameters, optimizers, sample sorting, sampler locations, torch random status, and step-by-step Loses. The project opens dropout inspection recovery extraly to prevent an accidental equivalence only when there are no random branches. These two types of tests test different state machines and should not synthesize a green line without coverage.

When reading the test report, confirm that each case is actually executed, has not been skipped, and look at the failed information and source code Hashi. By covering only listed input and nature, the test does not deduce any data, device or accuracy. A practical countermeasure is to intentionally introduce known faults, such as removing future masks, resetting cache locations, leak saving sampler status, confirming that the corresponding test will fail and then be restored. This tests the ability to detect errors that it claims to have detected.

Validation Stage 2: Training on the Same Data and Parameters

The reference command uses CPU, single-line, thirty-two, two-storey, four-headed, window 16, pre-divide, eight batches. Small configurations reduce waiting costs, but all parameters of attention, foreplay, embedding, reclassification and output head are optimized. Optimizers are AdamW, learning zero zero zero, weight decline zero one, gradient paradigm is cut to one; no linear training on fixed attention features has been renamed full Transformer training.

Terminal CommandFull CPU small-scale training, 160 total updates.
.venv/bin/python -m hero_transformer.train   --output-dir runs/my-model --steps 160 --batch-size 8   --block-size 16 --d-model 32 --n-heads 4 --n-layers 2 --d-ff 128   --dropout 0 --lr 0.003 --seed 7 --threads 1 --eval-every 40

The training catalogue will get checkpoints.pt, metrics.json and run-manifest.json. First, check the logbook: when the list of examples is 3815, the total parameter is 50,000,368 and can be trained. Then look at the full training and validation points from the initial to the final model, rather than just the last random batch of Loses. The difficulty of sampling lots changes so that a single step down or up cannot directly substitute for an ex-post evaluation of the same fixed pool.

In a true reference operation retained for this course, the training set Loss dropped from 5.962669 to 2. 026304, and the validation set from 5.964481 to 5. 435705. The decline was very different, suggesting that the model 's preparation of this small training material is much more than an improvement on the unattractive information. This is the result of an explanation, not the “It's not good.” number that should be deleted. The training curve, file division and uncertainty are seen together to determine whether the next change of data, expression, capacity or optimization protocol should take place.

Validation Stage 3: Independent Scoring, Generation, and Recovery

Terminal CommandEvaluates with the same name sheet as is maintained; test sets do not participate in the selection of steps.
.venv/bin/python -m hero_transformer.evaluate   --checkpoint runs/my-model/checkpoint.pt --split validation   --batch-size 8 --threads 1 --output runs/my-model/validation.json
.venv/bin/python -m hero_transformer.evaluate   --checkpoint runs/my-model/checkpoint.pt --split test   --batch-size 8 --threads 1 --output runs/my-model/test.json

The independent test for the reference operation is Loss 5. 460169, with a confusion of about 235. 137101; the test set has two hundred and twenty-eight original characters, plus three EOS, with a score of 231 points. Verifying the unknown character ratio is about 13.14 per cent, testing about 17.54 per cent. They are folded to UNK, and not all characters remain in the original character identity; therefore, the confusion cannot be ranked directly with other wordboxes or large models of open vocabulary. The figures refer only to the corresponding checkpoint and rating protocol within the package, and are not the generic constant that each machine should receive for each operation.

Terminal CommandGenerating and without cache reference; temperature=0 walking corrupt branches.
.venv/bin/python -m hero_transformer.generate   --checkpoint runs/my-model/checkpoint.pt --prompt 小禾   --max-new-tokens 60 --temperature 0 --threads 1   --output runs/my-model/greedy.json
.venv/bin/python -m hero_transformer.generate   --checkpoint runs/my-model/checkpoint.pt --prompt 小禾   --max-new-tokens 60 --temperature 0 --threads 1 --no-cache   --output runs/my-model/greedy-reference.json

The reference model 's greed for the reminder “Mio.” is exported to “Siu-wo give Siu-wo a check。”, followed by EOS. It has obvious repetitions and lack of consistency. Keeping this example is part of the project report: the model can correctly implement complete training and reasoning algorithms, while still lacking sufficient data and capacity. Changing the repeat example to a handwritten and fluid sentence would confuse software behavior with the editing product and render the receipt ahead meaningless.

Terminal CommandChecking the state of preservation and continuing to step 164; not extra training step 164.
.venv/bin/python -m hero_transformer.checkpoint   --checkpoint runs/my-model/checkpoint.pt   --output runs/my-model/checkpoint-summary.json
.venv/bin/python -m hero_transformer.train   --resume runs/my-model/checkpoint.pt --output-dir runs/my-model-continued   --steps 164 --threads 1 --eval-every 4

To test the accuracy of this continuation, the same initial setting of the separate directory is used to train directly to one hundred and sixty-four steps and to compare the two-sided state. This true CLI comparison is kept in the package with a maximum deviation of zero parameters, with optimisers, samplers, random status and the same list. This conclusion covers only the same CPU environment, fixed threads and recorded versions, not the same commitments across GPU, across frames. The quality table of the 160-step model, compared to the recovery of 164 steps here, should be indicated separately and not folded into a vague one. “Best model.” Achievements.

Validation Stage 4: Let Another Reader Reconstruct What You Ran

The first page of the report should be clear about the task, the reader ' s recantable command, model configuration, data source and division, environment, check-point identity and key results. Three types of material are then available: original output of correctness checks, training on fixed data and evaluation results, with examples of failure. At least one case of failure that has not been optimized is retained and an explanation of what it supports and what it cannot support.

Hashi used to bind these records to a clear document, which does not justify the algorithm or the material authorization. Using fixed sorting and coding for the configuration list to calculate SHA256 would avoid obtaining another identity simply because the dictionaries have different dictionaries; modifying seeds, phrases, document contents or structures would create different identities. Model weights, source numbers, and data each have their own habitat, and it is not relevant to call a transect " “Model receipts” ". The following example would merely standardize the list and not substitute for checking actual project documents.

Standalone ProgramIndependent check list identity: The key order does not change the prescriptive content, and the torrent changes the content.
import json,hashlib
def digest(manifest):
    encoded = json.dumps(manifest,sort_keys=True,ensure_ascii=False,separators=(',',':')).encode('utf-8')
    return hashlib.sha256(encoded).hexdigest()
a = {'seed':7,'config':{'width':32,'layers':2},'data_hash':'example-data'}
b = {'data_hash':'example-data','config':{'layers':2,'width':32},'seed':7}
changed = {**a,'seed':8}
assert digest(a)==digest(b)
assert digest(a)!=digest(changed)
print({'manifest':digest(a),'changed_seed':digest(changed)})
Receiving and Inspection LineThe results that must be displayedThere's no substitute for it.
Mathematics and ModulesHand calculations, gradient differentials, causal/morphological checksOnly output shape or text
Full trainingAll parameters access optimized and actually updatedOnly output layering or pre-training weights
Restore and CacheThe status of the same input/trajectories versus fractionsJust look at the last text like it.
Mission evaluationFreezing split, valid targets, unknown rates and failuresTraining lost or sampled only
Recoverable deliveryEnvironment, command, source/data/weight identityA schedule of accomplishments without corresponding documentation
These lines are separately inspected and one item cannot offset the other.

Self-evaluations can be divided between deduction, realization, experimentation and delivery, but meet the non-compensable threshold of causal failure, mislabelling, participation in training of test data, inconsistent recovery, reports and actual checkpoints, either of which should be repaired before total scores are discussed. The “Completed” button on the course page is just a log of your current browser and does not automatically prove acceptance through the project, nor does it grant credits or certificates to external course institutions.

By this point, chapter 12 forms a recastable closed circle: task definition determines labels, labels determine target, attention and location determines information path, decoding blocks and backpropagation decisions can be trained functions, training protocols determine parameter trajectory, evaluation and generation of tests for final behaviour. A subsequent expansion model or entry into courses from zero language models, after-training, efficient reasoning etc. can be replaced with a clear module and re-accepted along the same chain without having to rely on new presentations.

TRY IT YOURSELF

Practice a gradient exists, but no real training.

All parameters are given a non-zero limited gradient, but only the output head changes after training. Give at least two possible reasons, how each is located, and why “Gradient check through” is not enough.

Hint 1
The gradient already exists, which objects do we need to check for a true update?
Hint 2
Compares all parameters immediately before and after Step, then tracks whether they are subsequently overwritten by the old state; and contrasts with an optimist parameter identification missing.
Compare with the Solution
The first is that the optimiser receives only output head parameters: the identity of the object of the print optimizer parameter corresponds to the mode.named_parameters, checking whether the embedded or block is missing. The second is that the update restores part of the weight from the old state or covers the submodule in situ: immediately compares each parameter before and after optimizer.step and tracks the follow-up assigned value. There may also be zero learning rates, freezing marks or minor updates that are swallowed up with precision. The gradient check only examines the local lead numbers of the lost to the current parameter and does not support the correct coverage, updating range and follow-up status management of the updateer.

TRY IT YOURSELF

Practice two-packs to locate the boundary for recovery failure.

A 16-step continuous training, B 7-step saving, 9-step recovery. Models on both sides are the same as an optimist start value, but the final parameters differ. List the intermediate status to be compared in the order of input to output, and do not recommend only “Change the torrent.”.

Hint 1
Arranges the check points by "Data Identity — Batch Input — Random State — Forward Gradient — Optimizer".
Hint 2
The first divisive step of recovery was found, which was to check the index, the cursor and RNG, and then compare the steps with the parameters without first replacing the seeds.
Compare with the Solution
Check the source, configuration, data and splithasi first; compare the number of sampling windows per step, complete targets with valid tokens; compare the sample sequence, cursor, epoch and local generator status at the time of preservation; then compare the torch RNG, especially dropout; compare the forward loss, gradients, optimisations and updating parameters for the first recovery step. The first boundary where differences occur determines where the boundary fails. The replacement of seeds replaces the experiment and does not fix the recovery equivalent.

TRY IT YOURSELF

Practice three-by-three to write the correct final conclusions on the border.

The reference model training losss has dropped significantly, independent tests are still high, unknown character ratios 17.54 %, and duplicates are generated, but values have been checked with recovery. Write a project conclusion that neither conceals progress nor exaggerates capacity.

Hint 1
The correctness of the realization chain is divided into two columns of evidence from independent testing quality.
Hint 2
A sentence that describes the proven training/restoration/cache mechanism and a sentence that generates binding capacity conclusions with unknown rates and repetitions, does not delete failure.
Compare with the Solution
It can be written that we delivered and validated the complete data of the self-written character Transformer, full parameter training, restoration, rating and cache generation chain; observed a drop in the target for training on a fixed medium, and retained independent tests and error samples. The test was high and there were duplications, so the evidence only supported the completion of the mini-scription mechanism and did not support extensive Chinese understanding or high quality production. Listing the same version of documents and commands, and treating the next extension of data coverage and evaluation as an experiment rather than a completed capability.

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. Is the final project showing only fluid text sufficient to prove a complete training chain?

Show ExplanationAppearance samples do not provide evidence of data sources, optimized coverage, scoring agreements or recovery status.
2. The parameters are non-zero gradients and must be updated by the optimist?

Show ExplanationThe guidance chart, the optimizer parameter pool and subsequent status management are different components.
3. Resume command -- what does Steps 164 mean?

Show ExplanationThis item defines Steps as a cumulative updating cap; it resumes from the saved step.
4. Which conclusions are consistent with the reference experimental boundary?

Show ExplanationThe correctness check is reported separately from the mission capability; low training loss cannot erase unknown rates and create failures.

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