diff options
Diffstat (limited to 'tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb')
-rw-r--r-- | tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb | 148 |
1 files changed, 92 insertions, 56 deletions
diff --git a/tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb b/tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb index e8f16b431d..e7dfb13e15 100644 --- a/tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb +++ b/tensorflow/contrib/autograph/examples/notebooks/workshop.ipynb @@ -11,6 +11,24 @@ } }, "colab_type": "code", + "id": "u3B7Uh50lozN" + }, + "outputs": [], + "source": [ + "!pip install -U -q tf-nightly" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "autoexec": { + "startup": false, + "wait_interval": 0 + } + }, + "colab_type": "code", "id": "qWUV0FYjDSKj" }, "outputs": [], @@ -62,7 +80,7 @@ "# ...into graph-building functions like this:\n", "def tf_g(x):\n", " with tf.name_scope('g'):\n", - " \n", + "\n", " def if_true():\n", " with tf.name_scope('if_true'):\n", " x_1, = x,\n", @@ -76,7 +94,7 @@ " return x_1,\n", "\n", " x = autograph_utils.run_cond(tf.greater(x, 0), if_true, if_false)\n", - " return x\n" + " return x" ] }, { @@ -101,14 +119,14 @@ "# Generate a graph-version of g and call it:\n", "tf_g = autograph.to_graph(g)\n", "\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " # The result works like a regular op: takes tensors in, returns tensors.\n", " # You can inspect the graph using tf.get_default_graph().as_graph_def()\n", " g_ops = tf_g(tf.constant(9.0))\n", " with tf.Session() as sess:\n", " print('Autograph value: %2.2f\\n' % sess.run(g_ops))\n", - " \n", - " \n", + "\n", + "\n", "# You can view, debug and tweak the generated code:\n", "print(autograph.to_code(g))" ] @@ -155,10 +173,10 @@ "print('Original value: %d' % f([10,12,15,20]))\n", "\n", "tf_f = autograph.to_graph(f)\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session():\n", " print('Graph value: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())\n", - " \n", + "\n", "print(autograph.to_code(f))" ] }, @@ -194,7 +212,7 @@ " return x * x\n", "\n", "tf_f = autograph.to_graph(f)\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session():\n", " try:\n", " print(tf_f(tf.constant(0)).eval())\n", @@ -233,7 +251,7 @@ " n += 1\n", " print(n)\n", " return n\n", - " \n", + "\n", "tf_f = autograph.to_graph(f)\n", "with tf.Graph().as_default():\n", " with tf.Session():\n", @@ -247,7 +265,7 @@ "id": "NqF0GT-VCVFh" }, "source": [ - "Appending to lists in loops also works (we create a `TensorArray` for you behind the scenes)" + "Appending to lists in loops also works (we create a tensor list ops behind the scenes)" ] }, { @@ -268,15 +286,15 @@ "def f(n):\n", " z = []\n", " # We ask you to tell us the element dtype of the list\n", - " z = autograph.utils.set_element_type(z, tf.int32)\n", + " autograph.set_element_type(z, tf.int32)\n", " for i in range(n):\n", " z.append(i)\n", " # when you're done with the list, stack it\n", " # (this is just like np.stack)\n", - " return autograph.stack(z) \n", + " return autograph.stack(z)\n", "\n", "tf_f = autograph.to_graph(f)\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session():\n", " print(tf_f(tf.constant(3)).eval())\n", "\n", @@ -327,7 +345,7 @@ "source": [ "tf_g = autograph.to_graph(fizzbuzz)\n", "\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " # The result works like a regular op: takes tensors in, returns tensors.\n", " # You can inspect the graph using tf.get_default_graph().as_graph_def()\n", " g_ops = tf_g(tf.constant(15))\n", @@ -384,7 +402,7 @@ " return x\n", "\n", "\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(square_log(tf.constant(4))))" ] @@ -396,7 +414,7 @@ "id": "_R-Q7BbxmkBF" }, "source": [ - "#### Now some exercises. Convert the TensorFlow code into AutoGraph'd Python code." + "#### Convert the TensorFlow code into Python code for AutoGraph" ] }, { @@ -439,8 +457,10 @@ "source": [ "@autograph.convert()\n", "def square_if_positive(x):\n", - " ... # \u003c\u003c\u003c fill it in!\n", - " \n", + "\n", + " pass # TODO: fill it in!\n", + "\n", + "\n", "with tf.Session() as sess:\n", " print(sess.run(square_if_positive(tf.constant(4))))" ] @@ -517,7 +537,7 @@ " x = tf.cond(tf.greater(x, 0), if_positive, lambda: x)\n", " return x\n", "\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(nearest_odd_square(tf.constant(4))))" ] @@ -539,8 +559,10 @@ "source": [ "@autograph.convert()\n", "def nearest_odd_square(x):\n", - " ... # \u003c\u003c\u003c fill it in!\n", - " \n", + "\n", + " pass # TODO: fill it in!\n", + "\n", + "\n", "with tf.Session() as sess:\n", " print(sess.run(nearest_odd_square(tf.constant(4))))" ] @@ -578,7 +600,7 @@ " x = x + 1\n", " return x\n", "\n", - "with tf.Graph().as_default(): \n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(nearest_odd_square(tf.constant(4))))" ] @@ -612,8 +634,8 @@ "def square_until_stop(x, y):\n", " x = tf.while_loop(lambda x: tf.less(x, y), lambda x: x * x, [x])\n", " return x\n", - " \n", - "with tf.Graph().as_default(): \n", + "\n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(square_until_stop(tf.constant(4), tf.constant(100))))" ] @@ -635,9 +657,11 @@ "source": [ "@autograph.convert()\n", "def square_until_stop(x, y):\n", - " ... # fill it in!\n", - " \n", - "with tf.Graph().as_default(): \n", + "\n", + " pass # TODO: fill it in!\n", + "\n", + "\n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(square_until_stop(tf.constant(4), tf.constant(100))))" ] @@ -672,8 +696,8 @@ " while x \u003c y:\n", " x = x * x\n", " return x\n", - " \n", - "with tf.Graph().as_default(): \n", + "\n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", " print(sess.run(square_until_stop(tf.constant(4), tf.constant(100))))" ] @@ -707,7 +731,7 @@ "def argwhere_cumsum(x, threshold):\n", " current_sum = 0.0\n", " idx = 0\n", - " \n", + "\n", " for i in range(len(x)):\n", " idx = i\n", " if current_sum \u003e= threshold:\n", @@ -715,10 +739,10 @@ " current_sum += x[i]\n", " return idx\n", "\n", - "N = 10\n", - "with tf.Graph().as_default(): \n", + "n = 10\n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", - " idx = argwhere_cumsum(tf.ones(N), tf.constant(float(N/2)))\n", + " idx = argwhere_cumsum(tf.ones(n), tf.constant(float(n / 2)))\n", " print(sess.run(idx))" ] }, @@ -739,12 +763,14 @@ "source": [ "@autograph.convert()\n", "def argwhere_cumsum(x, threshold):\n", - " ...\n", "\n", - "N = 10\n", - "with tf.Graph().as_default(): \n", + " pass # TODO: fill it in!\n", + "\n", + "\n", + "n = 10\n", + "with tf.Graph().as_default():\n", " with tf.Session() as sess:\n", - " idx = argwhere_cumsum(tf.ones(N), tf.constant(float(N/2)))\n", + " idx = argwhere_cumsum(tf.ones(n), tf.constant(float(n / 2)))\n", " print(sess.run(idx))" ] }, @@ -784,10 +810,10 @@ " current_sum += x[i]\n", " return idx\n", "\n", - "N = 10\n", + "n = 10\n", "with tf.Graph().as_default(): \n", " with tf.Session() as sess:\n", - " idx = argwhere_cumsum(tf.ones(N), tf.constant(float(N/2)))\n", + " idx = argwhere_cumsum(tf.ones(n), tf.constant(float(n / 2)))\n", " print(sess.run(idx))" ] }, @@ -980,43 +1006,50 @@ "def train(train_ds, test_ds, hp):\n", " m = mlp_model((28 * 28,))\n", " opt = tf.train.MomentumOptimizer(hp.learning_rate, 0.9)\n", - " \n", + "\n", " # We'd like to save our losses to a list. In order for AutoGraph\n", " # to convert these lists into their graph equivalent,\n", " # we need to specify the element type of the lists.\n", " train_losses = []\n", - " train_losses = autograph.utils.set_element_type(train_losses, tf.float32)\n", " test_losses = []\n", - " test_losses = autograph.utils.set_element_type(test_losses, tf.float32)\n", " train_accuracies = []\n", - " train_accuracies = autograph.utils.set_element_type(train_accuracies, tf.float32)\n", " test_accuracies = []\n", - " test_accuracies = autograph.utils.set_element_type(test_accuracies, tf.float32)\n", - " \n", + " autograph.set_element_type(train_losses, tf.float32)\n", + " autograph.set_element_type(test_losses, tf.float32)\n", + " autograph.set_element_type(train_accuracies, tf.float32)\n", + " autograph.set_element_type(test_accuracies, tf.float32)\n", + "\n", " # This entire training loop will be run in-graph.\n", " i = tf.constant(0)\n", " while i \u003c hp.max_steps:\n", " train_x, train_y = get_next_batch(train_ds)\n", " test_x, test_y = get_next_batch(test_ds)\n", - " # add get next\n", + "\n", " step_train_loss, step_train_accuracy = fit(m, train_x, train_y, opt)\n", " step_test_loss, step_test_accuracy = predict(m, test_x, test_y)\n", + "\n", " if i % (hp.max_steps // 10) == 0:\n", " print('Step', i, 'train loss:', step_train_loss, 'test loss:',\n", " step_test_loss, 'train accuracy:', step_train_accuracy,\n", " 'test accuracy:', step_test_accuracy)\n", + "\n", " train_losses.append(step_train_loss)\n", " test_losses.append(step_test_loss)\n", " train_accuracies.append(step_train_accuracy)\n", " test_accuracies.append(step_test_accuracy)\n", + "\n", " i += 1\n", - " \n", - " # We've recorded our loss values and accuracies \n", + "\n", + " # We've recorded our loss values and accuracies\n", " # to a list in a graph with AutoGraph's help.\n", - " # In order to return the values as a Tensor, \n", + " # In order to return the values as a Tensor,\n", " # we need to stack them before returning them.\n", - " return (autograph.stack(train_losses), autograph.stack(test_losses), autograph.stack(train_accuracies),\n", - " autograph.stack(test_accuracies))" + " return (\n", + " autograph.stack(train_losses),\n", + " autograph.stack(test_losses),\n", + " autograph.stack(train_accuracies),\n", + " autograph.stack(test_accuracies),\n", + " )" ] }, { @@ -1042,14 +1075,17 @@ " train_ds = setup_mnist_data(True, hp, 50)\n", " test_ds = setup_mnist_data(False, hp, 1000)\n", " tf_train = autograph.to_graph(train)\n", - " (train_losses, test_losses, train_accuracies,\n", - " test_accuracies) = tf_train(train_ds, test_ds, hp)\n", + " loss_tensors = tf_train(train_ds, test_ds, hp)\n", "\n", " with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", - " (train_losses, test_losses, train_accuracies,\n", - " test_accuracies) = sess.run([train_losses, test_losses, train_accuracies,\n", - " test_accuracies])\n", + " (\n", + " train_losses,\n", + " test_losses,\n", + " train_accuracies,\n", + " test_accuracies\n", + " ) = sess.run(loss_tensors)\n", + "\n", " plt.title('MNIST train/test losses')\n", " plt.plot(train_losses, label='train loss')\n", " plt.plot(test_losses, label='test loss')\n", |