ProjectReport Kanwarpal
ProjectReport Kanwarpal
Project Report #1
Instructor
Dr.Scott Dick
University of Alberta
Submitted by
Kanwarpal Singh
Student ID: 1524706
We have been given wine dataset, which consist of 178 examples of 13 different
attributes, it is a 3 class problem. Now we are required to build a Multi-layer
perceptron network using the backpropagation with momentum learning. So,in
order to do that we first need to divide the dataset into two parts i.e Training and
Testing,we have selected 10% 1)of data as test data and remaining as the training
data.The next step we need to do is to perform is parameter 10 fold cross validation
on the training data ,which will divide the data into 10 equal parts ,each with 16
examples,these 10 equal parts as known as folds.For each different folds we will
set parameters,these parameters will be the number of layers,number of neurons,
learning rate and momentum.After setting these parameters in our network,we will
create and train the network using these.The best parameters will be selected on the
basis of accuracy of network.
Multi-layer as the name suggests, a network with one or more hidden layer
between the input and output layers, it is basically a network of neurons which are
called as perceptrons.A Multi-layer perceptron network looks like this:
Here the middle layer is the hidden layer,every node is full connected to other
layers in the node.In Neural network,every input in the network adds a weight to
the network,so the sum of these weights and the biased is feed into a function and
this function is known as Activation function, this function transforms the input
into the output of the network.
There are basically three types of activation functions,which are used most often in
Nueral networks, i.e Sigmoid functions and Tanh Activation functions.
Backpropagation algorithm:
1.First,a forward pass is executed,this computes the activation for the layers,say
l1,l2,l3.to the output layer l.t1
set
In this project,we are provided with Wine dataset,this dataset can be downloaded
from UCI Machine Learning Repository. So,in this dataset,there are 13 attributes
with 178 examples.The 13 attributes in this dataset are:
1. Alchohal
2. Malic Acid
3. Ash
4. Alcalinity of ash.
5. Magnesium
6. Total phenols
7. Flavanoids
8. Nonflavanoids phenols
9. Proanthocyanins
10.Color Intensity
11.Hue
12.OD280/OD315 of diluted wines
13.Proline
inputs.normal_input2= removeconstantrows(normal_input)
fixed_input = fixunknowns(normal_input2)
Since our inputs have been normalized now,so now we need to design the neural
network.But firstly, we need to divide the dataset into training and testing data
set.In this problem,we will use 10% of samples as testing set,that sums to 18.So
remaining 160 samples is given to training set.Now,the next step is to divide 160
samples of training set into 10 folds of k-fold cross validation.So,using k-fold cross
validation,the training data is divided into 10 equal parts of 16 samples each.K-fold
cross fold validation can also be done using in-built matlab functions such as
crossvalind and cvpartition.For every iteration,one fold out of 10 folds will be
used as Testing and the remaining folds will be used as Training set.
So ,our data is divided into 10 folds,we need to create network for each fold.A
neural network can be created by Neural network object named net in matlab.Here
we are creating feedforward network,next we need to give the network a training
function,number of layers,number of neurons .Since we need to select the best
hyper parameters,so the 4 parameters we will use to judge the best network are:
Number of layers
Number of neurons
Learning Rate
Momentum
We will assign different parameter values to different networks and on the basis of
Accuracy of network,best Hyper parameters will be chosen.
Training the network:
After the network objects and the parameters of the networks are defined, we now
need to train the network.For training a network train function is used
Along with the train function, we will assign the training parameters,like number
of epochs,the learning rate,momentum constant .
net.trainParam.lr=0.01
net.trainParam.epochs = 2000
net.trainParam.mc=0.5
After the network is created and trained,we need to compute the confusion
matrix.In the confusion matrix,we can calculate the True Positive Rate,True
Negative rate,False Positive rate and the False Negative rate.From these factors,we
can compute the accuracy.The table below shows the Accuracy,TPR and FPR rates
of every fold.
In this report,we have discussed in detail about the methodology for creating a
multi-layer perceptron network using backpropagation algorithm.The problem
defined in this dataset is 3 class data classification problem.In this problem we first
divided the data into 2 parts i.e. training and testing,the training dataset is divided
into 10 folds and every fold is tested once.Finally this trained data is compared
with the testing set,which we have separated in the first step,then the whole
network is trained and tested.In this problem we have learned how to train a neural
network along with different parameters,how to avoid over fitting.The best
hyperparamters based on resuts is
load wine_dataset;
inputs = wineInputs
outputs = wineTargets
normal_output= mapminmax(outputs)
normal_output2 = removeconstantrows(normal_output)
t1 = fixunknowns(normal_output2)
q = size(i1,2)
q1 = floor(q*0.90) % 160 parts here
trainFcn = 'traingdm';
data1 = a1(:,1:160);
n = length(data1)
k = 10;
allix1 = datasample(data1,13);
numineach = ceil(n/k)
y = allix1;
%allixineach = reshape([allix1 NaN(1,k*numineach-n)],k,numineach);
cvpart = crossvalind('Kfold',n,k)
for i = 1:k
if i ==1
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 10
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.01
net.trainParam.epochs = 2000;
net.trainParam.mc=0.1
net = init(net);
y = net(a1)
perf1 = perform(net,b1,y)
end
if i ==2
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.02
net.trainParam.epochs = 2000;
net.trainParam.mc=0.2
net = init(net);
y = net(a1)
perf2 = perform(net,b1,y)
end
if i ==3
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 10
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.03
net.trainParam.epochs = 2000;
net.trainParam.mc=0.3
net = init(net);
y = net(a1)
perf3 = perform(net,b1,y)
end
if i ==4
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 20
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.04
net.trainParam.epochs = 2000;
net.trainParam.mc=0.4
net = init(net);
y = net(a1)
perf4 = perform(net,b1,y)
end
if i ==5
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 10
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.05
net.trainParam.epochs = 2000;
net.trainParam.mc=0.5
net = init(net);
y = net(a1)
perf5 = perform(net,b1,y)
end
if i ==6
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.06
net.trainParam.epochs = 2000;
net.trainParam.mc=0.6
net = init(net);
y = net(a1)
perf6 = perform(net,b1,y)
end
if i ==7
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.07
net.trainParam.epochs = 2000;
net.trainParam.mc=0.7
net = init(net);
y = net(a1)
perf7 = perform(net,b1,y)
end
if i ==8
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 20
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.08
net.trainParam.epochs = 2000;
net.trainParam.mc=0.8
net = init(net);
y = net(a1)
perf8 = perform(net,b1,y)
end
if i ==9
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=0.09
net.trainParam.epochs = 2000;
net.trainParam.mc=0.9
net = init(net);
y = net(a1)
perf9 = perform(net,b1,y)
end
if i ==10
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 20
net = patternnet(hiddenLayerSize, trainFcn);
net.performFcn = 'mse';
[net,tr] = train(net,a1,b1);
net.trainParam.lr=1.0
net.trainParam.epochs = 2000;
net.trainParam.mc=1.0
net = init(net);
y = net(a1)
perf10 = perform(net,b1,y)
end
end
net.trainFcn = 'traingdm' ;
hiddenLayerSize = 10
net = patternnet(hiddenLayerSize, trainFcn);
%net.performFcn = 'mse';
[net,tr] = train(net,testset1,testset2);
net.trainParam.lr=0.01
net.trainParam.epochs = 2000;
net.trainParam.mc=0.1
net = init(net);
y = net(testset1)
perffinal = perform(net,testset2,y)
Matlab code 1