-
Notifications
You must be signed in to change notification settings - Fork 49
/
validateTwoLayerPerceptron.m
69 lines (62 loc) · 2.32 KB
/
validateTwoLayerPerceptron.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
function [correctlyClassified, classificationErrors] = validateTwoLayerPerceptron(activationFunction, hiddenWeights, outputWeights, inputValues, labels)
% validateTwoLayerPerceptron Validate the twolayer perceptron using the
% validation set.
%
% INPUT:
% activationFunction : Activation function used in both layers.
% hiddenWeights : Weights of the hidden layer.
% outputWeights : Weights of the output layer.
% inputValues : Input values for training (784 x 10000).
% labels : Labels for validation (1 x 10000).
%
% OUTPUT:
% correctlyClassified : Number of correctly classified values.
% classificationErrors : Number of classification errors.
%
testSetSize = size(inputValues, 2);
classificationErrors = 0;
correctlyClassified = 0;
for n = 1: testSetSize
inputVector = inputValues(:, n);
outputVector = evaluateTwoLayerPerceptron(activationFunction, hiddenWeights, outputWeights, inputVector);
class = decisionRule(outputVector);
if class == labels(n) + 1
correctlyClassified = correctlyClassified + 1;
else
classificationErrors = classificationErrors + 1;
end;
end;
end
function class = decisionRule(outputVector)
% decisionRule Model based decision rule.
%
% INPUT:
% outputVector : Output vector of the network.
%
% OUTPUT:
% class : Class the vector is assigned to.
%
max = 0;
class = 1;
for i = 1: size(outputVector, 1)
if outputVector(i) > max
max = outputVector(i);
class = i;
end;
end;
end
function outputVector = evaluateTwoLayerPerceptron(activationFunction, hiddenWeights, outputWeights, inputVector)
% evaluateTwoLayerPerceptron Evaluate two-layer perceptron given by the
% weights using the given activation function.
%
% INPUT:
% activationFunction : Activation function used in both layers.
% hiddenWeights : Weights of hidden layer.
% outputWeights : Weights for output layer.
% inputVector : Input vector to evaluate.
%
% OUTPUT:
% outputVector : Output of the perceptron.
%
outputVector = activationFunction(outputWeights*activationFunction(hiddenWeights*inputVector));
end