Nine spreads, from how machines learn, to training a small model yourself, to building an app on a large language model with your own data, tools, and tests.
"We start with a hosted model and a clear task, ground it with retrieval and tools, measure it with an eval set, and only fine-tune if the numbers say we must."
Examples use Python. Use the tabs or the ← → keys.
A model has parameters (numbers). Training adjusts them to reduce a loss: how wrong the predictions are on known examples. Then you use the model on new data (inference).
| Task | Type | Example |
|---|---|---|
| predict a number | regression | house price |
| pick a category | classification | fraud / not fraud |
| group similar things | clustering | customer segments |
| generate text/images | generative | chat, summaries |
$ python -m venv .venv && source .venv/bin/activate $ pip install numpy pandas scikit-learn matplotlib jupyter
"It's supervised classification. We minimize loss on training data and judge it on held-out data."
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv("customers.csv")
print(df.shape); print(df.head()); print(df.isna().sum()) # look at it first!
df = df.dropna(subset=["churned"]) # need labels
X = df.drop(columns=["churned"])
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y) # keep class balance
print(y_train.mean(), y_test.mean())
"Stratified split, no leakage, a locked test set, and a baseline to beat."
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.dummy import DummyClassifier
from sklearn.metrics import classification_report, roc_auc_score
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
print("baseline acc", baseline.score(X_test, y_test)) # the bar to beat
model = make_pipeline(StandardScaler(), RandomForestClassifier(n_estimators=300, random_state=42))
model.fit(X_train, y_train)
pred = model.predict(X_test)
print(classification_report(y_test, pred)) # precision, recall, F1
print("AUC", roc_auc_score(y_test, model.predict_proba(X_test)[:, 1]))
import joblib; joblib.dump(model, "churn.joblib") # save it
"We beat the baseline on held-out data, and we picked the metric that matches the cost of mistakes."
A neuron computes activation(w·x + b). Stack thousands of them in layers and the network can learn very complex patterns (images, speech, language). Training uses gradient descent: nudge every weight in the direction that lowers the loss.
pip install torch # PyTorch
import torch, torch.nn as nn
Xt = torch.tensor(X_train.values, dtype=torch.float32)
yt = torch.tensor(y_train.values, dtype=torch.float32).unsqueeze(1)
net = nn.Sequential(nn.Linear(Xt.shape[1], 32), nn.ReLU(),
nn.Linear(32, 16), nn.ReLU(),
nn.Linear(16, 1))
opt = torch.optim.Adam(net.parameters(), lr=1e-3)
loss_fn = nn.BCEWithLogitsLoss()
for epoch in range(50):
opt.zero_grad()
loss = loss_fn(net(Xt), yt) # forward + loss
loss.backward() # backpropagation
opt.step() # gradient descent step
if epoch % 10 == 0: print(epoch, loss.item())
"Trees for tables, pretrained networks for perception and language, and transfer learning before training from scratch."
An LLM is a transformer neural network trained on huge amounts of text to predict the next token. Then it's tuned to follow instructions and be helpful and safe. You rarely train one; you use one.
| Term | Meaning |
|---|---|
| token | a chunk of text, roughly ¾ of a word |
| context window | how many tokens it can read at once (prompt + reply) |
| system prompt | standing instructions for role, tone, rules |
| temperature | randomness of sampling |
| embedding | a vector that captures meaning; similar text → nearby vectors |
| fine-tuning | extra training on your examples to change style or skill |
"It's a next-token predictor, so I give it the facts it needs in context and verify the output."
$ pip install anthropic $ export ANTHROPIC_API_KEY="..." # from console.anthropic.com; never commit it
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
SYSTEM = "You are a concise support assistant for Acme. If unsure, say so. Answer in under 120 words."
history = []
def ask(user_text):
history.append({"role": "user", "content": user_text})
resp = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=500,
system=SYSTEM,
messages=history,
)
text = resp.content[0].text
history.append({"role": "assistant", "content": text})
return text
print(ask("How do I reset my password?"))
# streaming for a snappy UI
with client.messages.stream(model="claude-sonnet-5-5", max_tokens=500,
messages=[{"role": "user", "content": "Explain RAG in two lines"}]) as s:
for chunk in s.text_stream: print(chunk, end="", flush=True)
"System prompt for behaviour, streamed responses, validated structured output, and the key stays on the server."
# minimal RAG with an in-memory index (swap in pgvector / Chroma / Qdrant at scale)
import numpy as np
from sentence_transformers import SentenceTransformer # pip install sentence-transformers
emb = SentenceTransformer("all-MiniLM-L6-v2")
chunks = [c for doc in load_docs() for c in split(doc, size=800, overlap=100)]
vecs = emb.encode(chunks, normalize_embeddings=True)
def retrieve(q, k=4):
qv = emb.encode([q], normalize_embeddings=True)[0]
top = np.argsort(vecs @ qv)[::-1][:k] # cosine similarity
return [chunks[i] for i in top]
def answer(q):
ctx = "\n\n".join(f"[{i+1}] {c}" for i, c in enumerate(retrieve(q)))
return client.messages.create(
model="claude-sonnet-5-5", max_tokens=600,
system="Answer ONLY from the sources. Cite like [1]. If they don't contain the answer, say you don't know.",
messages=[{"role": "user", "content": f"Sources:\n{ctx}\n\nQuestion: {q}"}],
).content[0].text
"Retrieval first, a grounded prompt that allows 'I don't know', citations, and per-user access filters."
tools = [{
"name": "get_order_status",
"description": "Look up an order's status by its ID.",
"input_schema": {"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}]
messages = [{"role": "user", "content": "Where is order A1042?"}]
while True:
r = client.messages.create(model="claude-sonnet-5-5", max_tokens=800, tools=tools, messages=messages)
messages.append({"role": "assistant", "content": r.content})
if r.stop_reason != "tool_use":
print(r.content[0].text); break
results = []
for block in r.content:
if block.type == "tool_use":
out = get_order_status(block.input["order_id"]) # YOUR code, YOUR checks
results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(out)})
messages.append({"role": "user", "content": results})
"Narrow, validated tools, human approval for risky actions, a step budget, and full traces."
# a tiny eval harness: run, score, compare versions
import json
cases = json.load(open("evals.json")) # [{"q": "...", "must_include": ["30 days"], "must_not": ["guarantee"]}]
def run_eval(answer_fn):
passed = 0
for c in cases:
a = answer_fn(c["q"]).lower()
ok = all(s in a for s in c.get("must_include", [])) and not any(s in a for s in c.get("must_not", []))
passed += ok
if not ok: print("FAIL:", c["q"])
print(f"{passed}/{len(cases)} passed")
run_eval(answer) # re-run after every prompt or retrieval change
"An eval suite gates every change, we log and sample production, and risky actions keep a human in the loop."