Basic Concepts and Manipulations with TensorFlow

by Sergey KovalevFebruary 19, 2016
This blog post overviews the core TensorFlow entities—a graph, an operation, a tensor, and a session—to exemplify common computations made by the library.

TensorFlow is designed to make distributed machine and deep learning easy for everyone, but using it does require understanding of some general principles and algorithms. In this tutorial, we take a closer look at the library’s main concepts and try to do some basic operations to get you started.

 

Understanding TensorFlow

TensorFlow is based on the concept of the data flow graph. The nodes of this graph represent operations. The edges are tensors. In terms of TensorFlow, a tensor is just a multi-dimensional array. Each data flow graph computation runs within a session on a CPU or GPU.

Taking into account the popularity of Python among data scientists, TensorFlow exposes a Python-based API, where tensors are NumPy ndarray entities. On the low level, the library relies on highly optimized C++ code and supports a native C and C++ API. Below, you can see an example of a TensorFlow graph (the image is taken from tensorflow.org).

So, the main TensorFlow entities are:

  1. Graph. As we mentioned, TensorFlow computations can be represented as data flow graphs. Each graph is built as a set of Operation objects.
  2. Operation. Each Operation object represents a graph node, which is a unit of computation (addition, multiplication, or something more complex) performed on a tensor flow. It takes tensors as input and produces a tensor as output.
  3. Tensor. Tensors can be represented as edges of a data flow graph. They do not represent or hold any particular value produced by an application of an operation. Instead, they define the type of this value and the means by which this value should be calculated during the session.
  4. Session. This is an entity that represents an environment for running calculations on the data flow graph.

 

Basic operations

Now, it’s time to try TensorFlow in action. Here are some basic computation examples that help to better understand the main concepts.

# Import TensorFlow.
import tensorflow as tf

# Basic operations with some constants

# Create constants a and b

# Build a data flow graph
a = tf.constant(10)
b = tf.constant(5)

# Create a session to evaluate the symbolic expression
sess = tf.Session() 

# Trigger an evaluation of the data flow graph.
print "Constants addition: %i" % sess.run(a + b)
print "Constants  multiplication: %i" % sess.run(a * b)

# Close the session
sess.close()

Output:

Constants addition: 15
Constants multiplication: 50
# Perform the same operations on constant matrices

# Build a dataflow graph
c = tf.constant([[2.0, 2.0], [5.0, 4.0]])
d = tf.constant([[1.0, 2.0], [1.0, 2.0]])
e = tf.matmul(c, d)

# Construct a `Session` to execute the graph
sess = tf.Session()

# Execute the graph and print the resulting matrix
print sess.run(e)

# Close the session
sess.close()

Output:

[[  4.   8.]
[  9.  18.]]
# Operations with variables as graph input flow
a = tf.placeholder(tf.types.int16)
b = tf.placeholder(tf.types.int16)

# Apply some operations
add = tf.add(a, b)
mul = tf.mul(a, b)

# Construct a `Session` to execute the graph.
sess = tf.Session()

# Execute the graph with variable input
print "Addition input variables: %i" % sess.run(add, feed_dict={a: 5, b: 8})
print "Multiplication input variables: %i" % sess.run(mul, feed_dict={a: 5, b: 8})

# Close the session
sess.close()

Output:

Addition input variables: 13
Multiplication input variables: 40
# Operations with matrices as graph input flow
a = tf.placeholder(tf.types.float32, shape=(2, 4))
b = tf.placeholder(tf.types.float32, shape=(4, 2))

# Apply multiplication
mul = tf.matmul(a, b)

# Construct a `Session` to execute the graph
sess = tf.Session()

# Import Numpy
import numpy as np

# Create matrices with defined dimensions and fill them with random values
rand_array_a = np.random.rand(2, 4)
rand_array_b = np.random.rand(4, 2)

# Execute the graph and print the resulting matrix
print sess.run(mul, feed_dict={a: rand_array_a, b: rand_array_b})

# Close the session
sess.close()

Output:

[[ 1.32082355  0.97404259]
[ 1.06961715  1.15667367]]

 

More on TensorFlow?

In our other tutorials, we cover more advanced topics—such as training a model for different types of regression (linear and logistic/softmax regressions), k-means clustering, etc.

You can also download our TensorFlow cheat sheet to learn how to get started with the library or attend our ML training in your city.

 

Further reading

 


The tutorial was created by Sergey Kovalev; edited and published by Sophia Turol and Alex Khizhniak.