{"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\"))\nimport tensorflow as tf\n# Any results you write to the current directory are saved as output.","execution_count":39,"outputs":[{"output_type":"stream","text":"['train.csv', 'test.csv', 'sample_submission.csv']\n","name":"stdout"}]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/train.csv')\nlabel = data.label.values\n#label = pd.get_dummies(data.label).values.reshape([42000,10,1])\ndata = data.drop('label', axis=1).values","execution_count":60,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = data.reshape([42000, 28, 28])\ndata = data/255","execution_count":61,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.CuDNNLSTM(128, input_shape=(28,28), return_sequences=True))\nmodel.add(tf.keras.layers.CuDNNLSTM(128))\nmodel.add(tf.keras.layers.Dense(10, activation='relu'))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy')","execution_count":62,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(data, label, epochs=10)","execution_count":63,"outputs":[{"output_type":"stream","text":"Epoch 1/10\n42000/42000 [==============================] - 11s 251us/sample - loss: 0.4456\nEpoch 2/10\n42000/42000 [==============================] - 9s 226us/sample - loss: 0.1290\nEpoch 3/10\n42000/42000 [==============================] - 9s 207us/sample - loss: 0.0852\nEpoch 4/10\n42000/42000 [==============================] - 9s 211us/sample - loss: 0.0647\nEpoch 5/10\n42000/42000 [==============================] - 10s 234us/sample - loss: 0.0512\nEpoch 6/10\n42000/42000 [==============================] - 9s 215us/sample - loss: 0.0447\nEpoch 7/10\n42000/42000 [==============================] - 9s 213us/sample - loss: 0.0366\nEpoch 8/10\n42000/42000 [==============================] - 9s 207us/sample - loss: 0.0327\nEpoch 9/10\n42000/42000 [==============================] - 9s 207us/sample - loss: 0.0306\nEpoch 10/10\n42000/42000 [==============================] - 9s 213us/sample - loss: 0.0266\n","name":"stdout"},{"output_type":"execute_result","execution_count":63,"data":{"text/plain":"<tensorflow.python.keras.callbacks.History at 0x7fbac6dead68>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/test.csv').values\ndata = data.reshape([data.shape[0], 28, 28])\ndata = data/255","execution_count":67,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(data)","execution_count":68,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"r=[]\nfor p in pred:\n    r.append(np.argmax(p))\nres = pd.DataFrame()\nres['Label'] = r\nres.index = res.index+1\nres.index.names = ['ImageId']","execution_count":72,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res.to_csv('result.csv')","execution_count":76,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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"}},"nbformat":4,"nbformat_minor":1}