Tải bản đầy đủ (.pdf) (575 trang)

Deep learning with tensorflow explore neural networks and build intelligent systems with python 2nd edition fully revised and updated

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (13.48 MB, 575 trang )


Table of Contents
Deep Learning with TensorFlow - Second Edition
Why subscribe?
PacktPub.com
Contributors
About the authors
About the reviewers
Packt is Searching for Authors Like You
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
1. Getting Started with Deep Learning
A soft introduction to machine learning
Supervised learning
Unbalanced data
Unsupervised learning
Reinforcement learning
What is deep learning?
Artificial neural networks
The biological neurons
The artificial neuron
How does an ANN learn?
ANNs and the backpropagation algorithm
Weight optimization


Stochastic gradient descent
Neural network architectures
Deep Neural Networks (DNNs)
Multilayer perceptron
Deep Belief Networks (DBNs)
Convolutional Neural Networks (CNNs)
AutoEncoders
Recurrent Neural Networks (RNNs)
Emergent architectures
Deep learning frameworks
Summary
2. A First Look at TensorFlow
A general overview of TensorFlow
What's new from TensorFlow v1.6 forwards?
Nvidia GPU support optimized
Introducing TensorFlow Lite
Eager execution
Optimized Accelerated Linear Algebra (XLA)
Installing and configuring TensorFlow
TensorFlow computational graph
TensorFlow code structure
Eager execution with TensorFlow
Data model in TensorFlow
Tensor
Rank and shape
Data type
Variables
Fetches



Feeds and placeholders
Visualizing computations through TensorBoard
How does TensorBoard work?
Linear regression and beyond
Linear regression revisited for a real dataset
Summary
3. Feed-Forward Neural Networks with TensorFlow
Feed-forward neural networks (FFNNs)
Feed-forward and backpropagation
Weights and biases
Activation functions
Using sigmoid
Using tanh
Using ReLU
Using softmax
Implementing a feed-forward neural network
Exploring the MNIST dataset
Softmax classifier
Implementing a multilayer perceptron (MLP)
Training an MLP
Using MLPs
Dataset description
Preprocessing
A TensorFlow implementation of MLP for client-subscription assessment
Deep Belief Networks (DBNs)
Restricted Boltzmann Machines (RBMs)
Construction of a simple DBN
Unsupervised pre-training
Supervised fine-tuning
Implementing a DBN with TensorFlow for client-subscription assessment

Tuning hyperparameters and advanced FFNNs
Tuning FFNN hyperparameters
Number of hidden layers
Number of neurons per hidden layer
Weight and biases initialization
Selecting the most suitable optimizer
GridSearch and randomized search for hyperparameters tuning
Regularization
Dropout optimization
Summary
4. Convolutional Neural Networks
Main concepts of CNNs
CNNs in action
LeNet5
Implementing a LeNet-5 step by step
AlexNet
Transfer learning
Pretrained AlexNet
Dataset preparation
Fine-tuning implementation
VGG
Artistic style learning with VGG-19
Input images
Content extractor and loss
Style extractor and loss
Merger and total loss
Training
Inception-v3
Exploring Inception with TensorFlow
Emotion recognition with CNNs

Testing the model on your own image


Source code
Summary
5. Optimizing TensorFlow Autoencoders
How does an autoencoder work?
Implementing autoencoders with TensorFlow
Improving autoencoder robustness
Implementing a denoising autoencoder
Implementing a convolutional autoencoder
Encoder
Decoder
Fraud analytics with autoencoders
Description of the dataset
Problem description
Exploratory data analysis
Training, validation, and testing set preparation
Normalization
Autoencoder as an unsupervised feature learning algorithm
Evaluating the model
Summary
6. Recurrent Neural Networks
Working principles of RNNs
Implementing basic RNNs in TensorFlow
RNN and the long-term dependency problem
Bi-directional RNNs
RNN and the gradient vanishing-exploding problem
LSTM networks
GRU cell

Implementing an RNN for spam prediction
Data description and preprocessing
Developing a predictive model for time series data
Description of the dataset
Pre-processing and exploratory analysis
LSTM predictive model
Model evaluation
An LSTM predictive model for sentiment analysis
Network design
LSTM model training
Visualizing through TensorBoard
LSTM model evaluation
Human activity recognition using LSTM model
Dataset description
Workflow of the LSTM model for HAR
Implementing an LSTM model for HAR
Summary
7. Heterogeneous and Distributed Computing
GPGPU computing
The GPGPU history
The CUDA architecture
The GPU programming model
The TensorFlow GPU setup
Update TensorFlow
GPU representation
Using a GPU
GPU memory management
Assigning a single GPU on a multi-GPU system
The source code for GPU with soft placement
Using multiple GPUs

