-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
maskomic
committed
Feb 1, 2022
1 parent
9e5b52a
commit 6e79a3d
Showing
12 changed files
with
522 additions
and
7 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using DrWatson | ||
@quickactivate | ||
include(srcdir("models", "utils.jl")) | ||
include(srcdir("models", "PoolAE.jl")) | ||
|
||
using Plots | ||
using StatsPlots | ||
ENV["GKSwstype"] = "100" | ||
|
||
data1 = [randn(2,rand(Poisson(20))) .+ [2.1, -1.4] for _ in 1:100] | ||
data2 = [randn(2,rand(Poisson(20))) .+ [-2.1, 1.4] for _ in 1:100] | ||
data3 = [randn(2,rand(Poisson(20))) for _ in 1:100] | ||
data4 = [randn(2,rand(Poisson(50))) .+ [2.1, -1.4] for _ in 1:100] | ||
|
||
train_data = vcat(data1,data2) | ||
val_data = vcat( | ||
[randn(2,rand(Poisson(20))) .+ [2.1, -1.4] for _ in 1:100], | ||
[randn(2,rand(Poisson(20))) .+ [-2.1, 1.4] for _ in 1:100] | ||
) | ||
|
||
model = pm_constructor(;idim=2, hdim=8, zdim=2, poolf=mean_max) | ||
opt = ADAM() | ||
ps = Flux.params(model) | ||
loss(x) = pm_variational_loss(model, x) | ||
|
||
for i in 1:100 | ||
Flux.train!(loss, ps, train_data, opt) | ||
@info i mean(loss.(val_data)) | ||
end | ||
|
||
scatter = Plots.scatter | ||
scatter! = Plots.scatter! | ||
|
||
X = hcat(val_data...) | ||
Y = hcat([reconstruct(model, x) for x in val_data]...) | ||
|
||
scatter(X[1,:],X[2,:], markersize=2, markerstrokewidth=0) | ||
scatter!(Y[1,:],Y[2,:], markersize=2, markerstrokewidth=0) | ||
savefig("val_data.png") | ||
|
||
E = hcat([encoding(model, x) for x in val_data]...) | ||
scatter(E[1,:],E[2,:],zcolor=vcat(zeros(Int, 100),ones(Int, 100))) | ||
savefig("enc.png") | ||
|
||
E_an1 = hcat([encoding(model, x) for x in data3]...) | ||
E_an2 = hcat([encoding(model, x) for x in data4]...) | ||
scatter(E[1,:],E[2,:],label="normal") | ||
scatter!(E_an1[1,:],E_an1[2,:],label="anomalous 1") | ||
scatter!(E_an2[1,:],E_an2[2,:],label="anomalous 2") | ||
savefig("enc_anomaly.png") | ||
|
||
# different pooling fuction (with cardinality) | ||
model = pm_constructor(;idim=2, hdim=8, zdim=2, poolf=mean_max_card) | ||
opt = ADAM() | ||
ps = Flux.params(model) | ||
loss(x) = pm_variational_loss(model, x) | ||
|
||
for i in 1:100 | ||
Flux.train!(loss, ps, train_data, opt) | ||
@info "$i: $(mean(loss.(val_data)))" | ||
end | ||
|
||
X = hcat(val_data...) | ||
Y = hcat([reconstruct(model, x) for x in val_data]...) | ||
|
||
scatter(X[1,:],X[2,:], markersize=2, markerstrokewidth=0) | ||
scatter!(Y[1,:],Y[2,:], markersize=2, markerstrokewidth=0) | ||
savefig("val_data_card.png") | ||
|
||
E = hcat([encoding(model, x) for x in val_data]...) | ||
scatter(E[1,:],E[2,:],zcolor=vcat(zeros(Int, 100),ones(Int, 100))) | ||
savefig("enc_card.png") | ||
|
||
E_an1 = hcat([encoding(model, x) for x in data3]...) | ||
E_an2 = hcat([encoding(model, x) for x in data4]...) | ||
scatter(E[1,:],E[2,:];label="normal", legend=:bottomright) | ||
scatter!(E_an1[1,:],E_an1[2,:],label="anomalous 1") | ||
scatter!(E_an2[1,:],E_an2[2,:],label="anomalous 2") | ||
savefig("enc_anomaly_card.png") | ||
|
||
E_all = hcat(E, E_an1, E_an2) | ||
card = vcat( | ||
map(x -> size(x, 2), val_data), | ||
map(x -> size(x ,2), data3), | ||
map(x -> size(x ,2), data4) | ||
) | ||
scatter(E_all[1,:], E_all[2,:], zcolor=card, color=:jet) | ||
savefig("enc_card.png") | ||
|
||
|
||
model = pm_constructor(;idim=2, hdim=8, zdim=2, poolf=mean_max) | ||
opt = ADAM() | ||
ps = Flux.params(model) | ||
loss(x) = pm_variational_loss(model, x; β=10) | ||
|
||
for i in 1:200 | ||
Flux.train!(loss, ps, train_data, opt) | ||
@info i mean(loss.(val_data)) | ||
end | ||
|
||
X = hcat(val_data...) | ||
Y = hcat([reconstruct(model, x) for x in val_data]...) | ||
|
||
scatter(X[1,:],X[2,:], markersize=2, markerstrokewidth=0) | ||
scatter!(Y[1,:],Y[2,:], markersize=2, markerstrokewidth=0) | ||
savefig("val_data_card_β=10.png") | ||
|
||
E = hcat([encoding(model, x) for x in val_data]...) | ||
scatter(E[1,:],E[2,:],zcolor=vcat(zeros(Int, 100),ones(Int, 100))) | ||
savefig("enc_card_β=10.png") | ||
|
||
E_an1 = hcat([encoding(model, x) for x in data3]...) | ||
E_an2 = hcat([encoding(model, x) for x in data4]...) | ||
scatter(E[1,:],E[2,:];label="normal", legend=:bottomright) | ||
scatter!(E_an1[1,:],E_an1[2,:],label="anomalous 1") | ||
scatter!(E_an2[1,:],E_an2[2,:],label="anomalous 2") | ||
savefig("enc_anomaly_card_β=10.png") | ||
|
||
E_all = hcat(E, E_an1, E_an2) | ||
card = vcat( | ||
map(x -> size(x, 2), val_data), | ||
map(x -> size(x ,2), data3), | ||
map(x -> size(x ,2), data4) | ||
) | ||
scatter(E_all[1,:], E_all[2,:], zcolor=card, color=:jet) | ||
savefig("enc_card_β=10.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using DrWatson | ||
@quickactivate | ||
using GroupAD | ||
using GroupAD: Evaluation | ||
using DataFrames | ||
using Statistics | ||
using EvalMetrics | ||
using PrettyTables | ||
|
||
using Plots | ||
using StatsPlots | ||
#using PlotlyJS | ||
ENV["GKSwstype"] = "100" | ||
|
||
modelnames = ["knn_basic", "vae_basic", "vae_instance", "statistician", "PoolModel", "MGMM"] | ||
modelscores = [:distance, :score, :type, :type, :type, :score] | ||
|
||
# load results collection | ||
toy_results_collection = load(datadir("results/toy", "toy_results_collection.bson")) | ||
|
||
df_vec = map(name -> toy_results_collection[name], modelnames) | ||
df_vec2 = map(name -> insertcols!(toy_results_collection[name], :model => name), modelnames) | ||
df_full = vcat(df_vec2..., cols=:union) | ||
sort!(df_full, :val_AUC_mean, rev=true) | ||
g = groupby(df_full, [:model, :scenario]) | ||
df_best = map(df -> DataFrame(df[1,[:model, :scenario, :test_AUC_mean]]), g) | ||
df_red = vcat(df_best...) | ||
|
||
s1 = filter(:scenario => scenario -> scenario == 1, df_red)[:, [:model, :test_AUC_mean]] | ||
s2 = filter(:scenario => scenario -> scenario == 2, df_red)[:, [:model, :test_AUC_mean]] | ||
s3 = filter(:scenario => scenario -> scenario == 3, df_red)[:, [:model, :test_AUC_mean]] | ||
|
||
H = [] | ||
for modelname in modelnames | ||
v1 = s1[s1[:, :model] .== modelname, :test_AUC_mean] | ||
v2 = s2[s2[:, :model] .== modelname, :test_AUC_mean] | ||
v3 = s3[s3[:, :model] .== modelname, :test_AUC_mean] | ||
V = vcat(v1,v2,v3) | ||
push!(H, V) | ||
end | ||
|
||
H2 = hcat(H...) | ||
H3 = vcat(H2, mean(H2, dims=1)) | ||
_final = DataFrame(hcat(["1","2","3","Average"],H3)) | ||
nice_modelnames = ["scenario", "kNNagg", "VAEagg", "VAE", "NS", "PoolModel", "MGMM"] | ||
final = rename(_final, nice_modelnames) | ||
|
||
|
||
l_max = LatexHighlighter( | ||
(data, i, j) -> (data[i,j] == maximum(final[i, 2:7])) && typeof(data[i,j])!==String, | ||
["textbf", "textcolor{blue}"] | ||
) | ||
l_min = LatexHighlighter( | ||
(data, i, j) -> (data[i,j] == minimum(final[i, 2:7])) && typeof(data[i,j])!==String, | ||
["textcolor{red}"] | ||
) | ||
|
||
t = pretty_table( | ||
final, | ||
highlighters = (l_max, l_min), | ||
formatters = ft_printf("%5.3f"), | ||
backend=:latex, tf=tf_latex_booktabs, nosubheader=true | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.