{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b6dba7eb-6f6c-6bfb-1b28-54910623aa22"
      },
      "source": [
        "Trying to play with play with TensorFlow finding troubles to output csv (solved now :-)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7279a27a-c03a-1ffc-6cce-a406e0dfc5ad"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "# Any results you write to the current directory are saved as output.\n",
        "\n",
        "# import warnings\n",
        "# warnings.filterwarnings(\"ignore\")\n",
        "import csv\n",
        "\n",
        "import tflearn\n",
        "import tensorflow as tf\n",
        "from keras.utils.np_utils import to_categorical"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "432a315a-9546-2ffb-d64e-cec832b25a86"
      },
      "outputs": [],
      "source": [
        "df_trn = pd.read_csv(\"../input/train.csv\")\n",
        "df_tst = pd.read_csv(\"../input/test.csv\")\n",
        "\n",
        "x_trn = df_trn.ix[:,1:].values\n",
        "y_trn = df_trn.ix[:,0].values\n",
        "y_trn_cat = to_categorical(y_trn)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9b3d8101-4ec3-34fd-51d3-c3a1eb25aba9"
      },
      "outputs": [],
      "source": [
        "tf.reset_default_graph()\n",
        "\n",
        "net = tflearn.input_data([None, 784])\n",
        "\n",
        "net = tflearn.fully_connected(net, 200, activation='ReLU')\n",
        "net = tflearn.fully_connected(net, 100, activation='ReLU')\n",
        "net = tflearn.fully_connected(net,  50, activation='ReLU')\n",
        "\n",
        "net = tflearn.fully_connected(net, 10, activation='softmax')\n",
        "net = tflearn.regression(net, optimizer='sgd', learning_rate=0.07, loss='categorical_crossentropy')\n",
        "\n",
        "model = tflearn.DNN(net)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8742f3d-d409-efe2-c2c5-4103ee709d07"
      },
      "outputs": [],
      "source": [
        "model.fit(x_trn, y_trn_cat, validation_set=0, show_metric=True, batch_size=500, n_epoch=120)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "81998166-29a4-2b04-4fc9-0fd7b4f04591"
      },
      "outputs": [],
      "source": [
        "np.argmax(model.predict(df_tst),1)[0:100]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8af5bfeb-a111-0674-35e1-76170f83996e"
      },
      "outputs": [],
      "source": [
        "def prediction(predictions):\n",
        "    return np.argmax(predictions,1)\n",
        "\n",
        "predictions = prediction(model.predict(df_tst))\n",
        "submissions = pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n",
        "                         \"Label\": predictions})\n",
        "\n",
        "submissions.to_csv(\"tests.csv\", index=False, header=True)"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}