diff --git a/.gitignore b/.gitignore index 7df4764..c8cffdf 100644 --- a/.gitignore +++ b/.gitignore @@ -193,3 +193,8 @@ ipython_config.py data/** !data/**/ !**/.gitkeep + + + +models/** +!**/.gitkeep diff --git a/models/.gitkeep b/models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/notebooks/tutorial-cnn.ipynb b/notebooks/tutorial-cnn.ipynb index f490625..945c9f9 100644 --- a/notebooks/tutorial-cnn.ipynb +++ b/notebooks/tutorial-cnn.ipynb @@ -359,6 +359,80 @@ "# SGD is used on tutorial, but Adam optimizer is more popular as well\n", "optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) # Defualt learning rate from tutorial: compromise btwn overshoot and training infinitely long, 0.9 --> 90% of update comes from prev. direction\n" ] + }, + { + "cell_type": "markdown", + "id": "572d80e3", + "metadata": {}, + "source": [ + "Tutorial training cell adapted to use 10 epochs " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "374d0590", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1: Loss=1.575, Accuracy=50.54%\n", + "Epoch 2: Loss=1.196, Accuracy=60.35%\n", + "Epoch 3: Loss=1.018, Accuracy=64.98%\n", + "Epoch 4: Loss=0.909, Accuracy=68.58%\n", + "Epoch 5: Loss=0.842, Accuracy=70.87%\n", + "Epoch 6: Loss=0.784, Accuracy=72.83%\n", + "Epoch 7: Loss=0.738, Accuracy=74.24%\n", + "Epoch 8: Loss=0.703, Accuracy=75.30%\n", + "Epoch 9: Loss=0.671, Accuracy=76.30%\n", + "Epoch 10: Loss=0.641, Accuracy=77.64%\n", + "Finished Training\n" + ] + } + ], + "source": [ + "for epoch in range(10): # 10 epochs\n", + " running_loss = 0.0\n", + " correct = 0\n", + " total = 0\n", + "\n", + " for i, data in enumerate(train_loader, 0):\n", + " inputs, labels = data\n", + " inputs, labels = inputs.to(device), labels.to(device) # use GPU\n", + "\n", + " optimizer.zero_grad()\n", + "\n", + " outputs = model(inputs)\n", + " loss = criterion(outputs, labels)\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + " # loss tracking\n", + " running_loss += loss.item()\n", + "\n", + " # accuracy tracking\n", + " _, predicted = torch.max(outputs, 1)\n", + " total += labels.size(0)\n", + " correct += (predicted == labels).sum().item()\n", + "\n", + " accuracy = 100 * correct / total\n", + " print(f'Epoch {epoch + 1}: Loss={running_loss / len(train_loader):.3f}, Accuracy={accuracy:.2f}%') # Credit: Claude for printing accuracy in good formatting\n", + "\n", + "print('Finished Training')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "2bf2b9a2", + "metadata": {}, + "outputs": [], + "source": [ + "PATH = '../models/tutorial-cnn.pth'\n", + "torch.save(model.state_dict(), PATH)" + ] } ], "metadata": {