-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryModule_Hopfield.m
More file actions
74 lines (60 loc) · 2.25 KB
/
Copy pathMemoryModule_Hopfield.m
File metadata and controls
74 lines (60 loc) · 2.25 KB
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
classdef MemoryModule_Hopfield < MemoryModule
properties
eta = 1;
sigmoid = @sign;
maxSteps = 100;
stoppingCriterion = 0.01;
end
methods
function z = myName(~), z = 'Hopfield'; end
function AddOnePattern(self, P0, alpha)
if nargin == 2, alpha = 1; end
self.M = self.M + alpha * (P0 * P0'); % memory pattern: outer product
if self.FLAG_keepTrackOfPatterns
self.P = cat(2, self.P, P0);
end
end
function X = Recall(self, X0)
% note that this is doing each cycle synchronously rather than
% asynchronously, which Hopfield requires, but asynchronous is
% incredibly slow.
X = X0;
for iStep = 1:self.maxSteps
X = self.sigmoid(self.M * X0 + self.eta * randn(self.nU,1));
if mean(abs(X-X0)) < self.stoppingCriterion, break; end
X0=X;
end
end
end
methods(Static)
function P = MakePatterns(nP, nU)
P = 2*double(rand(nU, nP)<0.5)-1;
end
function S = PatternSimilarity(P, X)
if nargin == 1, X = P;end
nP = size(P,2); nX = size(X,2);
S = nan(nP, nX);
for iP = 1:nP, for iX = 1:nX
S(iP,iX) = mean(P(:,iP) == X(:,iX));
end; end
% pattern similarity of random patterns ranges from 0.5 to 1
% so need to rescale from 0 to 1
S = (S-0.5)*2;
end
function P = AddNoise(P, eta)
P = MemoryModule_Hopfield.Noise_ReplaceRand(P, eta);
%P = MemoryModule_Hopfield.Noise_FlipRand(P, eta);
end
function P = Noise_ReplaceRand(P, eta)
[nP, nU] = size(P);
randYN = rand(nP,nU) < eta;
R = MemoryModule_Hopfield.MakePatterns(nP, nU);
P(randYN) = R(randYN);
end
function P = Noise_FlipRand(P, eta)
[nP, nU] = size(P);
randYN = rand(nP,nU) < eta;
P(randYN) = -P(randYN);
end
end
end