The Artificial Neuron and Linear Combination
Compute the weighted sum z = Σ(w_i * x_i) + b and validate dimension consistency between inputs and weights.
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.
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:python 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:typescript 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:
json { "z": -0.3 }
Starting and checking progress
Open your language starter’s README. From its root, execute the build command in shipcode.yml if present, then run. The scaffold is intentionally unfinished and initially reports missing implementation. After developing this stage, run shipcode test; preserve earlier stages’ behavior and use shipcode submit after passing.
You may use a JSON library for the input/output envelope, but not to implement the project’s algorithm. If your language has no built-in JSON support, document the dependency and update the shipcode.yml build command; verify installation in a clean directory.
Snippets using Math, .shift(), or similar notation illustrate algorithm ideas; they do not require JavaScript. Adapt them to your language. A byte is not always a character: count bytes in binary formats and specify the encoding.
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.