Distributed computing
Model parallelism
Data parallelism
The distributed TensorFlow setup


Summary
8. Advanced TensorFlow Programming
tf.estimator
Estimators
Graph actions
Parsing resources
Flower predictions
TFLearn
Installation
Titanic survival predictor
PrettyTensor
Chaining layers
Normal mode
Sequential mode
Branch and join
Digit classifier
Keras
Keras programming models
Sequential model
Sentiment classification of movie reviews
Functional API
SqueezeNet
Summary
9. Recommendation Systems Using Factorization Machines

Recommendation systems
Collaborative filtering approaches
Content-based filtering approaches
Hybrid recommender systems
Model-based collaborative filtering
Movie recommendation using collaborative filtering
The utility matrix
Description of the dataset
Ratings data
Movies data
Users data
Exploratory analysis of the MovieLens dataset
Implementing a movie RE
Training the model with the available ratings
Inferencing the saved model
Generating the user-item table
Clustering similar movies
Movie rating prediction by users
Finding top k movies
Predicting top k similar movies
Computing user-user similarity
Evaluating the recommender system
Factorization machines for recommendation systems
Factorization machines
Cold-start problem and collaborative-filtering approaches
Problem definition and formulation
Dataset description
Workflow of the implementation
Preprocessing
Training the FM model

Improved factorization machines
Neural factorization machines
Dataset description
Using NFM for the movie recommendation
Model training
Model evaluation
Summary


10. Reinforcement Learning
The RL problem
OpenAI Gym
OpenAI environments
The env class
Installing and running OpenAI Gym
The Q-Learning algorithm
The FrozenLake environment
Deep Q-learning
Deep Q neural networks
The Cart-Pole problem
Deep Q-Network for the Cart-Pole problem
The Experience Replay method
Exploitation and exploration
The Deep Q-Learning training algorithm
Summary
Other Books You May Enjoy
Leave a review – let other readers know what you think
Index



Deep Learning with TensorFlow Second Edition


Deep Learning with TensorFlow Second Edition
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored
in a retrieval system, or transmitted in any form or by any means,
without the prior written permission of the publisher, except in the
case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure
the accuracy of the information presented. However, the information
contained in this book is sold without warranty, either express or
implied. Neither the authors, nor Packt Publishing or its dealers and
distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information
about all of the companies and products mentioned in this book by
the appropriate use of capitals. However, Packt Publishing cannot
guarantee the accuracy of this information.
Acquisition Editors: Ben Renow-Clarke, Suresh Jain
Project Editor: Savvy Sequeira
Content Development Editors: Jo Lovell
Technical Editor: Nidhisha Shetty
Copy Editor: Safis Editing
Indexers: Tejal Daruwale Soni
Graphics: Tom Scaria


Production Coordinator: Arvindkumar Gupta
First published: April 2017

Second edition: March 2018
Production reference: 1290318
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78883-110-9
www.packtpub.com

mapt.io
Mapt is an online digital library that gives you full access to over
5,000 books and videos, as well as industry leading tools to help you
plan your personal development and advance your career. For more
information, please visit our website.

Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Learn better with Skill Plans built especially for you


Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content


PacktPub.com
Did you know that Packt offers eBook versions of every book
published, with PDF and ePub files available? You can upgrade to
the eBook version at www.PacktPub.com and as a print book

customer, you are entitled to a discount on the eBook copy. Get in
touch with us at <> for more details.
At www.PacktPub.com, you can also read a collection of free
technical articles, sign up for a range of free newsletters, and
receive exclusive discounts and offers on Packt books and eBooks.


