<s> from __future__ import print_function <EOL> import numpy as np <EOL> from sklearn . datasets import fetch_mldata <EOL> from deepx . nn import * <EOL> from deepx . loss import * <EOL> from deepx . optimize import * <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> mnist = fetch_mldata ( "<STR_LIT>" ) <EOL> X = mnist [ '<STR_LIT:data>' ] <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> X = X . reshape ( ( N , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> labels = mnist [ '<STR_LIT:target>' ] <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> idx = np . random . permutation ( np . arange ( <NUM_LIT> ) ) <EOL> X = X [ idx ] <EOL> labels = labels [ idx ] . astype ( np . int32 ) <EOL> y = np . zeros ( ( N , <NUM_LIT:10> ) ) <EOL> for i in range ( N ) : <EOL> y [ i , labels [ i ] ] = <NUM_LIT:1> <EOL> split = int ( <NUM_LIT> * N ) <EOL> train_idx , test_idx = idx [ : split ] , idx [ split : ] <EOL> Xtrain , Xtest = X [ train_idx ] , X [ test_idx ] <EOL> ytrain , ytest = y [ train_idx ] , y [ test_idx ] <EOL> conv_net = Image ( ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) ) >> Conv ( ( <NUM_LIT:10> , <NUM_LIT:2> , <NUM_LIT:2> ) , activation = Elu ) >> Conv ( ( <NUM_LIT:20> , <NUM_LIT:2> , <NUM_LIT:2> ) ) >> Flatten ( ) >> Softmax ( <NUM_LIT:10> ) <EOL> rmsprop = RMSProp ( CrossEntropy ( conv_net ) ) <EOL> def train ( n_iter , lr ) : <EOL> for i in range ( n_iter ) : <EOL> u = np . random . choice ( np . arange ( split ) ) <EOL> loss = rmsprop . train ( Xtrain [ u : u + <NUM_LIT:50> ] , ytrain [ u : u + <NUM_LIT:50> ] , lr ) <EOL> print ( "<STR_LIT>" , loss ) </s>
