Build your own neural network
Stage 6 of 6v1 · 30176cd4

Convergent Training and Inference

Train weights and bias over multiple epochs to learn logical patterns and classify new unseen inputs.

Build it in your language

The tester treats your program as a black box: any language works. These are just the most convenient ones for this project.

First Principles: The Culmination: From Zeroes to an Intelligent Model

You have constructed every foundational component of modern machine learning:

  1. The Linear Neuron: Computes weighted combinations of environmental signals.
  2. The Activation Function: Introduces non-linear squashing into bounded probabilities.
  3. The Loss Function: Quantifies prediction error with mathematical rigor.
  4. The Optimization Step: Shifts weights downhill along the loss slope.
  5. The Training Epoch: Uses the chain rule to backpropagate errors across all data samples.

In this final stage, you bring all these components together to build the complete training and inferenceThe stage where a trained model processes new input data to make classificationsView official documentation engine.

The Challenge: Teaching Boolean Logic to a Neuron

Consider the truth table for the logical AND gate:

  • Inputs [0,0]  ⟹  0[0, 0] \implies 0
  • Inputs [1,0]  ⟹  0[1, 0] \implies 0
  • Inputs [0,1]  ⟹  0[0, 1] \implies 0
  • Inputs [1,1]  ⟹  1[1, 1] \implies 1

At the beginning, we initialize all weights to 00 (w1=0,w2=0w_1 = 0, w_2 = 0) and bias to 00 (b=0b = 0). With these initial values, the neuron knows nothing: for any input it computes z=0z = 0, giving a sigmoid output of σ(0)=0.5\sigma(0) = 0.5 (maximum uncertainty).

However, as the optimization loop runs through 200 epochs:

  1. During each epoch, the model compares its predictions against the truth table targets.
  2. Backpropagation computes the average gradients across all samples.
  3. The weights gradually increase while the bias becomes negative.
  4. By the end of training, the parameters converge such that the weighted sum only clears the threshold when both inputs are active.

You have just trained a machine to discover a boolean rule purely from examples!

Your Objective in this Stage

When standard input receives "op": "train-and-predict", process:

  • epochs: number of full training epochs to run (positive integer)
  • learning_rate: learning rate α\alpha
  • samples: training dataset containing pairs of {"x": [...], "y": 0 or 1}
  • test_inputs: list of feature vectors to classify after training

Step-by-Step Procedure

  1. Initialization:

    • Determine input dimension dd from len(samples[0]["x"]).
    • Initialize weights to zeroes: weights = [0.0] * d.
    • Initialize bias to zero: bias = 0.0.
    • Batch size: N=len(samples)N = \text{len}(samples).
  2. Training Loop (Repeat epochs times):

    • Initialize gradient accumulators: grad_w = [0.0] * d and grad_b = 0.0.
    • For each sample ss in samples:
      • z=∑j=1d(wj⋅s.xj)+bz = \sum_{j=1}^{d} (w_j \cdot s.x_j) + b.
      • y^=11+e−z\hat{y} = \frac{1}{1 + e^{-z}}.
      • δ=(y^−s.y)⋅y^⋅(1−y^)\delta = (\hat{y} - s.y) \cdot \hat{y} \cdot (1 - \hat{y}).
      • Accumulate: grad_w[j] += delta * s.x[j] and grad_b += delta.
    • Update weights and bias by subtracting the mean gradient scaled by learning_rate: wj←wj−α⋅grad_w[j]Nw_j \leftarrow w_j - \alpha \cdot \frac{\text{grad\_w}[j]}{N} b←b−α⋅grad_bNb \leftarrow b - \alpha \cdot \frac{\text{grad\_b}}{N}
  3. Inference Phase:

    • For each feature vector xtestx_{\text{test}} in test_inputs:
      • Evaluate the neuron using the finalized weights and bias: z=∑j=1d(wj⋅xtest,j)+bz = \sum_{j=1}^{d} (w_j \cdot x_{\text{test}, j}) + b p=11+e−zp = \frac{1}{1 + e^{-z}}
      • Apply the standard classification threshold: prediction={1if p≥0.50if p<0.5\text{prediction} = \begin{cases} 1 & \text{if } p \ge 0.5 \\ 0 & \text{if } p < 0.5 \end{cases}

Implementation direction

Separate gradient accumulation from parameter updates. Check intermediate values against the numerical example. Keep training and inference distinct; write the loops yourself and reuse the earlier functions.

ML Engineering Insight

Notice the sharp separation between phases:

  1. Training: Model weights mutate along loss gradients guided by ground-truth labels y.
  2. Inference: Weights are completely frozen; the network only executes the forward pass on unseen data without knowing true answers or computing gradients.

Validation and Output Rules

  1. If samples is empty or epochs <= 0, write error to stderr and exit with code 1.
  2. Output a JSON object containing the array of predicted integers:
    json
    { "predictions": [0, 0, 0, 1] }
    

Output and errors

Multiline examples are formatted for readability. Emit compact JSON without optional spaces, with properties in the order shown by the output examples, and a final newline. Keep logs off stdout. Tests compare exact text. Invalid inputs use stderr containing error, empty stdout, and exit 1. Valid inputs exit 0. Serialize integer results as JSON integers; neural-network uses the stage’s four-decimal rounding rule.