From c2fd0506069997cf2c28114fa0a11f360b765688 Mon Sep 17 00:00:00 2001 From: Mirko Birbaumer <mirko.birbaumer@hslu.ch> Date: Sat, 17 Sep 2022 15:18:46 +0000 Subject: [PATCH] Added notebook on colab --- ...upyter Notebook - Introduction Colab.ipynb | 1372 +++++++++++++++++ 1 file changed, 1372 insertions(+) create mode 100644 notebooks/Preliminaries_Numpy_Pandas/Jupyter Notebook - Introduction Colab.ipynb diff --git a/notebooks/Preliminaries_Numpy_Pandas/Jupyter Notebook - Introduction Colab.ipynb b/notebooks/Preliminaries_Numpy_Pandas/Jupyter Notebook - Introduction Colab.ipynb new file mode 100644 index 0000000..f1aff13 --- /dev/null +++ b/notebooks/Preliminaries_Numpy_Pandas/Jupyter Notebook - Introduction Colab.ipynb @@ -0,0 +1,1372 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "UoQjqjBOIzfG" + }, + "source": [ + "# Colaboratory" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BcXbc54tbXq7" + }, + "source": [ + "## Introduction" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fPP3Zw5iV2DP" + }, + "source": [ + "\n", + "\n", + "Some important links to keep open during this course – open these tabs **now**!:\n", + "\n", + "- [TF documentation](https://www.tensorflow.org/api_docs/python/tf) : Use the search box (top right) to get documentation on Tensorflow's rich API.\n", + "\n", + "\n", + "---\n", + "\n", + "Colaboratory (Colab) is a Jupyter notebook environment which allows you to work with data and code in an interactive manner. You can decide where you want to run your code:\n", + "\n", + "* Using a hosted runtime provided by Google (**default**)\n", + "* Locally using your own machine and resources\n", + "\n", + "It supports Python 3 and comes with a set of pre-installed libraries like Tensorflow and Matplotlib but also gives you the option to install more libraries on demand. The resulting notebooks can be shared in a straightforward way.\n", + "\n", + "Caveats:\n", + "\n", + "* The virtual machines used for the runtimes are **ephemeral** so make sure to save your data in a persistent location like locally (downloading), in the Google Cloud Storage or Google Drive.\n", + "* The service is free of use but the performance of default runtimes can be insufficient for your purposes.\n", + "* You have the option to select a runtime with GPU or TPU support.\n", + "* \"Colaboratory is intended for interactive use. Long-running background computations, particularly on GPUs, may be stopped. [...] We encourage users who wish to run continuous or long-running computations through Colaboratory’s UI to use a local runtime.\" - See [Colaboratory FAQ](https://research.google.com/colaboratory/faq.html \"Colaboratory FAQ\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iDbiROodhaFQ" + }, + "source": [ + "**Getting started**\n", + "\n", + "1. Connect to a runtime now by clicking `connect` in the top right corner if you don't already see a green checkmark there.\n", + "2. To get a better overview you might want to activate the *Table of contents* by clicking on the arrow on the left." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "M54Z135xV2DW" + }, + "source": [ + "### Important shortcuts\n", + "\n", + "Action | Colab Shortcut | Jupyter Shortcut\n", + "---|---|---\n", + "Executes current cell | `<CTRL-ENTER>` | `<CTRL-ENTER>`\n", + "Executes current cell and moves to next cell | `<SHIFT-ENTER>` | `S<HIFT-ENTER>`\n", + "Executes current selection | `<CTRL-SHIFT-ENTER>` | `N/A`\n", + "Insert cell above | `<CTRL-M> <A>` | `<A>`\n", + "Append cell below | `<CTRL-M> <B>` | `<B>`\n", + "Shows searchable command palette | `<CTRL-SHIFT-P>` | `<CTRL-SHIFT-P>`\n", + "Convert cell to code | `<CTRL-M> <Y>` | `<Y>`\n", + "Convert cell to Markdown | `<CTRL-M> <M>` | `<M>`\n", + "Autocomplete (on by default) | `<CTRL+SPACE>` | `<TAB>`\n", + "Goes from edit to \"command\" mode | `<ESC>` | `<ESC>`\n", + "Goes from \"command\" to edit mode | `<ENTER>` | `<ENTER>`\n", + "Show keyboard shortcuts | `<CTRL-M> <H>` | `<H>`\n", + "<p align=\"center\"><b>Note:</b> On OS X you can use `<COMMAND>` instead of `<CTRL>`</p>\n", + "\n", + "Give it a try!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "iJ4YliVgZNCq", + "outputId": "a601b693-3213-4b75-e5db-2d0e9de22b64" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n" + ] + } + ], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Execute this cell first using <CTRL-ENTER> and then using <SHIFT-ENTER>.\n", + "# Note the difference in which cell is selected after execution.\n", + "print('Hello world!')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U-B6-3lmkMwJ" + }, + "source": [ + "You can also only execute one single statement in a cell." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "id": "IprMQ_z8kOsA", + "outputId": "855c699a-efa7-4d68-fbf4-e9a316d50fe2" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Only print this line.\n", + "Avoid printing this line.\n" + ] + } + ], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Execute only the first print statement by selecting the first line and pressing\n", + "# <CTRL-SHIFT-ENTER>.\n", + "print('Only print this line.')\n", + "print('Avoid printing this line.')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UPQzxvqbMXqr" + }, + "source": [ + "### Importing TensorFlow" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rScwW1Q69xJ-" + }, + "source": [ + "We'll be using **TensorFlow 2.3.0** in this course. This is the default, but for the time being we still need to activate it with the Colab-specific `%tensorflow_version` magic." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "Ys4hO5MtkhCC" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "UsageError: Line magic function `%tensorflow_version` not found.\n" + ] + } + ], + "source": [ + "%tensorflow_version " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "n_J1HPc4kpbl", + "outputId": "52b55281-1601-4e55-fc5e-7f2671b7cf14" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'2.1.0'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Include basic dependencies and display the tensorflow version.\n", + "import tensorflow as tf\n", + "tf.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1uKqdyHFB8NY" + }, + "source": [ + "### Running shell commands\n", + "\n", + "You can run shell commands directly in Colab: simply prepend the command with a **!**." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "id": "FY3pksK4V2DY", + "outputId": "962c2489-0095-4718-c090-fb5fccabfc55" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/work/hslu-deep-learning/notebooks/Block 3\n", + "'Exercises Block 3 - Training and Optimizing Neural Networks.ipynb'\n", + "'Jupyter Notebook Block 3 - Training and Optimizing Neural Networks.ipynb'\n", + "'Jupyter Notebook - Introduction Colab.ipynb'\n" + ] + } + ], + "source": [ + "# Print the current working directory and list all files in it.\n", + "!pwd\n", + "!ls" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 412 + }, + "id": "8BbKSuKslVaI", + "outputId": "1707154b-d8d1-4cef-8363-e6afc747e8ee" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting qrcode\n", + " Downloading qrcode-6.1-py2.py3-none-any.whl (31 kB)\n", + "Requirement already satisfied: six in /opt/conda/lib/python3.7/site-packages (from qrcode) (1.14.0)\n", + "Installing collected packages: qrcode\n", + "Successfully installed qrcode-6.1\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAASIAAAEiAQAAAAB1xeIbAAABj0lEQVR4nO2ZQU6FMBRFz7MkDkviAlwK3Zlbo0txASZ0aAK5Dlr06x/o5P9iaQdNgZNw83Jp3i0mfh/x4Q8QdKpTnepUp45OWRkDkAYg7XdCVV2noCZJ0gJmIwBOkqTv1P11nYJKxePSsuVV+Qwq6zoZ5a7bzkPoap6y4N/trm88L7VvK15AAgHoc1VN14moaGZmI1hIj7LAltuc2rqaprLvvzyuODr03fXHVd8CZSENeWLSigXAAlv3/R2o+LwC/t2IZqY5mUEasFBXV9MUOUJNC5AT1QKa/Qr4tYSu+ajq/zdVao9XCbKTVvZJ6rW/IfUZXbdBMYBIIwKnyy7/qOpboKTFZY9bAIgjQDLTXFVX21T2/e5xtyqObjX8Vh5Mcx1dZ6DKnpNLnJ4AvyDYEKyDauk6E7WfY9rL64AFXNl9autqmRp+XCuOGNGAOL7V03VGyoIvLSfkk51j6GqR2rNV/m/lpBl30eTnpr/39zeh9myVh9st779SVq/9rairc8zLyfX/tZ3qVKc61Qr1AeMHwb8M15tKAAAAAElFTkSuQmCC\n", + "text/plain": [ + "<qrcode.image.pil.PilImage at 0x7f4d48194410>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Especially useful: Installs new packages.\n", + "!pip install qrcode\n", + "import qrcode\n", + "qrcode.make('Colab rocks!')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ywxYXQ-lCc2_" + }, + "source": [ + "**Autocompletion and docstrings**\n", + "\n", + "Jupyter shows possible completions of partially typed\n", + "commands.\n", + "\n", + "Try it for yourself by displaying all available `tf.` methods that start with `one`." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "a_jmV9woV2Db", + "outputId": "ea99ad25-965c-4101-d4a7-e0a0cf8a19d0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<function tensorflow.python.ops.array_ops.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)>" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Set the cursor to after tf.one and press <CTRL-SPACE>.\n", + "tf.one_hot #tf.one" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PuBe6L8WOWuS" + }, + "source": [ + "In addition, you can also display docstrings to see the function signature and possible parameters." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "hPiM64eaOi2y", + "outputId": "97d92f6f-7e97-4e8c-b6f5-e6632a8ab71c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<tf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Complete the command to \"maximum\" and then add the opening bracket \"(\" to see\n", + "# the function documentation.\n", + "#tf.maximum()\n", + "tf.maximum([1, 2, 3], [2, 2, 2]) " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NK1yV3Ye0pbC" + }, + "source": [ + "Alternatively, you might also inspect function details with docstrings if available by appending a \"?\"." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "P-IF1l5u0MLT" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mSignature:\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmaximum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m\n", + "Returns the max of x and y (i.e. x > y ? x : y) element-wise.\n", + "\n", + "*NOTE*: `math.maximum` supports broadcasting. More about broadcasting\n", + "[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\n", + "\n", + "Args:\n", + " x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `int32`, `int64`.\n", + " y: A `Tensor`. Must have the same type as `x`.\n", + " name: A name for the operation (optional).\n", + "\n", + "Returns:\n", + " A `Tensor`. Has the same type as `x`.\n", + "\u001b[0;31mFile:\u001b[0m /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_math_ops.py\n", + "\u001b[0;31mType:\u001b[0m function\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "tf.maximum?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-wBzGaDMaCg2" + }, + "source": [ + "**Note:** This also works for any other type of object as can be seen below." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "nLXoW6IXZEvP" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mType:\u001b[0m dict\n", + "\u001b[0;31mString form:\u001b[0m {'key0': 'Tensor', 'key1': 'Flow'}\n", + "\u001b[0;31mLength:\u001b[0m 2\n", + "\u001b[0;31mDocstring:\u001b[0m \n", + "dict() -> new empty dictionary\n", + "dict(mapping) -> new dictionary initialized from a mapping object's\n", + " (key, value) pairs\n", + "dict(iterable) -> new dictionary initialized as if via:\n", + " d = {}\n", + " for k, v in iterable:\n", + " d[k] = v\n", + "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n", + " in the keyword argument list. For example: dict(one=1, two=2)\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "test_dict = {'key0': 'Tensor', 'key1': 'Flow'}\n", + "test_dict?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_mahgn1AHL61" + }, + "source": [ + "## Runtimes\n", + "\n", + "As noted in the introduction above, Colab provides multiple runtimes with different hardware accelerators:\n", + "\n", + "* CPU (default)\n", + "* GPU\n", + "* TPU\n", + "\n", + "which can be selected by choosing the `\"Runtime\"` tab above and then `\"Change runtime type\"`.\n", + "\n", + "Please be aware that selecting a new runtime will assign a new virtual machine (VM).\n", + "In general, assume that any changes you make to the VM environment including data storage are **ephemeral**. Particularly, this might require to **execute previous cells again** as their content is unknown to a new runtime otherwise. \n", + "\n", + "Let's take a closer look at one of such provided VMs.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "whv1zmERgI5v" + }, + "source": [ + "Once we have been assigned a runtime we can inspect it further." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "AwLSimGKgHFd", + "outputId": "2617a190-93d9-4102-e1a5-ac5078a980a9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 14:31:31 up 4 days, 5:23, 0 users, load average: 0.38, 0.97, 0.88\n" + ] + } + ], + "source": [ + "# Display how long the system has been running.\n", + "!uptime" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f69GlgeggRED" + }, + "source": [ + "As can be seen, the machine has been allocated just very recently for our purposes.\n", + "\n", + "**VM specifications**" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 547 + }, + "id": "AAXhttijkq8P", + "outputId": "b0b9b8c4-1a7a-4bb4-f111-d81fc233210c" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " total used free shared buff/cache available\n", + "Mem: 47G 5.7G 10G 2.2M 31G 41G\n", + "Swap: 0B 0B 0B\n", + "----------------------------------------------------------------------\n", + "Architecture: x86_64\n", + "CPU op-mode(s): 32-bit, 64-bit\n", + "Byte Order: Little Endian\n", + "CPU(s): 16\n", + "On-line CPU(s) list: 0-15\n", + "Thread(s) per core: 1\n", + "Core(s) per socket: 1\n", + "Socket(s): 16\n", + "NUMA node(s): 1\n", + "Vendor ID: GenuineIntel\n", + "CPU family: 6\n", + "Model: 61\n", + "Model name: Intel Core Processor (Broadwell, IBRS)\n", + "Stepping: 2\n", + "CPU MHz: 2194.916\n", + "BogoMIPS: 4389.83\n", + "Virtualization: VT-x\n", + "Hypervisor vendor: KVM\n", + "Virtualization type: full\n", + "L1d cache: 32K\n", + "L1i cache: 32K\n", + "L2 cache: 4096K\n", + "NUMA node0 CPU(s): 0-15\n", + "Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti ssbd ibrs ibpb tpr_shadow vnmi flexpriority ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms invpcid rtm rdseed adx smap xsaveopt arat md_clear\n", + "----------------------------------------------------------------------\n", + "/bin/sh: 1: nvidia-smi: not found\n", + "/bin/sh: 1: nvidia-smi: not found\n" + ] + } + ], + "source": [ + "# Display available and used memory.\n", + "!free -h\n", + "print(\"-\"*70)\n", + "# Display the CPU specification.\n", + "!lscpu\n", + "print(\"-\"*70)\n", + "# Display the GPU specification (if available).\n", + "!(nvidia-smi | grep -q \"has failed\") && echo \"No GPU found!\" || nvidia-smi " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bqCA9-VDIjsu" + }, + "source": [ + "## Plotting\n", + "\n", + "The notebook environment also provides options to visualize and interact with data.\n", + "\n", + "We'll take a short look at the plotting/visualization libraries Matplotlib and Altair." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1iJfoQ86AsfO" + }, + "source": [ + "### Matplotlib\n", + "\n", + "Matplotlib is one of the most famous Python plotting libraries and can be used to plot results within a cell's output (see [Matplotlib Introduction](https://matplotlib.org/users/intro.html \"Matplotlib Introduction\")).\n", + "\n", + "Let's try to plot something with it." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 286 + }, + "id": "gyk4SEk_V2DW", + "outputId": "7120223a-a25d-416c-defc-d7e5cb30c334" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<matplotlib.collections.PathCollection at 0x7f4bec110710>" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD4CAYAAADlwTGnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAce0lEQVR4nO3df5BdZXkH8O83myVs1LpoVoGFkNgiCNIxegd10rH80mBaSQSt4DgFR5sZW9opdTIuw4ygHcdYpnVGZaQpMqJViYrGdYiTqolDhxqHmyYIQaIRBXKhZYUkHc0qm83TP+698ebm3Ht+n/Oe834/M5ncHyf3nLPZ+z7nfd7nfQ/NDCIi4p8FZR+AiIiUQwFARMRTCgAiIp5SABAR8ZQCgIiIpxaWfQDDLFmyxJYtW1b2YYiIVMbOnTt/ZWYTUbZ1OgAsW7YMzWaz7MMQEakMko9H3VYpIBERT2USAEjeSfIZkg8PeP8ikodI7u78+XAW+xURkeSySgF9HsBnAHxhyDb/aWZ/ntH+REQkpUx6AGZ2H4DnsvgsEREpRpFjAG8k+SDJ75A8f9BGJNeRbJJszszMFHh4IiJ+KaoK6L8BnGVmvya5GsBmAGcHbWhmGwFsBIBGo6GV6hLavKuFW7fuxVMHZ3H6+BjWrzoHa1dMln1YIuKQQnoAZvZ/ZvbrzuMtAEZJLili3z7avKuFG7/xEFoHZ2EAWgdnceM3HsLmXa2yD01EHFJIACB5Kkl2Hl/Y2e+zRezbR7du3YvZufnjXpudm8etW/eWdEQi4qJMUkAkvwLgIgBLSO4HcDOAUQAws9sBvAPAB0geATAL4GrTjQhy89TB2Vivi4ifMgkAZnZNyPufQbtMVApw+vgYWgGN/enjYyUcjYi4Sj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QnuQd7pvm2kA13YevwPANuuMktRA6PmTXIH2fZqvqFkOGAg5fzM7ZGZLzGyZmS1DewzkCjNrlnO4mYryu78Z7at/kFyCdkrosQKPMU9Rzv8JAJcCAMlXoR0AfLm14DSAv+xUA70BwCEze3rYP6hcCsjMjpC8HsBWtKsC7jSzPSQ/CqBpZtMAPod2128f2oMmV5d3xNmKeP63AnghgK91xr6fMLMrSjvoDEU8/1qKeO5bAbyF5CMA5gGsN7Na9H4jnv8HAfwbyRvQHhC+ri4XfyS/gnZwX9IZ47gZwCgAmNntaI95rAawD8BhAO8N/cya/GxERCSmKqaAREQkAwoAIiKeUgAQEfGUAoCIiKcUAEREPKUAICLiKQUAERFP/T+wEss+Mdta3AAAAABJRU5ErkJggg==\n", + "text/plain": [ + "<Figure size 432x288 with 1 Axes>" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "# Display the Matplotlib outputs within a cell's output. \n", + "%matplotlib inline\n", + "import numpy as np\n", + "from matplotlib import pyplot\n", + "\n", + "# Create a randomized scatterplot using matplotlib.\n", + "x = np.random.rand(100).astype(np.float32)\n", + "noise = np.random.normal(scale=0.3, size=len(x))\n", + "y = np.sin(x * 7) + noise\n", + "pyplot.scatter(x, y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lhUudwcUafIm" + }, + "source": [ + "### Altair\n", + "\n", + "Another declarative visualization library for Python is Altair (see [Altair: Declarative Visualization in Python](https://altair-viz.github.io/)).\n", + "\n", + "Try to zoom in/out and to hover over individual data points in the resulting plot below." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 368 + }, + "id": "uO3MmbhbIw_t", + "outputId": "ed43ddf2-7d39-4310-ce85-65bd3bc87486" + }, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'vega_datasets'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-20-3e82446dd3d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Load an example dataset.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mvega_datasets\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mcars\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcars\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# Plot the dataset, referencing dataframe column names.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'vega_datasets'" + ] + } + ], + "source": [ + "# Load an example dataset.\n", + "from vega_datasets import data\n", + "cars = data.cars()\n", + "\n", + "# Plot the dataset, referencing dataframe column names.\n", + "import altair as alt\n", + "alt.Chart(cars).mark_point().encode(\n", + " x='Horsepower',\n", + " y='Miles_per_Gallon',\n", + " color='Origin',\n", + " tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']\n", + " ).interactive()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cBUraBMAHE0J" + }, + "source": [ + "## Notebook Magics\n", + "\n", + "The IPython and Colab environment support built-in magic commands called magics (see: [IPython - Magics](https://ipython.readthedocs.io/en/stable/interactive/magics.html)).\n", + "\n", + "In addition to default Python, these commands might be handy for example when it comes to interacting directly with the VM or the Notebook itself.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KO1K1UnIfU0u" + }, + "source": [ + "### Cell magics\n", + "\n", + "Cell magics define a mode for a complete cell and are prefixed with **%%**.\n", + "\n", + "Examples include:\n", + "\n", + "* **%%bash** or **%%sh**\n", + "* **%%html**\n", + "* **%%javascript**\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 207 + }, + "id": "2qT4YGbOQ9FG", + "outputId": "09778496-9cd0-4a59-b5e4-9ba9205a8332" + }, + "outputs": [], + "source": [ + "%%sh\n", + "echo \"This is a shell script!\"\n", + "# List all running VM processes.\n", + "ps -ef\n", + "echo \"Done\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 38 + }, + "id": "qUSc9sT2JskX", + "outputId": "743ca85b-35ed-4df4-ba58-c4fc0c8cc06c" + }, + "outputs": [], + "source": [ + "# Embed custom HTML directly into a cell's output.\n", + "%%html\n", + "<marquee>HTML rocks</marquee>" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Uy0j4iJ5d8v3" + }, + "source": [ + "### Line magics \n", + "\n", + "You can also make use of line magics which can be inserted anywhere at the beginning of a line inside a cell and need to be prefixed with **%**.\n", + "\n", + "Examples include:\n", + "\n", + "\n", + "* **%time** - display the required time to execute the current line\n", + "* **%cd** - change the current working directory\n", + "* **%pdb** - invoke an interactive Python debugger\n", + "* **%lsmagic** - list all available line magic and cell magic functions\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mrdhMGsyN2Gl" + }, + "source": [ + "For example, if you want to find out how long one specific line requires to be executed you can just prepend **%time**.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + }, + "id": "Kz0d1QoINx95", + "outputId": "e356539c-ac9f-40d1-de36-03fcf8004b9d" + }, + "outputs": [], + "source": [ + "n = 1000000\n", + "%time list1 = [i for i in range(n)]\n", + "print(\"\")\n", + "%time list2 = [i for i in range(int(n/2))]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y_Q3lleVPMVJ" + }, + "source": [ + "**Note:** Some line magics like **%time** can also be used for complete cells by writing **%%time**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "id": "LCIrCKs9a1eu", + "outputId": "3c85e834-d2fd-4f83-9741-eb89878cd255" + }, + "outputs": [], + "source": [ + "%%time\n", + "n = 1000000\n", + "list1 = [i for i in range(n)]\n", + "list2 = [i for i in range(int(n/2))]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BGurTn7rVH_I" + }, + "source": [ + "## Data handling\n", + "\n", + "There are multiple ways to provide data to a Colabs's VM environment.\n", + "> **Note:** This section only applies to Colab.\n", + "> Jupyter has a file explorer and other options for data handling.\n", + "\n", + "The options include:\n", + "* Uploading files from the local file system.\n", + "* Connecting to Google Cloud Storage (explained below).\n", + "* Connecting to Google Drive (see: [Snippets: Drive](https://colab.sandbox.google.com/notebooks/snippets/drive.ipynb); will be used in the next Colabs)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Zdb5RpNmxoJl" + }, + "source": [ + "**Uploading files from the local file system**" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 157, + "resources": { + "http://localhost:8080/nbextensions/google.colab/files.js": { + "data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8ge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0byBiZSByZWFkeS4KICAgIGxldCBmaWxlRGF0YSA9IHlpZWxkIHsKICAgICAgcHJvbWlzZTogZmlsZURhdGFQcm9taXNlLAogICAgICByZXNwb25zZTogewogICAgICAgIGFjdGlvbjogJ2NvbnRpbnVlJywKICAgICAgfQogICAgfTsKCiAgICAvLyBVc2UgYSBjaHVua2VkIHNlbmRpbmcgdG8gYXZvaWQgbWVzc2FnZSBzaXplIGxpbWl0cy4gU2VlIGIvNjIxMTU2NjAuCiAgICBsZXQgcG9zaXRpb24gPSAwOwogICAgd2hpbGUgKHBvc2l0aW9uIDwgZmlsZURhdGEuYnl0ZUxlbmd0aCkgewogICAgICBjb25zdCBsZW5ndGggPSBNYXRoLm1pbihmaWxlRGF0YS5ieXRlTGVuZ3RoIC0gcG9zaXRpb24sIE1BWF9QQVlMT0FEX1NJWkUpOwogICAgICBjb25zdCBjaHVuayA9IG5ldyBVaW50OEFycmF5KGZpbGVEYXRhLCBwb3NpdGlvbiwgbGVuZ3RoKTsKICAgICAgcG9zaXRpb24gKz0gbGVuZ3RoOwoKICAgICAgY29uc3QgYmFzZTY0ID0gYnRvYShTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KG51bGwsIGNodW5rKSk7CiAgICAgIHlpZWxkIHsKICAgICAgICByZXNwb25zZTogewogICAgICAgICAgYWN0aW9uOiAnYXBwZW5kJywKICAgICAgICAgIGZpbGU6IGZpbGUubmFtZSwKICAgICAgICAgIGRhdGE6IGJhc2U2NCwKICAgICAgICB9LAogICAgICB9OwogICAgICBwZXJjZW50LnRleHRDb250ZW50ID0KICAgICAgICAgIGAke01hdGgucm91bmQoKHBvc2l0aW9uIC8gZmlsZURhdGEuYnl0ZUxlbmd0aCkgKiAxMDApfSUgZG9uZWA7CiAgICB9CiAgfQoKICAvLyBBbGwgZG9uZS4KICB5aWVsZCB7CiAgICByZXNwb25zZTogewogICAgICBhY3Rpb246ICdjb21wbGV0ZScsCiAgICB9CiAgfTsKfQoKc2NvcGUuZ29vZ2xlID0gc2NvcGUuZ29vZ2xlIHx8IHt9OwpzY29wZS5nb29nbGUuY29sYWIgPSBzY29wZS5nb29nbGUuY29sYWIgfHwge307CnNjb3BlLmdvb2dsZS5jb2xhYi5fZmlsZXMgPSB7CiAgX3VwbG9hZEZpbGVzLAogIF91cGxvYWRGaWxlc0NvbnRpbnVlLAp9Owp9KShzZWxmKTsK", + "headers": [ + [ + "content-type", + "application/javascript" + ] + ], + "ok": true, + "status": 200, + "status_text": "" + } + } + }, + "id": "qvxTx9m9xZJ2", + "outputId": "5d709c16-ea27-4214-c75a-fe3c32d85de6" + }, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'google.colab'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-21-9dc6fa463cb8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR ACTION REQUIRED:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Upload any test file to the target system.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0muploaded\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mfn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0muploaded\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'google.colab'" + ] + } + ], + "source": [ + "# Snippet as provided in https://colab.research.google.com/notebooks/io.ipynb\n", + "# YOUR ACTION REQUIRED:\n", + "# Upload any test file to the target system.\n", + "from google.colab import files\n", + "uploaded = files.upload()\n", + "for fn in uploaded.keys():\n", + " print('User uploaded file \"{name}\" with length {length} bytes'.format(\n", + " name=fn, length=len(uploaded[fn])))\n", + "# The file should now appear in the current working directory.\n", + "!pwd\n", + "!ls -l" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 17 + }, + "id": "toQxJSVfxjew", + "outputId": "c97f24cd-a8fb-4dd9-85f6-507445f519c1" + }, + "outputs": [], + "source": [ + "# A file can also be downloaded by using:\n", + "from google.colab import files\n", + "!touch test_file.txt\n", + "files.download('test_file.txt')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "swVo-jxCW6KK" + }, + "source": [ + "**Connecting to Google Cloud Storage**\n", + "\n", + "[Google Cloud Storage](https://cloud.google.com/storage/?hl=de) (GCS) is a cloud file storage service with a RESTful API.\n", + "\n", + "We can utilize it to store our own data or to access data provided by the following identifier:\n", + "\n", + "\n", + "```\n", + "gs://[BUCKET_NAME]/[OBJECT_NAME]\n", + "```\n", + "\n", + "We'll use the data provided in **gs://amld-datasets/zoo_img** as can be seen below.\n", + "\n", + "Before we can interact with the cloud environment, we need to grant permissions accordingly (also see [External data: Cloud Storage](https://colab.research.google.com/notebooks/io.ipynb#scrollTo=S7c8WYyQdh5i))." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "1aJ1e7JFVIsQ" + }, + "outputs": [], + "source": [ + "from google.colab import auth\n", + "auth.authenticate_user()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f6_4yqiOfWKt" + }, + "source": [ + "List a subset of the contained files using the [gsutil tool](https://cloud.google.com/storage/docs/gsutil?hl=en)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "id": "wJAutwkEWWxg", + "outputId": "d20afb18-b0b1-4dce-f6c4-bfcdf7363ab9" + }, + "outputs": [], + "source": [ + "!gsutil ls gs://amld-datasets/zoo_img | head" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d10y2KZpfsed" + }, + "source": [ + "Conveniently, TensorFlow natively supports multiple file systems such as:\n", + "\n", + "* GCS - Google Cloud Storage\n", + "* HDFS - Hadoop\n", + "* S3 - Amazon Simple Storage\n", + "\n", + "An example for the GCS filesystem can be seen below." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 187 + }, + "id": "MDUnLXGXWnO8", + "outputId": "dc694698-93d4-4132-ee3f-638eb1db5159" + }, + "outputs": [], + "source": [ + "# Note: This cell hangs if you forget to call auth.authenticate_user() above.\n", + "tf.io.gfile.glob('gs://amld-datasets/zoo_img/*')[:10]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AHhyyTLRHDlH" + }, + "source": [ + "## Snippets\n", + "\n", + "Finally, we can take a look at the snippets support in Colab.\n", + "> If you're using Jupyter please see [Jupyter contrib nbextensions - Snippets menu](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/snippets_menu/readme.html) as this is not natively supported.\n", + "\n", + "Snippets are a way to quickly \"bookmark\" pieces of code or text that you might want to insert into specific cells.\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "4Dph99hAr61H" + }, + "outputs": [], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Explore existing snippets by going to the `Code snippets` section.\n", + "# Click on the <> button on the left sidebar to open the snippets.\n", + "# Alternatively, you can press `<CTRL><ALT><P>` (or `<COMMAND><OPTION><P>` for OS X)." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "_rO9y-43U_lL" + }, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'google.colab'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-22-f69ea5624b36>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msnippets\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m# snippets.register('https://colab.research.google.com/drive/1OFSjEmqC-UC66xs-LR7-xmgkvxYTrAcN')\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'google.colab'" + ] + } + ], + "source": [ + "from google.colab import snippets\n", + "# snippets.register('https://colab.research.google.com/drive/1OFSjEmqC-UC66xs-LR7-xmgkvxYTrAcN')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "57T5LlVEpfp2" + }, + "source": [ + "**Pro tip** : Maybe this is a good moment to create your own snippets and register them in settings. You can then start collecting often-used code and have it ready when you need it..." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "grr29b3-gCd-" + }, + "source": [ + "# ----- Optional part -----" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W0Uq0G0LrtjG" + }, + "source": [ + "## Custom line magic\n", + "\n", + "You can also define your own line/cell magic in the following way." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Y8bjXSjBJ2oP" + }, + "outputs": [], + "source": [ + "from IPython.core.magic import register_line_cell_magic\n", + "\n", + "@register_line_cell_magic\n", + "def mymagic(line_content, cell_content=None):\n", + " print('line_content=\"%s\" cell_content=\"%s\"' % (line_content, cell_content))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "lu7Z7VTEKPWh", + "outputId": "1b3a751e-384d-4e67-f8b8-30ced872e0b0" + }, + "outputs": [], + "source": [ + "%mymagic Howdy Alice!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "id": "GGBRfLm6LTvU", + "outputId": "2d413b95-ce2f-4af4-c226-ac4792460afa" + }, + "outputs": [], + "source": [ + "%%mymagic simple question\n", + "Howdy Alice!\n", + "how are you?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hZUSUu5A1mAD" + }, + "source": [ + "## Forms\n", + "\n", + "You can simplify cells by hiding their code and displaying a form instead.\n", + "\n", + "\n", + "**Note:** You can display or hide the code by double clicking the form which might be on the right side." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "2EkpzaQA1u7y", + "outputId": "61d75dff-eb8e-40e2-dd11-28fb9cd9bca9" + }, + "outputs": [], + "source": [ + "#@title Execute me\n", + "\n", + "# Hidden cell content.\n", + "print(\"Double click the cell to see its content.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "both", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "id": "adKaHZNZ2jZo", + "outputId": "4eb20e86-c4a3-4521-96f5-e487717d6748" + }, + "outputs": [], + "source": [ + "# Form example mostly taken from \"Adding form fields\" Snippet.\n", + "#@title Example form\n", + "#@markdown Specify some test data and execute this cell.\n", + "\n", + "string_type = 'test_string' #@param {type: \"string\"}\n", + "slider_value = 145 #@param {type: \"slider\", min: 100, max: 200}\n", + "number = 1339 #@param {type: \"number\"}\n", + "date = '2019-01-26' #@param {type: \"date\"}\n", + "pick_me = \"a\" #@param ['a', 'b', 'c']\n", + "#@markdown ---\n", + "print(\"Submitted data:\")\n", + "print(string_type, slider_value, number, date, pick_me)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kx_ETyD5r0vJ" + }, + "source": [ + "## Interactive debugging" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8rXJALYPr-Eg" + }, + "source": [ + "An example of an IPython tool that you can utilize is the interactive debugger \n", + "provided inside an IPython environment like Colab.\n", + "\n", + "For instance, by using **%pdb on**, you can automatically trigger the debugger on exceptions to further analyze the state.\n", + "\n", + "Some useful debugger commands are:\n", + "\n", + "Description | Command\n", + "---|---\n", + "**h**(elp) | Display available commands\n", + "**p**(rint) `x` | Show content of object `x`\n", + "**w**(here) | Show current instruction pointer position\n", + "**q**(uit) | Leave the debugger" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "id": "sMGORnaE2yJ-", + "outputId": "65d027eb-2094-4a20-9f8d-1e8cb759add2" + }, + "outputs": [], + "source": [ + "# YOUR ACTION REQUIRED:\n", + "# Execute this cell, print the variable contents of a, b and exit the debugger.\n", + "%pdb on\n", + "a = 67069 / 47 - 0x5a\n", + "b = a - 0x539\n", + "#c = a / b # Will throw an exception." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eHQDeKx06WPq" + }, + "source": [ + "We'll not dive further into debugging but it's useful to know that this option exists.\n", + "\n", + "Please see [Python Docs - pdb The Python Debugger](https://docs.python.org/2/library/pdb.html) for more information." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kPD8cAscKCes" + }, + "source": [ + "## A Word of Warning\n", + "\n", + "While notebook environments like Colab/Jupyter provide many benefits, they also come with some caveats that you should be aware of.\n", + "One example is that you might quickly execute cells in a wrong order leading to unexpected behavior.\n", + "\n", + "If you're interested in more examples feel free to take a look at:\n", + "\n", + "[Youtube - I don't like notebooks by Joel Grus](https://www.youtube.com/watch?v=7jiPeIFXb6U) (duration ~56 minutes)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "svJa3PRtN4l3" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [], + "name": "Jupyter Notebook - Introduction Colab.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab