<s> import numpy <EOL> import Mariana . activations as MA <EOL> import Mariana . initializations as MI <EOL> import Mariana . decorators as MD <EOL> import Mariana . layers as ML <EOL> import Mariana . costs as MC <EOL> import Mariana . regularizations as MR <EOL> import Mariana . scenari as MS <EOL> from Mariana . examples . useful import load_mnist <EOL> """<STR_LIT>""" <EOL> def Perceptron ( ls , cost ) : <EOL> i = ML . Input ( <NUM_LIT> * <NUM_LIT> , name = '<STR_LIT>' ) <EOL> o = ML . SoftmaxClassifier ( <NUM_LIT:10> , learningScenario = ls , costObject = cost , name = "<STR_LIT>" , regularizations = [ MR . L1 ( <NUM_LIT:0> ) , MR . L2 ( <NUM_LIT:0> ) ] ) <EOL> return i > o <EOL> def MLP ( ls , cost ) : <EOL> i = ML . Input ( <NUM_LIT> * <NUM_LIT> , name = '<STR_LIT>' ) <EOL> h = ML . Hidden ( <NUM_LIT> , activation = MA . Tanh ( ) , decorators = [ MD . BinomialDropout ( <NUM_LIT> ) ] , initializations = [ MI . GlorotTanhInit ( ) ] , regularizations = [ MR . L1 ( <NUM_LIT:0> ) , MR . L2 ( <NUM_LIT> ) ] , name = "<STR_LIT>" ) <EOL> o = ML . SoftmaxClassifier ( <NUM_LIT:10> , initializations = [ MI . ZerosWeights ( ) ] , learningScenario = ls , costObject = cost , name = "<STR_LIT>" , regularizations = [ MR . L1 ( <NUM_LIT:0> ) , MR . L2 ( <NUM_LIT> ) ] ) <EOL> mlp = i > h > o <EOL> return mlp <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ls = MS . GradientDescent ( lr = <NUM_LIT> ) <EOL> cost = MC . NegativeLogLikelihood ( ) <EOL> train_set , validation_set , validation_set = load_mnist ( ) <EOL> maxEpochs = <NUM_LIT:1000> <EOL> miniBatchSize = <NUM_LIT:20> <EOL> model = MLP ( ls , cost ) <EOL> model . printLog ( ) <EOL> o = model . outputs . values ( ) [ <NUM_LIT:0> ] <EOL> epoch = <NUM_LIT:0> <EOL> bestValScore = numpy . inf <EOL> while True : <EOL> trainScores = [ ] <EOL> for i in xrange ( <NUM_LIT:0> , len ( train_set [ <NUM_LIT:0> ] ) , miniBatchSize ) : <EOL> res = model . train ( o , inp = train_set [ <NUM_LIT:0> ] [ i : i + miniBatchSize ] , targets = train_set [ <NUM_LIT:1> ] [ i : i + miniBatchSize ] ) <EOL> trainScores . append ( res [ <NUM_LIT:0> ] ) <EOL> trainScore = numpy . mean ( trainScores ) <EOL> res = model . test ( o , inp = validation_set [ <NUM_LIT:0> ] , targets = validation_set [ <NUM_LIT:1> ] ) <EOL> print "<STR_LIT>" , epoch <EOL> print "<STR_LIT>" , trainScore <EOL> if bestValScore > res [ <NUM_LIT:0> ] : <EOL> bestValScore = res [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" , res [ <NUM_LIT:0> ] , "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" , res [ <NUM_LIT:0> ] , "<STR_LIT>" , bestValScore <EOL> epoch += <NUM_LIT:1> </s>
