In [ ]:
"""Summary of tensorflow basics.

Parag K. Mital, Jan 2016."""

In [1]:
# %% Import tensorflow and pyplot
import tensorflow as tf
import matplotlib.pyplot as plt


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-d9aae85f7164> in <module>()
      1 # %% Import tensorflow and pyplot
----> 2 import tensorflow as tf
      3 import matplotlib.pyplot as plt

ImportError: No module named tensorflow

In [3]:
# %% tf.Graph represents a collection of tf.Operations
# You can create operations by writing out equations.
# By default, there is a graph: tf.get_default_graph()
# and any new operations are added to this graph.
# The result of a tf.Operation is a tf.Tensor, which holds
# the values.

In [4]:
# %% First a tf.Tensor
n_values = 32
x = tf.linspace(-3.0, 3.0, n_values)

In [5]:
# %% Construct a tf.Session to execute the graph.
sess = tf.Session()
result = sess.run(x)

In [6]:
# %% Alternatively pass a session to the eval fn:
x.eval(session=sess)
# x.eval() does not work, as it requires a session!


Out[6]:
array([-3.        , -2.80645156, -2.61290312, -2.41935492, -2.22580647,
       -2.03225803, -1.83870971, -1.64516139, -1.45161295, -1.25806451,
       -1.06451619, -0.87096786, -0.67741942, -0.48387098, -0.29032278,
       -0.09677434,  0.0967741 ,  0.29032254,  0.48387098,  0.67741919,
        0.87096763,  1.06451607,  1.25806427,  1.45161295,  1.64516115,
        1.83870935,  2.03225803,  2.22580624,  2.41935444,  2.61290312,
        2.80645132,  3.        ], dtype=float32)

In [7]:
# %% We can setup an interactive session if we don't
# want to keep passing the session around:
sess.close()
sess = tf.InteractiveSession()

In [8]:
# %% Now this will work!
x.eval()


Out[8]:
array([-3.        , -2.80645156, -2.61290312, -2.41935492, -2.22580647,
       -2.03225803, -1.83870971, -1.64516139, -1.45161295, -1.25806451,
       -1.06451619, -0.87096786, -0.67741942, -0.48387098, -0.29032278,
       -0.09677434,  0.0967741 ,  0.29032254,  0.48387098,  0.67741919,
        0.87096763,  1.06451607,  1.25806427,  1.45161295,  1.64516115,
        1.83870935,  2.03225803,  2.22580624,  2.41935444,  2.61290312,
        2.80645132,  3.        ], dtype=float32)

In [9]:
# %% Now a tf.Operation
# We'll use our values from [-3, 3] to create a Gaussian Distribution
sigma = 1.0
mean = 0.0
z = (tf.exp(tf.neg(tf.pow(x - mean, 2.0) /
                   (2.0 * tf.pow(sigma, 2.0)))) *
     (1.0 / (sigma * tf.sqrt(2.0 * 3.1415))))

In [10]:
# %% By default, new operations are added to the default Graph
assert z.graph is tf.get_default_graph()

In [11]:
# %% Execute the graph and plot the result
plt.plot(z.eval())


---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-11-8197a1e2fd40> in <module>()
      1 # %% Execute the graph and plot the result
----> 2 plt.plot(z.eval())

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   2978 @_autogen_docstring(Axes.plot)
   2979 def plot(*args, **kwargs):
-> 2980     ax = gca()
   2981     # allow callers to override the hold state by passing hold=True|False
   2982     washold = ax.ishold()

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in gca(**kwargs)
    801     """
    802 
--> 803     ax =  gcf().gca(**kwargs)
    804     return ax
    805 

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in gcf()
    448         return figManager.canvas.figure
    449     else:
--> 450         return figure()
    451 
    452 fignum_exists = _pylab_helpers.Gcf.has_fignum

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    421                                         frameon=frameon,
    422                                         FigureClass=FigureClass,
--> 423                                         **kwargs)
    424 
    425         if figLabel:

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     77     FigureClass = kwargs.pop('FigureClass', Figure)
     78     figure = FigureClass(*args, **kwargs)
---> 79     return new_figure_manager_given_figure(num, figure)
     80 
     81 

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     85     """
     86     _focus = windowing.FocusManager()
---> 87     window = Tk.Tk()
     88     window.withdraw()
     89 

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1765                 baseName = baseName + ext
   1766         interactive = 0
-> 1767         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1768         if useTk:
   1769             self._loadtk()

TclError: no display name and no $DISPLAY environment variable

In [16]:
# %% We can find out the shape of a tensor like so:
print(z.get_shape())


(32,)

In [17]:
# %% Or in a more friendly format
print(z.get_shape().as_list())


[32]

