From 5b03a1decc20dea7ac7fc6c7ba41e1211950eb9d Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Fri, 19 Nov 2021 13:15:38 +1000 Subject: [PATCH] update loss and acc metric calculations --- 04_pytorch_custom_datasets.ipynb | 123 ++++++++++--------------------- 1 file changed, 37 insertions(+), 86 deletions(-) diff --git a/04_pytorch_custom_datasets.ipynb b/04_pytorch_custom_datasets.ipynb index ed5b200..1cdd2d2 100644 --- a/04_pytorch_custom_datasets.ipynb +++ b/04_pytorch_custom_datasets.ipynb @@ -966,11 +966,12 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "def train(data_loader, model, loss_fn, optimizer, print_freq=10):\n", + " train_loss, train_acc, count = 0, 0, 0\n", " for batch, (X, y) in enumerate(data_loader):\n", " # Send data to GPU\n", " X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)\n", @@ -985,6 +986,10 @@ "\n", " # 2. Calculate loss\n", " loss = loss_fn(y_pred, y)\n", + " train_loss += loss\n", + " count += len(X)\n", + " y_pred_class = torch.softmax(y_pred, dim=1).argmax(dim=1)\n", + " train_acc += torch.eq(y_pred_class, y).sum().item()\n", "\n", " # 3. Optimizer zero grad\n", " optimizer.zero_grad()\n", @@ -998,12 +1003,12 @@ " # Print out what's happening\n", " if batch % print_freq == 0:\n", " sample_number = batch * len(X)\n", - " print(f\"Loss: {loss:.5f} | Looked at {sample_number}/{len(data_loader.dataset)} samples\")" + " print(f\"Loss: {loss/count:.5f} | Train Acc: {train_acc/count:.3f} | Looked at {sample_number}/{len(data_loader.dataset)} samples\")" ] }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 94, "metadata": {}, "outputs": [], "source": [ @@ -1012,12 +1017,12 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "def test(data_loader, model, loss_fn, accuracy_fn=None):\n", - " test_loss, test_acc = 0, 0\n", + " test_loss, test_acc, count = 0, 0, 0\n", " model.eval() # put model in eval mode\n", " # Turn on inference context manager\n", " with torch.inference_mode():\n", @@ -1030,6 +1035,7 @@ "\n", " # 2. Calculate loss and accuracy (if necessary\n", " test_loss += loss_fn(test_pred, y)\n", + " count += len(X)\n", " # print(test_pred.argmax(dim=1))\n", " if accuracy_fn is not None:\n", " test_acc += accuracy_fn(y_true=y,\n", @@ -1038,15 +1044,15 @@ " print(\"No accuracy_fn given, accuracy will show 0%\")\n", " \n", " # Adjust metrics and print out\n", - " test_loss /= len(data_loader)\n", - " test_acc /= len(data_loader)\n", + " test_loss /= count\n", + " test_acc /= count\n", " print(f\"\\nTesting for Epoch: {epoch}\\n----------\")\n", - " print(f\"Test loss: {test_loss:.5f} | Test accuracy: {test_acc:.2f}%\\n\")" + " print(f\"Test loss: {test_loss:.5f} | Test accuracy: {test_acc:.3f}%\\n\")" ] }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 96, "metadata": {}, "outputs": [], "source": [ @@ -1057,7 +1063,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 97, "metadata": {}, "outputs": [ { @@ -1066,110 +1072,55 @@ "text": [ "Epoch: 0\n", "----------\n", - "Loss: 2.35703 | Looked at 0/654 samples\n", - "Loss: 2.32441 | Looked at 320/654 samples\n", - "Loss: 2.30014 | Looked at 280/654 samples\n", - "tensor([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,\n", - " 8, 8, 8, 8, 8, 8, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,\n", - " 8, 8, 8, 8, 8, 8, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,\n", - " 8, 8, 8, 8, 8, 8, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,\n", - " 8, 8, 8, 8, 8, 8, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,\n", - " 8, 8, 8, 8, 8, 8, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8], device='cuda:0')\n", + "Loss: 0.06658 | Train Acc: 0.188 | Looked at 0/654 samples\n", + "Loss: 0.00639 | Train Acc: 0.173 | Looked at 320/654 samples\n", + "Loss: 0.00352 | Train Acc: 0.164 | Looked at 280/654 samples\n", "\n", "Testing for Epoch: 0\n", "----------\n", - "Test loss: 2.32970 | Test accuracy: 5.73%\n", + "Test loss: 0.08215 | Test accuracy: 0.46%\n", "\n", "Epoch: 1\n", "----------\n", - "Loss: 2.29623 | Looked at 0/654 samples\n", - "Loss: 2.34511 | Looked at 320/654 samples\n", - "Loss: 2.37775 | Looked at 280/654 samples\n", - "tensor([3, 3, 8, 3, 3, 3, 3, 3, 8, 3, 3, 8, 8, 3, 3, 8, 3, 3, 3, 8, 3, 3, 3, 3,\n", - " 3, 3, 3, 3, 3, 3, 3, 3], device='cuda:0')\n", - "tensor([3, 3, 3, 3, 3, 3, 3, 8, 3, 3, 3, 8, 3, 3, 8, 8, 8, 3, 3, 8, 3, 3, 8, 3,\n", - " 3, 3, 3, 3, 8, 8, 3, 8], device='cuda:0')\n", - "tensor([8, 3, 3, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\n", - " 3, 3, 8, 3, 3, 3, 3, 8], device='cuda:0')\n", - "tensor([3, 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8, 3,\n", - " 3, 8, 3, 8, 3, 8, 3, 3], device='cuda:0')\n", - "tensor([3, 8, 3, 3, 3, 3, 8, 9, 3, 3, 3, 3, 8, 8, 8, 3, 3, 8, 3, 3, 3, 3, 3, 3,\n", - " 3, 8, 3, 3, 8, 3, 3, 3], device='cuda:0')\n", - "tensor([3, 3, 3, 3], device='cuda:0')\n", + "Loss: 0.06965 | Train Acc: 0.219 | Looked at 0/654 samples\n", + "Loss: 0.00613 | Train Acc: 0.170 | Looked at 320/654 samples\n", + "Loss: 0.00330 | Train Acc: 0.180 | Looked at 280/654 samples\n", "\n", "Testing for Epoch: 1\n", "----------\n", - "Test loss: 2.29934 | Test accuracy: 9.38%\n", + "Test loss: 0.08113 | Test accuracy: 0.59%\n", "\n", "Epoch: 2\n", "----------\n", - "Loss: 2.30758 | Looked at 0/654 samples\n", - "Loss: 2.21940 | Looked at 320/654 samples\n", - "Loss: 2.34643 | Looked at 280/654 samples\n", - "tensor([8, 8, 3, 8, 3, 3, 8, 3, 3, 9, 3, 3, 3, 8, 8, 9, 3, 9, 9, 3, 3, 3, 8, 3,\n", - " 8, 9, 8, 3, 8, 3, 8, 8], device='cuda:0')\n", - "tensor([3, 3, 3, 8, 8, 3, 3, 9, 8, 8, 3, 8, 9, 8, 9, 3, 8, 3, 3, 3, 8, 8, 3, 8,\n", - " 8, 3, 9, 8, 8, 3, 3, 3], device='cuda:0')\n", - "tensor([8, 3, 3, 8, 3, 3, 8, 8, 8, 3, 8, 8, 3, 9, 3, 3, 3, 8, 8, 8, 3, 9, 8, 3,\n", - " 8, 8, 9, 3, 8, 3, 3, 8], device='cuda:0')\n", - "tensor([8, 8, 8, 8, 9, 8, 3, 9, 8, 3, 3, 9, 3, 3, 8, 3, 8, 9, 8, 3, 3, 8, 3, 3,\n", - " 8, 8, 9, 3, 9, 3, 3, 8], device='cuda:0')\n", - "tensor([8, 3, 9, 3, 3, 3, 3, 3, 3, 3, 8, 3, 8, 9, 9, 8, 8, 9, 3, 9, 8, 3, 3, 3,\n", - " 8, 8, 9, 8, 3, 8, 3, 3], device='cuda:0')\n", - "tensor([9, 8, 3, 9], device='cuda:0')\n", + "Loss: 0.06402 | Train Acc: 0.250 | Looked at 0/654 samples\n", + "Loss: 0.00634 | Train Acc: 0.153 | Looked at 320/654 samples\n", + "Loss: 0.00344 | Train Acc: 0.179 | Looked at 280/654 samples\n", "\n", "Testing for Epoch: 2\n", "----------\n", - "Test loss: 2.26508 | Test accuracy: 8.85%\n", + "Test loss: 0.08225 | Test accuracy: 0.51%\n", "\n", "Epoch: 3\n", "----------\n", - "Loss: 2.38322 | Looked at 0/654 samples\n", - "Loss: 2.22241 | Looked at 320/654 samples\n", - "Loss: 2.26243 | Looked at 280/654 samples\n", - "tensor([3, 8, 8, 9, 8, 8, 3, 8, 8, 3, 3, 3, 8, 3, 9, 8, 8, 9, 8, 8, 3, 8, 9, 3,\n", - " 8, 8, 3, 3, 8, 3, 8, 8], device='cuda:0')\n", - "tensor([8, 8, 9, 3, 3, 3, 9, 9, 3, 3, 8, 8, 8, 8, 8, 3, 3, 3, 8, 8, 3, 8, 9, 8,\n", - " 3, 8, 3, 9, 3, 8, 9, 8], device='cuda:0')\n", - "tensor([3, 9, 9, 3, 8, 9, 9, 8, 9, 3, 3, 9, 3, 3, 8, 3, 9, 9, 8, 3, 9, 8, 9, 8,\n", - " 9, 8, 8, 8, 8, 8, 8, 3], device='cuda:0')\n", - "tensor([9, 8, 3, 9, 3, 3, 8, 9, 8, 9, 9, 8, 8, 3, 8, 3, 8, 9, 8, 8, 3, 8, 3, 3,\n", - " 8, 8, 9, 8, 8, 8, 9, 9], device='cuda:0')\n", - "tensor([3, 8, 9, 3, 3, 3, 8, 9, 3, 9, 8, 9, 8, 3, 3, 8, 8, 3, 8, 8, 8, 8, 9, 3,\n", - " 8, 8, 8, 8, 8, 9, 3, 3], device='cuda:0')\n", - "tensor([8, 3, 9, 8], device='cuda:0')\n", + "Loss: 0.06454 | Train Acc: 0.281 | Looked at 0/654 samples\n", + "Loss: 0.00635 | Train Acc: 0.170 | Looked at 320/654 samples\n", + "Loss: 0.00309 | Train Acc: 0.188 | Looked at 280/654 samples\n", "\n", "Testing for Epoch: 3\n", "----------\n", - "Test loss: 2.29451 | Test accuracy: 8.85%\n", + "Test loss: 0.08215 | Test accuracy: 0.42%\n", "\n", "Epoch: 4\n", "----------\n", - "Loss: 2.24605 | Looked at 0/654 samples\n", - "Loss: 2.21941 | Looked at 320/654 samples\n", - "Loss: 2.19423 | Looked at 280/654 samples\n", - "tensor([9, 3, 8, 8, 9, 8, 9, 9, 3, 9, 8, 9, 9, 3, 8, 3, 9, 3, 3, 8, 9, 8, 3, 3,\n", - " 9, 3, 3, 8, 9, 8, 9, 8], device='cuda:0')\n", - "tensor([9, 8, 8, 3, 9, 9, 8, 8, 9, 9, 3, 8, 9, 8, 8, 9, 8, 9, 3, 9, 3, 9, 9, 8,\n", - " 9, 3, 9, 8, 8, 8, 8, 9], device='cuda:0')\n", - "tensor([8, 8, 9, 9, 3, 9, 8, 8, 9, 8, 9, 3, 8, 9, 8, 9, 9, 9, 8, 9, 3, 3, 3, 3,\n", - " 9, 8, 8, 9, 9, 9, 3, 9], device='cuda:0')\n", - "tensor([9, 3, 9, 8, 9, 8, 9, 8, 3, 9, 3, 9, 3, 3, 9, 8, 8, 9, 9, 9, 3, 9, 8, 3,\n", - " 8, 9, 3, 9, 3, 9, 8, 9], device='cuda:0')\n", - "tensor([3, 9, 9, 8, 9, 3, 9, 3, 9, 8, 8, 9, 3, 8, 3, 9, 9, 3, 3, 3, 8, 8, 8, 3,\n", - " 9, 9, 9, 8, 3, 9, 3, 9], device='cuda:0')\n", - "tensor([9, 8, 8, 3], device='cuda:0')\n", + "Loss: 0.07109 | Train Acc: 0.094 | Looked at 0/654 samples\n", + "Loss: 0.00577 | Train Acc: 0.193 | Looked at 320/654 samples\n", + "Loss: 0.00329 | Train Acc: 0.200 | Looked at 280/654 samples\n", "\n", "Testing for Epoch: 4\n", "----------\n", - "Test loss: 2.29248 | Test accuracy: 8.33%\n", + "Test loss: 0.08137 | Test accuracy: 0.42%\n", "\n", - "Total training time: 161.1517569271382\n" + "Total training time: 165.23413556395099\n" ] } ],