<s> import numpy as np <EOL> import cudarray as ca <EOL> from . . base import Model , CollectionMixin , PickleMixin <EOL> from . . feed import Feed <EOL> from . . import expr as ex <EOL> class FeedForwardNet ( Model , CollectionMixin , PickleMixin ) : <EOL> _pickle_ignore = [ '<STR_LIT>' ] <EOL> def __init__ ( self , expression , loss ) : <EOL> self . expression = expression <EOL> self . loss = loss <EOL> self . graph = None <EOL> self . collection = [ expression ] <EOL> def setup ( self , x_shape , y_shape = None ) : <EOL> self . x_src = ex . Source ( x_shape ) <EOL> y_expr = self . _fprop_expr ( self . x_src ) <EOL> if y_shape is not None : <EOL> self . y_src = ex . Source ( y_shape ) <EOL> y_expr = self . loss ( y_expr , self . y_src ) <EOL> y_expr . grad_array = ca . array ( <NUM_LIT:1.0> ) <EOL> self . graph = ex . graph . ExprGraph ( y_expr ) <EOL> self . graph . setup ( ) <EOL> def _fprop_expr ( self , x ) : <EOL> return self . expression ( x ) <EOL> def update ( self , x , y ) : <EOL> self . x_src . array = x <EOL> self . y_src . array = y <EOL> self . graph . fprop ( ) <EOL> self . graph . bprop ( ) <EOL> return self . loss . array <EOL> def _batchwise ( self , feed , expr_fun ) : <EOL> feed = Feed . from_any ( feed ) <EOL> src = ex . Source ( feed . x_shape ) <EOL> sink = expr_fun ( src ) <EOL> graph = ex . graph . ExprGraph ( sink ) <EOL> graph . setup ( ) <EOL> y = [ ] <EOL> for x , in feed . batches ( ) : <EOL> src . array = x <EOL> graph . fprop ( ) <EOL> y . append ( np . array ( sink . array ) ) <EOL> y = np . concatenate ( y ) [ : feed . n_samples ] <EOL> return y <EOL> def predict ( self , feed ) : <EOL> """<STR_LIT>""" <EOL> return self . _batchwise ( feed , self . _fprop_expr ) <EOL> class ClassifierNet ( FeedForwardNet ) : <EOL> def _fprop_expr ( self , x ) : <EOL> y_expr = super ( ClassifierNet , self ) . _fprop_expr ( x ) <EOL> if isinstance ( self . loss , ex . nnet . SoftmaxCrossEntropy ) and not isinstance ( y_expr , ex . nnet . Softmax ) : <EOL> y_expr = ex . nnet . Softmax ( ) ( y_expr ) <EOL> return y_expr <EOL> def _predict_expr ( self , x ) : <EOL> y_expr = self . _fprop_expr ( x ) <EOL> y_expr = ex . nnet . one_hot . OneHotDecode ( ) ( y_expr ) <EOL> return y_expr <EOL> def predict ( self , feed ) : <EOL> """<STR_LIT>""" <EOL> return self . _batchwise ( feed , self . _predict_expr ) <EOL> def predict_proba ( self , feed ) : <EOL> """<STR_LIT>""" <EOL> return self . _batchwise ( feed , self . _fprop_expr ) <EOL> class RegressorNet ( FeedForwardNet ) : <EOL> def predict ( self , feed ) : <EOL> """<STR_LIT>""" <EOL> return self . _batchwise ( feed , self . _fprop_expr ) </s>
