{"cells":[{"metadata":{"trusted":true,"_uuid":"80d8d9d21a7e90cc70decafa9dc0475108c9c26c","collapsed":true},"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout,Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import backend as K\nfrom keras.optimizers import Adam\nfrom keras.models import model_from_json\nfrom keras.callbacks import ReduceLROnPlateau, TensorBoard, EarlyStopping, ModelCheckpoint\nfrom keras.models import load_model\nfrom keras import regularizers","execution_count":17,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":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":18,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"data = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\nsample = pd.read_csv('../input/sample_submission.csv')\nsample.head()","execution_count":19,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0cbc50ecfeddf6a3f937832d08392ab0333f89e","collapsed":true},"cell_type":"code","source":"data_train = data.values\ndata_test = test.values","execution_count":20,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54c1c52b067c70f41a256aebc59e2def88e9a110","collapsed":true},"cell_type":"code","source":"y_train = data_train[:,0]\nX_train = data_train[:,1:]\nX_test = data_test\nprint(\"Shape of X_test  :\",X_test.shape)","execution_count":21,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"93138a020df416768c26c20f3f0fac9da4308a1d"},"cell_type":"code","source":"import numpy as np\nbatch_size = 128\nnum_classes = 10\nepochs = 100","execution_count":22,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0ee291cbe232f823a4eba9fce505554f080953b","collapsed":true},"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0],28,28,1)\nX_test = X_test.reshape(X_test.shape[0],28,28,1)   \ninput_shape = (28,28,1) \nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_train/=255\nX_test/=255","execution_count":23,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa270772f52a819c97c841ea4a8be8183510f52d","collapsed":true},"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train,num_classes)","execution_count":24,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a06a207e1741d30b0c364cd3d28d60c53e3d4e2d","collapsed":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=input_shape))\nmodel.add(Conv2D(128,(3,3),activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes,activation='softmax'))\nmodel.summary()","execution_count":25,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ae3c409cb175ab9067760cb887631aa807dd836","collapsed":true},"cell_type":"code","source":"model.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7),\n              metrics=['accuracy'])\nlr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=3, verbose=1)\ntensorboard = TensorBoard(log_dir='./logs')\nearly_stopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=8, verbose=1, mode='auto')\ncheckpointer = ModelCheckpoint('./model.h5', monitor='val_loss', verbose=1, save_best_only=True)\nmodel.fit(np.array(X_train), np.array(y_train),\n          batch_size=batch_size,\n          epochs=epochs,\n          verbose=1,\n          shuffle=True,\n          callbacks=[lr_reducer, tensorboard, early_stopper, checkpointer])","execution_count":26,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44dfdc03c945adf61cc63d964abd0646f911437d","_kg_hide-output":true,"collapsed":true},"cell_type":"code","source":"predict = model.predict(X_test)\npredicts = np.argmax(predict,axis = 1)\nresults = pd.Series(predicts,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)\n","execution_count":28,"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}