ScaledCov: Implementation issue of simple scaled cov function

Hello there (again :slight_smile:),
I am trying to implement the following scaled covariance:

\sigma(x)k(x,x')\sigma(x'),

where \sigma(x) takes a different value (parameter value) based on value of x. Something very simple as:

def scale_function(x,s1,s2):
    if x == 0:
        return theano.shared(s1)
    elif x == 1:
        return theano.shared(s2)
s1 = 1
s2 = 2

x = np.array([0,0,1,1])[:,None]
cov_const = pm.gp.cov.Constant(1)
cov_scaled = pm.gp.cov.ScaledCov(1, args= (s1,s2), scaling_func=scale_function, cov_func = cov_const)
cov_scaled(x).eval()

However, this fails due to:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-223-2fafcd88808c> in <module>()
----> 1 cov_scaled(x).eval()

~\Anaconda3\lib\site-packages\pymc3\gp\cov.py in __call__(self, X, Xs, diag)
     59             return self.diag(X)
     60         else:
---> 61             return self.full(X, Xs)
     62 
     63     def diag(self, X):

~\Anaconda3\lib\site-packages\pymc3\gp\cov.py in full(self, X, Xs)
    587         scf_x = self.scaling_func(X, self.args)
    588         if Xs is None:
--> 589             return tt.outer(scf_x, scf_x) * self.cov_func(X)
    590         else:
    591             scf_xs = self.scaling_func(Xs, self.args)

~\Anaconda3\lib\site-packages\theano\tensor\basic.py in outer(x, y)
   6323 
   6324     """
-> 6325     if x.ndim != 1:
   6326         x = x.flatten()
   6327     if y.ndim != 1:

AttributeError: 'NoneType' object has no attribute 'ndim'

I am not sure that I exactly understand what the scaling function exactly suppose to look like. Is there any way to implement such scaling function for ScaledCov?

That error message is pretty inscrutable… I would guess that maybe its the pure python if/else that is causing problems? You could try writing the scaling function using theano conditionals.

1 Like

Using theano.tensor.switch did the trick, thanks!

1 Like