Created
April 16, 2020 09:40
-
-
Save Arkoniak/53479c4aff9eaaea42b08455a1aa2244 to your computer and use it in GitHub Desktop.
Labeling algorithm evolution
This file contains hidden or 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
using ParallelKMeans | |
using BenchmarkTools | |
using Distances | |
using Random | |
using UnsafeArrays | |
Random.seed!(2020) | |
X = rand(100, 1000) # 1000 points | |
locations = rand(100, 10) # 10 cluster | |
function predict1(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = zeros(Int, n) | |
@inbounds for i ∈ 1:n | |
minv = Inf | |
for j ∈ 1:k | |
curv = Distances.evaluate(Distances.SqEuclidean(), view(X, :, i), view(locations, :, j)) | |
P = curv < minv | |
pred[i] = j * P + pred[i] * !P # if P is true --> j | |
minv = curv * P + minv * !P # if P is true --> curvalue | |
end | |
end | |
return pred | |
end | |
function predict2(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = zeros(Int, n) | |
@inbounds for i ∈ 1:n | |
minv = Inf | |
for j ∈ 1:k | |
curv = ParallelKMeans.distance(X, locations, i, j) | |
P = curv < minv | |
pred[i] = j * P + pred[i] * !P # if P is true --> j | |
minv = curv * P + minv * !P # if P is true --> curvalue | |
end | |
end | |
return pred | |
end | |
function predict3(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = zeros(Int, n) | |
@inbounds for i ∈ 1:n | |
minv = Inf | |
for j ∈ 1:k | |
curv = ParallelKMeans.distance(X, locations, i, j) | |
pred[i] = curv < minv ? j : pred[i] | |
minv = curv < minv ? curv : minv | |
end | |
end | |
return pred | |
end | |
function predict4(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = ones(Int, n) | |
@inbounds for i ∈ 1:n | |
minv = ParallelKMeans.distance(X, locations, i, 1) | |
for j ∈ 2:k | |
curv = ParallelKMeans.distance(X, locations, i, j) | |
pred[i] = curv < minv ? j : pred[i] | |
minv = curv < minv ? curv : minv | |
end | |
end | |
return pred | |
end | |
function predict5(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = Vector{Int}(undef, n) | |
@inbounds for i ∈ 1:n | |
label = 1 | |
minv = ParallelKMeans.distance(X, locations, i, 1) | |
for j ∈ 2:k | |
curv = ParallelKMeans.distance(X, locations, i, j) | |
label = curv < minv ? j : label | |
minv = curv < minv ? curv : minv | |
end | |
pred[i] = label | |
end | |
return pred | |
end | |
function predict6(X, locations) | |
n = size(X, 2) | |
k = size(locations, 2) | |
pred = Vector{Int}(undef, n) | |
@inbounds for i ∈ 1:n | |
label = 1 | |
minv = Distances.evaluate(Distances.SqEuclidean(), uview(X, :, i), uview(locations, :, 1)) | |
for j ∈ 2:k | |
curv = Distances.evaluate(Distances.SqEuclidean(), uview(X, :, i), uview(locations, :, j)) | |
label = curv < minv ? j : label | |
minv = curv < minv ? curv : minv | |
end | |
pred[i] = label | |
end | |
return pred | |
end | |
res1 = predict1(X, locations) | |
res2 = predict2(X, locations) | |
res3 = predict3(X, locations) | |
res4 = predict4(X, locations) | |
res5 = predict5(X, locations) | |
res6 = predict6(X, locations) | |
@assert res1 == res2 | |
@assert res1 == res3 | |
@assert res1 == res4 | |
@assert res1 == res5 | |
@assert res1 == res6 | |
@btime predict1($X, $locations) | |
# 348.479 μs (20001 allocations: 945.44 KiB) | |
@btime predict2($X, $locations) | |
# 183.494 μs (1 allocation: 7.94 KiB) | |
@btime predict3($X, $locations) | |
# 172.322 μs (1 allocation: 7.94 KiB) | |
@btime predict4($X, $locations) | |
# 168.629 μs (1 allocation: 7.94 KiB) | |
@btime predict5($X, $locations) | |
# 139.911 μs (1 allocation: 7.94 KiB) | |
@btime predict6($X, $locations) | |
# 190.055 μs (1 allocation: 7.94 KiB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment