Objectives
In this post, you will:
- run python commands on python interpreter
- create a session to build a simple graph with tensorflow
- perform some mathematical operations on tensors in TensorFlow.
- understand how graphs are used to define computations and sessions are used to perform the computations defined in a graph.
- demonstrate how to use TensorFlow variables to store the results of intermediate computations.
Prerequisites
1- You should have the ability to connect with EC2 instance (local port) from your local host. If you don't know how to do that, read my blog on medium from here or on dev from here
2- you have to have an evironment on your terminal that have Python 3.6 and tensorflow library.I'll explain how to do this later.
Step 1
After you connect to your EC2 instance, enter the following command in your SSH shell to load the Python 3.6 TensorFlow virtual environment in the AMI(EC2 instance):
source activate Your_Environment_Name
For example, If you named your environment as tensorflow_p36
, then enter the following:
source activate tensorflow_p36
- The virtual environment sets all the environment variables required to use libraries and binaries required for TensorFlow.
- (tensorflow_p36) is prepended to your shell prompt to let you know you are using the TensorFlow Python 3.6 virtual environment.
Step 2
Start the interactive Python interpreter by entering:
python
Step 3
Enter the following import statements to import the TensorFlow module:
import tensorflow as tf
- The TensorFlow module is conventionally imported as tf.
- Don't pay attention to any warning messages you face just now, you can ignore them and still complete without any issue.
Step 4
To define a dataflow graph with two constant tensors as input and use the tf.add operation to produce the output, enter the following and press enter:
# Explicitly create a computation graph
graph = tf.Graph()
with graph.as_default():
# Declare one-dimensional tensors (vectors)
input1 = tf.constant([1.0, 2.0])
input2 = tf.constant([3.0, 4.0])
# Add the two tensors
output = tf.add(input1, input2)
The graph keeps track of all the inputs and operations you define so the results can be computed when you run a TensorFlow session with the graph.
Step 4
To print the graph to see that it stored the inputs and operations, add the following and press enter:
print(graph.get_operations())
After a few moments you will see the following output:
- The list of each operation corresponding to the two input constants and the addition operation are displayed.
- Default unique names are shown in single quotations.
- You can set the names when you declare the operation by passing a named argument called
name
.
Step 5
Evaluate the graph by creating a session with the graph and calling the output.eval()
function:
# Evaluate the graph in a session
with tf.Session(graph = graph):
result = output.eval()
print("result: ", result)
After a few moments you will see the following output:
- The output displays an information message letting you know that the session will run on the instance's graphics processing unit (GPU).
- The result is printed on the last line.
Step 7
When you are only using a single graph in a session, you can use the default graph as shown in the following example that repeats the computation using the default graph:
# Evaluate using the default graph
with tf.Session():
input1 = tf.constant([1.0, 2.0])
input2 = tf.constant([3.0, 4.0])
output = tf.add(input1, input2)
# Show the operations in the default graph
print(tf.get_default_graph().get_operations())
result = output.eval()
print("result: ", result)
- In the above code, the default graph is implicitly passed to all Tensorflow API functions.
- It can be convenient to use the default graph, but you may need multiple graphs when you develop separate training and test graphs for machine learning algorithms.
Step 8
To multiply a matrix by a vector add the following and press enter:
matmul_graph = tf.Graph()
with matmul_graph.as_default():
# Declare a 2x2 matrix and a 2x1 vector
matrix = tf.constant([[1.0, 2.0], [3.0, 4.0]])
vector = tf.constant([[1.0], [2.0]])
# Matrix multiply (matmul) the two tensors
output = tf.matmul(matrix, vector)
with tf.Session(graph = matmul_graph):
result = output.eval()
print(result)
After a few moments you will see the following output:
- You have now seen how to add vectors and multiply a matrix by a vector in TensorFlow.
- These two operations are the building blocks of several machine learning algorithms.
- The examples have only used constant inputs so far.
- TensorFlow supports using variables to allow tensors to be updated with different values as graph evaluation proceeds.
Step 9
Use variables to store the result of repeatedly multiplying a matrix by a vector as in the following annotated example:
# Evaluate a matrix-vector multiplication
var_graph = tf.Graph()
with var_graph.as_default():
# Declare a constant 2x2 matrix and a variable 2x1 vector
matrix = tf.constant([[1.0, 1.0], [1.0, 1.0]])
vector = tf.Variable([[1.0], [1.0]])
# Multiply the matrix and vector 4 times
for _ in range(4):
# Repeatedly update vector with the multiplication result
vector = tf.matmul(matrix, vector)
with tf.Session(graph = var_graph):
# Initialize the variables we defined above.
tf.global_variables_initializer().run()
result = vector.eval()
print(result)
After a few moments you will see the following output:
- The output value of vector shows that the value has been updated.
- This is similar to what you would expect using a variable in Python.
- One catch is that variables must be initialized with an explicit call.
-
tf.global_variables_initializer().run()
initializes all variables in a global variable collection. By default, every variable is added to the global variable collection.
Step 10
Exit the Python interpreter by entering:
exit()
In the next post, I'll explain how to use Jupyter Notebook to run your code.
Step 11
Deactivate the TensorFlow virtual environment:
source deactivate
- The tensorflow_p36 prefix(Environment Name) of the shell prompt is no longer displayed.
- The Jupyter notebook server needs to be started outside of any virtual environment to be able to discover the available virtual environments.
Top comments (0)