Deep learning: Forward pass through a network

Run a multi-layer network on inputs, as it does when making predictions.

What to build

A program that computes the outputs of a multi-layer network and its predicted class.

Requirements

  • Each layer has m neurons and n inputs (n equals the previous layer's m) and an activation relu, sigmoid, tanh or linear.
  • For each sample, print the final outputs with four decimal places, then -> class K (K from 0).

Input

Line 1: L. For each layer: a line m n activation, m rows of weights, a line of biases. Then k and k samples.

Output

k lines.

Constraints

L ≤ 6; layers have at most 20 neurons.

Example

Input:

2
2 2 relu
1 -1
1 1
0 -1
2 2 linear
1 0
-1 2
0 0
2
1 0
2 3

Output:

1.0000 -1.0000 -> class 0
0.0000 8.0000 -> class 1

Why it works

Each layer turns the previous layer's outputs into new features, so applying the layers in order computes the network's function.

Examples

Example 1 · Input
2
2 2 relu
1 -1
1 1
0 -1
2 2 linear
1 0
-1 2
0 0
2
1 0
2 3
Expected output
1.0000 -1.0000 -> class 0
0.0000 8.0000 -> class 1
Example 2 · Input
1
2 2 linear
1 0
0 1
0 0
1
5 7
Expected output
5.0000 7.0000 -> class 1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Store each layer as (W, b, activation) and apply them one after another; each layer's output is the next layer's input.

Hint 2

The predicted class is the index of the largest final output; on a tie, the first.

Solution

Show one correct solution (try it yourself first)
import math

F = {
    "relu": lambda v: max(0.0, v),
    "sigmoid": lambda v: 1 / (1 + math.exp(-v)),
    "tanh": math.tanh,
    "linear": lambda v: v,
}
layers = []
for _ in range(int(input())):
    m, n, activation = input().split()
    W = [list(map(float, input().split())) for _ in range(int(m))]
    b = list(map(float, input().split()))
    layers.append((W, b, F[activation]))
for _ in range(int(input())):
    a = list(map(float, input().split()))
    for W, b, f in layers:
        a = [f(sum(w * x for w, x in zip(row, a)) + bi) for row, bi in zip(W, b)]
    print(" ".join(f"{v:.4f}" for v in a) + f" -> class {a.index(max(a))}")

Lesson: Neurons, layers and activation functions →