Created
December 24, 2008 20:11
-
-
Save brendano/39760 to your computer and use it in GitHub Desktop.
load the MNIST data set in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load the MNIST digit recognition dataset into R | |
# http://yann.lecun.com/exdb/mnist/ | |
# assume you have all 4 files and gunzip'd them | |
# creates train$n, train$x, train$y and test$n, test$x, test$y | |
# e.g. train$x is a 60000 x 784 matrix, each row is one digit (28x28) | |
# call: show_digit(train$x[5,]) to see a digit. | |
# brendan o'connor - gist.github.com/39760 - anyall.org | |
load_mnist <- function() { | |
load_image_file <- function(filename) { | |
ret = list() | |
f = file(filename,'rb') | |
readBin(f,'integer',n=1,size=4,endian='big') | |
ret$n = readBin(f,'integer',n=1,size=4,endian='big') | |
nrow = readBin(f,'integer',n=1,size=4,endian='big') | |
ncol = readBin(f,'integer',n=1,size=4,endian='big') | |
x = readBin(f,'integer',n=ret$n*nrow*ncol,size=1,signed=F) | |
ret$x = matrix(x, ncol=nrow*ncol, byrow=T) | |
close(f) | |
ret | |
} | |
load_label_file <- function(filename) { | |
f = file(filename,'rb') | |
readBin(f,'integer',n=1,size=4,endian='big') | |
n = readBin(f,'integer',n=1,size=4,endian='big') | |
y = readBin(f,'integer',n=n,size=1,signed=F) | |
close(f) | |
y | |
} | |
train <<- load_image_file('mnist/train-images-idx3-ubyte') | |
test <<- load_image_file('mnist/t10k-images-idx3-ubyte') | |
train$y <<- load_label_file('mnist/train-labels-idx1-ubyte') | |
test$y <<- load_label_file('mnist/t10k-labels-idx1-ubyte') | |
} | |
show_digit <- function(arr784, col=gray(12:1/12), ...) { | |
image(matrix(arr784, nrow=28)[,28:1], col=col, ...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works like a charm! Maybe the "catch" here is that you should change the file name "train-images.idx3-ubyte" to "train-images-idx3-ubyte" if you downloaded the data directly from source.