{
  "metadata": {
    "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,
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b311565c-2acb-3ca2-769a-b07e2069bff7",
        "_active": false
      },
      "source": "https://www.tensorflow.org/get_started/mnist/beginners",
      "outputs": [],
      "execution_count": null,
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "188c7a99-9a4b-1a92-44b4-7370f0907710",
        "_active": false
      },
      "outputs": [],
      "source": "import tensorflow as tf\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nplt.rcParams['image.cmap'] = 'Greys'\nplt.rcParams['image.interpolation'] = 'nearest'",
      "execution_state": "idle"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "649e45f1-dd12-b9f9-3243-6ccd7d658dfd",
        "_active": false
      },
      "source": "# Data input",
      "outputs": [],
      "execution_count": null,
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9d911a07-792f-f766-58c6-86e144df2ea1",
        "_active": false
      },
      "outputs": [],
      "source": "IMAGE_WIDTH = IMAGE_HEIGHT = 28\nIMAGE_SIZE = IMAGE_WIDTH*IMAGE_HEIGHT\nLABELS = 10",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "62b07768-d6f2-30d5-1c7c-2ea7d25c66ff",
        "_active": false
      },
      "outputs": [],
      "source": "data_train = pd.read_csv('../input/train.csv')",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "39790e3f-23db-2497-dddd-d29548cb72ad",
        "_active": false
      },
      "outputs": [],
      "source": "labels = np.array(data_train.pop('label')) # Remove the labels as a numpy array from the dataframe\nlabels = np.array([np.arange(LABELS) == label for label in labels])\ndata = np.array(data_train, dtype=np.float32)/255.0",
      "execution_state": "idle"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "1450bef9-7154-21dc-2cd9-ce5c5adb0e64",
        "_active": false
      },
      "source": "¿Están las etiquetas ordenadas de forma aleatoria?",
      "outputs": [],
      "execution_count": null,
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cfea71f6-a17f-c0c1-966e-d47dc29465b1",
        "_active": false
      },
      "outputs": [],
      "source": "plt.plot(np.argmax(labels[0:200], axis=1))",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "893f415c-0a69-01a3-70a9-5624dd15c388",
        "_active": false
      },
      "outputs": [],
      "source": "def showImage(image_data):\n    plt.imshow(image_data.reshape(IMAGE_WIDTH, IMAGE_HEIGHT))\n    plt.axis('off')\n    plt.show()\n    \nfor i in range(1,4):\n    showImage(data[[i]])",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1f52a81d-df2a-8f16-e27f-7cc4f8d161e2",
        "_active": true
      },
      "outputs": [],
      "source": "x = tf.placeholder(tf.float32, [None, IMAGE_SIZE])\nW = tf.Variable(tf.random_uniform([IMAGE_SIZE, LABELS]))\nb = tf.Variable(tf.random_uniform([LABELS]))\n\n# \ny_ = tf.placeholder(tf.float32, [None, 10])\n\n# Prediction\ny = tf.nn.softmax(tf.matmul(x, W) + b)\n\n# Evaluate model\n#cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\ncross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))\n\n#train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\ntrain_step = tf.train.AdamOptimizer().minimize(cross_entropy)",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a1248d2d-1614-fdbf-068a-e95dde800ec5",
        "_active": false
      },
      "outputs": [],
      "source": "sess = tf.InteractiveSession()",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e46de91c-b5ab-a3f1-7d2b-1b0f3190a7cd",
        "_active": false
      },
      "outputs": [],
      "source": "sess.run(tf.global_variables_initializer())\nevolution = []\nfor i in range(10000):\n    row_selection = np.random.permutation(40000)[0:100]\n    train_step.run(feed_dict={x: data[row_selection], y_: labels[row_selection]})\n    evolution += [cross_entropy.eval(feed_dict={x: data[row_selection], y_: labels[row_selection]})]\n    \nplt.plot(evolution)",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4e833d6-fff2-08fc-8dd4-48ec09e066db",
        "_active": false
      },
      "outputs": [],
      "source": "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\nprint(accuracy.eval(feed_dict={x: data[40000:], y_: labels[40000:]}))",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "35eae72a-c9bd-e8c5-614a-f5e7ac9a635b",
        "_active": false
      },
      "outputs": [],
      "source": "predictions = tf.argmax(y,1).eval(session=sess, feed_dict={x:data[100:200]})\nfor i in range(3):\n    showImage(data[100+i])\n    print( \"Prediction of img #{0} is {1}\".format(100+i, predictions[i]))",
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d28fb7f0-4fba-ceea-1940-cdf1fdea85ab",
        "_active": false,
        "collapsed": false
      },
      "outputs": [],
      "source": "def weight_variable(shape):\n    initial = tf.truncated_normal(shape, stddev=0.1)\n    return tf.Variable(initial)\n\nx_image = tf.reshape(x, [-1,28,28,1])\nconv = tf.nn.conv2d(x_image, weight_variable([5, 5, 1, 32]), strides=[1, 1, 1, 1], padding='SAME')\nIn [12]:\nconv = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\nconv.eval(feed_dict={x:data[0]})",
      "execution_state": "idle"
    }
  ]
}