"llama/binding/binding.cpp" did not exist on "172274b8091e8925fc53d19bd8a58171dfec00be"
Commit 71b64ccb authored by Mark Daoust's avatar Mark Daoust
Browse files

Use meaningful function names

parent b07b494e
......@@ -3,14 +3,15 @@
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "autograph.ipynb",
"name": "Copy of Copy of Copy of autograph.ipynb",
"version": "0.3.2",
"provenance": [],
"private_outputs": true,
"collapsed_sections": [
"Jxv6goXm7oGF"
],
"toc_visible": true
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
......@@ -18,6 +19,16 @@
}
},
"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": {
"id": "Jxv6goXm7oGF",
......@@ -199,7 +210,7 @@
},
"cell_type": "code",
"source": [
"def g(x):\n",
"def square_if_positive(x):\n",
" if x > 0:\n",
" x = x * x\n",
" else:\n",
......@@ -227,7 +238,7 @@
},
"cell_type": "code",
"source": [
"print(autograph.to_code(g))"
"print(autograph.to_code(square_if_positive))"
],
"execution_count": 0,
"outputs": []
......@@ -250,7 +261,8 @@
},
"cell_type": "code",
"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,
"outputs": []
......@@ -273,13 +285,13 @@
},
"cell_type": "code",
"source": [
"tf_g = autograph.to_graph(g)\n",
"tf_square_if_positive = autograph.to_graph(square_if_positive)\n",
"\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_out1 = tf_g(tf.constant( 9.0))\n",
" g_out2 = tf_g(tf.constant(-9.0))\n",
" g_out1 = tf_square_if_positive(tf.constant( 9.0))\n",
" g_out2 = tf_square_if_positive(tf.constant(-9.0))\n",
" with tf.Session() as sess:\n",
" print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
],
......@@ -305,21 +317,21 @@
"cell_type": "code",
"source": [
"# Continue in a loop\n",
"def f(l):\n",
"def sum_even(items):\n",
" s = 0\n",
" for c in l:\n",
" for c in items:\n",
" if c % 2 > 0:\n",
" continue\n",
" s += c\n",
" return s\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",
"tf_f = autograph.to_graph(f)\n",
"tf_sum_even = autograph.to_graph(sum_even)\n",
"\n",
"with tf.Graph().as_default(): \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,
"outputs": []
......@@ -332,7 +344,7 @@
},
"cell_type": "code",
"source": [
"print(autograph.to_code(f))"
"print(autograph.to_code(sum_even))"
],
"execution_count": 0,
"outputs": []
......@@ -360,15 +372,13 @@
"@autograph.convert()\n",
"def fizzbuzz(num):\n",
" if num % 3 == 0 and num % 5 == 0:\n",
" print('FizzBuzz')\n",
" return 'FizzBuzz'\n",
" elif num % 3 == 0:\n",
" print('Fizz')\n",
" return 'Fizz'\n",
" elif num % 5 == 0:\n",
" print('Buzz')\n",
" return 'Buzz'\n",
" else:\n",
" print(num)\n",
" return num\n",
"\n",
" return tf.as_string(num)\n",
"\n",
"with tf.Graph().as_default():\n",
" # The result works like a regular op: takes tensors in, returns tensors.\n",
......@@ -377,7 +387,7 @@
" result = fizzbuzz(num)\n",
" with tf.Session() as sess:\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,
"outputs": []
......@@ -415,14 +425,14 @@
"cell_type": "code",
"source": [
"@autograph.convert()\n",
"def f(x):\n",
" assert x != 0, 'Do not pass zero!'\n",
" return x * x\n",
"def inverse(x):\n",
" assert x != 0.0, 'Do not pass zero!'\n",
" return 1.0/x\n",
"\n",
"with tf.Graph().as_default(): \n",
" with tf.Session():\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",
" print('Got error message:\\n %s' % e.message)"
],
......@@ -443,30 +453,30 @@
},
{
"metadata": {
"id": "ySTsuxnqCTQi",
"id": "ehBac9rUR6nh",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"@autograph.convert()\n",
"def f(n):\n",
" if n >= 0:\n",
" while n < 5:\n",
" n += 1\n",
" print(n)\n",
"def count(n):\n",
" i=0\n",
" while i < n:\n",
" print(i)\n",
" i += 1\n",
" return n\n",
" \n",
"with tf.Graph().as_default():\n",
" with tf.Session():\n",
" f(tf.constant(0)).eval()"
" count(tf.constant(0)).eval()"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "NqF0GT-VCVFh",
"id": "mtpegD_YR6HK",
"colab_type": "text"
},
"cell_type": "markdown",
......@@ -485,10 +495,10 @@
"cell_type": "code",
"source": [
"@autograph.convert()\n",
"def f(n):\n",
"def arange(n):\n",
" z = []\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",
" for i in range(n):\n",
" z.append(i)\n",
......@@ -500,7 +510,7 @@
"\n",
"with tf.Graph().as_default(): \n",
" with tf.Session():\n",
" print(f(tf.constant(3)).eval())"
" print(arange(tf.constant(10)).eval())"
],
"execution_count": 0,
"outputs": []
......@@ -512,7 +522,7 @@
},
"cell_type": "markdown",
"source": [
"### Nested if statements"
"### Nested control flow"
]
},
{
......@@ -746,13 +756,13 @@
" # to convert these lists into their graph equivalent,\n",
" # we need to specify the element type of the lists.\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",
" autograph.utils.set_element_type(test_losses, tf.float32)\n",
" autograph.set_element_type(test_losses, tf.float32)\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",
" autograph.utils.set_element_type(test_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",
......
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