Book Image

Natural Language Processing with TensorFlow

By : Motaz Saad, Thushan Ganegedara
Book Image

Natural Language Processing with TensorFlow

By: Motaz Saad, Thushan Ganegedara

Overview of this book

Natural language processing (NLP) supplies the majority of data available to deep learning applications, while TensorFlow is the most important deep learning framework currently available. Natural Language Processing with TensorFlow brings TensorFlow and NLP together to give you invaluable tools to work with the immense volume of unstructured data in today’s data streams, and apply these tools to specific NLP tasks. Thushan Ganegedara starts by giving you a grounding in NLP and TensorFlow basics. You'll then learn how to use Word2vec, including advanced extensions, to create word embeddings that turn sequences of words into vectors accessible to deep learning algorithms. Chapters on classical deep learning algorithms, like convolutional neural networks (CNN) and recurrent neural networks (RNN), demonstrate important NLP tasks as sentence classification and language generation. You will learn how to apply high-performance RNN models, like long short-term memory (LSTM) cells, to NLP tasks. You will also explore neural machine translation and implement a neural machine translator. After reading this book, you will gain an understanding of NLP and you'll have the skills to apply TensorFlow in deep learning NLP applications, and how to perform specific NLP tasks.
Table of Contents (16 chapters)
Natural Language Processing with TensorFlow

Introduction to Keras

Here we will provide a brief introduction to Keras, which is a sublibrary of TensorFlow that provides more high-level functions for implementing deep learning algorithms. Keras uses basic TensorFlow operations, underneath; however, it exposes a higher level, beginner-friendly API for users. To see how to use Keras, we will look at a quick example. We will outline how one might create a CNN using Keras. Full exercise can be found at keras_cnn.ipynb located in the appendix folder.

We will first determine what type of a model we will be defining. Keras has two different APIs: sequential and functional. The sequential API is simpler and allows designing a model, layer by layer. However, the sequential API has limited flexibility in designing the organization and connections between layers of the network. On the other hand, the functional API has much more flexibility and allows the user to design the specific details of the neural network. For demonstration purposes, we will implement a CNN using the sequential API in Keras. A sequential model in this case is a sequence of stack of layers (for example, input layer, convolution layer, and pooling layer):

model = Sequential()

Next, we will define the layers of our CNN one by one. First, we will define a convolution layer with 32 filters, a kernel size of 3 × 3 and ReLU nonlinearity. This layer will be taking an input of size 28 × 28 × 1 (that is, the size of an MNIST image):

model.add(Conv2D(32, 3, activation='relu', input_shape=[28, 28, 1]))

Next, we will define a max-pooling layer. If the kernel size and stride are not defined, they default to 2 (kernel size) and 1 (stride):


Then we will add a batch normalization layer:


A batch normalization layer (refer to Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift, Ioffe and Szegedy, International Conference on Machine Learning, 2015) normalizes (that is, make activations zero-mean and unit-variance) the outputs of the previous layer. This is an additional step used to improve the performance of the CNN, especially in computer vision applications. Note that we did not use batch normalization in the chapter exercises, as the batch normalization has not been used heavily for NLP tasks, compared to the amount it is used for computer vision applications.

Next, we will add two more convolution layers, followed by a max-pooling layer and a batch normalization layer:

model.add(Conv2D(64, 3, activation='relu'))
model.add(Conv2D(128, 3, activation='relu'))

Next, we will flatten the input as this is required to feed the output into a fully connected layer:


Then we will add a fully connected layer with 256 hidden units, a ReLU activation, and a final softmax output layer with 10 softmax units (that is, for the 10 different classes of MNIST):

model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))

Finally, we will compile the model, when we also tell Keras to use Adam as the optimizer and categorical cross-entropy loss and output metric to be the accuracy of the model:

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Once the model, the loss and an optimizer is defined, we can run the Keras model as follows.

To train the model you can use the following command:, y_train, batch_size = batch_size)

Here, x_train and y_train are the training data. And  batch_size defines the batch size. When you run this, the training progress will be shown below.

Then to evaluate the model, use the following:

test_acc = model.evaluate(x_test, y_test, batch_size=batch_size)  

This line will again output a progress bar as well as the test loss and accuracy of each epoch.