Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71b64cc

Browse files
committed
Use meaningful function names
1 parent b07b494 commit 71b64cc

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

samples/core/guide/autograph.ipynb

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@
33
"nbformat_minor": 0,
44
"metadata": {
55
"colab": {
6-
"name": "autograph.ipynb",
6+
"name": "Copy of Copy of Copy of autograph.ipynb",
77
"version": "0.3.2",
88
"provenance": [],
99
"private_outputs": true,
1010
"collapsed_sections": [
1111
"Jxv6goXm7oGF"
1212
],
13-
"toc_visible": true
13+
"toc_visible": true,
14+
"include_colab_link": true
1415
},
1516
"kernelspec": {
1617
"name": "python3",
1718
"display_name": "Python 3"
1819
}
1920
},
2021
"cells": [
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {
25+
"id": "view-in-github",
26+
"colab_type": "text"
27+
},
28+
"source": [
29+
"[View in Colaboratory](https://colab.research.google.com/github/MarkDaoust/models/blob/autograph/samples/core/guide/autograph.ipynb)"
30+
]
31+
},
2132
{
2233
"metadata": {
2334
"id": "Jxv6goXm7oGF",
@@ -199,7 +210,7 @@
199210
},
200211
"cell_type": "code",
201212
"source": [
202-
"def g(x):\n",
213+
"def square_if_positive(x):\n",
203214
" if x > 0:\n",
204215
" x = x * x\n",
205216
" else:\n",
@@ -227,7 +238,7 @@
227238
},
228239
"cell_type": "code",
229240
"source": [
230-
"print(autograph.to_code(g))"
241+
"print(autograph.to_code(square_if_positive))"
231242
],
232243
"execution_count": 0,
233244
"outputs": []
@@ -250,7 +261,8 @@
250261
},
251262
"cell_type": "code",
252263
"source": [
253-
"print('Eager results: %2.2f, %2.2f' % (g(tf.constant(9.0)), g(tf.constant(-9.0))))"
264+
"print('Eager results: %2.2f, %2.2f' % (square_if_positive(tf.constant(9.0)), \n",
265+
" square_if_positive(tf.constant(-9.0))))"
254266
],
255267
"execution_count": 0,
256268
"outputs": []
@@ -273,13 +285,13 @@
273285
},
274286
"cell_type": "code",
275287
"source": [
276-
"tf_g = autograph.to_graph(g)\n",
288+
"tf_square_if_positive = autograph.to_graph(square_if_positive)\n",
277289
"\n",
278290
"with tf.Graph().as_default(): \n",
279291
" # The result works like a regular op: takes tensors in, returns tensors.\n",
280292
" # You can inspect the graph using tf.get_default_graph().as_graph_def()\n",
281-
" g_out1 = tf_g(tf.constant( 9.0))\n",
282-
" g_out2 = tf_g(tf.constant(-9.0))\n",
293+
" g_out1 = tf_square_if_positive(tf.constant( 9.0))\n",
294+
" g_out2 = tf_square_if_positive(tf.constant(-9.0))\n",
283295
" with tf.Session() as sess:\n",
284296
" print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
285297
],
@@ -305,21 +317,21 @@
305317
"cell_type": "code",
306318
"source": [
307319
"# Continue in a loop\n",
308-
"def f(l):\n",
320+
"def sum_even(items):\n",
309321
" s = 0\n",
310-
" for c in l:\n",
322+
" for c in items:\n",
311323
" if c % 2 > 0:\n",
312324
" continue\n",
313325
" s += c\n",
314326
" return s\n",
315327
"\n",
316-
"print('Eager result: %d' % f(tf.constant([10,12,15,20])))\n",
328+
"print('Eager result: %d' % sum_even(tf.constant([10,12,15,20])))\n",
317329
"\n",
318-
"tf_f = autograph.to_graph(f)\n",
330+
"tf_sum_even = autograph.to_graph(sum_even)\n",
319331
"\n",
320332
"with tf.Graph().as_default(): \n",
321333
" with tf.Session():\n",
322-
" print('Graph result: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())"
334+
" print('Graph result: %d\\n\\n' % tf_sum_even(tf.constant([10,12,15,20])).eval())"
323335
],
324336
"execution_count": 0,
325337
"outputs": []
@@ -332,7 +344,7 @@
332344
},
333345
"cell_type": "code",
334346
"source": [
335-
"print(autograph.to_code(f))"
347+
"print(autograph.to_code(sum_even))"
336348
],
337349
"execution_count": 0,
338350
"outputs": []
@@ -360,15 +372,13 @@
360372
"@autograph.convert()\n",
361373
"def fizzbuzz(num):\n",
362374
" if num % 3 == 0 and num % 5 == 0:\n",
363-
" print('FizzBuzz')\n",
375+
" return 'FizzBuzz'\n",
364376
" elif num % 3 == 0:\n",
365-
" print('Fizz')\n",
377+
" return 'Fizz'\n",
366378
" elif num % 5 == 0:\n",
367-
" print('Buzz')\n",
379+
" return 'Buzz'\n",
368380
" else:\n",
369-
" print(num)\n",
370-
" return num\n",
371-
"\n",
381+
" return tf.as_string(num)\n",
372382
"\n",
373383
"with tf.Graph().as_default():\n",
374384
" # The result works like a regular op: takes tensors in, returns tensors.\n",
@@ -377,7 +387,7 @@
377387
" result = fizzbuzz(num)\n",
378388
" with tf.Session() as sess:\n",
379389
" for n in range(10,16):\n",
380-
" sess.run(result, feed_dict={num:n})"
390+
" print(sess.run(result, feed_dict={num:n}))"
381391
],
382392
"execution_count": 0,
383393
"outputs": []
@@ -415,14 +425,14 @@
415425
"cell_type": "code",
416426
"source": [
417427
"@autograph.convert()\n",
418-
"def f(x):\n",
419-
" assert x != 0, 'Do not pass zero!'\n",
420-
" return x * x\n",
428+
"def inverse(x):\n",
429+
" assert x != 0.0, 'Do not pass zero!'\n",
430+
" return 1.0/x\n",
421431
"\n",
422432
"with tf.Graph().as_default(): \n",
423433
" with tf.Session():\n",
424434
" try:\n",
425-
" print(f(tf.constant(0)).eval())\n",
435+
" print(inverse(tf.constant(0.0)).eval())\n",
426436
" except tf.errors.InvalidArgumentError as e:\n",
427437
" print('Got error message:\\n %s' % e.message)"
428438
],
@@ -443,30 +453,30 @@
443453
},
444454
{
445455
"metadata": {
446-
"id": "ySTsuxnqCTQi",
456+
"id": "ehBac9rUR6nh",
447457
"colab_type": "code",
448458
"colab": {}
449459
},
450460
"cell_type": "code",
451461
"source": [
452462
"@autograph.convert()\n",
453-
"def f(n):\n",
454-
" if n >= 0:\n",
455-
" while n < 5:\n",
456-
" n += 1\n",
457-
" print(n)\n",
463+
"def count(n):\n",
464+
" i=0\n",
465+
" while i < n:\n",
466+
" print(i)\n",
467+
" i += 1\n",
458468
" return n\n",
459469
" \n",
460470
"with tf.Graph().as_default():\n",
461471
" with tf.Session():\n",
462-
" f(tf.constant(0)).eval()"
472+
" count(tf.constant(0)).eval()"
463473
],
464474
"execution_count": 0,
465475
"outputs": []
466476
},
467477
{
468478
"metadata": {
469-
"id": "NqF0GT-VCVFh",
479+
"id": "mtpegD_YR6HK",
470480
"colab_type": "text"
471481
},
472482
"cell_type": "markdown",
@@ -485,10 +495,10 @@
485495
"cell_type": "code",
486496
"source": [
487497
"@autograph.convert()\n",
488-
"def f(n):\n",
498+
"def arange(n):\n",
489499
" z = []\n",
490500
" # We ask you to tell us the element dtype of the list\n",
491-
" autograph.utils.set_element_type(z, tf.int32)\n",
501+
" autograph.set_element_type(z, tf.int32)\n",
492502
" \n",
493503
" for i in range(n):\n",
494504
" z.append(i)\n",
@@ -500,7 +510,7 @@
500510
"\n",
501511
"with tf.Graph().as_default(): \n",
502512
" with tf.Session():\n",
503-
" print(f(tf.constant(3)).eval())"
513+
" print(arange(tf.constant(10)).eval())"
504514
],
505515
"execution_count": 0,
506516
"outputs": []
@@ -512,7 +522,7 @@
512522
},
513523
"cell_type": "markdown",
514524
"source": [
515-
"### Nested if statements"
525+
"### Nested control flow"
516526
]
517527
},
518528
{
@@ -746,13 +756,13 @@
746756
" # to convert these lists into their graph equivalent,\n",
747757
" # we need to specify the element type of the lists.\n",
748758
" train_losses = []\n",
749-
" autograph.utils.set_element_type(train_losses, tf.float32)\n",
759+
" autograph.set_element_type(train_losses, tf.float32)\n",
750760
" test_losses = []\n",
751-
" autograph.utils.set_element_type(test_losses, tf.float32)\n",
761+
" autograph.set_element_type(test_losses, tf.float32)\n",
752762
" train_accuracies = []\n",
753-
" autograph.utils.set_element_type(train_accuracies, tf.float32)\n",
763+
" autograph.set_element_type(train_accuracies, tf.float32)\n",
754764
" test_accuracies = []\n",
755-
" autograph.utils.set_element_type(test_accuracies, tf.float32)\n",
765+
" autograph.set_element_type(test_accuracies, tf.float32)\n",
756766
" \n",
757767
" # This entire training loop will be run in-graph.\n",
758768
" i = tf.constant(0)\n",

0 commit comments

Comments
 (0)