{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\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\nfrom keras.utils import to_categorical\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntest_data = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a246255be86a97248168ff53464b21560687c84b"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4aa94728f96666966ddacb373ff298721dca0843"},"cell_type":"code","source":"test_len = len(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"387f356ad338ae7b67cca383e104971571c531a4"},"cell_type":"code","source":"train_x = train_data.drop(['label'], axis=1)\ntrain_y = to_categorical(train_data['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03fab2bf00630c9de25d3c414a025e8e6881c250"},"cell_type":"code","source":"train_y[1:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27aae3377bd25076e3008b3e2fa3c6d0e7f80b7c"},"cell_type":"code","source":"train_x = train_x/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4862dc25ed8624e409e6de0cf9fd56abcd7e7372"},"cell_type":"code","source":"test_data = test_data/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9753ba1c37d4c6e2e0407eb5fa9c798451633a9e"},"cell_type":"code","source":"total_len = len(train_x['pixel0'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"300a5a60e9a8653311fcad2479614c1e431798c9"},"cell_type":"code","source":"train_x.loc[total_len-5:total_len].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c47d630e6d24c33ab14980e366b21444a70b2085"},"cell_type":"code","source":"train_x.loc[total_len-5:total_len].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"feed8f5161e3ce29c6888ab80eb269df3cd1dda2"},"cell_type":"code","source":"print(total_len)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1d9ec03a20640f75d5fc92e41043ae3c7dce5f8"},"cell_type":"code","source":"# Training Parameters\nlearning_rate = 0.001\nnum_steps = 200\nbatch_size = 128\ndisplay_step = 10\n\n# Network Parameters\nnum_input = 784 # MNIST data input (img shape: 28*28)\nnum_classes = 10 # MNIST total classes (0-9 digits)\ndropout = 0.75 # Dropout, probability to keep units","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bb84189ec3a977957a9b5eceaeb4d919b63c296"},"cell_type":"code","source":"# tf Graph input\nX = tf.placeholder(tf.float32, [None, num_input])\nY = tf.placeholder(tf.float32, [None, num_classes])\nkeep_prob = tf.placeholder(tf.float32) # dropout (keep probability)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37963542714743ec66c6533ff3325c8eeec28618"},"cell_type":"code","source":"# Create some wrappers for simplicity\ndef conv2d(x, W, b, strides=1):\n    # Conv2D wrapper, with bias and relu activation\n    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')\n    x = tf.nn.bias_add(x, b)\n    return tf.nn.relu(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c390ba88574593442ac883c2fd5dafad88614ab8"},"cell_type":"code","source":"def maxpool2d(x, k=2):\n    # MaxPool2D wrapper\n    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],\n                          padding='SAME')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33932e0c4abc0e702dda376cb04bb710322fc5d2"},"cell_type":"code","source":"# Create model\ndef conv_net(x, weights, biases, dropout):\n    # MNIST data input is a 1-D vector of 784 features (28*28 pixels)\n    # Reshape to match picture format [Height x Width x Channel]\n    # Tensor input become 4-D: [Batch Size, Height, Width, Channel]\n    x = tf.reshape(x, shape=[-1, 28, 28, 1])\n\n    # Convolution Layer\n    conv1 = conv2d(x, weights['wc1'], biases['bc1'])\n    # Max Pooling (down-sampling)\n    conv1 = maxpool2d(conv1, k=2)\n\n    # Convolution Layer\n    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])\n    # Max Pooling (down-sampling)\n    conv2 = maxpool2d(conv2, k=2)\n\n    # Fully connected layer\n    # Reshape conv2 output to fit fully connected layer input\n    fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])\n    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])\n    fc1 = tf.nn.relu(fc1)\n    # Apply Dropout\n    fc1 = tf.nn.dropout(fc1, dropout)\n\n    # Output, class prediction\n    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])\n    return out","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"636c16ad51a8f72a55ce64a9a94f45c3747a6b6f"},"cell_type":"code","source":"# Store layers weight & bias\nweights = {\n    # 5x5 conv, 1 input, 32 outputs\n    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),\n    # 5x5 conv, 32 inputs, 64 outputs\n    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),\n    # fully connected, 7*7*64 inputs, 1024 outputs\n    'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),\n    # 1024 inputs, 10 outputs (class prediction)\n    'out': tf.Variable(tf.random_normal([1024, num_classes]))\n}\n\nbiases = {\n    'bc1': tf.Variable(tf.random_normal([32])),\n    'bc2': tf.Variable(tf.random_normal([64])),\n    'bd1': tf.Variable(tf.random_normal([1024])),\n    'out': tf.Variable(tf.random_normal([num_classes]))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"0be0d17bf27d3ee1dd05ed2ae88367daa7af182d"},"cell_type":"code","source":"# Construct model\nlogits = conv_net(X, weights, biases, keep_prob)\nprediction = tf.nn.softmax(logits)\n\n# Define loss and optimizer\nloss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(\n    logits=logits, labels=Y))\noptimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)\ntrain_op = optimizer.minimize(loss_op)\n\n\n# Evaluate model\ncorrect_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))\naccuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n\n# Initialize the variables (i.e. assign their default value)\ninit = tf.global_variables_initializer()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd76ca1d98fd3666d70f5d1d98dde6509c991b5d"},"cell_type":"code","source":"# Start training\nwith tf.Session() as sess:\n\n    # Run the initializer\n    sess.run(init)\n\n    for step in range(1, num_steps+1):\n        for i in range(0, int(total_len/batch_size), batch_size):\n            batch_x = train_x.loc[i:i+batch_size-1]\n            batch_y = train_y[i:i+batch_size]\n            sess.run(train_op, feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.8})\n        \n        if step % display_step == 0 or step == 1:\n            # Calculate batch loss and accuracy\n            loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,\n                                                                 Y: batch_y,\n                                                                 keep_prob: 1.0})\n            print(\"Step \" + str(step) + \", Minibatch Loss= \" + \\\n                  \"{:.4f}\".format(loss) + \", Training Accuracy= \" + \\\n                  \"{:.3f}\".format(acc))\n\n    print(\"Optimization Finished!\")\n\n    # Calculate accuracy for 256 MNIST test images\n    prediction_to_write = []\n    print(\"Prediction:\")\n    for image_id in range(test_len):\n        if image_id % 1000 ==0:\n            print('LOG: Prediced ', image_id, ' images.')\n        image = [test_data.loc[image_id]]\n        pred_y = sess.run(prediction, feed_dict={X: image,\n                                      keep_prob: 1.0})\n        prediction_to_write.append([image_id+1, int(pred_y[0][0])])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c782921f11d85b5e76b1f4e51a56a290fafb426"},"cell_type":"code","source":"import csv\nheading = ['ImageId' ,'Label']\nwith open('submit.csv', \"w\") as csv_file:\n        writer = csv.writer(csv_file, delimiter=',')\n        writer.writerow(heading)\n        for line in prediction_to_write:\n            writer.writerow(line)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ede4dd265dd44d13ce8dc44aad3e5404614345b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}