Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Go Further · ML-N06 · 12–18 MIN

Capacity and regularisation

Interpret controlled width and alpha experiments.

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

Understand the idea

Width increases representational capacity. Alpha controls weight regularisation. Use controlled training/validation evidence to reason about both; production’s small width grid does not explore every possible network.

Interpret controlled width and alpha experiments.InputsHidden unitsOutputs
Interpret controlled width and alpha experiments.Scroll the diagram horizontally if needed.

What the syntax does

hidden_layer_sizes=(16, 8)
  • Requests two hidden layers, with 16 units and then 8 units.
  • hidden_layer_sizes: (24,), (16, 8)
alpha=0.01
  • Sets the L2 weight penalty; its useful value needs validation evidence.
  • alpha: 0.0001, 0.01
model.set_params
  • Changes estimator settings.
  • Refit before evaluating the changed model.

Outputs you create

layers
  1. Requested result described in the task and deliverables.
alpha
  1. Requested result described in the task and deliverables.
LINE24 · first 8 rows
distanceduration
15.30501
1.4782611.2361
1.9565211.8488
2.4347814.809
2.9130410.7589
3.391319.4972
3.8695717.89
4.3478315.531
Your task
  1. Create a network with one 24-unit hidden layer and alpha=0.0001.
  2. Change its hidden layers to (16, 8) and alpha to 0.01.
  3. Store the resulting settings in layers and alpha.
layersalpha
Hints

Think: Use set_params(), tuples and a float to describe capacity and regularisation before fitting.

Tools: Use hidden_layer_sizes=(16, 8), alpha=0.01, model.set_params. Read the visible syntax meanings before editing.

Approach: Create a network with one 24-unit hidden layer and alpha=0.0001. Change its hidden layers to (16, 8) and alpha to 0.01. Store the resulting settings in layers and alpha. Keep the supplied row order and inspect the named output after running.

Explained solution
from sklearn.neural_network import MLPClassifier
model = MLPClassifier(hidden_layer_sizes=(24,), alpha=0.0001, random_state=42)
model.set_params(hidden_layer_sizes=(16, 8), alpha=0.01)
layers = model.hidden_layer_sizes
alpha = model.alpha

The new tuple changes the network capacity and alpha changes the penalty on large weights. These are candidate settings, not automatic improvements. Compare candidates on the same training folds and consider convergence and cost.

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.