Sunday, June 2, 2019

Pandas - 49 Programming with TensorFlow

Last post was an overview of TensorFlow framework and now is the time to start using this framework. In order to install TensorFlow, open command prompt and use the following pip command:

pip3 install tensorflow

The following window shows the progress and successful installation:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\python>pip3 install tensorflow
Collecting tensorflow
  Downloading https://files.pythonhosted.org/packages/bf/58/34bfa8fa17f863333611
72b3b502e805195180f19a7496ad0f6149138d55/tensorflow-1.13.1-cp36-cp36m-win_amd64.
whl (63.1MB)
    100% |████████████████████████████████| 63.1MB 12kB/s

:
:
:
Successfully installed absl-py-0.7.1 astor-0.8.0 gast-0.2.2 grpcio-1.20.1 h5py-2
.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.1.1 mock-3.0.
5 numpy-1.16.3 protobuf-3.7.1 six-1.12.0 tensorboard-1.13.1 tensorflow-1.13.1 te
nsorflow-estimator-1.13.0 termcolor-1.1.0 werkzeug-0.15.4 wheel-0.33.4
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.

C:\Users\python>


On Windows systems, we can use Anaconda to install the package:

conda install tensorflow

Once TensorFlow is installed, we can start programming with this library by importing the library in our programs:

import tensorflow as tf

Before starting to program it is important to understand the internal operation of TensorFlow, including how it interprets the commands in Python and how it is executed internally. TensorFlow works through the concept of model and sessions, which define the structure of a program with a certain sequence of commands.

At the base of any TensorFlow project there is a model that includes a whole series of variables to be taken into consideration and that will define the system. Variables can be defined directly or parameterized through mathematical expressions on constants.

Let's understand this through a program:

import tensorflow as tf

c = tf.constant(2,name='c')
x = tf.Variable(3,name='x')
y = tf.Variable(c*x,name='y')

print(x)
print(y)


The output of the program is shown below:


<tf.Variable 'x:0' shape=() dtype=int32_ref>
<tf.Variable 'y:0' shape=() dtype=int32_ref>
------------------
(program exited with code: 0)

Press any key to continue . . . 


When we try to see the internal value of y (we expect a value of 6) with the print() function, we  see that it gives us the object and not the value. The values can be accessed via sessions as shown in the following program:

import tensorflow as tf

c = tf.constant(2,name='c')
x = tf.Variable(3,name='x')
y = tf.Variable(c*x,name='y')

X = tf.placeholder("int32")
X = tf.placeholder("int32")

model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    print(session.run(y))



For the variables directly involved in the calculation of the deep learning method, placeholders are used, i.e. those tensors directly involved in the flow of data and in the processing of each single neuron. Placeholders allow us to build the graph corresponding to the neural network, and to create operations inside without absolutely knowing the data to be calculated.

In fact, we can build the structure of the graph (and also the neural network). In practical cases, given a training set consisting of the value to be analyzed x (a tensor) and an expected value y (a tensor), we will define two placeholders xey, i.e., two tensors that will contain the values processed by the data for the whole neural network.

Thus in our program we define two placeholders that contain integers with the tf.placeholder() function. After we defined all the variables involved, we perform the appropriate processing and initialize the whole model with the tf.global_variables_initializer() function.

Now we have a model initialized and loaded into memory, we need to start doing the calculations, but to do that we need to communicate with the TensorFlow runtime system. For this purpose a TensorFlow session is created, during which we can launch a series of commands to interact with the underlying graph corresponding to the model we have created.

We can create a new session with the tf.Session() constructor. Within a session, we can perform the calculations and receive the values of the variables obtained as results, i.e., we can check the status of the graph during processing.

We know that the operation of TensorFlow is based on the creation of an internal graph structure, in which the nodes are able to perform processing on the flow of data inside tensors that follow the connections of the graph. So when we start a session, in practice we do nothing but instantiate this graph.

A session has two main methods:

• session.extend() allows you to make changes to the graph during the calculation, such as adding new nodes or connections.

• session.run() launches the execution of the graph and allows you to obtain the results in output.

Since several operations are carried out within the same session, it is preferred to use the construct with: with all calls to methods inherent to it.

In our case, we simply wanted to see the values of the variables defined in the model and print them on the terminal so we used session.run().

The output of the program is shown below:

6
------------------
(program exited with code: 0)

Press any key to continue . . . 


Thus we can see within the session, we can access the values of the Data Flow Graph, including the y variable that we previously defined.

The basic element of the TensorFlow library is the tensor which is the data that follow the flow within the Data Flow Graph. A tensor is identified by three parameters:

• rank—Dimension of the tensor (a matrix has rank 2, a vector has rank 1)
• shape—Number of rows and columns (e.g. (3.3) is a 3x3 matrix)
• type—Type of tensor elements.

Tensors can be called as multidimensional arrays which can be defined as shown below:

import tensorflow as tf
import numpy as np

md_array= np.arange(9).reshape((3,3))

print('Created multidimensional array\n')
print(md_array)

tensor = tf.convert_to_tensor(t, dtype=tf.int32)


with tf.Session() as session:
    print('\nContent of the sensor\n')
    print(session.run(tensor))


Our program first defines a multidimensional array and print it. Then we convert this multidimensional array into a TensorFlow tensor using the tf.convert_to_tensor() function, which takes two parameters. The first parameter is the array md_array that we want to convert and the second is the type of data we want to convert it to, in our case int32. Finally we see the content of the sensor. To do so we create a TensorFlow session and run it. The output of the program is shown below:

Created multidimensional array

[[0 1 2]
 [3 4 5]
 [6 7 8]]

Content of the sensor

[[0 1 2]
 [3 4 5]
 [6 7 8]]
------------------
(program exited with code: 0)

Press any key to continue . . .


From the output we can see, we have a tensor containing the same values and the same dimensions as the multidimensional array defined with NumPy. This approach is very useful for calculating deep learning, since many input values are in the form of NumPy arrays.

We can built tensors directly from TensorFlow, without using the NumPy library. There are a number of functions that make it possible to enhance the tensors quickly and easily as shown in the program below:

import tensorflow as tf
import numpy as np

tensor_with_zeros = tf.zeros((3,3),'float64')

with tf.Session() as session:
    print('\nA tensor with all the values zero\n')
    print(session.run(tensor_with_zeros))


tensor_with_ones = tf.ones((3,3),'float64')

with tf.Session() as session:
    print('\nA tensor with all the values one\n')
    print(session.run(tensor_with_zeros))

tensor_with_random_value = tf.random_uniform((3, 3), minval=0, maxval=1,dtype=tf.float32)

with tf.Session() as session:
    print('\nA tensor with all random values\n')
    print(session.run(tensor_with_random_value))
   
norm = tf.random_normal((3, 3), mean=0, stddev=3)

with tf.Session() as session:
    print('\nA tensor of 3x3 size with mean 0 and standarddeviation of 3\n')
    print(session.run(norm))



In the above program when we want to initialize a tensor with all the values zero, we use the tf.zeros() method. To initialize a tensor with all the values of 1, we use the tf.ones() method. To create a tensor containing random values, which follow a uniform distribution (all the values within a range are equally likely to exit), we use the tf.random_uniform() function. If we want to create a tensor containing values that follow a normal distribution with a choice of mean and standard deviation we can use the tf.random_normal() function. The output of the program is shown below:


A tensor with all the values zero

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

A tensor with all the values one

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

A tensor with all random values

[[0.16982412 0.3028159  0.6906557 ]
 [0.3437289  0.6935015  0.42577183]
 [0.49587762 0.98294985 0.3181746 ]]

A tensor of 3x3 size with mean 0 and standarddeviation of 3

[[ 1.7878513  -1.790443   -1.7801567 ]
 [ 3.2362757   2.2292213   0.6370112 ]
 [ 1.6167108  -0.31830445 -2.863762  ]]


------------------
(program exited with code: 0)

Press any key to continue . . . 


The next step after defining tensors is to carry out operations on them. The most common mathematical calculations performed on the tensors are sum and multiplication between tensors. Let's define two tensors and then perform operations between them. See the following program:

import tensorflow as tf
import numpy as np

t1 = tf.random_uniform((3, 3), minval=0, maxval=1, dtype=tf.float32)
t2 = tf.random_uniform((3, 3), minval=0, maxval=1, dtype=tf.float32)

with tf.Session() as session:
    print('\ntensor t1 with all the values\n')
    print('t1 = ',session.run(t1))
    print('\ntensor t2 with all the values\n')
    print('t2 = ',session.run(t2))

sum = tf.add(t1,t2)
mul = tf.matmul(t1,t2)
det = tf.matrix_determinant(t1)
with tf.Session() as session:
    print('\nSum of tensors\n')
    print('sum =', session.run(sum))
    print('\nMultiplication of tensors\n')
    print('mul =', session.run(mul))
    print('\nMatrix_determinant\n')
    print('det =', session.run(det))


The output of the program is shown below:

tensor t1 with all the values

t1 =  [[0.18523002 0.00788283 0.8860909 ]
 [0.07531261 0.59244144 0.14576197]
 [0.9674524  0.29524982 0.09114099]]

tensor t2 with all the values

t2 =  [[0.63234806 0.08789527 0.5247965 ]
 [0.8434621  0.30624616 0.41807485]
 [0.03678679 0.01598358 0.32429242]]

Sum of tensors

sum = [[1.2564764  0.9734479  0.79735017]
 [1.6787658  0.7732457  0.69099784]
 [0.5368959  0.31615496 1.2494062 ]]

Multiplication of tensors

mul = [[0.4633578  0.32817203 0.8197809 ]
 [0.6384865  0.44700155 1.0735676 ]
 [1.1651275  0.6197158  1.0699061 ]]

Matrix_determinant

det = 0.07747876
------------------
(program exited with code: 0)

Press any key to continue . . .



Here I am ending today's discussion wherein we covered the basics of Programming with TensorFlow framework. In the next post I'll focus on how to develop neural networks with TensorFlow . So till we meet again keep learning and practicing Python as Python is easy to learn!

Share:

0 comments:

Post a Comment