Created
August 8, 2015 15:45
-
-
Save primaryobjects/05b6ba542bc1d434049e to your computer and use it in GitHub Desktop.
MNIST machine learning example, plotting a learning curve.
This file contains hidden or 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
library(caret) | |
library(doParallel) | |
library(reshape2) | |
library(ggplot2) | |
# Enable parallel processing. | |
cl <- makeCluster(detectCores()) | |
registerDoParallel(cl) | |
# 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('train-images-idx3-ubyte') | |
test <<- load_image_file('t10k-images-idx3-ubyte') | |
train$y <<- load_label_file('train-labels-idx1-ubyte') | |
test$y <<- load_label_file('t10k-labels-idx1-ubyte') | |
} | |
show_digit <- function(arr784, col=gray(12:1/12), ...) { | |
image(matrix(arr784, nrow=28)[,28:1], col=col, ...) | |
} | |
train <- data.frame() | |
test <- data.frame() | |
# Load data. | |
load_mnist() | |
# Normalize: X = (X - min) / (max - min) => X = (X - 0) / (255 - 0) => X = X / 255. | |
train$x <- train$x / 255 | |
# Setup training data with digit and pixel values with 60/40 split for train/cv. | |
inTrain = data.frame(y=train$y, train$x) | |
inTrain$y <- as.factor(inTrain$y) | |
trainIndex = createDataPartition(inTrain$y, p = 0.60,list=FALSE) | |
training = inTrain[trainIndex,] | |
cv = inTrain[-trainIndex,] | |
learningData <- data.frame() | |
for (n in seq(from=500, to=3000, by=500)) { | |
print(paste('Start', n)) | |
row <- data.frame(TrainingAccuracy = 0, CVAccuracy = 0, Iterations = n) | |
fit <- train(y ~ ., data = head(training, n), method = 'svmRadial', tuneGrid = data.frame(sigma=0.0107249, C=1)) | |
results1 <- predict(fit, newdata = head(training, n)) | |
conf <- confusionMatrix(results1, head(training$y, n)) | |
row$TrainingAccuracy <- conf$overall[[1]] | |
results2 <- predict(fit, newdata = head(cv, n)) | |
conf <- confusionMatrix(results2, head(cv$y, n)) | |
row$CVAccuracy <- conf$overall[[1]] | |
learningData <<- rbind(learningData, row) | |
print(paste('Done', n)) | |
# Plot learning curve. | |
m <- melt(learningData, id.vars='Iterations') | |
m <- cbind(m, Iterations=learningData$Iterations) | |
qplot(x=Iterations, y=value, colour=variable, data=m) + geom_line(size=1) + theme_bw() | |
} | |
# SVM. 95/94. | |
#fit <- train(y ~ ., data = head(training, 1000), method = 'svmRadial', tuneGrid = data.frame(sigma=0.0107249, C=1)) | |
#results <- predict(fit, newdata = head(cv, 1000)) | |
#confusionMatrix(results, head(cv$y, 1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment