Skip to content

Instantly share code, notes, and snippets.

@brendano
Created December 24, 2008 20:11
Show Gist options
  • Save brendano/39760 to your computer and use it in GitHub Desktop.
Save brendano/39760 to your computer and use it in GitHub Desktop.
load the MNIST data set in R
# 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, ...)
}
@tom-christie
Copy link

This was helpful, thanks!

@xrobin
Copy link

xrobin commented Jun 21, 2014

Very useful. What is the license to re-use the code (for instance in an R package)? Would GPL work?

@brendano
Copy link
Author

I hereby license it as follows. This is the MIT license.

Copyright 2008, Brendan O'Connor

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@rmnppt
Copy link

rmnppt commented May 10, 2016

thank you that's very helpful!

@bob-carpenter
Copy link

Thanks! This saved me quite a bit of spelunking and coding time and the visualizer's really nice.

@chotota
Copy link

chotota commented Jan 25, 2017

hello !!!
well how can i use this in jupyter i mean how can i run this code ...i'm new a little bit on tenserflow and jupyter ... could u plz help me

@javadba
Copy link

javadba commented Feb 3, 2018

In RStudio the data gets loaded (tibble??) but then nothing gets shown. How can we display this?

show_digit <- function(arr784, col=gray(12:1/12), ...) {
image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}

@medovina
Copy link

medovina commented Feb 6, 2018

@javadba A comment at the top of the code says

# call:  show_digit(train$x[5,])   to see a digit.

Have you tried that? It works for me at the R command line (I'm not using RStudio).

@borislavib
Copy link

@medovina I've tried this in ESS, but when I run the whole file and after that I cannot execute "show_digit(train$x[5,])" in the command line,
It says " Error in matrix(arr784, nrow = 28) : object 'train' not found ", is there any catch in executing the file ? The paths are set.

@sohana08
Copy link

I am using jupyter notebook to run the above code. After calling show_digit function it is showing the following error

Error in matrix(arr784, nrow = 28): object 'train' not found

How do I solve for this

@WhiskersReneeWe
Copy link

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.

@gao157
Copy link

gao157 commented May 22, 2019

Very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment