update 05 materials

This commit is contained in:
Daniel Bourke committed 2022-05-11 10:26:28 +10:00
1 parent d266652368
commit 7a93928ee0
3 files changed
+494 -357

No files matched your search

+3 -3
View File
@@ -57,7 +57,7 @@ I usually start machine learning projects in Jupyter/Google Colab notebooks for
Then when I've got something working, I move the most useful pieces of code to Python scripts.
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-my-workflow-for-experimenting.png" alt="one possible workflow for writing machine learning code, start with jupyter or google colab notebooks and then move to Python scripts when you've got something working." width=800/>
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-my-workflow-for-experimenting.png" alt="one possible workflow for writing machine learning code, start with jupyter or google colab notebooks and then move to Python scripts when you've got something working." width=1000/>
*There are many possible workflows for writing machine learning code. Some prefer to start with scripts, others (like me) prefer to start with notebooks and go to scripts later on.*
@@ -71,7 +71,7 @@ For example, you might be instructred to run code like the following in a termin
python train.py --model MODEL_NAME --batch_size BATCH_SIZE --lr LEARNING_RATE --num_epochs NUM_EPOCHS
```
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-python-train-command-line-annotated.png" alt="command line call for training a PyTorch model with different hyperparameters" width=800/>
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-python-train-command-line-annotated.png" alt="command line call for training a PyTorch model with different hyperparameters" width=1000/>
*Running a PyTorch `train.py` script on the command line with various hyperparameter settings.*
@@ -114,7 +114,7 @@ Because sometimes the best way to learn something is to see how it *differs* fro
If you run each notebook side-by-side you'll see how they differ and that's where the key learnings are.
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-notebook-cell-mode-vs-script-mode.png" alt="running cell mode notebook vs a script mode notebook" width=800/>
<img src="https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/05-notebook-cell-mode-vs-script-mode.png" alt="running cell mode notebook vs a script mode notebook" width=1000/>
*Running the two notebooks for section 05 side-by-side. You'll notice that the **script mode notebook has extra code cells** to turn code from the cell mode notebook into Python scripts.*
@@ -16,16 +16,64 @@
"id": "1M5tsObEa17-"
},
"source": [
"# 05. Going Modular (cell mode)\n",
"# 05. Going Modular: Part 1 (cell mode)\n",
"\n",
"* TK - this notebook condenses the code from 04 into a handful of cells.\n",
"* TK - there will another version of this notebook \"script mode\" that turns these handful of cells into a handful of scripts\n",
"This notebook is part 1/2 of section [05. Going Modular](https://www.learnpytorch.io/05_pytorch_going_modular/).\n",
"\n",
"Turn notebook -> python scripts\n",
"For reference, the two parts are: \n",
"1. [**05. Going Modular: Part 1 (cell mode)**](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/going_modular/05_pytorch_going_modular_cell_mode.ipynb) - this notebook is run as a traditional Jupyter Notebook/Google Colab notebook and is a condensed version of [notebook 04](https://www.learnpytorch.io/04_pytorch_custom_datasets/).\n",
"2. [**05. Going Modular: Part 2 (script mode)**](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/going_modular/05_pytorch_going_modular_script_mode.ipynb) - this notebook is the same as number 1 but with added functionality to turn each of the major sections into Python scripts, such as, `data_setup.py` and `train.py`. \n",
"\n",
"Code in this notebook based off code in notebook 04 with minimal changes.\n",
"Why two parts?\n",
"\n",
"TK - get GPU (for Google Colab)"
"Because sometimes the best way to learn something is to see how it *differs* from something else.\n",
"\n",
"If you run each notebook side-by-side you'll see how they differ and that's where the key learnings are."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1M5tsObEa17-"
},
"source": [
"## What is cell mode?\n",
"\n",
"A cell mode notebook is a regular notebook run exactly how we've been running them through the course.\n",
"\n",
"Some cells contain text and others contain code."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1M5tsObEa17-"
},
"source": [
"## What's the difference between this notebook (Part 1) and the script mode notebook (Part 2)?\n",
"\n",
"This notebook, 05. PyTorch Going Modular: Part 1 (cell mode), runs a cleaned up version of the most useful code from section [04. PyTorch Custom Datasets](https://www.learnpytorch.io/04_pytorch_custom_datasets/).\n",
"\n",
"Running this notebook end-to-end will result in recreating the image classification model we built in notebook 04 (TinyVGG) trained on images of pizza, steak and sushi.\n",
"\n",
"The main difference between this notebook (Part 1) and Part 2 is that each section in Part 2 (script mode) has an extra subsection (e.g. 2.1, 3.1, 4.1) for turning cell code into script code."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1M5tsObEa17-"
},
"source": [
"## Where can you get help?\n",
"\n",
"You can find the book version of this section [05. PyTorch Going Modular on learnpytorch.io](https://www.learnpytorch.io/05_pytorch_going_modular/).\n",
"\n",
"The rest of the materials for this course [are available on GitHub](https://github.com/mrdbourke/pytorch-deep-learning).\n",
"\n",
"If you run into trouble, you can ask a question on the course [GitHub Discussions page](https://github.com/mrdbourke/pytorch-deep-learning/discussions).\n",
"\n",
"And of course, there's the [PyTorch documentation](https://pytorch.org/docs/stable/index.html) and [PyTorch developer forums](https://discuss.pytorch.org/), a very helpful place for all things PyTorch. "
]
},
{
@@ -34,9 +82,13 @@
"id": "I_0BPjLcawE-"
},
"source": [
"## TK - 0. Running a notebook in cell mode\n",
"## 0. Running a notebook in cell mode\n",
"\n",
"TK - What is cell mode? ... running a notebook normally."
"As discussed, we're going to be running this notebook normally.\n",
"\n",
"One cell at a time.\n",
"\n",
"The code is from notebook 04, however, it has been condensed down to its core functionality."
]
},
{
@@ -45,7 +97,7 @@
"id": "1XgXFr97bCsI"
},
"source": [
"## TK - 1. Get data\n",
"## 1. Get data\n",
"\n",
"We're going to start by downloading the same data we used in [notebook 04](https://www.learnpytorch.io/04_pytorch_custom_datasets/#1-get-data), the `pizza_steak_sushi` dataset with images of pizza, steak and sushi."
]
@@ -140,7 +192,7 @@
"id": "eYL6NynybU9Z"
},
"source": [
"## TK - 2. Create Datasets and DataLoaders\n",
"## 2. Create Datasets and DataLoaders\n",
"\n",
"Now we'll turn the image dataset into PyTorch `Dataset`'s and `DataLoader`'s. "
]
@@ -298,8 +350,8 @@
{
"data": {
"text/plain": [
"(<torch.utils.data.dataloader.DataLoader at 0x7fcc48a20c40>,\n",
" <torch.utils.data.dataloader.DataLoader at 0x7fcc48a20be0>)"
"(<torch.utils.data.dataloader.DataLoader at 0x7f853747bbe0>,\n",
" <torch.utils.data.dataloader.DataLoader at 0x7f853747b550>)"
]
},
"execution_count": 7,
@@ -547,7 +599,7 @@
"id": "8OB4xIsXcHrp"
},
"source": [
"## TK - 4. Creating `train_step()` and `test_step()` functions and `train()` to combine them \n",
"## 4. Creating `train_step()` and `test_step()` functions and `train()` to combine them \n",
"\n",
"Rather than writing them again, we can reuse the `train_step()` and `test_step()` functions from [notebook 04](https://www.learnpytorch.io/04_pytorch_custom_datasets/#75-create-train-test-loop-functions).\n",
"\n",
@@ -884,7 +936,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "54c42865cdff4fee8e2bb710e947e313",
"model_id": "803ff7aab243491e8451a74a5722b18d",
"version_major": 2,
"version_minor": 0
},
@@ -899,12 +951,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch: 1 | train_loss: 1.0956 | train_acc: 0.3867 | test_loss: 1.0629 | test_acc: 0.4133\n",
"Epoch: 2 | train_loss: 1.0114 | train_acc: 0.5067 | test_loss: 1.0214 | test_acc: 0.4533\n",
"Epoch: 3 | train_loss: 0.9571 | train_acc: 0.5689 | test_loss: 0.9993 | test_acc: 0.4533\n",
"Epoch: 4 | train_loss: 0.8963 | train_acc: 0.5733 | test_loss: 1.0074 | test_acc: 0.4667\n",
"Epoch: 5 | train_loss: 0.8658 | train_acc: 0.6356 | test_loss: 1.0082 | test_acc: 0.5200\n",
"[INFO] Total training time: 5.185 seconds\n",
"Epoch: 1 | train_loss: 1.0955 | train_acc: 0.3867 | test_loss: 1.0627 | test_acc: 0.4133\n",
"Epoch: 2 | train_loss: 1.0102 | train_acc: 0.5200 | test_loss: 1.0222 | test_acc: 0.4400\n",
"Epoch: 3 | train_loss: 0.9552 | train_acc: 0.5822 | test_loss: 1.0067 | test_acc: 0.4667\n",
"Epoch: 4 | train_loss: 0.8913 | train_acc: 0.5867 | test_loss: 1.0090 | test_acc: 0.4400\n",
"Epoch: 5 | train_loss: 0.8608 | train_acc: 0.6311 | test_loss: 1.0214 | test_acc: 0.4533\n",
"[INFO] Total training time: 5.859 seconds\n",
"[INFO] Saving model to: models/05_going_modular_cell_mode_tinyvgg_model.pth\n"
]
}
@@ -948,6 +1000,15 @@
" target_dir=\"models\",\n",
" model_name=\"05_going_modular_cell_mode_tinyvgg_model.pth\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We finish with a saved image classification model at `models/05_going_modular_cell_mode_tinyvgg_model.pth`.\n",
"\n",
"The code continues in [05. Going Modular: Part 2 (script mode)](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/going_modular/05_pytorch_going_modular_script_mode.ipynb)."
]
}
],
"metadata": {
File diff suppressed because it is too large. Load diff