Vous êtes sur la page 1sur 13

Neural Computing

Lab 01

Basic Functionalities of Matlab Neural Network Toolbox

Name E.N Number

: M.M.P Perera : EN 11 5132 92

Abstract
Neural networks are composed of simple elements operating in parallel. These elements are inspired by biological nervous systems. As in nature, the network function is determined largely by the connections between elements. We can train a neural network to perform a particular function by adjusting the values of the connections (weights) between elements. In this lab, learn a few basic functionalities of Matlab Neural Network Toolbox.

Table of Contents

Abstract ................................................................................................................................................... 2 Table of Contents .................................................................................................................................... 3 1 Introduction .................................................................................................................................... 4 1.1 2 Theory: Neuron with Vector Input .......................................................................................... 4

Methodology................................................................................................................................... 5 2.1 Building the first neural .......................................................................................................... 5 Answers for the exercise ................................................................................................. 5

2.1.1 2.2 3 4

Predicting Prices of Houses ..................................................................................................... 7

Discussion...................................................................................................................................... 12 Reference ...................................................................................................................................... 13

List of Figures Figure 1-1 : Particular input leads to a specific target output ................................................................ 4 Figure 1-2 : Neuron with Vector Input .................................................................................................... 4 Figure 2-1 : Selecting Data Input ............................................................................................................. 7 Figure 2-2 : Select Parentages................................................................................................................. 8 Figure 2-3 : Neuron network size ............................................................................................................ 8 Figure 2-4 :Results................................................................................................................................... 8 Figure 2-5 : Performance of the network ............................................................................................... 9 Figure 2-6 : Regression plots ................................................................................................................... 9 Figure 2-7 : Save results as a M-file ...................................................................................................... 10

1 Introduction
The neural networks is a way to model any input to output relations based on some input output data when nothing is known about the model. Neural networks are often work in parallel. These elements are inspired by biological nervous systems. As in nature, the network function is determined largely by the connections between elements. We can train a neural network to perform a particular function by adjusting the values of the connections between elements. Commonly neural networks are adjusted, or trained, so that a particular input leads to a specific target output. Such a situation is shown below. There, the network is adjusted, based on a comparison of the output and the target, until the network output matches the target. Typically many such input/target pairs are used, in this supervised learning, to train a network.

Figure 1-1 : Particular input leads to a specific target output

Batch training of a network proceeds by making weight and bias changes based on an entire set (batch) of input vectors. Incremental training changes the weights and biases of a network as needed after presentation of each individual input vector. Incremental training is sometimes referred to as on line or adaptive training. Neural networks have been trained to perform complex functions in various fields of application including pattern recognition, identification, classification, speech, vision and control systems. Today neural networks can be trained to solve problems that are difficult for conventional computers or human beings. Throughout the toolbox emphasis is placed on neural network paradigms that build up to or are themselves used in engineering, financial and other practical applications.

1.1 Theory: Neuron with Vector Input


A neuron with a single R-element input vector is shown below. Here the individual element inputs p1, p2,... pR are multiplied by weights w1, 1, w1, 2, ... w1, R and the weighted values are fed to the summing junction. Their sum is simply Wp, the dot product of the (single row) matrix W and the vector p.

Figure 1-2 : Neuron with Vector Input

The neuron has a bias b, which is summed with the weighted inputs to form the net input n. This sum, n, is the argument of the transfer function f. n = w11 p1 + w12 p2 + ... + w1R pR + b

2 Methodology
We use MATLAB Neural Network Toolbox to do this lab. In order to get the MATLAB Neural Network Toolbox we typed nftool command.

2.1 Building the first neural Following matlab syntax shows how to calculate the net inputs of a neuron if, W = [1 2], b=0 p1 = 2, p2 = 1 net = linearlayer; net.inputs{1}.size = 2; net.layers{1}.dimensions = 1; net.IW{1,1} = [1 2]; net.b{1} = 0; P = [2; 1]; A = net(P)

