{"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\nfrom torch.utils.data import Dataset, DataLoader\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-25T01:03:05.892752Z","iopub.execute_input":"2022-05-25T01:03:05.893794Z","iopub.status.idle":"2022-05-25T01:03:05.900245Z","shell.execute_reply.started":"2022-05-25T01:03:05.893728Z","shell.execute_reply":"2022-05-25T01:03:05.899431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Mnist(Dataset):\n    def __init__(self, images, labels):\n        self.x = images\n        self.y = labels\n        self.num_images = images.size()[0]\n    \n    def __getitem__(self, index):\n        return self.x[index], self.y[index]\n        \n    def __len__(self):\n        return self.num_images","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:05.902747Z","iopub.execute_input":"2022-05-25T01:03:05.903461Z","iopub.status.idle":"2022-05-25T01:03:05.928392Z","shell.execute_reply.started":"2022-05-25T01:03:05.903425Z","shell.execute_reply":"2022-05-25T01:03:05.927588Z"},"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-25T01:03:05.929932Z","iopub.execute_input":"2022-05-25T01:03:05.930309Z","iopub.status.idle":"2022-05-25T01:03:05.944452Z","shell.execute_reply.started":"2022-05-25T01:03:05.930279Z","shell.execute_reply":"2022-05-25T01:03:05.943620Z"},"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-25T01:03:05.945874Z","iopub.execute_input":"2022-05-25T01:03:05.946274Z","iopub.status.idle":"2022-05-25T01:03:09.008226Z","shell.execute_reply.started":"2022-05-25T01:03:05.946240Z","shell.execute_reply":"2022-05-25T01:03:09.007266Z"},"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-25T01:03:09.010863Z","iopub.execute_input":"2022-05-25T01:03:09.011392Z","iopub.status.idle":"2022-05-25T01:03:09.316247Z","shell.execute_reply.started":"2022-05-25T01:03:09.011356Z","shell.execute_reply":"2022-05-25T01:03:09.315237Z"},"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_full = X_train_full.reshape(42000, 28, 28)\nX_train_full = X_train_full.unsqueeze(1)\nprint(X_train_full.size())\nprint(X_train_full[1,:,1,1])\n","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:09.317616Z","iopub.execute_input":"2022-05-25T01:03:09.318268Z","iopub.status.idle":"2022-05-25T01:03:09.327078Z","shell.execute_reply.started":"2022-05-25T01:03:09.318213Z","shell.execute_reply":"2022-05-25T01:03:09.325719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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())\nData1 = Mnist(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:09.328440Z","iopub.execute_input":"2022-05-25T01:03:09.328720Z","iopub.status.idle":"2022-05-25T01:03:09.930723Z","shell.execute_reply.started":"2022-05-25T01:03:09.328688Z","shell.execute_reply":"2022-05-25T01:03:09.929684Z"},"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, hidden_1, hidden_2):\n        super(Network, self).__init__()\n        self.conv1 = nn.Conv2d(1, 10, kernel_size=3, stride=1, padding=0)\n        self.activation_1 = nn.ReLU()\n        self.conv2 = nn.Conv2d(10, 32, kernel_size=3, stride=1, padding=0)\n        self.activation_2 = nn.ReLU()\n        self.linear_1 = nn.Linear(32*24*24, hidden_1)\n        self.activation_3 = nn.ReLU()\n        self.linear_2 = nn.Linear(hidden_1, hidden_2)\n        self.activation_4 = nn.ReLU()\n        self.linear_3 = nn.Linear(hidden_2, 10)\n        self.activation_5 = nn.ReLU()\n        \n    def forward(self, input):\n        out = self.conv1(input)\n        out = self.activation_1(out)\n        out = self.conv2(out)\n        out = self.activation_2(out)\n        out = out.view(-1, 32*24*24)\n        out = self.linear_1(out)\n        out = self.activation_3(out)\n        out = self.linear_2(out)\n        out = self.activation_4(out)\n        out = self.linear_3(out)\n        out = self.activation_5(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:09.932066Z","iopub.execute_input":"2022-05-25T01:03:09.932328Z","iopub.status.idle":"2022-05-25T01:03:09.945929Z","shell.execute_reply.started":"2022-05-25T01:03:09.932296Z","shell.execute_reply":"2022-05-25T01:03:09.945190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## instantiate a model\nmodel = Network(64, 64)\n\n## choose our loss function\ncriterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:09.947288Z","iopub.execute_input":"2022-05-25T01:03:09.947565Z","iopub.status.idle":"2022-05-25T01:03:09.977989Z","shell.execute_reply.started":"2022-05-25T01:03:09.947531Z","shell.execute_reply":"2022-05-25T01:03:09.976277Z"},"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.005)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T01:03:09.980325Z","iopub.execute_input":"2022-05-25T01:03:09.980701Z","iopub.status.idle":"2022-05-25T01:03:09.988213Z","shell.execute_reply.started":"2022-05-25T01:03:09.980657Z","shell.execute_reply":"2022-05-25T01:03:09.987221Z"},"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\ndload = DataLoader(Data1, batch_size=1000, shuffle=True)\n\nfor epoch in range(num_epochs):\n    for i,(images,labels) in enumerate(dload):\n    \n        # forward pass and loss calculation\n        y_pred = model(images)\n        loss = criterion(y_pred, labels)\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 i % 200 == 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-25T01:03:09.991419Z","iopub.execute_input":"2022-05-25T01:03:09.992192Z"},"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":{"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":{}},{"cell_type":"code","source":"prediction_array = y_test_predictions.detach().numpy()\nvector_of_predictions = np.argmax(prediction_array, axis=1)\ntest_numpy_array = y_test.detach().numpy()\na = test_numpy_array == vector_of_predictions\nnp.mean(a)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Want to test the number of hyperparemeters\nWant to do hyperparmetering tuning\nwant a loop to run through all the hyperparemeters\n(maybe change epochs, read about dataloader, dataset)\n(how to input different datasets, setup minibatch)\n(need convulution neural network)\nepochs, dimension of matrix\n\nDo hyperperamter training loop: epochs, learning rate, dimensions of (hidden) layers in a loop, need a function,\nAlso maybe dataset, dataloader\nHow to run a long program in kaggle","metadata":{}},{"cell_type":"markdown","source":"import model,\ndefining the model, make dataloader,\ndefine combos of hyperparemters\n    \n      make training function\n      illustrate model\n      run training loop\n      test resutls\n      return results\n      \n  run loop of training every set of hyper-parameters\n","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}