{"cells":[{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"## Importing packages\n\n# This R environment comes with all of CRAN and many other helpful packages preinstalled.\n# You can see which packages are installed by checking out the kaggle/rstats docker image: \n# https://github.com/kaggle/docker-rstats\n\nlibrary(tidyverse) # metapackage with lots of helpful functions\n\n## Running code\n\n# In a notebook, you can run a single code cell by clicking in the cell and then hitting \n# the blue arrow to the left, or by clicking in the cell and pressing Shift+Enter. In a script, \n# you can run code by highlighting the code you want to run and then clicking the blue arrow\n# at the bottom of this window.\n\n## Reading in files\n\n# You can access files from datasets you've added to this kernel in the \"../input/\" directory.\n# You can see the files added to this kernel by running the code below. \n\nlist.files(path = \"../input\")\n\n## Saving data\n\n# If you save any files or images, these will be put in the \"output\" directory. You \n# can see the output directory by committing and running your kernel (using the \n# Commit & Run button) and then checking out the compiled version of your kernel.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nif (!require(\"pacman\")) install.packages(\"pacman\") \n\npacman::p_load(tidyverse, keras, tensorflow)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train <- data.matrix(read.csv(\"../input/digit-recognizer/train.csv\", header=T))\ntest <- data.matrix(read.csv(\"../input/digit-recognizer/test.csv\", header=T))\n\ntrain1 <- read.csv(\"../input/digit-recognizer/train.csv\", header=T)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Exploring data"},{"metadata":{"trusted":true},"cell_type":"code","source":"dim(train)\nbarplot(table(train[,1]))\nstr(train1)\nhead(train1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotTrain <- function(images){\n  op <- par(no.readonly=TRUE)\n  x <- ceiling(sqrt(length(images)))\n  par(mfrow=c(x, x), mar=c(.1, .1, .1, .1))\n  \n  for (i in images){ #reverse and transpose each matrix to rotate images\n    m <- matrix(train[i,-1], nrow=28, byrow=TRUE)\n    m <- apply(m, 2, rev)\n    image(t(m), col=grey.colors(255), axes=FALSE)\n    text(0.05, 0.2, col=\"white\", cex=1.2, train[i, 1])\n  }\n  par(op) #reset the original graphics parameters\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotTrain(1:36)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.x <- train[,-1] #remove 'label' column\ntrain.y <- train[,1] #label column \ntrain.x<-train[,-1] %>% normalize()\ntrain.y<- train.y %>% to_categorical\ndim(train.x)\ntest.x<-test %>% normalize()\n#dim(train.x)<-c(nrow(train.x),28,28,1)\n#dim(test)<-c(nrow(test),28,28,1)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model<-keras_model_sequential()\nmodel %>% \n  layer_dense(units = 32, input_shape = c(784)) %>% \n  layer_activation('relu') %>% \n  layer_dense(units = 10) %>% \n  layer_activation('softmax')\n  \n\n model%>%compile(\n\n  loss='categorical_crossentropy',\n\n  optimizer='adam',\n\n  metrics='accuracy'\n\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"summary(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndatagen <- image_data_generator(\n\n  featurewise_center = F,\n\n  samplewise_center=F,\n\n  featurewise_std_normalization = F,\n\n  samplewise_std_normalization=F,\n\n  zca_whitening=F,\n\n  horizontal_flip = F,\n\n  vertical_flip = F,\n\n  width_shift_range = 0.15,\n\n  height_shift_range = 0.15,\n\n  zoom_range = 0.15,\n\n  rotation_range = .15,\n\n  shear_range = 0.15\n\n)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#datagen %>% fit_image_data_generator(train.x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nhistory<-model %>%\n\n  fit (train.x, train.y, batch_size = 64,\n\n                steps_per_epoch = nrow(train.x)/64, epochs = 30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred<-model %>% predict_classes(test.x,batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ncnnsubmission<-data.frame(ImageId=1:nrow(test),Label=pred)\n\n\n\nwrite.csv(cnnsubmission, file=\"cnnsubmission.csv\", row.names=F)\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}