pythonnumpytheanotheano-cuda

Element wise matrix multiplication in Theano


I could see element wise matrix multiplication using numpy can be done with * operator.

print np.mat(np.ones((10,10)))*np.mat(np.ones((10,10)))

But couldnt get it working under theano. The code I tried is

x = T.dmatrix('x')
y = T.dmatrix('y')
z = x * y
f1 = theano.function([x, y], z)

print f1(np.mat(np.ones((10,10))),np.mat(np.ones((10,10))))

Solution

  • If I try the following (which is basically your code):

    import theano
    import theano.tensor as T
    
    import numpy as np
    
    x = T.dmatrix('x')
    y = T.dmatrix('y')
    z = x * y
    f1 = theano.function([x, y], z)
    
    print f1(np.mat(np.ones((10,10))),np.mat(np.ones((10,10))))
    

    I get the following:

    [[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]
    

    So, it works for me.