In [18]:
# %% Sometimes we may not know the shape of a tensor
# until it is computed in the graph.  In that case
# we should use the tf.shape fn, which will return a
# Tensor which can be eval'ed, rather than a discrete
# value of tf.Dimension
print(tf.shape(z).eval())


[32]

In [19]:
# %% We can combine tensors like so:
print(tf.pack([tf.shape(z), tf.shape(z), [3], [4]]).eval())


[[32]
 [32]
 [ 3]
 [ 4]]

In [20]:
# %% Let's multiply the two to get a 2d gaussian
z_2d = tf.matmul(tf.reshape(z, [n_values, 1]), tf.reshape(z, [1, n_values]))

In [21]:
# %% Execute the graph and store the value that `out` represents in `result`.
plt.imshow(z_2d.eval())


---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-21-32e12786682b> in <module>()
      1 # %% Execute the graph and store the value that `out` represents in `result`.
----> 2 plt.imshow(z_2d.eval())

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2879            filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None,
   2880            hold=None, **kwargs):
-> 2881     ax = gca()
   2882     # allow callers to override the hold state by passing hold=True|False
   2883     washold = ax.ishold()

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in gca(**kwargs)
    801     """
    802 
--> 803     ax =  gcf().gca(**kwargs)
    804     return ax
    805 

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in gcf()
    448         return figManager.canvas.figure
    449     else:
--> 450         return figure()
    451 
    452 fignum_exists = _pylab_helpers.Gcf.has_fignum

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    421                                         frameon=frameon,
    422                                         FigureClass=FigureClass,
--> 423                                         **kwargs)
    424 
    425         if figLabel:

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     77     FigureClass = kwargs.pop('FigureClass', Figure)
     78     figure = FigureClass(*args, **kwargs)
---> 79     return new_figure_manager_given_figure(num, figure)
     80 
     81 

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     85     """
     86     _focus = windowing.FocusManager()
---> 87     window = Tk.Tk()
     88     window.withdraw()
     89 

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1765                 baseName = baseName + ext
   1766         interactive = 0
-> 1767         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1768         if useTk:
   1769             self._loadtk()

TclError: no display name and no $DISPLAY environment variable

In [ ]:
# %% For fun let's create a gabor patch:
x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1])
y = tf.reshape(tf.ones_like(x), [1, n_values])
z = tf.mul(tf.matmul(x, y), z_2d)
plt.imshow(z.eval())

In [ ]:
# %% We can also list all the operations of a graph:
ops = tf.get_default_graph().get_operations()
print([op.name for op in ops])

In [ ]:
# %% Lets try creating a generic function for computing the same thing:
def gabor(n_values=32, sigma=1.0, mean=0.0):
    x = tf.linspace(-3.0, 3.0, n_values)
    z = (tf.exp(tf.neg(tf.pow(x - mean, 2.0) /
                       (2.0 * tf.pow(sigma, 2.0)))) *
         (1.0 / (sigma * tf.sqrt(2.0 * 3.1415))))
    gauss_kernel = tf.matmul(
        tf.reshape(z, [n_values, 1]), tf.reshape(z, [1, n_values]))
    x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1])
    y = tf.reshape(tf.ones_like(x), [1, n_values])
    gabor_kernel = tf.mul(tf.matmul(x, y), gauss_kernel)
    return gabor_kernel

In [ ]:
# %% Confirm this does something:
plt.imshow(gabor().eval())

In [ ]:
# %% And another function which can convolve
def convolve(img, W):
    # The W matrix is only 2D
    # But conv2d will need a tensor which is 4d:
    # height x width x n_input x n_output
    if len(W.get_shape()) == 2:
        dims = W.get_shape().as_list() + [1, 1]
        W = tf.reshape(W, dims)

    if len(img.get_shape()) == 2:
        # num x height x width x channels
        dims = [1] + img.get_shape().as_list() + [1]
        img = tf.reshape(img, dims)
    elif len(img.get_shape()) == 3:
        dims = [1] + img.get_shape().as_list()
        img = tf.reshape(img, dims)
        # if the image is 3 channels, then our convolution
        # kernel needs to be repeated for each input channel
        W = tf.concat(2, [W, W, W])

    # Stride is how many values to skip for the dimensions of
    # num, height, width, channels
    convolved = tf.nn.conv2d(img, W,
                             strides=[1, 1, 1, 1], padding='SAME')
    return convolved

In [ ]:
# %% Load up an image:
from skimage import data
img = data.astronaut()
plt.imshow(img)
print(img.shape)

In [ ]:
# %% Now create a placeholder for our graph which can store any input:
x = tf.placeholder(tf.float32, shape=img.shape)

In [ ]:
# %% And a graph which can convolve our image with a gabor
out = convolve(x, gabor())

In [ ]:
# %% Now send the image into the graph and compute the result
result = tf.squeeze(out).eval(feed_dict={x: img})
plt.imshow(result)