PIXELBANKv9.1.0
Menu

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 WW and a bias vector bb. The forward pass involves computing the output of each neuron by taking the dot product of the input vector xx with the corresponding weights and adding the bias term.

To compute the output, follow these steps:

  1. Initialize output vector yy
  2. For each output neuron, compute the weighted sum of inputs
  3. Add the bias term to the weighted sum
y=Wx+b\mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b}

This technique is widely used in image classification tasks.

Example:

Input:
dense_forward([[1,2],[3,4]], [1,1], [0,0])
Output:
[3, 7]
Reasoning:
  • Interpret inputs as: weight matrix W=[1234]W=\begin{bmatrix}1 & 2\\3 & 4\end{bmatrix}, input vector x=[11]\mathbf{x}=\begin{bmatrix}1\\1\end{bmatrix}, bias vector b=[00]\mathbf{b}=\begin{bmatrix}0\\0\end{bmatrix}.
  • Compute WxW\mathbf{x}:
    • First output: 1â‹…1+2â‹…1=31\cdot1 + 2\cdot1 = 3
    • Second output: 3â‹…1+4â‹…1=73\cdot1 + 4\cdot1 = 7
  • Add bias b\mathbf{b} (which is zero), so the final output remains [3,7][3, 7].

Constraints:

  • W is a 2D weight matrix
  • x is input vector
  • b is bias vector
  • Return output rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Dense Layer Forward - Medium | PixelBank