Contributors
About the authors
Giancarlo Zaccone has over ten years of experience in managing
research projects in scientific and industrial areas.
Giancarlo worked as a researcher at the CNR, the National
Research Council of Italy. As part of his data science and software
engineering projects, he gained experience in numerical computing,
parallel computing, and scientific visualization.
Currently, Giancarlo is a senior software and system engineer,
based in the Netherlands. Here he tests and develops software
systems for space and defense applications.
Giancarlo holds a master's degree in Physics from the Federico II of
Naples and a 2nd level postgraduate master course in Scientific
Computing from La Sapienza of Rome.
Giancarlo is the author of the following books: Python Parallel
Programminng Cookbook, Getting Started with TensorFlow, Deep
Learning with TensorFlow, all by Packt Publishing.
You can follow him at />Md. Rezaul Karim is a research scientist at Fraunhofer FIT,
Germany. He is also pursuing his PhD at the RWTH Aachen
University, Aachen, Germany. He holds BSc and MSc degrees in
Computer Science. Before joining Fraunhofer FIT, Rezaul had been
working as a researcher at Insight Centre for Data Analytics,
Ireland. Previously, he worked as a Lead Engineer at Samsung

Electronics. He also worked as a research assistant at Database
Lab, Kyung Hee University, Korea and as an R&D engineer with
BMTech21 Worldwide, Korea.


Rezaul has over 9 years of experience in research and development
with a solid understanding of algorithms and data structures in C,
C++, Java, Scala, R, and Python. He has published several research
papers and technical articles concerning Bioinformatics, Semantic
Web, Big Data, Machine Learning and Deep Learning using Spark,
Kafka, Docker, Zeppelin, Hadoop, and MapReduce.
Rezaul is also equally competent with (deep) machine learning
libraries such as Spark ML, Keras, Scikit-learn, TensorFlow,
DeepLearning4j, MXNet, and H2O. Moreover, Rezaul is the author of
the following books:
Large-Scale Machine Learning with Spark, Deep Learning with
TensorFlow, Scala and Spark for Big Data Analytics, Predictive
Analytics with TensorFlow, Scala Machine Learning Projects, all by
Packt Publishing.
Writing this book was made easier by amazing efforts by many open source communities and
documentation about many projects. Further, I would like to thank a wonderful team at Packt for their
sincere cooperation and coordination. Finally, I appreciate numerous efforts by the TensorFlow community
and all those who have contributed to APIs, whose work ultimately brought the machine learning to the
masses!


About the reviewers
Motaz Saad holds a PhD in Computer Science from the University of
Lorraine. He loves data and likes to play with it. Motaz has over ten
years of professional experience in NLP, computational linguistics,

and data science machine learning. Motaz currently works as an
assistant professor at the faculty of Information Technology, IUG.
Sefik Ilkin Serengil received his MSc in Computer Science from the
Galatasaray University in 2011.
Sefik has been working as a software developer for a FinTech
company since 2010. Currently, he is a member of the AI team as a
data scientist in this company.
Sefik's current research interests are Machine Learning and
Cryptography. He has published several research papers on these
topics. Nowadays, he enjoys speaking to communities about these
disciplines.
Sefik has also created several online courses on Machine Learning.
Vihan Jain has made several key contributions to the open-sourced
TensorFlow project. He has been advocating for the adoption of
TensorFlow since two years. Vihan has given tech-talks and has
taught tutorials on TensorFlow at various conferences. His research
interests include reinforcement learning, wide and deep learning,
recommendation systems, and machine learning infrastructure. Vihan
graduated from the Indian Institute of Technology, Roorkee, in 2013
with the President's gold medal.
I express my deepest gratitude to my parents, brother, sister, and my good friend and mentor, Eugene Ie.


Packt is Searching for Authors Like
You
If you're interested in becoming an author for Packt, please
visit authors.packtpub.com and apply today. We have worked with
thousands of developers and tech professionals, just like you, to help
them share their insight with the global tech community. You can
make a general application, apply for a specific hot topic that we are

recruiting an author for, or submit your own idea.


Preface
Every week, we follow news of applications and the shocking results
obtained from them, thanks to the artificial intelligence algorithms
applied in different fields. What we are witnessing is one of the
biggest accelerations in the entire history of this sector, and the main
suspect behind these important developments is called deep
learning.
Deep learning comprises a vast set of algorithms that are based on
the concept of neural networks and expand to contain a huge
number of nodes that are disseminated at several levels of depth.
Though the concept of neural networks, the so-called Artificial Neural
Network (ANN), dates back to the late 1940s, initially, they were
difficult to be used because of the need for huge computational
power resources and the lack of data required to train the
algorithms. Presently, the ability to use graphics processors (GPUs)
in parallel to perform intensive calculation operations has completely
opened the way to the use of deep learning.
In this context, we propose the second edition of this book, with
expanded and revised contents that introduce the core concepts of
deep learning, using the last version of TensorFlow.
TensorFlow is Google's open-source framework for the
mathematical, Machine Learning, and Deep Learning capabilities,
released in 2011. Subsequently, TensorFlow has been widely
adopted in academia, research, and industry. Recently, the most
stable version 1.6 has been released with a unified API. The most
stable version of TensorFlow at the time of writing was version 1.6,
which was released with a unified API and is thus a significant and

