Created
July 29, 2020 13:45
-
-
Save harshraj22/3269d336952214f45fa288014e2beb7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", | |
| "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5" | |
| }, | |
| "outputs": [], | |
| "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", | |
| "\n", | |
| "import numpy as np # linear algebra\n", | |
| "import 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", | |
| "\n", | |
| "import os\n", | |
| "for 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 5GB 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" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "!pip install tensorflow" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", | |
| "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" | |
| }, | |
| "outputs": [ | |
| { | |
| "ename": "ModuleNotFoundError", | |
| "evalue": "No module named 'tensorflow'", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[1;32m<ipython-input-2-a555c29f7f7c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mdata_tr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"/kaggle/input/digit-recognizer/train.csv\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mdata_t\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"/kaggle/input/digit-recognizer/test.csv\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import tensorflow as tf\n", | |
| "\n", | |
| "data_tr = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n", | |
| "data_t = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "print(data_t)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "data_train = data_tr.loc[:,\"pixel0\":]\n", | |
| "data_train_label = data_tr.loc[:,\"label\"]\n", | |
| "\n", | |
| "data_test = data_t.loc[:,:]\n", | |
| "# data_test_label = data_t.loc[:,\"label\"]\n", | |
| "data_train = data_train/255\n", | |
| "data_test = data_test/255" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "data_train = np.array(data_train)\n", | |
| "data_train_label = np.array(data_train_label)\n", | |
| "data_train = data_train.reshape(data_train.shape[0],28,28,1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "model = tf.keras.models.Sequential([\n", | |
| " tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(28,28,1)),\n", | |
| " tf.keras.layers.MaxPooling2D(2,2),\n", | |
| " tf.keras.layers.Conv2D(16,(3,3),activation='relu'),\n", | |
| " tf.keras.layers.MaxPooling2D(2,2),\n", | |
| " tf.keras.layers.Conv2D(32,(3,3),activation='relu'),\n", | |
| " tf.keras.layers.MaxPooling2D(2,2),\n", | |
| " tf.keras.layers.Flatten(),\n", | |
| " tf.keras.layers.Dense(512,activation='relu'),\n", | |
| " tf.keras.layers.Dropout(0.2),\n", | |
| " tf.keras.layers.Dense(10,activation='softmax')\n", | |
| "])\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "model.summary()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from keras.utils.np_utils import to_categorical\n", | |
| "\n", | |
| "model.compile(optimizer = \"adam\",\n", | |
| " loss = tf.losses.categorical_crossentropy,\n", | |
| " metrics = ['accuracy'])\n", | |
| "\n", | |
| "data_train_label = to_categorical(data_train_label)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "\n", | |
| "history = model.fit(data_train,data_train_label,epochs = 25,verbose=1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "NameError", | |
| "evalue": "name 'data_test' is not defined", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[1;32m<ipython-input-3-550caf48d274>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdata_test\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mdata_test\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata_test\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata_test\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m28\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m28\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mpred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;31mNameError\u001b[0m: name 'data_test' is not defined" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "data_test = np.array(data_test)\n", | |
| "data_test = data_test.reshape(data_test.shape[0],28,28,1)\n", | |
| "pred = model.predict(data_test)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "predictions_label = []\n", | |
| "\n", | |
| "for i in pred:\n", | |
| " predictions_label.append(np.argmax(i))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "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.7.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment