aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/eager
diff options
context:
space:
mode:
authorGravatar Mark Daoust <markdaoust@google.com>2018-08-27 11:34:33 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-08-27 11:41:51 -0700
commit8199873c4460baec443bade20b121c1516081fc6 (patch)
tree6dccec42b77fd6753a6c6cc32ebe52a1e82701b0 /tensorflow/contrib/eager
parent5b08d751575c577f4ec5f241e5d8b3a6e356c07a (diff)
These files have moved to the docs repo.
Diffstat (limited to 'tensorflow/contrib/eager')
-rw-r--r--tensorflow/contrib/eager/python/examples/notebooks/README.md14
-rw-r--r--tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb298
-rw-r--r--tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb389
-rw-r--r--tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb467
-rw-r--r--tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb485
5 files changed, 127 insertions, 1526 deletions
diff --git a/tensorflow/contrib/eager/python/examples/notebooks/README.md b/tensorflow/contrib/eager/python/examples/notebooks/README.md
index 0d5ed84894..2778b228e9 100644
--- a/tensorflow/contrib/eager/python/examples/notebooks/README.md
+++ b/tensorflow/contrib/eager/python/examples/notebooks/README.md
@@ -1,11 +1,3 @@
-## Research and experimentation
-
-Eager execution provides an imperative, define-by-run interface for advanced
-operations. Write custom layers, forward passes, and training loops with auto
-differentiation. Start with these notebooks, then read the
-[eager execution guide](https://www.tensorflow.org/guide/eager).
-
-1. [Eager execution basics](./eager_basics.ipynb)
-2. [Automatic differentiation and gradient tapes](./automatic_differentiation.ipynb)
-3. [Custom training: basics](./custom_training.ipynb)
-4. [Custom layers](./custom_layers.ipynb)
+The notebooks have been moved to the
+[tensorflow/docs](https://github.com/tensorflow/docs/tree/master/site/en/tutorials/eager)
+repository.
diff --git a/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb b/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb
index 51b7ffc4de..8fae622e12 100644
--- a/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb
+++ b/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb
@@ -15,12 +15,7 @@
"execution_count": 0,
"metadata": {
"cellView": "form",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
+ "colab": {},
"colab_type": "code",
"id": "GCCk8_dHpuNf"
},
@@ -53,308 +48,35 @@
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
- "id": "idv0bPeCp325"
- },
- "source": [
- "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n",
- "\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb\"\u003e\n",
- " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
- "\u003c/td\u003e\u003ctd\u003e\n",
- "\u003ca target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/automatic_differentiation.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "vDJ4XzMqodTy"
- },
- "source": [
- "In the previous tutorial we introduced `Tensor`s and operations on them. In this tutorial we will cover [automatic differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation), a key technique for optimizing machine learning models."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "GQJysDM__Qb0"
- },
- "source": [
- "## Setup\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "OiMPZStlibBv"
- },
- "outputs": [],
- "source": [
- "import tensorflow as tf\n",
- "tf.enable_eager_execution()\n",
- "\n",
- "tfe = tf.contrib.eager # Shorthand for some symbols"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "1CLWJl0QliB0"
- },
- "source": [
- "## Derivatives of a function\n",
- "\n",
- "TensorFlow provides APIs for automatic differentiation - computing the derivative of a function. The way that more closely mimics the math is to encapsulate the computation in a Python function, say `f`, and use `tfe.gradients_function` to create a function that computes the derivatives of `f` with respect to its arguments. If you're familiar with [autograd](https://github.com/HIPS/autograd) for differentiating numpy functions, this will be familiar. For example: "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "9FViq92UX7P8"
- },
- "outputs": [],
- "source": [
- "from math import pi\n",
- "\n",
- "def f(x):\n",
- " return tf.square(tf.sin(x))\n",
- "\n",
- "assert f(pi/2).numpy() == 1.0\n",
- "\n",
- "\n",
- "# grad_f will return a list of derivatives of f\n",
- "# with respect to its arguments. Since f() has a single argument,\n",
- "# grad_f will return a list with a single element.\n",
- "grad_f = tfe.gradients_function(f)\n",
- "assert tf.abs(grad_f(pi/2)[0]).numpy() \u003c 1e-7"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "v9fPs8RyopCf"
- },
- "source": [
- "### Higher-order gradients\n",
- "\n",
- "The same API can be used to differentiate as many times as you like:\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "3D0ZvnGYo0rW"
- },
- "outputs": [],
- "source": [
- "def f(x):\n",
- " return tf.square(tf.sin(x))\n",
- "\n",
- "def grad(f):\n",
- " return lambda x: tfe.gradients_function(f)(x)[0]\n",
- "\n",
- "x = tf.lin_space(-2*pi, 2*pi, 100) # 100 points between -2π and +2π\n",
- "\n",
- "import matplotlib.pyplot as plt\n",
- "\n",
- "plt.plot(x, f(x), label=\"f\")\n",
- "plt.plot(x, grad(f)(x), label=\"first derivative\")\n",
- "plt.plot(x, grad(grad(f))(x), label=\"second derivative\")\n",
- "plt.plot(x, grad(grad(grad(f)))(x), label=\"third derivative\")\n",
- "plt.legend()\n",
- "plt.show()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "-39gouo7mtgu"
- },
- "source": [
- "## Gradient tapes\n",
- "\n",
- "Every differentiable TensorFlow operation has an associated gradient function. For example, the gradient function of `tf.square(x)` would be a function that returns `2.0 * x`. To compute the gradient of a user-defined function (like `f(x)` in the example above), TensorFlow first \"records\" all the operations applied to compute the output of the function. We call this record a \"tape\". It then uses that tape and the gradients functions associated with each primitive operation to compute the gradients of the user-defined function using [reverse mode differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation).\n",
- "\n",
- "Since operations are recorded as they are executed, Python control flow (using `if`s and `while`s for example) is naturally handled:\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "MH0UfjympWf7"
- },
- "outputs": [],
- "source": [
- "def f(x, y):\n",
- " output = 1\n",
- " # Must use range(int(y)) instead of range(y) in Python 3 when\n",
- " # using TensorFlow 1.10 and earlier. Can use range(y) in 1.11+\n",
- " for i in range(int(y)):\n",
- " output = tf.multiply(output, x)\n",
- " return output\n",
- "\n",
- "def g(x, y):\n",
- " # Return the gradient of `f` with respect to it's first parameter\n",
- " return tfe.gradients_function(f)(x, y)[0]\n",
- "\n",
- "assert f(3.0, 2).numpy() == 9.0 # f(x, 2) is essentially x * x\n",
- "assert g(3.0, 2).numpy() == 6.0 # And its gradient will be 2 * x\n",
- "assert f(4.0, 3).numpy() == 64.0 # f(x, 3) is essentially x * x * x\n",
- "assert g(4.0, 3).numpy() == 48.0 # And its gradient will be 3 * x * x"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "aNmR5-jhpX2t"
- },
- "source": [
- "At times it may be inconvenient to encapsulate computation of interest into a function. For example, if you want the gradient of the output with respect to intermediate values computed in the function. In such cases, the slightly more verbose but explicit [tf.GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape) context is useful. All computation inside the context of a `tf.GradientTape` is \"recorded\".\n",
- "\n",
- "For example:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "bAFeIE8EuVIq"
+ "id": "clNGnJ3u8Rl6"
},
- "outputs": [],
"source": [
- "x = tf.ones((2, 2))\n",
- " \n",
- "# TODO(b/78880779): Remove the 'persistent=True' argument and use\n",
- "# a single t.gradient() call when the bug is resolved.\n",
- "with tf.GradientTape(persistent=True) as t:\n",
- " # TODO(ashankar): Explain with \"watch\" argument better?\n",
- " t.watch(x)\n",
- " y = tf.reduce_sum(x)\n",
- " z = tf.multiply(y, y)\n",
- "\n",
- "# Use the same tape to compute the derivative of z with respect to the\n",
- "# intermediate value y.\n",
- "dz_dy = t.gradient(z, y)\n",
- "assert dz_dy.numpy() == 8.0\n",
- "\n",
- "# Derivative of z with respect to the original input tensor x\n",
- "dz_dx = t.gradient(z, x)\n",
- "for i in [0, 1]:\n",
- " for j in [0, 1]:\n",
- " assert dz_dx[i][j].numpy() == 8.0"
+ "This file has moved."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
- "id": "DK05KXrAAld3"
- },
- "source": [
- "### Higher-order gradients\n",
- "\n",
- "Operations inside of the `GradientTape` context manager are recorded for automatic differentiation. If gradients are computed in that context, then the gradient computation is recorded as well. As a result, the exact same API works for higher-order gradients as well. For example:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "colab_type": "code",
- "id": "cPQgthZ7ugRJ"
- },
- "outputs": [],
- "source": [
- "# TODO(ashankar): Should we use the persistent tape here instead? Follow up on Tom and Alex's discussion\n",
- "\n",
- "x = tf.constant(1.0) # Convert the Python 1.0 to a Tensor object\n",
- "\n",
- "with tf.GradientTape() as t:\n",
- " with tf.GradientTape() as t2:\n",
- " t2.watch(x)\n",
- " y = x * x * x\n",
- " # Compute the gradient inside the 't' context manager\n",
- " # which means the gradient computation is differentiable as well.\n",
- " dy_dx = t2.gradient(y, x)\n",
- "d2y_dx2 = t.gradient(dy_dx, x)\n",
- "\n",
- "assert dy_dx.numpy() == 3.0\n",
- "assert d2y_dx2.numpy() == 6.0"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "colab_type": "text",
- "id": "4U1KKzUpNl58"
+ "id": "idv0bPeCp325"
},
"source": [
- "## Next Steps\n",
- "\n",
- "In this tutorial we covered gradient computation in TensorFlow. With that we have enough of the primitives required to build an train neural networks, which we will cover in the [next tutorial](https://github.com/tensorflow/models/tree/master/official/contrib/eager/python/examples/notebooks/3_neural_networks.ipynb)."
+ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/eager/automatic_differentiation.ipynb\"\u003e\n",
+ " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
+ "\u003c/td\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/site/en/tutorials/eager/automatic_differentiation.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
- "default_view": {},
"name": "automatic_differentiation.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true,
- "version": "0.3.2",
- "views": {}
+ "version": "0.3.2"
},
"kernelspec": {
"display_name": "Python 3",
diff --git a/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb b/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb
index a0bbbb6123..d89774c45e 100644
--- a/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb
+++ b/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb
@@ -1,46 +1,25 @@
{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
- "colab": {
- "name": "custom_layers.ipynb",
- "version": "0.3.2",
- "views": {},
- "default_view": {},
- "provenance": [],
- "private_outputs": true,
- "collapsed_sections": [],
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
- }
- },
"cells": [
{
+ "cell_type": "markdown",
"metadata": {
- "id": "tDnwEv8FtJm7",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "tDnwEv8FtJm7"
},
- "cell_type": "markdown",
"source": [
"##### Copyright 2018 The TensorFlow Authors."
]
},
{
+ "cell_type": "code",
+ "execution_count": 0,
"metadata": {
- "id": "JlknJBWQtKkI",
+ "cellView": "form",
+ "colab": {},
"colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "form"
+ "id": "JlknJBWQtKkI"
},
- "cell_type": "code",
+ "outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
@@ -53,347 +32,57 @@
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "60RdWsg1tETW",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "# Custom layers"
- ]
- },
- {
- "metadata": {
- "id": "BcJg7Enms86w",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "<table class=\"tfo-notebook-buttons\" align=\"left\"><td>\n",
- "<a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb\">\n",
- " <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
- "</td><td>\n",
- "<a target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/custom_layers.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a></td></table>"
- ]
- },
- {
- "metadata": {
- "id": "UEu3q4jmpKVT",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "We recommend using `tf.keras` as a high-level API for building neural networks. That said, most TensorFlow APIs are usable with eager execution.\n"
]
},
{
- "metadata": {
- "id": "pwX7Fii1rwsJ",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "import tensorflow as tf\n",
- "tfe = tf.contrib.eager\n",
- "\n",
- "tf.enable_eager_execution()"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "zSFfVVjkrrsI",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "## Layers: common sets of useful operations\n",
- "\n",
- "Most of the time when writing code for machine learning models you want to operate at a higher level of abstraction than individual operations and manipulation of individual variables.\n",
- "\n",
- "Many machine learning models are expressible as the composition and stacking of relatively simple layers, and TensorFlow provides both a set of many common layers as a well as easy ways for you to write your own application-specific layers either from scratch or as the composition of existing layers.\n",
- "\n",
- "TensorFlow includes the full [Keras](https://keras.io) API in the tf.keras package, and the Keras layers are very useful when building your own models.\n"
- ]
- },
- {
"metadata": {
- "id": "8PyXlPl-4TzQ",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
+ "colab_type": "text",
+ "id": "60RdWsg1tETW"
},
- "cell_type": "code",
- "source": [
- "# In the tf.keras.layers package, layers are objects. To construct a layer,\n",
- "# simply construct the object. Most layers take as a first argument the number\n",
- "# of output dimensions / channels.\n",
- "layer = tf.keras.layers.Dense(100)\n",
- "# The number of input dimensions is often unnecessary, as it can be inferred\n",
- "# the first time the layer is used, but it can be provided if you want to \n",
- "# specify it manually, which is useful in some complex models.\n",
- "layer = tf.keras.layers.Dense(10, input_shape=(None, 5))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "Fn69xxPO5Psr",
- "colab_type": "text"
- },
- "cell_type": "markdown",
"source": [
- "The full list of pre-existing layers can be seen in [the documentation](https://www.tensorflow.org/api_docs/python/tf/keras/layers). It includes Dense (a fully-connected layer),\n",
- "Conv2D, LSTM, BatchNormalization, Dropout, and many others."
+ "# Custom layers"
]
},
{
- "metadata": {
- "id": "E3XKNknP5Mhb",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "# To use a layer, simply call it.\n",
- "layer(tf.zeros([10, 5]))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "Wt_Nsv-L5t2s",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "# Layers have many useful methods. For example, you can inspect all variables\n",
- "# in a layer by calling layer.variables. In this case a fully-connected layer\n",
- "# will have variables for weights and biases.\n",
- "layer.variables"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "6ilvKjz8_4MQ",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "# The variables are also accessible through nice accessors\n",
- "layer.kernel, layer.bias"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "O0kDbE54-5VS",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "## Implementing custom layers\n",
- "The best way to implement your own layer is extending the tf.keras.Layer class and implementing:\n",
- " * `__init__` , where you can do all input-independent initialization\n",
- " * `build`, where you know the shapes of the input tensors and can do the rest of the initialization\n",
- " * `call`, where you do the forward computation\n",
- "\n",
- "Note that you don't have to wait until `build` is called to create your variables, you can also create them in `__init__`. However, the advantage of creating them in `build` is that it enables late variable creation based on the shape of the inputs the layer will operate on. On the other hand, creating variables in `__init__` would mean that shapes required to create the variables will need to be explicitly specified."
- ]
- },
- {
- "metadata": {
- "id": "5Byl3n1k5kIy",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "class MyDenseLayer(tf.keras.layers.Layer):\n",
- " def __init__(self, num_outputs):\n",
- " super(MyDenseLayer, self).__init__()\n",
- " self.num_outputs = num_outputs\n",
- " \n",
- " def build(self, input_shape):\n",
- " self.kernel = self.add_variable(\"kernel\", \n",
- " shape=[input_shape[-1].value, \n",
- " self.num_outputs])\n",
- " \n",
- " def call(self, input):\n",
- " return tf.matmul(input, self.kernel)\n",
- " \n",
- "layer = MyDenseLayer(10)\n",
- "print(layer(tf.zeros([10, 5])))\n",
- "print(layer.variables)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
"metadata": {
- "id": "tk8E2vY0-z4Z",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "9sFn_RV_8zM-"
},
- "cell_type": "markdown",
"source": [
- "Note that you don't have to wait until `build` is called to create your variables, you can also create them in `__init__`.\n",
- "\n",
- "Overall code is easier to read and maintain if it uses standard layers whenever possible, as other readers will be familiar with the behavior of standard layers. If you want to use a layer which is not present in tf.keras.layers or tf.contrib.layers, consider filing a [github issue](http://github.com/tensorflow/tensorflow/issues/new) or, even better, sending us a pull request!"
+ "This file has moved."
]
},
{
- "metadata": {
- "id": "Qhg4KlbKrs3G",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "## Models: composing layers\n",
- "\n",
- "Many interesting layer-like things in machine learning models are implemented by composing existing layers. For example, each residual block in a resnet is a composition of convolutions, batch normalizations, and a shortcut.\n",
- "\n",
- "The main class used when creating a layer-like thing which contains other layers is tf.keras.Model. Implementing one is done by inheriting from tf.keras.Model."
- ]
- },
- {
- "metadata": {
- "id": "N30DTXiRASlb",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "class ResnetIdentityBlock(tf.keras.Model):\n",
- " def __init__(self, kernel_size, filters):\n",
- " super(ResnetIdentityBlock, self).__init__(name='')\n",
- " filters1, filters2, filters3 = filters\n",
- "\n",
- " self.conv2a = tf.keras.layers.Conv2D(filters1, (1, 1))\n",
- " self.bn2a = tf.keras.layers.BatchNormalization()\n",
- "\n",
- " self.conv2b = tf.keras.layers.Conv2D(filters2, kernel_size, padding='same')\n",
- " self.bn2b = tf.keras.layers.BatchNormalization()\n",
- "\n",
- " self.conv2c = tf.keras.layers.Conv2D(filters3, (1, 1))\n",
- " self.bn2c = tf.keras.layers.BatchNormalization()\n",
- "\n",
- " def call(self, input_tensor, training=False):\n",
- " x = self.conv2a(input_tensor)\n",
- " x = self.bn2a(x, training=training)\n",
- " x = tf.nn.relu(x)\n",
- "\n",
- " x = self.conv2b(x)\n",
- " x = self.bn2b(x, training=training)\n",
- " x = tf.nn.relu(x)\n",
- "\n",
- " x = self.conv2c(x)\n",
- " x = self.bn2c(x, training=training)\n",
- "\n",
- " x += input_tensor\n",
- " return tf.nn.relu(x)\n",
- "\n",
- " \n",
- "block = ResnetIdentityBlock(1, [1, 2, 3])\n",
- "print(block(tf.zeros([1, 2, 3, 3])))\n",
- "print([x.name for x in block.variables])"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
"metadata": {
- "id": "wYfucVw65PMj",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "BcJg7Enms86w"
},
- "cell_type": "markdown",
"source": [
- "Much of the time, however, models which compose many layers simply call one layer after the other. This can be done in very little code using tf.keras.Sequential"
+ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/eager/custom_layers.ipynb\"\u003e\n",
+ " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
+ "\u003c/td\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/tutorials/eager/custom_layers.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e"
]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "collapsed_sections": [],
+ "name": "custom_layers.ipynb",
+ "private_outputs": true,
+ "provenance": [],
+ "toc_visible": true,
+ "version": "0.3.2"
},
- {
- "metadata": {
- "id": "L9frk7Ur4uvJ",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- " my_seq = tf.keras.Sequential([tf.keras.layers.Conv2D(1, (1, 1)),\n",
- " tf.keras.layers.BatchNormalization(),\n",
- " tf.keras.layers.Conv2D(2, 1, \n",
- " padding='same'),\n",
- " tf.keras.layers.BatchNormalization(),\n",
- " tf.keras.layers.Conv2D(3, (1, 1)),\n",
- " tf.keras.layers.BatchNormalization()])\n",
- "my_seq(tf.zeros([1, 2, 3, 3]))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "c5YwYcnuK-wc",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "# Next steps\n",
- "\n",
- "Now you can go back to the previous notebook and adapt the linear regression example to use layers and models to be better structured."
- ]
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
}
- ]
-} \ No newline at end of file
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb b/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb
index 5f1b48fa0d..86dca0b423 100644
--- a/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb
+++ b/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb
@@ -1,46 +1,25 @@
{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
- "colab": {
- "name": "Custom training: basics",
- "version": "0.3.2",
- "views": {},
- "default_view": {},
- "provenance": [],
- "private_outputs": true,
- "collapsed_sections": [],
- "toc_visible": true
- },
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3"
- }
- },
"cells": [
{
+ "cell_type": "markdown",
"metadata": {
- "id": "5rmpybwysXGV",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "5rmpybwysXGV"
},
- "cell_type": "markdown",
"source": [
"##### Copyright 2018 The TensorFlow Authors."
]
},
{
+ "cell_type": "code",
+ "execution_count": 0,
"metadata": {
- "id": "m8y3rGtQsYP2",
+ "cellView": "form",
+ "colab": {},
"colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "form"
+ "id": "m8y3rGtQsYP2"
},
- "cell_type": "code",
+ "outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
@@ -53,425 +32,57 @@
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "hrXv0rU9sIma",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "# Custom training: basics"
- ]
- },
- {
- "metadata": {
- "id": "7S0BwJ_8sLu7",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "<table class=\"tfo-notebook-buttons\" align=\"left\"><td>\n",
- "<a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb\">\n",
- " <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
- "</td><td>\n",
- "<a target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a></td></table>"
- ]
- },
- {
- "metadata": {
- "id": "k2o3TTG4TFpt",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "In the previous tutorial we covered the TensorFlow APIs for automatic differentiation, a basic building block for machine learning.\n",
- "In this tutorial we will use the TensorFlow primitives introduced in the prior tutorials to do some simple machine learning.\n",
- "\n",
- "TensorFlow also includes a higher-level neural networks API (`tf.keras`) which provides useful abstractions to reduce boilerplate. We strongly recommend those higher level APIs for people working with neural networks. However, in this short tutorial we cover neural network training from first principles to establish a strong foundation."
- ]
- },
- {
- "metadata": {
- "id": "3LXMVuV0VhDr",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## Setup"
- ]
- },
- {
- "metadata": {
- "id": "PJ64L90aVir3",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "import tensorflow as tf\n",
- "\n",
- "tf.enable_eager_execution()"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "eMAWbDJFVmMk",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## Variables\n",
- "\n",
- "Tensors in TensorFlow are immutable stateless objects. Machine learning models, however, need to have changing state: as your model trains, the same code to compute predictions should behave differently over time (hopefully with a lower loss!). To represent this state which needs to change over the course of your computation, you can choose to rely on the fact that Python is a stateful programming language:\n"
- ]
- },
- {
- "metadata": {
- "id": "VkJwtLS_Jbn8",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "# Using python state\n",
- "x = tf.zeros([10, 10])\n",
- "x += 2 # This is equivalent to x = x + 2, which does not mutate the original\n",
- " # value of x\n",
- "print(x)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "wfneTXy7JcUz",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "TensorFlow, however, has stateful operations built in, and these are often more pleasant to use than low-level Python representations of your state. To represent weights in a model, for example, it's often convenient and efficient to use TensorFlow variables.\n",
- "\n",
- "A Variable is an object which stores a value and, when used in a TensorFlow computation, will implicitly read from this stored value. There are operations (`tf.assign_sub`, `tf.scatter_update`, etc) which manipulate the value stored in a TensorFlow variable."
]
},
{
- "metadata": {
- "id": "itxmrMil6DQi",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "v = tf.Variable(1.0)\n",
- "assert v.numpy() == 1.0\n",
- "\n",
- "# Re-assign the value\n",
- "v.assign(3.0)\n",
- "assert v.numpy() == 3.0\n",
- "\n",
- "# Use `v` in a TensorFlow operation like tf.square() and reassign\n",
- "v.assign(tf.square(v))\n",
- "assert v.numpy() == 9.0"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "-paSaeq1JzwC",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "Computations using Variables are automatically traced when computing gradients. For Variables representing embeddings TensorFlow will do sparse updates by default, which are more computation and memory efficient.\n",
- "\n",
- "Using Variables is also a way to quickly let a reader of your code know that this piece of state is mutable."
- ]
- },
- {
"metadata": {
- "id": "BMiFcDzE7Qu3",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "hrXv0rU9sIma"
},
- "cell_type": "markdown",
"source": [
- "## Example: Fitting a linear model\n",
- "\n",
- "Let's now put the few concepts we have so far ---`Tensor`, `GradientTape`, `Variable` --- to build and train a simple model. This typically involves a few steps:\n",
- "\n",
- "1. Define the model.\n",
- "2. Define a loss function.\n",
- "3. Obtain training data.\n",
- "4. Run through the training data and use an \"optimizer\" to adjust the variables to fit the data.\n",
- "\n",
- "In this tutorial, we'll walk through a trivial example of a simple linear model: `f(x) = x * W + b`, which has two variables - `W` and `b`. Furthermore, we'll synthesize data such that a well trained model would have `W = 3.0` and `b = 2.0`."
- ]
- },
- {
- "metadata": {
- "id": "gFzH64Jn9PIm",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### Define the model\n",
- "\n",
- "Let's define a simple class to encapsulate the variables and the computation."
+ "# Custom training: basics"
]
},
{
- "metadata": {
- "id": "_WRu7Pze7wk8",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "class Model(object):\n",
- " def __init__(self):\n",
- " # Initialize variable to (5.0, 0.0)\n",
- " # In practice, these should be initialized to random values.\n",
- " self.W = tf.Variable(5.0)\n",
- " self.b = tf.Variable(0.0)\n",
- " \n",
- " def __call__(self, x):\n",
- " return self.W * x + self.b\n",
- " \n",
- "model = Model()\n",
- "\n",
- "assert model(3.0).numpy() == 15.0"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "xa6j_yXa-j79",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "### Define a loss function\n",
- "\n",
- "A loss function measures how well the output of a model for a given input matches the desired output. Let's use the standard L2 loss."
- ]
- },
- {
- "metadata": {
- "id": "Y0ysUFGY924U",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "def loss(predicted_y, desired_y):\n",
- " return tf.reduce_mean(tf.square(predicted_y - desired_y))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
"metadata": {
- "id": "qutT_fkl_CBc",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "IGPZTmwn9IT4"
},
- "cell_type": "markdown",
"source": [
- "### Obtain training data\n",
- "\n",
- "Let's synthesize the training data with some noise."
+ "This file has moved."
]
},
{
- "metadata": {
- "id": "gxPTb-kt_N5m",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "TRUE_W = 3.0\n",
- "TRUE_b = 2.0\n",
- "NUM_EXAMPLES = 1000\n",
- "\n",
- "inputs = tf.random_normal(shape=[NUM_EXAMPLES])\n",
- "noise = tf.random_normal(shape=[NUM_EXAMPLES])\n",
- "outputs = inputs * TRUE_W + TRUE_b + noise"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "-50nq-wPBsAW",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "Before we train the model let's visualize where the model stands right now. We'll plot the model's predictions in red and the training data in blue."
- ]
- },
- {
"metadata": {
- "id": "_eb83LtrB4nt",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
+ "colab_type": "text",
+ "id": "7S0BwJ_8sLu7"
},
- "cell_type": "code",
"source": [
- "import matplotlib.pyplot as plt\n",
- "\n",
- "plt.scatter(inputs, outputs, c='b')\n",
- "plt.scatter(inputs, model(inputs), c='r')\n",
- "plt.show()\n",
- "\n",
- "print('Current loss: '),\n",
- "print(loss(model(inputs), outputs).numpy())"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "sSDP-yeq_4jE",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### Define a training loop\n",
- "\n",
- "We now have our network and our training data. Let's train it, i.e., use the training data to update the model's variables (`W` and `b`) so that the loss goes down using [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent). There are many variants of the gradient descent scheme that are captured in `tf.train.Optimizer` implementations. We'd highly recommend using those implementations, but in the spirit of building from first principles, in this particular example we will implement the basic math ourselves."
+ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/eager/custom_training.ipynb\"\u003e\n",
+ " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
+ "\u003c/td\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/tutorials/eager/custom_training.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e"
]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "collapsed_sections": [],
+ "name": "Custom training: basics",
+ "private_outputs": true,
+ "provenance": [],
+ "toc_visible": true,
+ "version": "0.3.2"
},
- {
- "metadata": {
- "id": "MBIACgdnA55X",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "def train(model, inputs, outputs, learning_rate):\n",
- " with tf.GradientTape() as t:\n",
- " current_loss = loss(model(inputs), outputs)\n",
- " dW, db = t.gradient(current_loss, [model.W, model.b])\n",
- " model.W.assign_sub(learning_rate * dW)\n",
- " model.b.assign_sub(learning_rate * db)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "RwWPaJryD2aN",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "Finally, let's repeatedly run through the training data and see how `W` and `b` evolve."
- ]
- },
- {
- "metadata": {
- "id": "XdfkR223D9dW",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "model = Model()\n",
- "\n",
- "# Collect the history of W-values and b-values to plot later\n",
- "Ws, bs = [], []\n",
- "epochs = range(10)\n",
- "for epoch in epochs:\n",
- " Ws.append(model.W.numpy())\n",
- " bs.append(model.b.numpy())\n",
- " current_loss = loss(model(inputs), outputs)\n",
- "\n",
- " train(model, inputs, outputs, learning_rate=0.1)\n",
- " print('Epoch %2d: W=%1.2f b=%1.2f, loss=%2.5f' %\n",
- " (epoch, Ws[-1], bs[-1], current_loss))\n",
- "\n",
- "# Let's plot it all\n",
- "plt.plot(epochs, Ws, 'r',\n",
- " epochs, bs, 'b')\n",
- "plt.plot([TRUE_W] * len(epochs), 'r--',\n",
- " [TRUE_b] * len(epochs), 'b--')\n",
- "plt.legend(['W', 'b', 'true W', 'true_b'])\n",
- "plt.show()\n",
- " "
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "vPnIVuaSJwWz",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## Next Steps\n",
- "\n",
- "In this tutorial we covered `Variable`s and built and trained a simple linear model using the TensorFlow primitives discussed so far.\n",
- "\n",
- "In theory, this is pretty much all you need to use TensorFlow for your machine learning research.\n",
- "In practice, particularly for neural networks, the higher level APIs like `tf.keras` will be much more convenient since it provides higher level building blocks (called \"layers\"), utilities to save and restore state, a suite of loss functions, a suite of optimization strategies etc. \n",
- "\n",
- "The [next tutorial](TODO) will cover these higher level APIs."
- ]
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
}
- ]
-} \ No newline at end of file
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb b/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb
index f1e13de5de..c6d1a56604 100644
--- a/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb
+++ b/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb
@@ -1,46 +1,25 @@
{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
- "colab": {
- "name": "eager_basics.ipynb",
- "version": "0.3.2",
- "views": {},
- "default_view": {},
- "provenance": [],
- "private_outputs": true,
- "collapsed_sections": [],
- "toc_visible": true
- },
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3"
- }
- },
"cells": [
{
+ "cell_type": "markdown",
"metadata": {
- "id": "iPpI7RaYoZuE",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "iPpI7RaYoZuE"
},
- "cell_type": "markdown",
"source": [
"##### Copyright 2018 The TensorFlow Authors."
]
},
{
+ "cell_type": "code",
+ "execution_count": 0,
"metadata": {
- "id": "hro2InpHobKk",
+ "cellView": "form",
+ "colab": {},
"colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "form"
+ "id": "hro2InpHobKk"
},
- "cell_type": "code",
+ "outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
@@ -53,439 +32,47 @@
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "U9i2Dsh-ziXr",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "# Eager execution basics"
- ]
- },
- {
- "metadata": {
- "id": "Hndw-YcxoOJK",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "<table class=\"tfo-notebook-buttons\" align=\"left\"><td>\n",
- "<a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb\">\n",
- " <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
- "</td><td>\n",
- "<a target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a></td></table>"
- ]
- },
- {
- "metadata": {
- "id": "6sILUVbHoSgH",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "This is an introductory tutorial for using TensorFlow. It will cover:\n",
- "\n",
- "* Importing required packages\n",
- "* Creating and using Tensors\n",
- "* Using GPU acceleration\n",
- "* Datasets"
- ]
- },
- {
- "metadata": {
- "id": "z1JcS5iBXMRO",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## Import TensorFlow\n",
- "\n",
- "To get started, import the `tensorflow` module and enable eager execution.\n",
- "Eager execution enables a more interactive frontend to TensorFlow, the details of which we will discuss much later."
- ]
- },
- {
- "metadata": {
- "id": "RlIWhyeLoYnG",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "code"
- },
- "cell_type": "code",
- "source": [
- "import tensorflow as tf\n",
- "\n",
- "tf.enable_eager_execution()"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "H9UySOPLXdaw",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## Tensors\n",
- "\n",
- "A Tensor is a multi-dimensional array. Similar to NumPy `ndarray` objects, `Tensor` objects have a data type and a shape. Additionally, Tensors can reside in accelerator (like GPU) memory. TensorFlow offers a rich library of operations ([tf.add](https://www.tensorflow.org/api_docs/python/tf/add), [tf.matmul](https://www.tensorflow.org/api_docs/python/tf/matmul), [tf.linalg.inv](https://www.tensorflow.org/api_docs/python/tf/linalg/inv) etc.) that consume and produce Tensors. These operations automatically convert native Python types. For example:\n"
- ]
- },
- {
- "metadata": {
- "id": "ngUe237Wt48W",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "code"
- },
- "cell_type": "code",
- "source": [
- "print(tf.add(1, 2))\n",
- "print(tf.add([1, 2], [3, 4]))\n",
- "print(tf.square(5))\n",
- "print(tf.reduce_sum([1, 2, 3]))\n",
- "print(tf.encode_base64(\"hello world\"))\n",
- "\n",
- "# Operator overloading is also supported\n",
- "print(tf.square(2) + tf.square(3))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "IDY4WsYRhP81",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "Each Tensor has a shape and a datatype"
- ]
- },
- {
- "metadata": {
- "id": "srYWH1MdJNG7",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "x = tf.matmul([[1]], [[2, 3]])\n",
- "print(x.shape)\n",
- "print(x.dtype)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "eBPw8e8vrsom",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "The most obvious differences between NumPy arrays and TensorFlow Tensors are:\n",
- "\n",
- "1. Tensors can be backed by accelerator memory (like GPU, TPU).\n",
- "2. Tensors are immutable."
- ]
- },
- {
- "metadata": {
- "id": "Dwi1tdW3JBw6",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### NumPy Compatibility\n",
- "\n",
- "Conversion between TensorFlow Tensors and NumPy ndarrays is quite simple as:\n",
- "* TensorFlow operations automatically convert NumPy ndarrays to Tensors.\n",
- "* NumPy operations automatically convert Tensors to NumPy ndarrays.\n",
- "\n",
- "Tensors can be explicitly converted to NumPy ndarrays by invoking the `.numpy()` method on them.\n",
- "These conversions are typically cheap as the array and Tensor share the underlying memory representation if possible. However, sharing the underlying representation isn't always possible since the Tensor may be hosted in GPU memory while NumPy arrays are always backed by host memory, and the conversion will thus involve a copy from GPU to host memory."
- ]
- },
- {
- "metadata": {
- "id": "lCUWzso6mbqR",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "import numpy as np\n",
- "\n",
- "ndarray = np.ones([3, 3])\n",
- "\n",
- "print(\"TensorFlow operations convert numpy arrays to Tensors automatically\")\n",
- "tensor = tf.multiply(ndarray, 42)\n",
- "print(tensor)\n",
- "\n",
- "\n",
- "print(\"And NumPy operations convert Tensors to numpy arrays automatically\")\n",
- "print(np.add(tensor, 1))\n",
- "\n",
- "print(\"The .numpy() method explicitly converts a Tensor to a numpy array\")\n",
- "print(tensor.numpy())"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "PBNP8yTRfu_X",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "## GPU acceleration\n",
- "\n",
- "Many TensorFlow operations can be accelerated by using the GPU for computation. Without any annotations, TensorFlow automatically decides whether to use the GPU or CPU for an operation (and copies the tensor between CPU and GPU memory if necessary). Tensors produced by an operation are typically backed by the memory of the device on which the operation executed. For example:"
- ]
- },
- {
- "metadata": {
- "id": "3Twf_Rw-gQFM",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- },
- "cellView": "code"
- },
- "cell_type": "code",
- "source": [
- "x = tf.random_uniform([3, 3])\n",
- "\n",
- "print(\"Is there a GPU available: \"),\n",
- "print(tf.test.is_gpu_available())\n",
- "\n",
- "print(\"Is the Tensor on GPU #0: \"),\n",
- "print(x.device.endswith('GPU:0'))"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "vpgYzgVXW2Ud",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### Device Names\n",
- "\n",
- "The `Tensor.device` property provides a fully qualified string name of the device hosting the contents of the Tensor. This name encodes a bunch of details, such as an identifier of the network address of the host on which this program is executing and the device within that host. This is required for distributed execution of TensorFlow programs, but we'll skip that for now. The string will end with `GPU:<N>` if the tensor is placed on the `N`-th tensor on the host."
]
},
{
- "metadata": {
- "id": "ZWZQCimzuqyP",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "\n",
- "\n",
- "### Explicit Device Placement\n",
- "\n",
- "The term \"placement\" in TensorFlow refers to how individual operations are assigned (placed on) a device for execution. As mentioned above, when there is no explicit guidance provided, TensorFlow automatically decides which device to execute an operation, and copies Tensors to that device if needed. However, TensorFlow operations can be explicitly placed on specific devices using the `tf.device` context manager. For example:"
- ]
- },
- {
- "metadata": {
- "id": "RjkNZTuauy-Q",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "def time_matmul(x):\n",
- " %timeit tf.matmul(x, x)\n",
- "\n",
- "# Force execution on CPU\n",
- "print(\"On CPU:\")\n",
- "with tf.device(\"CPU:0\"):\n",
- " x = tf.random_uniform([1000, 1000])\n",
- " assert x.device.endswith(\"CPU:0\")\n",
- " time_matmul(x)\n",
- "\n",
- "# Force execution on GPU #0 if available\n",
- "if tf.test.is_gpu_available():\n",
- " with tf.device(\"GPU:0\"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.\n",
- " x = tf.random_uniform([1000, 1000])\n",
- " assert x.device.endswith(\"GPU:0\")\n",
- " time_matmul(x)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
"metadata": {
- "id": "o1K4dlhhHtQj",
- "colab_type": "text"
+ "colab_type": "text",
+ "id": "U9i2Dsh-ziXr"
},
- "cell_type": "markdown",
"source": [
- "## Datasets\n",
- "\n",
- "This section demonstrates the use of the [`tf.data.Dataset` API](https://www.tensorflow.org/guide/datasets) to build pipelines to feed data to your model. It covers:\n",
- "\n",
- "* Creating a `Dataset`.\n",
- "* Iteration over a `Dataset` with eager execution enabled.\n",
- "\n",
- "We recommend using the `Dataset`s API for building performant, complex input pipelines from simple, re-usable pieces that will feed your model's training or evaluation loops.\n",
- "\n",
- "If you're familiar with TensorFlow graphs, the API for constructing the `Dataset` object remains exactly the same when eager execution is enabled, but the process of iterating over elements of the dataset is slightly simpler.\n",
- "You can use Python iteration over the `tf.data.Dataset` object and do not need to explicitly create an `tf.data.Iterator` object.\n",
- "As a result, the discussion on iterators in the [TensorFlow Guide](https://www.tensorflow.org/guide/datasets) is not relevant when eager execution is enabled."
- ]
- },
- {
- "metadata": {
- "id": "zI0fmOynH-Ne",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### Create a source `Dataset`\n",
- "\n",
- "Create a _source_ dataset using one of the factory functions like [`Dataset.from_tensors`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensors), [`Dataset.from_tensor_slices`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensor_slices) or using objects that read from files like [`TextLineDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TextLineDataset) or [`TFRecordDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TFRecordDataset). See the [TensorFlow Guide](https://www.tensorflow.org/guide/datasets#reading_input_data) for more information."
+ "# Eager execution basics"
]
},
{
- "metadata": {
- "id": "F04fVOHQIBiG",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6])\n",
- "\n",
- "# Create a CSV file\n",
- "import tempfile\n",
- "_, filename = tempfile.mkstemp()\n",
- "\n",
- "with open(filename, 'w') as f:\n",
- " f.write(\"\"\"Line 1\n",
- "Line 2\n",
- "Line 3\n",
- " \"\"\")\n",
- "\n",
- "ds_file = tf.data.TextLineDataset(filename)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "vbxIhC-5IPdf",
- "colab_type": "text"
- },
"cell_type": "markdown",
- "source": [
- "### Apply transformations\n",
- "\n",
- "Use the transformations functions like [`map`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map), [`batch`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#batch), [`shuffle`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle) etc. to apply transformations to the records of the dataset. See the [API documentation for `tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for details."
- ]
- },
- {
"metadata": {
- "id": "uXSDZWE-ISsd",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
+ "colab_type": "text",
+ "id": "Hndw-YcxoOJK"
},
- "cell_type": "code",
"source": [
- "ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2)\n",
- "\n",
- "ds_file = ds_file.batch(2)"
- ],
- "execution_count": 0,
- "outputs": []
- },
- {
- "metadata": {
- "id": "A8X1GNfoIZKJ",
- "colab_type": "text"
- },
- "cell_type": "markdown",
- "source": [
- "### Iterate\n",
- "\n",
- "When eager execution is enabled `Dataset` objects support iteration.\n",
- "If you're familiar with the use of `Dataset`s in TensorFlow graphs, note that there is no need for calls to `Dataset.make_one_shot_iterator()` or `get_next()` calls."
+ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/eager/eager_basics.ipynb\"\u003e\n",
+ " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
+ "\u003c/td\u003e\u003ctd\u003e\n",
+ "\u003ca target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/tutorials/eager/eager_basics.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e"
]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "collapsed_sections": [],
+ "name": "eager_basics.ipynb",
+ "private_outputs": true,
+ "provenance": [],
+ "toc_visible": true,
+ "version": "0.3.2"
},
- {
- "metadata": {
- "id": "ws-WKRk5Ic6-",
- "colab_type": "code",
- "colab": {
- "autoexec": {
- "startup": false,
- "wait_interval": 0
- }
- }
- },
- "cell_type": "code",
- "source": [
- "print('Elements of ds_tensors:')\n",
- "for x in ds_tensors:\n",
- " print(x)\n",
- "\n",
- "print('\\nElements in ds_file:')\n",
- "for x in ds_file:\n",
- " print(x)"
- ],
- "execution_count": 0,
- "outputs": []
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
}
- ]
-} \ No newline at end of file
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}