-
Notifications
You must be signed in to change notification settings - Fork 8
/
NNminiProject1_a.m
83 lines (63 loc) · 2.15 KB
/
NNminiProject1_a.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% ---------------------------------------------------------------------------------
% Mini Project #1 - a : 10 Nodes in 1 Hidden Later.
% Training (80%), Validation(20%) and Testing(0%).
% Mean and variance of MSEs for training, validation
% and testing.
% ---------------------------------------------------------------------------------
% Load dataset
load bodyfat_dataset;
% Variable X and T
[X,T] = bodyfat_dataset;
% Fitting Network
hiddenLayerSize = 1;
% Build the Net
net = fitnet(hiddenLayerSize);
% Number of Neurons in layer
net.layers{1}.size = 10;
% Number of Epochs
net.trainParam.epochs = 10;
% Data Vector excluded 50 testing
allData = (1:202);
% Indexes
validate = datasample(1:202, 40);
testData = 202:252;
trainData = setdiff(allData,validate);
% Train Input and Target
TrainSetInput = X(trainData);
TrainSetTarget = T(trainData);
% Test Input and Target
TestSetInput = X(testData);
TestSetTarget = T(testData);
% Validation
ValidateInput = X(validate);
ValidateTarget = T(validate);
% Divide Param Ratio
net.divideParam.trainRatio = 0.8;
net.divideParam.valRatio = 0.2;
net.divideParam.testRatio = 0;
% Train the Network
[net, tr] = train(net, TrainSetInput, TrainSetTarget);
% Train Outputs
TrainOutputs = net(TrainSetInput);
% Test the Network
TestOutputs = net(TestSetInput);
% Validate Output
ValidateOutputs = net(ValidateInput);
% Errors and Performance
TrainErrors = gsubtract(TrainOutputs, TrainSetTarget);
TestErrors = gsubtract(TestOutputs, TestSetTarget);
ValidateErrors = gsubtract(ValidateOutputs, ValidateTarget);
performance = perform(net, TestSetTarget, TestOutputs);
%figure, plotconfusion(TestSetTarget,Testoutputs)
% MSEs
TrainMSE = mse(net, TrainSetTarget, TrainOutputs, 'regularization',0.01);
TestMSE = mse(net, TestSetTarget, TestOutputs, 'regularization',0.01);
ValidationMSE = mse(net, ValidateTarget, ValidateOutputs, 'regularization',0.01);
% Mean Errors
meanTrainE = mean(TrainErrors);
meanTestE = mean(TestErrors);
meanValidateE = mean(ValidateErrors);
% Variance Errors
varTrainE = var(TrainErrors);
varTestE = var(TestErrors);
varValidateE = var(ValidateErrors);