{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nfrom tensorflow.keras import  utils\n\n","execution_count":4,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load the data\ntrain = pd.read_csv(\"../input/train.csv\")\nY_train = train[\"label\"]\ntraining_images = train.drop(labels = [\"label\"],axis = 1) \ntraining_images=training_images.values.reshape(-1, 28, 28, 1)\ntraining_images=training_images / 255.0\n","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create model\nmodel = tf.keras.models.Sequential([\n  tf.keras.layers.Conv2D(8, (7,7),strides=(1, 1), activation='relu', input_shape=(28, 28, 1)),\n  tf.keras.layers.MaxPooling2D(5, 5),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(10, activation='softmax')\n])\n\n#Optimizer + summary of the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":6,"outputs":[{"output_type":"stream","text":"_________________________________________________________________\nLayer (type)                 Output Shape              Param #   \n=================================================================\nconv2d_1 (Conv2D)            (None, 22, 22, 8)         400       \n_________________________________________________________________\nmax_pooling2d_1 (MaxPooling2 (None, 4, 4, 8)           0         \n_________________________________________________________________\nflatten_1 (Flatten)          (None, 128)               0         \n_________________________________________________________________\ndense_1 (Dense)              (None, 10)                1290      \n=================================================================\nTotal params: 1,690\nTrainable params: 1,690\nNon-trainable params: 0\n_________________________________________________________________\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train the model\nmodel.fit(training_images, Y_train, epochs=30)\n","execution_count":null,"outputs":[{"output_type":"stream","text":"Epoch 1/30\n42000/42000 [==============================] - 9s 217us/sample - loss: 0.4611 - acc: 0.8712\nEpoch 2/30\n42000/42000 [==============================] - 6s 150us/sample - loss: 0.1336 - acc: 0.9603\nEpoch 3/30\n42000/42000 [==============================] - 6s 149us/sample - loss: 0.1052 - acc: 0.9680\nEpoch 4/30\n42000/42000 [==============================] - 6s 150us/sample - loss: 0.0897 - acc: 0.9732\nEpoch 5/30\n42000/42000 [==============================] - 6s 149us/sample - loss: 0.0804 - acc: 0.9749\nEpoch 6/30\n42000/42000 [==============================] - 6s 148us/sample - loss: 0.0736 - acc: 0.9769\nEpoch 7/30\n42000/42000 [==============================] - 6s 149us/sample - loss: 0.0686 - acc: 0.9783\nEpoch 8/30\n24576/42000 [================>.............] - ETA: 2s - loss: 0.0642 - acc: 0.9799","name":"stdout"}]},{"metadata":{"trusted":false},"cell_type":"code","source":"import numpy as np\ntest = pd.read_csv(\"../input/test.csv\")\ntest = test / 255.0\ntest=test.values.reshape(-1,28,28,1)\nresults = model.predict(test)\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"model.save('mmymnist.h5')\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.4"}},"nbformat":4,"nbformat_minor":1}