Compute a whole layer of neurons at once.
What to build
A program that applies a fully connected layer to several input samples.
Requirements
- The layer has m neurons and n inputs: W is m × n, b has m values, and the activation is
relu,sigmoid,tanhorlinear. - For each sample, print the m outputs with four decimal places, separated by spaces.
Input
Line 1: m, n and the activation. Then m rows of W. Then the biases. Then k and k samples.
Output
k lines.
Constraints
m, n ≤ 20; k ≤ 50.
Example
Input:
2 2 relu
1 -1
0.5 0.5
0 1
2
2 1
0 3
Output:
1.0000 2.5000
0.0000 2.5000
Why it works
Every neuron reads the same inputs with its own weights, so a layer is a matrix-vector product followed by the activation.
Examples
Example 1 · Input
2 2 relu 1 -1 0.5 0.5 0 1 2 2 1 0 3
Expected output
1.0000 2.5000 0.0000 2.5000
Example 2 · Input
1 1 linear 2 -1 3 0 1 -2
Expected output
-1.0000 1.0000 -5.0000
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each output is one row of W dotted with the input, plus that row's bias, then the activation.
Hint 2
Use the same function for every sample.
Solution
Show one correct solution (try it yourself first)
import math
m, n, activation = input().split()
m = int(m)
W = [list(map(float, input().split())) for _ in range(m)]
b = list(map(float, input().split()))
f = {
"relu": lambda v: max(0.0, v),
"sigmoid": lambda v: 1 / (1 + math.exp(-v)),
"tanh": math.tanh,
"linear": lambda v: v,
}[activation]
for _ in range(int(input())):
x = list(map(float, input().split()))
out = [f(sum(w * xi for w, xi in zip(row, x)) + bi) for row, bi in zip(W, b)]
print(" ".join(f"{v:.4f}" for v in out))