2.1.1

Answers for the exercise

1) Draw a neural with two inputs and one output.

1. Initialize the Neural Network: net = linearlayer; net.inputs{1}.size = 2; net.layers{1}.dimensions = 1; 2. Set the weight to [1 2] and bias to 0: net.IW{1,1} = [1 2]; net.b{1} = 0;

3. Define inputs p1 = 2, p2 = 3: P = [2; 3];

4. To calculate outputs. A = net (P) A = 8 5. net.IW{1,1} = [2 3]; net.b{1} = 2; P = [-2; -3]; A = net(P) A =-11

6. Then we Changed the input vector to P = [1 2;2 1] and calculated the output
net.IW{1,1} = [1 2]; P = [1 2; 2 1]; A = net (P) A = 7 6

8. for the above neural network design we got two values as the output because we are giving two sets of inputs and also we have defined the size of the matrix as two.
9. Set P = [1 2 2 3; 2 1 3 1] and calculate the output P = [1 2 2 3;2 1 3 1]; A = net(P) A = 7 6 10 7 10. For the above neural network we got four values as outputs because we gave four sets of inputs. The rows of the input values mean the two values of a input. The columns mean the different number of inputs.

11) No it wont work for this neural network because the matrix dimensions are not correct 12) P = [1; 2; 2; 3; 2; 1; 3; 1] Next we build a neural network which supports the input [1;2;2;3;2;1;3;1].
net = linearlayer; % defines the network net.inputs{1}.size = 8; % defines the size of the input net.layers{1}.dimensions = 1; % number of neurons net.IW{1,1} = [1 1 1 1 2 2 2 2]; % weight matrix net.b{1} = 0; % bias P = [1;2;2;3;2;1;3;1]; A = net(P) A = 22

The input is an 8*1, matrix and the weight is a 1*8 matrix therefore we get the multiply of those two matrixes we are getting a 1*1 matrix.

2.2 Predicting Prices of Houses


Function fitting is the process of training a neural network on a set of inputs in order to produce an associated set of target outputs. Once the neural network has fit the data, it forms a generalization of the input-output relationship and can be used to generate outputs for inputs it was not trained on. There are some of the attributes of the provided dataset. It has following attributres. houseInputs - a 13x506 matrix defining thirteen attributes of 506 different neighborhoods. 1. Per capita crime rate per town 2. Proportion of residential land zoned for lots over 25,000 sq. ft. 3. Proportion of non-retail business acres per town 4. 1 if tract bounds Charles river, 0 otherwise 5. Nitric oxides concentration (parts per 10 million) 6. Average number of rooms per dwelling 7. Proportion of owner-occupied units built prior to 1940 8. Weighted distances to five Boston employment centres 9. Index of accessibility to radial highways 10. Full-value property-tax rate per $10,000 11. Pupil-teacher ratio by town 12. 1000(Bk - 0.63)^2, where Bk is the proportion of blacks by town 13. Percent lower status of the population houseTargets - a 1x506 matrix of median values of owner-occupied homes in each neighborhood in 1000's of dollars. This dataset can be used to train a neural network to estimate the median house price in a neighborhood based on neighborhood statistics using Matlab NN toolbox. We had to follow some step to train the system. These steps are shown in below.

Step 1
First Matlab fitting tool is started by using the command nftool; >> nftool

Step 2
Then go through the prompts and houseInputs dataset and houseOutputs dataset are imported to the Input Data and Target Data.

Figure 2-1 : Selecting Data Input

Step 3
Then the validation and test is set to 15% of the total sample.

Figure 2-2 : Select Parentages

With these settings, the input vectors and target vectors will be randomly divided into three sets as follows: 70% will be used for training. 15% will be used to validate that the network is generalizing and to stop training before over fitting. The last 15% will be used as a completely independent test of network generalization.

Step 4
The number of hidden neurons is set to 10. Increasing the number of neurons will increase the accuracy.

