Sympy to theano printing for complex expressions.

34 views
Skip to first unread message

Alexander Loschilov

unread,
Oct 2, 2015, 6:15:57 AM10/2/15
to sympy
Hello everybody,

Could somebody give me a hint how to introduce new operator mapping for sympy to theano code generation?

What are limitations of this mapping? For example, «We could map only subclass of expression to subclass of something else.»

I have got the following simplified test snippet:

from sympy import MatrixSymbol, Matrix, pprint, HadamardProduct
from sympy.abc import x, y


M
= 3
A
= 4


Nu = MatrixSymbol('Nu', M, A)
p
= MatrixSymbol('p', M, 2)


G
= Matrix([[1, 0, 0, 1, 1, 0],
           
[0, 0, 0, 1, 0, 0],
           
[0, 0, 1, 0, 1, 0],
           
[0, 0, 0, 0, 0, 1]])


fs
= [x*1+10,
      x
*2+10,
      x
*3+10,
      x
*4+10,
      x
*5+10,
      x
*6+10]


F
= Matrix(M, len(fs), lambda i, j: fs[j].subs({x: x-p[i, 0], y: y-p[i, 1]}))


P
= Matrix([[Matrix(1, M, lambda i, j: 0 if j == k else 1)*(HadamardProduct(Nu*G, F))
     
for k in xrange(0, M)]])


pprint
(P, num_columns=500)


inputs
= [x, y, p]
outputs
= [P]
dtypes
= {inp: 'float64' for inp in inputs}


from sympy.printing.theanocode import theano_function
f
= theano_function(inputs, outputs)




Unfortunately, an attempt of code generation failed, since there is no mapping exists for MatrixElement.

  File "/Users/aloschil/workspace/sympy/sympy/printing/theanocode.py", line 102, in _print_Basic
    op
= mapping[type(expr)]
KeyError: <class 'sympy.matrices.expressions.matexpr.MatrixElement'>


I have made an assumption that I should add the following mapping:
MatrixElement -> Subtensor{int64, int64}.

I am missing something. I have got no clear vision of underlying ideas, that is why it is a little bit tricky for me to go ahead.

I did not dive into details and tried the following:

sympy.matrices.expressions.matexpr.MatrixElement: tt.var.TensorVariable



Unfortunately, this straightforward approach failed.

I would like to read documentation that describes core ideas, which lies behind operation building either in SymPy and Theano.

best regards,
Alexander.
Reply all
Reply to author
Forward
0 new messages