# Understand neural networks and language models through their objectives

Canonical page: https://yishto.com/en/learn/ai-foundations/learning-attention-and-language-models/

Author: [Yishto](https://yishto.com/en/about/)

Connect training objectives and token prediction to model behavior, use a softmax experiment to understand aggregation, and distinguish the full architecture from application evaluation.

Published: 2026-11-10
Updated: 2026-11-10

A language model continues a textbook passage in a way that appears to understand and answer it. Assessing responsibility requires separating its training objective, internal computation, and application requirements. Next-token prediction can produce useful text without guaranteeing citations, arithmetic, or business decisions.

This lesson uses scores and values for three positions to demonstrate attention aggregation. It checks normalization and weighting without training a network or treating numeric weights as complete language understanding.

## Ask what the objective optimizes

Rule systems use explicit conditions, statistical learning estimates patterns, and neural networks learn parameterized mappings. These approaches coexist; AI history is not a claim that every method became a language model. Inputs, outputs, data, and error costs determine suitability.

Training typically computes a loss between prediction and target, then updates parameters using gradients. The loss is an optimizable proxy and may differ from the product objective. Lower training error can reflect memorization or patterns that do not generalize.

Training data updates parameters, validation data guides configuration, and test data supports independent evaluation. Avoid related examples across splits, such as neighboring passages from one document. Repeatedly using test results to tune prompts or parameters weakens independence, so retain another final check set.

## Tokens and probabilities explain generation

Tokens are tokenizer units such as words, subwords, or other fragments. Character counts do not equal token counts, and languages and tokenizers produce different partitions. A generative language model commonly predicts the next token distribution from the available context, chooses a token, and continues.

Choosing the most likely token differs from sampling. Sampling settings affect variability without establishing factual correctness. A predictable phrase need not be true. Applications that require evidence must still verify sources.

Context limits matter. Instructions, examples, retrieved material, and output need a budget defined by the actual interface. Truncation can remove essential conditions. Select passages, retain provenance, and record omissions instead of concatenating unlimited history.

## Attention combines information across positions

Scaled dot-product attention commonly transforms inputs into Q, K, and V. Queries and keys produce compatibility scores, scaled by the square root of the key dimension. Softmax normalizes them, and the resulting weights aggregate values.

Softmax turns real-valued scores into positive weights summing to one. Subtracting the maximum score preserves mathematical normalization while reducing exponential overflow risk. Weights are computed values, not sufficient evidence explaining a model's conclusion.

This example supplies scores 0, 1, 2 and values 10, 20, 30 directly. It omits the QK matrix multiplication and checks only softmax and the weighted sum. These are teaching values, not text embeddings.

```python
import math
scores = [0.0, 1.0, 2.0]
shift = max(scores)
weights = [math.exp(x-shift) for x in scores]
weights = [x/sum(weights) for x in weights]
values = [10, 20, 30]
result = sum(w*v for w, v in zip(weights, values))
assert abs(sum(weights)-1) < 1e-12
assert 10 < result < 30
print([round(x, 3) for x in weights], round(result, 3))
```

The actual output is `[0.09, 0.245, 0.665] 25.752`. The highest score receives the largest weight, while other positions still contribute. The result lies between 10 and 30. Assertions check normalization and that range. Displayed numbers are rounded; computation uses unrounded weights.

## A Transformer also needs order and other layers

Attention Is All You Need introduces an attention-based sequence architecture. A full Transformer also includes feed-forward layers, residual paths, normalization, and positional information. The example is not a complete Transformer.

Removing recurrence does not remove sequence order. Positional information distinguishes different arrangements of the same words. A causal language-model mask prevents a position from seeing future tokens, avoiding information unavailable during generation. Encoder, decoder, bidirectional, and causal tasks have different visibility rules.

Multi-head attention creates several projections and aggregation paths without guaranteeing that each head has one clear human concept. Parameter count alone does not establish product performance; training data, objectives, and deployment conditions also matter.

Embeddings represent discrete inputs as numerical vectors. Learned similarities can reflect task-data patterns, but nearby vectors do not establish the same fact and may mix distinct objects. Labels, data distributions, and optimization determine how representations form; the supplied values here underwent no learning.

A causal mask excludes future positions before normalization. Removing values after aggregation changes the computation. Implementations also need checks for fully masked rows, tensor dimensions, and numeric ranges, because an output can exist while using the wrong positions.

## Separate framework execution from evaluation

PyTorch provides tensors, automatic differentiation, and parameter-update mechanisms. Running a training loop establishes execution, not quality. Check losses, splits, baselines, and independent results. Hardware and model size affect resources; this lesson runs no training or benchmark.

A textbook summary needs checks for factual coverage, omissions, and citations. Classification needs per-class errors and abstention cases. Deterministic code can perform required arithmetic. Define which candidate model outputs can be adopted automatically and which need verification.

Locate failures in parsing, missing context, generation, or validation. Each stage requires a different repair. One overall score can hide these causes. Preserve task configuration and sample versions to compare changes meaningfully.

## Exercise: test computation and task boundaries

Make the three scores equal and check weights near one third. Increase one score to observe concentration. Add the same constant to every score and expect approximately unchanged results. Negative scores should still produce positive normalized weights.

Accept the exercise when it explains that these checks validate arithmetic, not learned language capability. Design separate training, validation, and test splits for textbook classification, identify leakage paths, and include an unanswerable input. Express the product goal through observable error categories. A real training experiment needs a separate record of framework, model, data, and results.

## Sources & further reading

- [Attention Is All You Need](https://arxiv.org/abs/1706.03762)
- [Deep Learning textbook](https://www.deeplearningbook.org/)
- [Learning neural-network basics](https://docs.pytorch.org/tutorials/beginner/basics/intro.html)

Sources checked on 2026-11-10. Examples are teaching data, not production measurements.

## Related guides

- [LLM API basics: tokens and context budgets](https://yishto.com/en/learn/models/context-and-tokens/)
- [LLM evaluation: compare quality, latency, and cost with a task set](https://yishto.com/en/learn/models/model-evaluation/)
