Created
February 12, 2022 13:31
-
-
Save op1490/1f67e8a0384dc1d49c20c553d725926c to your computer and use it in GitHub Desktop.
DeepLearningwithPython.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "DeepLearningwithPython.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyMgiErS4JE4skMKp2ZVXVwm", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"widgets": { | |
"application/vnd.jupyter.widget-state+json": { | |
"2ed5ba99b26d43fdb102c6b41ba10159": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "HBoxModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "HBoxView", | |
"_dom_classes": [], | |
"_model_name": "HBoxModel", | |
"_view_module": "@jupyter-widgets/controls", | |
"_model_module_version": "1.5.0", | |
"_view_count": null, | |
"_view_module_version": "1.5.0", | |
"box_style": "", | |
"layout": "IPY_MODEL_a714b6a0e2b44695a0a4e5a40d2d3069", | |
"_model_module": "@jupyter-widgets/controls", | |
"children": [ | |
"IPY_MODEL_b50e5088fde2442c9304bf6ae5ea8c15", | |
"IPY_MODEL_97ce12548f11408c9711dd73ff543ca4", | |
"IPY_MODEL_f81453dc01d4443bb22777bfd5a30145" | |
] | |
} | |
}, | |
"a714b6a0e2b44695a0a4e5a40d2d3069": { | |
"model_module": "@jupyter-widgets/base", | |
"model_name": "LayoutModel", | |
"model_module_version": "1.2.0", | |
"state": { | |
"_view_name": "LayoutView", | |
"grid_template_rows": null, | |
"right": null, | |
"justify_content": null, | |
"_view_module": "@jupyter-widgets/base", | |
"overflow": null, | |
"_model_module_version": "1.2.0", | |
"_view_count": null, | |
"flex_flow": null, | |
"width": null, | |
"min_width": null, | |
"border": null, | |
"align_items": null, | |
"bottom": null, | |
"_model_module": "@jupyter-widgets/base", | |
"top": null, | |
"grid_column": null, | |
"overflow_y": null, | |
"overflow_x": null, | |
"grid_auto_flow": null, | |
"grid_area": null, | |
"grid_template_columns": null, | |
"flex": null, | |
"_model_name": "LayoutModel", | |
"justify_items": null, | |
"grid_row": null, | |
"max_height": null, | |
"align_content": null, | |
"visibility": null, | |
"align_self": null, | |
"height": null, | |
"min_height": null, | |
"padding": null, | |
"grid_auto_rows": null, | |
"grid_gap": null, | |
"max_width": null, | |
"order": null, | |
"_view_module_version": "1.2.0", | |
"grid_template_areas": null, | |
"object_position": null, | |
"object_fit": null, | |
"grid_auto_columns": null, | |
"margin": null, | |
"display": null, | |
"left": null | |
} | |
}, | |
"b50e5088fde2442c9304bf6ae5ea8c15": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "HTMLModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "HTMLView", | |
"style": "IPY_MODEL_f504361dd913458e9f8ee8f121e12658", | |
"_dom_classes": [], | |
"description": "", | |
"_model_name": "HTMLModel", | |
"placeholder": "", | |
"_view_module": "@jupyter-widgets/controls", | |
"_model_module_version": "1.5.0", | |
"value": "Dl Completed...: 100%", | |
"_view_count": null, | |
"_view_module_version": "1.5.0", | |
"description_tooltip": null, | |
"_model_module": "@jupyter-widgets/controls", | |
"layout": "IPY_MODEL_cfcb533912b04fbeafa2e21c6c3c3e3c" | |
} | |
}, | |
"97ce12548f11408c9711dd73ff543ca4": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "FloatProgressModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "ProgressView", | |
"style": "IPY_MODEL_58b5ce6cdabd47258c50ea7e30f810bc", | |
"_dom_classes": [], | |
"description": "", | |
"_model_name": "FloatProgressModel", | |
"bar_style": "success", | |
"max": 4, | |
"_view_module": "@jupyter-widgets/controls", | |
"_model_module_version": "1.5.0", | |
"value": 4, | |
"_view_count": null, | |
"_view_module_version": "1.5.0", | |
"orientation": "horizontal", | |
"min": 0, | |
"description_tooltip": null, | |
"_model_module": "@jupyter-widgets/controls", | |
"layout": "IPY_MODEL_36f9d981c0ef41a2918f36f349171aa0" | |
} | |
}, | |
"f81453dc01d4443bb22777bfd5a30145": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "HTMLModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "HTMLView", | |
"style": "IPY_MODEL_d240397e001b472bb0cde3e94f7b0683", | |
"_dom_classes": [], | |
"description": "", | |
"_model_name": "HTMLModel", | |
"placeholder": "", | |
"_view_module": "@jupyter-widgets/controls", | |
"_model_module_version": "1.5.0", | |
"value": " 4/4 [00:00<00:00, 8.02 file/s]", | |
"_view_count": null, | |
"_view_module_version": "1.5.0", | |
"description_tooltip": null, | |
"_model_module": "@jupyter-widgets/controls", | |
"layout": "IPY_MODEL_6a2439b5a3054ced8c567790dc6d6ac5" | |
} | |
}, | |
"f504361dd913458e9f8ee8f121e12658": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "DescriptionStyleModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "StyleView", | |
"_model_name": "DescriptionStyleModel", | |
"description_width": "", | |
"_view_module": "@jupyter-widgets/base", | |
"_model_module_version": "1.5.0", | |
"_view_count": null, | |
"_view_module_version": "1.2.0", | |
"_model_module": "@jupyter-widgets/controls" | |
} | |
}, | |
"cfcb533912b04fbeafa2e21c6c3c3e3c": { | |
"model_module": "@jupyter-widgets/base", | |
"model_name": "LayoutModel", | |
"model_module_version": "1.2.0", | |
"state": { | |
"_view_name": "LayoutView", | |
"grid_template_rows": null, | |
"right": null, | |
"justify_content": null, | |
"_view_module": "@jupyter-widgets/base", | |
"overflow": null, | |
"_model_module_version": "1.2.0", | |
"_view_count": null, | |
"flex_flow": null, | |
"width": null, | |
"min_width": null, | |
"border": null, | |
"align_items": null, | |
"bottom": null, | |
"_model_module": "@jupyter-widgets/base", | |
"top": null, | |
"grid_column": null, | |
"overflow_y": null, | |
"overflow_x": null, | |
"grid_auto_flow": null, | |
"grid_area": null, | |
"grid_template_columns": null, | |
"flex": null, | |
"_model_name": "LayoutModel", | |
"justify_items": null, | |
"grid_row": null, | |
"max_height": null, | |
"align_content": null, | |
"visibility": null, | |
"align_self": null, | |
"height": null, | |
"min_height": null, | |
"padding": null, | |
"grid_auto_rows": null, | |
"grid_gap": null, | |
"max_width": null, | |
"order": null, | |
"_view_module_version": "1.2.0", | |
"grid_template_areas": null, | |
"object_position": null, | |
"object_fit": null, | |
"grid_auto_columns": null, | |
"margin": null, | |
"display": null, | |
"left": null | |
} | |
}, | |
"58b5ce6cdabd47258c50ea7e30f810bc": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "ProgressStyleModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "StyleView", | |
"_model_name": "ProgressStyleModel", | |
"description_width": "", | |
"_view_module": "@jupyter-widgets/base", | |
"_model_module_version": "1.5.0", | |
"_view_count": null, | |
"_view_module_version": "1.2.0", | |
"bar_color": null, | |
"_model_module": "@jupyter-widgets/controls" | |
} | |
}, | |
"36f9d981c0ef41a2918f36f349171aa0": { | |
"model_module": "@jupyter-widgets/base", | |
"model_name": "LayoutModel", | |
"model_module_version": "1.2.0", | |
"state": { | |
"_view_name": "LayoutView", | |
"grid_template_rows": null, | |
"right": null, | |
"justify_content": null, | |
"_view_module": "@jupyter-widgets/base", | |
"overflow": null, | |
"_model_module_version": "1.2.0", | |
"_view_count": null, | |
"flex_flow": null, | |
"width": null, | |
"min_width": null, | |
"border": null, | |
"align_items": null, | |
"bottom": null, | |
"_model_module": "@jupyter-widgets/base", | |
"top": null, | |
"grid_column": null, | |
"overflow_y": null, | |
"overflow_x": null, | |
"grid_auto_flow": null, | |
"grid_area": null, | |
"grid_template_columns": null, | |
"flex": null, | |
"_model_name": "LayoutModel", | |
"justify_items": null, | |
"grid_row": null, | |
"max_height": null, | |
"align_content": null, | |
"visibility": null, | |
"align_self": null, | |
"height": null, | |
"min_height": null, | |
"padding": null, | |
"grid_auto_rows": null, | |
"grid_gap": null, | |
"max_width": null, | |
"order": null, | |
"_view_module_version": "1.2.0", | |
"grid_template_areas": null, | |
"object_position": null, | |
"object_fit": null, | |
"grid_auto_columns": null, | |
"margin": null, | |
"display": null, | |
"left": null | |
} | |
}, | |
"d240397e001b472bb0cde3e94f7b0683": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_name": "DescriptionStyleModel", | |
"model_module_version": "1.5.0", | |
"state": { | |
"_view_name": "StyleView", | |
"_model_name": "DescriptionStyleModel", | |
"description_width": "", | |
"_view_module": "@jupyter-widgets/base", | |
"_model_module_version": "1.5.0", | |
"_view_count": null, | |
"_view_module_version": "1.2.0", | |
"_model_module": "@jupyter-widgets/controls" | |
} | |
}, | |
"6a2439b5a3054ced8c567790dc6d6ac5": { | |
"model_module": "@jupyter-widgets/base", | |
"model_name": "LayoutModel", | |
"model_module_version": "1.2.0", | |
"state": { | |
"_view_name": "LayoutView", | |
"grid_template_rows": null, | |
"right": null, | |
"justify_content": null, | |
"_view_module": "@jupyter-widgets/base", | |
"overflow": null, | |
"_model_module_version": "1.2.0", | |
"_view_count": null, | |
"flex_flow": null, | |
"width": null, | |
"min_width": null, | |
"border": null, | |
"align_items": null, | |
"bottom": null, | |
"_model_module": "@jupyter-widgets/base", | |
"top": null, | |
"grid_column": null, | |
"overflow_y": null, | |
"overflow_x": null, | |
"grid_auto_flow": null, | |
"grid_area": null, | |
"grid_template_columns": null, | |
"flex": null, | |
"_model_name": "LayoutModel", | |
"justify_items": null, | |
"grid_row": null, | |
"max_height": null, | |
"align_content": null, | |
"visibility": null, | |
"align_self": null, | |
"height": null, | |
"min_height": null, | |
"padding": null, | |
"grid_auto_rows": null, | |
"grid_gap": null, | |
"max_width": null, | |
"order": null, | |
"_view_module_version": "1.2.0", | |
"grid_template_areas": null, | |
"object_position": null, | |
"object_fit": null, | |
"grid_auto_columns": null, | |
"margin": null, | |
"display": null, | |
"left": null | |
} | |
} | |
} | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/op1490/1f67e8a0384dc1d49c20c553d725926c/deeplearningwithpython.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Fundamentals \n", | |
"\n", | |
"- Deep learning is in its early days and has only realised a fraction of its potential\n", | |
"- Machine learning systems are trained rather than explictly programmed. ML is fundamentally an engineering discipline, driven by empirical findings and software/hardware advances.\n", | |
"- It requires 3 things\n", | |
" - Input data points\n", | |
" - Expected outputs\n", | |
" - A performance measurement used as a feedback signal to adjust (learning)\n", | |
"- A model transforms input data into meaningful outputs - i.e., it learns useful representations of the input data that make it more amenable to the task at hand\n", | |
"- A representation is a different way to look at data (to represent or encode it)\n", | |
"- Learning in ML describes an automatic search process for data transformations that provide useful representations of some data, guided by some feedback signal\n", | |
" - For example, coordinate changes, linear projects and translations, non linear operations\n", | |
" - ML algorithms find these transformations by searching through a predefined set of operations - the **hypothesis space**\n", | |
"- Concisely defined: ML is searching for useful representations and rules over some input data, within a predefined hypothesis space, using guidance from a feedback signal\n", | |
"- Deep Learning refers to the idea of learning successive layers of representations (the depth of the model, which often involves tens or even hundreds of successive representation layers)\n", | |
"- A network transforms its input into representations that are increasingly different from the original input and increasingly informative about the output - you can think of it as a multistage infomation-distillation process\n", | |
"- The specification of the transformation a layer does to its input data is stored in the layer's weights (parameters) (the transformation implemented by a layer is *parameterized* by its weights)\n", | |
" - In this context learning means finding an optimum set of values for the weights in all the layers of the network\n", | |
"- The loss function (cost function) measures the distance beween the predictions and target by computing a distance or loss score\n", | |
"- The loss score is used as a feedback signal to adjust the weights a little in a direction that will lower the loss for that example - this adjustment is done by the optimiser, which implements the Backpropagation algorithm (the central algorithm in Deep Learning)\n", | |
" - Weights are initially assigned random values, so the network implements a series of random transformations (& the loss score is accordingly high)\n", | |
" - Each example that is processed, the weights are adjusted a little in the correct direction, and the loss score decreases\n", | |
" - The is the training loop, repeated a sufficent number of times (typically tens of iterations over thousands of examples), yields weight values that minimize the loss function - a trained network\n", | |
"\n", | |
"## Deep Learning compared to Classical ML" | |
], | |
"metadata": { | |
"id": "dOynk5dd0zdn" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "5hbE0tgeWb_J" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"https://www.notion.so/oliverplatt/Deep-Learning-With-Python-4bcae72d2e884d7b8c12d690c173f940\n", | |
"https://colab.research.google.com/github/fchollet/deep-learning-with-python-notebooks/blob/master/chapter02_mathematical-building-blocks.ipynb" | |
], | |
"metadata": { | |
"id": "WKxxvvtd4qsv" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Mathematical Foundations" | |
], | |
"metadata": { | |
"id": "Wu_9FgtMAP88" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Simple End to End Example" | |
], | |
"metadata": { | |
"id": "wHyZlyHiwSxW" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# import some data to work with\n", | |
"from tensorflow.keras.datasets import mnist\n", | |
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n" | |
], | |
"metadata": { | |
"id": "aOdVtJZ9tar6", | |
"outputId": "fe0ecce1-7f96-4725-fa33-df7a79d039f1", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | |
"11493376/11490434 [==============================] - 0s 0us/step\n", | |
"11501568/11490434 [==============================] - 0s 0us/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# simple network architecture\n", | |
"from tensorflow import keras\n", | |
"from tensorflow.keras import layers\n", | |
"\n", | |
"model = keras.Sequential([\n", | |
" layers.Dense(512, activation=\"relu\"),\n", | |
" layers.Dense(10, activation=\"softmax\")\n", | |
"])" | |
], | |
"metadata": { | |
"id": "Wqma1GpEvAIi" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# compilation step: specify optimizer, loss function and metrics\n", | |
"model.compile(optimizer=\"rmsprop\",\n", | |
" loss=\"sparse_categorical_crossentropy\",\n", | |
" metrics=[\"accuracy\"])" | |
], | |
"metadata": { | |
"id": "QoIS4h4pvJda" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# flatten / prep the training data\n", | |
"train_images = train_images.reshape((60000, 28 * 28))\n", | |
"train_images = train_images.astype(\"float32\") / 255\n", | |
"test_images = test_images.reshape((10000, 28 * 28))\n", | |
"test_images = test_images.astype(\"float32\") / 255" | |
], | |
"metadata": { | |
"id": "l0t9wbupvR5x" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# fit the model\n", | |
"history = model.fit(train_images, train_labels, epochs=5, batch_size=128)" | |
], | |
"metadata": { | |
"id": "SfZqJyqEvisq", | |
"outputId": "483cf6f0-5fe2-4712-8f60-8e823a71c08b", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/5\n", | |
"469/469 [==============================] - 6s 13ms/step - loss: 0.0289 - accuracy: 0.9913\n", | |
"Epoch 2/5\n", | |
"469/469 [==============================] - 5s 10ms/step - loss: 0.0218 - accuracy: 0.9934\n", | |
"Epoch 3/5\n", | |
"469/469 [==============================] - 5s 10ms/step - loss: 0.0169 - accuracy: 0.9951\n", | |
"Epoch 4/5\n", | |
"469/469 [==============================] - 5s 10ms/step - loss: 0.0126 - accuracy: 0.9964\n", | |
"Epoch 5/5\n", | |
"469/469 [==============================] - 5s 10ms/step - loss: 0.0096 - accuracy: 0.9974\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# make some predictions\n", | |
"test_digits = test_images[0:10]\n", | |
"predictions = model.predict(test_digits)\n", | |
"predictions[0]" | |
], | |
"metadata": { | |
"id": "JMEGwSQvvmbG", | |
"outputId": "523c06be-6e45-44c7-b1f7-62d8c4b0307f", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([3.9937942e-13, 1.1269198e-14, 1.0252963e-09, 9.6365540e-08,\n", | |
" 7.2064595e-18, 1.9635361e-12, 7.7062272e-18, 9.9999988e-01,\n", | |
" 5.9941316e-11, 3.7747854e-09], dtype=float32)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 8 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"predictions[0].argmax()" | |
], | |
"metadata": { | |
"id": "9dGv7tu1wE1l", | |
"outputId": "eccc5c79-f96c-4c21-a8df-08061a7721f1", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"7" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"predictions[0][7]" | |
], | |
"metadata": { | |
"id": "vpR-X3DmwGSo", | |
"outputId": "56313b5d-9ad9-4c4f-e2ba-e110c3739f6f", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.9999999" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"test_labels[0]" | |
], | |
"metadata": { | |
"id": "FBYSFe5dwH2t", | |
"outputId": "c536d272-df24-4c06-8f01-a7f9a90e3d16", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"7" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"#displaying that digit\n", | |
"import matplotlib.pyplot as plt\n", | |
"digit = test_digits[0].reshape(28, 28)\n", | |
"plt.imshow(digit, cmap=plt.cm.binary)\n", | |
"plt.show()" | |
], | |
"metadata": { | |
"id": "BE1nAAD6zmVE", | |
"outputId": "9d30f4ba-aa9b-42d0-fe0e-8100c4586f15", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 265 | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAANPUlEQVR4nO3df6hc9ZnH8c9n3TSCqZq7ucRo46abiBLETcsQVivVVTckQYj9RxKkZEE2BRVbKLriolX8J6w2paBUE5WmS9dSTCVBgls3VDR/WDKaqDGy668bm3DNnRihKQjZpM/+cU/KNd45M86ZX8nzfsFlZs4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vAAAAAASUVORK5CYII=\n", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# evaluate the model on new data\n", | |
"test_loss, test_acc = model.evaluate(test_images, test_labels)\n", | |
"print(f\"test_acc: {test_acc}\")" | |
], | |
"metadata": { | |
"id": "wO1qBC2dwf9y", | |
"outputId": "067c427d-5b96-4e1e-e8a8-54f0f2ea9d5e", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"313/313 [==============================] - 2s 4ms/step - loss: 0.0678 - accuracy: 0.9828\n", | |
"test_acc: 0.9828000068664551\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Data Representations \n", | |
"- Tensors (scalars, vectors, matrices) and manipulating tensors (slicing, reshaping, transposition)" | |
], | |
"metadata": { | |
"id": "ulPJAiqIwZSx" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# scalars (rank 0 tensors)\n", | |
"import numpy as np\n", | |
"x = np.array(12)\n", | |
"x" | |
], | |
"metadata": { | |
"id": "sgwJjN73weJO", | |
"outputId": "ab037ed1-8059-4874-8680-4571c2611305", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array(12)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 15 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x.ndim" | |
], | |
"metadata": { | |
"id": "PaiZWSCSxISG", | |
"outputId": "42d40642-757d-4a7c-fb3f-929cc88c6cc6", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 14 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x.shape" | |
], | |
"metadata": { | |
"id": "mmtB63DsxNom", | |
"outputId": "ab9e3fe1-8417-4896-891b-fdf5abda089c", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 17 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# vectors (rank 1 tensors)\n", | |
"x = np.array([1, 6, 33, 89])\n", | |
"x" | |
], | |
"metadata": { | |
"id": "Ie7zF8LsxJkJ", | |
"outputId": "2493ee76-624c-478a-fcc4-0f3dc8eb2a68", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([ 1, 6, 33, 89])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 18 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x.shape" | |
], | |
"metadata": { | |
"id": "NpUZ0SOzxZIg", | |
"outputId": "8afa7d8b-e485-4264-f96f-f501a444c15e", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(4,)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 19 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x.ndim" | |
], | |
"metadata": { | |
"id": "HvxB_o5exbiB", | |
"outputId": "a90f3fcb-e3f4-46c1-aa57-8c93c201fe6d", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 20 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# matrices (rank 2 tensors)\n", | |
"m = np.array([[5, 78, 2, 34, 0],\n", | |
" [6, 79, 3, 35, 1],\n", | |
" [7, 80, 4, 36, 2]])\n", | |
"print(m.ndim, m.shape)\n", | |
"\n", | |
"# Rank-3 and higher-rank tensors\n", | |
"x = np.array([[[5, 78, 2, 34, 0],\n", | |
" [6, 79, 3, 35, 1],\n", | |
" [7, 80, 4, 36, 2]],\n", | |
" [[5, 78, 2, 34, 0],\n", | |
" [6, 79, 3, 35, 1],\n", | |
" [7, 80, 4, 36, 2]],\n", | |
" [[5, 78, 2, 34, 0],\n", | |
" [6, 79, 3, 35, 1],\n", | |
" [7, 80,\n", | |
" 4, 36, 2]]])\n", | |
"print(x.ndim, x.shape)" | |
], | |
"metadata": { | |
"id": "GA_i4F46xcwZ", | |
"outputId": "694af6a7-b34f-455a-9427-1558989b2738", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"2 (3, 5)\n", | |
"3 (3, 3, 5)\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 3 key attributes of a tensory - ndim (rank), shape, and datatype:\n", | |
"def print_key_attrs(tensor):\n", | |
" print(f\"Dimensions: {tensor.ndim}\\nShape: {tensor.shape}\\nDatatype: \\\n", | |
" {tensor.dtype}\\n\")\n", | |
"\n", | |
"print_key_attrs(train_images)" | |
], | |
"metadata": { | |
"id": "C2xmcWTPyt9j", | |
"outputId": "97247717-5acf-40fe-8c9d-25c21be142ef", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Dimensions: 2\n", | |
"Shape: (60000, 784)\n", | |
"Datatype: float32\n", | |
"\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# tensor slicing\n", | |
"train_images[10:100].shape" | |
], | |
"metadata": { | |
"id": "MTANtN6i0Nx8", | |
"outputId": "259d3efa-96ed-43c5-e92e-b9b8924983e9", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(90, 784)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 29 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"train_images.reshape(len(train_images), 28, 28)[10:100, :, 7:-7];" | |
], | |
"metadata": { | |
"id": "w-TL8Eku0WZJ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# data batches\n", | |
"n = 3\n", | |
"batch = train_images[128 * n:128 * (n + 1)]" | |
], | |
"metadata": { | |
"id": "ocbF8p3V0stU" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"batch.shape" | |
], | |
"metadata": { | |
"id": "wXCYMEli0wUJ", | |
"outputId": "8c8ef97a-5fc8-456d-f893-d2b061211890", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(128, 784)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 35 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# tensor reshaping\n", | |
"x = np.array([[0., 1.],\n", | |
" [2., 3.],\n", | |
" [4., 5.]])\n", | |
"x.shape" | |
], | |
"metadata": { | |
"id": "vJe1SzOh4PYx", | |
"outputId": "a5ed7568-7140-4f6e-a91a-d87803e468b3", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(3, 2)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 57 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x = x.reshape((6, 1))\n", | |
"x" | |
], | |
"metadata": { | |
"id": "FhTJVxIg4T2j", | |
"outputId": "ffd43a43-c1f3-49bf-a901-205b60e2bf7a", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[0.],\n", | |
" [1.],\n", | |
" [2.],\n", | |
" [3.],\n", | |
" [4.],\n", | |
" [5.]])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 58 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x.shape" | |
], | |
"metadata": { | |
"id": "mXc2pQGq4VM8", | |
"outputId": "9fe78ef3-a5ed-4cc6-bb36-a7b3d8f0fb91", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(6, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 59 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# transposition \n", | |
"x = np.zeros((300, 20))\n", | |
"x = np.transpose(x)\n", | |
"x.shape" | |
], | |
"metadata": { | |
"id": "Z42Qk6zN4Xiw", | |
"outputId": "09662cd5-4af0-4020-f5a2-9d6c6eec3109", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(20, 300)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 60 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Element-wise operations" | |
], | |
"metadata": { | |
"id": "LM3CCKRi1Ar8" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# implement a naive version of the relu function\n", | |
"\n", | |
"def naive_relu(x):\n", | |
" assert len(x.shape) == 2\n", | |
" x = x.copy()\n", | |
" for i in range(x.shape[0]):\n", | |
" for j in range(x.shape[1]):\n", | |
" x[i, j] = max(x[i, j], 0)\n", | |
" return x\n" | |
], | |
"metadata": { | |
"id": "XYkFhKVT1DDA" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# <question> Implement a naive version of element wise addition.->\n", | |
"def naive_add(x, y):\n", | |
" assert len(x.shape) == 2\n", | |
" assert x.shape == y.shape\n", | |
" x = x.copy()\n", | |
" for i in range(x.shape[0]):\n", | |
" for j in range(x.shape[1]):\n", | |
" x[i, j] += y[i, j]\n", | |
" return x" | |
], | |
"metadata": { | |
"id": "I9Br4UPy1VcT" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# vectorised implementation is orders of magnitude faster:\n", | |
"import time\n", | |
"\n", | |
"x = np.random.random((20, 100))\n", | |
"y = np.random.random((20, 100))\n", | |
"\n", | |
"t0 = time.time()\n", | |
"for _ in range(1000):\n", | |
" z = x + y\n", | |
" z = np.maximum(z, 0.)\n", | |
"print(\"Took: {0:.2f} s\".format(time.time() - t0))\n", | |
"\n", | |
"t0 = time.time()\n", | |
"for _ in range(1000):\n", | |
" z = naive_add(x, y)\n", | |
" z = naive_relu(z)\n", | |
"print(\"Took: {0:.2f} s\".format(time.time() - t0))" | |
], | |
"metadata": { | |
"id": "1_Z7JaPM1YME", | |
"outputId": "46908522-ac30-420b-fd5f-18fc87aebdbc", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Took: 0.01 s\n", | |
"Took: 3.13 s\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Broadcasting" | |
], | |
"metadata": { | |
"id": "FuYuVggW2YeZ" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"X = np.random.random((32, 10))\n", | |
"y = np.random.random((10,))\n", | |
"\n", | |
"y = np.expand_dims(y, axis=0)\n", | |
"Y = np.concatenate([y] * 32, axis=0)\n", | |
"\n", | |
"def naive_add_matrix_and_vector(x, y):\n", | |
" assert len(x.shape) == 2\n", | |
" assert len(y.shape) == 1\n", | |
" assert x.shape[1] == y.shape[0]\n", | |
" x = x.copy()\n", | |
" for i in range(x.shape[0]):\n", | |
" for j in range(x.shape[1]):\n", | |
" x[i, j] += y[j]\n", | |
" return x" | |
], | |
"metadata": { | |
"id": "OKK80dL23hxl" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"x = np.random.random((64, 3, 32, 10))\n", | |
"y = np.random.random((32, 10))\n", | |
"z = np.maximum(x, y)" | |
], | |
"metadata": { | |
"id": "xGuX7qkQ3ttD" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Tensor Product" | |
], | |
"metadata": { | |
"id": "-kk1GSgG3zU2" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x = np.random.random((32,))\n", | |
"y = np.random.random((32,))\n", | |
"z = np.dot(x, y)" | |
], | |
"metadata": { | |
"id": "zMxAObGc31Zd" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x" | |
], | |
"metadata": { | |
"id": "xmTUB7N836hR", | |
"outputId": "1efe5333-b4d2-4eb4-da3b-c7c811539b51", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([0.39597366, 0.74595874, 0.52688394, 0.23107508, 0.96309033,\n", | |
" 0.63890687, 0.17086246, 0.07146645, 0.24876876, 0.54438564,\n", | |
" 0.82414345, 0.37371909, 0.84637098, 0.5876804 , 0.92719026,\n", | |
" 0.93700578, 0.28879868, 0.62220377, 0.12290088, 0.28048737,\n", | |
" 0.28259094, 0.34097824, 0.68510723, 0.97858789, 0.32029276,\n", | |
" 0.14191975, 0.7533047 , 0.57790271, 0.82507176, 0.60296011,\n", | |
" 0.95288671, 0.93334805])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 47 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"y" | |
], | |
"metadata": { | |
"id": "9BBjNTWI39TE", | |
"outputId": "66d5f30c-7de9-4bfd-fd2c-11e8b662b1e2", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([5.15177960e-01, 1.20516826e-01, 9.33569272e-01, 9.32513790e-01,\n", | |
" 5.05601423e-01, 8.57185137e-01, 7.58539409e-01, 3.99197580e-01,\n", | |
" 8.56895333e-01, 3.59242003e-01, 6.85713477e-01, 5.34016006e-01,\n", | |
" 9.96928607e-02, 6.88465400e-01, 9.62644638e-01, 4.77547396e-01,\n", | |
" 1.26774376e-01, 9.43651923e-01, 2.60169147e-01, 1.13434096e-01,\n", | |
" 2.27754266e-01, 8.48016536e-01, 8.91197096e-01, 1.14245233e-01,\n", | |
" 5.65588192e-01, 2.21385432e-01, 9.01912889e-01, 1.80310890e-05,\n", | |
" 8.81125744e-01, 4.50189234e-03, 4.35923774e-01, 5.08529824e-02])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 52 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"z" | |
], | |
"metadata": { | |
"id": "xJOyMBwe37Uv", | |
"outputId": "b5b66577-a2d4-4e80-9d1c-7c7a52386382", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"9.044407971050417" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 48 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def naive_vector_dot(x, y):\n", | |
" assert len(x.shape) == 1\n", | |
" assert len(y.shape) == 1\n", | |
" assert x.shape[0] == y.shape[0]\n", | |
" z = 0.\n", | |
" for i in range(x.shape[0]):\n", | |
" z += x[i] * y[i]\n", | |
" return z" | |
], | |
"metadata": { | |
"id": "yW1vJJ0H4HWJ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def naive_matrix_vector_dot(x, y):\n", | |
" assert len(x.shape) == 2\n", | |
" assert len(y.shape) == 1\n", | |
" assert x.shape[1] == y.shape[0]\n", | |
" z = np.zeros(x.shape[0])\n", | |
" for i in range(x.shape[0]):\n", | |
" for j in range(x.shape[1]):\n", | |
" z[i] += x[i, j] * y[j]\n", | |
" return z" | |
], | |
"metadata": { | |
"id": "I0Pi4fqy4H_0" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def naive_matrix_vector_dot(x, y):\n", | |
" z = np.zeros(x.shape[0])\n", | |
" for i in range(x.shape[0]):\n", | |
" z[i] = naive_vector_dot(x[i, :], y)\n", | |
" return z" | |
], | |
"metadata": { | |
"id": "iMLKOUvG4JZv" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def naive_matrix_dot(x, y):\n", | |
" assert len(x.shape) == 2\n", | |
" assert len(y.shape) == 2\n", | |
" assert x.shape[1] == y.shape[0]\n", | |
" z = np.zeros((x.shape[0], y.shape[1]))\n", | |
" for i in range(x.shape[0]):\n", | |
" for j in range(y.shape[1]):\n", | |
" row_x = x[i, :]\n", | |
" column_y = y[:, j]\n", | |
" z[i, j] = naive_vector_dot(row_x, column_y)\n", | |
" return z" | |
], | |
"metadata": { | |
"id": "DR_UcWVL4Luk" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Gradient based optimization\n", | |
"- Derivatives\n", | |
"- Derivative of a tensor operation: the gradient\n", | |
"- Stochastic gradient descent\n", | |
"- Chaining derivatives: backpropogation\n", | |
"- Chain rule\n", | |
"- Automatic differentiation with computation graphs\n", | |
"- GradientTape()" | |
], | |
"metadata": { | |
"id": "ZZhc1x5K4xJx" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# gradient Tape object in TF\n", | |
"import tensorflow as tf\n", | |
"x = tf.Variable(0.)\n", | |
"with tf.GradientTape() as tape:\n", | |
" y = 2 * x + 3\n", | |
"grad_of_y_wrt_x = tape.gradient(y, x)\n", | |
"grad_of_y_wrt_x" | |
], | |
"metadata": { | |
"id": "WBuDNH8g6TI5", | |
"outputId": "396407bc-90bd-4ab6-b183-1e1bfd074529", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<tf.Tensor: shape=(), dtype=float32, numpy=2.0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 62 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x = tf.Variable(tf.random.uniform((2, 2)))\n", | |
"with tf.GradientTape() as tape:\n", | |
" y = 2 * x + 3\n", | |
"grad_of_y_wrt_x = tape.gradient(y, x)\n", | |
"grad_of_y_wrt_x\n" | |
], | |
"metadata": { | |
"id": "vyRXLqiE6YLO", | |
"outputId": "43d075f4-9c75-4cff-c5b5-96a2a09c3008", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n", | |
"array([[2., 2.],\n", | |
" [2., 2.]], dtype=float32)>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 63 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"W = tf.Variable(tf.random.uniform((2, 2)))\n", | |
"b = tf.Variable(tf.zeros((2,)))\n", | |
"x = tf.random.uniform((2, 2))\n", | |
"with tf.GradientTape() as tape:\n", | |
" y = tf.matmul(x, W) + b\n", | |
"grad_of_y_wrt_W_and_b = tape.gradient(y, [W, b])" | |
], | |
"metadata": { | |
"id": "9lXEWeOK6XUL" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"grad_of_y_wrt_W_and_b" | |
], | |
"metadata": { | |
"id": "m8bAWWiX6zwo", | |
"outputId": "087eb503-30a9-406b-e5df-48145a354292", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n", | |
" array([[0.54857814, 0.54857814],\n", | |
" [1.295201 , 1.295201 ]], dtype=float32)>,\n", | |
" <tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>]" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 65 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Simple Linear Classifier from Scratch in TF\n", | |
"\n", | |
"\n", | |
"\n", | |
"* Implement Layer class (NaiveDense)\n", | |
"* Implement Model class (NaiveSequential)\n", | |
"* Implement BatchGenerator class\n", | |
"* Implement loss function\n", | |
"* Implement training loop\n", | |
"\n", | |
"\n" | |
], | |
"metadata": { | |
"id": "W0nMMGKA78Wb" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import tensorflow as tf \n", | |
"from typing import List, Callable\n", | |
"\n", | |
"class NaiveDense: \n", | |
"\n", | |
" def __init__(self, input_size: int, output_size: int, activation: Callable): \n", | |
" self.activation = activation # non linear, element wise activation function, e.g. softmax, relu \n", | |
" w_shape = (input_size, output_size) # shape of trainable parameters \n", | |
" w_initial_value = tf.random.uniform(w_shape, minval=0, maxval=1e-1) # initialise with random parameters \n", | |
" self.W = tf.Variable(w_initial_value) # create Variable to hold mutable state\n", | |
" b_shape = (output_size) # initialise constant with shape of output size\n", | |
" b_initial_value = tf.zeros(b_shape) \n", | |
" self.b = tf.Variable(b_initial_value) # create Variable to hold mutable bias\n", | |
" \n", | |
" def __call__(self, inputs): \n", | |
" \"\"\"\n", | |
" apply activation function to linear transform (Layer Input ⋅ Weights + bias)\n", | |
" \"\"\"\n", | |
" return self.activation(tf.matmul(inputs, self.W) + self.b) \n", | |
" \n", | |
" @property \n", | |
" def weights(self):\n", | |
" return [self.W, self.b] " | |
], | |
"metadata": { | |
"id": "rvQhZK2X7_Db" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"class NaiveSequential: \n", | |
" \n", | |
" def __init__(self, layers): \n", | |
" self.layers = layers \n", | |
" \n", | |
" def __call__(self, inputs): \n", | |
" x = inputs \n", | |
" for layer in self.layers: \n", | |
" x = layer(x) \n", | |
" return x \n", | |
" \n", | |
" @property \n", | |
" def weights(self): \n", | |
" weights = [] \n", | |
" for layer in self.layers: \n", | |
" weights += layer.weights \n", | |
" return weights" | |
], | |
"metadata": { | |
"id": "aG6VKEBQ-H00" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model = NaiveSequential([\n", | |
" NaiveDense(input_size=28 * 28, output_size=512, activation=tf.nn.relu),\n", | |
" NaiveDense(input_size=512, output_size=10, activation=tf.nn.softmax)\n", | |
"])\n", | |
"assert len(model.weights) == 4" | |
], | |
"metadata": { | |
"id": "m9XtSUoA9ElY" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# batch generator\n", | |
"import math\n", | |
"\n", | |
"class BatchGenerator:\n", | |
" def __init__(self, images, labels, batch_size=128):\n", | |
" assert len(images) == len(labels)\n", | |
" self.index = 0\n", | |
" self.images = images\n", | |
" self.labels = labels\n", | |
" self.batch_size = batch_size\n", | |
" self.num_batches = math.ceil(len(images) / batch_size)\n", | |
"\n", | |
" def next(self):\n", | |
" images = self.images[self.index : self.index + self.batch_size]\n", | |
" labels = self.labels[self.index : self.index + self.batch_size]\n", | |
" self.index += self.batch_size\n", | |
" return images, labels" | |
], | |
"metadata": { | |
"id": "EyZMSJFk77b_" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# running one training step \n", | |
"def one_training_step(model, images_batch, labels_batch):\n", | |
" with tf.GradientTape() as tape:\n", | |
" predictions = model(images_batch)\n", | |
" per_sample_losses = tf.keras.losses.sparse_categorical_crossentropy(\n", | |
" labels_batch, predictions)\n", | |
" average_loss = tf.reduce_mean(per_sample_losses)\n", | |
" gradients = tape.gradient(average_loss, model.weights)\n", | |
" update_weights(gradients, model.weights)\n", | |
" return average_loss\n", | |
"\n", | |
"learning_rate = 1e-3\n", | |
"\n", | |
"def update_weights(gradients, weights):\n", | |
" for g, w in zip(gradients, weights):\n", | |
" w.assign_sub(g * learning_rate)\n", | |
"\n", | |
"from tensorflow.keras import optimizers\n", | |
"\n", | |
"optimizer = optimizers.SGD(learning_rate=1e-3)\n", | |
"\n", | |
"def update_weights(gradients, weights):\n", | |
" optimizer.apply_gradients(zip(gradients, weights))" | |
], | |
"metadata": { | |
"id": "Nyhn8Bk77W_a" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# the full training loop\n", | |
"def fit(model, images, labels, epochs, batch_size=128):\n", | |
" for epoch_counter in range(epochs):\n", | |
" print(f\"Epoch {epoch_counter}\")\n", | |
" batch_generator = BatchGenerator(images, labels)\n", | |
" for batch_counter in range(batch_generator.num_batches):\n", | |
" images_batch, labels_batch = batch_generator.next()\n", | |
" loss = one_training_step(model, images_batch, labels_batch)\n", | |
" if batch_counter % 100 == 0:\n", | |
" print(f\"loss at batch {batch_counter}: {loss:.2f}\")" | |
], | |
"metadata": { | |
"id": "y-ts3mvi7d9S" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from tensorflow.keras.datasets import mnist\n", | |
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n", | |
"\n", | |
"train_images = train_images.reshape((60000, 28 * 28))\n", | |
"train_images = train_images.astype(\"float32\") / 255\n", | |
"test_images = test_images.reshape((10000, 28 * 28))\n", | |
"test_images = test_images.astype(\"float32\") / 255\n", | |
"\n", | |
"fit(model, train_images, train_labels, epochs=10, batch_size=128)" | |
], | |
"metadata": { | |
"id": "ADPp7_827hJY", | |
"outputId": "4328c6bb-e91c-4de4-ae9d-462b1d113707", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 0\n", | |
"loss at batch 0: 5.29\n", | |
"loss at batch 100: 2.25\n", | |
"loss at batch 200: 2.21\n", | |
"loss at batch 300: 2.08\n", | |
"loss at batch 400: 2.22\n", | |
"Epoch 1\n", | |
"loss at batch 0: 1.91\n", | |
"loss at batch 100: 1.89\n", | |
"loss at batch 200: 1.83\n", | |
"loss at batch 300: 1.70\n", | |
"loss at batch 400: 1.83\n", | |
"Epoch 2\n", | |
"loss at batch 0: 1.58\n", | |
"loss at batch 100: 1.59\n", | |
"loss at batch 200: 1.50\n", | |
"loss at batch 300: 1.41\n", | |
"loss at batch 400: 1.51\n", | |
"Epoch 3\n", | |
"loss at batch 0: 1.33\n", | |
"loss at batch 100: 1.34\n", | |
"loss at batch 200: 1.24\n", | |
"loss at batch 300: 1.19\n", | |
"loss at batch 400: 1.27\n", | |
"Epoch 4\n", | |
"loss at batch 0: 1.13\n", | |
"loss at batch 100: 1.16\n", | |
"loss at batch 200: 1.03\n", | |
"loss at batch 300: 1.03\n", | |
"loss at batch 400: 1.11\n", | |
"Epoch 5\n", | |
"loss at batch 0: 0.98\n", | |
"loss at batch 100: 1.02\n", | |
"loss at batch 200: 0.89\n", | |
"loss at batch 300: 0.91\n", | |
"loss at batch 400: 0.99\n", | |
"Epoch 6\n", | |
"loss at batch 0: 0.88\n", | |
"loss at batch 100: 0.91\n", | |
"loss at batch 200: 0.78\n", | |
"loss at batch 300: 0.82\n", | |
"loss at batch 400: 0.90\n", | |
"Epoch 7\n", | |
"loss at batch 0: 0.79\n", | |
"loss at batch 100: 0.82\n", | |
"loss at batch 200: 0.71\n", | |
"loss at batch 300: 0.75\n", | |
"loss at batch 400: 0.84\n", | |
"Epoch 8\n", | |
"loss at batch 0: 0.73\n", | |
"loss at batch 100: 0.75\n", | |
"loss at batch 200: 0.64\n", | |
"loss at batch 300: 0.70\n", | |
"loss at batch 400: 0.78\n", | |
"Epoch 9\n", | |
"loss at batch 0: 0.68\n", | |
"loss at batch 100: 0.70\n", | |
"loss at batch 200: 0.60\n", | |
"loss at batch 300: 0.65\n", | |
"loss at batch 400: 0.74\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# evaluating the model\n", | |
"predictions = model(test_images)\n", | |
"predictions = predictions.numpy()\n", | |
"predicted_labels = np.argmax(predictions, axis=1)\n", | |
"matches = predicted_labels == test_labels\n", | |
"print(f\"accuracy: {matches.mean():.2f}\")" | |
], | |
"metadata": { | |
"id": "EKg3jS9O7iZW", | |
"outputId": "5013e20b-c285-446e-bc9f-9072da10057a", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"accuracy: 0.82\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Tensorflow & Keras Fundamentals/Cheatsheet\n" | |
], | |
"metadata": { | |
"id": "f0VW-k0w6kFD" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Datasets " | |
], | |
"metadata": { | |
"id": "lZ5p6wBu6wZ5" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import tensorflow_datasets as tfds\n", | |
"mnist_data = tfds.load(\"mnist\")\n", | |
"mnist_train, mnist_test = mnist_data[\"train\"], mnist_data[\"test\"]\n", | |
"assert isinstance(mnist_train, tf.data.Dataset)" | |
], | |
"metadata": { | |
"id": "LqQXKoYIzMdU", | |
"outputId": "4adb63fb-600e-4833-f01d-7aa6c788d548", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 520, | |
"referenced_widgets": [ | |
"2ed5ba99b26d43fdb102c6b41ba10159", | |
"a714b6a0e2b44695a0a4e5a40d2d3069", | |
"b50e5088fde2442c9304bf6ae5ea8c15", | |
"97ce12548f11408c9711dd73ff543ca4", | |
"f81453dc01d4443bb22777bfd5a30145", | |
"f504361dd913458e9f8ee8f121e12658", | |
"cfcb533912b04fbeafa2e21c6c3c3e3c", | |
"58b5ce6cdabd47258c50ea7e30f810bc", | |
"36f9d981c0ef41a2918f36f349171aa0", | |
"d240397e001b472bb0cde3e94f7b0683", | |
"6a2439b5a3054ced8c567790dc6d6ac5" | |
] | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[1mDownloading and preparing dataset mnist/3.0.1 (download: 11.06 MiB, generated: 21.00 MiB, total: 32.06 MiB) to /root/tensorflow_datasets/mnist/3.0.1...\u001b[0m\n" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"WARNING:absl:Dataset mnist is hosted on GCS. It will automatically be downloaded to your\n", | |
"local data directory. If you'd instead prefer to read directly from our public\n", | |
"GCS bucket (recommended if you're running on GCP), you can instead pass\n", | |
"`try_gcs=True` to `tfds.load` or set `data_dir=gs://tfds-data/datasets`.\n", | |
"\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "2ed5ba99b26d43fdb102c6b41ba10159", | |
"version_minor": 0, | |
"version_major": 2 | |
}, | |
"text/plain": [ | |
"Dl Completed...: 0%| | 0/4 [00:00<?, ? file/s]" | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "error", | |
"ename": "KeyboardInterrupt", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-20-d8d561a631c8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtensorflow_datasets\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtfds\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmnist_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtfds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"mnist\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mmnist_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmnist_test\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmnist_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"train\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmnist_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"test\"\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;32massert\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmnist_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_datasets/core/load.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(name, split, data_dir, batch_size, shuffle_files, download, as_supervised, decoders, read_config, with_info, builder_kwargs, download_and_prepare_kwargs, as_dataset_kwargs, try_gcs)\u001b[0m\n\u001b[1;32m 342\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdownload\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 343\u001b[0m \u001b[0mdownload_and_prepare_kwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdownload_and_prepare_kwargs\u001b[0m \u001b[0;32mor\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;32m--> 344\u001b[0;31m \u001b[0mdbuilder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdownload_and_prepare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mdownload_and_prepare_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 345\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 346\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mas_dataset_kwargs\u001b[0m \u001b[0;32mis\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;32m/usr/local/lib/python3.7/dist-packages/tensorflow_datasets/core/dataset_builder.py\u001b[0m in \u001b[0;36mdownload_and_prepare\u001b[0;34m(self, download_dir, download_config)\u001b[0m\n\u001b[1;32m 380\u001b[0m gcs_utils.is_dataset_on_gcs(self.info.full_name)):\n\u001b[1;32m 381\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mGCS_HOSTED_MSG\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 382\u001b[0;31m \u001b[0mgcs_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdownload_gcs_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 383\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_from_directory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 384\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_datasets/core/utils/gcs_utils.py\u001b[0m in \u001b[0;36mdownload_gcs_dataset\u001b[0;34m(dataset_name, local_dataset_dir, max_simultaneous_downloads)\u001b[0m\n\u001b[1;32m 123\u001b[0m \u001b[0mexecutor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubmit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_copy_from_gcs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgcs_paths_to_dl\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 124\u001b[0m ]\n\u001b[0;32m--> 125\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfuture\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mconcurrent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfutures\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_completed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfutures\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[0m\u001b[1;32m 126\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\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;32m/usr/lib/python3.7/concurrent/futures/_base.py\u001b[0m in \u001b[0;36mas_completed\u001b[0;34m(fs, timeout)\u001b[0m\n\u001b[1;32m 238\u001b[0m len(pending), total_futures))\n\u001b[1;32m 239\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 240\u001b[0;31m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwait_timeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 241\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 242\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlock\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/lib/python3.7/threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 550\u001b[0m \u001b[0msignaled\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_flag\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 551\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0msignaled\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 552\u001b[0;31m \u001b[0msignaled\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_cond\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 553\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msignaled\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/lib/python3.7/threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# restore state no matter what (e.g., KeyboardInterrupt)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 295\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\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;32m--> 296\u001b[0;31m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\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[0m\u001b[1;32m 297\u001b[0m \u001b[0mgotit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 298\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m: " | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"mnist_train" | |
], | |
"metadata": { | |
"id": "4_udMBJYzeGA" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"\n" | |
], | |
"metadata": { | |
"id": "gcooRTQJ7KCq" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "tyN8DVEfXGG_" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Deep Learning Fundamentals\n" | |
], | |
"metadata": { | |
"id": "84W_nIq_XHK5" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "qSiFV4Z6XJAk" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Classification and Regression" | |
], | |
"metadata": { | |
"id": "x1gOw2K49FeG" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from tensorflow.keras.datasets import reuters\n", | |
"\n", | |
"(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)" | |
], | |
"metadata": { | |
"id": "I39uIL99Ranc", | |
"outputId": "8cc0cc58-7ff2-47b8-dce0-e28193591567", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/reuters.npz\n", | |
"2113536/2110848 [==============================] - 0s 0us/step\n", | |
"2121728/2110848 [==============================] - 0s 0us/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"len(train_data)" | |
], | |
"metadata": { | |
"id": "8FoqoL3kRpXq", | |
"outputId": "164ab4f6-e00f-47da-b42a-00550c079235", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"8982" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 3 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"type(train_data)" | |
], | |
"metadata": { | |
"id": "HlRQzgLbSQVn", | |
"outputId": "06197fc0-eee6-435f-faa6-b6f41898598e", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 6 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Convolutional Neural Nets & Image Recognition" | |
], | |
"metadata": { | |
"id": "32y_Nbpz9Fk4" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Sequence Models and Text " | |
], | |
"metadata": { | |
"id": "Cdf4G4g49Nrp" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Deep Learning Exam" | |
], | |
"metadata": { | |
"id": "nC0K3KzF8-Rd" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "mjbadK9K9Aw2" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment