diff --git a/chapter_appendix-tools-for-deep-learning/jupyter.ipynb b/chapter_appendix-tools-for-deep-learning/jupyter.ipynb new file mode 100644 index 0000000..6d40a89 --- /dev/null +++ b/chapter_appendix-tools-for-deep-learning/jupyter.ipynb @@ -0,0 +1,584 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "31d9a4c9", + "metadata": { + "origin_pos": 0, + "id": "31d9a4c9" + }, + "source": [ + "# Using Jupyter Notebooks\n", + ":label:`sec_jupyter`\n", + "\n", + "\n", + "This section describes how to edit and run the code\n", + "in each section of this book\n", + "using the Jupyter Notebook. Make sure you have\n", + "installed Jupyter and downloaded the\n", + "code as described in\n", + ":ref:`chap_installation`.\n", + "If you want to know more about Jupyter see the excellent tutorial in\n", + "their [documentation](https://jupyter.readthedocs.io/en/latest/).\n", + "\n", + "\n", + "## Editing and Running the Code Locally\n", + "\n", + "Suppose that the local path of the book's code is `xx/yy/d2l-en/`. Use the shell to change the directory to this path (`cd xx/yy/d2l-en`) and run the command `jupyter notebook`. If your browser does not do this automatically, open http://localhost:8888 and you will see the interface of Jupyter and all the folders containing the code of the book, as shown in :numref:`fig_jupyter00`.\n", + "\n", + "![The folders containing the code of this book.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter00.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter00`\n", + "\n", + "\n", + "You can access the notebook files by clicking on the folder displayed on the webpage.\n", + "They usually have the suffix \".ipynb\".\n", + "For the sake of brevity, we create a temporary \"test.ipynb\" file.\n", + "The content displayed after you click it is\n", + "shown in :numref:`fig_jupyter01`.\n", + "This notebook includes a markdown cell and a code cell. The content in the markdown cell includes \"This Is a Title\" and \"This is text.\".\n", + "The code cell contains two lines of Python code.\n", + "\n", + "![Markdown and code cells in the \"text.ipynb\" file.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter01.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter01`\n", + "\n", + "\n", + "Double click on the markdown cell to enter edit mode.\n", + "Add a new text string \"Hello world.\" at the end of the cell, as shown in :numref:`fig_jupyter02`.\n", + "\n", + "![Edit the markdown cell.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter02.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter02`\n", + "\n", + "\n", + "As demonstrated in :numref:`fig_jupyter03`,\n", + "click \"Cell\" $\\rightarrow$ \"Run Cells\" in the menu bar to run the edited cell.\n", + "\n", + "![Run the cell.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter03.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter03`\n", + "\n", + "After running, the markdown cell is shown in :numref:`fig_jupyter04`.\n", + "\n", + "![The markdown cell after running.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter04.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter04`\n", + "\n", + "\n", + "Next, click on the code cell. Multiply the elements by 2 after the last line of code, as shown in :numref:`fig_jupyter05`.\n", + "\n", + "![Edit the code cell.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter05.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter05`\n", + "\n", + "\n", + "You can also run the cell with a shortcut (\"Ctrl + Enter\" by default) and obtain the output result from :numref:`fig_jupyter06`.\n", + "\n", + "![Run the code cell to obtain the output.](https://github.com/d2l-ai/d2l-en-colab/blob/master/img/jupyter06.png?raw=1)\n", + ":width:`600px`\n", + ":label:`fig_jupyter06`\n", + "\n", + "\n", + "When a notebook contains more cells, we can click \"Kernel\" $\\rightarrow$ \"Restart & Run All\" in the menu bar to run all the cells in the entire notebook. By clicking \"Help\" $\\rightarrow$ \"Edit Keyboard Shortcuts\" in the menu bar, you can edit the shortcuts according to your preferences.\n", + "\n", + "## Advanced Options\n", + "\n", + "Beyond local editing two things are quite important: editing the notebooks in the markdown format and running Jupyter remotely.\n", + "The latter matters when we want to run the code on a faster server.\n", + "The former matters since Jupyter's native ipynb format stores a lot of auxiliary data that is\n", + "irrelevant to the content,\n", + "mostly related to how and where the code is run.\n", + "This is confusing for Git, making\n", + "reviewing contributions very difficult.\n", + "Fortunately there is an alternative---native editing in the markdown format.\n", + "\n", + "### Markdown Files in Jupyter\n", + "\n", + "If you wish to contribute to the content of this book, you need to modify the\n", + "source file (md file, not ipynb file) on GitHub.\n", + "Using the notedown plugin we\n", + "can modify notebooks in the md format directly in Jupyter.\n", + "\n", + "\n", + "First, install the notedown plugin, run the Jupyter Notebook, and load the plugin:\n", + "\n", + "```\n", + "pip install d2l-notedown # You may need to uninstall the original notedown.\n", + "jupyter notebook --NotebookApp.contents_manager_class='notedown.NotedownContentsManager'\n", + "```\n", + "\n", + "You may also turn on the notedown plugin by default whenever you run the Jupyter Notebook.\n", + "First, generate a Jupyter Notebook configuration file (if it has already been generated, you can skip this step).\n", + "\n", + "```\n", + "jupyter notebook --generate-config\n", + "```\n", + "\n", + "Then, add the following line to the end of the Jupyter Notebook configuration file (for Linux or macOS, usually in the path `~/.jupyter/jupyter_notebook_config.py`):\n", + "\n", + "```\n", + "c.NotebookApp.contents_manager_class = 'notedown.NotedownContentsManager'\n", + "```\n", + "\n", + "After that, you only need to run the `jupyter notebook` command to turn on the notedown plugin by default.\n", + "\n", + "### Running Jupyter Notebooks on a Remote Server\n", + "\n", + "Sometimes, you may want to run Jupyter notebooks on a remote server and access it through a browser on your local computer. If Linux or macOS is installed on your local machine (Windows can also support this function through third-party software such as PuTTY), you can use port forwarding:\n", + "\n", + "```\n", + "ssh myserver -L 8888:localhost:8888\n", + "```\n", + "\n", + "The above string `myserver` is the address of the remote server.\n", + "Then we can use http://localhost:8888 to access the remote server `myserver` that runs Jupyter notebooks. We will detail on how to run Jupyter notebooks on AWS instances\n", + "later in this appendix.\n", + "\n", + "### Timing\n", + "\n", + "We can use the `ExecuteTime` plugin to time the execution of each code cell in Jupyter notebooks.\n", + "Use the following commands to install the plugin:\n", + "\n", + "```\n", + "pip install jupyter_contrib_nbextensions\n", + "jupyter contrib nbextension install --user\n", + "jupyter nbextension enable execute_time/ExecuteTime\n", + "```\n", + "\n", + "## Summary\n", + "\n", + "* Using the Jupyter Notebook tool, we can edit, run, and contribute to each section of the book.\n", + "* We can run Jupyter notebooks on remote servers using port forwarding.\n", + "\n", + "\n", + "## Exercises\n", + "\n", + "1. Edit and run the code in this book with the Jupyter Notebook on your local machine.\n", + "1. Edit and run the code in this book with the Jupyter Notebook *remotely* via port forwarding.\n", + "1. Compare the running time of the operations $\\mathbf{A}^\\top \\mathbf{B}$ and $\\mathbf{A} \\mathbf{B}$ for two square matrices in $\\mathbb{R}^{1024 \\times 1024}$. Which one is faster?\n", + "\n", + "\n", + "[Discussions](https://discuss.d2l.ai/t/421)\n" + ] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np\n", + "import pandas as pd\n" + ], + "metadata": { + "id": "VksGkt3e4_Om" + }, + "id": "VksGkt3e4_Om", + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "dataset = pd.read_csv('/content/Titanic-Dataset.csv')" + ], + "metadata": { + "id": "oZgkUOmX5-qs" + }, + "id": "oZgkUOmX5-qs", + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "dataset.head(3)" + ], + "metadata": { + "id": "V1mhF_Uj6YMv", + "outputId": "1257cf02-7f7b-4fb1-ef9c-b987dc6b33f8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 319 + } + }, + "id": "V1mhF_Uj6YMv", + "execution_count": 9, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S " + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "dataset", + "summary": "{\n \"name\": \"dataset\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "source": [ + "np.mean(dataset[\"Age\"])" + ], + "metadata": { + "id": "AsljcVoP6fPY", + "outputId": "5d8da5b3-7300-4399-c1b3-5fb00b37ddcc", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "id": "AsljcVoP6fPY", + "execution_count": 11, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "np.float64(29.69911764705882)" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + }, + "required_libs": [], + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file