{"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from __future__ import division, print_function, absolute_import\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\n\ntrain = pd.read_csv('../input/train.csv', nrows = 40000)\nvalid = pd.read_csv('../input/train.csv',skiprows = 40000, nrows = 2000)\ntrain_dataset = train.iloc[:,1:785]\ntrain_labels  = train.iloc[:,0]\n\nvalid_dataset = valid.iloc[:,1:785]\nvalid_labels  = valid.iloc[:,0]\n\ntrain_dataset_np = (train_dataset.as_matrix()/255.0) - 1.0\ntrain_labels_np = train_labels.as_matrix()\n\nvalid_dataset_np = (valid_dataset.as_matrix()/255.0) - 1.0\nvalid_labels_np = valid_labels.as_matrix()\n\n# Training Parameters\nlearning_rate = 0.001\nnum_steps = 2000\nbatch_size = 128\n\n# Network Parameters\nnum_input = 784\nnum_classes = 10 # MNIST total classes (0-9 digits)\ndropout = 0.5 # Dropout, probability to drop a unit\n\n\n# Create the neural network\ndef conv_net(x_dict, n_classes, dropout, reuse, is_training):\n    # Define a scope for reusing the variables\n    with tf.variable_scope('ConvNet', reuse=reuse):\n        # TF Estimator input is a dict, in case of multiple inputs\n        #x = x_dict['images']\n\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_dict, shape=[-1, 256, 256, 1])\n\n        conv1 = tf.layers.conv2d(x , 32, 5, padding='same', activation=tf.nn.relu)\n        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)\n        conv2 = tf.layers.conv2d(conv1, 32, 5, padding='same', activation=tf.nn.relu)\n        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)\n        conv3 = tf.layers.conv2d(conv2, 32, 5, padding='same', activation=tf.nn.relu)\n        conv3 = tf.layers.max_pooling2d(conv3, 2, 2)\n        conv4 = tf.layers.conv2d_transpose(conv3, 32, 5, padding='same', activation=tf.nn.relu)\n        conv4 = tf.concat([conv4, conv3], axis=-1)\n        conv5 = tf.layers.conv2d(conv4, 32, 5, padding='same', activation=tf.nn.relu)\n        conv6 = tf.layers.conv2d_transpose(conv5, 32, 5, padding='same', activation=tf.nn.relu)\n        conv6 = tf.concat([conv6, conv2], axis=-1)\n        conv7 = tf.layers.conv2d(conv6, 32, 5, padding='same', activation=tf.nn.relu)\n        conv8 = tf.layers.conv2d_transpose(conv7, 32, 5, padding='same', activation=tf.nn.relu)\n        conv8 = tf.concat([conv8, conv1], axis=-1)\n        conv9 = tf.layers.conv2d(conv8, 32, 5, padding='same', activation=tf.nn.relu)\n        # Flatten the data to a 1-D vector for the fully connected layer\n        fc1 = tf.contrib.layers.flatten(conv9)\n\n        # Fully connected layer (in tf contrib folder for now)\n        fc1 = tf.layers.dense(fc1, 1024, activation=tf.nn.relu)\n        # Apply Dropout (if is_training is False, dropout is not applied)\n        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)\n\n        # Output layer, class prediction\n        out = tf.layers.dense(fc1, n_classes, activation=tf.nn.softmax)\n\n    return out\n\n\n# Define the model function (following TF Estimator Template)\ndef model_fn(features, labels, mode):\n    # Build the neural network\n    # Because Dropout have different behavior at training and prediction time, we\n    # need to create 2 distinct computation graphs that still share the same weights.\n    logits_train = conv_net(features, num_classes, dropout, reuse=False,\n                            is_training=True)\n    logits_test = conv_net(features, num_classes, dropout, reuse=True,\n                           is_training=False)\n\n    # Predictions\n    pred_classes = tf.argmax(logits_test, axis=1)\n    pred_probas = tf.nn.softmax(logits_test)\n\n    # If prediction mode, early return\n    if mode == tf.estimator.ModeKeys.PREDICT:\n        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)\n\n        # Define loss and optimizer\n    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(\n        logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))\n    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)\n    train_op = optimizer.minimize(loss_op,\n                                  global_step=tf.train.get_global_step())\n\n    # Evaluate the accuracy of the model\n    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)\n\n    # TF Estimators requires to return a EstimatorSpec, that specify\n    # the different ops for training, evaluating, ...\n    estim_specs = tf.estimator.EstimatorSpec(\n        mode=mode,\n        predictions=pred_classes,\n        loss=loss_op,\n        train_op=train_op,\n        eval_metric_ops={'accuracy': acc_op})\n\n    return estim_specs\n\n# Build the Estimator\nmodel = tf.estimator.Estimator(model_fn)\n\n# Define the input function for training\ninput_fn = tf.estimator.inputs.numpy_input_fn(\n    train_dataset_np, train_labels_np,\n    batch_size=batch_size, num_epochs=300, shuffle=True)\n# Train the Model\nmodel.train(input_fn, steps=num_steps)\n\n# Evaluate the Model\n# Define the input function for evaluating\ninput_fn = tf.estimator.inputs.numpy_input_fn(\n    x=valid_dataset_np, y=valid_labels_np,\n    batch_size=batch_size,num_epochs=200, shuffle=False)\n# Use the Estimator 'evaluate' method\ne = model.evaluate(input_fn)\n\nprint(\"Validation Accuracy:\", e['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3ed11781fce67f97cb49b8215fd09f0e04ee74d"},"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}