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:
- The Linear Neuron: Computes weighted combinations of environmental signals.
- The Activation Function: Introduces non-linear squashing into bounded probabilities.
- The Loss Function: Quantifies prediction error with mathematical rigor.
- The Optimization Step: Shifts weights downhill along the loss slope.
- 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
- Inputs
- Inputs
- Inputs
At the beginning, we initialize all weights to () and bias to (). With these initial values, the neuron knows nothing: for any input it computes , giving a sigmoid output of (maximum uncertainty).
However, as the optimization loop runs through 200 epochs:
- During each epoch, the model compares its predictions against the truth table targets.
- Backpropagation computes the average gradients across all samples.
- The weights gradually increase while the bias becomes negative.
- 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 ratesamples: training dataset containing pairs of{"x": [...], "y": 0 or 1}test_inputs: list of feature vectors to classify after training
Step-by-Step Procedure
-
Initialization:
- Determine input dimension from
len(samples[0]["x"]). - Initialize weights to zeroes:
weights = [0.0] * d. - Initialize bias to zero:
bias = 0.0. - Batch size: .
- Determine input dimension from
-
Training Loop (Repeat
epochstimes):- Initialize gradient accumulators:
grad_w = [0.0] * dandgrad_b = 0.0. - For each sample in
samples:- .
- .
- .
- Accumulate:
grad_w[j] += delta * s.x[j]andgrad_b += delta.
- Update weights and bias by subtracting the mean gradient scaled by
learning_rate:
- Initialize gradient accumulators:
-
Inference Phase:
- For each feature vector in
test_inputs:- Evaluate the neuron using the finalized weights and bias:
- Apply the standard classification threshold:
- For each feature vector in
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:
- Training: Model weights mutate along loss gradients guided by ground-truth labels
y. - 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
- If
samplesis empty orepochs <= 0, writeerrortostderrand exit with code1. - 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.