-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSimple_softmax.m
105 lines (88 loc) · 2.97 KB
/
Simple_softmax.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create data
dataset = [
1 0 0;
1 1 0;
1 1 1;
];
% generate targets
targets = [
1 0 0;
0 1 0;
0 0 1;
];
% add the bias unit to inputs
dataset = [ones(3, 1), dataset];
% This will be a very simple NN, consisting only of the input layer and
% the softmax group. As such, it will only have one set of weights.
weights = rand(4, 3) - 0.5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Performing the actual calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i = 0;
fprintf('Misclassified example count before training: %d\n\n', ...
error_count(weights, dataset, targets))
while true
weights = weights - gradient(weights, dataset, targets);
i = i + 1;
if error_count(weights, dataset, targets) == 0
break
end
end
fprintf('Training completed in %d iterations\n', i)
fprintf('Misclassified example count after training: %d\n\n', ...
error_count(weights, dataset, targets))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definitions of functions used in the calculations above
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [probabilities] = feed_forward(weights, dataset, targets)
z = dataset * weights;
numerator = exp(z);
denumerator = sum(numerator, 2);
probabilities = numerator ./ denumerator;
end
function [grad] = numerical_gradient(weights, dataset, targets)
grad = zeros(size(weights));
delta = 1e-4;
for i = 1:size(weights, 1)
for j = 1:size(weights, 2)
old_v = weights(i, j);
weights(i, j) = weights(i, j) - delta;
cost_a = cost(weights, dataset, targets);
weights(i, j) = old_v;
weights(i, j) = weights(i, j) + delta;
cost_b = cost(weights, dataset, targets);
weights(i, j) = old_v;
grad(i, j) = (cost_b - cost_a) / (2 * delta);
end
end
end
function [grad] = gradient(weights, dataset, targets)
probabilities = feed_forward(weights, dataset, targets);
grad = zeros(size(weights));
for i = 1:size(dataset, 1)
grad = grad + dataset(i, :)' * (targets(i, :) - probabilities(i, :));
end
grad = -grad;
end
function [h] = hypothesis(weights, dataset, targets)
[vals, idx] = max(feed_forward(weights, dataset, targets), [], 2);
h = zeros(3,3);
for i = 1:3
h(i, idx(i)) = 1;
end
end
function [count] = error_count(weights, dataset, targets)
h = hypothesis(weights, dataset, targets);
misclassified_examples = ~all(h == targets, 2);
count = sum(misclassified_examples);
end
function [c] = cost(weights, dataset, targets)
h = hypothesis(weights, dataset, targets);
probabilities = feed_forward(weights, dataset, targets);
% normally the below will not work
individual_costs = -log(probabilities) .* targets;
c = sum(sum(individual_costs));
end