When a trained network is saved to disk, it isn't saved layer by layer. Every weight and every bias is written out one after another into one long flat list of numbers. To use the network again you need two things: that list and the layer widths. Nothing in the list marks where one layer's numbers stop and the next layer's start. Only the widths tell you that.
Task: write network_forward(batch, sizes, params). It rebuilds the network from params and runs a batch through it.
sizes lists the layer widths in order and has at least two entries. sizes[0] is the number of input values. The input layer is just the data arriving, so it has no neurons and no parameters. Every later entry is a layer of neurons, and the last one is the output layer.params holds all of these, layer by layer, starting from the first layer after the input. Within a layer the weights come first, neuron by neuron: all of the first neuron's incoming weights (in the order of the units they come from), then all of the second neuron's, and so on. That layer's biases follow, one per neuron, in neuron order. Then the next layer begins.batch is a list of examples, and each example has sizes[0] values. Return one row per example, holding the output layer's values rounded to 4 decimal places. Round only at the end.params always has exactly the right length for sizes.For example, with sizes = [3, 2, 1] the list holds numbers, laid out like this:
| positions | what they are |
|---|---|
| 0 – 2 | hidden neuron 1's weights, from inputs 1, 2, 3 |
| 3 – 5 | hidden neuron 2's weights, from inputs 1, 2, 3 |
| 6 – 7 | the two hidden biases |
| 8 – 9 | the output neuron's weights, from hidden neurons 1, 2 |
| 10 | the output bias |
The first test is small enough to check by hand: two inputs that are each or , two hidden neurons and one output. Once your code runs, look at the four answers it gives. No single neuron could produce that column of answers, because no straight line splits those four points into the two groups the network makes. The hidden layer is what bends the boundary.