Introduction to tensorflow

Author: Dr. Rahul Remanan

CEO and Chief Imagination Officer, Moad Computer

This notebook is an introduction to tensorflow, a popular deep-learning framework. Tensorflow has brilliant integration with Python. This notebook uses the tensorflow python3 APIs. This notebook is a modified fork of tensorflow examples repository.

Part 01 -- Hello world in tensorflow

This is a simple hello world example using tensorflow.

In this example, a tensorflow constant op is created. This op is added as a node to the default graph. The value returned by the constructor represents the output of this constant op.


In [0]:
import tensorflow as tf

Create a tensorflow constant op


In [0]:
hello = tf.constant('Hello, TensorFlow!')

Initialize the tf session


In [0]:
sess = tf.Session()

Run the tensorflow graph


In [4]:
print(sess.run(hello))


b'Hello, TensorFlow!'

Part 02 -- Basic operations in tensorflow

In part 02 of this notebook, implementation of some of basic constant operations in tensorflow is discussed. The value returned by the constructor represents the output of the tensorflow constant op.

Initialize constant op in tensorflow


In [0]:
a = tf.constant(2)
b = tf.constant(3)

In [6]:
print (a)


Tensor("Const_1:0", shape=(), dtype=int32)

Launching the default graph and perform some basic functions

  • Launch tensorflow graph
  • Return the values of constant op
  • Return the sum and product of the graph input

In [7]:
with tf.Session() as sess:
    print ("a: %i" % sess.run(a), "b: %i" % sess.run(b))
    print ("Addition with constants using tensorflow: %i" % sess.run(a+b))
    print ("Multiplication with constants using tensorflow: %i" % sess.run(a*b))


a: 2 b: 3
Addition with constants using tensorflow: 5
Multiplication with constants using tensorflow: 6

Passing a variable as the tensorflow graph input

In this portion of the notebook, basic operations with variable as graph input is discussed. The value returned by the constructor represents the output of the Variable op. It is defined as an input when running session.

Initialize a tensorflow placeholder


In [0]:
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

Create functions for adding and multiplying


In [0]:
add = tf.add(a, b)
mul = tf.multiply(a, b)

Launch the default graph

  • Launch default graph
  • Run the addition and multiplication functions

In [10]:
with tf.Session() as sess:
    input_dict = {a: 2, b: 3}
    print ("a: %i" % sess.run(a, feed_dict=input_dict), "b: %i" % sess.run(b, feed_dict=input_dict))
    print ("Addition with variables using tensorflow: %i" % sess.run(add, feed_dict=input_dict))
    print ("Multiplication with variables using tensorflow: %i" % sess.run(mul, feed_dict=input_dict))


a: 2 b: 3
Addition with variables using tensorflow: 5
Multiplication with variables using tensorflow: 6

Matrix multiplication example

This portion of the tutorial uses the matrix multiplication example from TensorFlow official tutorial. Create a Constant op that produces a 1x2 matrix. The op is added as a node to the default graph. The value returned by the constructor represents the output of the Constant op.


In [0]:
matrix1 = tf.constant([[3., 3.]])

Create another Constant that produces a 2x1 matrix


In [0]:
matrix2 = tf.constant([[2.],[2.]])

Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs. The returned value, 'product', represents the result of the matrix multiplication.


In [0]:
mat_mul = tf.matmul(matrix1, matrix2)

Running the matmul op we call the session 'run()' method, passing 'product' which represents the output of the matmul op. This indicates to the call that we want to get the output of the matmul op back. All inputs needed by the op are run automatically by the session. They typically are run in parallel. The call 'run(product)' thus causes the execution of threes ops in the graph: the two constants and matmul. The output of the op is returned in 'result' as a numpy ndarray object.


In [14]:
with tf.Session() as sess:
    result = sess.run(mat_mul)
    print (result)


[[12.]]

Part 03 -- Tensorflow Eager API

Eager execution is an imperative, define-by-run interface where operations are executed immediately as they are called from Python. This makes it easier to get started with TensorFlow, and can make research and development more intuitive. A vast majority of the TensorFlow API remains the same whether eager execution is enabled or not. As a result, the exact same code that constructs TensorFlow graphs (e.g. using the layers API) can be executed imperatively by using eager execution. Conversely, most models written with Eager enabled can be converted to a graph that can be further optimized and/or extracted for deployment in production without changing code. - Rajat Monga

Read more about tensorflow eager API.


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

import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe

Setting Eager API


In [2]:
print("Enabling Eager mode ...")
tfe.enable_eager_execution()


Enabling Eager mode ...

Define constant tensors


In [3]:
print("Initialize constant tensors ...")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)


Initialize constant tensors ...
a = 2
b = 3

Running the tensorflow operations without calling the tf.Session explicitly


In [4]:
print("Running operations, without tf.Session")
c = a + b
print("a + b = %i" % c)
d = a * b
print("a * b = %i" % d)


Running operations, without tf.Session
a + b = 5
a * b = 6

Compatibility with Numpy

  • Define constant tensors
  • Mixed operations with both tensors and numpy arrays

In [5]:
print("Mixed operations with tensors and numpy arrays ...")

a = tf.constant([[2., 1.],
                 [1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % a)
b = np.array([[3., 0.],
              [5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)


Mixed operations with tensors and numpy arrays ...
Tensor:
 a = tf.Tensor(
[[2. 1.]
 [1. 0.]], shape=(2, 2), dtype=float32)
NumpyArray:
 b = [[3. 0.]
 [5. 1.]]

Matrix operations using tensorflow eager API

  • Run matrix operations without explicitly calling tf.Session

In [6]:
print("Running matrix operations without tf.Session ...")

c = a + b
print("a + b = %s" % c)

d = tf.matmul(a, b)
print("a * b = %s" % d)


Running matrix operations without tf.Session ...
a + b = tf.Tensor(
[[5. 1.]
 [6. 1.]], shape=(2, 2), dtype=float32)
a * b = tf.Tensor(
[[11.  1.]
 [ 3.  0.]], shape=(2, 2), dtype=float32)

Iteration operations through a tensor


In [7]:
print("Iterating through Tensor 'a': ...")
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])


Iterating through Tensor 'a': ...
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0.0, shape=(), dtype=float32)

Part 04 -- Object Oriented Programming Using Tensorflow

Object oriented example of tensorflow addition and multiplication:


In [0]:
import tensorflow as tf
class tf_basic_ops:  
  def __init__(self, a = None, b = None):    
    self.a = a
    self.b = b    
    a = tf.placeholder(tf.int16)
    b = tf.placeholder(tf.int16)    
    self.input_dict = {a: self.a, b: self.b}  
  def tf_add(self):
    add = tf.add(self.a, self.b)
    with tf.Session() as sess:
      return sess.run(add, feed_dict=self.input_dict)
  def tf_mul(self):
    mul = tf.multiply(self.a , self.b)
    with tf.Session() as sess:
      return sess.run(mul, feed_dict=self.input_dict)
  def tf_mat_mul(self):
    mat_mul = tf.matmul(self.a , self.b)
    with tf.Session() as sess:
      return sess.run(mat_mul, feed_dict=self.input_dict)

In [0]:
tf_ops = tf_basic_ops(a=2, b = 3)

In [19]:
tf_ops.tf_add()


Out[19]:
5

In [20]:
tf_ops.tf_mul()


Out[20]:
6

In [0]:
tf_matrix_ops = tf_basic_ops(a=[[3., 3.]], b = [[2.],[2.]])

In [22]:
tf_matrix_ops.tf_mat_mul()


Out[22]:
array([[12.]], dtype=float32)

In [0]: