{"cells":[{"metadata":{"_uuid":"58a338c7f03f47360102a96147bfa06090a8a57f","_cell_guid":"cc4b2986-b966-467a-b107-93375d5b2c36","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nn_steps=28\nn_inputs = 28\n\nn_outputs = 10\nn_neurons=150\nX=tf.placeholder(tf.float32,shape=[None,n_steps,n_inputs],name=\"X\")\nY=tf.placeholder(tf.int64,shape=(None),name=\"Y\")","execution_count":3,"outputs":[]},{"metadata":{"_uuid":"b8744f0080ff0943d3f0a0f857d41cc2c38b2f5f","_cell_guid":"43f0f9a7-a3e7-4f6c-b79d-7fba1512ea27","trusted":true},"cell_type":"code","source":"basic_Cell=tf.contrib.rnn.BasicRNNCell(num_units=n_neurons,activation=tf.nn.relu)\noutputs1,states=tf.nn.dynamic_rnn(basic_Cell,X,dtype=tf.float32)","execution_count":4,"outputs":[]},{"metadata":{"_uuid":"9d4a1ba23b692b37cb77981140e583f705c1ec0f","_cell_guid":"3e3e9a03-07fe-4116-b2f4-95bf5c31ccba","trusted":true},"cell_type":"code","source":"logits=tf.layers.dense(states,n_outputs)\nwith tf.name_scope(\"loss\"):\n    xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(labels=Y,logits=logits)\n    loss=tf.reduce_mean(xentropy,name=\"loss\")\nprint(\"loss\")","execution_count":5,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"bd23caf25f842a458d096ec778960ee904789ce6","_cell_guid":"58e866ee-48b4-43c1-986a-d1443dc66100","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"a709808d67a7592adc1b798663223043190bcfaa","_cell_guid":"763be1df-f7d8-4930-bc06-733331c6a11f","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"941ea5a1bcd3cc3247674efd97522000ea86ee18","_cell_guid":"e29b8e55-458e-45db-824d-c41e016af190","trusted":true},"cell_type":"code","source":"learning_rate=0.0001\nwith tf.name_scope(\"train\"):\n    optimizer=tf.train.AdamOptimizer(learning_rate)\n    training_op=optimizer.minimize(loss)\nprint(\"train\")","execution_count":6,"outputs":[]},{"metadata":{"_uuid":"4ceb51452ee2a38dc7a2bc19488e0180cac70ded","_cell_guid":"00f6c367-c166-48f3-aecc-199dd62e9528","trusted":true},"cell_type":"code","source":"with tf.name_scope(\"eval\"):\n    correct=tf.nn.in_top_k(logits,Y,1)\n    accuracy=tf.reduce_mean((tf.cast(correct,tf.float32)))\nprint(\"eval\")","execution_count":7,"outputs":[]},{"metadata":{"_uuid":"58c69800fe438f1e24f260320c5e63717875e6bc","_cell_guid":"eb991dc2-8ea4-4821-9ca1-87cae5b73fbf","trusted":true},"cell_type":"code","source":"init=tf.global_variables_initializer()\nsaver=tf.train.Saver()\n\nprint(\"init\")","execution_count":8,"outputs":[]},{"metadata":{"_uuid":"c32fd28da49f855ff7d2201b8e9b46a6749682e2","_cell_guid":"151537ca-d743-417a-84bd-3bdb20ea2d07","trusted":true},"cell_type":"code","source":"dataset=pd.read_csv(\"../input/train.csv\")\nX_data=dataset.iloc[:,1:785].values\nY_data=dataset.iloc[:,0:1].values\nfrom sklearn.cross_validation import train_test_split\nX_train,X_valid,y_train,y_valid=train_test_split(X_data,Y_data,test_size=0.2)\nprint(\"input\")","execution_count":9,"outputs":[]},{"metadata":{"_uuid":"5297399630a14d2b18285c8d8d9320b9ca9c5af0","_cell_guid":"78028f8e-734c-430b-b1ad-d682ec96e2ea","trusted":true},"cell_type":"code","source":"X_valid=X_valid.reshape((-1,n_steps,n_inputs))\ny_train=y_train.reshape(33600,)\ny_valid=y_valid.reshape(8400,)\nn_epochs=300\nbatch_size=20\nlengthofdata=len(X_train)\nprint(\"Initialized\")","execution_count":10,"outputs":[]},{"metadata":{"_uuid":"c2d7efa9dc131b70cacf61f3241b0260df369ff7","_cell_guid":"a00b1c0a-97e0-4ac1-85fb-0de9ed984349","trusted":true},"cell_type":"code","source":"%matplotlib inline\nlist1=[]\nwith tf.Session() as sess:\n    init.run()\n    for epochs in range(n_epochs):\n        i=0\n        while i<lengthofdata:\n            start=i\n            end=i+batch_size\n            X_batch=np.array(X_train[start:end])\n            X_batch=X_batch.reshape((-1,n_steps,n_inputs))\n            \n            Y_batch=np.array(y_train[start:end])\n            i=end\n            sess.run(training_op,feed_dict={X:X_batch,Y:Y_batch})\n        acc_train=accuracy.eval(feed_dict={X:X_batch,Y:Y_batch})\n        X_valid=np.array(X_valid)\n        y_valid=np.array(y_valid)\n        acc_test=accuracy.eval(feed_dict={X:X_valid,Y:y_valid})\n        plt.plot(acc_test)\n        print(epochs, \"Train Accuracy: \",acc_train,\"Test Accuracy\", acc_test)\n        list1.append(acc_test)\n    save_path=saver.save(sess,\"./my_model_final.ckpt\")","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"0201bde0231670183aea787d400b24dbf9b4f284","_cell_guid":"ddd4cf4d-a743-408d-b491-7e7b600bbc36","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"44555558afa3046fec1dc07c7b7b290e2f4d3d68","_cell_guid":"05b548cc-9461-4b99-b228-bd3558b92e2d","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}