AI Lab11 Task
AI Lab11 Task
1|Page
– sensor networks: weather measurements, traffic. . .
– people: social networks, blogs, mobile phones, purchases, bank transactions. . .
• Data is not random; it contains structure that can be used to predict outcomes, or gain knowledge in
some way.
Ex: patterns of Amazon purchases can be used to recommend items.
• It is more difficult to design algorithms for such tasks (compared to, say, sorting an array or
calculating a payroll). Such algorithms need data.
Ex: construct a spam filter, using a collection of email messages labelled as spam/not spam.
• Data mining: the application of ML methods to large databases.
• Ex of ML applications: fraud detection, medical diagnosis, speech or face recognition. . .
• ML is programming computers using data (past experience) to optimize a performance criterion.
• ML relies on:
– Statistics: making inferences from sample data.
– Numerical algorithms (linear algebra, optimization): optimize criteria, manipulate models.
– Computer science: data structures and programs that solve a ML problem efficiently.
• A model:
– is a compressed version of a database;
– extracts knowledge from it;
– does not have perfect performance but is a useful approximation to the data.
Examples of ML problems
• Supervised learning: labels provided.
– Classification (pattern recognition):
∗ Face recognition. Difficult because of the complex variability in the data: pose and illumination in a
face image, occlusions, glasses/beard/make-up/etc.
∗ Optical character recognition: different styles, slant. . .
∗ Medical diagnosis: often, variables are missing (tests are costly).
∗ Speech recognition, machine translation, biometrics. . .
∗ Credit scoring: classify customers into high- and low-risk, based on their income and savings, using
data about past loans (whether they were paid or not).
Supervised learning
Learning a class from examples: two-class problems
• We are given a training set of labeled examples (positive and negative) and want to learn a
classifier that we can use to predict unseen examples, or to understand the data.
2|Page
• Input representation: we need to decide what attributes (features) to use to describe the input
patterns (examples, instances). This implies ignoring other attributes as irrelevant.
– Regression: the labels to be predicted are continuous:
∗ Predict the price of a car from its mileage.
∗ Navigating a car: angle of the steering.
∗ Kinematics of a robot arm: predict workspace location from angles.
LAB Tasks:
Task 01: Train a neural network to estimate an engines torque and emissions from its fuel use and
speed.
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.
engineInputs - a 2x1199 matrix defining two attributes of a given engines activity under different
conditions:
1. Fuel rate
2. Speed
1. Torque
2. Nitrous oxide emissions
[X,T] = engine_dataset loads the inputs and targets into variables of your own choosing.
Here is how to design a fitting neural network with 10 hidden neurons with this data at the command
line. See fitnet for more details in Matlab.
[x,t] = engine_dataset;
net = fitnet(10);
net = train(net,x,t);
view(net)
y = net(x);
Solution:
1. Open the Neural Network Start GUI with this command:
>> nnstart
3|Page
2. Click Fitting app to open the Neural Network Fitting App.
3. Click Next to proceed.
4|Page
4. Click Load Example Data Set in the Select Data window. The Fitting Data Set Chooser
window opens.
5|Page
Engine data_set
Chemical
Engine
Engine dataset
5. Select Engine, and click Import. This returns you to the Select Data window.
6. Click Next to display the Validation and Test Data window, shown in the following figure.
The validation and test data sets are each set to 15% of the original data.
6|Page
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
overfitting.
The last 15% will be used as a completely independent test of network generalization.
7. Click Next.
The standard network that is used for function fitting is a two-layer feedforward network, with a sigmoid
transfer function in the hidden layer and a linear transfer function in the output layer. The default number
of hidden neurons is set to 10. You might want to increase this number later, if the network training
performance is poor.
7|Page
8. Click Next.
8|Page
9. Select a training algorithm, then click Train.
9|Page
10. Under Plots, click Regression. 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, you could retrain the network by
10 | P a g e
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.
11. View the error histogram to obtain additional verification of network performance. Under
the Plots pane, click Error Histogram.
11 | P a g e
12 | P a g e
12. Click Next in the Neural Network Fitting App to evaluate the network.
At this point, you can test the network against new data.
If you are dissatisfied with the network's performance on the original or new data, you can do one of the
following:
Train it again.
Increase the number of neurons.
Get a larger training data set.
If the performance on the training set is good, but the test set performance is significantly worse, which
could indicate overfitting, then reducing the number of neurons can improve your results. If training
performance is poor, then you may want to increase the number of neurons.
13 | P a g e
15. Use the buttons on this screen to generate scripts or to save your results.
14 | P a g e
% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by NFTOOL
%
% This script assumes these variables are defined:
%
% houseInputs - input data.
% houseTargets - target data.
inputs = engineInputs;
targets = engineTargets;
15 | P a g e
% Train the Network
[net,tr] = train(net,inputs,targets);
% Plots
% Uncomment these lines to enable various plots.
% figure, plotperform(tr)
% figure, plottrainstate(tr)
% figure, plotfit(targets,outputs)
% figure, plotregression(targets,outputs)
% figure, ploterrhist(errors)
Task 02: This task can be used to train a neural network to estimate the bodyfat of someone from various
measurements.
1. Age (years)
2. Weight (lbs)
3. Height (inches)
4. Neck circumference (cm)
5. Chest circumference (cm)
6. Abdomen 2 circumference (cm)
7. Hip circumference (cm)
8. Thigh circumference (cm)
9. Knee circumference (cm)
10. Ankle circumference (cm)
11. Biceps (extended) circumference (cm)
12. Forearm circumference (cm)
13. Wrist circumference (cm)
bodyfatTargets - a 1x252 matrix of associated body fat percentages,to be estimated from the inputs.
[X,T] = bodyfat_dataset loads the inputs and targets into variables of your own choosing.
Here is how to design a fitting neural network with 10 hidden neurons with this data at the command
line.
[x,t] = bodyfat_dataset;
net = fitnet(10);
16 | P a g e
net = train(net,x,t);
view(net)
y = net(x);
Solution:
Step 1. The script assumes that the input vectors and target vectors are already loaded into the
workspace. If the data are not loaded, you can load them as follows:
load bodyfat_dataset
inputs = bodyfatInputs;
targets = bodyfatTargets;
This data set is one of the sample data sets that is part of the toolbox (see Sample Data Sets for Shallow
Neural Networks). You can see a list of all available data sets by entering the command help nndatasets.
The load command also allows you to load the variables from any of these data sets using your own
variable names. For example, the command
[inputs,targets] = bodyfat_dataset;
will load the body fat inputs into the array inputs and the body fat targets into the array targets.
Step 2. Create a network. The default network for function fitting (or regression) problems, fitnet, is a
feedforward network with the default tan-sigmoid transfer function in the hidden layer and linear transfer
function in the output layer. You assigned ten neurons (somewhat arbitrary) to the one hidden layer in the
previous section. The network has one output neuron, because there is only one target value associated
with each input vector.
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize);
Step 3. Set up the division of data.
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
With these settings, the input vectors and target vectors will be randomly divided, with 70% used for
training, 15% for validation and 15% for testing. (See “Dividing the Data” for more discussion of the data
division process.)
Step 4. Train the network. The network uses the default Levenberg-Marquardt algorithm (trainlm) for
training. For problems in which Levenberg-Marquardt does not produce as accurate results as desired, or
for large data problems, consider setting the network training function to Bayesian Regularization
(trainbr) or Scaled Conjugate Gradient (trainscg), respectively, with either
net.trainFcn = 'trainbr';
net.trainFcn = 'trainscg';
To train the network, enter:
[net, tr] = train(net, inputs, targets);
During training, the following training window opens. This window displays training progress and allows
you to interrupt training at any point by clicking Stop Training.
17 | P a g e
This training stopped when the validation error increased for six iterations, which occurred at iteration 20.
If you click Performance in the training window, a plot of the training errors, validation errors, and test
errors appears, as shown in the following figure. In this example, the result is reasonable because of the
following considerations:
The final mean-square error is small.
18 | P a g e
The test set error and the validation set error have similar characteristics.
No significant overfitting has occurred by iteration 14 (where the best validation performance
occurs).
Step 5. Test the network. After the network has been trained, you can use it to compute the network
outputs. The following code calculates the network outputs, errors and overall performance.
outputs = net(inputs);
errors = gsubtract(targets, outputs);
performance = perform(net, targets, outputs)
performance =
19.3193
It is also possible to calculate the network performance only on the test set, by using the testing
indices, which are located in the training record. (See Analyze Shallow Neural Network Performance
After Training for a full description of the training record.)
tInd = tr.testInd;
tstOutputs = net(inputs(:, tInd));
tstPerform = perform(net, targets(tInd), tstOutputs)
tstPerform =
19 | P a g e
53.7680
Step 6. Perform some analysis of the network response. If you click Regression in the training
window, you can perform a linear regression between the network outputs and the corresponding
targets.
The following figure shows the results.
20 | P a g e
The output tracks the targets very well for training, testing, and validation, and the R-value is over
0.96 for the total response. If even more accurate results were required, you could try any of these
approaches:
Reset the initial network weights and biases to new values with init and train again
(see “Initializing Weights” (init)).
Increase the number of hidden neurons.
Increase the number of training vectors.
Increase the number of input values, if more relevant information is available.
Try a different training algorithm (see “Training Algorithms”).
In this case, the network response is satisfactory, and you can now put the network to use on new
inputs.
Step 7. View the network diagram.
view(net)
21 | P a g e