Skip to content

Commit f2e18bb

Browse files
committed
Added tests for adjoints
1 parent f511f52 commit f2e18bb

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ julia = "1.0"
2121
[extras]
2222
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2323
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
24+
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
2425

2526
[targets]
26-
test = ["Random", "Test"]
27+
test = ["Random", "Test", "FiniteDifferences"]

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ include("test_transform.jl")
1111
include("test_distances.jl")
1212
include("test_kernels.jl")
1313
include("test_generic.jl")
14+
include("test_adjoints.jl")
1415
#include("types.jl")
1516
end

test/test_adjoints.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Zygote
2+
using FiniteDifferences
3+
using KernelFunctions
4+
using Distances
5+
using Test
6+
7+
x = rand(5)
8+
y = rand(5)
9+
10+
@testset "Testing Zygote adjoints" begin
11+
gzeucl = first(Zygote.gradient(xy->evaluate(Euclidean(),xy[1],xy[2]),[x,y]))
12+
gzsqeucl = first(Zygote.gradient(xy->evaluate(SqEuclidean(),xy[1],xy[2]),[x,y]))
13+
gzdotprod = first(Zygote.gradient(xy->evaluate(KernelFunctions.DotProduct(),xy[1],xy[2]),[x,y]))
14+
15+
FDM = central_fdm(5,1)
16+
17+
gfeucl = collect(first(FiniteDifferences.grad(FDM,xy->evaluate(Euclidean(),xy[1],xy[2]),(x,y))))
18+
gfsqeucl = collect(first(FiniteDifferences.grad(FDM,xy->evaluate(SqEuclidean(),xy[1],xy[2]),(x,y))))
19+
gfdotprod =collect(first(FiniteDifferences.grad(FDM,xy->evaluate(KernelFunctions.DotProduct(),xy[1],xy[2]),(x,y))))
20+
21+
@test all(gzeucl .≈ gfeucl)
22+
@test all(gzsqeucl .≈ gfsqeucl)
23+
@test all(gzdotprod .≈ gfdotprod)
24+
25+
end

0 commit comments

Comments
 (0)