add solutions notebook for 04

This commit is contained in:
Daniel Bourke committed 2022-04-14 12:20:16 +10:00
1 parent 5f18e12236
commit bfe4f6c538
1 file changed
+301
@@ -0,0 +1,301 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "04_pytorch_custom_datasets_exercise_solutions.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPOuvL8INOro3NtVpwTfpO9",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/mrdbourke/pytorch-deep-learning/blob/main/extras/solutions/04_pytorch_custom_datasets_exercise_solutions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# 04. PyTorch Custom Datasets Exercise Solutions\n",
"\n",
"The following is one possible set (there may be more than one way to do things) of solutions for the 04. PyTorch Custom Datasets exercise template.\n",
"\n",
"## Resources\n",
"\n",
"1. These exercises/solutions are based on [notebook 04 of the Learn PyTorch for Deep Learning course](https://www.learnpytorch.io/04_pytorch_custom_datasets/).\n",
"2. TK - See a live [walkthrough of the solutions (errors and all) on YouTube](https://youtu.be/ByyHwoEgF0Q).\n",
"3. See [other solutions on the course GitHub](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/solutions)."
],
"metadata": {
"id": "Vex99np2wFVt"
}
},
{
"cell_type": "code",
"source": [
"# Check for GPU\n",
"!nvidia-smi"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GaeYzOTLwWh2",
"outputId": "d09f3563-f5c5-4b44-df8d-fd3c5f9734de"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Thu Apr 14 02:08:18 2022 \n",
"+-----------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |\n",
"|-------------------------------+----------------------+----------------------+\n",
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|===============================+======================+======================|\n",
"| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 35C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |\n",
"| | | N/A |\n",
"+-------------------------------+----------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=============================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------+\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Import torch\n",
"import torch\n",
"\n",
"# Exercises require PyTorch > 1.10.0\n",
"print(torch.__version__)\n",
"\n",
"# Setup device agnostic code\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"device"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "DNwZLMbCzJLk",
"outputId": "cb266dbc-b1e7-435d-c111-364872ffb371"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1.10.0+cu111\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'cuda'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "markdown",
"source": [
"## 1. Our models are underperforming (not fitting the data well). What are 3 methods for preventing underfitting? Write them down and explain each with a sentence."
],
"metadata": {
"id": "FSFX7tc1w-en"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "mLY-VCWIyEgi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 2. Recreate the data loading functions we built in sections 1, 2, 3 and 4 of notebook 04. You should have train and test `DataLoader`'s ready to use."
],
"metadata": {
"id": "oBK-WI6YxDYa"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "qhewzwPcyEMI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 3. Recreate `model_0` we built in section 7 of notebook 04."
],
"metadata": {
"id": "XeYFEqw8xK26"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "MBErjcUCyDzE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 4. Create training and testing functions for `model_0`."
],
"metadata": {
"id": "DKdEEFEqxM-8"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "rnUox1qayDes"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 5. Try training the model you made in exercise 3 for 5, 20 and 50 epochs, what happens to the results?"
],
"metadata": {
"id": "lvf-3pODxXYI"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "rV7s2qtIyDIZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 6. Double the number of layers in your model and train it for 20 epochs, what happens to the results?"
],
"metadata": {
"id": "qxZW-uAbxe_F"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "HdRM86voyC0x"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 7. Double the data you're using with your model and train it for 20 epochs, what happens to the results?\n",
"* **Note:** You can use the [custom data creation notebook](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/04_custom_data_creation.ipynb) to scale up your Food101 dataset."
],
"metadata": {
"id": "JAPDzW0wxhi3"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "8tWfa7Y0yCkX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 8. Make a prediction on your own custom image of pizza/steak/sushi (you could even download one from the internet) and share your prediction. \n",
"* Does the model you trained in exercise 7 get it right? \n",
"* If not, what do you think you could do to improve it?"
],
"metadata": {
"id": "bCCVfXk5xjYS"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "xeBNV2AtyCP6"
},
"execution_count": null,
"outputs": []
}
]
}