{"metadata": {"language_info": {"version": "3.6.1", "mimetype": "text/x-python", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py", "pygments_lexer": "ipython3", "name": "python"}, "kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}}, "cells": [{"metadata": {"collapsed": false, "_execution_state": "idle", "_cell_guid": "383d65d5-7468-4f63-8463-074f1709bbdb", "_uuid": "69fe25d55ef17ddd1ec8c8767ad1f894be9440f1"}, "source": "The Easy Version Of CNN", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"trusted": false, "_execution_state": "idle", "_cell_guid": "f8f3d274-a939-435e-bc79-fb167218c99d", "_uuid": "5ad7f06c2da97f7e069d70698f9a5f44b355fc13"}, "source": "import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport random\nfrom sklearn.model_selection import train_test_split", "outputs": [], "cell_type": "code", "execution_count": 1}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "ca7943d248d72d2587c418975814e207973bad51"}, "source": "Make y_ To One-Hot ", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_cell_guid": "7f91b6d2-e9a5-4a1e-a45f-695003e48b8a", "_uuid": "7ef45561158f3144250af6ca3fce3c24afce9e6b"}, "source": "train_df=pd.read_csv('../input/train.csv')\ny_=train_df['label'].values.ravel()\nx_=train_df.iloc[:,1:].values.astype(np.float)\nx_=np.multiply(x_, 1.0 / 255.0)\ny_=pd.get_dummies(y_)\nprint(y_.head())", "outputs": [], "cell_type": "code", "execution_count": 2}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_cell_guid": "2c826597-7b1c-4ee6-ae71-6d839a5c1a53", "_uuid": "6bf59adad5f7f4f45625b6cc7d344fc466def5d6"}, "source": "x_train,x_test,y_train,y_test=train_test_split(x_,y_,test_size=0.1,random_state=1)", "outputs": [], "cell_type": "code", "execution_count": 3}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "68eec6968411122046971c12fb2960b95eee0eb0"}, "source": "def Weight(shape):\n    return tf.Variable(tf.truncated_normal(shape,stddev=0.1))\ndef Biase(shape):\n    return tf.Variable(tf.zeros(shape)+0.1)\ndef conv2d(x,w):\n    return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')\ndef pooling(x):\n    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')", "outputs": [], "cell_type": "code", "execution_count": 4}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "1ebe8a84cdf5dc26dc97fad980861cd7c2348cc3"}, "source": "x=tf.placeholder(tf.float32,[None,784])\ny=tf.placeholder(tf.float32,[None,10])\nx_image=tf.reshape(x,[-1,28,28,1])", "outputs": [], "cell_type": "code", "execution_count": 5}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "c571ef74b3310ec46c26d1e99a9e923ca6a4e83c"}, "source": "The Convolution Layer", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "a39c314d79e38305ce018464ab9155031e75b036"}, "source": "w1_cov=Weight([5,5,1,32])\nb1_cov=Biase([32])\nl1_cov=conv2d(x_image,w1_cov)+b1_cov\nl1_pooling=pooling(l1_cov)\n \nw2_cov=Weight([5,5,32,64])\nb2_cov=Biase([64])\nl2_cov=conv2d(l1_pooling,w2_cov)+b2_cov\nl2_pooling=pooling(l2_cov)", "outputs": [], "cell_type": "code", "execution_count": 6}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "3eaa2ba8beb4a0ce1cb93efa71cdcbaa350f35ea"}, "source": "The Full Layer", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "a2d8eaf59ff37970121a48ea598109880c0dd325"}, "source": "l_flat=tf.reshape(l2_pooling,[-1,7*7*64])\n \nw1_full=Weight([7*7*64,1024])\nb1_full=Biase([1024])\nwx1_full=tf.matmul(l_flat,w1_full)+b1_full\nl1_all=tf.nn.relu(wx1_full)\n \nw2_full=Weight([1024,10])\nb2_full=Biase([10])\nwx2_full=tf.matmul(l1_all,w2_full)+b2_full\nl2_full=tf.nn.softmax(wx2_full)", "outputs": [], "cell_type": "code", "execution_count": 7}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "680d6c99ce614a150361835a72745e4262bd9f47"}, "source": "Make Train", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "a59a8ee005d3a00a5189c453382f941c74afb4c1"}, "source": "loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=l2_full))\ntrain=tf.train.AdamOptimizer(0.0001).minimize(loss)", "outputs": [], "cell_type": "code", "execution_count": 8}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "1cf20362e999ba0e7ee51f60e6b537fdd70bf1f2"}, "source": "Make Accuracy", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "fbda7a65ef9b95056b743b08961b1971761978fe"}, "source": "correct=tf.equal(tf.argmax(y,1),tf.argmax(l2_full,1))\naccuracy=tf.reduce_mean(tf.cast(correct,tf.float32))", "outputs": [], "cell_type": "code", "execution_count": 9}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "b1b98ab6407f2448c9fa883bb7f0830e66c1b41c"}, "source": "SGD     \uff08n\u2014how many data in one time , num\u2014how many times\uff09", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "34a6da41269d30fac7e9a65369c641966f6c3745"}, "source": "def minbatch(n):\n    all_train=np.concatenate([x_train,y_train],axis=1)\n    random.shuffle(all_train)\n    result=[]\n    num=int(x_train.shape[0]/n)\n    for i in range(num):\n        result.append(all_train[i*n:(i+1)*n])\n    return result", "outputs": [], "cell_type": "code", "execution_count": 10}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "931b28500b63b8e630124aa0672fdcf8dba1f443"}, "source": "Save model", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "3e70173a03a7dafae7f85df1f12af2dcdc1bdfe0"}, "source": "saver=tf.train.Saver()", "outputs": [], "cell_type": "code", "execution_count": 11}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "e5e43eab161222b33162ee0e6d7d94c96adea0ce"}, "source": "Make n and num of SGD", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "0488c1c915bd7db478c8b636220aa3ca79583bf9"}, "source": "n=100\nnum=int(x_train.shape[0]/100)", "outputs": [], "cell_type": "code", "execution_count": 12}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "769687a04ae80578d15ebd5d178a84eb4bfd6709"}, "source": "Start Training   (Because of time,just execute three times)", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "trusted": false, "_execution_state": "idle", "_uuid": "a466a140783c0d2d2c41584b123afa50a447f6bb"}, "source": "with tf.Session() as sess:\n    sess.run(tf.global_variables_initializer())\n    print('Start-Train')\n    for i in range(11):\n        all_train=minbatch(n)\n        for j in range(num):\n            x_batch,y_batch=all_train[j][:,:784],all_train[j][:,-10:]\n            sess.run(train,feed_dict={x:x_batch,y:y_batch})\n        acc=sess.run(accuracy,feed_dict={x:x_test,y:y_test})\n        print(i,acc)\n    saver.save(sess,'..\\\\net-cnn.ckpt')", "outputs": [], "cell_type": "code", "execution_count": 13}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "066c7d90051d42a49591eddc23a85df0d9160fd8"}, "source": "Train test", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "39453e740609440b0b86fbaa29e75e2b29c0f322"}, "source": "test_df=pd.read_csv('..\\\\test.csv')\ntest_data=test_df.iloc[:,:].values.astype(np.float)\ntest_data=np.multiply(test_data, 1.0 / 255.0)\nwith tf.Session() as sess:\n    sess.run(tf.global_variables_initializer())\n    saver.restore(sess,'..\\\\net-cnn.ckpt')\n    y_hat=sess.run(l2_full,feed_dict={x:test_data})", "outputs": [], "cell_type": "code", "execution_count": null}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "583faceb9b9c60ae3c97644c71e08ed69a5805a0"}, "source": "Make One-Hot To Digit", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "ac44cc00cc2982cb94d7d51fc364443dcfa4f59e"}, "source": "result=[]\nfor i in y_hat:\n    result.append((np.argmax(i)))", "outputs": [], "cell_type": "code", "execution_count": null}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "6feb5bb4d0b7e793b8f34f96ea0e80a8d4bd1641"}, "source": "Submit", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "7de8a93bc7fef9b2cf27c3e86d233fcf11848915"}, "source": "predict=pd.DataFrame(data={'ImageId':list(range(1,28001)),'Label':result})\npredict.to_csv('..\\\\cnn-1.csv',index=False)", "outputs": [], "cell_type": "code", "execution_count": null}], "nbformat": 4, "nbformat_minor": 0}