0
votes

I'm trying to construct a class framework for a neural network (ANN) in Matlab by defining a Node class:

function NodeObject = Node(Input, Weights, Activation)

Features.Input      = [];
Features.Weights    = [];
Features.Activation = [];

Features.Input      = Input;
Features.Weights    = Weights;
Features.Activation = Activation;

NodeObject = class(Features, 'Node');

Where here the input is an integer (expected number of inputs), Weights is a vector of length Features.Input, and Features.Activation is a string that references an activation function stored in the methods.

What I want to do next is construct a cell array of nodes and define a Network class based on this array:

function Architecture = Network(NodeArray) 

ANN.Layers      = []; % Number of layers in architecture
ANN.LayerWidths = []; % Vector of length ANN.Layers containing width of each layer
ANN.NodeArray   = []; % Original input is cell array with layers in ascending order (input at top, output at bottom) with nodes in each row.
ANN.InputSizes  = [];

% Find number of layers
ANN.Layers = length(NodeArray(:,1));

% Find width of each layer
Widths = zeros(ANN.Layers,1);
for i = 1:length(Widths)
    Widths(i) = length(NodeArray(:,i));
end
ANN.LayerWidths = Widths;

% Pass NodeArray to Network class
ANN.NodeArray = NodeArray;

% Construct cell of input sizes
InputSizes = [];
for i = 1:ANN.Layers
    for j = 1:Widths(i)
        InputSizes(i,j) = NodeArray{i,j}.Inputs;
    end
end

ANN.InputSizes = InputSizes;



Architecture = class(ANN, 'Network');

The attribute ANN.InputSizes tries to extract the attributes from a Node object, but my code doesn't allow me to do this. How do I amend this problem, or do you recommend a different architecture to this problem all together? Currently my classes Node and Network are contained in two separate directories, but I have a feeling that there is something else I'm not seeing. For reference, I have absolutely no prior experience in OOP, and from what I've gathered it seems Matlab is not the best environment in which to implement these structures. At the moment though I don't have enough experience to implement this type of framework in another language.

1
It may be easier to use the classdef format for defining your classes. - Suever
@Suever I did try defining a Node2 class with the classdef format saved in the @Network directory, but the Network definition didn't recognize it. Part of this problem is in how I'm defining my classes, but part of it too might be how I'm linking my directories. - Mnifldz
The @Folder has to have the same name as the class, so it would need to be @Node2. But with classdef definitions, you don't need the @Folder at all. - Suever

1 Answers

4
votes

Your InputSizes isn't a cell. You initialize it to a double array ([]) and then fill it as such. If you want to define it as a cell you should do something like

InputSizes = cell();
for i = 1:ANN.Layers
    for j = 1:Widths(i)
        InputSizes{i,j} = NodeArray{i,j}.Inputs;
    end
end

All of that, you should really look into defining your classes using a classdef file, as it is much more straightforward.

Node.m

classdef Node < handle

    properties
        Inputs
        Weights
        Activation
    end

    methods
        function obj = Node(inputs, weights, activation)
            obj.Inputs = inputs;
            obj.Weights = weights;
            obj.Activation = activation;
        end
    end
end

Network.m

classdef Network < handle
    properties
        NodeArray
    end

    properties (Dependent)
        Layers
        LayerWidths
        InputSizes          
    end

    methods
        function obj = Network(nodes)
            obj.NodeArray = nodes;
        end

        function result = get.Layers(obj)
            result = size(obj.NodeArray, 1);
        end

        function result = get.LayerWidths(obj)
            result = size(obj.NodeArray, 2);
        end

        function result = get.InputSizes(obj)
            result = arrayfun(@(x)x.Inputs, obj.NodeArray, 'uniformoutput', 0);
        end
    end
end

As far as suggesting a better layout, that's subject to the opinion of the individual developer.