#NEWLINE# def read_mnist_images( filename , dtype = None ) : #NEWLINE# #INDENT# "Read MNIST images from the original ubyte file format.\n\n    Parameters\n    ----------\n    filename : str\n        Filename/path from which to read images.\n\n    dtype : 'float32', 'float64', or 'bool'\n        If unspecified, images will be returned in their original\n        unsigned byte format.\n\n    Returns\n    -------\n    images : :class:`~numpy.ndarray`, shape (n_images, 1, n_rows, n_cols)\n        An image array, with individual examples indexed along the\n        first axis and the image dimensions along the second and\n        third axis.\n\n    Notes\n    -----\n    If the dtype provided was Boolean, the resulting array will\n    be Boolean with `True` if the corresponding pixel had a value\n    greater than or equal to 128, `False` otherwise.\n\n    If the dtype provided was a float dtype, the values will be mapped to\n    the unit interval [0, 1], with pixel values that were 255 in the\n    original unsigned byte representation equal to 1.0.\n\n    " #NEWLINE# with gzip . open ( filename , 'rb' ) as f : #NEWLINE# #INDENT# ( magic , number , rows , cols ) = struct . unpack ( '>iiii' , f . read ( 16 ) ) #NEWLINE# if ( magic != MNIST_IMAGE_MAGIC ) : #NEWLINE# #INDENT# raise ValueError ( 'Wrong magic number reading MNIST image file' ) #NEWLINE# #UNINDENT# array = numpy . frombuffer ( f . read ( ) , dtype = 'uint8' ) #NEWLINE# array = array . reshape ( ( number , 1 , rows , cols ) ) #NEWLINE# #UNINDENT# if dtype : #NEWLINE# #INDENT# dtype = numpy . dtype ( dtype ) #NEWLINE# if ( dtype . kind == 'b' ) : #NEWLINE# #INDENT# array = ( array >= 128 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( dtype . kind == 'f' ) : #NEWLINE# #INDENT# array = array . astype ( dtype ) #NEWLINE# array /= 255.0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'Unknown dtype to convert MNIST to' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return array