"Fossies" - the Fresh Open Source Software Archive

Member "tensorflow-2.0.0/tensorflow/contrib/eager/python/examples/workshop/1_basic.ipynb" (27 Sep 2019, 7695 Bytes) of package /linux/misc/tensorflow-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "1_basic.ipynb": 1.14.0_vs_2.0.0.

    1 {
    2   "nbformat": 4,
    3   "nbformat_minor": 0,
    4   "metadata": {
    5     "colab": {
    6       "name": "TFE Workshop: control flow",
    7       "version": "0.3.2",
    8       "provenance": [],
    9       "include_colab_link": true
   10     }
   11   },
   12   "cells": [
   13     {
   14       "cell_type": "markdown",
   15       "metadata": {
   16         "id": "view-in-github",
   17         "colab_type": "text"
   18       },
   19       "source": [
   20         "[View in Colaboratory](https://colab.research.google.com/gist/alextp/664b2f8700485ff6801f4d26293bd567/tfe-workshop-control-flow.ipynb)"
   21       ]
   22     },
   23     {
   24       "metadata": {
   25         "id": "9BpQzh9BvJlj",
   26         "colab_type": "code",
   27         "colab": {
   28           "base_uri": "https://localhost:8080/",
   29           "height": 37
   30         },
   31         "outputId": "0b336886-8204-4815-89fa-5291a49d5784"
   32       },
   33       "cell_type": "code",
   34       "source": [
   35         "import tensorflow as tf\n",
   36         "import numpy as np\n",
   37         "tf.enable_eager_execution()"
   38       ],
   39       "execution_count": 1,
   40       "outputs": []
   41     },
   42     {
   43       "metadata": {
   44         "id": "0roIB19GvOjI",
   45         "colab_type": "text"
   46       },
   47       "cell_type": "markdown",
   48       "source": [
   49         "# Eager execution basics\n",
   50         "\n",
   51         "When eager execution is enabled TensorFlow immediately executes operations, and Tensors are always available. "
   52       ]
   53     },
   54     {
   55       "metadata": {
   56         "id": "jeO8F-V-vN24",
   57         "colab_type": "code",
   58         "colab": {
   59           "base_uri": "https://localhost:8080/",
   60           "height": 68
   61         },
   62         "outputId": "aeb3bdec-50b7-440d-93d8-5a171f091081"
   63       },
   64       "cell_type": "code",
   65       "source": [
   66         "t = tf.constant([[1, 2], [3, 4]])\n",
   67         "t"
   68       ],
   69       "execution_count": 2,
   70       "outputs": [
   71         {
   72           "output_type": "execute_result",
   73           "data": {
   74             "text/plain": [
   75               "<tf.Tensor: id=0, shape=(2, 2), dtype=int32, numpy=\n",
   76               "array([[1, 2],\n",
   77               "       [3, 4]], dtype=int32)>"
   78             ]
   79           },
   80           "metadata": {
   81             "tags": []
   82           },
   83           "execution_count": 2
   84         }
   85       ]
   86     },
   87     {
   88       "metadata": {
   89         "id": "Y17RwSFxvlDL",
   90         "colab_type": "code",
   91         "colab": {
   92           "base_uri": "https://localhost:8080/",
   93           "height": 68
   94         },
   95         "outputId": "cfcc10c7-707b-4997-99b3-a5f382c5166b"
   96       },
   97       "cell_type": "code",
   98       "source": [
   99         "tf.matmul(t, t)"
  100       ],
  101       "execution_count": 3,
  102       "outputs": [
  103         {
  104           "output_type": "execute_result",
  105           "data": {
  106             "text/plain": [
  107               "<tf.Tensor: id=2, shape=(2, 2), dtype=int32, numpy=\n",
  108               "array([[ 7, 10],\n",
  109               "       [15, 22]], dtype=int32)>"
  110             ]
  111           },
  112           "metadata": {
  113             "tags": []
  114           },
  115           "execution_count": 3
  116         }
  117       ]
  118     },
  119     {
  120       "metadata": {
  121         "id": "Dab1bS3TvmRE",
  122         "colab_type": "code",
  123         "colab": {
  124           "base_uri": "https://localhost:8080/",
  125           "height": 34
  126         },
  127         "outputId": "8a624f3d-a658-4359-c586-1c5f6bf4c8b7"
  128       },
  129       "cell_type": "code",
  130       "source": [
  131         "# It's also possible to have Python control flow which depends on the value of tensors.\n",
  132         "if t[0, 0] > 0.5:\n",
  133         "  print(\"T is bigger\")\n",
  134         "else:\n",
  135         "  print(\"T is smaller\")"
  136       ],
  137       "execution_count": 4,
  138       "outputs": [
  139         {
  140           "output_type": "stream",
  141           "text": [
  142             "T is bigger\n"
  143           ],
  144           "name": "stdout"
  145         }
  146       ]
  147     },
  148     {
  149       "metadata": {
  150         "id": "dPgptJcGwIon",
  151         "colab_type": "code",
  152         "colab": {
  153           "base_uri": "https://localhost:8080/",
  154           "height": 34
  155         },
  156         "outputId": "c4f27f2b-0848-4475-dde5-2534dac65a5c"
  157       },
  158       "cell_type": "code",
  159       "source": [
  160         "# Tensors are also usable as numpy arrays\n",
  161         "np.prod(t)"
  162       ],
  163       "execution_count": 6,
  164       "outputs": [
  165         {
  166           "output_type": "execute_result",
  167           "data": {
  168             "text/plain": [
  169               "24"
  170             ]
  171           },
  172           "metadata": {
  173             "tags": []
  174           },
  175           "execution_count": 6
  176         }
  177       ]
  178     },
  179     {
  180       "metadata": {
  181         "id": "p3DTfQXnwXzj",
  182         "colab_type": "text"
  183       },
  184       "cell_type": "markdown",
  185       "source": [
  186         "# Exercise\n",
  187         "\n",
  188         "The algorithm for bisecting line search is a pretty simple way to find a zero of a continuous scalar function in an interval [a,b] where f(a) and f(b) have different signs. Simply evaluate f((a+b)/2), and narrow the interval by replacing either a or b with (a+b)/2 such that the function when applied on the boundary of the interval still has different signs.\n",
  189         "\n",
  190         "Implement a python function `bisecting_line_search(f, a, b, epsilon)` which returns a value such that `tf.abs(f(value)) < epsilon`.\n",
  191         "\n",
  192         "One thing to keep in mind: python's `==` operator is not overloaded on Tensors, so you need to use `tf.equal` to compare for equality."
  193       ]
  194     },
  195     {
  196       "metadata": {
  197         "id": "6eq0YuI6ykm5",
  198         "colab_type": "code",
  199         "colab": {}
  200       },
  201       "cell_type": "code",
  202       "source": [
  203         "# Example test harness to get you going\n",
  204         "\n",
  205         "def test_f(x):\n",
  206         "  return x - 0.1234\n",
  207         "def bisecting_line_search(f, a, b, epsilon):\n",
  208         "  # Return x such that f(x) <= epsilon.\n",
  209         "  pass\n",
  210         "a = tf.constant(0.0)\n",
  211         "b = tf.constant(1.0)\n",
  212         "epsilon = tf.constant(0.001)\n",
  213         "x = bisecting_line_search(test_f, a, b, epsilon)\n"
  214       ],
  215       "execution_count": 0,
  216       "outputs": []
  217     },
  218     {
  219       "metadata": {
  220         "id": "LcMmEfd_xvej",
  221         "colab_type": "code",
  222         "colab": {
  223           "base_uri": "https://localhost:8080/",
  224           "height": 170
  225         },
  226         "outputId": "f402aa50-8ce3-4416-f755-8bbcd1af7809"
  227       },
  228       "cell_type": "code",
  229       "source": [
  230         "#@title Double-click to see the solution\n",
  231         "\n",
  232         "def bisecting_line_search(f, a, b, epsilon):\n",
  233         "  f_a = f(a)\n",
  234         "  f_b = f(b)\n",
  235         "  probe = (a + b) / 2\n",
  236         "  f_probe = f(probe)\n",
  237         "  while tf.abs(f_probe) > epsilon:\n",
  238         "    if tf.equal(tf.sign(f_probe), tf.sign(f_a)):\n",
  239         "      a = probe\n",
  240         "      f_a = f_probe\n",
  241         "    else:\n",
  242         "      b = probe\n",
  243         "      f_b = f_probe\n",
  244         "    probe = (a + b) / 2\n",
  245         "    f_probe = f(probe)\n",
  246         "    print(\"new probe\", probe)\n",
  247         "  return probe\n",
  248         "\n",
  249         "bisecting_line_search(test_f, 0., 1., 0.001)"
  250       ],
  251       "execution_count": 8,
  252       "outputs": [
  253         {
  254           "output_type": "stream",
  255           "text": [
  256             "('new probe', 0.25)\n",
  257             "('new probe', 0.125)\n",
  258             "('new probe', 0.0625)\n",
  259             "('new probe', 0.09375)\n",
  260             "('new probe', 0.109375)\n",
  261             "('new probe', 0.1171875)\n",
  262             "('new probe', 0.12109375)\n",
  263             "('new probe', 0.123046875)\n"
  264           ],
  265           "name": "stdout"
  266         },
  267         {
  268           "output_type": "execute_result",
  269           "data": {
  270             "text/plain": [
  271               "0.123046875"
  272             ]
  273           },
  274           "metadata": {
  275             "tags": []
  276           },
  277           "execution_count": 8
  278         }
  279       ]
  280     }
  281   ]
  282 }