|
3 | 3 | "nbformat_minor": 0,
|
4 | 4 | "metadata": {
|
5 | 5 | "colab": {
|
6 |
| - "name": "autograph.ipynb", |
| 6 | + "name": "Copy of Copy of Copy of autograph.ipynb", |
7 | 7 | "version": "0.3.2",
|
8 | 8 | "provenance": [],
|
9 | 9 | "private_outputs": true,
|
10 | 10 | "collapsed_sections": [
|
11 | 11 | "Jxv6goXm7oGF"
|
12 | 12 | ],
|
13 |
| - "toc_visible": true |
| 13 | + "toc_visible": true, |
| 14 | + "include_colab_link": true |
14 | 15 | },
|
15 | 16 | "kernelspec": {
|
16 | 17 | "name": "python3",
|
17 | 18 | "display_name": "Python 3"
|
18 | 19 | }
|
19 | 20 | },
|
20 | 21 | "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 | + }, |
21 | 32 | {
|
22 | 33 | "metadata": {
|
23 | 34 | "id": "Jxv6goXm7oGF",
|
|
199 | 210 | },
|
200 | 211 | "cell_type": "code",
|
201 | 212 | "source": [
|
202 |
| - "def g(x):\n", |
| 213 | + "def square_if_positive(x):\n", |
203 | 214 | " if x > 0:\n",
|
204 | 215 | " x = x * x\n",
|
205 | 216 | " else:\n",
|
|
227 | 238 | },
|
228 | 239 | "cell_type": "code",
|
229 | 240 | "source": [
|
230 |
| - "print(autograph.to_code(g))" |
| 241 | + "print(autograph.to_code(square_if_positive))" |
231 | 242 | ],
|
232 | 243 | "execution_count": 0,
|
233 | 244 | "outputs": []
|
|
250 | 261 | },
|
251 | 262 | "cell_type": "code",
|
252 | 263 | "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))))" |
254 | 266 | ],
|
255 | 267 | "execution_count": 0,
|
256 | 268 | "outputs": []
|
|
273 | 285 | },
|
274 | 286 | "cell_type": "code",
|
275 | 287 | "source": [
|
276 |
| - "tf_g = autograph.to_graph(g)\n", |
| 288 | + "tf_square_if_positive = autograph.to_graph(square_if_positive)\n", |
277 | 289 | "\n",
|
278 | 290 | "with tf.Graph().as_default(): \n",
|
279 | 291 | " # The result works like a regular op: takes tensors in, returns tensors.\n",
|
280 | 292 | " # 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", |
283 | 295 | " with tf.Session() as sess:\n",
|
284 | 296 | " print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
|
285 | 297 | ],
|
|
305 | 317 | "cell_type": "code",
|
306 | 318 | "source": [
|
307 | 319 | "# Continue in a loop\n",
|
308 |
| - "def f(l):\n", |
| 320 | + "def sum_even(items):\n", |
309 | 321 | " s = 0\n",
|
310 |
| - " for c in l:\n", |
| 322 | + " for c in items:\n", |
311 | 323 | " if c % 2 > 0:\n",
|
312 | 324 | " continue\n",
|
313 | 325 | " s += c\n",
|
314 | 326 | " return s\n",
|
315 | 327 | "\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", |
317 | 329 | "\n",
|
318 |
| - "tf_f = autograph.to_graph(f)\n", |
| 330 | + "tf_sum_even = autograph.to_graph(sum_even)\n", |
319 | 331 | "\n",
|
320 | 332 | "with tf.Graph().as_default(): \n",
|
321 | 333 | " 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())" |
323 | 335 | ],
|
324 | 336 | "execution_count": 0,
|
325 | 337 | "outputs": []
|
|
332 | 344 | },
|
333 | 345 | "cell_type": "code",
|
334 | 346 | "source": [
|
335 |
| - "print(autograph.to_code(f))" |
| 347 | + "print(autograph.to_code(sum_even))" |
336 | 348 | ],
|
337 | 349 | "execution_count": 0,
|
338 | 350 | "outputs": []
|
|
360 | 372 | "@autograph.convert()\n",
|
361 | 373 | "def fizzbuzz(num):\n",
|
362 | 374 | " if num % 3 == 0 and num % 5 == 0:\n",
|
363 |
| - " print('FizzBuzz')\n", |
| 375 | + " return 'FizzBuzz'\n", |
364 | 376 | " elif num % 3 == 0:\n",
|
365 |
| - " print('Fizz')\n", |
| 377 | + " return 'Fizz'\n", |
366 | 378 | " elif num % 5 == 0:\n",
|
367 |
| - " print('Buzz')\n", |
| 379 | + " return 'Buzz'\n", |
368 | 380 | " else:\n",
|
369 |
| - " print(num)\n", |
370 |
| - " return num\n", |
371 |
| - "\n", |
| 381 | + " return tf.as_string(num)\n", |
372 | 382 | "\n",
|
373 | 383 | "with tf.Graph().as_default():\n",
|
374 | 384 | " # The result works like a regular op: takes tensors in, returns tensors.\n",
|
|
377 | 387 | " result = fizzbuzz(num)\n",
|
378 | 388 | " with tf.Session() as sess:\n",
|
379 | 389 | " for n in range(10,16):\n",
|
380 |
| - " sess.run(result, feed_dict={num:n})" |
| 390 | + " print(sess.run(result, feed_dict={num:n}))" |
381 | 391 | ],
|
382 | 392 | "execution_count": 0,
|
383 | 393 | "outputs": []
|
|
415 | 425 | "cell_type": "code",
|
416 | 426 | "source": [
|
417 | 427 | "@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", |
421 | 431 | "\n",
|
422 | 432 | "with tf.Graph().as_default(): \n",
|
423 | 433 | " with tf.Session():\n",
|
424 | 434 | " try:\n",
|
425 |
| - " print(f(tf.constant(0)).eval())\n", |
| 435 | + " print(inverse(tf.constant(0.0)).eval())\n", |
426 | 436 | " except tf.errors.InvalidArgumentError as e:\n",
|
427 | 437 | " print('Got error message:\\n %s' % e.message)"
|
428 | 438 | ],
|
|
443 | 453 | },
|
444 | 454 | {
|
445 | 455 | "metadata": {
|
446 |
| - "id": "ySTsuxnqCTQi", |
| 456 | + "id": "ehBac9rUR6nh", |
447 | 457 | "colab_type": "code",
|
448 | 458 | "colab": {}
|
449 | 459 | },
|
450 | 460 | "cell_type": "code",
|
451 | 461 | "source": [
|
452 | 462 | "@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", |
458 | 468 | " return n\n",
|
459 | 469 | " \n",
|
460 | 470 | "with tf.Graph().as_default():\n",
|
461 | 471 | " with tf.Session():\n",
|
462 |
| - " f(tf.constant(0)).eval()" |
| 472 | + " count(tf.constant(0)).eval()" |
463 | 473 | ],
|
464 | 474 | "execution_count": 0,
|
465 | 475 | "outputs": []
|
466 | 476 | },
|
467 | 477 | {
|
468 | 478 | "metadata": {
|
469 |
| - "id": "NqF0GT-VCVFh", |
| 479 | + "id": "mtpegD_YR6HK", |
470 | 480 | "colab_type": "text"
|
471 | 481 | },
|
472 | 482 | "cell_type": "markdown",
|
|
485 | 495 | "cell_type": "code",
|
486 | 496 | "source": [
|
487 | 497 | "@autograph.convert()\n",
|
488 |
| - "def f(n):\n", |
| 498 | + "def arange(n):\n", |
489 | 499 | " z = []\n",
|
490 | 500 | " # 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", |
492 | 502 | " \n",
|
493 | 503 | " for i in range(n):\n",
|
494 | 504 | " z.append(i)\n",
|
|
500 | 510 | "\n",
|
501 | 511 | "with tf.Graph().as_default(): \n",
|
502 | 512 | " with tf.Session():\n",
|
503 |
| - " print(f(tf.constant(3)).eval())" |
| 513 | + " print(arange(tf.constant(10)).eval())" |
504 | 514 | ],
|
505 | 515 | "execution_count": 0,
|
506 | 516 | "outputs": []
|
|
512 | 522 | },
|
513 | 523 | "cell_type": "markdown",
|
514 | 524 | "source": [
|
515 |
| - "### Nested if statements" |
| 525 | + "### Nested control flow" |
516 | 526 | ]
|
517 | 527 | },
|
518 | 528 | {
|
|
746 | 756 | " # to convert these lists into their graph equivalent,\n",
|
747 | 757 | " # we need to specify the element type of the lists.\n",
|
748 | 758 | " train_losses = []\n",
|
749 |
| - " autograph.utils.set_element_type(train_losses, tf.float32)\n", |
| 759 | + " autograph.set_element_type(train_losses, tf.float32)\n", |
750 | 760 | " test_losses = []\n",
|
751 |
| - " autograph.utils.set_element_type(test_losses, tf.float32)\n", |
| 761 | + " autograph.set_element_type(test_losses, tf.float32)\n", |
752 | 762 | " train_accuracies = []\n",
|
753 |
| - " autograph.utils.set_element_type(train_accuracies, tf.float32)\n", |
| 763 | + " autograph.set_element_type(train_accuracies, tf.float32)\n", |
754 | 764 | " test_accuracies = []\n",
|
755 |
| - " autograph.utils.set_element_type(test_accuracies, tf.float32)\n", |
| 765 | + " autograph.set_element_type(test_accuracies, tf.float32)\n", |
756 | 766 | " \n",
|
757 | 767 | " # This entire training loop will be run in-graph.\n",
|
758 | 768 | " i = tf.constant(0)\n",
|
|
0 commit comments