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

The forward pass of a fully connected layer multiplies the input with the weight matrix, adds the bias vector, and applies the activation function. In MATLAB, this can be done as:

main.m
% Weight matrix
W = rand(5, 5);

% Bias vector
b = rand(25, 1);

% Input matrix
X = rand(25, 2);

% Compute the forward pass
Z = X*W + b';
A = sigmoid(Z);
155 chars
13 lines

Here, the weight matrix W has dimensions 5x5, the bias vector b has dimensions 25x1, and the input matrix X has dimensions 25x2.

The forward pass matrix multiplication X*W will result in a matrix with dimensions 25x5, which when added to the bias vector b' (transpose of b) of dimensions 1x25, results in a matrix with dimensions 25x25. Applying the sigmoid activation function element-wise to this matrix results in the output matrix A with dimensions 25x25.

Note that the sigmoid function needs to be defined separately in MATLAB as:

main.m
% Sigmoid activation function
function y = sigmoid(x)
    y = 1./(1 + exp(-x));
end
84 chars
5 lines

This can be put in a separate file and called within the main code.

gistlibby LogSnag