Lab (1)
Neural Network – Perceptron Architecture

Objective:
Learn to create Perceptron networks

You will use MATLAB's Neural Network Toolbox to complete this lab.   The manual for Neural Network Toolbox is available under the resource page.

A Perceptron can be created using the newp function, usually, by running a command like this:

net = newp(PR,S,TF,LF)

Perceptrons are used to solve simple (i.e. linearly separable) classification problems.

Use the MATLAB help on newp to explain what each parameter of the function mean:

PR:

S :

TF:

LF:

The following command creates a Perceptron network with a single one-element input vector and one neuron.  The range for the input is [0 2].

net = newp([0 2], 1);

To view everything that is created, you can run the above command without the ";".

At this time the weights and biases are set to the default values. The default learning function is learnp. The net input to the hardlim transfer function is dotprod.  Thus, the DOT product of the input vector and weight matrix will be computed then the bias will be added to compute the net input to the transfer function. The default initialization function, initzero, is used to set the initial values of the weights to zero.

To check the weights and biases, we can run:

inputweights = net.inputweights{1, 1}

biases = net.biases{1}

Simulation (sim)

When a network is created, that does not necessary mean it is ready for use.  A network should be trained for the given cases, and then be used for other inputs. Here we will try an example in which we set the weights and biases manually.  This means, we set the parameters and will run the network.  If we are happy with the outcome, then we will keep the weights and the biases. If we are no happy, we make some changes, and will try again the network again.

Example

Suppose we want to create a perceptron network with a single-neuron, one bias, and two inputs.  This network will separate some patterns from each other.  The limits for the input are [-1 and 1].  As we mentioned before, when you create a perceptron network (all networks in MATLAB), the weights and the bias are set to 0 by default.
net = newp([-1 1; -1 1], 1);

Let’s set the weights to: w11 = -1, w12 = 1, and bias = 1.
net.IW{1,1} = [-1 1];
net.b{1} = [1];

Note: at anytime of the process, if you want to check the outcome, just ignore the ; at the end of the command.  Now, let's create an inputs.  Each input matrix in our case should have two values, a pair.
p = [1  1   -1   -1 ; 1   -1   1   -1]

You can run a simulation of this network using:

a1 = sim(net, p)

What is the output for p_new = [1 –1]?

OR Gate Perceptron Network

This code creates a perceptron layer with one 2-element input (ranges [0 1] and [-2 2]) and one neuron. (Supplying only two arguments to NEWP results in the default perceptron learning function LEARNP being used.)

net = newp([0 1; -2 2],1);

Now we define a problem, an OR gate, with a set of four 2-element input vectors  P and the corresponding four 1-element targets T.

P = [0 0 1 1; 0 1 0 1];
T = [0 1 1 1];

Here we simulate the network's output, train for a maximum of 20 epochs, and then simulate it again.

Y = sim(net,P)
net.trainParam.epochs = 20;
net = train(net,P,T);
Y = sim(net,P)

Note:  Just in case you want to reset the weights and biases back to the default values (0), you can use the init command. For example in the network that we just created, we can type:

net= init(net).

to reset them back to 0 again.

Sometimes, one may want to assign the weights and biases randomly. There is a function that does this:

net.inputweights{1,1}.initFcn = ‘rands’;

net.biases{1}.initFcn = ‘rands’;
net = init(net);
Let’s check it out:
wts = net.IW{1,1}

What do you have for the weights and the bias?
Try your network with these parameter for the two inputs and see what you will get this time?

You can use the nntool command to create neural networks using a Graphical Interface.  Try to see if you can create the above network using that tool.

Lab Assignment - Due at the end of the lab
Now that you learned to set up a Perceptron network, design a network to separate apples and oranges using the weights and biases given in class,
.
Thus, the input parameter for an apple and orange, respectively, are:
and
The network produces [1] for orange and [-1] for apple.
Once your network is trained, try the following inputs:

and then

What did your network produce for these two cases?

What to submit?
The list of all commands that you have used following by the outcome of MATLAB run. You can create a blank file and cut an paste your commands as you will progress. Then print that file or send it as an e-mail attachment.  You can also send the file in the body of your e-mail.