import gzip
import os
import struct
from array import array
import random
_allowed_modes = (
# integer values in {0..255}
'vanilla',
# integer values in {0,1}
# values set at 1 (instead of 0) with probability p = orig/255
# as in Ruslan Salakhutdinov and Iain Murray's paper
# 'On The Quantitative Analysis of Deep Belief Network' (2008)
'randomly_binarized',
# integer values in {0,1}
# values set at 1 (instead of 0) if orig/255 > 0.5
'rounded_binarized',
)
_allowed_return_types = (
# default return type. Computationally more expensive.
# Useful if numpy is not installed.
'lists',
# Numpy module will be dynamically loaded on demand.
'numpy',
)
np = None
def _import_numpy():
# will be called only when the numpy return type has been specifically
# requested via the 'return_type' parameter in MNIST class' constructor.
global np
if np is None: # import only once
try:
import numpy as _np
except ImportError as e:
raise MNISTException(
"need to have numpy installed to return numpy arrays."\
+" Otherwise, please set return_type='lists' in constructor."
)
np = _np
else:
pass # was already previously imported
return np
class MNISTException(Exception):
pass
class MNIST(object):
def __init__(self, path='.', mode='vanilla', return_type='lists', gz=False):
self.path = path
assert mode in _allowed_modes, \
"selected mode '{}' not in {}".format(mode,_allowed_modes)
self._mode = mode
assert return_type in _allowed_return_types, \
"selected return_type '{}' not in {}".format(
return_type,
_allo