{"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)\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\n\nsns.set(style='white', context='notebook', palette='deep')\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":{"trusted":true,"_uuid":"da7214c7b3a97e2475da49c87793b80903c5aaf6"},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntest_data = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"624ad30dd49489b8eded94ef22d7208f385b1bf5"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1ca2ff47c29eec1304fbf57b71b9e4adb27ac3a5"},"cell_type":"markdown","source":"First let us use basic Keras and then using Tensorflow Keras API\n\n### Basic Keras"},{"metadata":{"trusted":true,"_uuid":"9e459921898a479a25a34f11b2c5d05b1010a43e"},"cell_type":"code","source":"y_train = train_data['label']\nX_train = train_data.drop(columns=['label'],axis=1)\n\ndel train_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c20fcfdf4043b70467684e5f86fbb4d0f60d4575"},"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(y_train.value_counts())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eae1d3e5ca3ee4e4c3c3dffb95e9438667dfd8cf"},"cell_type":"code","source":"sns.countplot(y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5651e701e6e8f637cbeb627fbb5c020ba15292c"},"cell_type":"code","source":"X_train.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26c0769a5de9a7f893c860bc97de94fa1b1a6a8f"},"cell_type":"code","source":"test_data.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"447eedf410d458075b480732f20e373e78b80fc9"},"cell_type":"markdown","source":"### Prepare data\n\nThis is a very important step. We have to convert the data into float and scale the input data****\n\n* Normalization\n* Reshaping\n* Categorization of labels"},{"metadata":{"_uuid":"63d13f7ce816ade0ed89367e96e59495cc468612"},"cell_type":"markdown","source":"### Normalization\n\n1. But why do we normalization by 255\n2. Another way of nomralization is reducing by mean and dividing by std. "},{"metadata":{"trusted":true,"_uuid":"fc85c996110f77ebfe2a4b2c67a9946dc3718c49"},"cell_type":"code","source":"X_train = X_train/255.\ntest_data = test_data/255.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ef6db10eb09aa580dc586ebd27ef1ec70f84ddb2"},"cell_type":"markdown","source":"### Label Encoding"},{"metadata":{"trusted":true,"_uuid":"5ab24636bc12e6a0431bc37e0888d541eab1b14a"},"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"### Reshape"},{"metadata":{"trusted":true,"_uuid":"527a8a465763243a7cf5b34bb17aeef7be3ed82b"},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest_data = test_data.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cee4c67eb44987ffc228f984ba6b251550cb1e9f"},"cell_type":"code","source":"print(X_train.shape)\n#print(X_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b10165e338a675112fa02be4a9b6183ef188950"},"cell_type":"code","source":"X_train,X_val,y_train,y_val = train_test_split(X_train,y_train,random_state = 42,test_size=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fd14c29db3909f1a7ca2f68b0a29f43e294d1be"},"cell_type":"code","source":"y_train[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16408e3629eab4ec27b72349f9bfd28731e21754"},"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebe5f94d91a26b53cf67295c75fa671030c9fbf8"},"cell_type":"code","source":"network = Sequential()\nnetwork.add(Dense(512,activation='relu',input_shape = (28,28,1)))\nnetwork.add(Flatten())\n#network.add(Dense(512, activation='relu'))\nnetwork.add(Dense(10, activation='softmax'))\nprint(\"input shape \",network.input_shape)\nprint(\"output shape \",network.output_shape)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1550690f19b693cb3ad3d87620e65f2ecf0efbd9"},"cell_type":"code","source":"network.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9970c5961b4e192dc07ac38695aaca96221c5d1d"},"cell_type":"code","source":"network.fit(X_train, y_train, epochs=5, batch_size=128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0fd737a2b5c0fa3861d4d71c4f6c5871b3925f2"},"cell_type":"code","source":"history = network.fit(X_train,y_train,epochs=20,batch_size=512,validation_data=(X_val,y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a698d54729cbfe07ef42004d2e99652316e618de"},"cell_type":"code","source":"history_dict = history.history\nhistory_dict.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e7681d57aecd12cebaebf43352a34053b500af5"},"cell_type":"code","source":"loss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\nacc_values = history_dict['acc']\nval_acc_values = history_dict['val_acc']\n\nepochs = range(1,len(acc_values)+1)\n\nplt.plot(epochs, loss_values, 'bo', label='Training loss')\nplt.plot(epochs, val_loss_values, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}