Instant download Deep Learning for Natural Language Processing 1st Edition Palash Goyal pdf all chapter
Instant download Deep Learning for Natural Language Processing 1st Edition Palash Goyal pdf all chapter
com
https://ebookmeta.com/product/deep-learning-for-natural-
language-processing-1st-edition-palash-goyal/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/transfer-learning-for-natural-language-
processing-1st-edition-paul-azunre/
ebookmeta.com
https://ebookmeta.com/product/diabetes-meal-planning-nutrition-for-
dummies-2nd-edition-simon-poole/
ebookmeta.com
Devoted (The Bonding Trials Book 2) 1st Edition Evelyn
Flood
https://ebookmeta.com/product/devoted-the-bonding-trials-book-2-1st-
edition-evelyn-flood/
ebookmeta.com
https://ebookmeta.com/product/spell-of-an-omega-an-m-m-mpreg-shifter-
romance-riverrun-alphas-3-1st-edition-kaia-pierce/
ebookmeta.com
https://ebookmeta.com/product/my-hero-academia-vol-4-kohei-horikoshi/
ebookmeta.com
https://ebookmeta.com/product/the-vendetti-empire-ruthless-matteo-
vendetti-the-vendetti-famiglia-1-1st-edition-sapphire-knight-knight-
sapphire/
ebookmeta.com
Myths of the Iroquois 1st Edition Erminnie A Smith
https://ebookmeta.com/product/myths-of-the-iroquois-1st-edition-
erminnie-a-smith/
ebookmeta.com
Deep Learning for
Natural Language
Processing
Creating Neural Networks with Python
—
Palash Goyal
Sumit Pandey
Karan Jain
www.allitebooks.com
Deep Learning for
Natural Language
Processing
Creating Neural Networks
with Python
Palash Goyal
Sumit Pandey
Karan Jain
www.allitebooks.com
Deep Learning for Natural Language Processing: Creating Neural Networks
with Python
Palash Goyal Sumit Pandey
Bangalore, Karnataka, India Bangalore, Karnataka, India
Karan Jain
Bangalore, Karnataka, India
www.allitebooks.com
To our parents, sisters, brothers, and friends
without whom this book would have been
completed one year earlier :)
www.allitebooks.com
Table of Contents
About the Authors��������������������������������������������������������������������������������xi
About the Technical Reviewer�����������������������������������������������������������xiii
Acknowledgments������������������������������������������������������������������������������xv
Introduction��������������������������������������������������������������������������������������xvii
www.allitebooks.com
Table of Contents
Pattern�����������������������������������������������������������������������������������������������������������29
Stanford CoreNLP������������������������������������������������������������������������������������������29
Getting Started with NLP�������������������������������������������������������������������������������������29
Text Search Using Regular Expressions��������������������������������������������������������30
Text to List�����������������������������������������������������������������������������������������������������30
Preprocessing the Text����������������������������������������������������������������������������������31
Accessing Text from the Web������������������������������������������������������������������������32
Removal of Stopwords�����������������������������������������������������������������������������������32
Counter Vectorization������������������������������������������������������������������������������������33
TF-IDF Score��������������������������������������������������������������������������������������������������33
Text Classifier������������������������������������������������������������������������������������������������35
Introduction to Deep Learning����������������������������������������������������������������������������35
How Deep Is “Deep”?������������������������������������������������������������������������������������37
What Are Neural Networks?��������������������������������������������������������������������������������38
Basic Structure of Neural Networks��������������������������������������������������������������������40
Types of Neural Networks�����������������������������������������������������������������������������������45
Feedforward Neural Networks����������������������������������������������������������������������46
Convolutional Neural Networks���������������������������������������������������������������������46
Recurrent Neural Networks���������������������������������������������������������������������������47
Encoder-Decoder Networks���������������������������������������������������������������������������49
Recursive Neural Networks���������������������������������������������������������������������������49
Multilayer Perceptrons����������������������������������������������������������������������������������������50
Stochastic Gradient Descent�������������������������������������������������������������������������������54
Backpropagation�������������������������������������������������������������������������������������������������57
Deep Learning Libraries��������������������������������������������������������������������������������������60
Theano�����������������������������������������������������������������������������������������������������������60
Theano Installation����������������������������������������������������������������������������������������61
vi
Table of Contents
Theano Examples������������������������������������������������������������������������������������������63
TensorFlow����������������������������������������������������������������������������������������������������64
Data Flow Graphs������������������������������������������������������������������������������������������65
TensorFlow Installation����������������������������������������������������������������������������������66
TensorFlow Examples������������������������������������������������������������������������������������67
Keras�������������������������������������������������������������������������������������������������������������69
Next Steps�����������������������������������������������������������������������������������������������������������74
vii
Table of Contents
viii
Table of Contents
Index�������������������������������������������������������������������������������������������������269
ix
About the Authors
Palash Goyal is a senior data scientist and
currently works with the applications of
data science and deep learning in the online
marketing domain. He studied Mathematics
and Computing at the Indian Institute of
Technology (IIT) Guwahati and proceeded to
work in a fast-paced upscale environment.
He has wide experience in E-commerce
and travel, insurance, and banking industries.
Passionate about mathematics and finance,
Palash manages his portfolio of multiple
cryptocurrencies and the latest Initial Coin
Offerings (ICOs) in his spare time, using deep learning and reinforcement
learning techniques for price prediction and portfolio management. He
keeps in touch with the latest trends in the data science field and shares
these on his personal blog, http://madoverdata.com, and mines articles
related to smart farming in free time.
xi
About the Authors
xii
About the Technical Reviewer
Santanu Pattanayak currently works at GE
Digital as a staff data scientist and is the author
of the deep learning–related book Pro Deep
Learning with TensorFlow—A Mathematical
Approach to Advanced Artificial Intelligence
in Python. He has about 12 years of overall
work experience, 8 in the data analytics/
data science field, and has a background in
development and database technologies.
Prior to joining GE, Santanu worked in
such companies as RBS, Capgemini, and IBM. He graduated with a degree
in electrical engineering from Jadavpur University, Kolkata, and is an avid
math enthusiast. Santanu is currently pursuing a master’s degree in data
science from IIT Hyderabad. He also devotes his time to data science
hackathons and Kaggle competitions, in which he ranks within the top 500
across the globe. Santanu was born and brought up in West Bengal, India,
and currently resides in Bangalore, India, with his wife.
xiii
Acknowledgments
This work would not have been possible without those who saw us through
this book, to all those who believed in us, talked things over, read, wrote,
and offered their valuable time throughout the process, and allowed us to
use the knowledge that we gained together, be it for proofreading or overall
design.
We are especially indebted to Aditee Mirashi, coordinating editor,
Apress, Springer Science+Business, who has been a constant support and
motivator to complete the task and who worked actively to provide us with
valuable suggestions to pursue our goals on time.
We are grateful to Santanu Pattanayak, who went through all the
chapters and provided valuable input, giving final shape to the book.
Nobody has been more important to us in the pursuit of this project
than our family members. We would like to thank our parents, whose love
and guidance are with us in whatever we pursue. Their being our ultimate
role models has provided us unending inspiration to start and finish the
difficult task of writing and giving shape to our knowledge.
xv
Introduction
This book attempts to simplify and present the concepts of deep learning
in a very comprehensive manner, with suitable, full-fledged examples of
neural network architectures, such as Recurrent Neural Networks (RNNs)
and Sequence to Sequence (seq2seq), for Natural Language Processing
(NLP) tasks. The book tries to bridge the gap between the theoretical and
the applicable.
It proceeds from the theoretical to the practical in a progressive
manner, first by presenting the fundamentals, followed by the underlying
mathematics, and, finally, the implementation of relevant examples.
The first three chapters cover the basics of NLP, starting with the most
frequently used Python libraries, word vector representation, and then
advanced algorithms like neural networks for textual data.
The last two chapters focus entirely on implementation, dealing with
sophisticated architectures like RNN, Long Short-Term Memory (LSTM)
Networks, Seq2seq, etc., using the widely used Python tools TensorFlow
and Keras. We have tried our best to follow a progressive approach,
combining all the knowledge gathered to move on to building a question-
and-answer system.
The book offers a good starting point for people who want to get
started in deep learning, with a focus on NLP.
All the code presented in the book is available on GitHub, in the form
of IPython notebooks and scripts, which allows readers to try out these
examples and extend them in interesting, personal ways.
xvii
CHAPTER 1
Introduction
to Natural Language
Processing and Deep
Learning
Natural language processing (NPL) is an extremely difficult task in
computer science. Languages present a wide variety of problems that
vary from language to language. Structuring or extracting meaningful
information from free text represents a great solution, if done in the
right manner. Previously, computer scientists broke a language into its
grammatical forms, such as parts of speech, phrases, etc., using complex
algorithms. Today, deep learning is a key to performing the same exercises.
This first chapter of Deep Learning for Natural Language Processing
offers readers the basics of the Python language, NLP, and Deep Learning.
First, we cover the beginner-level codes in the Pandas, NumPy, and SciPy
libraries. We assume that the user has the initial Python environment
(2.x or 3.x) already set up, with these libraries installed. We will also briefly
discuss commonly used libraries in NLP, with some basic examples.
Finally, we will discuss the concepts behind deep learning and some
common frameworks, such as TensorFlow and Keras. Then, in later
chapters, we will move on to providing a higher level overview of NLP.
Depending on the machine and version preferences, one can install
Python by using the following references:
• www.python.org/downloads/
• www.continuum.io/downloads
The preceding links and the basic packages installations will provide
the user with the environment required for deep learning.
We will be using the following packages to begin. Please refer to the
following links, in addition to the package name for your reference:
Python Machine Learning
Pandas (http://pandas.pydata.org/pandas-docs/stable)
NumPy (www.numpy.org)
SciPy (www.scipy.org)
TensorFlow (http://tensorflow.org/)
Keras (https://keras.io/)
Spacy (https://spacy.io/)
NLTK (www.nltk.org/)
TextBlob (http://textblob.readthedocs.io/en/dev/)
2
Chapter 1 Introduction to Natural Language Processing and Deep Learning
P
ython Packages
We will be covering the references to the installation steps and the initial-
level coding for the Pandas, NumPy, and SciPy packages. Currently,
Python offers versions 2.x and 3.x, with compatible functions for machine
learning. We will be making use of Python2.7 and Python3.5, where
required. Version 3.5 has been used extensively throughout the chapters of
this book.
N
umPy
NumPy is used particularly for scientific computing in Python. It is designed
to efficiently manipulate large multidimensional arrays of arbitrary records,
without sacrificing too much speed for small multidimensional arrays. It
could also be used as a multidimensional container for generic data. The
ability of NumPy to create arrays of arbitrary type, which also makes NumPy
suitable for interfacing with general-purpose data-base applications, makes
it one of the most useful libraries you are going to use throughout this book,
or thereafter for that matter.
3
Chapter 1 Introduction to Natural Language Processing and Deep Learning
Following are the codes using the NumPy package. Most of the lines
of code have been appended with a comment, to make them easier to
understand by the user.
## Numpy
2 in b
#'in' searches for the element in the array
> True
0 in b
> False
4
Chapter 1 Introduction to Natural Language Processing and Deep Learning
5
Chapter 1 Introduction to Natural Language Processing and Deep Learning
print(p.shape)
a1 = np.array([[1,2],[3,4],[5,6]], float)
a2 = np.array([-1,3], float)
print(a1+a2)
> [[ 0. 5.] [ 2. 7.] [ 4. 9.]]
6
Chapter 1 Introduction to Natural Language Processing and Deep Learning
One can refer to the following sources for detailed tutorials on NumPy:
www.numpy.org/ and https://docs.scipy.org/doc/numpy-dev/user/
quickstart.html.
NumPy offers few of the functions that are directly applicable on the
arrays: sum (summation of elements), prod (product of the elements), mean
(mean of the elements), var (variance of the elements), std (standard
deviation of the elements), argmin (index of the smallest element in array),
argmax (index of the largest element in array), sort (sort the elements),
unique (unique elements of the array).
a3 = np.array([[0,2],[3,-1],[3,5]], float)
print(a3.mean(axis=0)) # Mean of elements column-wise
> [ 2. 2.]
NumPy offers functions for testing the values present in the array,
such as nonzero (checks for nonzero elements), isnan (checks for “not
a number” elements), and isfinite (checks for finite elements). The
where function returns an array with the elements satisfying the following
conditions:
a4 = np.array([1,3,0], float)
np.where(a!=0, 1/a ,a)
> array([ 0.2 , 0.25 , 0.2 , 0.125])
7
Chapter 1 Introduction to Natural Language Processing and Deep Learning
np.random.rand(2,3)
> array([[ 0.41453991, 0.46230172, 0.78318915],
[0.54716578, 0.84263735, 0.60796399]])
Pandas
Pandas is an open sourced software library. DataFrames and Series are two
of its major data structures that are widely used for data analysis purposes.
Series is a one-dimensional indexed array, and DataFrame is tabular data
structure with column- and row-level indexes. Pandas is a great tool for
preprocessing datasets and offers highly optimized performance.
import pandas as pd
series_1 = pd.Series([2,9,0,1]) # Creating a series object
print(series_1.values) # Print values of the
series object
> [2 9 0 1]
8
Chapter 1 Introduction to Natural Language Processing and Deep Learning
class_df.Names
>Student1 John
Student2 Ryan
Student3 Emily
Name: Names, dtype: object
9
Chapter 1 Introduction to Natural Language Processing and Deep Learning
10
Chapter 1 Introduction to Natural Language Processing and Deep Learning
# Concatenation of 2 dataframes
student_age = pd.DataFrame(data = {'Age': [13,10,15,18]} ,
index=['Student1','Student2',
'Student3','Student4'])
print(student_age)
> Age
Student1 13
Student2 10
Student3 15
Student4 18
# MAP Function
class_data['Subject'] = class_data['Subject'].map(lambda x :
x + 'Sub')
class_data['Subject']
11
Chapter 1 Introduction to Natural Language Processing and Deep Learning
# APPLY Function
def age_add(x): # Defining a new function which
will increment the age by 1
return(x+1)
print('-----Old values-----')
print(class_data['Age'])
print('-----New values-----')
print(class_data['Age'].apply(age_add)) # Applying the age
function on top of
the age column
> -----Old values-----
Student1 13
Student2 10
Student3 15
Student4 18
Name: Age, dtype: int64
-----New values-----
Student1 14
Student2 11
Student3 16
Student4 19
Name: Age, dtype: int64
12
Chapter 1 Introduction to Natural Language Processing and Deep Learning
S
ciPy
SciPy offers complex algorithms and their use as functions in NumPy. This
allocates high-level commands and a variety of classes to manipulate and
visualize data. SciPy is curated in the form of multiple small packages,
with each package targeting individual scientific computing domains. A
few of the subpackages are linalg (linear algebra), constants (physical
and mathematical constants), and sparse (sparse matrices and associated
routines).
Most of the NumPy package functions applicable on arrays are also
included in the SciPy package. SciPy offers pre-tested routines, thereby
saving a lot of processing time in the scientific computing applications.
import scipy
import numpy as np
13
Chapter 1 Introduction to Natural Language Processing and Deep Learning
Following are a few examples from Linalg and Stats out of multiple
subpackages offered by SciPy. As the subpackages are domain-specific, it
makes SciPy the perfect choice for data science.
SciPy subpackages, here for linear algebra (scipy.linalg), are
supposed to be imported explicitly in the following manner:
The code for performing singular value decomposition and storing the
individual components follows:
14
Chapter 1 Introduction to Natural Language Processing and Deep Learning
15
Chapter 1 Introduction to Natural Language Processing and Deep Learning
16
Discovering Diverse Content Through
Random Scribd Documents
—— Some Common Types of Lichen Formations. Torrey Bot. Club,
xxx. pp. 412-418, 1903.
—— Lichenologische Beiträge, xii. and xiv. Flora, lxiv. p. 111 and pp.
513-527, 1881; Beiträge, xxi. op. cit. lxviii. p. 345, 1885;
Beiträge, xxxiii. op. cit. lxxiii. p. 202, 1890.
—— Lichens de Palestine. Rev. Mycol. vi. pp. 12-13, 1884.
—— Enumeratio Lichenum aegyptiacorum. Tom. cit. pp. 13-20,
1884.
—— Lichenes Persici, etc. Hedwigia, xxxi. pp. 151-159, 1892.
Müller, Karl. Die chemische Zusammensetzung der Zellmembranen
bei verschiedenen Kryptogamen. Zeitschr. Phys. Chemie, xlv. pp.
265-298, 1905.
Münster, Georg, Count. Beiträge zur Petrefacten-Kunde. Heft vi.,
1846.
Necker, N. J. de. Methodus Muscorum, etc. Mannheim, 1771.
Nees von Esenbeck. Handbuch der Botanik, i. Nürnberg, 1820.
Neubert, Adolf. i. Toxikologische Studien über einige organische
Säuren. Diss. Dorpat (Jurjew), 1893. See Zopf, Die
Flechtenstoffe, p. 376, 1907.
Neubner, Eduard. Untersuchungen über den Thallus und die
Fruchtanfänge der Calycieen. Wiss. Beil. iv. Jahresber. K.
Gymnas. Plauen I. v. 12 pp., 1893.
Nienburg, W. Beiträge zur Entwickelungsgeschichte einiger
Flechtenapothecien. Flora, xcviii. pp. 1-40 (7 pls., 3 figs.), 1908.
—— Ueber die Beziehungen zwischen den Algen und Hyphen im
Flechtenthallus. Zeitschr. für Bot. ix. pp. 529-543 (1 pl., 6 figs.),
1917.
Nilson, Birger. Zur Entwickelungsgeschichte, Morphologie und
Systematik der Flechten. Bot. Not. pp. 1-33, 1903.
—— Die Flechtenvegetation des Sarekgebirges. Hamberg’s Nat.
Wiss. Untersuch. Sarekgebirges in Schwedisch-Lappland, iii. pp.
1-70 (9 pls.), Stockholm, 1907; Bot. Centralbl cv. p. 309, 1907.
Norman, J. M. Conatus praemissus redactionis novae generum, etc.
Nyt. Mag. Naturvid. vii. pp. 213-252, 1852.
—— Fuligines lichenosae Moriolei. Bot. Not. pp. 9-20, 1872.
—— Allelositismus eller det forhold, etc. K. Norsk. Vid. Selsk. Skrift.
vii. pp. 243-255 [1872], 1874.