@@ -75,13 +75,25 @@ closure passes its arguments to `build_latent_gp`, which must return the
7575- `newton_maxiter=100`: maximum number of Newton steps.
7676"""
7777function build_laplace_objective (build_latent_gp, xs, ys; kwargs... )
78- # TODO assumes type of `xs` will be same as `mean(lfx.fx)`
79- f = similar (xs, length (xs)) # will be mutated in-place to "warm-start" the Newton steps
80- return build_laplace_objective! (f, build_latent_gp, xs, ys; kwargs... )
78+ cache = LaplaceObjectiveCache (nothing )
79+ # cache.f will be mutated in-place to "warm-start" the Newton steps
80+ # f should be similar(mean(lfx.fx)), but to construct lfx we would need the arguments
81+ # so we set it to `nothing` initially, and set it to mean(lfx.fx) within the objective
82+ return build_laplace_objective! (cache, build_latent_gp, xs, ys; kwargs... )
83+ end
84+
85+ function build_laplace_objective! (f_init:: Vector , build_latent_gp, xs, ys; kwargs... )
86+ return build_laplace_objective! (
87+ LaplaceObjectiveCache (f_init), build_latent_gp, xs, ys; kwargs...
88+ )
89+ end
90+
91+ mutable struct LaplaceObjectiveCache
92+ f:: Union{Nothing,Vector}
8193end
8294
8395function build_laplace_objective! (
84- f ,
96+ cache :: LaplaceObjectiveCache ,
8597 build_latent_gp,
8698 xs,
8799 ys;
@@ -98,16 +110,18 @@ function build_laplace_objective!(
98110 # Zygote does not like the try/catch within @info etc.
99111 @debug " Objective arguments: $args "
100112 # Zygote does not like in-place assignments either
101- if initialize_f
102- f .= mean (lfx. fx)
113+ if cache. f === nothing
114+ cache. f = mean (lfx. fx)
115+ elseif initialize_f
116+ cache. f .= mean (lfx. fx)
103117 end
104118 end
105119 f_opt, lml = laplace_f_and_lml (
106- lfx, ys; f_init= f, maxiter= newton_maxiter, callback= newton_callback
120+ lfx, ys; f_init= cache . f, maxiter= newton_maxiter, callback= newton_callback
107121 )
108122 ignore_derivatives () do
109123 if newton_warmstart
110- f .= f_opt
124+ cache . f .= f_opt
111125 initialize_f = false
112126 end
113127 end
0 commit comments