Commit 71b64ccb authored by Mark Daoust's avatar Mark Daoust
Browse files

Use meaningful function names

parent b07b494e
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
"nbformat_minor": 0, "nbformat_minor": 0,
"metadata": { "metadata": {
"colab": { "colab": {
"name": "autograph.ipynb", "name": "Copy of Copy of Copy of autograph.ipynb",
"version": "0.3.2", "version": "0.3.2",
"provenance": [], "provenance": [],
"private_outputs": true, "private_outputs": true,
"collapsed_sections": [ "collapsed_sections": [
"Jxv6goXm7oGF" "Jxv6goXm7oGF"
], ],
"toc_visible": true "toc_visible": true,
"include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
"name": "python3", "name": "python3",
...@@ -18,6 +19,16 @@ ...@@ -18,6 +19,16 @@
} }
}, },
"cells": [ "cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/MarkDaoust/models/blob/autograph/samples/core/guide/autograph.ipynb)"
]
},
{ {
"metadata": { "metadata": {
"id": "Jxv6goXm7oGF", "id": "Jxv6goXm7oGF",
...@@ -199,7 +210,7 @@ ...@@ -199,7 +210,7 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"def g(x):\n", "def square_if_positive(x):\n",
" if x > 0:\n", " if x > 0:\n",
" x = x * x\n", " x = x * x\n",
" else:\n", " else:\n",
...@@ -227,7 +238,7 @@ ...@@ -227,7 +238,7 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"print(autograph.to_code(g))" "print(autograph.to_code(square_if_positive))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -250,7 +261,8 @@ ...@@ -250,7 +261,8 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"print('Eager results: %2.2f, %2.2f' % (g(tf.constant(9.0)), g(tf.constant(-9.0))))" "print('Eager results: %2.2f, %2.2f' % (square_if_positive(tf.constant(9.0)), \n",
" square_if_positive(tf.constant(-9.0))))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -273,13 +285,13 @@ ...@@ -273,13 +285,13 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"tf_g = autograph.to_graph(g)\n", "tf_square_if_positive = autograph.to_graph(square_if_positive)\n",
"\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", " # 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", " # You can inspect the graph using tf.get_default_graph().as_graph_def()\n",
" g_out1 = tf_g(tf.constant( 9.0))\n", " g_out1 = tf_square_if_positive(tf.constant( 9.0))\n",
" g_out2 = tf_g(tf.constant(-9.0))\n", " g_out2 = tf_square_if_positive(tf.constant(-9.0))\n",
" with tf.Session() as sess:\n", " with tf.Session() as sess:\n",
" print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))" " print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
], ],
...@@ -305,21 +317,21 @@ ...@@ -305,21 +317,21 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Continue in a loop\n", "# Continue in a loop\n",
"def f(l):\n", "def sum_even(items):\n",
" s = 0\n", " s = 0\n",
" for c in l:\n", " for c in items:\n",
" if c % 2 > 0:\n", " if c % 2 > 0:\n",
" continue\n", " continue\n",
" s += c\n", " s += c\n",
" return s\n", " return s\n",
"\n", "\n",
"print('Eager result: %d' % f(tf.constant([10,12,15,20])))\n", "print('Eager result: %d' % sum_even(tf.constant([10,12,15,20])))\n",
"\n", "\n",
"tf_f = autograph.to_graph(f)\n", "tf_sum_even = autograph.to_graph(sum_even)\n",
"\n", "\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" print('Graph result: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())" " print('Graph result: %d\\n\\n' % tf_sum_even(tf.constant([10,12,15,20])).eval())"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -332,7 +344,7 @@ ...@@ -332,7 +344,7 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"print(autograph.to_code(f))" "print(autograph.to_code(sum_even))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -360,15 +372,13 @@ ...@@ -360,15 +372,13 @@
"@autograph.convert()\n", "@autograph.convert()\n",
"def fizzbuzz(num):\n", "def fizzbuzz(num):\n",
" if num % 3 == 0 and num % 5 == 0:\n", " if num % 3 == 0 and num % 5 == 0:\n",
" print('FizzBuzz')\n", " return 'FizzBuzz'\n",
" elif num % 3 == 0:\n", " elif num % 3 == 0:\n",
" print('Fizz')\n", " return 'Fizz'\n",
" elif num % 5 == 0:\n", " elif num % 5 == 0:\n",
" print('Buzz')\n", " return 'Buzz'\n",
" else:\n", " else:\n",
" print(num)\n", " return tf.as_string(num)\n",
" return num\n",
"\n",
"\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", " # The result works like a regular op: takes tensors in, returns tensors.\n",
...@@ -377,7 +387,7 @@ ...@@ -377,7 +387,7 @@
" result = fizzbuzz(num)\n", " result = fizzbuzz(num)\n",
" with tf.Session() as sess:\n", " with tf.Session() as sess:\n",
" for n in range(10,16):\n", " for n in range(10,16):\n",
" sess.run(result, feed_dict={num:n})" " print(sess.run(result, feed_dict={num:n}))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -415,14 +425,14 @@ ...@@ -415,14 +425,14 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"@autograph.convert()\n", "@autograph.convert()\n",
"def f(x):\n", "def inverse(x):\n",
" assert x != 0, 'Do not pass zero!'\n", " assert x != 0.0, 'Do not pass zero!'\n",
" return x * x\n", " return 1.0/x\n",
"\n", "\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" try:\n", " try:\n",
" print(f(tf.constant(0)).eval())\n", " print(inverse(tf.constant(0.0)).eval())\n",
" except tf.errors.InvalidArgumentError as e:\n", " except tf.errors.InvalidArgumentError as e:\n",
" print('Got error message:\\n %s' % e.message)" " print('Got error message:\\n %s' % e.message)"
], ],
...@@ -443,30 +453,30 @@ ...@@ -443,30 +453,30 @@
}, },
{ {
"metadata": { "metadata": {
"id": "ySTsuxnqCTQi", "id": "ehBac9rUR6nh",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"@autograph.convert()\n", "@autograph.convert()\n",
"def f(n):\n", "def count(n):\n",
" if n >= 0:\n", " i=0\n",
" while n < 5:\n", " while i < n:\n",
" n += 1\n", " print(i)\n",
" print(n)\n", " i += 1\n",
" return n\n", " return n\n",
" \n", " \n",
"with tf.Graph().as_default():\n", "with tf.Graph().as_default():\n",
" with tf.Session():\n", " with tf.Session():\n",
" f(tf.constant(0)).eval()" " count(tf.constant(0)).eval()"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{ {
"metadata": { "metadata": {
"id": "NqF0GT-VCVFh", "id": "mtpegD_YR6HK",
"colab_type": "text" "colab_type": "text"
}, },
"cell_type": "markdown", "cell_type": "markdown",
...@@ -485,10 +495,10 @@ ...@@ -485,10 +495,10 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"@autograph.convert()\n", "@autograph.convert()\n",
"def f(n):\n", "def arange(n):\n",
" z = []\n", " z = []\n",
" # We ask you to tell us the element dtype of the list\n", " # We ask you to tell us the element dtype of the list\n",
" autograph.utils.set_element_type(z, tf.int32)\n", " autograph.set_element_type(z, tf.int32)\n",
" \n", " \n",
" for i in range(n):\n", " for i in range(n):\n",
" z.append(i)\n", " z.append(i)\n",
...@@ -500,7 +510,7 @@ ...@@ -500,7 +510,7 @@
"\n", "\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" print(f(tf.constant(3)).eval())" " print(arange(tf.constant(10)).eval())"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -512,7 +522,7 @@ ...@@ -512,7 +522,7 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"### Nested if statements" "### Nested control flow"
] ]
}, },
{ {
...@@ -746,13 +756,13 @@ ...@@ -746,13 +756,13 @@
" # to convert these lists into their graph equivalent,\n", " # to convert these lists into their graph equivalent,\n",
" # we need to specify the element type of the lists.\n", " # we need to specify the element type of the lists.\n",
" train_losses = []\n", " train_losses = []\n",
" autograph.utils.set_element_type(train_losses, tf.float32)\n", " autograph.set_element_type(train_losses, tf.float32)\n",
" test_losses = []\n", " test_losses = []\n",
" autograph.utils.set_element_type(test_losses, tf.float32)\n", " autograph.set_element_type(test_losses, tf.float32)\n",
" train_accuracies = []\n", " train_accuracies = []\n",
" autograph.utils.set_element_type(train_accuracies, tf.float32)\n", " autograph.set_element_type(train_accuracies, tf.float32)\n",
" test_accuracies = []\n", " test_accuracies = []\n",
" autograph.utils.set_element_type(test_accuracies, tf.float32)\n", " autograph.set_element_type(test_accuracies, tf.float32)\n",
" \n", " \n",
" # This entire training loop will be run in-graph.\n", " # This entire training loop will be run in-graph.\n",
" i = tf.constant(0)\n", " i = tf.constant(0)\n",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment