Skip to content

Commit 1a4e0dd

Browse files
gnadttheogf
andauthored
Bump compat to include Functors v0.5 (#566)
* Bump compat to include Functors v0.5 * Change ADAGrad to AdaGrad in example... limits compat to Flux v0.13 & v0.14 --------- Co-authored-by: Théo Galy-Fajou <[email protected]>
1 parent e50b747 commit 1a4e0dd

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Compat = "3.7, 4"
2727
CompositionsBase = "0.1"
2828
Distances = "0.10.9"
2929
FillArrays = "0.10, 0.11, 0.12, 0.13, 1"
30-
Functors = "0.1, 0.2, 0.3, 0.4"
30+
Functors = "0.1, 0.2, 0.3, 0.4, 0.5"
3131
IrrationalConstants = "0.1, 0.2"
3232
LogExpFunctions = "0.2.1, 0.3"
3333
Requires = "1.0.1"

examples/train-kernel-parameters/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
1313
[compat]
1414
BenchmarkTools = "1.2"
1515
Distributions = "0.25"
16-
Flux = "0.12, 0.13, 0.14"
16+
Flux = "0.13, 0.14"
1717
ForwardDiff = "0.10"
1818
KernelFunctions = "0.10"
1919
Literate = "2"

examples/train-kernel-parameters/script.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# # Train Kernel Parameters
22

3-
# Here we show a few ways to train (optimize) the kernel (hyper)parameters at the example of kernel-based regression using KernelFunctions.jl.
4-
# All options are functionally identical, but differ a little in readability, dependencies, and computational cost.
3+
# Here we show a few ways to train (optimize) the kernel (hyper)parameters at the example of kernel-based regression using KernelFunctions.jl.
4+
# All options are functionally identical, but differ a little in readability, dependencies, and computational cost.
55

6-
# We load KernelFunctions and some other packages. Note that while we use `Zygote` for automatic differentiation and `Flux.optimise` for optimization, you should be able to replace them with your favourite autodiff framework or optimizer.
6+
# We load KernelFunctions and some other packages. Note that while we use `Zygote` for automatic differentiation and `Flux.optimise` for optimization, you should be able to replace them with your favourite autodiff framework or optimizer.
77

88
using KernelFunctions
99
using LinearAlgebra
@@ -33,14 +33,14 @@ scatter(x_train, y_train; label="data")
3333
plot!(x_test, sinc; label="true function")
3434

3535
# ## Manual Approach
36-
# The first option is to rebuild the parametrized kernel from a vector of parameters
37-
# in each evaluation of the cost function. This is similar to the approach taken in
36+
# The first option is to rebuild the parametrized kernel from a vector of parameters
37+
# in each evaluation of the cost function. This is similar to the approach taken in
3838
# [Stheno.jl](https://github.com/JuliaGaussianProcesses/Stheno.jl).
3939

4040
# To train the kernel parameters via [Zygote.jl](https://github.com/FluxML/Zygote.jl),
4141
# we need to create a function creating a kernel from an array.
4242
# A simple way to ensure that the kernel parameters are positive
43-
# is to optimize over the logarithm of the parameters.
43+
# is to optimize over the logarithm of the parameters.
4444

4545
function kernel_creator(θ)
4646
return (exp(θ[1]) * SqExponentialKernel() + exp(θ[2]) * Matern32Kernel())
@@ -59,7 +59,7 @@ end
5959
nothing #hide
6060

6161
# Let's look at our prediction.
62-
# With starting parameters `p0` (picked so we get the right local
62+
# With starting parameters `p0` (picked so we get the right local
6363
# minimum for demonstration) we get:
6464

6565
p0 = [1.1, 0.1, 0.01, 0.001]
@@ -85,16 +85,16 @@ loss(θ)
8585

8686
@benchmark let
8787
θ = log.(p0)
88-
opt = Optimise.ADAGrad(0.5)
88+
opt = Optimise.AdaGrad(0.5)
8989
grads = only((Zygote.gradient(loss, θ)))
9090
Optimise.update!(opt, θ, grads)
9191
end
9292

9393
# ### Training the model
9494

95-
# Setting an initial value and initializing the optimizer:
95+
# Setting an initial value and initializing the optimizer:
9696
θ = log.(p0) # Initial vector
97-
opt = Optimise.ADAGrad(0.5)
97+
opt = Optimise.AdaGrad(0.5)
9898
nothing #hide
9999

100100
# Optimize
@@ -119,10 +119,10 @@ nothing; #hide
119119
loss(θ)
120120

121121
# ## Using ParameterHandling.jl
122-
# Alternatively, we can use the [ParameterHandling.jl](https://github.com/invenia/ParameterHandling.jl) package
123-
# to handle the requirement that all kernel parameters should be positive.
124-
# The package also allows arbitrarily nesting named tuples that make the parameters
125-
# more human readable, without having to remember their position in a flat vector.
122+
# Alternatively, we can use the [ParameterHandling.jl](https://github.com/invenia/ParameterHandling.jl) package
123+
# to handle the requirement that all kernel parameters should be positive.
124+
# The package also allows arbitrarily nesting named tuples that make the parameters
125+
# more human readable, without having to remember their position in a flat vector.
126126

127127
using ParameterHandling
128128

@@ -133,7 +133,7 @@ raw_initial_θ = (
133133
flat_θ, unflatten = ParameterHandling.value_flatten(raw_initial_θ)
134134
flat_θ #hide
135135

136-
# We define a few relevant functions and note that compared to the previous `kernel_creator` function, we do not need explicit `exp`s.
136+
# We define a few relevant functions and note that compared to the previous `kernel_creator` function, we do not need explicit `exp`s.
137137

138138
function kernel_creator(θ)
139139
return.k1 * SqExponentialKernel() + θ.k2 * Matern32Kernel()) ScaleTransform.k3)
@@ -164,7 +164,7 @@ nothing #hide
164164

165165
@benchmark let
166166
θ = flat_θ[:]
167-
opt = Optimise.ADAGrad(0.5)
167+
opt = Optimise.AdaGrad(0.5)
168168
grads = (Zygote.gradient(loss unflatten, θ))[1]
169169
Optimise.update!(opt, θ, grads)
170170
end
@@ -173,7 +173,7 @@ end
173173

174174
# Optimize
175175

176-
opt = Optimise.ADAGrad(0.5)
176+
opt = Optimise.AdaGrad(0.5)
177177
for i in 1:15
178178
grads = (Zygote.gradient(loss unflatten, flat_θ))[1]
179179
Optimise.update!(opt, flat_θ, grads)
@@ -185,11 +185,11 @@ nothing #hide
185185
(loss unflatten)(flat_θ)
186186

187187
# ## Flux.destructure
188-
# If we don't want to write an explicit function to construct the kernel, we can alternatively use the `Flux.destructure` function.
189-
# Again, we need to ensure that the parameters are positive. Note that the `exp` function is now part of the loss function, instead of part of the kernel construction.
188+
# If we don't want to write an explicit function to construct the kernel, we can alternatively use the `Flux.destructure` function.
189+
# Again, we need to ensure that the parameters are positive. Note that the `exp` function is now part of the loss function, instead of part of the kernel construction.
190190

191-
# We could also use ParameterHandling.jl here.
192-
# To do so, one would remove the `exp`s from the loss function below and call `loss ∘ unflatten` as above.
191+
# We could also use ParameterHandling.jl here.
192+
# To do so, one would remove the `exp`s from the loss function below and call `loss ∘ unflatten` as above.
193193

194194
θ = [1.1, 0.1, 0.01, 0.001]
195195

@@ -217,7 +217,7 @@ nothing #hide
217217

218218
# Cost for one step
219219

220-
@benchmark let θt = θ[:], optt = Optimise.ADAGrad(0.5)
220+
@benchmark let θt = θ[:], optt = Optimise.AdaGrad(0.5)
221221
grads = only((Zygote.gradient(loss, θt)))
222222
Optimise.update!(optt, θt, grads)
223223
end
@@ -228,9 +228,9 @@ end
228228
θ = log.([1.1, 0.1, 0.01, 0.001]) # Initial vector
229229
loss(θ)
230230

231-
# Initialize optimizer
231+
# Initialize optimizer
232232

233-
opt = Optimise.ADAGrad(0.5)
233+
opt = Optimise.AdaGrad(0.5)
234234
nothing #hide
235235

236236
# Optimize

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Distances = "0.10"
2828
Documenter = "0.25, 0.26, 0.27"
2929
FiniteDifferences = "0.10.8, 0.11, 0.12"
3030
ForwardDiff = "0.10"
31-
Functors = "0.2, 0.3, 0.4"
31+
Functors = "0.2, 0.3, 0.4, 0.5"
3232
Kronecker = "0.4, 0.5"
3333
LogExpFunctions = "0.2, 0.3"
3434
PDMats = "0.9, 0.10, 0.11"

0 commit comments

Comments
 (0)