fully connected layer forward pass only with 5x5 weight matrix, 25x1 bias vector, and 25x2 input size in matlab

Here is an example code for a fully connected layer forward pass with a 5x5 weight matrix, 25x1 bias vector, and 25x2 input size in MATLAB:

main.m
% Define the input matrix
input = rand(25, 2);

% Define the weight matrix and bias vector
weight = rand(5, 25);
bias = rand(5, 1);

% Compute the output of the fully connected layer
output = weight * input' + bias;

% Transpose the output matrix to match the desired size
output = output';
291 chars
13 lines

Explanation:

  1. We define an input matrix of size 25x2, which represents 25 input examples each with 2 features. The specific values of the input matrix are not important for this example.

  2. We define a weight matrix of size 5x25, representing the connections between the input layer and the fully connected layer. We also define a bias vector of size 5x1, which is added to each output node of the layer.

  3. We compute the output of the fully connected layer using the matrix multiplication between the weight matrix and the input matrix, and adding the bias vector. To do this, we first transpose the input matrix (using the ' operator) to match the expected dimensions of the weight matrix (25x2 -> 2x25). The result of this multiplication is a 5x25 matrix, which represents the activations of the 5 nodes in the fully connected layer for each of the 25 input examples.

  4. Finally, we transpose the output matrix (using the ' operator again) to match the desired dimensions of 25x5, where each row represents the activations of the 5 nodes for a specific input example.

gistlibby LogSnag