Figure 2-3 : Neuron network size

Step 5
Then click Train icon to train the system.

8
Figure 2-4 :Results

Step 6
Next, work through the prompts and plot the performance of the network.

Figure 2-5 : Performance of the network

The mentioned plots (Consider about above figure) are the systems behavior plots which describe its behavior of training the system over the test set and validation set. If the training set and the validation set is similar and the error is less in the training set and other two data sets, the system is said to be properly trained. If the testing set is increased more than the validation set there would be an over fitting. If the error is too large the system is may not properly trained. Increase the number of hidden neurons in the hidden layer give the network more flexibility because the network has more parameters it can optimize. It can be observed by changing the hidden neurons parameter. In this session we also click the view Regression button. This is used to validate the network performance. The following regression plots display the network outputs with respect to targets for training, validation, and test sets. For a perfect fit, the data should fall along a 45 degree line, where the network outputs are equal to the targets. For this problem, the fit is reasonably good for all data sets, with R values in each case of 0.93 or above. If even more accurate results were required, we could retrain the network by clicking Retrain in nftool. This will change the initial weights and biases of the network, and may produce an improved network after retraining. Other options are provided on the following pane.

9
Figure 2-6 : Regression plots

Step 7
Then Mfile is generated by clicking Genarate M-file.

Figure 2-7 : Save results as a M-file

The generated M-file is shown in following. % % % % % % % % Solve an Input-Output Fitting problem with a Neural Network Script generated by NFTOOL Created Thu July 31 12:36:49 IST 2013 This script assumes these variables are defined: houseInputs - input data. houseTargets - target data.

inputs = houseInputs; targets = houseTargets; % Create a Fitting Network hiddenLayerSize = 10; net = fitnet(hiddenLayerSize);

% Setup Division of Data for Training, Validation, Testing net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100;

% Train the Network [net,tr] = train(net,inputs,targets); % Test the Network outputs = net(inputs); errors = gsubtract(targets,outputs);

10

performance = perform(net,targets,outputs) % View the Network view(net) % Plots % Uncomment these lines to enable various plots. %figure, plotperform(tr) %figure, plottrainstate(tr) %figure, plotfit(net,inputs,targets) %figure, plotregression(targets,outputs) %figure, ploterrhist(errors)

%**********************************************************************

11

3 Discussion
In this lab, we learn a few basic functionalities of Matlab Neural Network Toolbox. The network function is determined largely by the connections between elements. We can train a neural network to perform a particular function by adjusting the values of the connections (weights) between elements. We gain knowledge of how to achieve best validation performance by tanning the neurons.

12

4 Reference
[1] Neural network - Wikipedia, the free encyclopedia. 2013. Neural network - Wikipedia, the free encyclopedia. [ONLINE] Available at: http://en.wikipedia.org/wiki/Neural_network. [3] Neural Network for pattern recognition- Tutorial - File Exchange - MATLAB Central. 2013. Neural Network for pattern recognition- Tutorial - File Exchange - MATLAB Central. [ONLINE] Available at: http://www.mathworks.com/matlabcentral/fileexchange/19997-neural-network-for-pattern-recognition-tutorial. [4] Introduction - Prediction using neural networks. 2013. Introduction - Prediction using neural networks. [ONLINE] Available at: http://www.obitko.com/tutorials/neural-network-prediction/. [5] Neural Network Toolbox - MATLAB - MathWorks India. 2013. Neural Network Toolbox - MATLAB MathWorks India. [ONLINE] Available at: http://www.mathworks.in/products/neural-network/. [Accessed 01 August 2013]. [6] Getting Started with Neural Network Toolbox - MATLAB Video - MathWorks India. 2013. Getting Started with Neural Network Toolbox - MATLAB Video - MathWorks India. [ONLINE] Available at: http://www.mathworks.in/videos/getting-started-with-neural-network-toolbox-68794.html.

13

Vous aimerez peut-être aussi