{"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_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-06T22:41:50.464790Z","iopub.execute_input":"2023-11-06T22:41:50.465284Z","iopub.status.idle":"2023-11-06T22:41:50.879184Z","shell.execute_reply.started":"2023-11-06T22:41:50.465254Z","shell.execute_reply":"2023-11-06T22:41:50.878096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:41:50.881109Z","iopub.execute_input":"2023-11-06T22:41:50.881540Z","iopub.status.idle":"2023-11-06T22:41:54.453545Z","shell.execute_reply.started":"2023-11-06T22:41:50.881510Z","shell.execute_reply":"2023-11-06T22:41:54.452732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:41:54.457522Z","iopub.execute_input":"2023-11-06T22:41:54.458724Z","iopub.status.idle":"2023-11-06T22:41:58.796246Z","shell.execute_reply.started":"2023-11-06T22:41:54.458680Z","shell.execute_reply":"2023-11-06T22:41:58.795202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:41:58.798586Z","iopub.execute_input":"2023-11-06T22:41:58.798920Z","iopub.status.idle":"2023-11-06T22:42:01.433830Z","shell.execute_reply.started":"2023-11-06T22:41:58.798893Z","shell.execute_reply":"2023-11-06T22:42:01.432682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.drop('label', axis = 1)\nY_train = train_df.label.to_numpy()\nX_train /= 255\nX_train = X_train.to_numpy()\n\nx_train = []\nx_test = []\n\nX_test = test_df\nX_test /= 255\nX_test = X_test.to_numpy()\n\nfor i in range(len(X_train)):\n    x_train.append(X_train[i].reshape((28,28)).astype(np.float32))\n\nfor i in range(len(X_test)):\n    x_test.append(X_test[i].reshape((28,28)).astype(np.float32))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:01.434972Z","iopub.execute_input":"2023-11-06T22:42:01.435303Z","iopub.status.idle":"2023-11-06T22:42:01.996934Z","shell.execute_reply.started":"2023-11-06T22:42:01.435275Z","shell.execute_reply":"2023-11-06T22:42:01.995531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\nx_test = np.array(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:01.999372Z","iopub.execute_input":"2023-11-06T22:42:01.999690Z","iopub.status.idle":"2023-11-06T22:42:02.156296Z","shell.execute_reply.started":"2023-11-06T22:42:01.999662Z","shell.execute_reply":"2023-11-06T22:42:02.155269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.157481Z","iopub.execute_input":"2023-11-06T22:42:02.157834Z","iopub.status.idle":"2023-11-06T22:42:02.162936Z","shell.execute_reply.started":"2023-11-06T22:42:02.157795Z","shell.execute_reply":"2023-11-06T22:42:02.161765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[10])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.164521Z","iopub.execute_input":"2023-11-06T22:42:02.164896Z","iopub.status.idle":"2023-11-06T22:42:02.445728Z","shell.execute_reply.started":"2023-11-06T22:42:02.164867Z","shell.execute_reply":"2023-11-06T22:42:02.444540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class NN(nn.Module):\n#     def __init__(self, num_inputs, num_classes=10):\n#         super(NN, self).__init__()\n#         self.fc1 = nn.Linear(num_inputs, 512)\n#         self.fc2 = nn.Linear(512, 128)\n#         self.fc3 = nn.Linear(128, 64)\n#         self.fc4 = nn.Linear(64, 10)\n    \n#     def forward(self, x):\n#         x = F.relu(self.fc1(x))\n#         x = F.relu(self.fc2(x))\n#         x = F.relu(self.fc3(x))\n#         x = self.fc4(x)\n        \n#         return x\n\nclass CNN(nn.Module):\n    def __init__(self, in_channels, num_classes):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(in_channels, out_channels=32, kernel_size=5)\n        self.pool = nn.MaxPool2d(kernel_size=2)\n        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)\n        self.dropout = nn.Dropout(0.25)\n        self.fc1 = nn.Linear(in_features=1600, out_features=16)\n        self.fc2 = nn.Linear(in_features=16, out_features=num_classes)\n    \n    def forward(self, x):\n        x = F.relu(self.conv1(x))\n        x = self.pool(x)\n        x = F.relu(self.conv2(x))\n        x = self.pool(x)\n        \n        x = self.dropout(x)\n        \n        x = x.reshape(x.shape[0], -1)\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.447197Z","iopub.execute_input":"2023-11-06T22:42:02.447632Z","iopub.status.idle":"2023-11-06T22:42:02.458539Z","shell.execute_reply.started":"2023-11-06T22:42:02.447591Z","shell.execute_reply":"2023-11-06T22:42:02.457437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_batch(x_data, y_data, batch_size = 32):\n    i = 0\n    batch = []\n    while(i+batch_size<len(x_data)):\n        x = torch.from_numpy(x_data[i : i+batch_size])\n        y = torch.from_numpy(y_data[i : i+batch_size])\n        i += batch_size\n        batch.append([x,y])\n    x = torch.from_numpy(x_data[i : ])\n    y = torch.from_numpy(y_data[i : ])\n    batch.append([x, y])\n    \n    return batch","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.462254Z","iopub.execute_input":"2023-11-06T22:42:02.462582Z","iopub.status.idle":"2023-11-06T22:42:02.475949Z","shell.execute_reply.started":"2023-11-06T22:42:02.462535Z","shell.execute_reply":"2023-11-06T22:42:02.474981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\nepochs = 25\nbatch_size = 32\nlr = 0.01","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.476951Z","iopub.execute_input":"2023-11-06T22:42:02.477229Z","iopub.status.idle":"2023-11-06T22:42:02.487292Z","shell.execute_reply.started":"2023-11-06T22:42:02.477203Z","shell.execute_reply":"2023-11-06T22:42:02.486585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batch = create_batch(x_train, Y_train, batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.488474Z","iopub.execute_input":"2023-11-06T22:42:02.488763Z","iopub.status.idle":"2023-11-06T22:42:02.516973Z","shell.execute_reply.started":"2023-11-06T22:42:02.488739Z","shell.execute_reply":"2023-11-06T22:42:02.515902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_test_batch(x_data, batch_size):\n    i = 0\n    batch = []\n    while(i+batch_size<len(x_data)):\n        x = torch.from_numpy(x_data[i : i+batch_size])\n        i += batch_size\n        batch.append(x)\n    x = torch.from_numpy(x_data[i : ])\n    batch.append(x)\n    \n    return batch","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.518810Z","iopub.execute_input":"2023-11-06T22:42:02.519229Z","iopub.status.idle":"2023-11-06T22:42:02.525890Z","shell.execute_reply.started":"2023-11-06T22:42:02.519182Z","shell.execute_reply":"2023-11-06T22:42:02.524771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batch = create_test_batch(x_test, batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.527852Z","iopub.execute_input":"2023-11-06T22:42:02.528267Z","iopub.status.idle":"2023-11-06T22:42:02.543364Z","shell.execute_reply.started":"2023-11-06T22:42:02.528228Z","shell.execute_reply":"2023-11-06T22:42:02.542279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CNN(1, 10).to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.544544Z","iopub.execute_input":"2023-11-06T22:42:02.544893Z","iopub.status.idle":"2023-11-06T22:42:02.587280Z","shell.execute_reply.started":"2023-11-06T22:42:02.544857Z","shell.execute_reply":"2023-11-06T22:42:02.586095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = []\n\ndef get_accuracy(model, data):\n    model.eval()\n\n    with torch.no_grad():\n        for x in data:\n            x = x.to(device)\n            \n            x = x.reshape(32, 1, 28, 28)\n\n            scores = model(x)\n            _, predictions = scores.max(1)\n            results.append(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.588636Z","iopub.execute_input":"2023-11-06T22:42:02.588974Z","iopub.status.idle":"2023-11-06T22:42:02.597311Z","shell.execute_reply.started":"2023-11-06T22:42:02.588947Z","shell.execute_reply":"2023-11-06T22:42:02.596224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(epochs):\n    \n    running_loss = 0.0\n    for x, y in train_batch:\n        x = x.to(device)\n        y = y.to(device)\n        \n        x = x.reshape(x.shape[0], 1, 28, 28)\n        \n        \n        scores = model(x)\n        loss = criterion(scores, y)\n        \n        running_loss += loss\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    \n    print(f\"Epoch-{epoch+1} | Training Loss {running_loss/ len(train_batch)}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-06T22:42:02.599374Z","iopub.execute_input":"2023-11-06T22:42:02.599717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_accuracy(model, test_batch)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = []\nfor i in results:\n    for j in i:\n        y_preds.append(j.item())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index=[]\nfor i in range(1,28001):\n    index.append(i)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ndf","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Label'] = y_preds\ndf.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}