【问题标题】:Partial derivatives of Gaussian Process wrt features高斯过程wrt特征的偏导数
【发布时间】:2020-12-24 10:18:26
【问题描述】:

给定一个具有多维特征和标量观察的高斯过程模型,我如何在 GPyTorch 或 GPFlow(或 scikit-learn)中计算输出到每个输入的导数?

【问题讨论】:

    标签: python scikit-learn gpflow gaussian-process gpytorch


    【解决方案1】:

    如果我正确理解了您的问题,以下内容应该可以为您提供 GPflow with TensorFlow 所需的内容:

    import numpy as np
    import tensorflow as tf
    import gpflow
    
    ### Set up toy data & model -- change as appropriate:
    X = np.linspace(0, 10, 5)[:, None]
    Y = np.random.randn(5, 1)
    data = (X, Y)
    kernel = gpflow.kernels.SquaredExponential()
    model = gpflow.models.GPR(data, kernel)
    Xtest = np.linspace(-1, 11, 7)[:, None]  # where you want to predict
    
    ### Compute gradient of prediction with respect to input:
    # TensorFlow can only compute gradients with respect to tensor objects,
    # so let's convert the inputs to a tensor:
    Xtest_tensor = tf.convert_to_tensor(Xtest)  
    
    with tf.GradientTape(
            persistent=True  # this allows us to compute different gradients below
    ) as tape:
        # By default, only Variables are watched. For gradients with respect to tensors,
        # we need to explicitly watch them:
        tape.watch(Xtest_tensor)
    
        mean, var = model.predict_f(Xtest_tensor)  # or any other predict function
    
    grad_mean = tape.gradient(mean, Xtest_tensor)
    grad_var = tape.gradient(var, Xtest_tensor)
    

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2019-05-18
      • 2019-02-25
      • 2017-06-14
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多