diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb index 429740f337ea55bce401da1e055bf1ad0e2decd3..5359381472463223442fb1e4391f870e87f84f25 100644 --- a/TD2 Deep Learning.ipynb +++ b/TD2 Deep Learning.ipynb @@ -66,6 +66,34 @@ "cell_type": "markdown", "id": "0882a636", "metadata": {}, + "source": [ + "Import everything at once (useful if we want to run only some sections of the code)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26523cec", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import torch\n", + "from torchvision import models, datasets, transforms\n", + "from torch.utils.data.sampler import SubsetRandomSampler\n", + "import torch.nn as nn\n", + "import torch.nn.functional as F\n", + "import torch.optim as optim\n", + "import matplotlib.pyplot as plt\n", + "import pandas as pd\n", + "import torch.quantization\n", + "import os" + ] + }, + { + "cell_type": "markdown", + "id": "31f6a70e", + "metadata": {}, "source": [ "\n", "To test run the following code" @@ -73,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "b1950f0a", "metadata": {}, "outputs": [ @@ -140,14 +168,10 @@ } ], "source": [ - "import torch\n", - "\n", "N, D = 14, 10\n", "x = torch.randn(N, D).type(torch.FloatTensor)\n", "print(x)\n", "\n", - "from torchvision import models\n", - "\n", "alexnet = models.alexnet()\n", "print(alexnet)" ] @@ -178,7 +202,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "6e18f2fd", "metadata": {}, "outputs": [ @@ -191,8 +215,6 @@ } ], "source": [ - "import torch\n", - "\n", "# Auto-detect the best device\n", "if torch.backends.mps.is_built(): # MPS for macOS\n", " device = torch.device(\"mps\")\n", @@ -214,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "462666a2", "metadata": {}, "outputs": [ @@ -228,10 +250,6 @@ } ], "source": [ - "import numpy as np\n", - "from torchvision import datasets, transforms\n", - "from torch.utils.data.sampler import SubsetRandomSampler\n", - "\n", "# number of subprocesses to use for data loading\n", "num_workers = 0\n", "# how many samples per batch to load\n", @@ -295,7 +313,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "317bf070", "metadata": {}, "outputs": [ @@ -318,9 +336,6 @@ } ], "source": [ - "import torch.nn as nn\n", - "import torch.nn.functional as F\n", - "\n", "# define the CNN architecture\n", "\n", "\n", @@ -416,8 +431,6 @@ } ], "source": [ - "import torch.optim as optim\n", - "\n", "criterion = nn.CrossEntropyLoss() # specify loss function\n", "optimizer = optim.SGD(model.parameters(), lr=0.01) # specify optimizer\n", "\n", @@ -509,7 +522,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "d39df818", "metadata": {}, "outputs": [ @@ -525,8 +538,6 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", - "\n", "plt.plot(range(n_epochs), train_loss_list)\n", "plt.xlabel(\"Epoch\")\n", "plt.ylabel(\"Loss\")\n", @@ -650,7 +661,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "28da770d", "metadata": {}, "outputs": [ @@ -675,9 +686,6 @@ } ], "source": [ - "import torch.nn as nn\n", - "import torch.nn.functional as F\n", - "\n", "class NewNet(nn.Module):\n", " def __init__(self):\n", " super(NewNet, self).__init__()\n", @@ -722,7 +730,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "id": "210b2852", "metadata": {}, "outputs": [ @@ -774,8 +782,6 @@ } ], "source": [ - "import torch.optim as optim\n", - "\n", "criterion = nn.CrossEntropyLoss() # specify loss function\n", "optimizer = optim.SGD(new_model.parameters(), lr=0.01) # specify optimizer\n", "\n", @@ -851,7 +857,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "id": "557508e9", "metadata": {}, "outputs": [ @@ -867,8 +873,6 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", - "\n", "plt.plot(train_loss_list)\n", "plt.xlabel(\"Epoch\")\n", "plt.ylabel(\"Loss\")\n", @@ -991,7 +995,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "id": "ef623c26", "metadata": {}, "outputs": [ @@ -1014,8 +1018,6 @@ } ], "source": [ - "import os\n", - "\n", "def print_size_of_model(new_model, label=\"\"):\n", " torch.save(new_model.state_dict(), \"temp.p\")\n", " size = os.path.getsize(\"temp.p\")\n", @@ -1060,7 +1062,6 @@ } ], "source": [ - "import torch.quantization\n", "torch.backends.quantized.engine = 'qnnpack' # or 'fbgemm' for x86 architectures\n", "\n", "# Loading the model with CPU version to be able to quantize.\n", @@ -1093,7 +1094,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": null, "id": "b0aed3cc", "metadata": {}, "outputs": [ @@ -1215,7 +1216,6 @@ } ], "source": [ - "import pandas as pd\n", "# track test loss\n", "test_loss = 0.0\n", "test_loss_quantized = 0.0\n",