@@ -31,13 +31,13 @@ x_test = range(xmin - 0.1, xmax + 0.1; length=300)
31
31
scatter (x_train, y_train; lab= " data" )
32
32
plot! (x_test, sinc; lab= " true function" )
33
33
34
- # ## Kernel training, Method 1
34
+ # ## Method 1
35
35
# The first method is to rebuild the parametrized kernel from a vector of parameters
36
36
# in each evaluation of the cost fuction. This is similar to the approach taken in
37
37
# [Stheno.jl](https://github.com/JuliaGaussianProcesses/Stheno.jl).
38
38
39
39
40
- # ### Simplest Approach
40
+ # ### Base Approach
41
41
# A simple way to ensure that the kernel parameters are positive
42
42
# is to optimize over the logarithm of the parameters.
43
43
@@ -86,7 +86,6 @@ loss(log.(ones(4)))
86
86
anim = Animation ()
87
87
opt = Optimise. ADAGrad (0.5 )
88
88
for i in 1 : 30
89
- println (i)
90
89
grads = only (Zygote. gradient (loss, θ)) # We compute the gradients given the kernel parameters and regularization
91
90
Optimise. update! (opt, θ, grads)
92
91
scatter (
99
98
gif (anim)
100
99
101
100
# ### ParameterHandling.jl
102
- # Alternatively, we can use the [ParameterHandling.jl](https://github.com/invenia/ParameterHandling.jl) package.
101
+ # Alternatively, we can use the [ParameterHandling.jl](https://github.com/invenia/ParameterHandling.jl) package
102
+ # to handle the requirement that all kernel parameters should be positive.
103
103
104
104
raw_initial_θ = (
105
105
k1 = positive (1.1 ),
@@ -137,7 +137,6 @@ loss(initial_θ)
137
137
anim = Animation ()
138
138
opt = Optimise. ADAGrad (0.5 )
139
139
for i in 1 : 30
140
- println (i)
141
140
grads = only (Zygote. gradient (loss ∘ unflatten, flat_θ)) # We compute the gradients given the kernel parameters and regularization
142
141
Optimise. update! (opt, flat_θ, grads)
143
142
scatter (
@@ -147,4 +146,24 @@ for i in 1:30
147
146
plot! (x_test, f (x_test, x_train, y_train, unflatten (flat_θ)); lab= " Prediction" , lw= 3.0 )
148
147
frame (anim)
149
148
end
150
- gif (anim)
149
+ gif (anim)
150
+
151
+
152
+ # ## Method 2: Functor
153
+ # An alternative method is to use tools from Flux.jl, which is a fairly heavy package.
154
+
155
+ # raw_initial_θ = (
156
+ # k1 = positive(1.1),
157
+ # k2 = positive(0.1),
158
+ # k3 = positive(0.01),
159
+ # noise_var=positive(0.001),
160
+ # )
161
+ k1 = 1.1
162
+ k2 = 0.1
163
+ k3 = 0.01
164
+ noise_var = 0.001
165
+
166
+ kernel = (k1 * SqExponentialKernel () + k2 * Matern32Kernel ()) ∘
167
+ ScaleTransform (k3)
168
+
169
+ Θ = Flux. params (k1, k2, k3)
0 commit comments