Giter Club home page Giter Club logo

Comments (2)

FelixBenning avatar FelixBenning commented on June 11, 2024

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.

FelixBenning avatar FelixBenning commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.