Search code examples
pythonarraystheanobracketssquare-bracket

Theano function not working on a simple, 4-value array


I was working through the theano documentation/tutorial, and the very first example was this:

>>> import numpy
>>> import theano.tensor as T
>>> from theano import function
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
>>> z = x + y
>>> f = function([x, y], z)

This seemed simple enough, and so I wrote my own program that expanded on it:

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

x = T.dscalar('x')
y = T.dscalar('y')

z = x + y
f = function([x, y], z)


print f(2, 3)
print np.allclose(f(16.3, 12.1), 28.4) 
print ""

r  = (2, 3), (2, 2), (2, 1), (2, 0)

for i in r:
    print i
    print f(i)

And for some reason, it won't iterate:

5.0
True

(2, 3)
Traceback (most recent call last):
  File "TheanoBase2.py", line 20, in <module>
    print f(i)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 786, in __call__
    allow_downcast=s.allow_downcast)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 177, in filter
    data.shape))
TypeError: ('Bad input argument to theano function with name "TheanoBase2.py:9"  at index 0(0-based)', 'Wrong number of dimensions: expected 0, got 1 with shape (2,).')

Why does print f(2, 3) work and print f(i) not work, when they're the exact same expression. I tried replacing/enclosing the brackets with square brackets, and the result was the same.


Solution

  • function f take two scalars as input and return their sum, each element of r i.e (x, y) is a tuple not a scalar. This should work:

    import numpy as np
    import theano.tensor as T
    from theano import function
    
    x = T.dscalar('x')
    y = T.dscalar('y')
    
    z = x + y
    f = function([x, y], z)
    
    print f(2, 3)
    print np.allclose(f(16.3, 12.1), 28.4) 
    print ""
    
    r  = (2, 3), (2, 2), (2, 1), (2, 0)
    
    for i in r:
        print i
        print f(i[0], i[1])