{"metadata": {"language_info": {"mimetype": "text/x-python", "file_extension": ".py", "version": "3.6.3", "name": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "pygments_lexer": "ipython3"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}, "nbformat": 4, "cells": [{"cell_type": "code", "metadata": {"_uuid": "7d8b73f686fe43dcfc14b325f0d1b2b6b204612c", "_cell_guid": "57307c22-e185-4f35-98c2-92ef0bf4ae13"}, "source": ["import tensorflow as tf \n", "import numpy as np\n", "import pandas as pd"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"collapsed": true}, "source": ["def label_vector_to_one_hot_vector(vector, one_hot_size=10):\n", "    \"\"\"\n", "    Use to convert a column vector to a 'one-hot' matrix\n", "    \n", "    Example:\n", "        vector: [[2], [0], [1]]\n", "        one_hot_size: 3\n", "        returns:\n", "            [[ 0.,  0.,  1.],\n", "             [ 1.,  0.,  0.],\n", "             [ 0.,  1.,  0.]]\n", "    \n", "    Parameters:\n", "        vector (np.array): of size (n, 1) to be converted\n", "        one_hot_size (int) optional: size of 'one-hot' row vector\n", "        \n", "    Returns:\n", "        np.array size (vector.size, one_hot_size): converted to a 'one-hot' matrix\n", "    \"\"\"\n", "    squeezed_vector = np.squeeze(vector, axis=-1)\n", "\n", "    one_hot = np.zeros((squeezed_vector.size, one_hot_size))\n", "\n", "    one_hot[np.arange(squeezed_vector.size), squeezed_vector] = 1\n", "    \n", "    return one_hot"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"collapsed": true}, "source": ["class DataSet(object):\n", "    \n", "    def __init__(self, df, train_dev_slice):\n", "        labels = np.array(df['label'].values[train_dev_slice])\n", "        self._labels = labels.reshape(labels.shape[0], 1)\n", "        \n", "        self._images = df.drop(['label'], axis=1).values[train_dev_slice]\n", "        \n", "        self._current_batch = 0\n", "        \n", "    def next_batch(self, n):\n", "        start = n * self._current_batch\n", "        end = n * (self._current_batch + 1)\n", "        # increment for next mini-batch\n", "        self._current_batch += 1\n", "        return self.images[start:end], self.labels[start:end]\n", "    \n", "    @property\n", "    def labels(self):\n", "        return label_vector_to_one_hot_vector(self._labels)\n", "\n", "    @property\n", "    def images(self):\n", "        return self._images\n", "\n", "\n", "class Mnist(object):\n", "    \n", "    def __init__(self, df, train_dev_slice):\n", "        # df = pd.read_csv(csv_file_path)\n", "        \n", "        self._train = DataSet(df, train_dev_slice=slice(0, train_dev_slice))\n", "        size = df['label'].size # 42000\n", "        self._test = DataSet(df, train_dev_slice=slice(train_dev_slice, size))\n", "                    \n", "    @property\n", "    def train(self):\n", "        return self._train\n", "\n", "    @property\n", "    def test(self):\n", "        return self._test"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {}, "source": ["# Import data\n", "df = pd.read_csv('../input/train.csv')\n", "\n", "mnist = Mnist(df, 40000)\n", "\n", "# Create the model\n", "x = tf.placeholder(tf.float32, [None, 784])\n", "W = tf.Variable(tf.zeros([784, 10]))\n", "b = tf.Variable(tf.zeros([10]))\n", "y = tf.matmul(x, W) + b\n", "\n", "# Define loss and optimizer\n", "y_ = tf.placeholder(tf.float32, [None, 10])\n", "\n", "cross_entropy = tf.reduce_mean(\n", "      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n", "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n", "\n", "sess = tf.InteractiveSession()\n", "tf.global_variables_initializer().run()\n", "# Train\n", "for idx in range(1000):\n", "    batch_xs, batch_ys = mnist.train.next_batch(100)\n", "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n", "    if idx % 100 == 0:\n", "        # Test trained model\n", "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n", "\n", "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", "        print('Iteration: %s' % idx, sess.run(accuracy, feed_dict={x: mnist.test.images,\n", "                                          y_: mnist.test.labels}))"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"collapsed": true}, "source": ["test_df = pd.read_csv('../input/test.csv')\n", "test_data = test_df.values\n", "\n", "test_pred = sess.run(y, feed_dict={x: test_data})\n", "test_labels = np.argmax(test_pred, axis=1)"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {}, "source": ["submission = pd.DataFrame(data={'ImageId':(np.arange(test_labels.shape[0])+1), 'Label':test_labels})\n", "submission.to_csv('submission.csv', index=False)\n", "submission.tail()"], "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"collapsed": true}, "source": [], "execution_count": null, "outputs": []}], "nbformat_minor": 1}