stable version in the TensorFlow roadmap. This book also discusses
and is compliant with the pre-release version, 1.7, which was
available during the production stages of this book.


TensorFlow provides the flexibility needed to implement and
research cutting-edge architectures, while allowing users to focus on
the structure of their models as opposed to mathematical details.
You will learn deep learning programming techniques with hands-on
model building, data collection, transformation, and much more!
Enjoy reading!


Who this book is for
This book is dedicated to developers, data analysts, and deep
learning enthusiasts who do not have much background with complex
numerical computations, but want to know what deep learning is.
The book majorly appeals to beginners who are looking for a quick
guide to gain some hands-on experience with deep learning.


What this book covers
Chapter 1, Getting Started with Deep Learning, covers the concepts
that will be found in all the subsequent chapters. The basics of
machine learning and deep learning are also discussed. We will also
look at Deep learning architectures that are distinguished from the
more commonplace single-hidden-layer neural networks by their
depth, that is, the number of node layers through which data passes
in a multistep process of pattern recognition. We will also analyze
these architectures with a chart summarizing all the neural networks

from where most of the deep learning algorithm evolved. The
chapter ends with an analysis of the major deep learning
frameworks.
Chapter 2, A First Look at TensorFlow, gives a detailed description
of the main TensorFlow features based on a real-life problem,
followed by a detailed discussion on TensorFlow installation and
configurations. We then look at a computation graph, data, and
programming model before getting started with TensorFlow. Toward
the end of the chapter, we will look at an example of implementing
the linear regression model for predictive analytics.
Chapter 3, Feed-Forward Neural Networks with TensorFlow,
demonstrates the theoretical background of different Feed-Forward
Neural Networks' (FFNNs) architectures such as Deep Belief
Networks (DBNs) and Multilayer Perceptron (MLP). We will then see
how to train and analyze the performance metrics that are needed to
evaluate the models; also, how to tune the hyperparameters for
FFNNs for better and optimized performance. We will also look at
two examples using MLP and DBN on how to build very robust and
accurate predictive models for predictive analytics on a bank
marketing dataset.
Chapter 4, Convolutional Neural Networks, introduces the networks
of CNNs that are the basic blocks of a Deep Learning-based image


classifier. We will consider the most important CNN architectures,
such as Lenet, AlexNet, Vgg, and Inception with hands-on
examples, specifically for AlexNet and Vgg. We will then examine the
transfer learning and style learning techniques. We will end the
chapter by developing a CNN to train a network on a series of facial
images to classify their emotional stretch.

Chapter 5, Optimizing TensorFlow Autoencoders, provides sound
theoretical background on optimizing autoencoders for data
denoising and dimensionality reduction. We will then look at how to
implement an autoencoder, gradually moving over to more robust
autoencoder implementation, such as denoising autoencoders and
convolutional autoencoders. Finally, we will look at a real-life
example of fraud analytics using an autoencoder.
Chapter 6, Recurrent Neural Networks, provides some theoretical
background of RNNs. We will also look at a few examples for
implementing predictive models for classification of images,
sentiment analysis of movies, and products spam prediction for NLP.
Finally, we'll see how to develop predictive models for time series
data.
Chapter 7, Heterogeneous and Distributed Computing, shows the
fundamental topic to execute TensorFlow models on GPU cards and
distributed systems. We will also look at basic concepts with
application examples.
Chapter 8, Advanced TensorFlow Programming, gives an overview
of the following TensorFlow-based libraries: tf.contrib.learn, Pretty
Tensor, TFLearn, and Keras. For each library, we will describe the
main features with applications.
Chapter 9, Recommendation Systems using Factorization
Machines, provides several examples on how to develop
recommendation system for predictive analytics followed by some


