TensorFlow terminologies – recap

In this section, we will provide an overview of the TensorFlow library as well as the structure of a basic TensorFlow application. TensorFlow is an open source library for creating large-scale machine learning applications; it can model computations on a wide variety of hardware, ranging from android devices to heterogeneous multi-gpu systems.

TensorFlow uses a special structure in order to execute code on different devices such as CPUs and GPUs. Computations are defined as a graph and each graph is made up of operations, also known as ops, so whenever we work with TensorFlow, we define the series of operations in a graph.

To run these operations, we need to launch the graph into a session. The session translates the operations and passes them to a device for execution.

For example, the following image represents a graph in TensorFlow. W, x, and b are tensors over the edges of this graph. MatMul is an operation over the tensors W and x; after that, Add is called and we add the result of the previous operator with b. The resultant tensors of each operation cross the next one until the end, where it's possible to get the desired result.

    
Figure 9: Sample TensorFlow computational graph

In order to use TensorFlow, we need to import the library; we'll give it the name tf so that we can access a module by writing tf dot and then the module's name:

import tensorflow as tf

To create our first graph, we will start by using source operations, which do not require any input. These source operations or source ops will pass their information to other operations, which will actually run computations.

Let's create two source operations that will output numbers. We will define them as A and B, which you can see in the following piece of code:

A = tf.constant([2])
B = tf.constant([3])

After that, we'll define a simple computational operation tf.add(), used to sum two elements. You can also use C = A + B, as shown in this code:

C = tf.add(A,B)
#C = A + B is also a way to define the sum of the terms

Since graphs need to be executed in the context of a session, we need to create a session object:

session = tf.Session()

To watch the graph, let's run the session to get the result from the previously defined C operation:

result = session.run(C)
print(result)
Output:
[5]

You're probably thinking that it was a lot of work just to add two numbers together, but it's extremely important that you understand the basic structure of TensorFlow. Once you do so, you can define any computations that you want; again, TensorFlow's structure allows it to handle computations on different devices (CPU or GPU), and even in clusters. If you want to learn more about this, you can run the method tf.device().

Also feel free to experiment with the structure of TensorFlow in order to get a better idea of how it works. If you want a list of all the mathematical operations that TensorFlow supports, you can check out the documentation.

By now, you should understand the structure of TensorFlow and how to create a basic applications.

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

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