-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupervisedHebbianLayer.asv
130 lines (117 loc) · 3.81 KB
/
SupervisedHebbianLayer.asv
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
classdef SupervisedHebbianLayer
properties
weights
numInputs
outputSize
transferFunc
alpha
end
methods
%constructor
function this = SupervisedHebbianLayer(size, transfer)
if(nargin ~= 2)
error("Invalid number of arguments.")
else
this.numInputs = size;
this.weights = rand(this.numInputs, this.numInputs);
this.transferFunc = transfer;
end
end
%train function
function this = train(this, target, pattern)
this.weights = this.weights + target * pattern';
end
%training using the pseudoinverse rule
function this = pseudoInverseRule(this, input, target)
this.weights = target * pinv(input);
%becuase reformatting weirdly
%this.weights = this.weights(:);
%because of negtaive zero
this.weights(abs(this.weights) < eps) = 0;
%this.weights = target * (inv(input' * input) * input');
end
%% --- forward ---
%factory
function func = doFunc(this, n)
switch this.transferFunc
case "hardlim"
func = this.hardlim(n);
case "hardlims"
func = this.hardlims(n);
case "purelin"
func = this.purelin(n);
otherwise
error("Transfer function not supported");
end
end
%hardlim
function f = hardlim(this, n)
if(n < 0)
f = 0;
else %if n >= 0
f = 1;
end
end
%hardlimS
function f = hardlims(this, n)
if(n < 0)
f = -1;
else % n >=0
f = 1;
end
end
%purelin
function f = purelin(this, n)
f = n;
end
%do foward
function output = forward(this, input)
n = (this.weights * input');
output = arrayfun(@this.doFunc, n);
end
end
methods (Static)
%%addNoise to a vector, distort it
function pvec = addNoise(pvec, num)
% ADDNOISE Add noise to "binary" vector
% pvec pattern vector (-1 and 1)
% num number of elements to flip randomly
% Handle special case where there's no noise
if num == 0
return;
end
% first, generate a random permutation of all indices into pvec
inds = randperm(length(pvec));
% then, use the first n elements to flip pixels
pvec(inds(1:num)) = -pvec(inds(1:num));
end
%calculate the erros of neuron given the target value and produced output
function e = errorLoss(a, t)
e = t - a;
end
%----different printing functions----%
%print the image out to a color map, use for weights (output) because of
%color scale
function printOut(vec)
imagesc(vec, [-1 1]); % Set the display range to match the data range
colormap(gray); % Change the colormap to grayscale
colorbar;
end
% Print to console, can be used for output or input
function printCon(vec)
%adjust to be printed, resshaped for matrix
matrix = rot90(flipud(reshape(vec, 5, 6)), 3);
%print by row
for i = 1:size(matrix, 1)
for j = 1:size(matrix, 2)
if matrix(i, j) == -1
fprintf(' ');
else
fprintf('■');
end
end
fprintf('\n');
end
end
end
end