Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessonsQUESTIONS · MODELS · EVIDENCE
Shared network concepts · ML-N05 · 18–25 MIN

Convergence, early stopping and validation

Distinguish internal stopping, outer CV and final testing.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeReason about the Python
  3. PractiseReason about the Python
  4. TransferExplain the Python result

Understand the idea

A convergence warning means the stopping criterion was not reached within the budget. Internal early stopping, outer CV and final test have separate roles. sklearn’s internal stopping score need not equal the outer selection metric. Production disables early stopping for chronological MLP regression.

Distinguish internal stopping, outer CV and final testing.Training lossValidation errorFalling training loss does not establish generalisation.
Schematic · Distinguish internal stopping, outer CV and final testing.Scroll the diagram horizontally if needed.

Python skill: Distinguish n_iter_ (observed iterations) from max_iter (the configured upper limit).

Meet the syntax

network.n_iter_
network.max_iter
network.early_stopping
network.n_iter_
The number of iterations actually performed during this fit.
network.max_iter
The configured ceiling, not a guarantee that optimisation converged.
network.early_stopping
Whether fitting can stop using an internal validation subset of its training rows.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

iterations_used = network.n_iter_
iteration_limit = network.max_iter

This practice: Read and run the Python. Next: Change · Convergence, early stopping and validation.

Given data · CLASS180

180 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

CLASS180 · first 8 prepared rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
lengthfloat64
widthfloat64
labelstr
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df[['length', 'width']], df['label'], test_size=0.2, random_state=42, stratify=df['label'])
model = Pipeline([('scale', StandardScaler()), ('model', MLPClassifier(hidden_layer_sizes=(8,), max_iter=150, early_stopping=True, random_state=42))])
model.fit(X_train, y_train)
network = model.named_steps['model']

Your task · Follow

Store the fitted iteration count in iterations_used and the configured upper limit in iteration_limit.

Hint 1 — Think

Distinguish n_iter_ (observed iterations) from max_iter (the configured upper limit).

Hint 2 — Tools

Use network.n_iter_, network.max_iter, network.early_stopping. Read the visible syntax meanings before editing.

Hint 3 — Approach

Store the fitted iteration count in iterations_used and the configured upper limit in iteration_limit. Keep the supplied row order and inspect the named output after running.

Explained solution
iterations_used = network.n_iter_
iteration_limit = network.max_iter

Reaching the iteration limit calls for inspecting warnings and loss. Stopping earlier may reflect the stopping rule; it does not establish useful performance. Internal early stopping remains inside each training fit and cannot replace the outer validation comparison.

Helpful prior knowledge: Learning weights by reducing loss These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Store the fitted iteration count in iterations_used and the configured upper limit in iteration_limit.

iterations_usediteration_limit

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.