add more annotations for 04

This commit is contained in:
Daniel Bourke committed 2022-04-03 17:00:00 +10:00
1 parent ebc127a78f
commit 42c2c69a3f
1 file changed
+234 -162
+234 -162
View File
@@ -2213,36 +2213,46 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create train & test functions\n",
"### TK - Create train & test loop functions \n",
"\n",
"* TK - after 04, functionize everything\n",
"* TK - make the functions below the same as the previous notebook\n",
"* TK - up to here, fix the accumulation of loss and accuracy metrics\n",
"We've got data and we've got a model.\n",
"\n",
"UPTOHERE: fix the accumulation of loss and accuracy metrics"
"Now let's make some training and test loop functions to train our model on the training data and evaluate our model on the testing data.\n",
"\n",
"And to make sure we can use these the training and testing loops again, we'll functionize them.\n",
"\n",
"Specifically, we're going to make three functions:\n",
"1. `train_step()` - takes in a model, a `DataLoader`, a loss function and an optimizer and trains the model on the `DataLoader`.\n",
"2. `test_step()` - takes in a model, a `DataLoader` and a loss function and evaluates the model on the `DataLoader`.\n",
"3. `train()` - performs 1. and 2. together for a given number of epochs and returns a results dictionary.\n",
"\n",
"> **Note:** We covered the steps in a PyTorch opimization loop in [notebook 01](https://www.learnpytorch.io/01_pytorch_workflow/#creating-an-optimization-loop-in-pytorch), as well as the[ Unofficial PyTorch Optimization Loop Song](https://youtu.be/Nutpusq_AFw) and we've built similar functions in [notebook 03](https://www.learnpytorch.io/03_pytorch_computer_vision/#62-functionizing-training-and-test-loops).\n",
"\n",
"Let's start by building `train_step()`.\n",
"\n",
"Because we're dealing with batches in the `DataLoader`'s, we'll accumulate the model loss and accuracy values during training (by adding them up for each batch) and then adjust them at the end before we return them."
]
},
{
"cell_type": "code",
"execution_count": 215,
"execution_count": 309,
"metadata": {},
"outputs": [],
"source": [
"from tqdm.auto import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 280,
"metadata": {},
"outputs": [],
"source": [
"def train_step(model, dataloader, loss_fn, optimizer):\n",
"def train_step(model: torch.nn.Module, \n",
" dataloader: torch.utils.data.DataLoader, \n",
" loss_fn: torch.nn.Module, \n",
" optimizer: torch.optim):\n",
" # Put model in train mode\n",
" model.train()\n",
" \n",
" # Setup train loss and train accuracy values\n",
" train_loss, train_acc = 0, 0\n",
" \n",
" # Loop through data loader data batches\n",
" for batch, (X, y) in enumerate(dataloader):\n",
" # Send data to GPU\n",
" X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)\n",
" # Send data to target device\n",
" X, y = X.to(device), y.to(device)\n",
"\n",
" # 1. Forward pass\n",
" y_pred = model(X)\n",
@@ -2260,32 +2270,50 @@
" # 5. Optimizer step\n",
" optimizer.step()\n",
"\n",
" # 6. Calculate metrics\n",
" \n",
" # Calculate and accumulate accuracy metric across all batches\n",
" y_pred_class = torch.argmax(torch.softmax(y_pred, dim=1), dim=1)\n",
" train_acc += (y_pred_class == y).sum().item()/len(y_pred)\n",
"\n",
" # Adjust metrics to get average loss and accuracy per batch \n",
" train_loss /= len(dataloader)\n",
" train_acc /= len(dataloader)\n",
" train_loss = train_loss / len(dataloader)\n",
" train_acc = train_acc / len(dataloader)\n",
" return train_loss, train_acc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Woohoo! `train_step()` function done.\n",
"\n",
"Now let's do the same for the `test_step()` function.\n",
"\n",
"The main difference here will be the `test_step()` won't take in an optimizer and therefore won't perform gradient descent.\n",
"\n",
"But since we'll be doing inference, we'll make sure to turn on the `torch.inference_mode()` context manager for making predictions."
]
},
{
"cell_type": "code",
"execution_count": 287,
"metadata": {},
"outputs": [],
"source": [
"def test_step(model, dataloader, loss_fn):\n",
" model.eval() # put model in eval mode\n",
"def test_step(model: torch.nn.Module, \n",
" dataloader: torch.utils.data.DataLoader, \n",
" loss_fn: torch.optim):\n",
" # Put model in eval mode\n",
" model.eval() \n",
" \n",
" # Setup test loss and test accuracy values\n",
" test_loss, test_acc = 0, 0\n",
" \n",
" # Turn on inference context manager\n",
" with torch.inference_mode():\n",
" # Loop through DataLoader batches\n",
" for batch, (X, y) in enumerate(dataloader):\n",
" # Send data to GPU\n",
" X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)\n",
" # Send data to target device\n",
" X, y = X.to(device), y.to(device)\n",
" \n",
" # 1. Forward pass\n",
" test_pred_logits = model(X)\n",
@@ -2304,25 +2332,53 @@
" return test_loss, test_acc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Excellent!\n",
"\n",
"Now we need a way to put our `train_step()` and `test_step()` functions together.\n",
"\n",
"To do so, we'll package them up in a `train()` function.\n",
"\n",
"This function will train the model as well as evaluate it.\n",
"\n",
"Specificially, it'll:\n",
"1. Take in a model, a `DataLoader` for training and test sets, an optimizer, a loss function and how many epochs to perform each train and test step for.\n",
"2. Create an empty results dictionary for `train_loss`, `train_acc`, `test_loss` and `test_acc` values (we can fill this up as training goes on).\n",
"3. Loop through the training and test step functions for a number of epochs.\n",
"4. Print out what's happening at the end of each epoch.\n",
"5. Update the empty results dictionary with the updated metrics each epoch.\n",
"6. Return the filled\n",
"\n",
"To keep track of the number of epochs we've been through, let's import `tqdm` from `tqdm.auto` ([`tqdm`](https://github.com/tqdm/tqdm) is one of the most popular progress bar libraries for Python and `tqdm.auto` automatically decides what kind of progress bar is best for your computing environment, e.g. Jupyter Notebook vs. Python script)."
]
},
{
"cell_type": "code",
"execution_count": 288,
"execution_count": 314,
"metadata": {},
"outputs": [],
"source": [
"def train(model, \n",
" train_dataloader, \n",
" test_dataloader, \n",
" optimizer,\n",
" loss_fn=nn.CrossEntropyLoss(),\n",
" epochs=5):\n",
"from tqdm.auto import tqdm\n",
"\n",
"# 1. Take in various parameters required for training and test steps\n",
"def train(model: torch.nn.Module, \n",
" train_dataloader: torch.utils.data.DataLoader, \n",
" test_dataloader: torch.utils.data.DataLoader, \n",
" optimizer: torch.optim,\n",
" loss_fn: torch.nn.Module=nn.CrossEntropyLoss(),\n",
" epochs: int=5):\n",
" \n",
" # 2. Create empty results dictionary\n",
" results = {\"train_loss\": [],\n",
" \"train_acc\": [],\n",
" \"test_loss\": [],\n",
" \"test_acc\": []\n",
" }\n",
"\n",
" \n",
" # 3. Loop through training and testing steps for a number of epochs\n",
" for epoch in tqdm(range(epochs)):\n",
" train_loss, train_acc = train_step(model=model,\n",
" dataloader=train_dataloader,\n",
@@ -2332,7 +2388,7 @@
" dataloader=test_dataloader,\n",
" loss_fn=loss_fn)\n",
" \n",
" # Print out what's happening\n",
" # 4. Print out what's happening\n",
" print(\n",
" f\"Epoch: {epoch+1} | \"\n",
" f\"train_loss: {train_loss:.4f} | \"\n",
@@ -2341,29 +2397,49 @@
" f\"test_acc: {test_acc:.4f}\"\n",
" )\n",
"\n",
" # Update results\n",
" # 5. Update results dictionary\n",
" results[\"train_loss\"].append(train_loss)\n",
" results[\"train_acc\"].append(train_acc)\n",
" results[\"test_loss\"].append(test_loss)\n",
" results[\"test_acc\"].append(test_acc)\n",
"\n",
" # 6. Return the filled results at the end of the epochs\n",
" return results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - Train and Evaluate Model 0\n",
"\n",
"Alright, alright, alright we've got all of the ingredients we need to train and evaluate our model.\n",
"\n",
"Time to put our `TinyVGG` model, `DataLoader`'s and `train()` function together to see if we can build a model capable of discerning between pizza, steak and sushi!\n",
"\n",
"Let's recreate `model_0` (we don't need to but we will for completeness) then call our `train()` function passing in the necessary parameters.\n",
"\n",
"To keep our experiments quick, we'll train our model for **5 epochs** (though you could increase this if you want).\n",
"\n",
"As for an **optimizer** and **loss function**, we'll use `torch.nn.CrossEntropyLoss()` (since we're working with multi-class classification data) and `torch.optim.Adam()` with a learning rate of `1e-3` respecitvely.\n",
"\n",
"To see how long things take, we'll import Python's [`timeit.default_timer()`](https://docs.python.org/3/library/timeit.html#timeit.default_timer) method to calculate the training time."
]
},
{
"cell_type": "code",
"execution_count": 289,
"execution_count": 316,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b497a60a5f814f95b857cb6626f1365a",
"model_id": "1f5c814e1a9c4cfb80d82a9fd8a1437b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/20 [00:00<?, ?it/s]"
" 0%| | 0/5 [00:00<?, ?it/s]"
]
},
"metadata": {},
@@ -2377,56 +2453,112 @@
"Epoch: 2 | train_loss: 1.0847 | train_acc: 0.4258 | test_loss: 1.1620 | test_acc: 0.1979\n",
"Epoch: 3 | train_loss: 1.1157 | train_acc: 0.2930 | test_loss: 1.1697 | test_acc: 0.1979\n",
"Epoch: 4 | train_loss: 1.0955 | train_acc: 0.4141 | test_loss: 1.1385 | test_acc: 0.1979\n",
"Epoch: 5 | train_loss: 1.0985 | train_acc: 0.2930 | test_loss: 1.1429 | test_acc: 0.1979\n",
"Epoch: 6 | train_loss: 1.0956 | train_acc: 0.2500 | test_loss: 1.1466 | test_acc: 0.2604\n",
"Epoch: 7 | train_loss: 1.1188 | train_acc: 0.3047 | test_loss: 1.1419 | test_acc: 0.2708\n",
"Epoch: 8 | train_loss: 1.1038 | train_acc: 0.3398 | test_loss: 1.1098 | test_acc: 0.1979\n",
"Epoch: 9 | train_loss: 1.0960 | train_acc: 0.4336 | test_loss: 1.0917 | test_acc: 0.5417\n",
"Epoch: 10 | train_loss: 1.0918 | train_acc: 0.4023 | test_loss: 1.0800 | test_acc: 0.5417\n",
"Epoch: 11 | train_loss: 1.0866 | train_acc: 0.4023 | test_loss: 1.0661 | test_acc: 0.5417\n",
"Epoch: 12 | train_loss: 1.1265 | train_acc: 0.2812 | test_loss: 1.0497 | test_acc: 0.5417\n",
"Epoch: 13 | train_loss: 1.0819 | train_acc: 0.4023 | test_loss: 1.0664 | test_acc: 0.5417\n",
"Epoch: 14 | train_loss: 1.0867 | train_acc: 0.2969 | test_loss: 1.0623 | test_acc: 0.5938\n",
"Epoch: 15 | train_loss: 1.0397 | train_acc: 0.5195 | test_loss: 1.0820 | test_acc: 0.3305\n",
"Epoch: 16 | train_loss: 0.9473 | train_acc: 0.5000 | test_loss: 1.0744 | test_acc: 0.3617\n",
"Epoch: 17 | train_loss: 0.9654 | train_acc: 0.6289 | test_loss: 1.0315 | test_acc: 0.3731\n",
"Epoch: 18 | train_loss: 0.9355 | train_acc: 0.5859 | test_loss: 1.0057 | test_acc: 0.4034\n",
"Epoch: 19 | train_loss: 0.8851 | train_acc: 0.5430 | test_loss: 1.0108 | test_acc: 0.5246\n",
"Epoch: 20 | train_loss: 1.1535 | train_acc: 0.5117 | test_loss: 1.1068 | test_acc: 0.4545\n",
"Total training time: 20.229 seconds\n"
"Epoch: 5 | train_loss: 1.0985 | train_acc: 0.2930 | test_loss: 1.1428 | test_acc: 0.1979\n",
"Total training time: 5.227 seconds\n"
]
}
],
"source": [
"# Train\n",
"# Set random seeds\n",
"torch.manual_seed(42) \n",
"torch.cuda.manual_seed(42)\n",
"\n",
"# torch.manual_seed(42)\n",
"# Set number of epochs\n",
"NUM_EPOCHS = 5\n",
"\n",
"# Recreate an instance of TinyVGG\n",
"model_0 = TinyVGG(input_shape=3, # number of color channels (3 for RGB) \n",
" hidden_units=10, \n",
" output_shape=len(train_data.classes)).to(device)\n",
"\n",
"# Setup loss function and optimizer\n",
"loss_fn = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.Adam(params=model_0.parameters(), lr=0.001)\n",
"\n",
"# Start the timer\n",
"from timeit import default_timer as timer \n",
"start_time = timer()\n",
"NUM_EPOCHS = 20\n",
"\n",
"# Train model_0 \n",
"model_0_results = train(model=model_0, \n",
" train_dataloader=train_dataloader_simple,\n",
" test_dataloader=test_dataloader_simple,\n",
" optimizer=torch.optim.Adam(model_0.parameters(), lr=0.001),\n",
" epochs=NUM_EPOCHS,\n",
" loss_fn=nn.CrossEntropyLoss())\n",
" train_dataloader=train_dataloader_simple,\n",
" test_dataloader=test_dataloader_simple,\n",
" optimizer=optimizer,\n",
" loss_fn=loss_fn, \n",
" epochs=NUM_EPOCHS)\n",
"\n",
"# End the timer and print out how long it took\n",
"end_time = timer()\n",
"print(f\"Total training time: {end_time-start_time:.3f} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hmm...\n",
"\n",
"It looks like our model performed pretty poorly.\n",
"\n",
"But that's okay for now, we'll keep persevering.\n",
"\n",
"What are some ways you could potentially improve it?\n",
"\n",
"> **Note:** Check out the [*Improving a model (from a model perspective)* section in notebook 02](https://www.learnpytorch.io/02_pytorch_classification/#5-improving-a-model-from-a-model-perspective) for ideas on improving our TinyVGG model."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - Plot the loss curves of Model 0 \n",
"\n",
"From the print outs of our `model_0` training, it didn't look like it did too well.\n",
"\n",
"But we can further evaluate it by plotting the model's **loss curves**. \n",
"\n",
"**Loss curves** show the model's results overtime.\n",
"\n",
"And they're a great way to see how your model performs on different datasets (e.g. training and test).\n",
"\n",
"Let's create a function to plot the values in our `model_1_results` dictionary."
]
},
{
"cell_type": "code",
"execution_count": 290,
"execution_count": 319,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['train_loss', 'train_acc', 'test_loss', 'test_acc'])"
]
},
"execution_count": 319,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the model_1_results keys\n",
"model_1_results.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll need to extract each of these keys and turn them into a plot."
]
},
{
"cell_type": "code",
"execution_count": 320,
"metadata": {},
"outputs": [],
"source": [
"def plot_loss_curves(results) -> Dict:\n",
"def plot_loss_curves(results: Dict):\n",
" \"\"\"Plots training curves of a results dictionary.\n",
"\n",
" Args:\n",
@@ -2436,14 +2568,19 @@
" \"test_loss\": [...],\n",
" \"test_acc\": [...]}\n",
" \"\"\"\n",
" \n",
" # Get the loss values of the results dictionary (training and test)\n",
" loss = results['train_loss']\n",
" test_loss = results['test_loss']\n",
"\n",
" # Get the accuracy values of the results dictionary (training and test)\n",
" accuracy = results['train_acc']\n",
" test_accuracy = results['test_acc']\n",
"\n",
" # Figure out how many epochs there were\n",
" epochs = range(len(results['train_loss']))\n",
"\n",
" # Setup a plot \n",
" plt.figure(figsize=(15, 7))\n",
"\n",
" # Plot loss\n",
@@ -2464,112 +2601,20 @@
]
},
{
"cell_type": "code",
"execution_count": 291,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'train_loss': [1.1078328490257263,\n",
" 1.08471167832613,\n",
" 1.115689292550087,\n",
" 1.0955432206392288,\n",
" 1.0985070019960403,\n",
" 1.0956041365861893,\n",
" 1.1188458800315857,\n",
" 1.103834792971611,\n",
" 1.0960264950990677,\n",
" 1.0917672663927078,\n",
" 1.086602546274662,\n",
" 1.1265168190002441,\n",
" 1.0819187760353088,\n",
" 1.0866748839616776,\n",
" 1.0396769046783447,\n",
" 0.9473312422633171,\n",
" 0.9654476642608643,\n",
" 0.9355220720171928,\n",
" 0.8851398974657059,\n",
" 1.1535415202379227],\n",
" 'train_acc': [0.2578125,\n",
" 0.42578125,\n",
" 0.29296875,\n",
" 0.4140625,\n",
" 0.29296875,\n",
" 0.25,\n",
" 0.3046875,\n",
" 0.33984375,\n",
" 0.43359375,\n",
" 0.40234375,\n",
" 0.40234375,\n",
" 0.28125,\n",
" 0.40234375,\n",
" 0.296875,\n",
" 0.51953125,\n",
" 0.5,\n",
" 0.62890625,\n",
" 0.5859375,\n",
" 0.54296875,\n",
" 0.51171875],\n",
" 'test_loss': [1.1360406875610352,\n",
" 1.1620153586069744,\n",
" 1.1696961323420207,\n",
" 1.1385366916656494,\n",
" 1.1428732872009277,\n",
" 1.1466145515441895,\n",
" 1.141941785812378,\n",
" 1.1098192135492961,\n",
" 1.0917060772577922,\n",
" 1.0800325473149617,\n",
" 1.0660730799039204,\n",
" 1.049661099910736,\n",
" 1.0664246479670207,\n",
" 1.0622962315877278,\n",
" 1.0819924871126811,\n",
" 1.0744053721427917,\n",
" 1.031545619169871,\n",
" 1.0057262380917866,\n",
" 1.0108049909273784,\n",
" 1.1068363587061565],\n",
" 'test_acc': [0.2604166666666667,\n",
" 0.19791666666666666,\n",
" 0.19791666666666666,\n",
" 0.19791666666666666,\n",
" 0.19791666666666666,\n",
" 0.2604166666666667,\n",
" 0.2708333333333333,\n",
" 0.19791666666666666,\n",
" 0.5416666666666666,\n",
" 0.5416666666666666,\n",
" 0.5416666666666666,\n",
" 0.5416666666666666,\n",
" 0.5416666666666666,\n",
" 0.59375,\n",
" 0.33049242424242425,\n",
" 0.36174242424242425,\n",
" 0.3731060606060606,\n",
" 0.4034090909090909,\n",
" 0.524621212121212,\n",
" 0.4545454545454546]}"
]
},
"execution_count": 291,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model_0_results"
"Okay, let's test our `plot_loss_curves()` function out."
]
},
{
"cell_type": "code",
"execution_count": 292,
"execution_count": 321,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3AAAAG5CAYAAADPktaCAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAADIxUlEQVR4nOzdd3ib5dXA4d8jeUse8XamkzjL2ZNAIGwSCAQKYX6UFgoUSlsohRZaWrpoaaEtq0CBUkbZs5SRMENYWUDIcOI4iTO97diSbEu2rPf745Ucx/G2pFfj3NeVS4m1jrOko3Oec5SmaQghhBBCCCGECH0mowMQQgghhBBCCNE3ksAJIYQQQgghRJiQBE4IIYQQQgghwoQkcEIIIYQQQggRJiSBE0IIIYQQQogwIQmcEEIIIYQQQoQJSeCEEEIIIYQQIkxIAidEgCildiulTjE6DiGEECKQlFIrlVIHlVLxRsciRDSQBE4IIYQQQgyIUiofOA7QgKVBfN6YYD2XEKFGEjghgkgpFa+UukcpVeb9cY/vE0ulVKZS6k2lVL1Sqk4p9YlSyuS97udKqQNKKbtSqlgpdbKx34kQQggBwGXAauAJ4Du+LyqlRiilXlVKVSulapVSD3S47iql1Fbva1qRUmqW9+uaUqqgw+2eUEr9wfvzE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JyDoWFhURFRelA6AoZY4iKitKogIiL6T3Lc+h9Ua6ECjgRkfPQgZBz6OcoUjn0f81z6N9KLpcKOBEREREREQ+hAk5ERERERMRDqIATEXFTx44d4/XXX7/kx11//fUcO3bskh83ZswYPv7440t+nIgIVP57lkhVpQJORMRNnetgqKys7LyP++qrr4iIiHBRKhGRs/PW96wL5RepbGojICJyEZ77cgtb04879Tlb1Ann2RtannP7k08+SXJyMu3atcPf359q1apRu3ZtNmzYwNatW7npppvYv38/hYWFPPTQQ4wbNw6A+Ph41q5dS15eHgMHDuSqq65i+fLl1K1bl88//5zg4OALZvv+++957LHHKC0tpXPnzkyaNInAwECefPJJvvjiC/z8/Lj22mt5+eWX+eijj3juuefw9fWlevXqLF682Gk/IxG5PFXhPeuNN95g6tSpFBcX06hRI2bMmEFISAiHDh1iwoQJpKSkADBp0iR69OjBu+++y8svv4wxhjZt2jBjxgzGjBnD4MGDufXWWwGoVq0aeXl5LFy4kOeee+6i8s+fP5+nn36asrIyoqOj+e6772jatCnLly8nJiaG8vJymjRpwsqVK4mOjnbmP4lUUSrgRETc1IsvvsjmzZvZsGEDCxcuZNCgQWzevPlUz6Bp06YRGRnJiRMn6Ny5M7fccgtRUVG/eI5du3Yxc+ZM3njjDYYPH84nn3zCqFGjzrvfwsJCxowZw/fff0+TJk246667mDRpEnfddRdz5sxh+/btGGNOTXl6/vnn+eabb6hbt66mQYlUYZX9nnXzzTdz3333AfDMM8/w1ltv8eCDD/K73/2OPn36MGfOHMrKysjLy2PLli288MILLFu2jOjoaLKzsy/4elavXn3B/OXl5dx3330sXryYhIQEsrOz8fHxYdSoUbz//vs8/PDDLFiwgLZt26p4E6dRAScichHOd9a5snTp0uUXDV//85//MGfOHAD279/Prl27fnUwlJCQQLt27QDo2LEjqampF9zPjh07SEhIoEmTJgCMHj2a1157jQceeICgoCDGjh3LoEGDGDx4MAA9e/ZkzJgxDB8+nJtvvtkJr1RErlRVeM/avHkzzzzzDMeOHSMvL4/rrrsOgB9++I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05+/2MIHq/ZRWl7OxL6JPH5dM1ty6D3Lvaxdu5ZHHnmEJUuWnPM++jeT8znXZ6RG4ERcJSsZkmY7RtuO7gG/IMcIW5vbILEf+Pr/fF9fPxg6GSZfBV/8Dm6fBWeZUy8iIu6lrNziq00Z9GsWQ7kFH6zax4NXNybIX1PmqrIXX3yRSZMm/WJqp4izqIATcab8LMcoW9Isx6gbBhJ6Qe/HofkNEBR+7sdGN4bf/BnmPwk/vQcd7qys1FLF/Pa3v2XZsmW/uO2hhx7SNB+Ry7A2NZvM3CIGt6lDVGgA3209xBcb0xneqb7d0byGJ75nPfnkkzz55JN2xxAvpQJO5EqVFMLOrx2jbbu+hfJSqNkCfvMctB4G1ete/HN1GQ/b58H8pyChN9Ro4LrcUmW99tprdkcQ8RpzkzII8vehf/OaBPv70qRWNaYvS2VYx3pnXZ1QLp3es0R+SQWcyOUoL4d9yx3XtW39HIqOQ7VY6DYR2oyA2NaX97w+PnDLine truncated
"text/plain": [
"<Figure size 1080x504 with 2 Axes>"
]
@@ -2584,6 +2629,30 @@
"plot_loss_curves(model_0_results)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Woah.\n",
"\n",
"Looks like things are all over the place...\n",
"\n",
"But we kind of knew that because our model's print out results during training didn't show much promise.\n",
"\n",
"You could try training the model for longer and see what happens when you plot a loss curve over a longer time horizon.\n",
"\n",
"### TK - What should an ideal loss curve look like?\n",
"UPTOHERE: what should an ideal loss curve look like?\n",
"\n",
"TK - overfitting and regularization\n",
"\n",
"* overfitting - \"if your training loss is way lower than your test loss, you're in the \"overfitting\" regime.\"\n",
"* underfitting - try to overfit\n",
"* if your training loss is identical to your validation loss, you're wasting time if you try to regularize your model...\n",
"\n",
"TK image - ideal loss curves (these could be a graphic)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -2986,6 +3055,8 @@
"source": [
"## TK - Make a prediction on a custom image...\n",
"\n",
"TK - get the image of Pizza Dad & load it in... \n",
"\n",
"TK - see here for Image io - https://pytorch.org/vision/stable/io.html#image \n",
"\n",
"TK - How could someone use the trained model to predict on an image of their own?\n",
@@ -2999,6 +3070,7 @@
"source": [
"## TK - Exercises\n",
"\n",
"* Try training the model for longer -> 5 epochs, 20 epochs, 50 epochs, what does this do to the results?\n",
"* Try a bigger model (more layers/hidden units) -> double the model\n",
"* Try a bigger dataset (double the data) -> double the data\n",
"* Try a different transform -> double the image size \n",