{"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":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport torch \nimport torch.nn as nn\nimport torch.optim as optim \nimport torchmetrics \nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-21T11:06:42.464457Z","iopub.execute_input":"2022-11-21T11:06:42.464790Z","iopub.status.idle":"2022-11-21T11:06:47.144395Z","shell.execute_reply.started":"2022-11-21T11:06:42.464697Z","shell.execute_reply":"2022-11-21T11:06:47.143286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:06:48.836571Z","iopub.execute_input":"2022-11-21T11:06:48.837390Z","iopub.status.idle":"2022-11-21T11:06:51.926473Z","shell.execute_reply.started":"2022-11-21T11:06:48.837355Z","shell.execute_reply":"2022-11-21T11:06:51.925480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:08:00.600018Z","iopub.execute_input":"2022-11-21T11:08:00.600462Z","iopub.status.idle":"2022-11-21T11:08:02.934878Z","shell.execute_reply.started":"2022-11-21T11:08:00.600417Z","shell.execute_reply":"2022-11-21T11:08:02.933819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:08:10.676953Z","iopub.execute_input":"2022-11-21T11:08:10.677369Z","iopub.status.idle":"2022-11-21T11:08:10.708033Z","shell.execute_reply.started":"2022-11-21T11:08:10.677335Z","shell.execute_reply":"2022-11-21T11:08:10.707149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_df[\"label\"].to_numpy()\ntrain_X = train_df.iloc[:,1:].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:10:39.079318Z","iopub.execute_input":"2022-11-21T11:10:39.079680Z","iopub.status.idle":"2022-11-21T11:10:39.084747Z","shell.execute_reply.started":"2022-11-21T11:10:39.079650Z","shell.execute_reply":"2022-11-21T11:10:39.083853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset_X, testset_X, trainset_y, testset_y = train_test_split(train_X, train_y, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:13:06.899812Z","iopub.execute_input":"2022-11-21T11:13:06.900493Z","iopub.status.idle":"2022-11-21T11:13:07.278317Z","shell.execute_reply.started":"2022-11-21T11:13:06.900455Z","shell.execute_reply":"2022-11-21T11:13:07.277306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset_X.shape ","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:13:13.604388Z","iopub.execute_input":"2022-11-21T11:13:13.604872Z","iopub.status.idle":"2022-11-21T11:13:13.613426Z","shell.execute_reply.started":"2022-11-21T11:13:13.604824Z","shell.execute_reply":"2022-11-21T11:13:13.612292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_example = trainset_X[43,:].reshape(28,-1)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:14:12.790795Z","iopub.execute_input":"2022-11-21T11:14:12.791154Z","iopub.status.idle":"2022-11-21T11:14:12.796120Z","shell.execute_reply.started":"2022-11-21T11:14:12.791124Z","shell.execute_reply":"2022-11-21T11:14:12.794918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(one_example, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:14:13.092241Z","iopub.execute_input":"2022-11-21T11:14:13.092868Z","iopub.status.idle":"2022-11-21T11:14:13.288681Z","shell.execute_reply.started":"2022-11-21T11:14:13.092825Z","shell.execute_reply":"2022-11-21T11:14:13.287781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_tensor = torch.from_numpy(np.array([1, 2])).float().requires_grad_()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:21:01.401544Z","iopub.execute_input":"2022-11-21T11:21:01.401917Z","iopub.status.idle":"2022-11-21T11:21:01.407387Z","shell.execute_reply.started":"2022-11-21T11:21:01.401886Z","shell.execute_reply":"2022-11-21T11:21:01.406250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_tensor = (example_tensor.T)@(example_tensor)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:21:02.294810Z","iopub.execute_input":"2022-11-21T11:21:02.295161Z","iopub.status.idle":"2022-11-21T11:21:02.304392Z","shell.execute_reply.started":"2022-11-21T11:21:02.295132Z","shell.execute_reply":"2022-11-21T11:21:02.303367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_tensor.backward()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:21:03.721210Z","iopub.execute_input":"2022-11-21T11:21:03.721563Z","iopub.status.idle":"2022-11-21T11:21:03.796194Z","shell.execute_reply.started":"2022-11-21T11:21:03.721533Z","shell.execute_reply":"2022-11-21T11:21:03.795240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_tensor","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:21:11.001240Z","iopub.execute_input":"2022-11-21T11:21:11.001650Z","iopub.status.idle":"2022-11-21T11:21:11.013315Z","shell.execute_reply.started":"2022-11-21T11:21:11.001617Z","shell.execute_reply":"2022-11-21T11:21:11.012397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_tensor.grad ","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:22:10.617042Z","iopub.execute_input":"2022-11-21T11:22:10.617392Z","iopub.status.idle":"2022-11-21T11:22:10.625311Z","shell.execute_reply.started":"2022-11-21T11:22:10.617363Z","shell.execute_reply":"2022-11-21T11:22:10.624179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset_X_tens = torch.from_numpy(trainset_X).float().cuda()\ntrainset_y_tens = torch.from_numpy(trainset_y).long().cuda()\ntestset_X_tens = torch.from_numpy(testset_X).float().cuda()\ntestset_y_tens = torch.from_numpy(testset_y).long().cuda()\n","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:28:23.649494Z","iopub.execute_input":"2022-11-21T11:28:23.649884Z","iopub.status.idle":"2022-11-21T11:28:26.877294Z","shell.execute_reply.started":"2022-11-21T11:28:23.649852Z","shell.execute_reply":"2022-11-21T11:28:26.876309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LEARNING_RATE = 0.001 \nN_EPOCHS = 400 \nL2_REG = 0.01\n\nmodel = nn.Sequential(\n                        nn.Linear(784, 256),\n                        nn.ReLU(), \n                        nn.Linear(256, 10), \n).cuda()\n\nlossfn = nn.CrossEntropyLoss()\n\noptimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=L2_REG)\n\nacc = torchmetrics.Accuracy(n_classes=10).to('cuda')\n\ncosts = []\naccuracies = []\ntest_costs = []\ntest_accuracies = []","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:09.615602Z","iopub.execute_input":"2022-11-21T11:55:09.616285Z","iopub.status.idle":"2022-11-21T11:55:09.628596Z","shell.execute_reply.started":"2022-11-21T11:55:09.616248Z","shell.execute_reply":"2022-11-21T11:55:09.627299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm ","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:10.668404Z","iopub.execute_input":"2022-11-21T11:55:10.670966Z","iopub.status.idle":"2022-11-21T11:55:10.675242Z","shell.execute_reply.started":"2022-11-21T11:55:10.670928Z","shell.execute_reply":"2022-11-21T11:55:10.673976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in tqdm(range(N_EPOCHS)): \n    optimizer.zero_grad()\n    trainset_yhat_tens = model(trainset_X_tens) \n    train_cost_curr = lossfn(trainset_yhat_tens, trainset_y_tens) \n    train_cost_curr.backward()\n    optimizer.step()\n    with torch.no_grad(): \n        costs.append(train_cost_curr.detach().cpu().numpy().item()) \n        accuracies.append(acc(trainset_yhat_tens, trainset_y_tens).cpu().numpy().item())\n        testset_yhat_tens = model(testset_X_tens) \n        test_costs.append(lossfn(testset_yhat_tens, testset_y_tens).cpu().numpy().item()) \n        test_accuracies.append(acc(testset_yhat_tens, testset_y_tens).cpu().numpy().item())","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:11.646068Z","iopub.execute_input":"2022-11-21T11:55:11.646421Z","iopub.status.idle":"2022-11-21T11:55:15.630884Z","shell.execute_reply.started":"2022-11-21T11:55:11.646391Z","shell.execute_reply":"2022-11-21T11:55:15.629891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(costs, color='blue')\nplt.plot(test_costs, color='red')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:15.633049Z","iopub.execute_input":"2022-11-21T11:55:15.633708Z","iopub.status.idle":"2022-11-21T11:55:15.821503Z","shell.execute_reply.started":"2022-11-21T11:55:15.633670Z","shell.execute_reply":"2022-11-21T11:55:15.820642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(accuracies, color='blue')\nplt.plot(test_accuracies, color='red')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:15.822888Z","iopub.execute_input":"2022-11-21T11:55:15.823217Z","iopub.status.idle":"2022-11-21T11:55:16.008421Z","shell.execute_reply.started":"2022-11-21T11:55:15.823183Z","shell.execute_reply":"2022-11-21T11:55:16.007502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies[-1], test_accuracies[-1]","metadata":{"execution":{"iopub.status.busy":"2022-11-21T11:55:16.010454Z","iopub.execute_input":"2022-11-21T11:55:16.012020Z","iopub.status.idle":"2022-11-21T11:55:16.018774Z","shell.execute_reply.started":"2022-11-21T11:55:16.011985Z","shell.execute_reply":"2022-11-21T11:55:16.018034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array([[1, 2, 3],[0,0,0]])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:00:47.259155Z","iopub.execute_input":"2022-11-21T12:00:47.259519Z","iopub.status.idle":"2022-11-21T12:00:47.265196Z","shell.execute_reply.started":"2022-11-21T12:00:47.259487Z","shell.execute_reply":"2022-11-21T12:00:47.264102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = np.array([0,1,0])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:01:07.308608Z","iopub.execute_input":"2022-11-21T12:01:07.309285Z","iopub.status.idle":"2022-11-21T12:01:07.313518Z","shell.execute_reply.started":"2022-11-21T12:01:07.309248Z","shell.execute_reply":"2022-11-21T12:01:07.312598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a*b","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:01:13.978602Z","iopub.execute_input":"2022-11-21T12:01:13.978999Z","iopub.status.idle":"2022-11-21T12:01:13.985849Z","shell.execute_reply.started":"2022-11-21T12:01:13.978960Z","shell.execute_reply":"2022-11-21T12:01:13.984810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a@b","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:02:18.031704Z","iopub.execute_input":"2022-11-21T12:02:18.032078Z","iopub.status.idle":"2022-11-21T12:02:18.041074Z","shell.execute_reply.started":"2022-11-21T12:02:18.032047Z","shell.execute_reply":"2022-11-21T12:02:18.040052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b*a","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:02:50.092631Z","iopub.execute_input":"2022-11-21T12:02:50.093317Z","iopub.status.idle":"2022-11-21T12:02:50.099136Z","shell.execute_reply.started":"2022-11-21T12:02:50.093280Z","shell.execute_reply":"2022-11-21T12:02:50.098208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b@a","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:02:55.757837Z","iopub.execute_input":"2022-11-21T12:02:55.758533Z","iopub.status.idle":"2022-11-21T12:02:55.779011Z","shell.execute_reply.started":"2022-11-21T12:02:55.758496Z","shell.execute_reply":"2022-11-21T12:02:55.777762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = b.reshape((1,3))","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:03:21.644375Z","iopub.execute_input":"2022-11-21T12:03:21.644726Z","iopub.status.idle":"2022-11-21T12:03:21.649762Z","shell.execute_reply.started":"2022-11-21T12:03:21.644695Z","shell.execute_reply":"2022-11-21T12:03:21.648323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a@b","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:03:28.718563Z","iopub.execute_input":"2022-11-21T12:03:28.719142Z","iopub.status.idle":"2022-11-21T12:03:28.739917Z","shell.execute_reply.started":"2022-11-21T12:03:28.719105Z","shell.execute_reply":"2022-11-21T12:03:28.738707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(a)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:05:04.093645Z","iopub.execute_input":"2022-11-21T12:05:04.094332Z","iopub.status.idle":"2022-11-21T12:05:04.103014Z","shell.execute_reply.started":"2022-11-21T12:05:04.094297Z","shell.execute_reply":"2022-11-21T12:05:04.101790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(a, axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:05:14.380819Z","iopub.execute_input":"2022-11-21T12:05:14.381208Z","iopub.status.idle":"2022-11-21T12:05:14.388019Z","shell.execute_reply.started":"2022-11-21T12:05:14.381176Z","shell.execute_reply":"2022-11-21T12:05:14.386814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(a, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:05:22.605250Z","iopub.execute_input":"2022-11-21T12:05:22.605614Z","iopub.status.idle":"2022-11-21T12:05:22.612528Z","shell.execute_reply.started":"2022-11-21T12:05:22.605582Z","shell.execute_reply":"2022-11-21T12:05:22.611491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array([1,1,1])\nb = np.array([2,2,2])\nc = np.array([3,3,3])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:06:48.589576Z","iopub.execute_input":"2022-11-21T12:06:48.589944Z","iopub.status.idle":"2022-11-21T12:06:48.595714Z","shell.execute_reply.started":"2022-11-21T12:06:48.589911Z","shell.execute_reply":"2022-11-21T12:06:48.594622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.stack(a,b,c)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:06:59.054015Z","iopub.execute_input":"2022-11-21T12:06:59.054363Z","iopub.status.idle":"2022-11-21T12:06:59.085699Z","shell.execute_reply.started":"2022-11-21T12:06:59.054335Z","shell.execute_reply":"2022-11-21T12:06:59.084247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.stack([a,b,c])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:07:25.695456Z","iopub.execute_input":"2022-11-21T12:07:25.695833Z","iopub.status.idle":"2022-11-21T12:07:25.702640Z","shell.execute_reply.started":"2022-11-21T12:07:25.695802Z","shell.execute_reply":"2022-11-21T12:07:25.701339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.stack([a,b,c], axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:07:51.458453Z","iopub.execute_input":"2022-11-21T12:07:51.458819Z","iopub.status.idle":"2022-11-21T12:07:51.466109Z","shell.execute_reply.started":"2022-11-21T12:07:51.458788Z","shell.execute_reply":"2022-11-21T12:07:51.465096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.stack([a,b,c], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:07:57.156027Z","iopub.execute_input":"2022-11-21T12:07:57.157213Z","iopub.status.idle":"2022-11-21T12:07:57.165875Z","shell.execute_reply.started":"2022-11-21T12:07:57.157168Z","shell.execute_reply":"2022-11-21T12:07:57.164909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.concatenate([a,b,c])","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:08:27.730192Z","iopub.execute_input":"2022-11-21T12:08:27.732029Z","iopub.status.idle":"2022-11-21T12:08:27.738681Z","shell.execute_reply.started":"2022-11-21T12:08:27.731986Z","shell.execute_reply":"2022-11-21T12:08:27.737680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.concatenate([a,b,c], axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:08:41.999194Z","iopub.execute_input":"2022-11-21T12:08:41.999544Z","iopub.status.idle":"2022-11-21T12:08:42.007674Z","shell.execute_reply.started":"2022-11-21T12:08:41.999515Z","shell.execute_reply":"2022-11-21T12:08:42.006686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.concatenate([a,b,c], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-21T12:08:48.736674Z","iopub.execute_input":"2022-11-21T12:08:48.737048Z","iopub.status.idle":"2022-11-21T12:08:48.767857Z","shell.execute_reply.started":"2022-11-21T12:08:48.737018Z","shell.execute_reply":"2022-11-21T12:08:48.766706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}