Stumped: Gaussian Process w/ Polynomial Kernel - Incompatible Elemwise input shapes

Thoroughly confused - I have a dataset w/ n=564 observations, p=12 features, and a single target.

Applying a Linear kernel as such works fine (p=2 for simplicity and debugging at the moment):

features = FEATURES[0:2]

with pm.Model() as m5:
    eta_2 = pm.InverseGamma("eta2", 1, 1)
    c = pm.Normal("c", 0, 1, shape=2)
    cov_se = eta_2**2 * pm.gp.cov.Linear(len(features), c=c)
    gp = pm.gp.Marginal(cov_func=cov_se)

    # noise
    sigma = pm.InverseGamma("sigma", 1, 1)

    y_ = gp.marginal_likelihood("y", X=X[:,0:2], y=y, sigma=sigma)

with m5:
    mp = pm.find_MAP()

mp

However, instantiating a Polynomial kernel gives the following error: ValueError: Incompatible Elemwise input shapes [(564, 564), (1, 2)]

features = FEATURES[0:2]

with pm.Model() as m6:
    eta_2 = pm.InverseGamma("eta2", 1, 1)
    c = pm.Normal("c", 0, 1, shape=2)
    offset = pm.Normal("offset", 0, 1, shape=2)
    d = pm.Normal("d", 0, 1, shape=2)

    cov_se = eta_2**2 * pm.gp.cov.Polynomial(input_dim=len(features), c=c, offset=offset, d=d)

    gp = pm.gp.Marginal(cov_func=cov_se)

    # noise
    sigma = pm.InverseGamma("sigma", 1, 1)

    y_ = gp.marginal_likelihood("y", X=X[:,0:2], y=y, sigma=sigma)

PyMC version 5.6.0

Is features a DataFrame? If so, len(features) will return 564 and not 2, which would specify the wrong input dim.

1 Like

Chris - thanks for your comment. features is the list of features. But your comment helped me think a bit more about it -

Changing (removing the shape=2)

    c = pm.Normal("c", 0, 1, shape=2)
    offset = pm.Normal("offset", 0, 1, shape=2)
    d = pm.Normal("d", 0, 1, shape=2)

to

    c = pm.Normal("c", 0, 1, shape=2)
    offset = pm.Normal("offset", 0, 1)
    d = pm.Normal("d", 0, 1)

actually makes it work. I hadn’t necessarily understood what the Polynomial was expanding out, but it looks like one set of coefficients c at the power d, and a single offset or intercept term is what the Polynomial is actually doing. I think to do what I want I need to specify the d anyway so I’ve learned something - thank you!

2 Likes