Tensorflow.

TensorFlow™ is an open source software library for high performance numerical computation. Its flexible architecture allows easy deployment of computation across a variety of platforms (CPUs, GPUs, TPUs), and from desktops to clusters of servers to mobile and edge devices. Originally developed by researchers and engineers from the Google Brain team within Google’s AI organization, it comes with strong support for machine learning and deep learning and the flexible numerical computation core is used across many other scientific domains.

Importing Tensorflow

To use TensorFlow, we need to import the library called tensorflow. We imported it with the name "tf", so the modules can be accessed by tf.moduleName
In [2]:
import tensorflow as tf

Sessions

Sessions are contex for creating a graph inside tensorflow. The graphs need session for the computaion of the values
In [3]:
a = tf.constant(12)
b = tf.constant(13)
c = tf.multiply(12, 13)
In [4]:
with tf.Session() as session:
    print(session.run(c))
156

Matrix Multiplications

As we all know, most of the images are just matrix tables, matrix tables of pixel values. So, most of the computer vision task relies on matrix multiplications of the matrices.
In [9]:
matrixA = tf.constant([[3, 4], [4, 5]])
matrixB = tf.constant([[5, 6], [2, 3]])
matrixC = tf.matmul(matrixA, matrixB)

# Don't get confused with tf.multiply and tf.matmul as the first one does element wise multiplications 
# and the latter one gives the dot product of the two matrices
In [10]:
with tf.Session() as session:
    print(session.run(matrixC))
[[23 30]
 [30 39]]

Variables

A TensorFlow variable is the best way to represent shared, persistent state manipulated by your program. Variables are manipulated via the tf.Variable class. A tf.Variable represents a tensor whose value can be changed by running ops on it.
In [11]:
variableA = tf.Variable(0)
variableB = tf.constant(5)
activity1 = tf.assign(variableA, variableB)
In [13]:
with tf.Session() as session:
    # To be able to use variables in a computation graph it is necessary to initialize them before 
    # running the graph in a session.
    session.run(tf.global_variables_initializer())
    session.run(activity1)
    print(session.run(variableA))
5

Placeholders

A placeholder is simply a variable that we will assign data to at a later date. Unlike variables, the placeholders get's their data from outside of the computational graph.
In [14]:
placeholder1 = tf.placeholder(dtype=tf.float32)
In [15]:
with tf.Session() as session:
    print(session.run(placeholder1, feed_dict={placeholder1: 5}))
5.0
In [17]:
placeholder2 = tf.placeholder(dtype=tf.float32)
placeholder3 = tf.placeholder(dtype=tf.float32)
activity2 = tf.pow(placeholder2, placeholder3)
In [19]:
with tf.Session() as session:
    activity3 = session.run(activity2, feed_dict={placeholder2: 2, placeholder3: 3})
    print(activity3)
8.0

Thanks for completing this lesson!


Notebook created by: Sulabh Shrestha </h4>
Github: CodeExponent
Copyright © 2018 [CodeExponent]