output shape fully connected layer with batch size=2, weights shape=25x25, bias shape=1x25, input shape=25x2 in matlab

To compute the output of a fully connected layer in a neural network with batch size 2, weight shape of 25x25, bias shape of 1x25, and input shape of 25x2 in matlab, we can use matrix multiplication.

main.m
% Define the inputs
inputs = randn(25, 2, 2);  % input shape: 25x2x2 (batch size 2)

% Define the weights and bias
weights = randn(25, 25);  % weight shape: 25x25
bias = randn(1, 25);      % bias shape: 1x25

% Compute the output
output = reshape(weights * reshape(inputs, [], 2), [1, 1, 25, 2]) + bias;

% The output shape should be 1x1x25x2
disp(size(output))  % should output [1, 1, 25, 2]
393 chars
13 lines

Here, we first define the inputs with shape 25x2x2, representing the 2 input examples in the batch. We also define the weights with shape 25x25 and the bias with shape 1x25.

To compute the output, we first reshape the inputs to have dimensions (25x2) and perform matrix multiplication with the weights (resulting in a (25x2) matrix). We then reshape this result to have dimensions (1x1x25x2), representing the 2 output examples in the batch. Finally, we add the bias to each output example.

The resulting output should have shape 1x1x25x2, representing the 2 examples in the batch with output dimension of 25.

gistlibby LogSnag