Dense Layer Forward
Implement forward pass of a dense (fully connected) layer, a fundamental component in Neural Networks. This process is crucial for understanding how artificial neurons process inputs to produce meaningful outputs.
In the context of Neural Networks, a dense layer is where every input is connected to every output by a weight matrix and a bias vector . The forward pass involves computing the output of each neuron by taking the dot product of the input vector with the corresponding weights and adding the bias term.
To compute the output, follow these steps:
- Initialize output vector
- For each output neuron, compute the weighted sum of inputs
- Add the bias term to the weighted sum
This technique is widely used in image classification tasks.
Example
dense_forward([[1,2],[3,4]], [1,1], [0,0])
[3, 7]
- Inputs:
Wis a weight matrix where row holds the weights feeding output neuron , so it has one row per output and one column per input.xis the input vector andbgives one bias per output neuron. - Neuron 1: take row 1 of against : .
- Neuron 2: row 2 against : .
- Output: , a vector whose length equals the number of rows of , not the length of . Rounding to 4 decimal places leaves these whole numbers unchanged.
- Notice: with all ones, each is just the sum of row of . A non-zero bias would simply shift each output, e.g. would give .
Constraints
- W is a 2D weight matrix
- x is input vector
- b is bias vector
- Return output rounded to 4 decimal places