Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Shared network concepts · ML-N02 · 12–18 MIN

Learning weights by reducing loss

Separate optimisation from generalisation.

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

Understand the idea

Training adjusts weights to reduce an objective. Comparable feature scales help optimisation. Different initialisations can produce different learning trajectories. Training loss is not validation performance.

Illustrative data

Final training loss: 0.325. Training loss measures the fitting objective, not validation performance.Training-loss history00.7611300Iteration

Final training loss: 0.325. Training loss measures the fitting objective, not validation performance.

answer = pd.Series(network.loss_curve_, name="training_loss")
answer.index = answer.index + 1
answer.index.name = "iteration"

What the syntax does

network.loss_curve_
  • The training objective after each optimisation iteration; these values are not final-test errors.
pd.Series(network.loss_curve_, name='training_loss')
  • Labels the recorded sequence so its meaning is clear in the output.

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

answer
  1. The requested result, with the values, labels and row order described by this task.
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 network’s loss history in answer as a Series, keeping the training order.
  2. Call its index iteration.
  3. Consecutive iteration labels may start at 0 or 1.
answer
Hints

Think: Turn loss_curve_ into an indexed Series to inspect how optimisation changed training loss.

Tools: Use network.loss_curve_, pd.Series(network.loss_curve_, name='training_loss'). Read the visible syntax meanings before editing.

Approach: Store the fitted network’s loss history in answer as a Series, keeping the training order. Call its index iteration. Consecutive iteration labels may start at 0 or 1. Keep the supplied row order and inspect the named output after running.

Explained solution
answer = pd.Series(network.loss_curve_, name='training_loss')
answer.index = answer.index + 1
answer.index.name = 'iteration'

Each loss value describes the training objective at one iteration. Reducing this objective guides weight updates. Falling training loss does not prove good predictions on new rows; validation evidence answers that separate question.

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.