Search code examples
numpytheano

passing numpy array as parameter in theano function


As a beginner, i was trying to simply compute the dot product of two matrices using theano.

my code is very simple.

import theano
import theano.tensor as T
import numpy as np
from theano import function

def covarience(array):

    input_array=T.matrix('input_array')
    deviation_matrix = T.matrix('deviation_matrix')
    matrix_filled_with_1s=T.matrix('matrix_filled_with_1s')
    z = T.dot(input_array, matrix_filled_with_1s)

    identity=np.ones((len(array),len(array)))

    f=function([array,identity],z)
    # print(f)

covarience(np.array([[2,4],[6,8]]))

but the problem is each time i run this code , i get error message like "TypeError: Unknown parameter type: "

Can anyone tell me whats wrong with my code?


Solution

  • You cannot pass numpy array to theano function, theano functions can only be defined by theano.tensor variables. So you can always define computations with interaction of tensor/symbolic variables, and to perform actual computation on values/real data you can use functions, it doesn't make sense to define theano function itself with numpy array.

    This should work:

    import theano
    import theano.tensor as T
    import numpy as np
    
    a = T.matrix('a')
    b = T.matrix('b')
    z = T.dot(a, b)
    f = theano.function([a, b], z)
    a_d = np.asarray([[2, 4], [6, 8]], dtype=theano.config.floatX)
    b_d = np.ones(a_d.shape, dtype=theano.config.floatX)
    print(f(a_d, b_d))