MATLAB: Defining number of neurons/layers in neural network

backpropagationDeep Learning ToolboxMATLABneural networkneural networks

Hello all,
I am applying a multiplayer perceptron with back-propagation and momentum to a classification problem. The data consists of 20 continuous features and 1 binary predictor (class: 0 or 1).
As the title suggests, I am unsure how to specify the number of neurons/layers in my network. I am using the traingdm function.
Using the following code, I have access to the number of neurons (3 here) but not the number of hidden layers:
%%Data

%Load Data

data = csvread('voice.csv');
%Seperate features from predictors

X = data(:, 1:end-1)';
Y = data(:, end)';
%%Model 1
net1 = patternnet(3,'traingdm');
net2 = train(net1, X, Y);
Preferably though, I would rather use the traingdm function like this as it gives access to a lot more parameters, however, I seem to lose the ability to specify the number of neurons:
%%Data
%Load Data
data = csvread('voice.csv');
%Seperate features from predictors
X = data(:, 1:end-1)';
Y = data(:, end)';
%%Model 2
net.trainFcn = 'traingdm';
net.trainParam.epochs = 10000; %Maximum number of epochs to train
net.trainParam.lr = 0.5; %Learning rate
net.trainParam.max_fail = 6; %Validation vectors are used to stop training
%early if the network performance on the validation vectors fails to improve
%or remains the same for max_fail epochs in a row.
net.trainParam.mc = 0.3 % Momentum constant
[net,tr] = train(net, X, Y)
I've read the documentation: http://uk.mathworks.com/help/nnet/ref/traingdm.html but can't seem to find parameters for number of hidden layers or number of neurons in each layer.
I also can't seem to manually define training/validation/testing sets, it has defaults it uses (70/15/15) but I can't access these values and change them. (I can through the GUI using nnstart but then I lose access to the other parameters – eg learning rate).
Lastly, I can't seem to specify my own activation function.
Assuming I can get around these issues, the end goal would be to do a grid-search to find the best set of parameters for this problem.
I've attached the .csv file in case it's of interest/use to anyone.
Thanks in advance,
RĂ©mi

Best Answer

If you take a look at the example on the "traingdm" documentation page, you will see a call to "feedforwardnet":
net = feedforwardnet(3,'traingdm');
The first argument is a vector specifying one or more hidden layer sizes, so if you want more hidden layers:
net = feedforwardnet([3,4,5],'traingdm');
Alternatively, you could change these values from the "net" object:
net.numLayers = 5;
net.layers{1}.size = 4;
You can change the proportion of train/test and validation sets as follows:
net.divideParam.trainRatio = 80/100;
net.divideParam.valRatio = 10/100;
net.divideParam.testRatio = 10/100;
Setting a custom activation function is more difficult, but the following discussion could be a good place to start:
This link is useful for Neural Network object properties: