Learn Methods to Install and Use TensorFlow in Ubuntu

How to install and use tensorflow in ubuntu

TensorFlow is an open-source deep learning software system built by Google to train neural networks.TensorFlow can perform image recognition, human language audio recognition and solving partial differential equations.

We will install TensorFlow and all of the packages required to use TensorFlow in a Python virtual environment on Ubuntu. This isolates your TensorFlow environment from other Python programs on the same machine.

Step 1) Installing TensorFlow

we are going to create a virtual environment and install TensorFlow.
First, create a project directory called tf-test:

$ mkdir ~/tf-test

And navigating to our newly created tf-test directory:

$ cd ~/tf-test

Now we are going to create a new virtual environment tensorflow-venv. Run the following command to create the environment:

$ python3 -m venv tensorflow-venv

Run the command below to activate the environment.

$ source tensorflow-venv/bin/activate

Run the following command to install and upgrade to the newest version of TensorFlow available in PyPi.

(tensorflow-venv)$ pip install --upgrade tensorflow
Output

Collecting tensorflow
  Downloading tensorflow-1.4.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
    100% |████████████████████████████████| 39.3MB 35kB/s

...

Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30

Now we will give examples using tensorflow.

Example 1) Simple Constants

To create a simple constant with Tensorflow, which TF stores as a tensor object:

>>> import tensorflow as tf

We will create string constant as follow:

>>> hello = tf.constant('Hello World')

To know the type of object hello:

>>> type(hello)
    output
    tensorflow.python.framework.ops.Tensor

We will create integer constant as follow:

>>> x = tf.constant(300)
>>> type(x)
    output
    tensorflow.python.framework.ops.Tensor

Example 2) Running Sessions

Now we can create a TensorFlow Session, which is a class for running TensorFlow operations.

>>> sess = tf.Session()
>>> sess.run(hello)
    output
    b'Hello World'

b for unicode indication.

>>>type(sess.run(hello))
    output
    bytes

for the second constant.

>>> sess.run(x)
    output
    300
>>>type(sess.run(x))
    output
    numpy.int32

Example 3) Operations

We can line up multiple Tensorflow operations in to be run during a session:

    >>> x = tf.constant(20)
    >>> y = tf.constant(30)
    >>> with tf.Session() as sess:
            print('Operations with Constants')
            print('Addition',sess.run(x+y))
            print('Subtraction',sess.run(x-y))
            print('Multiplication',sess.run(x*y))
            print('Division',sess.run(x/y))
    output
    Operations with Constants
    Addition 50
    Subtraction -10
    Multiplication 600
    Division 0.666666666667

Example 4) Placeholder

We may not always have the constants right away, and we may be waiting for a constant to appear after a cycle of operations. tf.placeholder is a tool for this. It inserts a placeholder for a tensor that will be always fed.

Note: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument to Session.run().

    >>> x = tf.placeholder(tf.int32)
    >>> y = tf.placeholder(tf.int32)

We are going to define operations.

    >>> add = tf.add(x,y)
    >>> sub = tf.sub(x,y)
    >>> mul = tf.mul(x,y)

We are going to create dictionary to make operations on it.

>>> d = {x:20,y:30}
    >>> with tf.Session() as sess:
            print('Operations with Constants')
            print('Addition',sess.run(add,feed_dict=d))
            print('Subtraction',sess.run(sub,feed_dict=d))
            print('Multiplication',sess.run(mul,feed_dict=d))
    output
    Operations with Constants
    Addition 50
    Subtraction -10
    Multiplication 600

Example 5) Matrix Multiplication

Now we are going to use matrix multiplication. First we need to create the matrices:

>>> import numpy as np
    >>> a = np.array([[5.0,5.0]])
    >>> b = np.array([[2.0],[2.0]])
>>> print(a)
    output
    array([[ 5.,  5.]])
>>> print(a.shape)
    output
    (1, 2)
>>> print(b)
    output
    array([[ 2.],
       [ 2.]])
>>> print(b.shape)
    output
    (2, 1)

Now we are going to create constant tensor objects.

    >>> mat1 = tf.constant(a)
    >>> mat2 = tf.constant(b)

The matrix multiplication operation:

>>> matrix_multi = tf.matmul(mat1,mat2)

Now we run the session to perform the Operation:

    >>> with tf.Session() as sess:
            result = sess.run(matrix_multi)
            print(result)
    output
    [[ 20.]]

Thanks for reading and please leave your comments below.

Suggested Reading

Ahmed Abdalhamid 11:12 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.