Tensorflow Tutorial PDF
Tensorflow Tutorial PDF
Tensorflow Tutorial PDF
i
TensorFlow
Audience
This tutorial has been prepared for python developers who focus on research and
development with various machine learning and deep learning algorithms. The aim of this
tutorial is to describe all TensorFlow objects and methods.
Prerequisites
Before proceeding with this tutorial, you need to have a basic knowledge of any Python
programming language. Knowledge of artificial intelligence concepts will be a plus point.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
TensorFlow
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Vector ............................................................................................................................................................ 11
Deep Learning................................................................................................................................................ 15
6. TensorFlow — Basics............................................................................................................................... 19
ii
TensorFlow
Word2vec ...................................................................................................................................................... 38
iii
1. TensorFlow — Introduction TensorFlow
https://www.tensorflow.org/
TensorFlow is also called a “Google” product. It includes a variety of machine learning and
deep learning algorithms. TensorFlow can train and run deep neural networks for
1
TensorFlow
2
2. TensorFlow — Installation TensorFlow
Step 2: A user can pick up any mechanism to install TensorFlow in the system. We
recommend “pip” and “Anaconda”. Pip is a command used for executing and installing
modules in Python.
3
TensorFlow
After successful installation, check in command prompt through “conda” command. The
execution of command is displayed below:
4
TensorFlow
activate tensorflow
Step 5: Use pip to install “Tensorflow” in the system. The command used for installation
is mentioned as below:
5
TensorFlow
And,
6
TensorFlow
Following example helps us understand the basic program creation “Hello World” in
TensorFlow.
7
3. TensorFlow — Understanding Artificial TensorFlow
Intelligence
Machine learning is the branch of artificial intelligence, which deals with systems and
algorithms that can learn any new data and data patterns.
Let us focus on the Venn diagram mentioned below for understanding machine learning
and deep learning concepts.
Machine learning includes a section of machine learning and deep learning is a part of
machine learning. The ability of program which follows machine learning concepts is to
improve its performance of observed data. The main motive of data transformation is to
improve its knowledge in order to achieve better results in the future, provide output closer
to the desired output for that particular system. Machine learning includes “pattern
recognition” which includes the ability to recognize the patterns in data.
Supervised training
Unsupervised training
8
TensorFlow
Supervised Learning
Supervised learning or supervised training includes a procedure where the training set is
given as input to the system wherein, each example is labeled with a desired output value.
The training in this type is performed using minimization of a particular loss function, which
represents the output error with respect to the desired output system.
After completion of training, the accuracy of each model is measured with respect to
disjoint examples from training set, also called the validation set.
The best example to illustrate “Supervised learning” is with a bunch of photos given with
information included in them. Here, the user can train a model to recognize new photos.
Unsupervised Learning
In unsupervised learning or unsupervised training, include training examples, which are
not labeled by the system to which class they belong. The system looks for the data, which
share common characteristics, and changes them based on internal knowledge features.
This type of learning algorithms are basically used in clustering problems.
9
TensorFlow
The best example to illustrate “Unsupervised learning” is with a bunch of photos with no
information included and user trains model with classification and clustering. This type of
training algorithm works with assumptions as no information is given.
10
4. TensorFlow — Mathematical Foundations TensorFlow
Vector
An array of numbers, which is either continuous or discrete, is defined as a vector. Machine
learning algorithms deal with fixed length vectors for better output generation.
Machine learning algorithms deal with multidimensional data so vectors play a crucial role.
11
TensorFlow
Scalar
Scalar can be defined as one-dimensional vector. Scalars are those, which include only
magnitude and no direction. With scalars, we are only concerned with the magnitude.
Matrix
Matrix can be defined as multi-dimensional arrays, which are arranged in the format of
rows and columns. The size of matrix is defined by row length and column length. Following
figure shows the representation of any specified matrix.
Consider the matrix with “m” rows and “n” columns as mentioned above, the matrix
representation will be specified as “m*n matrix” which defined the length of matrix as well.
Mathematical Computations
In this section, we will learn about the different Mathematical Computations in TensorFlow.
Addition of matrices
Addition of two or more matrices is possible if the matrices are of the same dimension.
The addition implies addition of each element as per the given position.
12
TensorFlow
Subtraction of matrices
The subtraction of matrices operates in similar fashion like the addition of two matrices.
The user can subtract two matrices provided the dimensions are equal.
Multiplication of matrices
For two matrices A m*n and B p*q to be multipliable, n should be equal to p. The resulting
matrix is:
C m*q
Transpose of matrix
The transpose of a matrix A, m*n is generally represented by AT (transpose) n*m and is
obtained by transposing the column vectors as row vectors.
13
TensorFlow
14
5. TensorFlow — Machine Learning and Deep TensorFlow
Learning
Artificial Intelligence is one of the most popular trends of recent times. Machine learning
and deep learning constitute artificial intelligence. The Venn diagram shown below explains
the relationship of machine learning and deep learning:
Machine Learning
Machine learning is the art of science of getting computers to act as per the algorithms
designed and programmed. Many researchers think machine learning is the best way to
make progress towards human-level AI. Machine learning includes the following types of
patterns:
Deep Learning
Deep learning is a subfield of machine learning where concerned algorithms are inspired
by the structure and function of the brain called artificial neural networks.
All the value today of deep learning is through supervised learning or learning from labelled
data and algorithms.
Each algorithm in deep learning goes through the same process. It includes a hierarchy of
nonlinear transformation of input that can be used to generate a statistical model as
output.
Consider the following steps that define the Machine Learning process:
15
TensorFlow
Amount of data
Machine learning works with large amounts of data. It is useful for small amounts of data
too. Deep learning on the other hand works efficiently if the amount of data increases
rapidly. The following diagram shows the working of machine learning and deep learning
with the amount of data:
Hardware Dependencies
Deep learning algorithms are designed to heavily depend on high-end machines unlike the
traditional machine learning algorithms. Deep learning algorithms perform a number of
matrix multiplication operations, which require a large amount of hardware support.
16
TensorFlow
Feature Engineering
Feature engineering is the process of putting domain knowledge into specified features to
reduce the complexity of data and make patterns that are visible to learning algorithms it
works.
Example: Traditional machine learning patterns focus on pixels and other attributes
needed for feature engineering process. Deep learning algorithms focus on high-level
features from data. It reduces the task of developing new feature extractor of every new
problem.
Execution Time
Execution time is the amount of time required to train an algorithm. Deep learning requires
a lot of time to train as it includes a lot of parameters which takes a longer time than
usual. Machine learning algorithm comparatively requires less execution time.
Interpretability
Interpretability is the major factor for comparison of machine learning and deep learning
algorithms. The main reason is that deep learning is still given a second thought before its
usage in industry.
Computer vision which is used for facial recognition and attendance mark through
fingerprints or vehicle identification through number plate.
Information Retrieval from search engines like text search for image search.
Natural language processing for applications like photo tagging. The best example
to explain this scenario is used in Facebook.
Online Advertising.
17
TensorFlow
Future Trends
With the increasing trend of using data science and machine learning in the
industry, it will become important for each organization to inculcate machine
learning in their businesses.
Deep learning is gaining more importance than machine learning. Deep learning is
proving to be one of the best techniques in state-of-art performance.
Machine learning and deep learning will prove beneficial in research and academics
field.
Conclusion
In this article, we had an overview of machine learning and deep learning with illustrations
and differences also focusing on future trends. Many of AI applications utilize machine
learning algorithms primarily to drive self-service, increase agent productivity and
workflows more reliable. Machine learning and deep learning algorithms include an exciting
prospect for many businesses and industry leaders.
18
6. TensorFlow — Basics TensorFlow
In this chapter, we will learn about the basics of TensorFlow. We will begin by
understanding the data structure of tensor.
Rank
Unit of dimensionality described within tensor is called rank. It identifies the number of
dimensions of the tensor. A rank of a tensor can be described as the order or n-dimensions
of a tensor defined.
Shape
The number of rows and columns together define the shape of Tensor.
Type
Type describes the data type assigned to Tensor’s elements.
19
TensorFlow
Declaration
The indexing of elements is same as Python lists. The first element starts with index of 0;
to print the values through index, all you need to do is mention the index number.
20
TensorFlow
21
TensorFlow
[12 13 14 15]]
>>>
The specific elements of two dimensional tensors can be tracked with the help of row
number and column number specified as index numbers.
>>> tensor_2d[3][2]
14
22
TensorFlow
import tensorflow as tf
import numpy as np
matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype='int32')
matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype='int32')
print (matrix1)
print (matrix2)
matrix1 = tf.constant(matrix1)
matrix2 = tf.constant(matrix2)
matrix_product = tf.matmul(matrix1, matrix2)
matrix_sum = tf.add(matrix1,matrix2)
matrix_3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype='float32')
print (matrix_3)
matrix_det = tf.matrix_determinant(matrix_3)
with tf.Session() as sess:
result1 = sess.run(matrix_product)
result2 = sess.run(matrix_sum)
result3 = sess.run(matrix_det)
print (result1)
print (result2)
print (result3)
23
TensorFlow
Output
The above code will generate the following output:
Explanation
We have created multidimensional arrays in the above source code. Now, it is important
to understand that we created graph and sessions, which manage the Tensors and
generate the appropriate output. With the help of graph, we have the output specifying
the mathematical calculations between Tensors.
24
7. TensorFlow — Convolutional Neural TensorFlow
Networks
After understanding machine-learning concepts, we can now shift our focus to deep
learning concepts. Deep learning is a division of machine learning and is considered as a
crucial step taken by researchers in recent decades. The examples of deep learning
implementation include applications like image recognition and speech recognition.
The dominant approach of CNN includes solutions for problems of recognition. Top
companies like Google and Facebook have invested in research and development towards
recognition projects to get activities done with greater speed.
CNN utilizes spatial correlations that exist within the input data. Each concurrent layer of
a neural network connects some input neurons. This specific region is called local receptive
field. Local receptive field focusses on the hidden neurons. The hidden neurons process
the input data inside the mentioned field not realizing the changes outside the specific
boundary.
25
TensorFlow
If we observe the above representation, each connection learns a weight of the hidden
neuron with an associated connection with movement from one layer to another. Here,
individual neurons perform a shift from time to time. This process is called “convolution”.
The mapping of connections from the input layer to the hidden feature map is defined as
“shared weights” and bias included is called “shared bias”.
CNN or convolutional neural networks use pooling layers, which are the layers, positioned
immediately after CNN declaration. It takes the input from the user as a feature map that
comes out of convolutional networks and prepares a condensed feature map. Pooling
layers helps in creating layers with neurons of previous layers.
26
TensorFlow
Step 1: Include the necessary modules for TensorFlow and the data set modules, which
are needed to compute the CNN model.
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
Step 2: Declare a function called run_cnn(), which includes various parameters and
optimization variables with declaration of data placeholders. These optimization variables
will declare the training pattern.
def run_cnn():
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
learning_rate = 0.0001
epochs = 10
batch_size = 50
Step 3: In this step, we will declare the training data placeholders with input parameters
- for 28 x 28 pixels = 784. This is the flattened image data that is drawn from
mnist.train.nextbatch().
We can reshape the tensor according to our requirements. The first value (-1) tells
function to dynamically shape that dimension based on the amount of data passed to it.
The two middle dimensions are set to the image size (i.e. 28 x 28).
27
TensorFlow
Step 5: Let us flatten the output ready for the fully connected output stage - after two
layers of stride 2 pooling with the dimensions of 28 x 28, to dimension of 14 x 14 or
minimum 7 x 7 x,y co-ordinates, but with 64 output channels. To create the fully connected
with "dense" layer, the new shape needs to be [-1, 7 x 7 x 64]. We can set up some
weights and bias values for this layer, then activate with ReLU.
Step 6: Another layer with specific softmax activations with the required optimizer defines
the accuracy assessment, which makes the setup of initialization operator.
cross_entropy =
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=dense_layer2,
labels=y))
optimiser =
tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
init_op = tf.global_variables_initializer()
Step 7: We should set up recording variables. This adds up a summary to store the
accuracy of data.
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
28
TensorFlow
writer = tf.summary.FileWriter('E:\TensorFlowProject')
with tf.Session() as sess:
sess.run(init_op)
total_batch = int(len(mnist.train.labels) / batch_size)
for epoch in range(epochs):
avg_cost = 0
for i in range(total_batch):
batch_x, batch_y =
mnist.train.next_batch(batch_size=batch_size)
_, c = sess.run([optimiser, cross_entropy], feed_dict={x:
batch_x, y: batch_y})
avg_cost += c / total_batch
test_acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y:
mnist.test.labels})
summary = sess.run(merged, feed_dict={x: mnist.test.images, y:
mnist.test.labels})
writer.add_summary(summary, epoch)
print("\nTraining complete!")
writer.add_graph(sess.graph)
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y:
mnist.test.labels}))
out_layer += bias
29
TensorFlow
out_layer = tf.nn.relu(out_layer)
ksize = [1, pool_shape[0], pool_shape[1], 1]
strides = [1, 2, 2, 1]
out_layer = tf.nn.max_pool(out_layer, ksize=ksize, strides=strides,
padding='SAME')
return out_layer
if __name__ == "__main__":
run_cnn()
See @{tf.nn.softmax_cross_entropy_with_logits_v2}.
2018-09-19 17:22:58.802268: I
T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140]
Your CPU supports instructions that this TensorFlow binary was not compiled to
use: AVX2
2018-09-19 17:25:41.522845: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 1003520000 exceeds 10% of system memory.
2018-09-19 17:25:44.630941: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 501760000 exceeds 10% of system memory.
Epoch: 1 cost = 0.676 test accuracy: 0.940
2018-09-19 17:26:51.987554: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 1003520000 exceeds 10% of system memory.
30
8. TensorFlow — Recurrent Neural Networks TensorFlow
Step 2: Network will take an example and compute some calculations using randomly
initialized variables.
Step 4: The comparison of actual result generated with the expected value will produce
an error.
Step 5: To trace the error, it is propagated through same path where the variables are
also adjusted.
Step 6: The steps from 1 to 5 are repeated until we are confident that the variables
declared to get the output are defined properly.
Step 7: A systematic prediction is made by applying these variables to get new unseen
input.
31
TensorFlow
Step 1: TensorFlow includes various libraries for specific implementation of the recurrent
neural network module.
import tensorflow as tf
from tensorflow.contrib import rnn
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
As mentioned above, the libraries help in defining the input data, which forms the primary
part of recurrent neural network implementation.
Step 2: Our primary motive is to classify the images using a recurrent neural network,
where we consider every image row as a sequence of pixels. MNIST image shape is
specifically defined as 28*28 px. Now we will handle 28 sequences of 28 steps for each
sample that is mentioned. We will define the input parameters to get the sequential pattern
done.
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes]
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([n_classes]))
}
32
TensorFlow
Step 3: Compute the results using a defined function in RNN to get the best results. Here,
each data shape is compared with current input shape and the results are computed to
maintain the accuracy rate.
x = tf.unstack(x, n_steps, 1)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
Step 4: In this step, we will launch the graph to get the computational results. This also
helps in calculating the accuracy for test results.
test_len = 128
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
test_label = mnist.test.labels[:test_len]
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))
34
TensorFlow
35
9. TensorFlow — TensorBoard Visualization TensorFlow
TensorFlow includes a visualization tool, which is called the TensorBoard. It is used for
analyzing Data Flow Graph and also used to understand machine-learning models. The
important feature of TensorBoard includes a view of different types of statistics about the
parameters and details of any graph in vertical alignment.
Deep neural network includes up to 36,000 nodes. TensorBoard helps in collapsing these
nodes in high-level blocks and highlighting the identical structures. This allows better
analysis of graph focusing on the primary sections of the computation graph. The
TensorBoard visualization is said to be very interactive where a user can pan, zoom and
expand the nodes to display the details.
The algorithms collapse nodes into high-level blocks and highlight the specific groups with
identical structures, which separate high-degree nodes. The TensorBoard thus created is
useful and is treated equally important for tuning a machine learning model. This
visualization tool is designed for the configuration log file with summary information and
details that need to be displayed.
Let us focus on the demo example of TensorBoard visualization with the help of the
following code:
36
TensorFlow
import tensorflow as tf
# Constants creation for TensorBoard visualization
a = tf.constant(10,name="a")
b = tf.constant(90,name="b")
y = tf.Variable(a+b*2,name='y')
model = tf.initialize_all_variables() #Creation of model
The following table shows the various symbols of TensorBoard visualization used for the
node representation:
37
10. TensorFlow — Word Embedding TensorFlow
Word embedding is the concept of mapping from discrete objects such as words to vectors
and real numbers. It is important for input for machine learning. The concept includes
standard functions, which effectively transform discrete input objects to useful vectors.
Word2vec
Word2vec is the most common approach used for unsupervised word embedding
technique. It trains the model in such a way that a given input word predicts the word’s
context by using skip-grams.
TensorFlow enables many ways to implement this kind of model with increasing levels of
sophistication and optimization and using multithreading concepts and higher-level
abstractions.
import os
import math
import numpy as np
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
batch_size = 64
embedding_dimension = 5
negative_samples = 8
LOG_DIR = "logs/word2vec_intro"
38
TensorFlow
word2index_map = {}
index = 0
for sent in sentences:
for word in sent.lower().split():
if word not in word2index_map:
word2index_map[word] = index
index += 1
index2word_map = {index: word for word, index in word2index_map.items()}
vocabulary_size = len(index2word_map)
def get_skipgram_batch(batch_size):
instance_indices = list(range(len(skip_gram_pairs)))
np.random.shuffle(instance_indices)
batch = instance_indices[:batch_size]
x = [skip_gram_pairs[i][0] for i in batch]
y = [[skip_gram_pairs[i][1]] for i in batch]
return x, y
# batch example
x_batch, y_batch = get_skipgram_batch(8)
x_batch
y_batch
[index2word_map[word] for word in x_batch]
[index2word_map[word[0]] for word in y_batch]
39
TensorFlow
nce_weights = tf.Variable(
tf.truncated_normal([vocabulary_size, embedding_dimension],
stddev=1.0 / math.sqrt(embedding_dimension)))
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
loss = tf.reduce_mean(
tf.nn.nce_loss(weights=nce_weights, biases=nce_biases, inputs=embed,
labels=train_labels,
num_sampled=negative_samples, num_classes=vocabulary_size))
tf.summary.scalar("NCE_loss", loss)
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embeddings.name
# Link this tensor to its metadata file (e.g. labels).
embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')
projector.visualize_embeddings(train_writer, config)
tf.global_variables_initializer().run()
if step % 100 == 0:
saver.save(sess, os.path.join(LOG_DIR, "w2v_model.ckpt"), step)
loss_value = sess.run(loss,
feed_dict={train_inputs: x_batch,
train_labels: y_batch})
print("Loss at %d: %.5f" % (step, loss_value))
40
TensorFlow
ref_word = normalized_embeddings_matrix[word2index_map["one"]]
Output
The above code generates the following output:
41
11. TensorFlow — Single Layer Perceptron TensorFlow
The diagram shows that the hidden units communicate with the external layer. While the
input and output units communicate only through the hidden layer of the network.
The pattern of connection with nodes, the total number of layers and level of nodes
between inputs and outputs with the number of neurons per layer define the architecture
of a neural network.
There are two types of architecture. These types focus on the functionality artificial neural
networks as follows:
42
TensorFlow
Let us focus on the implementation of single layer perceptron for an image classification
problem using TensorFlow. The best example to illustrate the single layer perceptron is
through representation of “Logistic Regression”.
43
TensorFlow
Now, let us consider the following basic steps of training logistic regression:
The weights are initialized with random values at the beginning of the training.
For each element of the training set, the error is calculated with the difference
between desired output and the actual output. The error calculated is used to adjust
the weights.
The process is repeated until the error made on the entire training set is not less
than the specified threshold, until the maximum number of iterations is reached.
import tensorflow as tf
import matplotlib.pyplot as plt
# Parameters
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1
# tf Graph Input
x = tf.placeholder("float", [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder("float", [None, 10]) # 0-9 digits recognition => 10 classes
# Create model
# Construct model
activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
optimizer = tf.train.\
GradientDescentOptimizer(learning_rate).minimize(cost)
#Plot settings
avg_set = []
epoch_set=[]
44
TensorFlow
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_xs, batch_ys = \
mnist.train.next_batch(batch_size)
# Fit training using batch data
sess.run(optimizer, \
feed_dict={x: batch_xs, y: batch_ys})
# Compute average loss
avg_cost += sess.run(cost, \
feed_dict={x: batch_xs, \
y: batch_ys})/total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print ("Epoch:", '%04d' % (epoch+1), "cost=",
"{:.9f}".format(avg_cost))
avg_set.append(avg_cost)
epoch_set.append(epoch+1)
print ("Training phase finished")
# Test model
correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("Model accuracy:", accuracy.eval({x: mnist.test.images, y:
mnist.test.labels}))
45
TensorFlow
Output
The above code generates the following output:
46
12. TensorFlow — Linear Regression TensorFlow
In this chapter, we will focus on the basic example of linear regression implementation
using TensorFlow. Logistic regression or linear regression is a supervised machine learning
approach for the classification of order discrete categories. Our goal in this chapter is to
build a model by which a user can predict the relationship between predictor variables and
one or more independent variables.
The relationship between these two variables is considered linear. If y is the dependent
variable and x is considered as the independent variable, then the linear regression
relationship of two variables will look like the following equation:
Y= Ax+b
We will design an algorithm for linear regression. This will allow us to understand the
following two important concepts:
Cost Function
Gradient descent algorithms
47
TensorFlow
Step 1
It is important to import the necessary modules for plotting the linear regression module.
We start importing the Python library NumPy and Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
Step 2
Define the number of coefficients necessary for logistic regression.
number_of_points = 500
x_point = []
y_point = []
a = 0.22
b = 0.78
Step 3
Iterate the variables for generating 300 random points around the regression equation:
Y=0.22x+0.78
for i in range(number_of_points):
x = np.random.normal(0.0,0.5)
y = a*x + b +np.random.normal(0.0,0.1)
x_point.append([x])
y_point.append([y])
Step 4
View the generated points using Matplotlib.
48
TensorFlow
import numpy as np
import matplotlib.pyplot as plt
number_of_points = 500
x_point = []
y_point = []
a = 0.22
b = 0.78
for i in range(number_of_points):
x = np.random.normal(0.0,0.5)
y = a*x + b +np.random.normal(0.0,0.1)
x_point.append([x])
y_point.append([y])
49
13. TensorFlow — TFLearn and its installation TensorFlow
TFLearn can be defined as a modular and transparent deep learning aspect used in
TensorFlow framework. The main motive of TFLearn is to provide a higher level API to
TensorFlow for facilitating and showing up new experiments.
It includes easy concepts to build highly modular network layers, optimizers and
various metrics embedded within them.
It includes powerful helper functions to train the built in tensors which accept
multiple inputs, outputs and optimizers.
Upon execution of the above code, the following output will be generated:
50
TensorFlow
The following illustration shows the implementation of TFLearn with Random Forest
classifier:
m = RandomForestClassifier(n_estimators=100, max_nodes=1000)
m.fit(X, Y, batch_size=10000, display_step=10)
51
14. TensorFlow — CNN and RNN Difference TensorFlow
In this chapter, we will focus on the difference between CNN and RNN:
CNN RNN
It is suitable for spatial data such as RNN is suitable for temporal data, also
images. called sequential data.
This network takes fixed size inputs and RNN can handle arbitrary input/output
generates fixed size outputs. lengths.
CNN is a type of feed-forward artificial RNN unlike feed forward neural networks -
neural network with variations of can use their internal memory to process
multilayer perceptrons designed to use arbitrary sequences of inputs.
minimal amounts of preprocessing.
CNNs use connectivity pattern between the Recurrent neural networks use time-series
neurons. This is inspired by the information - what a user spoke last will
organization of the animal visual cortex, impact what he/she will speak next.
whose individual neurons are arranged in
such a way that they respond to
overlapping regions tiling the visual field.
CNNs are ideal for images and video RNNs are ideal for text and speech
processing. analysis.
52
15. TensorFlow — Keras TensorFlow
Keras is compact, easy to learn, high-level Python library run on top of TensorFlow
framework. It is made with focus of understanding deep learning techniques, such as
creating layers for neural networks maintaining the concepts of shapes and mathematical
details. The creation of freamework can be of the following two types:
Sequential API
Functional API
Consider the following eight steps to create deep learning model in Keras:
We will use the Jupyter Notebook for execution and display of output as shown below:
Step 1: Loading the data and preprocessing the loaded data is implemented first to
execute the deep learning model.
import warnings
warnings.filterwarnings('ignore')
import numpy as np
np.random.seed(123) # for reproducibility
from keras.models import Sequential
from keras.layers import Flatten, MaxPool2D, Conv2D, Dense, Reshape, Dropout
from keras.utils import np_utils
Using TensorFlow backend.
from keras.datasets import mnist
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
This step can be defined as “Import libraries and Modules” which means all the libraries
and modules are imported as an initial step.
model = Sequential()
model.add(Conv2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, 3, 3, activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
54
TensorFlow
55
16. TensorFlow — Distributed Computing TensorFlow
This chapter will focus on how to get started with distributed TensorFlow. The aim is to
help developers understand the basic distributed TF concepts that are reoccurring, such
as TF servers. We will use the Jupyter Notebook for evaluating distributed TensorFlow.
The implementation of distributed computing with TensorFlow is mentioned below:
import tensorflow as tf
Step 2: Create a TensorFlow cluster with one node. Let this node be responsible for a job
that that has name "worker" and that will operate one take at localhost:2222.
'grpc://localhost:2222'
The server is currently running.
Step 3: The server configuration with respective session can be calculated by executing
the following command:
server.server_def
cluster {
job {
name: "worker"
tasks {
value: "localhost:2222"
}
}
}
job_name: "worker"
56
TensorFlow
protocol: "grpc"
Step 4: Launch a TensorFlow session with the execution engine being the server. Use
TensorFlow to create a local server and use lsof to find out the location of the server.
sess = tf.Session(target=server.target)
server = tf.train.Server.create_local_server()
Step 5: View devices available in this session and close the respective session.
devices = sess.list_devices()
for d in devices:
print(d.name)
sess.close()
/job:worker/replica:0/task:0/device:CPU:0
57
17. TensorFlow — Exporting with TensorFlow TensorFlow
Here, we will focus on MetaGraph formation in TensorFlow. This will help us understand
export module in TensorFlow. The MetaGraph contains the basic information, which is
required to train, perform evaluation, or run inference on a previously trained graph.
Arguments:
filename: Optional meta_graph filename including the path.
collection_list: List of string keys to collect.
as_text: If `True`, writes the meta_graph as an ASCII proto.
Returns:
A `MetaGraphDef` proto.
"""
One of the typical usage model for the same is mentioned below:
58
18. TensorFlow — Multi-Layer Perceptron TensorFlow
Learning
MLP networks are usually used for supervised learning format. A typical learning algorithm
for MLP networks is also called back propagation’s algorithm.
Now, we will focus on the implementation with MLP for an image classification problem.
import tensorflow as tf
import matplotlib.pyplot as plt
# Parameters
learning_rate = 0.001
training_epochs = 20
batch_size = 100
display_step = 1
# Network Parameters
n_hidden_1 = 256 # 1st layer num features
59
TensorFlow
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
# weights layer 1
h = tf.Variable(tf.random_normal([n_input, n_hidden_1]))
# bias layer 1
bias_layer_1 = tf.Variable(tf.random_normal([n_hidden_1]))
# layer 1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, h), bias_layer_1))
# weights layer 2
w = tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]))
# bias layer 2
bias_layer_2 = tf.Variable(tf.random_normal([n_hidden_2]))
# layer 2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, w), bias_layer_2))
# cost function
cost =
tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output_layer,
labels=y))
#cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(output_layer,
y))
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# optimizer =
tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
# Plot settings
avg_set = []
epoch_set = []
# Training cycle
60
TensorFlow
# Test model
correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print
"Model Accuracy:", accuracy.eval({x: mnist.test.images, y:
mnist.test.labels})
61
TensorFlow
62
19. TensorFlow — Hidden Layers of Perceptron TensorFlow
In this chapter, we will be focus on the network we will have to learn from known set of
points called x and f(x). A single hidden layer will build this simple network.
The code for the explanation of hidden layers of perceptron is as shown below:
np.random.seed(1000)
function_to_learn = lambda x: np.cos(x) + 0.1*np.random.randn(*x.shape)
layer_1_neurons = 10
NUM_points = 1000
#Training the parameters
batch_size = 100
NUM_EPOCHS = 1500
train_size = int(900)
#Training the first 700 points in the given set
x_training = all_x[:train_size]
y_training = function_to_learn(x_training)
plt.figure(1)
plt.scatter(x_training, y_training, c='blue', label='train')
plt.scatter(x_validation, y_validation, c='pink', label='validation')
plt.legend()
plt.show()
#first layer
#Number of neurons = 10
w_h = tf.Variable(tf.random_uniform([1, layer_1_neurons],\
minval=-1, maxval=1, dtype=tf.float32))
b_h = tf.Variable(tf.zeros([1, layer_1_neurons], dtype=tf.float32))
h = tf.nn.sigmoid(tf.matmul(X, w_h) + b_h)
63
TensorFlow
#output layer
#Number of neurons = 10
w_o = tf.Variable(tf.random_uniform([layer_1_neurons, 1],\
minval=-1, maxval=1, dtype=tf.float32))
b_o = tf.Variable(tf.zeros([1, 1], dtype=tf.float32))
errors = []
for i in range(NUM_EPOCHS):
for start, end in zip(range(0, len(x_training), batch_size),\
range(batch_size, len(x_training), batch_size)):
sess.run(train_op, feed_dict={X: x_training[start:end],\
Y: y_training[start:end]})
cost = sess.run(tf.nn.l2_loss(model - y_validation),\
feed_dict={X:x_validation})
errors.append(cost)
if i%100 == 0:
print("epoch %d, cost = %g" % (i, cost))
64
TensorFlow
Output
Following is the representation of function layer approximation:
Here two data are represented in shape of W. The two data are: train and validation which
are represented in distinct colors as visible in legend section.
65
TensorFlow
66
20. TensorFlow — Optimizers in TensorFlow TensorFlow
Optimizers are the extended class, which include added information to train a specific
model. The optimizer class is initialized with given parameters but it is important to
remember that no Tensor is needed. The optimizers are used for improving speed and
performance for training a specific model.
tf.train.Optimizer
We will focus on the Stochastic Gradient descent. The illustration for creating optimizer for
the same is mentioned below:
def sgd(cost,
params,
lr=np.float32(0.01)):
updates = []
updates.append(param.assign(param -
lr*g_param))
return updates
The basic parameters are defined within the specific function. In our subsequent chapter,
we will focus on Gradient Descent Optimization with implementation of optimizers.
67
21. TensorFlow — XOR Implementation TensorFlow
In this chapter, we will learn about the XOR implementation using TensorFlow. Before
starting with XOR implementation in TensorFlow, let us see the XOR table values. This will
help us understand encryption and decryption process.
XOR Cipher encryption method is basically used to encrypt data which is hard to crack
with brute force method, i.e., by generating random encryption keys which match the
appropriate key.
The concept of implementation with XOR Cipher is to define a XOR encryption key and
then perform XOR operation of the characters in the specified string with this key, which
a user tries to encrypt. Now we will focus on XOR implementation using TensorFlow, which
is mentioned below:
68
TensorFlow
theta1=tf.cast(tf.Variable(tf.random_normal([3,hidden_s]),name="theta1"),tf.flo
at64)
theta2=tf.cast(tf.Variable(tf.random_normal([hidden_s+1,1]),name="theta2"),tf.f
loat64)
a1=tf.concat([np.c_[np.ones(x.shape[0])],x],1)
#the weights of the first layer are multiplied by the input of the first layer
z1=tf.matmul(a1,theta1)
#the input of the second layer is the output of the first layer, passed through
the activation function and column of biases is added
a2=tf.concat([np.c_[np.ones(x.shape[0])],tf.sigmoid(z1)],1)
#the input of the second layer is multiplied by the weights
z3=tf.matmul(a2,theta2)
#the output is passed through the activation function to obtain the final
probability
h3=tf.sigmoid(z3)
cost_func=-tf.reduce_sum(y*tf.log(h3)+(1-y)*tf.log(1-h3),axis=1)
69
TensorFlow
sess=tf.Session()
sess.run(init)
#running gradient descent for each iteration and printing the hypothesis
obtained using the updated theta values
for i in range(100000):
sess.run(optimiser, feed_dict={x:X,y:Y})#setting place holder values using
feed_dict
if i%100==0:
print("Epoch:",i)
print("Hyp:",sess.run(h3,feed_dict={x:X,y:Y}))
The above line of code generates an output as shown in the screenshot below:
70
22. TensorFlow — Gradient Descent Optimization TensorFlow
Consider the steps shown below to understand the implementation of gradient descent
optimization:
Step 1
Include necessary modules and declaration of x and y variables through which we are
going to define the gradient descent optimization.
import tensorflow as tf
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(log_x_squared)
Step 2
Initialize the necessary variables and call the optimizers for defining and calling it with
respective function.
init = tf.initialize_all_variables()
def optimize():
with tf.Session() as session:
session.run(init)
print("starting at", "x:", session.run(x), "log(x)^2:",
session.run(log_x_squared))
for step in range(10):
session.run(train)
print("step", step, "x:", session.run(x), "log(x)^2:",
session.run(log_x_squared))
71
TensorFlow
optimize()
The above line of code generates an output as shown in the screenshot below:
We can see that the necessary epochs and iterations are calculated as shown in the output.
72
23. TensorFlow — Forming Graphs TensorFlow
N=500
Now, we will compute partial differential equation and form the respective graph using it.
Consider the steps given below for computing graph.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Step 2: Include functions for transformation of a 2D array into a convolution kernel and
simplified 2D convolution operation.
def make_kernel(a):
a = np.asarray(a)
a = a.reshape(list(a.shape) + [1,1])
return tf.constant(a, dtype=1)
def laplace(x):
"""Compute the 2D laplacian of an array"""
laplace_k = make_kernel([[0.5, 1.0, 0.5],
[1.0, -6., 1.0],
[0.5, 1.0, 0.5]])
return simple_conv(x, laplace_k)
73
TensorFlow
sess = tf.InteractiveSession()
Step 3: Include the number of iterations and compute the graph to display the records
accordingly.
N = 500
plt.imshow(u_init)
plt.show()
# Parameters:
# eps -- time resolution
# damping -- wave damping
eps = tf.placeholder(tf.float32, shape=())
damping = tf.placeholder(tf.float32, shape=())
74
TensorFlow
step = tf.group(
U.assign(U_),
Ut.assign(Ut_))
75
TensorFlow
76
24. TensorFlow — Image Recognition using TensorFlow
TensorFlow
TensorFlow includes a special feature of image recognition and these images are stored in
a specific folder. With relatively same images, it will be easy to implement this logic for
security purposes.
The dataset_image includes the related images, which need to be loaded. We will focus on
image recognition with our logo defined in it. The images are loaded with “load_data.py”
script, which helps in keeping a note on various image recognition modules within them.
import pickle
from sklearn.model_selection import train_test_split
from scipy import misc
import numpy as np
import os
label = os.listdir("dataset_image")
label=label[1:]
dataset=[]
for image_label in label:
images = os.listdir("dataset_image/"+image_label)
77
TensorFlow
X=[]
Y=[]
X.append(input)
Y.append(label.index(image_label))
X=np.array(X)
Y=np.array(Y)
X_train,y_train, = X,Y
data_set=(X_train,y_train)
save_label = open("int_to_word_out.pickle","wb")
pickle.dump(label, save_label)
save_label.close()
78
TensorFlow
The training of images helps in storing the recognizable patterns within specified folder.
import numpy
import keras
K.set_image_dim_ordering('tf')
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load data
(X_train,y_train)=load_data.data_set
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(64, 64, 3), padding='same',
activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same',
kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
epochs = 10
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
model.compile(loss='categorical_crossentropy', optimizer=sgd,
metrics=['accuracy'])
print(model.summary())
#callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0,
patience=0, verbose=0, mode='auto')]
callbacks=[keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0,
batch_size=32, write_graph=True, write_grads=False, write_images=True,
embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)]
# Fit the model
model.fit(X_train, y_train, epochs=epochs,
batch_size=32,shuffle=True,callbacks=callbacks)
80
TensorFlow
81
25. TensorFlow — Recommendations for Neural TensorFlow
Network Training
In this chapter, we will understand the various aspects of neural network training which
can be implemented using TensorFlow framework.
Back Propagation
Back propagation is a simple method to compute partial derivatives, which includes the
basic form of composition best suitable for neural nets.
82
TensorFlow
Adapting the learning rate is one of the most important features of gradient descent
optimization. This is crucial to TensorFlow implementation.
83
TensorFlow
Dropout
Deep neural nets with a large number of parameters form powerful machine learning
systems. However, over fitting is a serious problem in such networks.
84
TensorFlow
Max Pooling
Max pooling is a sample-based discretization process. The object is to down-sample an
input representation, which reduces the dimensionality with the required assumptions.
85
TensorFlow
86