-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.m
More file actions
134 lines (88 loc) · 3.76 KB
/
Copy pathModule.m
File metadata and controls
134 lines (88 loc) · 3.76 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
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
131
132
133
134
classdef Module
properties
enc;
dec;
NH;
NC;
NL;
init_flag;
code_init;
eta_init;
high;
code;
low;
mse;
end
methods
function obj = Module(NL,NC,NH,NHid,g,eta_e,eta_d,eta_init)
N0 = 1 + NL + NC + NH;
N1 = NH;
N2 = NC;
obj.enc = SNet(N0,NHid,N2,g,eta_e);
obj.dec = SNet(1+NC+NH,NHid,NL+NC,g,eta_d);
obj.NH = NH;
obj.NC = NC;
obj.NL = NL;
obj.eta_init = eta_init;
obj.code_init = zeros(NC,1);
obj.low = zeros(NL,1);
obj.mse = 0;
end
function obj = SetInputs(obj,low,high)
obj.low = low;
obj.high = high;
end
function obj = PropagateLearn(obj,niter,learn_flag,eta_rec)
code_o = LinearThreshold(obj.code);
xIn = [1;obj.low;code_o;obj.high];
obj.enc = obj.enc.FProp(xIn);
code = LinearThreshold(obj.enc.X2);
if ~learn_flag
niter = 1;
end
target = LinearThreshold([obj.low;code_o]);
p = {};
p.X0 = [1;code;obj.high];
p.eta_min = eta_rec;
p.iters = niter;
p.constraint = p.X0;
p.cw = 1;
p.target = target;
p.cidxs = 2:(1+obj.NC);
obj.dec = obj.dec.Minimize(p);
obj.mse = norm(target-obj.dec.X2);
if learn_flag
obj.dec = obj.dec.ErrorLearn(target-obj.dec.X2,1);
obj.dec = obj.dec.Tag();
obj.dec = obj.dec.Untag();
obj.enc = obj.enc.FProp(xIn);
ch = LinearThreshold(obj.dec.X0(2:1+obj.NC));
obj.enc = obj.enc.ErrorLearn(ch-obj.enc.X2,1);
obj.enc = obj.enc.Tag();
obj.enc = obj.enc.Untag();
if obj.init_flag
obj.code_init = obj.code_init + obj.eta_init*(obj.dec.X2(1+obj.NL:end)-obj.code_init);
obj.init_flag = 0;
end
end
obj.code = obj.enc.X2;
end
function code = Encode(obj)
code = obj.enc.FastProp([1;obj.low;obj.code]);
end
function [low,code_o] = Decode(obj,code,high)
out = LinearThreshold(obj.dec.FastProp([1;code;high]));
low = out(1:obj.NL);
code_o = out(obj.NL+1:end);
end
function obj = ResetCode(obj)
obj.code = obj.code_init;
obj.low = 0*obj.low;
obj.high = 0*obj.high;
obj.init_flag = 1;
end
end
end
function y = LinearThreshold(x)
y = ((x > -1).*(x < 1)).*x - (x < -1) + (x > 1);
end