{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "98fa489f-4d0b-523b-89b1-af336d2669c9"
      },
      "source": [
        "Learning to use the H2O package in R to build a neural network, using code from https://www.kaggle.com/kobakhit/digit-recognizer/digital-recognizer-in-r/comments"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "541aff97-360a-b336-4161-62403c5c80cc"
      },
      "outputs": [],
      "source": [
        "library(readr)\n",
        "train <- read_csv(\"../input/train.csv\")\n",
        "test <- read_csv(\"../input/test.csv\")\n",
        "head(train)[1:10,1:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bcc83709-b47f-368d-819b-d8e5b1c629c0"
      },
      "outputs": [],
      "source": [
        "# Create a 28*28 matrix with pixel color values\n",
        "m <- matrix(unlist(train[1,-1]), nrow=28, byrow=T)\n",
        "# Plot that matrix\n",
        "image(m, col=grey.colors(255))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4a4433e-e01c-6237-1bd8-fc3cd3b02978"
      },
      "outputs": [],
      "source": [
        "rotate <- function(x) t(apply(x,2,rev)) # reverses (rotates the matrix)\n",
        "\n",
        "# Plot a bunch of images\n",
        "par(mfrow=c(2,3))\n",
        "lapply(1:6,\n",
        "      function(x) image(\n",
        "          rotate(matrix(unlist(train[x,-1]),nrow=28,byrow=T)),\n",
        "          col=grey.colors(255),\n",
        "          xlab=train[x,1]\n",
        "      )\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "362eeb7f-3dd8-6655-74ec-1a348a1da93c"
      },
      "outputs": [],
      "source": [
        "par(mfrow=c(1,1)) # set plot options back to default"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "69f0a004-66ad-84e6-f63e-1d17ea7f1f5b"
      },
      "outputs": [],
      "source": [
        "library(h2o)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9e3087ac-fa5d-8d38-7306-0e37a3259bb7"
      },
      "outputs": [],
      "source": [
        "## start a local h2o cluster\n",
        "localH2O = h2o.init(max_mem_size = '6g', nthreads = -1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "58c016e3-63cb-5fb1-dbce-4e46eda79df9"
      },
      "outputs": [],
      "source": [
        "## MNIST data as H2O\n",
        "train$label <- as.factor(train$label)\n",
        "train_h2o <- as.h2o(train)\n",
        "test_h2o <- as.h2o(test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "58f8b1e4-e6a5-346c-37bc-9854569ba0d6"
      },
      "outputs": [],
      "source": [
        "## set timer\n",
        "s <- proc.time()\n",
        "\n",
        "## train model\n",
        "model <- h2o.deeplearning(x=2:785, y=1, training_frame = train_h2o, activation = \"RectifierWithDropout\", \n",
        "                          input_dropout_ratio =0.2, hidden_dropout_ratios = c(0.5,0.5), balance_classes = T,\n",
        "                         hidden = c(100,100), momentum_stable = 0.99,\n",
        "                         nesterov_accelerated_gradient=T, epochs=15)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ef475eba-9742-f81f-c22a-936470b02acb"
      },
      "outputs": [],
      "source": [
        "## print confusion matrix\n",
        "h2o.confusionMatrix(model)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e1d7152c-20dc-e46d-bede-4f34e9d32157"
      },
      "outputs": [],
      "source": [
        "1-(414/9907)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0cc02f6d-7c54-c495-c003-f3de804ec64f"
      },
      "outputs": [],
      "source": [
        "s"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cf092078-c64a-7ec0-cdca-e85d5a4e63c8"
      },
      "outputs": [],
      "source": [
        "## classify test set\n",
        "h2o_y_test <- h2o.predict(model, test_h2o)\n",
        "\n",
        "## convert H2O format into data frame and  save as csv\n",
        "df_y_test <- as.data.frame(h2o_y_test)\n",
        "df_y_test <- data.frame(ImageId = seq(1,length(df_y_test$predict)), Label = df_y_test$predict)\n",
        "write.csv(df_y_test, file = \"digitRecognizerH2O.csv\", row.names=F)\n",
        "\n",
        "## shut down virutal H2O cluster\n",
        "h2o.shutdown(prompt=F)"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}