In [1]:
import tensorflow as tf

Graphs


In [2]:
tf.get_default_graph()


Out[2]:
<tensorflow.python.framework.ops.Graph at 0x7f8cbc578b70>

In [3]:
graph = tf.Graph()

In [4]:
graph


Out[4]:
<tensorflow.python.framework.ops.Graph at 0x7f8c9c483d68>

In [5]:
tf.get_default_graph()


Out[5]:
<tensorflow.python.framework.ops.Graph at 0x7f8cbc578b70>

Variables


In [6]:
x1 = tf.Variable(3.0, name='x')
y1 = tf.Variable(4.0)

In [7]:
x1, y1


Out[7]:
(<tensorflow.python.ops.variables.Variable at 0x7f8c9c48d908>,
 <tensorflow.python.ops.variables.Variable at 0x7f8c9c483f60>)

In [8]:
x1.name, y1.name


Out[8]:
('x:0', 'Variable:0')

In [9]:
x1.graph is tf.get_default_graph()


Out[9]:
True

In [10]:
y1.graph is tf.get_default_graph()


Out[10]:
True

In [11]:
with graph.as_default():
    x2 = tf.Variable(4.1, name='x2')

In [12]:
x2.graph is tf.get_default_graph()


Out[12]:
False

In [13]:
x2.graph is graph


Out[13]:
True

In [14]:
f1 = 2*x1 + y1

lazy evaluation


In [15]:
f1


Out[15]:
<tf.Tensor 'add:0' shape=() dtype=float32>

This is what happens when we operate on two variables that are from different graphs


In [16]:
f2 = 2*x2 + y1


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-617f6fa844bd> in <module>()
----> 1 f2 = 2*x2 + y1

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
    789 
    790   def binary_op_wrapper(x, y):
--> 791     with ops.name_scope(None, op_name, [x, y]) as name:
    792       if not isinstance(y, sparse_tensor.SparseTensor):
    793         y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")

~/lib/build/anaconda3/lib/python3.6/contextlib.py in __enter__(self)
     79     def __enter__(self):
     80         try:
---> 81             return next(self.gen)
     82         except StopIteration:
     83             raise RuntimeError("generator didn't yield") from None

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in name_scope(name, default_name, values)
   4149   if values is None:
   4150     values = []
-> 4151   g = _get_graph_from_inputs(values)
   4152   with g.as_default(), g.name_scope(n) as scope:
   4153     yield scope

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _get_graph_from_inputs(op_input_list, graph)
   3898         graph = graph_element.graph
   3899       elif original_graph_element is not None:
-> 3900         _assert_same_graph(original_graph_element, graph_element)
   3901       elif graph_element.graph is not graph:
   3902         raise ValueError(

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _assert_same_graph(original_item, item)
   3837   if original_item.graph is not item.graph:
   3838     raise ValueError(
-> 3839         "%s must be from the same graph as %s." % (item, original_item))
   3840 
   3841 

ValueError: Tensor("Variable:0", shape=(), dtype=float32_ref) must be from the same graph as Tensor("mul:0", shape=(), dtype=float32).

Variable initialization

  • all tf.Variables need to be initalized
  • tf.constant need NOT to be initialized

Initialization options

Manual initialization
x1.initializer.run()
sess.run(x.initializer)
Automatic initializer with tf.global_variables_initializer()
init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()

In [17]:
with tf.Session() as sess:
    x1.initializer.run()
    y1.initializer.run()
    f1_value = f1.eval()
print("f1: {}".format(f1_value))


f1: 10.0

This is what happens if you forget to initialize the variable


In [18]:
with tf.Session() as sess:
    x1.initializer.run()
    f1_value = f1.eval()
print("f1: {}".format(f1_value))


---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1021     try:
-> 1022       return fn(*args)
   1023     except errors.OpError as e:

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1003                                  feed_dict, fetch_list, target_list,
-> 1004                                  status, run_metadata)
   1005 

~/lib/build/anaconda3/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     87             try:
---> 88                 next(self.gen)
     89             except StopIteration:

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
    465           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466           pywrap_tensorflow.TF_GetCode(status))
    467   finally:

FailedPreconditionError: Attempting to use uninitialized value Variable
	 [[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

During handling of the above exception, another exception occurred:

FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-18-30f75bc72231> in <module>()
      1 with tf.Session() as sess:
      2     x1.initializer.run()
----> 3     f1_value = f1.eval()
      4 print("f1: {}".format(f1_value))

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in eval(self, feed_dict, session)
    565 
    566     """
--> 567     return _eval_using_default_session(self, feed_dict, self.graph, session)
    568 
    569 

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
   3727                        "the tensor's graph is different from the session's "
   3728                        "graph.")
-> 3729   return session.run(tensors, feed_dict)
   3730 
   3731 

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    963     if final_fetches or final_targets:
    964       results = self._do_run(handle, final_targets, final_fetches,
--> 965                              feed_dict_string, options, run_metadata)
    966     else:
    967       results = []

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1013     if handle is None:
   1014       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1015                            target_list, options, run_metadata)
   1016     else:
   1017       return self._do_call(_prun_fn, self._session, handle, feed_dict,

~/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1033         except KeyError:
   1034           pass
-> 1035       raise type(e)(node_def, op, message)
   1036 
   1037   def _extend_graph(self):

FailedPreconditionError: Attempting to use uninitialized value Variable
	 [[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

Caused by op 'Variable/read', defined at:
  File "/home/edy/lib/build/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2903, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-cdab7a48593a>", line 2, in <module>
    y1 = tf.Variable(4.0)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 197, in __init__
    expected_shape=expected_shape)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 315, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1490, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/edy/lib/build/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable
	 [[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

Using tf.global_variables_initializer()


In [19]:
init = tf.global_variables_initializer()

In [20]:
with tf.Session() as sess:
    init.run()
    f1_value_with_global_variables_initializer = f1.eval()
print("f1_value_with_global_variables_initializer: {}".format(f1_value_with_global_variables_initializer))


f1_value_with_global_variables_initializer: 10.0

Life cycle of a node value


In [28]:
w = tf.constant(3)
x = w + 2
y = x + 5
z = x * 3

In [29]:
x


Out[29]:
<tf.Tensor 'add_7:0' shape=() dtype=int32>

In [30]:
with tf.Session() as sess:
    print(y.eval())
    print(z.eval())


10
15

In [31]:
x


Out[31]:
<tf.Tensor 'add_7:0' shape=() dtype=int32>

A more efficient evaluation of the TensorFlow Graph


In [33]:
with tf.Session() as sess:
    y_val, z_val = sess.run([y,z])
    print(y_val)
    print(z_val)


10
15