{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"know dataset"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint('train shape:',train_df.shape)\nprint('test shape:',test_df.shape)\nprint(train_df['label'].value_counts())\nnp.random.seed(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = train_df.drop(['label'],axis = 1).values.reshape(train_df.shape[0],28,28,1)\ny_train = train_df['label'].copy()\nx_test = test_df.values.reshape(test_df.shape[0],28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"shuffled_indices = np.random.permutation(42000)\nx_train = x_train[shuffled_indices]\ny_train = y_train[shuffled_indices]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,x_valid = x_train[:35200],x_train[35200:]\ny_train,y_valid = y_train[:35200],y_train[35200:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.Sequential()\nmodel.add(layers.Conv2D(64,(7,7),input_shape=(28,28,1),padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2))\nmodel.add(layers.Conv2D(128,(3,3),padding='same',activation='relu'))\nmodel.add(layers.Conv2D(128,(3,3),padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2))\nmodel.add(layers.Conv2D(256,(3,3),padding='same',activation='relu'))\nmodel.add(layers.Conv2D(256,(3,3),padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(units=128,activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(units=64,activation = 'relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(units=10,activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='nadam',loss = 'sparse_categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint\n# filepath = \"./model.h5\"\n# checkpoint = ModelCheckpoint(filepath, monitor='accuracy', verbose=1, \n#                              save_best_only=True, mode='max')\n\n# reduce_lr = ReduceLROnPlateau(monitor='accuracy', factor=0.5, patience=2, \n#                                    verbose=1, mode='max', min_lr=0.00001)\n                              \n                              \n# callbacks_list = [checkpoint, reduce_lr]\n# history = model.fit(x_train, y_train, epochs=30, batch_size=64, validation_data=(x_valid, y_valid), \n#                      verbose=1, callbacks=callbacks_list)\nhistory = model.fit(x_train, y_train, epochs=30, batch_size=64, validation_data=(x_valid, y_valid), \n                     verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history.history","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(x_train[5].reshape(28,28))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.predict_classes(x_train[5].reshape(1,28,28,1))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}