What is TensorFlow?

TensorFlow is a Google open source software library for numerical computation using data flow graphs; that is, an open source machine learning library focusing on Deep Learning. Based loosely on neural networks, TensorFlow is the culmination of the work of Google's Brain Team researchers and engineers to apply Deep Learning to Google products and build production models for various Google teams including (but not limited to) search, photos, and speech.

Built on C++ with a Python interface, it has quickly become one of the most popular Deep Learning projects in a short amount of time. The following screenshot denotes a Google Trends comparison between four popular deep learning libraries; note the spike around November 8th - 14th, 2015 (when TensorFlow was announced) and the rapid rise over the last year (this snapshot was taken late December 2016):

What is TensorFlow?

Another way to measure the popularity of TensorFlow is to note that TensorFlow is the most popular machine learning framework on GitHub per http://www.theverge.com/2016/4/13/11420144/google-machine-learning-tensorflow-upgrade. Note that TensorFlow was only released in November 2015 and in two months it had already become the most popular forked ML GitHub repository. In the following diagram, you can review the GitHub Repositories Created in 2015 (Interactive Visualization) per http://donnemartin.com/viz/pages/2015:

What is TensorFlow?

As noted previously, TensorFlow performs numerical computation using data flow graphs. When thinking about graph (as per the previous chapter on GraphFrames), the node (or vertices) of this graph represent mathematical operations while the graph edges represent the multidimensional arrays (that is, tensors) that communicate between the different nodes (that is, mathematical operations).

Referring to the following diagram, t 1 is a 2x3 matrix while t 2 is a 3x2 matrix; these are the tensors (or edges of the tensor graph). The node is the mathematical operations represented as op 1:

What is TensorFlow?

In this example, op 1 is a matrix multiplication operation represented by the following diagram, though this could be any of the many mathematics operations available in TensorFlow:

What is TensorFlow?

Together, to perform your numerical computations within the graph, there is a flow of multidimensional arrays (that is, tensors) between the mathematical operations (nodes) - that is, the flow of tensors, or TensorFlow.

To better understand how TensorFlow works, let's start by installing TensorFlow within your Python environment (initially sans Spark). For the full instructions, please refer to TensorFlow | Download and Setup: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html.

For this chapter, let's focus on the Python pip package management system installation on Linux or Mac OS.

Installing Pip

Ensure that you have installed pip; if have not, please use the following commands to install the Python package installation manager for Ubuntu/Linux:

# Ubuntu/Linux 64-bit 
$ sudo apt-get install python-pip python-dev

For Mac OS, you would use the following commands:

# macOS 
$ sudo easy_install pip 
$ sudo easy_install --upgrade six

Note, for Ubuntu/Linux, you may also want to upgrade pip as the pip within the Ubuntu repository is old and may not be compatible with newer packages. To do this, you can run the command:

# Ubuntu/Linux pip upgrade
$ pip install --upgrade pip 

Installing TensorFlow

To install TensorFlow (with pip already installed), you only need to execute the following command:

$ pip install tensorflow

If you have a computer that has GPU support, you can instead use the following command:

$ pip install tensorflow-gpu

Note that if the preceding command does not work, there are specific instructions to install TensorFlow with GPU support based on your Python version (that is, 2.7, 3.4, or 3.5) and GPU support.

For example, if I wanted to install TensorFlow on Python 2.7 with GPU enabled on Mac OS, execute the following commands:

# macOS, GPU enabled, Python 2.7: 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc1-py2-none-any.whl
# Python 2 
$ sudo pip install --upgrade $TF_BINARY_URL

Note

Please refer to https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html for the latest installation instructions.

Matrix multiplication using constants

To better describe tensors and how TensorFlow works, let's start with a matrix multiplication calculation involving two constants. As noted in the following diagram, we have c 1 (3x1 matrix) and c 2 (1x3 matrix), where the operation (op 1) is a matrix multiplication:

Matrix multiplication using constants

We will now define c 1 (1x3 matrix) and c 2 (3x1 matrix) using the following code:

# Import TensorFlow
import tensorflow as tf
# Setup the matrix
#   c1: 1x3 matrix
#   c2: 3x1 matrix
c1 = tf.constant([[3., 2., 1.]])
c2 = tf.constant([[-1.], [2.], [1.]])

Now that we have our constants, let's run our matrix multiplication using the following code. Within the context of a TensorFlow graph, recall that the nodes in the graph are called operations (or ops). The following matrix multiplication is the ops, while the two matrices (c 1, c 2) are the tensors (typed multi-dimensional array). An op takes zero or more tensors as its input, performs the operation such as a mathematical calculation, with the output being zero or more tensors in the form of numpy ndarray objects (http://www.numpy.org/) or tensorflow::Tensor interfaces in C, C++:

# m3: matrix multiplication (m1 x m3)
mp = tf.matmul(c1, c2)

Now that this TensorFlow graph has been established, execution of this operation (for example, in this case, the matrix multiplication) is done within the context of a session; the session places the graph ops into the CPU or GPU (that is, devices) to be executed:

# Launch the default graph
s = tf.Session()

# run: Execute the ops in graph
r = s.run(mp)
print(r)

With the output being:

# [[ 2.]]

Once you have completed your operations, you can close the session:

# Close the Session when completed
s.close()

Matrix multiplication using placeholders

Now we will perform the same task as before, except this time, we will use tensors instead of constants. As noted in the following diagram, we will start off with two matrices (m1: 3x1, m2: 1x3) using the same values as in the previous section:

Matrix multiplication using placeholders

Within TensorFlow, we will use placeholder to define our two tensors as per the following code snippet:

# Setup placeholder for your model
#   t1: placeholder tensor
#   t2: placeholder tensor
t1 = tf.placeholder(tf.float32)
t2 = tf.placeholder(tf.float32)

# t3: matrix multiplication (m1 x m3)
tp = tf.matmul(t1, t2)

The advantage of this approach is that, with placeholders you can use the same operations (that is, in this case, the matrix multiplication) with tensors of different sizes and shape (provided they meet the criteria of the operation). Like the operations in the previous section, let's define two matrices and execute the graph (with a simplified session execution).

Running the model

The following code snippet is similar to the code snippet in the previous section, except that it now uses placeholders instead of constants:

# Define input matrices
m1 = [[3., 2., 1.]]
m2 = [[-1.], [2.], [1.]]

# Execute the graph within a session
with tf.Session() as s:
     print(s.run([tp], feed_dict={t1:m1, t2:m2}))

With the output being both the value, as well as the data type:

[array([[ 2.]], dtype=float32)]

Running another model

Now that we have a graph (albeit a simple one) using placeholders, we can use different tensors to perform the same operation using different input matrices. As noted in the following figure, we have m1 (4x1) and m2 (1x4):

Running another model

Because we're using placeholders, we can easily reuse the same graph within a new session using new input:

# setup input matrices
m1 = [[3., 2., 1., 0.]]
m2 = [[-5.], [-4.], [-3.], [-2.]]
# Execute the graph within a session
with tf.Session() as s:
     print(s.run([tp], feed_dict={t1:m1, t2:m2}))

With the output being:

[array([[-26.]], dtype=float32)]

Discussion

As noted previously, TensorFlow provides users with the ability to perform deep learning using Python libraries by representing computations as graphs where the tensors represent the data (edges of the graph) and operations represent what is to be executed (for example, mathematical computations) (vertices of the graph).

For more information, please refer to:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset