{"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":"2022-12-08T10:15:27.783369Z","iopub.execute_input":"2022-12-08T10:15:27.783799Z","iopub.status.idle":"2022-12-08T10:15:27.793816Z","shell.execute_reply.started":"2022-12-08T10:15:27.783767Z","shell.execute_reply":"2022-12-08T10:15:27.792921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest=pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nsubmission=pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:27.799399Z","iopub.execute_input":"2022-12-08T10:15:27.799842Z","iopub.status.idle":"2022-12-08T10:15:31.589315Z","shell.execute_reply.started":"2022-12-08T10:15:27.799807Z","shell.execute_reply":"2022-12-08T10:15:31.588134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label=train['label']","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.591628Z","iopub.execute_input":"2022-12-08T10:15:31.592106Z","iopub.status.idle":"2022-12-08T10:15:31.598899Z","shell.execute_reply.started":"2022-12-08T10:15:31.592061Z","shell.execute_reply":"2022-12-08T10:15:31.597610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.drop('label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.600974Z","iopub.execute_input":"2022-12-08T10:15:31.601724Z","iopub.status.idle":"2022-12-08T10:15:31.730200Z","shell.execute_reply.started":"2022-12-08T10:15:31.601674Z","shell.execute_reply":"2022-12-08T10:15:31.729238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.to_numpy('float32')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.732417Z","iopub.execute_input":"2022-12-08T10:15:31.733076Z","iopub.status.idle":"2022-12-08T10:15:31.833252Z","shell.execute_reply.started":"2022-12-08T10:15:31.733041Z","shell.execute_reply":"2022-12-08T10:15:31.831756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label=label.to_numpy('int32')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.834750Z","iopub.execute_input":"2022-12-08T10:15:31.835325Z","iopub.status.idle":"2022-12-08T10:15:31.842476Z","shell.execute_reply.started":"2022-12-08T10:15:31.835291Z","shell.execute_reply":"2022-12-08T10:15:31.841163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.843730Z","iopub.execute_input":"2022-12-08T10:15:31.844136Z","iopub.status.idle":"2022-12-08T10:15:31.855023Z","shell.execute_reply.started":"2022-12-08T10:15:31.844093Z","shell.execute_reply":"2022-12-08T10:15:31.853676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.856585Z","iopub.execute_input":"2022-12-08T10:15:31.856936Z","iopub.status.idle":"2022-12-08T10:15:31.865866Z","shell.execute_reply.started":"2022-12-08T10:15:31.856903Z","shell.execute_reply":"2022-12-08T10:15:31.864766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Mydata(Dataset):\n    def __init__(self,t,l):\n        self.t=t.reshape(-1,1,28,28)\n        self.l=l.reshape(-1)\n    def __len__(self):\n        return len(self.t[0])\n    def __getitem__(self,i):\n        x=torch.tensor(self.t[i]).float()\n        y=torch.tensor(self.l[i]).float()\n        return x,y","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.867057Z","iopub.execute_input":"2022-12-08T10:15:31.867527Z","iopub.status.idle":"2022-12-08T10:15:31.882732Z","shell.execute_reply.started":"2022-12-08T10:15:31.867484Z","shell.execute_reply":"2022-12-08T10:15:31.881271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mydata=Mydata(train,label)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.884667Z","iopub.execute_input":"2022-12-08T10:15:31.885545Z","iopub.status.idle":"2022-12-08T10:15:31.897082Z","shell.execute_reply.started":"2022-12-08T10:15:31.885492Z","shell.execute_reply":"2022-12-08T10:15:31.895717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y=mydata[0]\nprint(x.shape)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.902512Z","iopub.execute_input":"2022-12-08T10:15:31.903332Z","iopub.status.idle":"2022-12-08T10:15:31.911529Z","shell.execute_reply.started":"2022-12-08T10:15:31.903283Z","shell.execute_reply":"2022-12-08T10:15:31.910537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nclass CNN(nn.Module):\n    def __init__(self):\n        super(CNN,self).__init__()\n        self.conv1=nn.Conv2d(1,16,5,1,2)\n        self.conv2=nn.Conv2d(16,32,5,1,2)\n        self.maxpool=nn.MaxPool2d(kernel_size=2)\n        self.linear=nn.Linear(32*14*14,10)\n        self.relu=nn.ReLU()\n    def forward(self,x):\n        x=self.relu(self.conv1(x))\n        x=self.relu(self.conv2(x))\n        x=self.relu(self.maxpool(x))\n        x=x.view(x.size(0),-1)\n        x=self.linear(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.912857Z","iopub.execute_input":"2022-12-08T10:15:31.914097Z","iopub.status.idle":"2022-12-08T10:15:31.924482Z","shell.execute_reply.started":"2022-12-08T10:15:31.914045Z","shell.execute_reply":"2022-12-08T10:15:31.923196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn=CNN()","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.926039Z","iopub.execute_input":"2022-12-08T10:15:31.927056Z","iopub.status.idle":"2022-12-08T10:15:31.939601Z","shell.execute_reply.started":"2022-12-08T10:15:31.927010Z","shell.execute_reply":"2022-12-08T10:15:31.938111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\nfrom torch.utils.data import DataLoader\nimport torchvision\ntrain_dataset=mydata\noptimizer = torch.optim.SGD(cnn.parameters(), lr=0.02)\nloss_func = torch.nn.CrossEntropyLoss() \ntrain_dataloader=DataLoader(train_dataset,batch_size=20,shuffle=True,num_workers=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:15:31.941423Z","iopub.execute_input":"2022-12-08T10:15:31.942213Z","iopub.status.idle":"2022-12-08T10:15:31.953626Z","shell.execute_reply.started":"2022-12-08T10:15:31.942164Z","shell.execute_reply":"2022-12-08T10:15:31.952122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(100):\n    for step,(x,target) in enumerate(train_dataloader):\n        cnn_x=cnn(x)\n        target=torch.tensor(target).long()\n        loss=loss_func(cnn_x,target)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        if step % 200 == 0:\n            print(' : {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}'.format(\n            epoch, step * len(x), len(train_dataloader.dataset),\n            100. * step / len(train_dataloader), loss.item()))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:18:43.327132Z","iopub.execute_input":"2022-12-08T10:18:43.327588Z","iopub.status.idle":"2022-12-08T10:18:49.412109Z","shell.execute_reply.started":"2022-12-08T10:18:43.327553Z","shell.execute_reply":"2022-12-08T10:18:49.410720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:24.236849Z","iopub.execute_input":"2022-12-08T10:17:24.238221Z","iopub.status.idle":"2022-12-08T10:17:24.264898Z","shell.execute_reply.started":"2022-12-08T10:17:24.238174Z","shell.execute_reply":"2022-12-08T10:17:24.263498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop('id',axis=1,inplace=True)\ntest","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:28.948182Z","iopub.execute_input":"2022-12-08T10:17:28.948597Z","iopub.status.idle":"2022-12-08T10:17:28.993149Z","shell.execute_reply.started":"2022-12-08T10:17:28.948564Z","shell.execute_reply":"2022-12-08T10:17:28.991863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test.to_numpy('float32')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:34.733252Z","iopub.execute_input":"2022-12-08T10:17:34.733700Z","iopub.status.idle":"2022-12-08T10:17:34.762001Z","shell.execute_reply.started":"2022-12-08T10:17:34.733662Z","shell.execute_reply":"2022-12-08T10:17:34.760377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=torch.tensor(test)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:35.835405Z","iopub.execute_input":"2022-12-08T10:17:35.835912Z","iopub.status.idle":"2022-12-08T10:17:35.845971Z","shell.execute_reply.started":"2022-12-08T10:17:35.835859Z","shell.execute_reply":"2022-12-08T10:17:35.844615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:40.557400Z","iopub.execute_input":"2022-12-08T10:17:40.557813Z","iopub.status.idle":"2022-12-08T10:17:40.569022Z","shell.execute_reply.started":"2022-12-08T10:17:40.557783Z","shell.execute_reply":"2022-12-08T10:17:40.567767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=cnn(test.reshape(-1,1,28,28))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:45.664785Z","iopub.execute_input":"2022-12-08T10:17:45.665662Z","iopub.status.idle":"2022-12-08T10:17:48.102144Z","shell.execute_reply.started":"2022-12-08T10:17:45.665617Z","shell.execute_reply":"2022-12-08T10:17:48.100947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=pred.argmax(1)\npred=pred.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:17:52.099791Z","iopub.execute_input":"2022-12-08T10:17:52.100656Z","iopub.status.idle":"2022-12-08T10:17:52.186827Z","shell.execute_reply.started":"2022-12-08T10:17:52.100604Z","shell.execute_reply":"2022-12-08T10:17:52.185818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.reshape(-1)\nsubmission['label']=pred.reshape(-1)\nsubmission.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T10:19:18.783815Z","iopub.execute_input":"2022-12-08T10:19:18.784304Z","iopub.status.idle":"2022-12-08T10:19:18.800136Z","shell.execute_reply.started":"2022-12-08T10:19:18.784262Z","shell.execute_reply":"2022-12-08T10:19:18.799147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}