Problems
Loading...
1 / 1

Linear Layer Forward

Neural Networks
Easy

A linear (fully connected) layer is the most fundamental building block in neural networks. It transforms an input by multiplying with a weight matrix and adding a bias vector. This operation is also called a dense layer or an affine transformation.

Given an input matrix X (n samples, d_in features), a weight matrix W (d_in x d_out), and a bias vector b (d_out), compute the linear layer output.

Algorithm

  1. For each sample i and output neuron j, compute the weighted sum plus bias:
Yij=kXikWkj+bjY_{ij} = \sum_{k} X_{ik} \cdot W_{kj} + b_j
  1. In matrix form:
Y=XW+bY = X \cdot W + b

Where b is broadcast (added to every row of XW).

Loading visualization...

Examples

Input:

X = [[1, 2], [3, 4]], W = [[1, 0], [0, 1]], b = [0, 0]

Output:

[[1, 2], [3, 4]]

With W as the identity matrix and zero bias, the output equals the input.

Input:

X = [[1, 2]], W = [[1], [2]], b = [3]

Output:

[[8]]

11 + 22 = 5, plus bias 3 gives 8. The layer reduces from 2 features to 1 output.

Hint 1

For each output element Y[i][j], compute the dot product of row i of X with column j of W, then add b[j]. Use nested loops: outer over samples, inner over output dimensions, innermost over input dimensions.

Hint 2

The matrix multiplication X @ W produces an n x d_out matrix. Then add b[j] to every element in column j. In Python: Y[i][j] = sum(X[i][k] * W[k][j] for k in range(d_in)) + b[j].

Requirements

  • Compute Y = XW + b for the given inputs
  • X is n x d_in, W is d_in x d_out, b is a d_out vector
  • The bias vector b is broadcast (added to each row)
  • Return an n x d_out list of lists of floats

Constraints

  • X has at least 1 row and 1 column
  • W has dimensions d_in x d_out matching X's column count
  • b has length d_out
  • Return an n x d_out list of lists
  • Time limit: 300 ms
Try Similar Problems