{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"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","collapsed":true,"trusted":true},"cell_type":"code","source":"#Shallow-net demo","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"89d9ab50-c027-4210-b65c-1b8e9fd8642a","_uuid":"02e78481b1fa4f5a163219eb089a22c78045e8ab"},"cell_type":"markdown","source":"### Shallownet in Keras"},{"metadata":{"_cell_guid":"1c639e82-8bc7-4dc0-91df-3ec31aaa85bc","_uuid":"964ee89653963593b82efb017c876e2c989f8283"},"cell_type":"markdown","source":"Building a Shallow net in keras for digit recognizer for MNIST"},{"metadata":{"_cell_guid":"de258f65-50c9-4c9c-a723-676e65c94f47","_uuid":"4f373446d528de1cd30efe8e856f54e4b59a2005"},"cell_type":"markdown","source":"#### Set seed for reproducibility"},{"metadata":{"_cell_guid":"8a505004-0560-4168-8daa-937792197dc3","_uuid":"469e575fdb7144d05e0eba6a262e4b31adb4b8b6","collapsed":true,"trusted":true},"cell_type":"code","source":"import numpy as np\nnp.random.seed(42)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ed4ccf0e-7ddb-4426-bc45-bdddc4c76ef5","_uuid":"834ce884576e68e7cf68c3f573728a287ae2cc9c"},"cell_type":"markdown","source":"#### Load Dependencies"},{"metadata":{"_cell_guid":"52512b4a-5986-4c42-9725-c2d15fb1b204","_uuid":"ed919255e91c32a9e4a2222c64b49d732892af64","collapsed":true,"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.optimizers import SGD","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5151c270-669b-4df1-9a2a-0904e342fb14","_uuid":"cc72a9a7eac794f4c87696cedbcd3b355687f081"},"cell_type":"markdown","source":"#### Load data"},{"metadata":{"_cell_guid":"5eeaba69-2586-4264-bee4-0eb5984eee50","_uuid":"1de6344c6c26a249497670dd851910d88265a82c","collapsed":true,"trusted":true},"cell_type":"code","source":"import pandas as pd\n#(X_train, y_train), (X_test, y_test) = mnist.load_data() Not working due to some issues","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"23f0b2e3-1a11-43cd-b453-9e93b0b5106e","_uuid":"af2c4841f3b43e626fb7b63ad3ed9a4847b6a367","collapsed":true,"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"50ffef9a-8d13-4394-ad04-7b20287522ee","_uuid":"a9976af92a03065d969686eb9fbff80376b1f8d0","collapsed":true,"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f79a94d4-5eab-423d-9f8e-85d09bf0cf17","_uuid":"e3c02cdffac39d453bb01966ea51f162d217603d","collapsed":true,"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"36e68937-e4ce-44b8-9889-177ae95cff14","_uuid":"bd6a3e8cd70b5a8ddd95be94529a8de83254725d","collapsed":true,"trusted":true},"cell_type":"code","source":"X_train = train.drop('label', axis = 1)\n#X_test = test.drop('label', axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"95306762-476a-465e-bd35-87d94109bb80","_uuid":"ede93e9ed3c74819c727377d4760948b9be87cb5","collapsed":true,"trusted":true},"cell_type":"code","source":"y_train = train['label']","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6b4d779e-1652-4c25-bb9d-632c02db82ca","_uuid":"6a6270ef5c68d81460a3d16b857c7940a01643dd","collapsed":true,"trusted":true},"cell_type":"code","source":"X_train /= 255\nX_test = test/255","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ca0958da-1c91-4f13-b0d7-d1d1859cbdeb","_uuid":"8142c9ad919820b041456c90017312a6a2c7f0e8","collapsed":true,"trusted":true},"cell_type":"code","source":"n_classes = 10\ny_train = keras.utils.to_categorical(y_train, n_classes)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"40604784-9fbb-46b7-b55f-d2b48c61e640","_uuid":"ebe298e00e7408a7620d02e4b2ab6c33df5b0220","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d75b59f0-4ec7-4b60-92f5-621ebf3f38ac","_uuid":"61199becf8a151838905bb5e7819dbbb14f470c1"},"cell_type":"markdown","source":"####  Design neural network architecture"},{"metadata":{"_cell_guid":"a9b57e17-253e-4c8b-b267-df6aac8fe907","_uuid":"2642a326042e21cd52fe78305a77bf60021f2b26","collapsed":true,"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(64, activation='relu', input_dim=784))\n#model.add(Dropout(0.5))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\n#model.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ec650d93-7ea2-4f78-b510-7cb473fc8f6d","_uuid":"11f6f1a86a8b123469b079127e29762af6ad97a2","collapsed":true,"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e1541b5c-7899-4771-850d-1d2fb6665408","_uuid":"ac4acf7796616af5588a77fcff6d53567e035b60"},"cell_type":"markdown","source":"####  Configure Model"},{"metadata":{"_cell_guid":"01312a2a-cc14-460c-af54-0a2ec15b82f6","_uuid":"784b685e387718db598c374e4d780469b30eb928","collapsed":true,"trusted":true},"cell_type":"code","source":"model.compile(optimizer='SGD',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"902c3a64-286a-4900-b596-d23ee1b74680","_uuid":"a634c4e53ae3c7990cde18e9942cc4263aeefc63","collapsed":true,"trusted":true},"cell_type":"code","source":"# Train the model\nmodel.fit(X_train, y_train, epochs=50)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"a22f0fdc-2184-493b-9a6d-bb26038b3511","_uuid":"ffd39c019d39c2cfee0fa9ba68dd379feeee7a18","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test = model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6dcd4a95-9bc5-4c48-b956-d4e259aa3d03","_uuid":"f45ef1c8db6d28a093f8a3e3e2e0b08ae0884eed","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fa60b8c2-2d4a-47c8-9ab7-3be8ef7e4253","_uuid":"7a32b131894edcd8392e066b3ff4701f7ad6fecf","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test[0]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c1c7a1cd-031b-4051-b892-f24012a197dc","_uuid":"37f76e4fd4bc0c9b15bccaa59e2e560ea4a134a0","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test = y_test.argmax(axis =1 )","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ba51bc92-1330-4aad-8aa3-69c3ddc646c4","_uuid":"4d9e0ffced195ae84ab6e8d6fb78bb807ea0b351","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"39891792-9895-40c2-930e-06577b596107","_uuid":"9c187112c0eaef2c0ec7ad127c42f70f5b8271ae","collapsed":true,"trusted":true},"cell_type":"code","source":"y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e28731e9-f3db-447e-b665-f535c0600fea","_uuid":"ddf3bf232691531338c0d8d7a2eac1aeaf077970","collapsed":true,"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2d16dc2f-c918-43fa-9239-cd55f0f33399","_uuid":"472ed9113b8cb15628b6a2c4a735be5705f0cd0c","collapsed":true,"trusted":true},"cell_type":"code","source":"out = pd.DataFrame({'ImageId':np.arange(1,28001), 'Label': y_test})","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"abe1e898-4c8a-43b4-a9b3-b51cf2db19d4","_uuid":"0e889d3448f69e883bd6ea38b52490bb941b2d97","collapsed":true,"trusted":true},"cell_type":"code","source":"out.to_csv('submit.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2be3ad78-fae8-4a16-9f37-e67536e21c46","_uuid":"c210348aedb6236247556b5743ee8655b8dadb54","collapsed":true,"trusted":true},"cell_type":"code","source":"ls","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"54408783-520e-4a02-8d27-c9610e86b7a2","_uuid":"f4dd27533780e80d35bf4cc1dccacf6e5b0ab130","collapsed":true,"trusted":true},"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.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}