The Artificial Neuron and Linear Combination
Compute the weighted sum z = Σ(w_i * x_i) + b and validate dimension consistency between inputs and weights.
Project Contract
Your program is a neural computation and training engine that processes commands from standard input in compact JSON format and writes results to standard output, reporting errors to stderr with exit code 1.
The Shipcode runner runs your program using the command declared in shipcode.yml:
challenge: neural-network
run: python3 main.py
First Principles: What is an Artificial Neuron?
To understand modern artificial intelligence, you do not need to start with deep networks containing billions of parameters. Everything starts with a single elementary unit conceived in 1943: the artificial neuronA simplified mathematical model inspired by biological brain cellsView official documentation.
Imagine you are deciding whether to go out for a walk today. Your brain weighs several environmental clues:
- Is the weather nice? ()
- Is it the weekend? ()
- Do you have pending errands? ()
However, not all clues carry equal importance to you. Good weather might be a huge positive factor, while errands might be a negative deterrent. The relative importance of each factor is called its weight ().
An artificial neuron models this exact process:
- It receives numerical input values ().
- It multiplies each input by its corresponding weight ().
- It sums all these weighted multiplications.
- It adds an independent baseline value called the bias ().
Why is the Bias Necessary?
Without a bias, if all inputs were zero (), the output would always be zero. The bias acts as the neuron's natural baseline tendency before receiving any external stimulus. Geometrically, the bias allows the decision boundary to shift up or down so it is not forced to pass through the origin .
Mathematical Formulation
The linear combination (commonly denoted as ) is calculated using the affine sum:
Your Objective in this Stage
When standard input receives an operation with "op": "linear-neuron", your program must process:
{
"op": "linear-neuron",
"inputs": [1.0, 2.0],
"weights": [0.5, -0.5],
"bias": 0.2
}
Step-by-Step Walkthrough
Using the values from the example above:
- Multiply the first input by its weight: .
- Multiply the second input by its weight: .
- Sum the weighted products: .
- Add the baseline bias: .
- The final output is .
Practical Implementation and Code Advice
In practical model development:
- In Python: Use the built-in
zip(inputs, weights)function to cleanly iterate across both vectors:if len(inputs) != len(weights): raise ValueError("Mismatched dimensions") z = sum(x * w for x, w in zip(inputs, weights)) + bias - In TypeScript: Use a standard
forloop orreduce()starting frombias:if (inputs.length !== weights.length) { throw new Error("Dimension mismatch"); } const z = inputs.reduce((acc, x, i) => acc + x * weights[i], bias);
Debugging Tips
If a test fails:
- Print
inputs,weights, andbiastosys.stderrorconsole.errorto inspect raw values without altering standard output. - Make sure bias is added once at the end; it should not be multiplied by any inputs.
Validation and Output Rules
- Dimension Consistency: The number of elements in
inputsmust matchweightsexactly. If a neuron receives 3 inputs but only has 2 weights, write a message tostderrcontaining the worderrorand exit with code1. - Error Handling and Malformed JSON: If standard input is not valid JSON or the
opfield is unrecognized, outputerrortostderrand exit with code1. - Precision Handling: Round the result to 4 decimal places using the
round_value/roundValuefunction provided in your starter. If the rounded value is an integer (including0), serialize it as an integer in JSON (for example{"z":0}instead of{"z":0.0}). - Response Format: Emit only the compact JSON object on a single line terminated by a newline:
{ "z": -0.3 }