Skip to content

Fixes #115 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ abstract type BaseKernel <: Kernel end
abstract type SimpleKernel <: BaseKernel end

include("utils.jl")
include("distances/pairwise.jl")
include("distances/dotproduct.jl")
include("distances/delta.jl")
include("distances/sinus.jl")
Expand Down
41 changes: 41 additions & 0 deletions src/distances/pairwise.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Add our own pairwise function to be able to apply it on vectors

pairwise(d::PreMetric, X::AbstractVector, Y::AbstractVector) = broadcast(d, X, Y')

pairwise(d::PreMetric, X::AbstractVector) = pairwise(d, X, X)

function pairwise!(
out::AbstractMatrix,
d::PreMetric,
X::AbstractVector,
Y::AbstractVector,
)
broadcast!(d, out, X, Y')
end

pairwise!(out::AbstractMatrix, d::PreMetric, X::AbstractVector) = pairwise!(out, d, X, X)

function pairwise(d::PreMetric, x::AbstractVector{<:Real})
return Distances.pairwise(d, reshape(x, :, 1); dims = 1)
end

function pairwise(
d::PreMetric,
x::AbstractVector{<:Real},
y::AbstractVector{<:Real},
)
return Distances.pairwise(d, reshape(x, :, 1), reshape(y, :, 1); dims = 1)
end

function pairwise!(out::AbstractMatrix, d::PreMetric, x::AbstractVector{<:Real})
return Distances.pairwise!(out, d, reshape(x, :, 1); dims = 1)
end

function pairwise!(
out::AbstractMatrix,
d::PreMetric,
x::AbstractVector{<:Real},
y::AbstractVector{<:Real},
)
return Distances.pairwise!(out, d, reshape(x, :, 1), reshape(y, :, 1); dims=1)
end
26 changes: 0 additions & 26 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,3 @@ end

# Fallback implementation of evaluate for `SimpleKernel`s.
(k::SimpleKernel)(x, y) = kappa(k, evaluate(metric(k), x, y))

# This is type piracy. We should not doing this.
function Distances.pairwise(d::PreMetric, x::AbstractVector{<:Real})
return pairwise(d, reshape(x, :, 1); dims=1)
end

function Distances.pairwise(
d::PreMetric,
x::AbstractVector{<:Real},
y::AbstractVector{<:Real},
)
return pairwise(d, reshape(x, :, 1), reshape(y, :, 1); dims=1)
end

function Distances.pairwise!(out::AbstractMatrix, d::PreMetric, x::AbstractVector{<:Real})
return pairwise!(out, d, reshape(x, :, 1); dims=1)
end

function Distances.pairwise!(
out::AbstractMatrix,
d::PreMetric,
x::AbstractVector{<:Real},
y::AbstractVector{<:Real},
)
return pairwise!(out, d, reshape(x, :, 1), reshape(y, :, 1); dims=1)
end
24 changes: 12 additions & 12 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Base.getindex(D::ColVecs, i) = ColVecs(view(D.X, :, i))

dim(x::ColVecs) = size(x.X, 1)

Distances.pairwise(d::PreMetric, x::ColVecs) = pairwise(d, x.X; dims=2)
Distances.pairwise(d::PreMetric, x::ColVecs, y::ColVecs) = pairwise(d, x.X, y.X; dims=2)
function Distances.pairwise!(out::AbstractMatrix, d::PreMetric, x::ColVecs)
return pairwise!(out, d, x.X; dims=2)
pairwise(d::PreMetric, x::ColVecs) = Distances.pairwise(d, x.X; dims=2)
pairwise(d::PreMetric, x::ColVecs, y::ColVecs) = Distances.pairwise(d, x.X, y.X; dims=2)
function pairwise!(out::AbstractMatrix, d::PreMetric, x::ColVecs)
return Distances.pairwise!(out, d, x.X; dims=2)
end
function Distances.pairwise!(out::AbstractMatrix, d::PreMetric, x::ColVecs, y::ColVecs)
return pairwise!(out, d, x.X, y.X; dims=2)
function pairwise!(out::AbstractMatrix, d::PreMetric, x::ColVecs, y::ColVecs)
return Distances.pairwise!(out, d, x.X, y.X; dims=2)
end

"""
Expand All @@ -73,13 +73,13 @@ Base.getindex(D::RowVecs, i) = RowVecs(view(D.X, i, :))

dim(x::RowVecs) = size(x.X, 2)

Distances.pairwise(d::PreMetric, x::RowVecs) = pairwise(d, x.X; dims=1)
Distances.pairwise(d::PreMetric, x::RowVecs, y::RowVecs) = pairwise(d, x.X, y.X; dims=1)
function Distances.pairwise!(out::AbstractMatrix, d::PreMetric, x::RowVecs)
return pairwise!(out, d, x.X; dims=1)
pairwise(d::PreMetric, x::RowVecs) = Distances.pairwise(d, x.X; dims=1)
pairwise(d::PreMetric, x::RowVecs, y::RowVecs) = Distances.pairwise(d, x.X, y.X; dims=1)
function pairwise!(out::AbstractMatrix, d::PreMetric, x::RowVecs)
return Distances.pairwise!(out, d, x.X; dims=1)
end
function Distances.pairwise!(out::AbstractMatrix, d::PreMetric, x::RowVecs, y::RowVecs)
return pairwise!(out, d, x.X, y.X; dims=1)
function pairwise!(out::AbstractMatrix, d::PreMetric, x::RowVecs, y::RowVecs)
return Distances.pairwise!(out, d, x.X, y.X; dims=1)
end

"""
Expand Down
16 changes: 8 additions & 8 deletions src/zygote_adjoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
end
end

@adjoint function pairwise(d::Delta, X::AbstractMatrix, Y::AbstractMatrix; dims=2)
D = pairwise(d, X, Y; dims = dims)
@adjoint function Distances.pairwise(d::Delta, X::AbstractMatrix, Y::AbstractMatrix; dims=2)
D = Distances.pairwise(d, X, Y; dims = dims)
if dims == 1
return D, Δ -> (nothing, nothing, nothing)
else
return D, Δ -> (nothing, nothing, nothing)
end
end

@adjoint function pairwise(d::Delta, X::AbstractMatrix; dims=2)
D = pairwise(d, X; dims = dims)
@adjoint function Distances.pairwise(d::Delta, X::AbstractMatrix; dims=2)
D = Distances.pairwise(d, X; dims = dims)
if dims == 1
return D, Δ -> (nothing, nothing)
else
Expand All @@ -30,17 +30,17 @@ end
end
end

@adjoint function pairwise(d::DotProduct, X::AbstractMatrix, Y::AbstractMatrix; dims=2)
D = pairwise(d, X, Y; dims = dims)
@adjoint function Distances.pairwise(d::DotProduct, X::AbstractMatrix, Y::AbstractMatrix; dims=2)
D = Distances.pairwise(d, X, Y; dims = dims)
if dims == 1
return D, Δ -> (nothing, Δ * Y, (X' * Δ)')
else
return D, Δ -> (nothing, (Δ * Y')', X * Δ)
end
end

@adjoint function pairwise(d::DotProduct, X::AbstractMatrix; dims=2)
D = pairwise(d, X; dims = dims)
@adjoint function Distances.pairwise(d::DotProduct, X::AbstractMatrix; dims=2)
D = Distances.pairwise(d, X; dims = dims)
if dims == 1
return D, Δ -> (nothing, 2 * Δ * X)
else
Expand Down
29 changes: 29 additions & 0 deletions test/distances/pairwise.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@testset "pairwise" begin
rng = MersenneTwister(123456)
d = SqEuclidean()
Ns = (4, 5)
D = 3
x = [randn(rng, D) for _ in 1:Ns[1]]
y = [randn(rng, D) for _ in 1:Ns[2]]
X = hcat(x...)
Y = hcat(y...)
K = zeros(Ns)

@test KernelFunctions.pairwise(d, x, y) ≈ pairwise(d, X, Y, dims=2)
@test KernelFunctions.pairwise(d, x) ≈ pairwise(d, X, dims=2)
KernelFunctions.pairwise!(K, d, x, y)
@test K ≈ pairwise(d, X, Y, dims=2)
K = zeros(Ns[1], Ns[1])
KernelFunctions.pairwise!(K, d, x)
@test K ≈ pairwise(d, X, dims=2)

x = randn(rng, 10)
X = reshape(x, :, 1)
y = randn(rng, 11)
Y = reshape(y, :, 1)
K = zeros(10, 11)
@test KernelFunctions.pairwise(d, x, y) ≈ pairwise(d, X, Y; dims=1)
@test KernelFunctions.pairwise(d, x) ≈ pairwise(d, X; dims=1)
KernelFunctions.pairwise!(K, d, x, y)
@test K ≈ pairwise(d, X, Y, dims=1)
end
8 changes: 0 additions & 8 deletions test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,4 @@
@test length(k) == 1
@test iterate(k) == (k,nothing)
@test iterate(k,1) == nothing

rng = MersenneTwister(123456)
x = randn(rng, 10)
X = reshape(x, :, 1)
y = randn(rng, 11)
Y = reshape(y, :, 1)
@test pairwise(SqEuclidean(), x, y) ≈ pairwise(SqEuclidean(), X, Y; dims=1)
@test pairwise(SqEuclidean(), x) ≈ pairwise(SqEuclidean(), X; dims=1)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using KernelFunctions: metric, kappa, ColVecs, RowVecs
include("utils_AD.jl")

@testset "distances" begin
include(joinpath("distances", "pairwise.jl"))
include(joinpath("distances", "dotproduct.jl"))
include(joinpath("distances", "delta.jl"))
include(joinpath("distances", "sinus.jl"))
Expand Down
16 changes: 8 additions & 8 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

Y = randn(rng, D, N + 1)
DY = ColVecs(Y)
@test pairwise(SqEuclidean(), DX) ≈ pairwise(SqEuclidean(), X; dims=2)
@test pairwise(SqEuclidean(), DX, DY) ≈ pairwise(SqEuclidean(), X, Y; dims=2)
@test KernelFunctions.pairwise(SqEuclidean(), DX) ≈ pairwise(SqEuclidean(), X; dims=2)
@test KernelFunctions.pairwise(SqEuclidean(), DX, DY) ≈ pairwise(SqEuclidean(), X, Y; dims=2)
K = zeros(N, N)
pairwise!(K, SqEuclidean(), DX)
KernelFunctions.pairwise!(K, SqEuclidean(), DX)
@test K ≈ pairwise(SqEuclidean(), X; dims=2)
K = zeros(N, N + 1)
pairwise!(K, SqEuclidean(), DX, DY)
KernelFunctions.pairwise!(K, SqEuclidean(), DX, DY)
@test K ≈ pairwise(SqEuclidean(), X, Y; dims=2)

let
Expand Down Expand Up @@ -56,13 +56,13 @@

Y = randn(rng, D + 1, N)
DY = RowVecs(Y)
@test pairwise(SqEuclidean(), DX) ≈ pairwise(SqEuclidean(), X; dims=1)
@test pairwise(SqEuclidean(), DX, DY) ≈ pairwise(SqEuclidean(), X, Y; dims=1)
@test KernelFunctions.pairwise(SqEuclidean(), DX) ≈ pairwise(SqEuclidean(), X; dims=1)
@test KernelFunctions.pairwise(SqEuclidean(), DX, DY) ≈ pairwise(SqEuclidean(), X, Y; dims=1)
K = zeros(D, D)
pairwise!(K, SqEuclidean(), DX)
KernelFunctions.pairwise!(K, SqEuclidean(), DX)
@test K ≈ pairwise(SqEuclidean(), X; dims=1)
K = zeros(D, D + 1)
pairwise!(K, SqEuclidean(), DX, DY)
KernelFunctions.pairwise!(K, SqEuclidean(), DX, DY)
@test K ≈ pairwise(SqEuclidean(), X, Y; dims=1)

let
Expand Down