-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.m
100 lines (84 loc) · 4.46 KB
/
Network.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
classdef Network < handle
properties
% The vector of neurons belonging to the network.
neurons;
% The learning rate of the network.
learningRate;
end
methods
% Constructor for the network.
function network = Network(learningRate, neuronCount, neuronWidth, clusterData)
if (nargin ~= 0)
% Create the neurons.
network.learningRate = learningRate;
network.neurons = createNeurons(neuronCount, neuronWidth, clusterData);
end
end
% Feeds the input value to the single layer network.
function [output, activationValues] = feed(network, input)
% Work out the size of the network.
neuronCount = size(network.neurons, 1);
% Initialise matrices to hold the neuron's output values.
activationValues = zeros(neuronCount, 1);
outputValues = zeros(neuronCount, 1);
% For the given input, store the activation value for each neuron.
for neuronIndex = 1:neuronCount
% Get the neuron's activatstrion.
neuron = network.neurons(neuronIndex);
activation = neuron.activation(input);
% Append the result matrices.
activationValues(neuronIndex) = activation;
outputValues(neuronIndex) = activation * neuron.weight;
end
% Now we have the activation value and weighted output value for each
% hidden node, we can compute the output of the network.
output = sum(outputValues) / sum(activationValues);
end
% Feeds the input vector to the single layer network.
function [outputVector, activationValues] = feedBatch(network, input)
% Work out the size of the network & input data.
neuronCount = size(network.neurons, 1);
dataCount = size(input(:, 1), 1);
% Initialise matrices to hold the neuron's output values.
activationValues = zeros(dataCount, neuronCount);
outputValues = zeros(dataCount, neuronCount);
% For each data point, store the activation value for each neuron.
for dataIndex = 1 : dataCount
for neuronIndex = 1 : neuronCount
% Get the neuron's activation.
neuron = network.neurons(neuronIndex);
activation = neuron.activation(input(dataIndex, :));
% Append the result matrices.
activationValues(dataIndex, neuronIndex) = activation;
outputValues(dataIndex, neuronIndex) = activation * neuron.weight;
end
end
% Now we have the activation values and weighted output values for each
% data point for each hidden node, we can compute the output of the network.
outputVector = zeros(dataCount, 1);
for dataIndex = 1 : dataCount
outputVector(dataIndex) = sum(outputValues(dataIndex, :)) / sum(activationValues(dataIndex, :));
end
end
function train(network, input, target)
% Find the output of the network for the data point.
if size(input, 1) > 1
[output, activationValues] = network.feedBatch(input);
% Updates the weight of each neuron based on the input, output & target.
for dataIndex = 1:size(input(:, 1), 1)
for neuronIndex = 1:size(network.neurons, 1)
neuron = network.neurons(neuronIndex);
neuron.weight = neuron.weight + network.learningRate * (target(dataIndex) - output(dataIndex)) * activationValues(dataIndex, neuronIndex);
network.neurons(neuronIndex) = neuron;
end
end
else
[output, activationValues] = network.feed(input);
% Updates the weight of each neuron based on the input, output & target.
for neuronIndex = 1:size(network.neurons, 1)
network.neurons(neuronIndex).weight = network.neurons(neuronIndex).weight + network.learningRate * (target - output) * activationValues(neuronIndex);
end
end
end
end
end