<s> import gzip <EOL> import os <EOL> import numpy as np <EOL> import six <EOL> from six . moves . urllib import request <EOL> parent = '<STR_LIT>' <EOL> train_images = '<STR_LIT>' <EOL> train_labels = '<STR_LIT>' <EOL> test_images = '<STR_LIT>' <EOL> test_labels = '<STR_LIT>' <EOL> num_train = <NUM_LIT> <EOL> num_test = <NUM_LIT> <EOL> dim = <NUM_LIT> <EOL> def load_mnist ( images , labels , num ) : <EOL> data = np . zeros ( num * dim , dtype = np . uint8 ) . reshape ( ( num , dim ) ) <EOL> target = np . zeros ( num , dtype = np . uint8 ) . reshape ( ( num , ) ) <EOL> with gzip . open ( images , '<STR_LIT:rb>' ) as f_images , gzip . open ( labels , '<STR_LIT:rb>' ) as f_labels : <EOL> f_images . read ( <NUM_LIT:16> ) <EOL> f_labels . read ( <NUM_LIT:8> ) <EOL> for i in six . moves . range ( num ) : <EOL> target [ i ] = ord ( f_labels . read ( <NUM_LIT:1> ) ) <EOL> for j in six . moves . range ( dim ) : <EOL> data [ i , j ] = ord ( f_images . read ( <NUM_LIT:1> ) ) <EOL> return data , target <EOL> def download_mnist_data ( ) : <EOL> print ( '<STR_LIT>' . format ( train_images ) ) <EOL> request . urlretrieve ( '<STR_LIT>' . format ( parent , train_images ) , train_images ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( train_labels ) ) <EOL> request . urlretrieve ( '<STR_LIT>' . format ( parent , train_labels ) , train_labels ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( test_images ) ) <EOL> request . urlretrieve ( '<STR_LIT>' . format ( parent , test_images ) , test_images ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( test_labels ) ) <EOL> request . urlretrieve ( '<STR_LIT>' . format ( parent , test_labels ) , test_labels ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> data_train , target_train = load_mnist ( train_images , train_labels , <EOL> num_train ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> data_test , target_test = load_mnist ( test_images , test_labels , num_test ) <EOL> mnist = { } <EOL> mnist [ '<STR_LIT:data>' ] = np . append ( data_train , data_test , axis = <NUM_LIT:0> ) <EOL> mnist [ '<STR_LIT:target>' ] = np . append ( target_train , target_test , axis = <NUM_LIT:0> ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as output : <EOL> six . moves . cPickle . dump ( mnist , output , - <NUM_LIT:1> ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> def load_mnist_data ( ) : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> download_mnist_data ( ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as mnist_pickle : <EOL> mnist = six . moves . cPickle . load ( mnist_pickle ) <EOL> return mnist </s>
