<s> from collections import OrderedDict <EOL> import theano , sys , numpy <EOL> import sys <EOL> import Mariana . candies as MCAN <EOL> import Mariana . settings as MSET <EOL> DEVICE_IS_GPU = ( theano . config . device . find ( "<STR_LIT>" ) > - <NUM_LIT:1> ) <EOL> class TheanoFunction ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , outputLayer , output_expressions , additional_input_expressions = { } , updates = [ ] , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . cast_warning_told = False <EOL> self . name = name <EOL> self . outputLayer = outputLayer <EOL> self . inputs = OrderedDict ( ) <EOL> inpSet = set ( ) <EOL> for inp in self . outputLayer . network . inputs . itervalues ( ) : <EOL> inpOut = inp . inputs <EOL> if inpOut not in inpSet : <EOL> self . inputs [ inp . name ] = inpOut <EOL> inpSet . add ( inpOut ) <EOL> for k , v in additional_input_expressions . iteritems ( ) : <EOL> if v not in inpSet : <EOL> self . inputs [ k ] = v <EOL> inpSet . add ( v ) <EOL> self . fctInputs = OrderedDict ( ) <EOL> for i in self . inputs : <EOL> self . fctInputs [ i ] = None <EOL> self . additional_input_expressions = additional_input_expressions <EOL> self . outputs = output_expressions <EOL> self . updates = updates <EOL> self . theano_fct = theano . function ( inputs = self . inputs . values ( ) , outputs = self . outputs , updates = self . updates , ** kwargs ) <EOL> warningMsg = False <EOL> if DEVICE_IS_GPU : <EOL> device = "<STR_LIT>" <EOL> msg = "<STR_LIT>" % ( device , name , outputLayer . name ) <EOL> if str ( self . getToposort ( ) ) . find ( "<STR_LIT>" ) > - <NUM_LIT:1> : <EOL> warningMsg = True <EOL> msg += "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> else : <EOL> device = "<STR_LIT>" <EOL> msg = "<STR_LIT>" % ( device , name , outputLayer . name ) <EOL> MCAN . friendly ( "<STR_LIT>" , msg , warning = warningMsg ) <EOL> def getToposort ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . theano_fct . maker . fgraph . toposort ( ) <EOL> def printGraph ( self ) : <EOL> """<STR_LIT>""" <EOL> theano . printing . debugprint ( self . theano_fct ) <EOL> def dumpToImage ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> theano . printing . pydotprint ( self . theano_fct , filename ) <EOL> def dumpToHTML ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> import theano . d3viz as d3v <EOL> d3v . d3viz ( self . theano_fct , filename ) <EOL> def run ( self , ** kwargs ) : <EOL> def _die ( fctName , outputLayer , kwargs , exc ) : <EOL> sys . stderr . write ( "<STR_LIT>" % ( fctName , outputLayer . name ) ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( kwargs ) ) <EOL> raise exc <EOL> self . fctInputs . update ( kwargs ) <EOL> return self . theano_fct ( * self . fctInputs . values ( ) ) <EOL> def __call__ ( self , ** kwargs ) : <EOL> return self . run ( ** kwargs ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . theano_fct ) </s>
