Designing A Custom Neural Network In MATLAB

Posted on Updated on

The MATLAB Neural Network toolbox ships with numerous predefined and canonical neural nets, however sometimes you may need to create a custom net with just the right connections, biases and hidden layers to suite your particular problem domain. To achieve this goal we can use the matlab network object. The  network object allows granular design of neural networks by exposing all properties of the net that we are designing. The preceding code demonstrates how to build a simple neural to learn the truth table for Logical AND.

First lets look at the Logical AND truth table:

p q p ∧ q

Open a new edit window in MATLAB and enter the following code:

    1. This creates an empty network object and assigns it to the net variable, sets up the number of inputs and uses cell array syntax to index into its properties.
      %% Design Custom Neural Network
      net = network;                                  % create network
      net.numInputs = 2;                              % set number of inputs
      net.inputs{1}.size = 1;                         % assign 2 to input size
      net.inputs{2}.size = 1;
      net.numLayers = 1;                              % add 1 layer to network
      net.layers{1}.size = 1;                         % assign number of neurons in layer
      net.inputConnect(1) = 1;                        % connet input to layer 1
      net.inputConnect(2) = 1;
      net.biasConnect(1) = 1;                         % connect bias to layer 1
      net.biases{1}.learnFcn = 'learnp';              % set bias learning function
      net.biases{1}.initFcn = 'initzero';             % set bias init function
      net.outputConnect(1) = 1;
      net.layers{1}.transferFcn = 'hardlim';          % set layer transfer function [hard limit]
      net.inputWeights{1}.initFcn = 'initzero';       % set input wieghts init function
      net.inputWeights{1}.learnFcn = 'learnp';        % set input weight learning function
      net.inputWeights{2}.learnFcn = 'learnp';
      net.inputWeights{2}.initFcn = 'initzero';
      net.initFcn = 'initlay';                        % set network init function
      net.trainFcn = 'trainc';                        % set network training function
      net.performFcn = 'mae';                         % set network perf evaluation function
      net = train(net,[0 0 1 1;0 1 0 1],[0 0 0 1]) ;  % train network
    2. Custom Network Diagram:
    3. Test Network
      In the command window type

      This should output a 1 to the command window indicating 1 AND 1 = 1

3 thoughts on “Designing A Custom Neural Network In MATLAB

    Viraj Gamage said:
    May 22, 2014 at 11:17 am

    it is a very helpful article. I am working on a project that predict future exchange rates between two currencies. I am using a neural network and I have been adviced to use newff function in matlab. What I cannot understand there is usage of newff function. when creating new neural network how to define number of input nodes it has. I am giving set of inputs(say 10 inputs ) and take one single output. How to use newff function here??

    siddharth185 said:
    August 4, 2014 at 11:23 am

    Hi bro,
    this is a very useful article.
    I am really stuck trying to make a specific neural net arcitecture in matlab.
    can we configure the connections between specific neurons in a neural net?
    I am looking for a structure which looks like this –
    Is this possible in matlab?
    Thanks a lot!

      Yaqi said:
      June 2, 2016 at 11:52 am

      Could you share the code you used for the structure in your picture? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s