create fully connected layer scratch in matlab

To create a fully connected layer from scratch in MATLAB, we need to define the weights and biases for the layer and implement the forward pass procedure. Here's an example implementation:

main.m
classdef FullyConnectedLayer < nnet.layer.Layer
    
    properties
        InputSize
        OutputSize
        Weights
        Bias
    end
    
    methods
        function layer = FullyConnectedLayer(outputSize, name)
            layer.Name = name;
            layer.OutputSize = outputSize;
        end
        
        function layer = initialize(layer, inputSize)
            layer.InputSize = inputSize;
            layer.Weights = randn(layer.OutputSize, layer.InputSize);
            layer.Bias = randn(layer.OutputSize, 1);
        end
        
        function Z = predict(layer, X)
            Z = layer.Weights*X + layer.Bias;
        end
    end
end
665 chars
27 lines

In this implementation, we define a custom class FullyConnectedLayer that extends MATLAB's nnet.layer.Layer class. The properties InputSize, OutputSize, Weights, and Bias are defined to store layer information.

The initialize method initializes the layer's weights and biases with random normal values. The predict method defines the forward pass computation, where we take the input X and output the result Z computed by the formula Z = Weights*X + Bias.

To use this layer in a neural network model, we can create an object of this layer and add it to our model:

main.m
layer = FullyConnectedLayer(10, 'fc');
net = nnet.SequentialLayer();
net.add(layer);
85 chars
4 lines

gistlibby LogSnag