0

I have a theano covariance matrix, and i am trying to calculate element wise square of it. I have written following code for same:

    import theano
    a, b = theano.tensor.matrices('a', 'b')
    square = theano.function([a, b], a * b)
    sq = square(cov, cov)

where cov is covariance matrix, calculated as:

    y1_pre = T.dot(self.x, self.W_left) + self.b_left
    y1 = activation(y1_pre, self.hidden_activation)
    y2_pre = T.dot(self.x, self.W_right) + self.b_right
    y2 = activation(y2_pre, self.hidden_activation)
    y1_mean = T.mean(y1, axis=0)
    y1_centered = y1 - y1_mean
    y2_mean = T.mean(y2, axis=0)
    y2_centered = y2 - y2_mean
    cov = T.sum(y1_centered[:, :, None] * y2_centered[:, None, :], axis=0)

But it is throwing following error:

TypeError: ('Bad input argument to theano function with name "cov.py:114"  at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

I know it is trivial to do, but still could not find possible fix. Please help me in this regard.

Shweta
  • 1,111
  • 3
  • 15
  • 30
  • theano covariance matrix – Shweta Mar 19 '16 at 12:17
  • Have you tried using `sq = square(cov[:], cov[:])` ? I assme theano matrices behave like numpy arrays? – Jan Mar 19 '16 at 12:21
  • Do you calculate `cov` in another file or another function? – Jan Mar 19 '16 at 12:25
  • I have tried sq = square(cov[:], cov[:]), but still same error. cov is in same file. And I have updated the post with how cov is calculated. I feel that the error has something to do with how cov is calculated, but could not spot exact thing. – Shweta Mar 19 '16 at 12:27
  • Doesn't T.sum return a vector the way you do it? Try printing cov or the dimensions of cov after calculation. – Jan Mar 19 '16 at 12:30
  • no no, term inside sum is 3d, so sum will return 2d... – Shweta Mar 19 '16 at 12:32
  • Weirdly enough the term inside is 4d, becaus `None` is weird. Try printing `cov` – Jan Mar 19 '16 at 12:40

1 Answers1

1

Your input to the Theano function you compiled cannot be a symbolic expression, it has to be a NumPy array or a shared variable. For example:

A = T.matrix('input matrix')
B = T.matrix('dummy matrix')
C = np.random.rand(5,5).astype(theano.config.floatX)
squared = A**2 
get_squared = theano.function([A], squared)

If I run the following command:

get_squared(B)

I will get the following error:

TypeError: ('Bad input argument to theano function with name ":1" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

However if I run:

get_squared(C)

I get the squared matrix back.

I'm not sure about your codebase, how it's structured, but one very direct (possibly naive but it will work) solution is to create a symbolic expression for your squared covariance matrix and return that as part of your function. For example, if y1 and y2 are part of the graph to compute cov, you can create a theano function which returns the covariance squared:

cov = ... # (some expressions involving y1 and y2 as in your original post)
get_cov_squared = theano.function([y1,y2], cov**2)

But again, your input to the function must be actual arrays or shared variables, not symbolic expressions.

Indie AI
  • 601
  • 1
  • 6
  • 6
  • Thanks Indie for great explanation!!! But I am newbie to theano and it is still not wrking for me. I have updated my code to give some more idea about codebase – Shweta Mar 19 '16 at 17:23