The Rule Nobody Wrote

Neural Network Building BlocksEasy+10 XP+10

The Rule Nobody Wrote

A bank's old fraud check was a rule somebody typed in by hand: flag a card payment if it is for more than 500 and the card has never been used at that shop before. Its replacement is a single artificial neuron, and nobody chose its numbers. Its three weights and its bias were learned from thousands of past payments that had already been labelled fraud or genuine.

Each payment reaches the neuron as three inputs:

inputwhat it measures
x1x_1x1​the amount, in hundreds
x2x_2x2​how many payments the card made in the past hour
x3x_3x3​111 if the card has been used at this shop before, otherwise 000

The trained neuron has weights w1=0.6w_1 = 0.6w1​=0.6, w2=0.35w_2 = 0.35w2​=0.35, w3=−2.4w_3 = -2.4w3​=−2.4 and bias b=−1.2b = -1.2b=−1.2. A positive output leans towards fraud and a negative one leans towards letting the payment through.

Payments don't arrive one at a time, though. They arrive as a batch, a list of examples handled together, and the neuron has to score every payment in it.

Task: write neuron_forward(batch, weights, bias). It returns the neuron's output

z=∑iwixi+bz = \sum_i w_i x_i + bz=∑i​wi​xi​+b

for every example in the batch, as a list, with each value rounded to 4 decimal places.

  • batch is a list of examples. Each example is a list of input values, in the same order as weights.
  • weights has one entry per input. bias is a single number.
  • Return one zzz per example, in batch order.
  • The function must work for any number of inputs and any batch size. The fraud neuron is only the first test.

Once the first test passes, run the hand-written rule over the same three payments and compare verdicts. The rule and the neuron look at the same facts. What differs is who decided how much each fact counts, and a learned weight can trade one fact off against another in a way a yes-or-no rule never can.

×1.5×−0.4×0.2rooms3age12area9017.7weighted sum

4.5 − 4.8 + 18.0 = 17.7

Examples

batch = [[3.0, 5.0, 0.0], [1.0, 0.0, 1.0], [9.0, 1.0, 1.0]]
weights = [0.6, 0.35, -2.4]
bias = -1.2
returns [2.35, -3.0, 2.15]
batch = [[2.0, 3.0], [0.0, 0.0], [-1.0, 4.5]]
weights = [-1.25, 0.8]
bias = 0.5
returns [0.4, 0.5, 5.35]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run