{"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-07-21T22:05:57.64118Z","iopub.execute_input":"2023-07-21T22:05:57.641893Z","iopub.status.idle":"2023-07-21T22:05:57.688462Z","shell.execute_reply.started":"2023-07-21T22:05:57.641857Z","shell.execute_reply":"2023-07-21T22:05:57.687573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# How gradients could be computed in PyTorch","metadata":{}},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:06:03.585467Z","iopub.execute_input":"2023-07-21T22:06:03.58592Z","iopub.status.idle":"2023-07-21T22:06:06.856994Z","shell.execute_reply.started":"2023-07-21T22:06:03.585873Z","shell.execute_reply":"2023-07-21T22:06:06.855833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= torch.tensor([1,2,3.0,4,5],dtype=torch.float64,requires_grad=True)\nx","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:11:52.368263Z","iopub.execute_input":"2023-07-21T19:11:52.368994Z","iopub.status.idle":"2023-07-21T19:11:52.440491Z","shell.execute_reply.started":"2023-07-21T19:11:52.368956Z","shell.execute_reply":"2023-07-21T19:11:52.439037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_without= torch.tensor([1,2,3.0,4,5],dtype=torch.float64)\nx_without","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:14.063672Z","iopub.execute_input":"2023-07-21T19:12:14.064135Z","iopub.status.idle":"2023-07-21T19:12:14.073926Z","shell.execute_reply.started":"2023-07-21T19:12:14.064089Z","shell.execute_reply":"2023-07-21T19:12:14.072735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.grad)\nprint(x_without.grad)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:13:13.9159Z","iopub.execute_input":"2023-07-21T19:13:13.917026Z","iopub.status.idle":"2023-07-21T19:13:13.922562Z","shell.execute_reply.started":"2023-07-21T19:13:13.916985Z","shell.execute_reply":"2023-07-21T19:13:13.921601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y,y_without= torch.sum(x**2),torch.sum(x_without**2)\ny.backward()\ny_without.backward()   # Error!","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:14:25.548259Z","iopub.execute_input":"2023-07-21T19:14:25.548652Z","iopub.status.idle":"2023-07-21T19:14:25.643231Z","shell.execute_reply.started":"2023-07-21T19:14:25.548613Z","shell.execute_reply":"2023-07-21T19:14:25.641475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.grad)\nprint(x_without.grad)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:14:56.455413Z","iopub.execute_input":"2023-07-21T19:14:56.455798Z","iopub.status.idle":"2023-07-21T19:14:56.462952Z","shell.execute_reply.started":"2023-07-21T19:14:56.455768Z","shell.execute_reply":"2023-07-21T19:14:56.461656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Let's make it more complex!\n\nx.grad= None #Try removing and run cells multiple times, what do you conclude?\ny= torch.sum(x**2 + 0.5* x**4 + 100*x) # Gradient is 2x + 2x^3 + 100\ny.backward()\nprint(x)\nprint(x.grad)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:23:37.810642Z","iopub.execute_input":"2023-07-21T19:23:37.811039Z","iopub.status.idle":"2023-07-21T19:23:37.82138Z","shell.execute_reply.started":"2023-07-21T19:23:37.811008Z","shell.execute_reply":"2023-07-21T19:23:37.819832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building a Neural Network for classification! ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom torch.utils.data import Dataset\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:06:11.870968Z","iopub.execute_input":"2023-07-21T22:06:11.872783Z","iopub.status.idle":"2023-07-21T22:06:11.878865Z","shell.execute_reply.started":"2023-07-21T22:06:11.872733Z","shell.execute_reply":"2023-07-21T22:06:11.877503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MnistDataset(Dataset):\n    def __init__(self,path):\n        super(MnistDataset).__init__()\n        self.dataset= pd.read_csv(path)\n#         print(self.dataset.head())\n    def __getitem__(self,idx):\n        item= self.dataset.iloc[idx]\n        data= item.to_numpy()\n        try:\n            label= item['label']\n            data= data[1:]\n        except:\n            label=None\n        rows= int(len(data)**0.5)\n        data= data.reshape((rows,-1))\n        data= torch.from_numpy(data)\n        data=data.type(torch.float32)\n        return data,label\n    def __len__(self):\n        return len(self.dataset)\ndataset=MnistDataset('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:06:14.100665Z","iopub.execute_input":"2023-07-21T22:06:14.101091Z","iopub.status.idle":"2023-07-21T22:06:18.200335Z","shell.execute_reply.started":"2023-07-21T22:06:14.101058Z","shell.execute_reply":"2023-07-21T22:06:18.198965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(dataset))\ndata,label= dataset[5000]\nplt.imshow(data)\nprint(label)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:06:18.733106Z","iopub.execute_input":"2023-07-21T22:06:18.733543Z","iopub.status.idle":"2023-07-21T22:06:19.010749Z","shell.execute_reply.started":"2023-07-21T22:06:18.733509Z","shell.execute_reply":"2023-07-21T22:06:19.00933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Generate a dataloader\ndataloader= torch.utils.data.DataLoader(dataset,256,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:06:21.5402Z","iopub.execute_input":"2023-07-21T22:06:21.540653Z","iopub.status.idle":"2023-07-21T22:06:21.548557Z","shell.execute_reply.started":"2023-07-21T22:06:21.54061Z","shell.execute_reply":"2023-07-21T22:06:21.547093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Now let's make out NN\nclass MnistNet(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.fc1= torch.nn.Linear(784,512)\n        self.fc2= torch.nn.Linear(512,16)\n        self.fc3= torch.nn.Linear(16,10)\n    def forward(self,x):\n        x= x.reshape((-1,784))\n        x= torch.nn.functional.sigmoid(self.fc1(x))\n        x= torch.nn.functional.sigmoid(self.fc2(x))\n        x= torch.nn.functional.softmax(self.fc3(x),1)\n        return x\nmodel= MnistNet()\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:25:27.515204Z","iopub.execute_input":"2023-07-21T22:25:27.515654Z","iopub.status.idle":"2023-07-21T22:25:27.527943Z","shell.execute_reply.started":"2023-07-21T22:25:27.515624Z","shell.execute_reply":"2023-07-21T22:25:27.526689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Now define the loss and optimizer\nloss= torch.nn.CrossEntropyLoss()\noptimizer= torch.optim.Adam(model.parameters(),lr=1E-6)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:34:11.566048Z","iopub.execute_input":"2023-07-21T22:34:11.566569Z","iopub.status.idle":"2023-07-21T22:34:11.572692Z","shell.execute_reply.started":"2023-07-21T22:34:11.56653Z","shell.execute_reply":"2023-07-21T22:34:11.571381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses= []\naccs= []","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:25:27.537656Z","iopub.execute_input":"2023-07-21T22:25:27.538049Z","iopub.status.idle":"2023-07-21T22:25:27.54782Z","shell.execute_reply.started":"2023-07-21T22:25:27.538015Z","shell.execute_reply":"2023-07-21T22:25:27.546477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs= 10\nfor i in range(epochs):\n    totalLoss= 0\n    acc=0\n    for X,labels in dataloader:\n        optimizer.zero_grad()\n        predictions= model(X)\n        preds=torch.argmax(predictions,1)\n        acc+= torch.sum(preds == labels)\n        loss_value=loss(predictions,labels)\n        totalLoss+=(float(loss_value))/X.shape[0]\n        loss_value.backward()\n        optimizer.step()\n    print(f'Epoch {i+1}: Loss= {totalLoss} \\t Accuraccy={acc/len(dataset) *100}%')\n    losses.append(totalLoss)\nplt.plot(losses)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:34:13.365786Z","iopub.execute_input":"2023-07-21T22:34:13.366501Z","iopub.status.idle":"2023-07-21T22:35:10.767604Z","shell.execute_reply.started":"2023-07-21T22:34:13.366458Z","shell.execute_reply":"2023-07-21T22:35:10.766438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Let's test!\ntest_dataset= MnistDataset('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:36:54.006521Z","iopub.execute_input":"2023-07-21T22:36:54.007033Z","iopub.status.idle":"2023-07-21T22:36:56.121279Z","shell.execute_reply.started":"2023-07-21T22:36:54.006993Z","shell.execute_reply":"2023-07-21T22:36:56.120051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"while True:\n    inputt= int(input('Enter a number: ')) % len(test_dataset)\n    data,label= test_dataset[inputt]\n    predictedNumber= torch.argmax(model(data))\n    print(f'Predicted is {predictedNumber}')\n    plt.imshow(data)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T22:39:16.42492Z","iopub.execute_input":"2023-07-21T22:39:16.425342Z","iopub.status.idle":"2023-07-21T22:40:53.380914Z","shell.execute_reply.started":"2023-07-21T22:39:16.425312Z","shell.execute_reply":"2023-07-21T22:40:53.379333Z"},"trusted":true},"execution_count":null,"outputs":[]}]}