<s> import theano <EOL> import numpy as np <EOL> from os . path import join as pjoin <EOL> from smartlearner . interfaces import BatchScheduler <EOL> class MiniBatchScheduler ( BatchScheduler ) : <EOL> def __init__ ( self , dataset , batch_size ) : <EOL> super ( MiniBatchScheduler , self ) . __init__ ( dataset ) <EOL> self . _shared_batch_size = theano . shared ( np . array ( <NUM_LIT:0> , dtype = '<STR_LIT>' ) ) <EOL> self . batch_size = batch_size <EOL> self . shared_batch_count = theano . shared ( np . array ( <NUM_LIT:0> , dtype = '<STR_LIT>' ) ) <EOL> self . dataset . symb_inputs . tag . test_value = self . dataset . inputs . get_value ( ) [ : batch_size ] <EOL> if self . dataset . has_targets : <EOL> self . dataset . symb_targets . tag . test_value = self . dataset . targets . get_value ( ) [ : batch_size ] <EOL> self . shared_batch_count . tag . test_value = self . shared_batch_count . get_value ( ) <EOL> @ property <EOL> def batch_size ( self ) : <EOL> return self . _shared_batch_size . get_value ( ) <EOL> @ batch_size . setter <EOL> def batch_size ( self , value ) : <EOL> self . _shared_batch_size . set_value ( np . array ( value , dtype = '<STR_LIT>' ) ) <EOL> self . nb_updates_per_epoch = int ( np . ceil ( len ( self . dataset ) / self . batch_size ) ) <EOL> @ property <EOL> def updates ( self ) : <EOL> return { } <EOL> @ property <EOL> def givens ( self ) : <EOL> start = self . shared_batch_count * self . _shared_batch_size <EOL> end = ( self . shared_batch_count + <NUM_LIT:1> ) * self . _shared_batch_size <EOL> if self . dataset . targets is not None : <EOL> return { self . dataset . symb_inputs : self . dataset . inputs [ start : end ] , <EOL> self . dataset . symb_targets : self . dataset . targets [ start : end ] } <EOL> else : <EOL> return { self . dataset . symb_inputs : self . dataset . inputs [ start : end ] } <EOL> def __iter__ ( self ) : <EOL> for batch_count in range ( self . nb_updates_per_epoch ) : <EOL> self . shared_batch_count . set_value ( batch_count ) <EOL> yield batch_count + <NUM_LIT:1> <EOL> def save ( self , path = "<STR_LIT>" ) : <EOL> state = { "<STR_LIT:version>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : self . batch_size , <EOL> "<STR_LIT>" : self . shared_batch_count . get_value ( ) } <EOL> np . savez ( pjoin ( path , '<STR_LIT>' ) , ** state ) <EOL> def load ( self , path = "<STR_LIT>" ) : <EOL> state = np . load ( pjoin ( path , '<STR_LIT>' ) ) <EOL> self . batch_size = state [ "<STR_LIT>" ] <EOL> self . shared_batch_count . set_value ( state [ "<STR_LIT>" ] ) <EOL> class FullBatchScheduler ( MiniBatchScheduler ) : <EOL> def __init__ ( self , dataset ) : <EOL> super ( FullBatchScheduler , self ) . __init__ ( dataset , batch_size = len ( dataset ) ) </s>
