comparison datasets/MNIST.py @ 504:19ab9ce916e3

slightly more sophisticated system for finding the mnist data
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 29 Oct 2008 11:38:49 -0400
parents 11e0357f06f4
children 74b3e65f5f24
comparison
equal deleted inserted replaced
503:c7ce66b4e8f4 504:19ab9ce916e3
5 5
6 import numpy 6 import numpy
7 7
8 from ..amat import AMat 8 from ..amat import AMat
9 9
10 default_path = '/u/bergstrj/pub/data/mnist.amat' 10 from .config import MNIST_amat
11 """the location of a file containing mnist data in .amat format"""
12
13 11
14 def head(n=10, path=None): 12 def head(n=10, path=None):
15 """Load the first MNIST examples. 13 """Load the first MNIST examples.
16 14
17 Returns two matrices: x, y. x has N rows of 784 columns. Each row of x represents the 15 Returns two matrices: x, y. x has N rows of 784 columns. Each row of x represents the
18 28x28 grey-scale pixels in raster order. y is a vector of N integers. Each element y[i] 16 28x28 grey-scale pixels in raster order. y is a vector of N integers. Each element y[i]
19 is the label of the i'th row of x. 17 is the label of the i'th row of x.
20 18
21 """ 19 """
22 path = path if path is not None else default_path 20 path = MNIST_amat if path is None else path
23 21
24 dat = AMat(path=path, head=n) 22 dat = AMat(path=path, head=n)
23
24 try:
25 assert dat.input.shape[0] == n
26 assert dat.target.shape[0] == n
27 except Exception , e:
28 raise Exception("failed to read MNIST data", (dat, e))
25 29
26 return dat.input, numpy.asarray(dat.target, dtype='int64').reshape(dat.target.shape[0]) 30 return dat.input, numpy.asarray(dat.target, dtype='int64').reshape(dat.target.shape[0])
27 31
28 def train_valid_test(ntrain=50000, nvalid=10000, ntest=10000, path=None): 32 def train_valid_test(ntrain=50000, nvalid=10000, ntest=10000, path=None):
29 all_x, all_targ = head(ntrain+nvalid+ntest, path=path) 33 all_x, all_targ = head(ntrain+nvalid+ntest, path=path)