<s> __author__ = '<STR_LIT>' <EOL> from scipy import append , zeros <EOL> from pybrain . utilities import abstractMethod , Named <EOL> class Module ( Named ) : <EOL> """<STR_LIT>""" <EOL> sequential = False <EOL> paramdim = <NUM_LIT:0> <EOL> offset = <NUM_LIT:0> <EOL> bufferlist = None <EOL> def __init__ ( self , indim , outdim , name = None , ** args ) : <EOL> """<STR_LIT>""" <EOL> self . setArgs ( name = name , ** args ) <EOL> self . bufferlist = [ ] if not self . bufferlist else self . bufferlist <EOL> self . bufferlist += [ ( '<STR_LIT>' , indim ) , <EOL> ( '<STR_LIT>' , indim ) , <EOL> ( '<STR_LIT>' , outdim ) , <EOL> ( '<STR_LIT>' , outdim ) , ] <EOL> self . indim = indim <EOL> self . outdim = outdim <EOL> self . _resetBuffers ( ) <EOL> def _resetBuffers ( self , length = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> for buffername , dim in self . bufferlist : <EOL> setattr ( self , buffername , zeros ( ( length , dim ) ) ) <EOL> if length == <NUM_LIT:1> : <EOL> self . offset = <NUM_LIT:0> <EOL> def _growBuffers ( self ) : <EOL> """<STR_LIT>""" <EOL> currentlength = getattr ( self , self . bufferlist [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) . shape [ <NUM_LIT:0> ] <EOL> tmp = [ getattr ( self , n ) for n , _ in self . bufferlist ] <EOL> Module . _resetBuffers ( self , currentlength * <NUM_LIT:2> ) <EOL> for previous , ( buffername , _dim ) in zip ( tmp , self . bufferlist ) : <EOL> buffer_ = getattr ( self , buffername ) <EOL> buffer_ [ : currentlength ] = previous <EOL> def forward ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _forwardImplementation ( self . inputbuffer [ self . offset ] , <EOL> self . outputbuffer [ self . offset ] ) <EOL> def backward ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _backwardImplementation ( self . outputerror [ self . offset ] , <EOL> self . inputerror [ self . offset ] , <EOL> self . outputbuffer [ self . offset ] , <EOL> self . inputbuffer [ self . offset ] ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . offset = <NUM_LIT:0> <EOL> for buffername , l in self . bufferlist : <EOL> buf = getattr ( self , buffername ) <EOL> buf [ : ] = zeros ( l ) <EOL> def shift ( self , items ) : <EOL> """<STR_LIT>""" <EOL> if items == <NUM_LIT:0> : <EOL> return <EOL> self . offset += items <EOL> for buffername , _ in self . bufferlist : <EOL> buf = getattr ( self , buffername ) <EOL> assert abs ( items ) <= len ( buf ) , "<STR_LIT>" <EOL> fill = zeros ( ( abs ( items ) , len ( buf [ <NUM_LIT:0> ] ) ) ) <EOL> if items < <NUM_LIT:0> : <EOL> buf [ : ] = append ( buf [ - items : ] , fill , <NUM_LIT:0> ) <EOL> else : <EOL> buf [ : ] = append ( fill , buf [ <NUM_LIT:0> : - items ] , <NUM_LIT:0> ) <EOL> def activateOnDataset ( self , dataset ) : <EOL> """<STR_LIT>""" <EOL> dataset . reset ( ) <EOL> self . reset ( ) <EOL> out = zeros ( ( len ( dataset ) , self . outdim ) ) <EOL> for i , sample in enumerate ( dataset ) : <EOL> out [ i , : ] = self . activate ( sample [ <NUM_LIT:0> ] ) <EOL> self . reset ( ) <EOL> dataset . reset ( ) <EOL> return out <EOL> def activate ( self , inpt ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . inputbuffer [ self . offset ] ) == len ( inpt ) , str ( ( len ( self . inputbuffer [ self . offset ] ) , len ( inpt ) ) ) <EOL> self . inputbuffer [ self . offset ] = inpt <EOL> self . forward ( ) <EOL> return self . outputbuffer [ self . offset ] . copy ( ) <EOL> def backActivate ( self , outerr ) : <EOL> """<STR_LIT>""" <EOL> self . outputerror [ self . offset ] = outerr <EOL> self . backward ( ) <EOL> return self . inputerror [ self . offset ] . copy ( ) <EOL> def _forwardImplementation ( self , inbuf , outbuf ) : <EOL> """<STR_LIT>""" <EOL> abstractMethod ( ) <EOL> def _backwardImplementation ( self , outerr , inerr , outbuf , inbuf ) : <EOL> """<STR_LIT>""" </s>
