Frage

I want to calculate the sumproduct of two arrays in Theano. Both arrays are declared as shared variables and are the result of prior computations. Reading the tutorial, I found out how to use scan to compute what I want using 'normal' tensor arrays, but when I tried to adapt the code to shared arrays I got the error message TypeError: function() takes at least 1 argument (1 given). (See minimal running code example below)

Where is the mistake in my code? Where is my misconception? I am also open to a different approach for solving my problem.

Generally I would prefer a version which takes the shared variables directly, because in my understanding, converting the arrays first back to Numpy arrays and than again passing them to Theano, would be wasteful.

Error message producing sumproduct code using shared variables:

import theano
import theano.tensor as T
import numpy

a1 = [1,2,4]
a2 = [3,4,5]

Ta1_shared = theano.shared(numpy.array(a1))
Ta2_shared = theano.shared(numpy.array(a2))

outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))

Tsumprod_result, updates = theano.scan(fn=lambda Ta1_shared, Ta2_shared, prior_value: 
                                       prior_value + Ta1_shared * Ta2_shared,
                                       outputs_info=outputs_info,
                                       sequences=[Ta1_shared, Ta2_shared])
Tsumprod_result = Tsumprod_result[-1]

Tsumprod = theano.function(outputs=Tsumprod_result)
print Tsumprod()

Error message:

TypeError: function() takes at least 1 argument (1 given)

Working sumproduct code using non-shared variables:

import theano
import theano.tensor as T
import numpy

a1 = [1, 2, 4]
a2 = [3, 4, 5]   

Ta1 = theano.tensor.vector("a1")
Ta2 = theano.tensor.vector("coefficients")
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))

Tsumprod_result, updates = theano.scan(fn=lambda Ta1, Ta2, prior_value:
                                       prior_value + Ta1 * Ta2,
                                       outputs_info=outputs_info,
                                       sequences=[Ta1, Ta2])
Tsumprod_result = Tsumprod_result[-1]

Tsumprod = theano.function(inputs=[Ta1, Ta2], outputs=Tsumprod_result)
print Tsumprod(a1, a2)
War es hilfreich?

Lösung

You need to change the compilation line to this one:

Tsumprod = theano.function([], outputs=Tsumprod_result)

theano.function() always need a list of inputs. If the function take 0 input, like in this case, you need to give an empty list for the inputs.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top