如何在 TensorFlow Probability 中创建不同内核对象的总和? [英] How to create sum of different kernel objects in TensorFlow Probability?
问题描述
我有一个关于在 Tensorflow-probability
中指定核函数的问题.
I have one question about specifying kernel function in Tensorflow-probability
.
通常,如果我想创建一个内核对象,我会写
Usually, if I want to create a kernel object, I will write
import tensorflow as tf
import tensorflow_probability as tfp
tfp_kernels = tfp.positive_semidefinite_kernels
kernel_obj = tfp_kernels.ExponentiateQuadratic(*args, **karwgs)
我知道内核对象支持批量广播.但是如果我想构建一个内核对象,它是几个不同内核对象的总和,比如加性高斯过程?
I know that kernel object support batch broadcasting. But what if I want to build a kernel object that is the sum of several different kernel objects, like additive Gaussian processes?
我不确定如何在 Tensorflow 中总结"内核对象.我能做的就是创建几个单独的内核对象K1, ... KJ
网上好像没有类似的问题.
I am not sure how to "sum" up the kernel object in Tensorflow. What I am able to do is to create several separate kernel objects K1, ... KJ
It seems that there is no similar question online.
提前感谢您的帮助.
更新:我试过直接+
,但是协方差矩阵有一些奇怪的.
Updates: I tried direct +
, but there is something strange with the covariance matrix.
我编写了以下示例:
feature1 = np.array([1, 2, 3, 5], dtype=np.float32)[:, np.newaxis]
feature2 = np.array([4.2, 6.5, 7.4, 8.3], dtype=np.float32)[:, np.newaxis]
features = np.concatenate([feature1, feature2], axis=1)
k1 = tfp_kernels.ExponentiatedQuadratic(amplitude=tf.cast(2.0, tf.float32),
length_scale=tf.cast(2.0, tf.float32),
feature_ndims=1,
name='k1')
k2 = tfp_kernels.ExponentiatedQuadratic(amplitude=tf.cast(1.5, tf.float32),
length_scale=tf.cast(1.5, tf.float32),
feature_ndims=1,
name='k2')
K = k1 + k2
gp_1 = tfd.GaussianProcess(kernel=k1,
index_points=feature1,
jitter=tf.cast(0, tf.float32),
name='gp_1')
gp_2 = tfd.GaussianProcess(kernel=k2,
index_points=feature2,
jitter=tf.cast(0, tf.float32),
name='gp_2')
gp_K1 = tfd.GaussianProcess(kernel=K,
index_points=feature1,
jitter=tf.cast(0, tf.float32),
name='gp_K')
gp_K2 = tfd.GaussianProcess(kernel=K,
index_points=feature2,
jitter=tf.cast(0, tf.float32),
name='gp_K')
gp_K = tfd.GaussianProcess(kernel=K,
index_points=features,
jitter=tf.cast(0, tf.float32),
name='gp_K')
gp_1_cov = gp_1.covariance()
gp_2_cov = gp_2.covariance()
gp_K1_cov = gp_K1.covariance()
gp_K2_cov = gp_K2.covariance()
gp_K_cov = gp_K.covariance()
with tf.Session() as my_sess:
[gp_1_cov_, gp_2_cov_, gp_K1_cov_, gp_K2_cov_, gp_K_cov_] = my_sess.run([gp_1_cov, gp_2_cov, gp_K1_cov, gp_K2_cov, gp_K_cov])
my_sess.close()
print(gp_1_cov_)
print(gp_2_cov_)
print(gp_K1_cov_)
print(gp_K2_cov_)
print(gp_K_cov_)
前四个协方差矩阵很好,我通过比较 k(x_i, x_j)
元素来仔细检查它.
The first four covariance matrices are fine, and I double check it by comparing the k(x_i, x_j)
element-wise.
但是,我不知道它是如何计算最后一个的.我试过
However, I don't know how it computes the last one. I tried
- feature_1 与 kernel_1 和 feature_2 与 kernel_2
- feature_1 与 kernel_2 和 feature_2 与 kernel_1
以下是最后三个矩阵的结果:
Below are the results of the last three matrices:
[[6.25 5.331647 3.3511252 0.60561347]
[5.331647 6.25 5.331647 1.6031142 ]
[3.3511252 5.331647 6.25 3.3511252 ]
[0.60561347 1.6031142 3.3511252 6.25 ]]
[[6.25 2.7592793 1.3433135 0.54289836]
[2.7592793 6.25 5.494186 3.7630994 ]
[1.3433135 5.494186 6.25 5.494186 ]
[0.54289836 3.7630994 5.494186 6.25 ]]
[[6.25 2.3782768 0.769587 0.06774138]
[2.3782768 6.25 4.694947 1.0143608 ]
[0.769587 4.694947 6.25 2.9651313 ]
[0.06774138 1.0143608 2.9651313 6.25 ]]
它们与我的结果不匹配.有谁知道他们如何用不同的 index_points
计算最后一个矩阵?
They don't match with my result. Does anyone know how they compute the last matrix with different index_points
?
或者一般来说,我如何指定内核,以便它们可以拟合模型,例如additive Gaussian processes
,其中不同的index_points
对应不同的内核函数,所以我可以在 TensorFlow Probability 框架下拟合模型 y_i = f_1(x_{1,i}) + f_2(x_{2,i}) + ...
?
Or in general, how do I specify the kernel so that they can fit the model such as additive Gaussian processes
, where different index_points
correspond to different kernel functions, so that I can fit the model y_i = f_1(x_{1,i}) + f_2(x_{2,i}) + ...
under TensorFlow Probability framework?
推荐答案
你可以直接写k_sum = k1 + k2
!查看基类 PositiveSemidefineKernel,其中我们覆盖了加法和乘法运算符,您想了解它是如何工作的.
You can just write k_sum = k1 + k2
! Check out the base class PositiveSemidefiniteKernel, where we've overridden the addition and multiplication operators, of you want to see how it works.
这篇关于如何在 TensorFlow Probability 中创建不同内核对象的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!