In [1]:
from __future__ import print_function
import numpy as np
import tensorflow as tf

In [2]:
from datetime import date
date.today()


Out[2]:
datetime.date(2017, 2, 27)

In [3]:
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"

In [4]:
tf.__version__


Out[4]:
'1.0.0'

In [5]:
np.__version__


Out[5]:
'1.12.0'

Q1-3. You are to implement the graph below. Complete the code.

<img src="figs/fig1.png",width=500>


In [6]:
# Q1. Create a graph
g = ...

with g.as_default():
    # Define inputs
    with tf.name_scope("inputs"):
        a = tf.constant(2, tf.int32, name="a")
        b = tf.constant(3, tf.int32, name="b")

    # Ops
    with tf.name_scope("ops"):
        c = tf.multiply(a, b, name="c")
        d = tf.add(a, b, name="d")
        e = tf.subtract(c, d, name="e")

# Q2. Start a session
sess = ...

# Q3. Fetch c, d, e
_c, _d, _e = ...
print("c =", _c)
print("d =", _d)
print("e =", _e)

# Close the session
sess.close()


c = 6
d = 5
e = 1

Q4-8. You are to implement the graph below. Complete the code.

<img src="figs/fig3.png",width=500>


In [7]:
tf.reset_default_graph()

In [8]:
# Define inputs
a = tf.Variable(tf.random_uniform([]))
b_pl = tf.placeholder(tf.float32, [None])

# Ops
c = a * b_pl
d = a + b_pl
e = tf.reduce_sum(c)
f = tf.reduce_mean(d)
g = e - f

# initialize variable(s)
init = tf.global_variables_initializer()

# Update variable
update_op = tf.assign(a, a + g)

# Q4. Create a (summary) writer to `asset`
writer = ...

#Q5. Add `a` to summary.scalar
...

#Q6. Add `c` and `d` to summary.histogram
...

#Q7. Merge all summaries.
summaries = ...

# Start a session
sess = tf.Session()

# Initialize Variable(s)
sess.run(init)

# Fetch the value of c, d, and e.
for step in range(5):
    _b = np.arange(10, dtype=np.float32)
    _, summaries_proto = sess.run([update_op, summaries], {b_pl:_b})
    
    # Q8. Attach summaries_proto to TensorBoard.
    ...
    
# Close the session
sess.close()

In [ ]: