{"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 pandas as pd\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport os\nfrom sklearn.model_selection import train_test_split\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-04T01:52:17.661964Z","iopub.execute_input":"2022-05-04T01:52:17.662284Z","iopub.status.idle":"2022-05-04T01:52:17.667327Z","shell.execute_reply.started":"2022-05-04T01:52:17.662243Z","shell.execute_reply":"2022-05-04T01:52:17.666306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Look in the file path to find where are data is being stored.","metadata":{}},{"cell_type":"code","source":"print(os.listdir(\"../\"))\nprint(os.listdir(\"../input/digit-recognizer/\"))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:17.673473Z","iopub.execute_input":"2022-05-04T01:52:17.673956Z","iopub.status.idle":"2022-05-04T01:52:17.685417Z","shell.execute_reply.started":"2022-05-04T01:52:17.673909Z","shell.execute_reply":"2022-05-04T01:52:17.684308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"load the data and look at its shapes and other info","metadata":{}},{"cell_type":"code","source":"training_data = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nprint(training_data.shape)\nprint(training_data.columns)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:17.686713Z","iopub.execute_input":"2022-05-04T01:52:17.68739Z","iopub.status.idle":"2022-05-04T01:52:19.889786Z","shell.execute_reply.started":"2022-05-04T01:52:17.687354Z","shell.execute_reply":"2022-05-04T01:52:19.888936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Put data into the correct format for pytorch","metadata":{}},{"cell_type":"code","source":"X_train_full = torch.Tensor(training_data.drop(columns = ['label']).values).type(torch.FloatTensor)\ny_train_full = torch.Tensor(training_data['label'].values).type(torch.LongTensor)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:19.891526Z","iopub.execute_input":"2022-05-04T01:52:19.891756Z","iopub.status.idle":"2022-05-04T01:52:20.082947Z","shell.execute_reply.started":"2022-05-04T01:52:19.891728Z","shell.execute_reply":"2022-05-04T01:52:20.081953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split data into training set and testing set with 'train_test_split' function","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_train_full, y_train_full, test_size=0.2, random_state=2022)\nprint(X_train.size())\nprint(X_test.size())\nprint(y_train.size())\nprint(y_test.size())\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.084418Z","iopub.execute_input":"2022-05-04T01:52:20.085037Z","iopub.status.idle":"2022-05-04T01:52:20.526691Z","shell.execute_reply.started":"2022-05-04T01:52:20.084989Z","shell.execute_reply":"2022-05-04T01:52:20.525839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have to use the pytorch modules and stuff to actually make a neural network that can attempt to learn how to classify our data!\n\nWe will need 3 things.\n1. network model\n2. loss function\n3. optimizer, (the program that will calculate our gradients, and run gradient descent)","metadata":{}},{"cell_type":"code","source":"## construct our network object\nclass Network(nn.Module):\n    def __init__(self, h1, h2):\n        super(Network, self).__init__()\n        self.linear_1 = nn.Linear(784,h1)\n        self.linear_2 = nn.Linear(h1, h2)\n        self.linear_3 = nn.Linear(h2, 10)\n        self.activation_1 = nn.ReLU()\n        self.activation_2 = nn.ReLU()\n        self.activation_3 = nn.ReLU()\n\n\n    def forward(self, input):\n        out = self.linear_1(input)\n        out = self.activation_1(out)\n        out = self.linear_2(out)\n        out = self.activation_2(out)\n        out = self.linear_3(out)\n        out = self.activation_3(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.528404Z","iopub.execute_input":"2022-05-04T01:52:20.528697Z","iopub.status.idle":"2022-05-04T01:52:20.536166Z","shell.execute_reply.started":"2022-05-04T01:52:20.528668Z","shell.execute_reply":"2022-05-04T01:52:20.535011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## instantiate a model\nmodel = Network(256, 64)\n\n## choose our loss function\ncriterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.537304Z","iopub.execute_input":"2022-05-04T01:52:20.537971Z","iopub.status.idle":"2022-05-04T01:52:20.551811Z","shell.execute_reply.started":"2022-05-04T01:52:20.537929Z","shell.execute_reply":"2022-05-04T01:52:20.550863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## choose and optimizer (thing that does gradient descent for us)\noptimizer = optim.Adam(model.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.552862Z","iopub.execute_input":"2022-05-04T01:52:20.553487Z","iopub.status.idle":"2022-05-04T01:52:20.561734Z","shell.execute_reply.started":"2022-05-04T01:52:20.553439Z","shell.execute_reply":"2022-05-04T01:52:20.560872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(model.parameters())","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.563077Z","iopub.execute_input":"2022-05-04T01:52:20.563823Z","iopub.status.idle":"2022-05-04T01:52:20.588061Z","shell.execute_reply.started":"2022-05-04T01:52:20.563754Z","shell.execute_reply":"2022-05-04T01:52:20.586859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have to write a 'training loop'","metadata":{}},{"cell_type":"code","source":"num_epochs = 100\n\nfor epoch in range(num_epochs):\n    \n    # forward pass and loss calculation\n    y_pred = model(X_train)\n    loss = criterion(y_pred, y_train)\n    \n    # backward pass\n    loss.backward()\n    \n    # update our parameters (weights and biases and stuff)\n    optimizer.step()\n    \n    # empty the grad calculations\n    optimizer.zero_grad()\n    \n    # print loss calculation\n    if epoch % 10 == 0:\n        print(f\"epoch: {epoch}, loss: {loss.item()}\")\n\nprint(f\"epoch: {epoch}, loss: {loss.item()}\")\n\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:20.589351Z","iopub.execute_input":"2022-05-04T01:52:20.590977Z","iopub.status.idle":"2022-05-04T01:52:53.817571Z","shell.execute_reply.started":"2022-05-04T01:52:20.590927Z","shell.execute_reply":"2022-05-04T01:52:53.816803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    y_test_predictions = model(X_test)\n    accuracy = criterion(y_test_predictions, y_test)\n    print(f\"prediciton loss on test set: {accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:53.81869Z","iopub.execute_input":"2022-05-04T01:52:53.818929Z","iopub.status.idle":"2022-05-04T01:52:53.857752Z","shell.execute_reply.started":"2022-05-04T01:52:53.818902Z","shell.execute_reply":"2022-05-04T01:52:53.856815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#pred_arr = y_test_predictions.detach().numpy()\nprint(np.shape(pred_arr))\n\nmax_inds = np.argmax(pred_arr, axis = 1)\nprint(np.shape(max_inds))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:42:59.953854Z","iopub.execute_input":"2022-05-04T01:42:59.954163Z","iopub.status.idle":"2022-05-04T01:42:59.960639Z","shell.execute_reply.started":"2022-05-04T01:42:59.95413Z","shell.execute_reply":"2022-05-04T01:42:59.959671Z"}}},{"cell_type":"code","source":"pred_arr = y_test_predictions.detach().numpy() \nprint(np.shape(pred_arr))\n\nmax_inds = np.argmax(pred_arr, axis = 1) \nprint(np.shape(max_inds))\ntrue_y = y_test.detach().numpy()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:53.860735Z","iopub.execute_input":"2022-05-04T01:52:53.861384Z","iopub.status.idle":"2022-05-04T01:52:53.869758Z","shell.execute_reply.started":"2022-05-04T01:52:53.861337Z","shell.execute_reply":"2022-05-04T01:52:53.868804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprint(pred_arr[1:10, :])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:53.870991Z","iopub.execute_input":"2022-05-04T01:52:53.871914Z","iopub.status.idle":"2022-05-04T01:52:53.882925Z","shell.execute_reply.started":"2022-05-04T01:52:53.871869Z","shell.execute_reply":"2022-05-04T01:52:53.881808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aa = np.equal(max_inds, true_y)\nprint(aa[0:100])\nprint(true_y.dtype)\nprint(max_inds.dtype)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:53.88507Z","iopub.execute_input":"2022-05-04T01:52:53.885858Z","iopub.status.idle":"2022-05-04T01:52:53.895594Z","shell.execute_reply.started":"2022-05-04T01:52:53.885808Z","shell.execute_reply":"2022-05-04T01:52:53.894745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(aa))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T01:52:53.897096Z","iopub.execute_input":"2022-05-04T01:52:53.897331Z","iopub.status.idle":"2022-05-04T01:52:53.913485Z","shell.execute_reply.started":"2022-05-04T01:52:53.897304Z","shell.execute_reply":"2022-05-04T01:52:53.912778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, as practice, use pytorch to see how many numbers you actually predicted correctly","metadata":{}}]}