{"nbformat_minor": 0, "cells": [{"cell_type": "markdown", "metadata": {"_cell_guid": "d661f6f2-b3d9-452e-94c9-b41a3dec1685", "_execution_state": "idle", "collapsed": false, "_uuid": "5072c97f92e4401cde83b65e65cea601a85893f1"}, "source": "A simple TFlearn approach for digit recognition.", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "78af12d3-2ba5-4d8a-93cc-0b2b09ab8fb8", "_execution_state": "idle", "collapsed": false, "_uuid": "5946340b697f82d8b5122f091e048a01c9a0a303", "trusted": false}, "source": "import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport tensorflow as tf\nimport tflearn\n%config IPCompleter.greedy=True", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "804d1adc-c357-4456-af13-be1f4d63b765", "_execution_state": "idle", "collapsed": false, "_uuid": "d84e6a5e05774d70db0c74eb750674a941424ced", "trusted": false}, "source": "# Load the input data\ntrain=pd.read_csv('../input/train.csv')\ntest=pd.read_csv('../input/test.csv')\nprint('Number of records in training file:'+str(len(train)))\nprint('Number of records in testing file:'+str(len(test)))\nprint(test.head(5))", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "41de5cc0-6335-405e-8d07-63333195cb4e", "_execution_state": "idle", "collapsed": false, "_uuid": "47a2c5a2b276fd1ed8aac623cc0f88de39fcfac8", "trusted": false}, "source": "# Generate trainX,trainY\ntrainX=train.drop('label',axis=1)\ntrainX=trainX.values\nprint(trainX[:5])\nprint()\nprint('Shape of trainX:'+str(trainX.shape))\nprint()\n#Convert trainY to one_hot encoding values\ntrainY=train['label']\ntrainY=np.eye(max(trainY)+1)[trainY]\nprint(trainY[:5])", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "2d0d985f-2e64-4df9-9daf-b90579768faf", "_execution_state": "idle", "collapsed": false, "_uuid": "dabd09df0242300d2d39a47d46d846008a299df4", "trusted": false}, "source": "# Visualizing the data\n\ndef show_digit(index):\n    label=trainY[index].argmax(axis=0)\n    #Reshape 784 array into 28x28 image\n    image=trainX[index].reshape([28,28])\n    plt.title('Training data,index:%d,label:%d' %(index,label))\n    plt.imshow(image,cmap='gray_r')\n    plt.show\n \n # Display the first training image\nshow_digit(9997)\n \n    ", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "b47450f9-104d-4ad5-906c-0e21d77f17da", "_execution_state": "idle", "collapsed": false, "_uuid": "ea48e45e389699dd28966064ab31d9a27f1759e2", "trusted": false}, "source": "# Building the network\n\ndef build_model():\n    # Input Layer\n    net=tflearn.input_data([None,trainX.shape[1]])\n    # Hidden Layer\n    net=tflearn.fully_connected(net,64,activation='ReLU',regularizer='L2',weight_decay=0.001)\n    net=tflearn.dropout(net,0.75)\n    net=tflearn.fully_connected(net,64,activation='ReLU',regularizer='L2',weight_decay=0.001)\n    net=tflearn.dropout(net,0.75)\n    # Output Layer\n    net=tflearn.fully_connected(net,10,activation='softmax')\n    \n    # Set up how to train the network\n    net=tflearn.regression(net,optimizer='sgd',learning_rate=0.001,loss='categorical_crossentropy')\n    \n    model=tflearn.DNN(net)\n    return model\n    \n    ", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "683ac6dc-62c4-412c-933e-a9dd6e0af249", "_execution_state": "idle", "collapsed": false, "_uuid": "498a0cdb24d89914fcd2c9dddd8ca607627f0469", "trusted": false}, "source": "# Build the model\nmodel=build_model()\n", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "0b9cdfff-548b-4248-b547-8c6898179675", "_execution_state": "busy", "collapsed": false, "_uuid": "70e51150d7ba557168f86f7c8aac7bdba0475427", "trusted": false}, "source": "# Train the model\n\nmodel.fit(trainX,trainY,validation_set=0.25,show_metric=True,batch_size=128,n_epoch=200)", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "747b22c4-71f5-4d65-8679-b1bdcc31d13c", "_execution_state": "busy", "collapsed": false, "_uuid": "f4831928554f3f49971eacc7a80c281ad4cefe9a", "trusted": false}, "source": "# Run test records and generate predictions\ntestX=test.values\nprediction=np.array(model.predict(testX)).argmax(axis=1)\nprediction=pd.Series(prediction,name='label')\nprint(prediction[:10])\n", "execution_count": null, "outputs": []}, {"cell_type": "code", "metadata": {"_cell_guid": "086d73b3-95c9-4d72-bff9-2a599e4760fe", "_execution_state": "busy", "collapsed": false, "_uuid": "1acf00c7b3745984eacf9207cbccc054867ccb4c", "trusted": false}, "source": "# Generate the submission file\n\nsubmission=pd.concat([pd.Series(range(1,len(test)+1),name='ImageID'),prediction],axis=1)\nsubmission.to_csv('cnn_mnist_datagen.csv',index=False)", "execution_count": null, "outputs": []}], "nbformat": 4, "metadata": {"language_info": {"version": "3.6.1", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}}