{"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)\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\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":{"trusted":true,"_uuid":"161f7189f4d6ea14be2f3aa9d358badb48fe390f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#Import Library\nimport numpy as np \nimport pandas as pd \nimport os\nimport tensorflow as tf\nfrom tensorflow.examples.tutorials.mnist import input_data\n# mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=False)\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5855685b7871680a5f1708710d3f8bcb18e0e85"},"cell_type":"code","source":"path_train = '../input/train.csv'\npath_test = '../input/test.csv'\n#Read Data\ntrain = pd.read_csv(path_train)\ntest = pd.read_csv(path_test)\n# Convert to numpy\ntrain = np.asarray(train, np.float32)\ntest = np.asarray(test, np.float32)\n\nX_train = train[:, 1:]\ny_train = np.reshape(train[:, :1], (train.shape[0], ))\ndel train\ndel path_train\ndel path_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dce36bad576a096bd7232575b0647287f8bc75e0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0e7f8df5a557e15036b4b293724d5888e7c3de9"},"cell_type":"code","source":"learning_rate = 0.001\nbatch_size =128\nnum_steps = 2000\nnum_input = 784\nnum_classes = 10\ndropout = 0.25\n# Create Model\ndef conv_net(X, n_classes, dropout):\n    x = tf.reshape(X, shape=[-1, 28, 28, 1])\n    conv1_1 = tf.layers.conv2d(x, 16, 5, activation=tf.nn.relu, padding='SAME')\n    conv1_2 = tf.layers.conv2d(x, 16, 3, activation=tf.nn.relu, padding='SAME')\n    conv1 = tf.concat([conv1_1, conv1_2], axis=3)\n    conv1_3 = tf.layers.max_pooling2d(conv1, 2, 2)\n    conv1_4 = tf.layers.conv2d(conv1, 32, 3, activation=tf.nn.relu, strides=(2, 2), padding='SAME')\n    conv1 = tf.concat([conv1_3, conv1_4], axis=3)\n    \n    conv2_1 = tf.layers.conv2d(conv1, 64, 1, activation=tf.nn.relu, padding='SAME')\n    conv2_2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu, padding='SAME')\n    conv2 = tf.concat([conv2_1, conv2_2], axis=3)\n    conv2_3 = tf.layers.max_pooling2d(conv2, 2, 2)\n    conv2_4 = tf.layers.conv2d(conv1, 128, 3, activation=tf.nn.relu, strides=(2, 2), padding='SAME')\n    conv2 = tf.concat([conv2_3, conv2_4], axis=3)\n    fc1 = tf.contrib.layers.flatten(conv2)\n    fc1 =tf.layers.dense(fc1, 9000)\n    fc1 = tf.layers.dropout(fc1, rate=dropout)\n    fc2 = tf.layers.dense(fc1, 1024)\n    out = tf.layers.dense(fc2, n_classes)\n    return out\ndef model_fn(features, labels, mode):\n  \n  logits = conv_net(features, num_classes, dropout)\n  \n  # Get prediction of model output\n  predictions = {\n      'classes': tf.argmax(logits, axis=1),\n      'probabilities': tf.nn.softmax(logits, name='softmax_tensor')\n  }\n  # for PREDICT mode\n  if(mode==tf.estimator.ModeKeys.PREDICT):\n    return tf.estimator.EstimatorSpec(mode, predictions=predictions)\n  loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(\n        logits=logits, labels=tf.cast(labels, dtype=tf.int32)))\n  # for TRAIN mode\n  if(mode==tf.estimator.ModeKeys.TRAIN):\n    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(\n        logits=logits, labels=tf.cast(labels, dtype=tf.int32)))\n    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)\n    train_op = optimizer.minimize(loss_op, global_step = tf.train.get_global_step())\n    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss_op, train_op = train_op)\n  # for Evaluation\n  if(mode==tf.estimator.ModeKeys.EVAL):\n    accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions['classes'])\n    metrics = {'accuracy': accuracy}\n    return tf.estimator.EstimatorSpec(\n        mode, loss=loss_op, eval_metric_ops=metrics)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d17ef602135c4c1cddae9d24ba14a88516bed847"},"cell_type":"code","source":"# Define the input function for training\ninput_fn = tf.estimator.inputs.numpy_input_fn(\n    x=X_train, y=y_train,\n    batch_size=batch_size, num_epochs=None, shuffle=True)\nmodel = tf.estimator.Estimator(model_fn)\n# Train the Model\n\nmodel.train(input_fn, steps=num_steps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a90eee166dde2b0c0780d254f30bf441da952b3"},"cell_type":"code","source":"# Test model \ninput_fn_test = tf.estimator.inputs.numpy_input_fn(\n    x=test,\n    num_epochs=1,\n    shuffle=False)\npredictions = model.predict(input_fn=input_fn_test)\ncls = [p['classes'] for p in predictions]\ncls_pred = np.array(cls, dtype='int').squeeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da90c83783c65181086912e313802335eba7e610"},"cell_type":"code","source":"# Convert to csv to submit to Kaggle Competitive\nsubmit = pd.DataFrame()\nsubmit['ImageId'] = range(1, test.shape[0]+1)\nsubmit['Label'] = cls_pred\nsubmit.to_csv(\"submit.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31256c65826d1849dded4e71bbfe4e21c1e1f68d"},"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}