fully connected layer forward pass weight matrix shape 5x5, bias vector shape 1x25, input shape 25x2, batch size=2 in matlab in matlab

Here's the code to compute the forward propagation of a fully connected layer for a batch size of 2 in Matlab:

main.m
% define layer parameters
input_size = 2;
output_size = 25;
batch_size = 2;

weights = randn(input_size, output_size);
bias = randn(1, output_size);

% define input
input = randn(batch_size, input_size);

% compute layer activations for the batch of inputs
activations = input * weights + repmat(bias, batch_size, 1);
318 chars
14 lines

Explanation:

  • input_size and output_size represent the number of neurons in the input and output layer respectively.
  • batch_size represents the number of inputs processed in parallel.
  • weights is a matrix of size input_size x output_size which contains the weights connecting the input layer to the output layer.
  • bias is a vector of size 1 x output_size which contains the bias values for each neuron in the output layer.
  • input is a matrix of size batch_size x input_size which contains the input values for each neuron in the input layer.
  • activations is a matrix of size batch_size x output_size which contains the output values for each neuron in the output layer. The activations are computed by multiplying the input matrix with the weights matrix and adding the bias vector. This operation can be achieved using matrix multiplication and broadcasting the bias to match the same shape as the input matrix.

gistlibby LogSnag