Tensorflow Basics


In [1]:
import tensorflow as tf
import numpy as np
Isess = tf.InteractiveSession()

Representing Tensors

The rank of a tensor is the number of indices required to specify an element


In [2]:
m1 = [[1.0, 2.0], [3.0, 4.0]]                               #list
m2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)   #numpy ndarray
m3 = tf.constant([[1.0, 2.0], [3.0, 4.0]])                  #Tensor constant object

print(type(m1))
print(type(m2))
print(type(m3))


<class 'list'>
<class 'numpy.ndarray'>
<class 'tensorflow.python.framework.ops.Tensor'>

In [3]:
t1 = tf.convert_to_tensor(m1, dtype=tf.float32)
print(type(t1))


<class 'tensorflow.python.framework.ops.Tensor'>

In [4]:
m4 = tf.constant([ [1,2], [3,4] ])
print(m4)


Tensor("Const_2:0", shape=(2, 2), dtype=int32)

In [5]:
tf.ones([3,3]) * 0.5


Out[5]:
<tf.Tensor 'mul:0' shape=(3, 3) dtype=float32>

Tensorflow Operators

  • tf.add(x,y)
  • tf.subtract(x,y)
  • tf.multiply(x,y)
  • tf.pow(x,y)
  • tf.sqrt(x,y)
  • tf.div(x,y)
  • tf.truediv(x,y)
  • tf.floordiv(x,y)
  • tf.mod(x,y)

Sessions

TF requires a session to execute an operation and retrieve its calculated value. A Session is a hardware environment definition describing how code will run. When the Session starts, it assigns the CPU/GPU devices to nodes in the graph. The Session definition can change, without requiring the machine learning code to change


In [6]:
x = tf.constant([1.,2.])
print(x)

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    result = sess.run(tf.negative(x))
    
print(result)
sess.close()


Tensor("Const_3:0", shape=(2,), dtype=float32)
[-1. -2.]

Sessions can take placeholders, variables, and constants as input

  • Placeholder - unassigned value that will be initialized by the session wherever it's run (usually model input/output)
  • Variable - value that can change (must be initialized before use)
  • Constant - value that does not change

In [7]:
# sess = tf.InteractiveSession()                 #start Session in interactive mode

raw_data = [1., 3., -5., -4., 0., 3., 9.]

spike = tf.Variable(False)                     #create a Boolean tf variable
spike.initializer.run()                        #all tf variables must be initialized

for i in range(1, len(raw_data)):
    if raw_data[i] - raw_data[i-1] > 3:
        updater = tf.assign(spike, True)       #update variable with assign(varName, value)
        updater.eval()                         #evaluate variable to see the change
    else:
        tf.assign(spike, False).eval()
    print("Spike", spike.eval())
    
# sess.close()


Spike False
Spike False
Spike False
Spike True
Spike False
Spike True

In [3]:
raw_data = [1., 3., -5., -4., 0., 3., 9.]

spikes = tf.Variable([False] * len(raw_data), name='spikes')    #create vector of boolean variables
spikes.initializer.run()                                        #and initialize

saver = tf.train.Saver()                                        #saves and restores variables (all by default)

for i in range(1, len(raw_data)):
    if raw_data[i] - raw_data[i-1] > 3:
        spikes_val = spikes.eval()
        spikes_val[i] = True
        updater = tf.assign(spikes, spikes_val)                 #update variable with assign(varName, value)
        updater.eval()                                          #evaluate variable to see the change
    
save_path = saver.save(Isess, './spikes.ckpt')                  #saves variables to disk
print("spikes data saved in file: %s" % save_path)


spikes data saved in file: ./spikes.ckpt

In [9]:
%ls spike*


spikes.ckpt.data-00000-of-00001  spikes.ckpt.index  spikes.ckpt.meta

saver.save() saves a compact binary version of the variables in 'ckpt' files. They can only be read by using the saver.restore() function


In [6]:
spikes = tf.Variable([False]*7, name='spikes')
# spikes.initializer.run()                                      #don't need to init, they'll be directly loaded
saver = tf.train.Saver()

saver.restore(Isess, "./spikes.ckpt")
print(spikes.eval())


INFO:tensorflow:Restoring parameters from ./spikes.ckpt
[False False False False  True False  True]

In [3]:
raw_data = np.random.normal(10, 1, 100)

alpha = tf.constant(0.05)
curr_value = tf.placeholder(tf.float32)
prev_avg = tf.Variable(0.)
update_avg = alpha * curr_value + (1 - alpha) * prev_avg

### Create Summary Nodes for TensorBoard ###
avg_hist = tf.summary.scalar("running_average", update_avg)
value_hist = tf.summary.scalar("incoming_values", curr_value)
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs")

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    
    for i in range(len(raw_data)):
        summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]})
        sess.run(tf.assign(prev_avg, curr_avg))
        print(raw_data[i], curr_avg)
        writer.add_summary(summary_str, i)


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1321     try:
-> 1322       return fn(*args)
   1323     except errors.OpError as e:

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1306       return self._call_tf_sessionrun(
-> 1307           options, feed_dict, fetch_list, target_list, run_metadata)
   1308 

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1408           self._session, options, feed_dict, fetch_list, target_list,
-> 1409           run_metadata)
   1410     else:

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
	 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-3-ac8e1a3d0079> in <module>()
     18 
     19     for i in range(len(raw_data)):
---> 20         summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]})
     21         sess.run(tf.assign(prev_avg, curr_avg))
     22         print(raw_data[i], curr_avg)

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1133     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1134       results = self._do_run(handle, final_targets, final_fetches,
-> 1135                              feed_dict_tensor, options, run_metadata)
   1136     else:
   1137       results = []

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1314     if handle is None:
   1315       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316                            run_metadata)
   1317     else:
   1318       return self._do_call(_prun_fn, handle, feeds, fetches)

~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333         except KeyError:
   1334           pass
-> 1335       raise type(e)(node_def, op, message)
   1336 
   1337   def _extend_graph(self):

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
	 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'Placeholder', defined at:
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/argyle/.local/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
    self.io_loop.start()
  File "/home/argyle/.local/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 132, in start
    self.asyncio_loop.run_forever()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 422, in run_forever
    self._run_once()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 1432, in _run_once
    handle._run()
  File "/usr/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/home/argyle/.local/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 122, in _handle_events
    handler_func(fileobj, events)
  File "/home/argyle/.local/lib/python3.6/site-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/argyle/.local/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "/home/argyle/.local/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/argyle/.local/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "/home/argyle/.local/lib/python3.6/site-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/argyle/.local/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/argyle/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/home/argyle/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/argyle/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2901, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/argyle/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-93c6e8cbd9fb>", line 4, in <module>
    curr_value = tf.placeholder(tf.float32)
  File "/home/argyle/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1734, in placeholder
    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
  File "/home/argyle/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 4924, in placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "/home/argyle/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/argyle/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3414, in create_op
    op_def=op_def)
  File "/home/argyle/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1740, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
	 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: