{ "cells": [ { "cell_type": "markdown", "id": "a8a0d6f1", "metadata": {}, "source": [ "# Neural Nets Notebook\n", "\n", "## Exercise 2\n", "\n", "```python\n", "class MLP_drop(nn.Module):\n", " def __init__(self, input_dim, output_dim):\n", " super().__init__()\n", "\n", " self.input_fc = nn.Linear(input_dim, 250)\n", " self.hidden_fc = nn.Linear(250, 100)\n", " self.output_fc = nn.Linear(100, output_dim)\n", " self.dropout = nn.Dropout(0.25)\n", "\n", " def forward(self, x):\n", "\n", " batch_size = x.shape[0]\n", " x = x.view(batch_size, -1)\n", " h_1 = F.relu(self.input_fc(x))\n", " x = self.hidden_fc(h_1)\n", " x = self.dropout(x)\n", " h_2 = F.relu(x)\n", " y_pred = self.output_fc(h_2)\n", "\n", " return y_pred\n", " \n", "\n", "model = MLP_drop(INPUT_DIM, OUTPUT_DIM)\n", "optimizer = optim.Adam(model.parameters())\n", "criterion = nn.CrossEntropyLoss()\n", "\n", "EPOCHS = 10\n", "\n", "best_valid_loss = float('inf')\n", "\n", "for epoch in trange(EPOCHS):\n", "\n", " start_time = time.monotonic()\n", "\n", " train_loss, train_acc = train(model, train_iterator, optimizer, criterion, device)\n", " valid_loss, valid_acc = evaluate(model, valid_iterator, criterion, device)\n", "\n", " if valid_loss < best_valid_loss:\n", " best_valid_loss = valid_loss\n", " torch.save(model.state_dict(), 'mlp-model-drop.pt')\n", "\n", " end_time = time.monotonic()\n", "\n", " epoch_mins, epoch_secs = epoch_time(start_time, end_time)\n", "\n", " print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')\n", " print(f'\\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')\n", " print(f'\\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%')\n", "```" ] }, { "cell_type": "markdown", "id": "4404d771", "metadata": {}, "source": [ "# Conv Nets Notebook \n", "\n", "## Set up the network\n", "\n", "```python\n", " def forward(self, x):\n", "\n", " x = self.conv1(x)\n", " x = F.max_pool2d(x, kernel_size=2)\n", " x = F.relu(x)\n", " x = self.conv2(x)\n", " x = F.max_pool2d(x, kernel_size=2)\n", " x = F.relu(x)\n", " x = x.view(x.shape[0], -1)\n", " x = self.fc_1(x)\n", " x = F.relu(x)\n", " x = self.fc_2(x)\n", " x = F.relu(x)\n", " x = self.fc_3(x)\n", "\n", " return x\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "cf3b9baa", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }