Try not to peek at the solutions when you go through the exercises. ;-)

First let's make sure this notebook works well in both Python 2 and Python 3:


In [ ]:
from __future__ import absolute_import, division, print_function, unicode_literals

TensorFlow basics


In [ ]:
import tensorflow as tf
tf.__version__

Construction Phase


In [ ]:
>>> a = tf.constant(3)
>>> b = tf.constant(5)
>>> s = a + b

In [ ]:
a

In [ ]:
b

In [ ]:
s

In [ ]:
tf.get_default_graph()

In [ ]:
>>> graph = tf.Graph()
>>> with graph.as_default():
...     a = tf.constant(3)
...     b = tf.constant(5)
...     s = a + b
...

Execution Phase


In [ ]:
>>> with tf.Session(graph=graph) as sess:
...     result = s.eval()
...
>>> result

In [ ]:
>>> with tf.Session(graph=graph) as sess:
...     result = sess.run(s)
...
>>> result

In [ ]:
>>> with tf.Session(graph=graph) as sess:
...     result = sess.run([a,b,s])
...
>>> result

Exercise 1

1.1) Create a simple graph that calculates $ c = \exp(\sqrt 8 + 3) $.

Tip: TensorFlow's API documentation is available at: https://www.tensorflow.org/versions/master/api_docs/python/


In [ ]:


In [ ]:


In [ ]:

1.2) Now create a Session() and evaluate the operation that gives you the result of the equation above:


In [ ]:


In [ ]:


In [ ]:

1.3) Create a graph that evaluates and prints both $ b = \sqrt 8 $ and $ c = \exp(\sqrt 8 + 3) $. Try to implement this in a way that only evaluates $ \sqrt 8 $ once.


In [ ]:


In [ ]:


In [ ]:

1.4) The following code is needed to display TensorFlow graphs in Jupyter. Just run this cell then visualize your graph by calling show_graph(your graph):


In [ ]:
import numpy as np
from IPython.display import display, HTML

def strip_consts(graph_def, max_const_size=32):
    """Strip large constant values from graph_def."""
    strip_def = tf.GraphDef()
    for n0 in graph_def.node:
        n = strip_def.node.add() 
        n.MergeFrom(n0)
        if n.op == 'Const':
            tensor = n.attr['value'].tensor
            size = len(tensor.tensor_content)
            if size > max_const_size:
                tensor.tensor_content = b"<stripped %d bytes>"%size
    return strip_def

def show_graph(graph_def=None, max_const_size=32):
    """Visualize TensorFlow graph."""
    graph_def = graph_def or tf.get_default_graph()
    if hasattr(graph_def, 'as_graph_def'):
        graph_def = graph_def.as_graph_def()
    strip_def = strip_consts(graph_def, max_const_size=max_const_size)
    code = """
        <script>
          function load() {{
            document.getElementById("{id}").pbtxt = {data};
          }}
        </script>
        <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
        <div style="height:600px">
          <tf-graph-basic id="{id}"></tf-graph-basic>
        </div>
    """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))

    iframe = """
        <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '&quot;'))
    display(HTML(iframe))

In [ ]:


In [ ]:


In [ ]:

Try not to peek at the solution below before you have done the exercise! :)

Exercise 1 - Solution

1.1)


In [ ]:
graph = tf.Graph()
with graph.as_default():
    c = tf.exp(tf.add(tf.sqrt(tf.constant(8.)), tf.constant(3.)))
    # or simply...
    c = tf.exp(tf.sqrt(8.) + 3.)

1.2)


In [ ]:
with tf.Session(graph=graph):
    c_val = c.eval()

In [ ]:
c_val

1.3)


In [ ]:
graph = tf.Graph()
with graph.as_default():
    b = tf.sqrt(8.)
    c = tf.exp(b + 3)

In [ ]:
with tf.Session(graph=graph) as sess:
    b_val, c_val = sess.run([b, c])

In [ ]:
b_val

In [ ]:
c_val

Important: the following implementation gives the right result, but it runs the graph twice, once to evaluate b, and once to evaluate c. Since c depends on b, it means that b will be evaluated twice. Not what we wanted.


In [ ]:
# WRONG!
with tf.Session(graph=graph):
    b_val = b.eval()  # evaluates b
    c_val = c.eval()  # evaluates c, which means evaluating b again!

In [ ]:
b_val

In [ ]:
c_val

1.4)


In [ ]:
show_graph(graph)