Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Shared network concepts · ML-N05 · 18–25 MIN

Convergence, early stopping and validation

Distinguish internal stopping, outer CV and final testing.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. PractiseCompute and inspect the evidence
  4. TransferCompute and inspect the evidence

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.

Illustrative data

Illustrative data
ColumnValues
Fit · same(-1, -1); (-1.2, -0.8); (-0.8, -1.2); (1, 1); (1.2, 0.8); (0.8, 1.2)
Fit · opposite(-1, 1); (-1.2, 0.8); (-0.8, 1.2); (1, -1); (1.2, -0.8); (0.8, -1.2)
Validation(-0.9, -0.9); (0.9, 0.9); (-0.9, 0.9); (0.9, -0.9) · labels: same, same, opposite, opposite

Click the highlighted code to change the result.

scaler = StandardScaler().fit(example_X_train)
example_network = MLPClassifier(hidden_layer_sizes=(8,), max_iter=, random_state=42)
example_network.fit(scaler.transform(example_X_train), example_y_train)
loss = example_network.loss_curve_
validation_predictions = example_network.predict(scaler.transform(example_X_validation))
validation_f1 = f1_score(example_y_validation, validation_predictions, average="macro")
Budget 100: 100 fitted iterations; final training loss 0.539; fixed validation macro F1 0.733. Same 12 fitting rows, four validation rows, eight hidden units and seed 42. Loss and validation F1 have separate meanings and scales.Training loss00.7611100IterationValidation macro F11020100300600Iteration budget

Budget 100: loss 0.539; validation macro F1 0.733.

What the syntax does

network.n_iter_
  • The number of iterations actually performed during this fit.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
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
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.
network
Already fitted on X_train, y_train. Supplied network; its definition below specifies the values and role. Fitted on X_train, y_train; reuse without refitting.

Outputs you create

iterations_used
  1. Requested result described in the task and deliverables.
iteration_limit
  1. Requested result described in the task and deliverables.
CLASS180 · first 8 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
Your task
  1. Store the fitted iteration count in iterations_used and the configured upper limit in iteration_limit.
iterations_usediteration_limit
Hints

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

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

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.

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.