A neural network's prediction is a forward pass: layer by layer, weighted sums and activations. Compute one for a network with a ReLU hidden layer and a sigmoid output.
What to build
A program that computes the hidden activations, output score, probability and class of a two-layer network.
Requirements
- Hidden layer: m neurons, each with n weights and a bias, with ReLU activation.
- Output: one neuron with m weights and a bias, with sigmoid activation.
- Print
hidden:followed by the m hidden values,z = Z(the output neuron's sum),output Pandclass C, all numbers with 4 decimal places.
Input
Line 1: n and m. Line 2: the n inputs. Then m lines, each n weights followed by a bias. Then one line of m weights followed by the output bias.
Output
Four lines.
Constraints
1 ≤ n, m ≤ 20; |z| ≤ 50.
Example
Input:
2 2
1 2
0.5 -1 0
1 1 -1
1 0.5 -0.5
Output:
hidden: 0.0000 2.0000
z = 0.5000
output 0.6225
class 1
Why it works
Each layer turns its inputs into new features with a weighted sum and a non-linear activation; the sigmoid turns the final score into a probability, which is at least 0.5 exactly when the score is at least 0.
Examples
2 2 1 2 0.5 -1 0 1 1 -1 1 0.5 -0.5
hidden: 0.0000 2.0000 z = 0.5000 output 0.6225 class 1
1 1 3 2 -1 -1 4
hidden: 5.0000 z = -1.0000 output 0.2689 class 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each hidden neuron: z = its weights · x + its bias, then ReLU, max(0, z).
Hint 2
The output neuron reads the hidden values: z = v · h + c, then the sigmoid.
Hint 3
The class is 1 when the output is at least 0.5.
Solution
Show one correct solution (try it yourself first)
import math
n, m = map(int, input().split())
x = list(map(float, input().split()))
hidden = [list(map(float, input().split())) for _ in range(m)]
out = list(map(float, input().split()))
h = [max(0.0, sum(w * v for w, v in zip(row[:n], x)) + row[n]) for row in hidden]
z = sum(w * v for w, v in zip(out[:m], h)) + out[m]
p = 1 / (1 + math.exp(-z))
print("hidden:", *[f"{v:.4f}" for v in h])
print(f"z = {z:.4f}")
print(f"output {p:.4f}")
print(f"class {1 if p >= 0.5 else 0}")