{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "02_pytorch_classification_exercise_solutions.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMN00jzQMAwFrBAxxyYY+e3",
"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": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# 02. PyTorch Classification Exercise Solutions\n",
"\n",
"The following is one possible set (there may be more than one way to do things) of solutions for the 02. PyTorch WorkFlow Exercise template.\n",
"\n",
"You can see a live [walkthrough of the solutions (errors and all) on YouTube](https://youtu.be/ByyHwoEgF0Q).\n",
"\n",
"See [other solutions on the course GitHub](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/solutions)."
],
"metadata": {
"id": "ZKJFt7YxH8yl"
}
},
{
"cell_type": "code",
"source": [
"# Check for GPU\n",
"!nvidia-smi"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bUDhp5i0IHps",
"outputId": "576d649c-6289-4c00-f7b5-a05783f0daeb"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Thu Feb 10 00:20:37 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 P100-PCIE... Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 33C P0 26W / 250W | 0MiB / 16280MiB | 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",
"# Setup device agnostic code\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"device"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "CSrUPgapO0tf",
"outputId": "eafe3b29-44fd-4cf2-fdac-6fee7ba1fbb5"
},
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'cuda'"
]
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "markdown",
"source": [
"## 1. Make a binary classification dataset with Scikit-Learn's [`make_moons()`](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_moons.html) function.\n",
" * For consistency, the dataset should have 1000 samples and a `random_state=42`.\n",
" * Turn the data into PyTorch tensors. \n",
" * Split the data into training and test sets using `train_test_split` with 80% training and 20% testing."
],
"metadata": {
"id": "pH7jIZ2SPFee"
}
},
{
"cell_type": "code",
"source": [
"from sklearn.datasets import make_moons\n",
"\n",
"NUM_SAMPLES = 1000\n",
"RANDOM_SEED = 42\n",
"\n",
"X, y = make_moons(n_samples=NUM_SAMPLES,\n",
" noise=0.07,\n",
" random_state=RANDOM_SEED)\n",
"\n",
"X[:10], y[:10]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5t4VhPV1PX1X",
"outputId": "ce54b01f-13d7-4575-d328-451d2b16eed8"
},
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(array([[-0.03341062, 0.4213911 ],\n",
" [ 0.99882703, -0.4428903 ],\n",
" [ 0.88959204, -0.32784256],\n",
" [ 0.34195829, -0.41768975],\n",
" [-0.83853099, 0.53237483],\n",
" [ 0.59906425, -0.28977331],\n",
" [ 0.29009023, -0.2046885 ],\n",
" [-0.03826868, 0.45942924],\n",
" [ 1.61377123, -0.2939697 ],\n",
" [ 0.693337 , 0.82781911]]), array([1, 1, 1, 1, 0, 1, 1, 1, 1, 0]))"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"# Turn data into a DataFrame\n",
"import pandas as pd\n",
"data_df = pd.DataFrame({\"X0\": X[:, 0],\n",
" \"X1\": X[:, 1],\n",
" \"y\": y})\n",
"data_df.head()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "SUeHZ3-3P9C7",
"outputId": "5a1883bb-5f38-4fa8-adf8-b217bd11b8f2"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
| \n", " | X0 | \n", "X1 | \n", "y | \n", "
|---|---|---|---|
| 0 | \n", "-0.033411 | \n", "0.421391 | \n", "1 | \n", "
| 1 | \n", "0.998827 | \n", "-0.442890 | \n", "1 | \n", "
| 2 | \n", "0.889592 | \n", "-0.327843 | \n", "1 | \n", "
| 3 | \n", "0.341958 | \n", "-0.417690 | \n", "1 | \n", "
| 4 | \n", "-0.838531 | \n", "0.532375 | \n", "0 | \n", "