{"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\"))\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","trusted":true},"cell_type":"code","source":"import keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68e9def621bc5484f3f194b27b267b4c0fac03e1","collapsed":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\nlabels = train.iloc[:,0].values.astype('int32')\n#y = train.iloc[:,0].values.astype('int32')\ntrain_X = train.iloc[:,1:].values.astype('float32')\ntest = (pd.read_csv('../input/test.csv').values).astype(\"float32\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3cf9a0c4c9b46e5f77b7d5075194cf7386d60c9e","collapsed":true},"cell_type":"code","source":"num_classes=10\nlabels=keras.utils.to_categorical(labels, num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df6b2777be9e2cd4237f28b099b9b90d4e9931cd"},"cell_type":"code","source":"train_X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28aa86ef9e6a6a232f390955303076ba2243916a"},"cell_type":"code","source":"labels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6246a12585d0065102358d2c1cca89c5028cb404"},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac6389d733b7583c48f7909a407ac6b83247b85f","collapsed":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3796116ae155ae694b3f62936ca3ead197f11854"},"cell_type":"code","source":"batch_size = 128\nnum_classes = 10\nepochs = 80","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"21a06405f5c20b18c820d0472a95bd36e8bf3e84"},"cell_type":"code","source":"img_rows,img_cols = 28,28","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a20bdafefc3b746d5a545d1fedc4557e6c0e841a"},"cell_type":"code","source":"train_X/=255\ntest/=255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c9f58c1c32b0ef84975535551c3e2a750019576"},"cell_type":"code","source":"from keras import backend as K\nK.image_data_format()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"22f8572a3a1d6a8dd92a75680ac0543c300f535d"},"cell_type":"code","source":"input_shape = (img_rows,img_cols,1)\n\n#trying out a new model\n\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), padding='same', activation='relu',input_shape = input_shape))\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\"))\nmodel.add(Conv2D(32, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(32, (3, 3), padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\"))\nmodel.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\"))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(units=256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(units=256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(units=10, activation='softmax'))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb37ff176f62f89cc706454d6270ef35d4594522"},"cell_type":"code","source":"train_X = train_X.reshape(train_X.shape[0], img_rows, img_cols,1)\nadam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)\nmodel.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"299460f1cfce72037d50094034945764d9ff0652"},"cell_type":"code","source":"\nmodel.fit(train_X, labels,\n          batch_size=batch_size,\n          epochs=epochs,\n          verbose=2,\nvalidation_split=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"170e0e668d46833d99d4cc54c6c23bdc25572e5c","collapsed":true},"cell_type":"code","source":"test = test.reshape(test.shape[0], img_rows, img_cols,1)\npreds = model.predict_classes(test, verbose=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3f1332774d8c64936ad9406c42fc57789bf7ba0e"},"cell_type":"code","source":"def write_preds(preds, fname):\n    pd.DataFrame({\"ImageId\": list(range(1,len(preds)+1)), \"Label\": preds}).to_csv(fname, index=False, header=True)\n\nwrite_preds(preds, \"mnist-cnn.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dacc2f54b1991b75007a494b6ced217346035c44"},"cell_type":"code","source":"!ls\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9f552140a969228851ceee7e7a3c8d146dd603db"},"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}