Kernel's hyper-parameters; initialization and setting bounds

有些话、适合烂在心里 提交于 2021-01-29 12:31:15

问题


I think many other people like me might be interested in how they can use GPFlow for their special problems. The key is how GPFlow is customizable, and a good example would be very helpful.

In my case, I read and tried lots of comments in raised issues without any real success. Setting kernel model parameters is not straightforward (creating with default values, and then do it via the delete object method). Transform method is vague.

It would be really helpful if you could add an example showing. how one can initialize and set bounds of an anisotropic kernel model (length-scales values and bounds, variances, ...) and specially adding observations error (as an array-like alpha parameter)


回答1:


If you just want to set a value, then you can do

model = gpflow.models.GPR(np.zeros((1, 1)),
                          np.zeros((1, 1)),
                          gpflow.kernels.RBF(1, lengthscales=0.2))

Alternatively

model = gpflow.models.GPR(np.zeros((1, 1)),
                          np.zeros((1, 1)),
                          gpflow.kernels.RBF(1))
model.kern.lengthscales = 0.2

If you want to change the transform, you either need to subclass the kernel, or you can also do

with gpflow.defer_build():
     model = gpflow.models.GPR(np.zeros((1, 1)),
                               np.zeros((1, 1)),
                               gpflow.kernels.RBF(1))
     transform = gpflow.transforms.Logistic(0.1, 1.))
     model.kern.lengthscales = gpflow.params.Parameter(0.3, transform=transform)
model.compile()

You need the defer_build to stop the graph being compiled before you've changed the transform. Using the approach above, the compilation of the tensorflow graph is delayed (until the explicit model.compile()) so is built with the intended bounding transform.

Using an array parameter for likelihood variance is outside the scope of gpflow. For what it's worth (and because it has been asked about before), that particular model is especially problematic as it is not clear how test points are defined.




回答2:


Setting kernel parameters can be done using the .assign() function, or through direct assignment. See the notebook https://github.com/GPflow/GPflow/blob/develop/doc/source/notebooks/understanding/tf_graphs_and_sessions.ipynb. You do not need to delete a parameter to assign a new value to it.

If you want to have per-datapoint noise, you will need to implement your own custom likelihood, which you can do by taking Gaussian likelihood in likelihoods.py as an example.

If by "bounds" you mean limiting the optimisation range for a parameter, you can use the Logistic transform. If you want to pass in a custom transformation for a parameter, you can pass a constructed Parameter object into constructors with a custom transform. Alternatively you can assign a newly created Parameter with a new transform to the model.




回答3:


Here is more information on how to access and change GPflow parameters: viewing, getting and settings parameters documentation.

Extra bit for @user1018464 answer about replacing transform in existing parameter: changing transformation is a bit tricky, you can't change transformation once a model was compiled in TensorFlow.

E.g.

likelihood = gpflow.likelihoods.Gaussian()
likelihood.variance.transform = gpflow.transforms.Logistic(1., 10.)
----
GPflowError: Parameter "Gaussian/variance" has already been compiled.

Instead you have to reset GPflow object:

likelihood = gpflow.likelihoods.Gaussian()  # All tensors compiled

likelihood.clear()
likelihood.variance.transform = gpflow.transforms.Logistic(2, 5)
likelihood.variance = 2.5
likelihood.compile()


来源:https://stackoverflow.com/questions/55457750/kernels-hyper-parameters-initialization-and-setting-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!