Comments (2)
The following code (with less mutation) does not yield any output and (after spamming ^C) finally prints segfaults:
using KernelFunctions
using OneHotArrays: OneHotVector
using ForwardDiff: derivative
struct Pt{Dim}
pos::AbstractArray
partial
end
Pt(x;partial=()) = Pt{length(x)}(x, partial)
struct TaylorKernel <: KernelFunctions.Kernel
k::KernelFunctions.Kernel
end
function (tk::TaylorKernel)(x::Pt{Dim}, y::Pt{Dim}) where Dim
k = tk.k
for ii in x.partial
k = (x₁,x₂) -> derivative(0) do Δx
return k(x₁ + Δx * OneHotVector(ii, Dim), x₂)
end
end
for jj in y.partial
k = (x₁,x₂) -> derivative(0) do Δx
return k(x₁,x₂ + Δx * OneHotVector(jj, Dim))
end
end
k(x.pos, y.pos)
end
k = TaylorKernel(MaternKernel())
k(Pt([1]), Pt([2])) # k(x,y) with x=1, y=2
k(Pt([1], partial=(1,)), Pt([2])) # ∂ₓk(x,y)
from forwarddiff.jl.
The second code snippet does not work, because unlike for variables you can not use the variable in the reassignment like this. This ends up in an infinite recursion which explains the segmentation fault I think.
The following seems to work
function (tk::TaylorKernel)(x::Pt{Dim}, y::Pt{Dim}) where Dim
if !isnothing(local next = iterate(x.partial))
ii, state = next # take partial derivative in direction ii
return FD.derivative(0) do dx
tk( # recursion
Pt(
x.pos + dx * OneHotVector(ii, Dim), # directional variation
partial=Base.rest(x.partial, state) # remaining partial derivatives
),
y
)
end
end
if !isnothing(local next = iterate(y.partial))
jj, state = next # take partial derivative in direction jj
return FD.derivative(0) do dy
tk( # recursion
x,
Pt(
y.pos + dy * OneHotVector(jj, Dim), # directional variation
partial=Base.rest(x.partial, state) # remaining partial derivatives
)
)
end
end
tk.k(x.pos, y.pos)
end
this leads me to believe, that the mutation in the first snippet was actually the culprit. I'll close the issue
from forwarddiff.jl.
Related Issues (20)
- Implement gammalogccdf for ForwardDiff HOT 1
- `ForwardDiff.jacobian` throws error for `fft` HOT 1
- Correctly forming nested dual numbers. HOT 8
- Derivative of a function of derivatives HOT 7
- Symbolics.jl compatibility HOT 1
- Support derivative(f, ::Complex) HOT 1
- `ForwardDiff` fails to compute correct derivative HOT 3
- Incorrect Hessian by `exp` function HOT 1
- Method ambiguities reported by Aqua HOT 3
- Document internals? HOT 1
- Bug (NaNs) when differentiating eigenvectors of Symmetric matrices
- Error requiring `Symbolics` from `Optimization` HOT 1
- promote_rule ambiguity with AbstractIrrational and ForwardDiff.Dual HOT 2
- Allocation tests broken since Julia 1.9
- LoadError: ArgumentError: Package AdaptStaticArraysCoreExt does not have Adapt in its dependencies: HOT 2
- Working with anonymous functions HOT 2
- DiffResults objects are not re-aliased properly HOT 2
- `gradient!` allocates for matrices but not for vectors
- ERROR: Package ForwardDiff errored during testing HOT 1
- Reinterpret discards information and returns incorrect result HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from forwarddiff.jl.