-
Notifications
You must be signed in to change notification settings - Fork 0
/
som.m
63 lines (51 loc) · 1.77 KB
/
som.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
classdef som
properties
nodes = [];
nrnodes = 0;
traindata = [];
nrtrdata = 0;
SampleNumber = 0;
Stress
Strain
Modulus = 0;
dw = [];
lr = 0.01;
spreadmatrix = [];
end
methods
function obj = som(examplepos, nrnodes)
obj.nrnodes = nrnodes;
strlength = size(examplepos, 2);
indexvec = floor(1:((strlength-1)/(obj.nrnodes-1)):strlength);
obj.nodes = examplepos(:,indexvec);
end
function obj = add(obj, pos)
obj.traindata = [obj.traindata, pos];
obj.nrtrdata = size(obj.traindata, 2);
end
function obj = adapt(obj, steps)
totalsteps = steps;
stepcount = 0;
if steps == 0;
cout << "error, no trainingdata";
end
while stepcount < totalsteps
steps = min(steps, obj.nrtrdata);
stepcount = stepcount + steps;
obj.dw = zeros(2,obj.nrnodes);
dataselect = randi(obj.nrtrdata, 1, steps);
pos = obj.traindata(:,dataselect);
[nodenr,dummy] = somdistance(pos, obj.nodes); %Get min distance node
%Training
for i=1:steps
obj.dw(:,nodenr(i)) = obj.dw(:,nodenr(i)) + (pos(:,i) - obj.nodes(:, nodenr(i)));
mini = mod(nodenr(i)-2,obj.nrnodes)+1;
plusi = mod(nodenr(i),obj.nrnodes)+1;
obj.dw(:,plusi) = obj.dw(:,plusi) + 0.5*(pos(:,i) - obj.nodes(:,plusi));
obj.dw(:,mini) = obj.dw(:,mini) + 0.5*(pos(:,i) - obj.nodes(:,mini));
end
obj.nodes = obj.nodes + obj.lr*obj.dw;
end
end
end% methods
end