CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Custom and Distributed Training with Tensorflow/Week 3 - Graph Mode/C2_W3_Lab_1_autograph-basics.ipynb
Views: 13370
Kernel: Python 3

Autograph: Basic

In this ungraded lab, you will go through some of the basics of autograph so you can explore what the generated code looks like.

Imports

import tensorflow as tf

Addition in autograph

You can use the @tf.function decorator to automatically generate the graph-style code as shown below:

@tf.function def add(a, b): return a + b a = tf.Variable([[1.,2.],[3.,4.]]) b = tf.Variable([[4.,0.],[1.,5.]]) print(tf.add(a, b)) # See what the generated code looks like print(tf.autograph.to_code(add.python_function))

if-statements in autograph

Control flow statements which are very intuitive to write in eager mode can look very complex in graph mode. You can see that in the next examples: first a simple function, then a more complicated one that involves lots of ops and conditionals (fizzbuzz).

# simple function that returns the square if the input is greater than zero @tf.function def f(x): if x>0: x = x * x return x print(tf.autograph.to_code(f.python_function))

Fizzbuzz in autograph

You may remember implementing fizzbuzz in preparation for a coding interview.

  • Imagine how much fun it would be if you were asked to impement the graph mode version of that code!

Fortunately, you can just use @tf.function and then call tf.autograph.to_code!

@tf.function def fizzbuzz(max_num): counter = 0 for num in range(max_num): if num % 3 == 0 and num % 5 == 0: print('FizzBuzz') elif num % 3 == 0: print('Fizz') elif num % 5 == 0: print('Buzz') else: print(num) counter += 1 return counter print(tf.autograph.to_code(fizzbuzz.python_function))