-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.jl
148 lines (120 loc) · 3.88 KB
/
main.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
using FastAI, StaticArrays, Colors
using FastAI: FluxTraining, Image
using CSV
using DataFrames
using MLDataPattern
using FastAI: encodedblockfilled, decodedblockfilled
using Flux
import CairoMakie
df = DataFrame(CSV.File("data/data.csv"; header=false, types=Float32))
function normalise(M)
min = minimum(minimum(eachcol(M)))
max = maximum(maximum(eachcol(M)))
return (M .- min) ./ (max - min)
end
normalised = Array(df) |> normalise
window_size = 60
data = slidingwindow(normalised', window_size, stride=1)
train_set, validate_set, test_set = splitobs(map(transpose, data), (0.7, 0.2));
getobs(train_set, 1)
function EmbeddingTask(block, encodings)
sample = block
encodedsample = x = y = ŷ = sample
blocks = (; sample, x, y, ŷ, encodedsample)
BlockTask(blocks, encodings)
end
task = EmbeddingTask(
Swarm(900, 60),
(ImagePreprocessing(),),
)
BATCHSIZE = 16
dataloader = DataLoader(taskdataset(shuffleobs(train_set), task, Training()), BATCHSIZE)
struct VAE{E,D}
encoder::E
decoder::D
end
Flux.@functor VAE
function (vae::VAE)(xs)
println(xs)
μ, logσ² = vae.encoder(xs)
zs = sample_latent(μ, logσ²)
x̄s = vae.decoder(zs)
return x̄s, (; μ, logσ²)
end
using Random: randn!
using Statistics: mean
sample_latent(μ::AbstractArray{T}, logσ²::AbstractArray{T}) where {T} =
μ .+ exp.(logσ² ./ 2) .* randn!(similar(logσ²))
function βELBO(x, x̄, μ, logσ²; β=1)
reconstruction_error = mean(sum(@.((x̄ - x)^2); dims=1))
# D(N(μ, Σ)||N(0, I)) = 1/2 * (μᵀμ + tr(Σ) - length(μ) - log(|Σ|))
kl_divergence = mean(sum(@.((μ^2 + exp(logσ²) - 1 - logσ²) / 2); dims=1))
return reconstruction_error + β * kl_divergence
end
encoder =
Chain(
Conv((9,), 900 => 9000, relu; pad=SamePad()),
MaxPool((2,)),
Conv((5,), 9000 => 4500, relu; pad=SamePad()),
MaxPool((2,)),
Conv((5,), 4500 => 2250, relu; pad=SamePad()),
MaxPool((3,)),
Conv((3,), 2250 => 1000, relu; pad=SamePad()),
Conv((3,), 1000 => 100, relu; pad=SamePad()),
Flux.flatten,
Parallel(
tuple,
Dense(500, 100), # μ
Dense(500, 100), # logσ²
),
) |> gpu
decoder = Chain(
Dense(100, 500, relu),
(x -> reshape(x, 5, 100, :)),
# 5x100xb
ConvTranspose((3,), 100 => 1000, relu; pad=SamePad()),
ConvTranspose((3,), 1000 => 2250, relu; pad=SamePad()),
Upsample((3,)),
# 15x2250xb
ConvTranspose((5,), 2250 => 4500, relu; pad=SamePad()),
Upsample((2,)),
# 30x4500xb
ConvTranspose((5,), 4500 => 9000, relu; pad=SamePad()),
Upsample((2,)),
# 60x9000xb
ConvTranspose((9,), 9000 => 900; pad=SamePad()),
# 60x900xb
) |> gpu
model = VAE(encoder, decoder)
struct VAETrainingPhase <: FluxTraining.AbstractTrainingPhase end
function FluxTraining.step!(learner, phase::VAETrainingPhase, batch)
FluxTraining.runstep(learner, phase, (xs=batch,)) do handle, state
gs = gradient(learner.params) do
μ, logσ² = learner.model.encoder(state.xs)
state.zs = sample_latent(μ, logσ²)
state.x̄s = learner.model.decoder(state.zs)
handle(FluxTraining.LossBegin())
state.loss = learner.lossfn(Flux.flatten(state.xs), Flux.flatten(state.x̄s), μ, logσ²)
handle(FluxTraining.BackwardBegin())
return state.loss
end
handle(FluxTraining.BackwardEnd())
Flux.Optimise.update!(learner.optimizer, learner.params, gs)
end
end
function FluxTraining.on(
::FluxTraining.StepBegin,
::VAETrainingPhase,
cb::ToDevice,
learner,
)
learner.step.xs = cb.movedatafn(learner.step.xs)
end
learner = Learner(model, (), ADAM(), βELBO, ToGPU())
FluxTraining.removecallback!(learner, ProgressPrinter);
fitonecycle!(
learner,
2,
0.00000001;
phases = (VAETrainingPhase() => dataloader,)
)