diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb index d5db02a12d4ee09069c167e7e00430fc4115fd00..8d88ed30fc8beb40df8a2bd65c56048c05b7ed5e 100644 --- a/TD2 Deep Learning.ipynb +++ b/TD2 Deep Learning.ipynb @@ -609,10 +609,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "e93efdfc", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Loss: 21.467534\n", + "\n", + "Test Accuracy of airplane: 67% (674/1000)\n", + "Test Accuracy of automobile: 77% (778/1000)\n", + "Test Accuracy of bird: 49% (499/1000)\n", + "Test Accuracy of cat: 47% (478/1000)\n", + "Test Accuracy of deer: 52% (528/1000)\n", + "Test Accuracy of dog: 55% (556/1000)\n", + "Test Accuracy of frog: 71% (713/1000)\n", + "Test Accuracy of horse: 62% (623/1000)\n", + "Test Accuracy of ship: 73% (736/1000)\n", + "Test Accuracy of truck: 69% (697/1000)\n", + "\n", + "Test Accuracy (Overall): 62% (6282/10000)\n" + ] + } + ], "source": [ "model.load_state_dict(torch.load(\"./model_cifar.pt\"))\n", "\n", @@ -693,6 +714,154 @@ "Compare the results obtained with this new network to those obtained previously." ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the new Network\n", + "import torch.nn as nn\n", + "import torch.nn.functional as F\n", + "\n", + "class new_Net(nn.Module):\n", + " def __init__(self):\n", + " super(new_Net, self).__init__()\n", + "\n", + " self.conv1 = nn.Conv2d(3, 16, 3)\n", + " self.conv2 = nn.Conv2d(16, 32, 3)\n", + " self.conv3 = nn.Conv2d(32, 64, 3)\n", + "\n", + " self.pool = nn.MaxPool2d(2, 2)\n", + "\n", + " self.fc1 = nn.Linear(64 * 2 * 2, 512)\n", + " self.fc2 = nn.Linear(512, 64)\n", + " self.fc3 = nn.Linear(64, 10)\n", + " \n", + " self.dropout = nn.Dropout(0.25) # Define a proportion to dropout\n", + "\n", + " def forward(self, x):\n", + " x = self.pool(F.relu(self.conv1(x)))\n", + " x = self.pool(F.relu(self.conv2(x)))\n", + " x = self.pool(F.relu(self.conv3(x)))\n", + "\n", + " x = x.view(-1, 64 * 2 * 2)\n", + " \n", + " x = F.relu(self.fc1(x))\n", + " x = self.dropout(x)\n", + "\n", + " x = F.relu(self.fc2(x))\n", + " x = self.dropout(x)\n", + "\n", + " x = self.fc3(x)\n", + "\n", + " return x\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new_Net(\n", + " (conv1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1))\n", + " (conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1))\n", + " (conv3): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))\n", + " (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", + " (fc1): Linear(in_features=256, out_features=512, bias=True)\n", + " (fc2): Linear(in_features=512, out_features=64, bias=True)\n", + " (fc3): Linear(in_features=64, out_features=10, bias=True)\n", + " (dropout): Dropout(p=0.25, inplace=False)\n", + ")\n" + ] + } + ], + "source": [ + "# create a CNN\n", + "new_model = new_Net()\n", + "print(new_model)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# New new_model training & saving new_model : new_model_cifar_2.pt\n", + "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", + "n_epochs = 30 # number of epochs to train the new_model\n", + "train_loss_list = [] # list to store loss to visualize\n", + "valid_loss_min = np.Inf # track change in validation loss\n", + "\n", + "for epoch in range(n_epochs):\n", + " # Keep track of training and validation loss\n", + " train_loss = 0.0\n", + " valid_loss = 0.0\n", + "\n", + " # Train the new_model\n", + " new_model.train()\n", + " for data, target in train_loader:\n", + " # Move tensors to GPU if CUDA is available\n", + " if train_on_gpu:\n", + " data, target = data.cuda(), target.cuda()\n", + " # Clear the gradients of all optimized variables\n", + " optimizer.zero_grad()\n", + " # Forward pass: compute predicted outputs by passing inputs to the new_model\n", + " output = new_model(data)\n", + " # Calculate the batch loss\n", + " loss = criterion(output, target)\n", + " # Backward pass: compute gradient of the loss with respect to new_model parameters\n", + " loss.backward()\n", + " # Perform a single optimization step (parameter update)\n", + " optimizer.step()\n", + " # Update training loss\n", + " train_loss += loss.item() * data.size(0)\n", + "\n", + " # Validate the new_model\n", + " new_model.eval()\n", + " for data, target in valid_loader:\n", + " # Move tensors to GPU if CUDA is available\n", + " if train_on_gpu:\n", + " data, target = data.cuda(), target.cuda()\n", + " # Forward pass: compute predicted outputs by passing inputs to the new_model\n", + " output = new_model(data)\n", + " # Calculate the batch loss\n", + " loss = criterion(output, target)\n", + " # Update average validation loss\n", + " valid_loss += loss.item() * data.size(0)\n", + "\n", + " # Calculate average losses\n", + " train_loss = train_loss / len(train_loader)\n", + " valid_loss = valid_loss / len(valid_loader)\n", + " train_loss_list.append(train_loss)\n", + "\n", + " # Print training/validation statistics\n", + " print(\n", + " \"Epoch: {} tTraining Loss: {:.6f} tValidation Loss: {:.6f}\".format(\n", + " epoch, train_loss, valid_loss\n", + " )\n", + " )\n", + "\n", + " # Save new_model if validation loss has decreased\n", + " if valid_loss <= valid_loss_min:\n", + " print(\n", + " \"Validation loss decreased ({:.6f} --> {:.6f}). Saving new_model ...\".format(\n", + " valid_loss_min, valid_loss\n", + " )\n", + " )\n", + " torch.save(new_model.state_dict(), \"new_model_cifar_2.pt\")\n", + " valid_loss_min = valid_loss" + ] + }, { "cell_type": "markdown", "id": "bc381cf4",