theoretical background of recommendation systems. We will then
look at an example of developing a movie recommendation engine
using collaborative filtering and K-means. Considering the limitations
of classical approaches, we'll see how to use Neural Factorization

Machines for developing more accurate and robust recommendation
systems.
Chapter 10, Reinforcement Learning, covers the basic concepts of
RL. We will experience the Q-learning algorithm, which is one of the
most popular reinforcement learning algorithms. Furthermore, we'll
introduce the OpenAI gym framework that is a TensorFlow
compatible toolkit for developing and comparing reinforcement
learning algorithms. We end the chapter with the implementation of a
Deep Q-Learning algorithm to resolve the cart-pole problem.


To get the most out of this book
A rudimentary level of programming in one language is assumed, as is a basic
familiarity with computer science techniques and technologies, including a basic
awareness of computer hardware and algorithms. Some competence in mathematics
is needed to the level of elementary linear algebra and calculus.
Software: Python 3.5.0, Pip, pandas, numpy, tensorflow, Matplotlib 2.1.1, IPython,
Scipy 0.19.0, sklearn, seaborn, tffm, and many more
Step: Issue the following command on Terminal on Ubuntu:
$ sudo pip3 install pandas numpy tensorflow sklearn
seaborn tffm

Nevertheless, installing guidelines are provided in the chapters.

Download the example code files
You can download the example code files for this book from your
account at . If you purchased this book
elsewhere, you can visit and
register to have the files emailed directly to you.
You can download the code files by following these steps:

1. Log in or register at .
2. Select the SUPPORT tab.
3. Click on Code Downloads & Errata.
4. Enter the name of the book in the Search box and follow the on-screen instructions.

Once the file is downloaded, please make sure that you unzip or
extract the folder using the latest version of any of the following:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for macOS
7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at
We also have other code bundles from our rich


catalog of books and videos available at
Check them out!

Download the color images

We also provide a PDF file that has color images of the
screenshots/diagrams used in this book. You can download it here:
/>
Conventions used
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names,
folder names, filenames, file extensions, pathnames, dummy URLs,
user input, and Twitter handles. For example; " This means that
using tf.enable_eager_execution() is recommended."
A block of code is set as follows:

import tensorflow as tf # Import TensorFlow
x = tf.constant(8) # X op
y = tf.constant(9) # Y op
z = tf.multiply(x, y) # New op Z
sess = tf.Session() # Create TensorFlow session
out_z = sess.run(z) # execute Z op
sess.close() # Close TensorFlow session
print('The multiplication of x and y: %d' % out_z)# print result

When we wish to draw your attention to a particular part of a code
block, the relevant lines or items are set in bold:


import tensorflow as tf # Import TensorFlow
x = tf.constant(8) # X op
y = tf.constant(9) # Y op
z = tf.multiply(x, y) # New op Z
sess = tf.Session() # Create TensorFlow session
out_z = sess.run(z) # execute Z op
sess.close() # Close TensorFlow session
print('The multiplication of x and y: %d' % out_z)# print result

Any command-line input or output is written as follows:
>>>
MSE: 27.3749

Bold: Indicates a new term, an important word, or words that you
see on the screen, for example, in menus or dialog boxes, also
appear in the text like this. For example: " Now let's move to
http://localhost:6006 and on click on the GRAPH tab."

NOTE
Warnings or important notes appear in a box like this.

TIP
Tips and tricks appear like this.


Get in touch
Feedback from our readers is always welcome.
General feedback: Email <>, and mention
the book's title in the subject of your message. If you have questions
about any aspect of this book, please email us at
<>.
Errata: Although we have taken every care to ensure the accuracy
of our content, mistakes do happen. If you have found a mistake in
this book we would be grateful if you would report this to us. Please
visit, selecting your book,
clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any
form on the Internet, we would be grateful if you would provide us
with the location address or website name. Please contact us at
<> with a link to the material.
If you are interested in becoming an author: If there is a topic
that you have expertise in and you are interested in either writing or
contributing to a book, please visit .

Reviews
Please leave a review. Once you have read and used this book, why
not leave a review on the site that you purchased it from? Potential
readers can then see and use your unbiased opinion to make

purchase decisions, we at Packt can understand what you think
about our products, and our authors can see your feedback on their
book. Thank you!
For more information about Packt, please visit packtpub.com.


×