-
Notifications
You must be signed in to change notification settings - Fork 0
/
CYCLOPS_FluxAutoEncoderModule.jl
237 lines (221 loc) · 11.8 KB
/
CYCLOPS_FluxAutoEncoderModule.jl
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
module CYCLOPS_FluxAutoEncoderModule
#= This module provides a function that creates a balanced autoencoder using Flux. Specifically, this function creates a circular node autoencoder where it is possible to specificy the number of circular nodes and linear nodes in the bottleneck layer in addition to the input (and thus also output since this is an balanced autoencoder) dimensions. It then returns a model that reflects these inputs that has been created using Flux's Chain function. The bottleneck layer is the concatenation of the specified number of circular and/or linear layers. =#
import Flux: Dense, Chain
import Flux.Tracker: data, param
export makeautoencoder_naive
# TODO: Change exports when makeautoencoder is fixed.
#=
function makeautoencoder(in_out_dim::Integer, n_circs::Integer, n_lins::Integer, lin_dim::Integer)
if n_circs == 0 && n_lins == 0
throw(ArgumentError("The number of circular nodes and linear layers in the bottleneck cannot both be zero."))
elseif n_circs < 0 || n_lins < 0
throw(ArgumentError("The number of circular nodes and linear layers in the bottleneck cannot be less than zero."))
elseif lin_dim < 1
throw(ArgumentError("The input/output dimensions of the linear node(s) in the bottleneck layer must be at least 1."))
end
function circ(x)
length(x) == 2 || throw(ArgumentError(string("Invalid length of input that should be 2 but is ", length(x))))
x./sqrt(sum(x .* x))
end
lin = Dense(lin_dim, lin_dim, x -> x)
makelins(in_out_dim1, lin_dim1, n_lins1) = x -> vcat( collect(Iterators.repeated(lin(Dense(in_out_dim1, lin_dim1)(x)), n_lins1)))
if n_circs == 0
encodetobottleneck = makelins(in_out_dim, lin_dim, n_lins)
elseif n_lins == 0
encodetobottleneckprep = makecircs(in_out_dim, n_circs)
encodetobottleneck = x -> encodetobottleneckprep(Dense(in_out_dim1, 2)(x))
else
encodetobottleneck = vcat(makecircs(in_out_dim, n_circs), makelins(in_out_dim, lin_dim, n_lins))
end
decoder = Dense(n_circs*2 + n_lins, in_out_dim)
model = Chain(encodetobottleneck(x)[1], decoder)
model
end
=#
#=
function makeautoencoder_naive(in_out_dim::Integer, n_circs::Integer, lin::Bool, lin_dim::Integer)
if n_circs == 0 && lin == false
throw(ArgumentError("The number of circular nodes and linear layers in the bottleneck cannot both be zero."))
elseif n_circs < 0
throw(ArgumentError("The number of circular nodes in the bottleneck cannot be less than zero."))
elseif lin == true && lin_dim < 1
throw(ArgumentError("The input/output dimensions of the linear node(s) in the bottleneck layer must be at least 1."))
end
function circ(x)
length(x) == 2 || throw(ArgumentError(string("Invalid length of input that should be 2 but is ", length(x))))
x./sqrt(sum(x .* x))
end
if n_circs == 0
println("linear layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
elseif lin == false
if n_circs == 1
println("single circular layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck = Chain(Dense(in_out_dim, 2), circ)
elseif n_circs == 2
println("double circular layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneck2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneck(x) = vcat(encodetobottleneck1(x), encodetobottleneck2(x))
else
println("triple circular layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneck2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneck3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneck(x) = vcat(encodetobottleneck1(x), encodetobottleneck2(x), encodetobottleneck3(x))
end
else
if n_circs == 1
println("single circular layer with linear layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck(x) = vcat(encodetobottleneckcirc(x), encodetobottlenecklin(x))
elseif n_circs == 2
println("double circular layer with linear layer autoencoder being created")
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck(x) = vcat(encodetobottleneckcirc1(x), encodetobottleneckcirc2(x), encodetobottlenecklin(x))
else
println("triple circular layer with linear layer autoencoder being created")
encodetobottleneckcirc1 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc2 = Chain(Dense(in_out_dim, 2), circ)
encodetobottleneckcirc3 = Chain(Dense(in_out_dim, 2), circ)
encodetobottlenecklin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
encodetobottleneck(x) = vcat(encodetobottleneckcirc1(x), encodetobottleneckcirc2(x), encodetobottleneckcirc3(x), encodetobottlenecklin(x))
end
end
decoder = Dense(n_circs*2 + lin, in_out_dim)
model = Chain(encodetobottleneck, decoder)
model
end
=#
function makeautoencoder_naive(in_out_dim::Integer, n_circs::Integer, lin::Bool, lin_dim::Integer)
if n_circs == 0 && lin == false
throw(ArgumentError("The number of circular nodes and linear layers in the bottleneck cannot both be zero."))
elseif n_circs < 0
throw(ArgumentError("The number of circular nodes in the bottleneck cannot be less than zero."))
elseif lin == true && lin_dim < 1
throw(ArgumentError("The input/output dimensions of the linear node(s) in the bottleneck layer must be at least 1."))
end
function circ(x)
length(x) == 2 || throw(ArgumentError(string("Invalid length of input that should be 2 but is ", length(x))))
x./sqrt(sum(x .* x))
end
if n_circs == 0
encodetobottleneck = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, identity))
elseif lin == false
for i in 1:n_circs
@eval $(Symbol("encoderbottle_$i")) = Chain(Dense($in_out_dim, 2), $circ)
end
modelmakerstring = "y -> vcat(u)"
u = "encoderbottle_1(y)"
if n_circs > 1
for i in 2:n_circs
u = u * ", encoderbottle_$i(y)"
end
end
modelmakerstring = modelmakerstring[1:findfirst(isequal('u'), modelmakerstring) - 1] * u * modelmakerstring[findfirst(isequal('u'), modelmakerstring) + 1:end]
encodetobottleneck = eval(Meta.parse(modelmakerstring))
else
for i in 1:n_circs
@eval $(Symbol("encoderbottle_$i")) = Chain(Dense($in_out_dim, 2), $circ)
end
encoderbottle_lin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, identity))
modelmakerstring = "y -> vcat(u, encoderbottle_lin(y))"
u = "encoderbottle_1(y)"
if n_circs > 1
for i in 2:n_circs
u = u * ", encoderbottle_$i(y)"
end
end
modelmakerstring = modelmakerstring[1:findfirst(isequal('u'), modelmakerstring)-1] * u * modelmakerstring[findfirst(isequal('u'), modelmakerstring)+1:end]
encodetobottleneck = eval(Meta.parse(modelmakerstring))
end
decoder = Dense(n_circs*2 + lin, in_out_dim)
model = Chain(encodetobottleneck, decoder)
model
end
#=
function makeautoencoder_string(in_out_dim::Integer, n_circs::Integer, lin::Bool, lin_dim::Integer)
if n_circs == 0 && lin == false
throw(ArgumentError("The number of circular nodes and linear layers in the bottleneck cannot both be zero."))
elseif n_circs < 0
throw(ArgumentError("The number of circular nodes in the bottleneck cannot be less than zero."))
elseif lin == true && lin_dim < 1
throw(ArgumentError("The input/output dimensions of the linear node(s) in the bottleneck layer must be at least 1."))
end
function circ(x)
length(x) == 2 || throw(ArgumentError(string("Invalid length of input that should be 2 but is ", length(x))))
x./sqrt(sum(x .* x))
end
if n_circs == 0
encodetobottleneck = "Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x)"
elseif lin == false
for i in 1:n_circs
@eval $(Symbol("encoderbottle_$i")) = Chain(Dense($in_out_dim, 2), $circ)
end
modelmakerstring = "y -> vcat(u)"
u = "encoderbottle_1(y)"
if n_circs > 1
for i in 2:n_circs
u = u * ", encoderbottle_$i(y)"
end
end
modelmakerstring = modelmakerstring[1:findfirst(isequal('u'), modelmakerstring) - 1] * u * modelmakerstring[findfirst(isequal('u'), modelmakerstring) + 1:end]
encodetobottleneck = eval(Meta.parse(modelmakerstring))
else
for i in 1:n_circs
@eval $(Symbol("encoderbottle_$i")) = Chain(Dense($in_out_dim, 2), $circ)
end
encoderbottle_lin = Chain(Dense(in_out_dim, lin_dim), Dense(lin_dim, lin_dim, x -> x))
modelmakerstring = "y -> vcat(u, encoderbottle_lin(y))"
u = "encoderbottle_1(y)"
if n_circs > 1
for i in 2:n_circs
u = u * ", encoderbottle_$i(y)"
end
end
modelmakerstring = modelmakerstring[1:findfirst(isequal('u'), modelmakerstring)-1] * u * modelmakerstring[findfirst(isequal('u'), modelmakerstring)+1:end]
encodetobottleneck = eval(Meta.parse(modelmakerstring))
end
decoder = Dense(n_circs*2 + lin, in_out_dim)
model = Chain(encodetobottleneck, decoder)
model
end
=#
# extracts the phase angles from the model for analysis
function extractphase(data_matrix, model, n_circs::Integer)
points = size(data_matrix, 2)
phases = zeros(n_circs, points)
base = 0
for circ in 1:n_circs
for n in 1:points
pos = model(data_matrix[:, n])
phases[circ, n] = data(atan(pos[2 + base], pos[1 + base]))
end
base += 2
end
phases
end
end # module CYCLOPS_FluxAutoEncoderModule