09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5

You might also notice that a text is not really a matrix but more a vector because two

words located in adjacent rows of text have very little in common. Indeed, this is a

major difference when compared with images, where two pixels located in adjacent

columns are likely to have some degree of correlation.

Now you might wonder: I understand that you represent the text as a vector but,

in doing so, we lose the position of the words. This position should be important,

shouldn't it? Well, it turns out that in many real applications, knowing that whether

a sentence contains a particular basic unit (a char, a word, an aggregate) or not is

pretty useful information even if we don't keep track of where exactly in the sentence

this basic unit is located.

For instance, ConvNets achieve pretty good results for "Sentiment Analysis" where

we need to understand if a piece of text has a positive or a negative sentiment;

for "Spam Detection" where we need to understand if a piece of text is useful

information or spam; and for "Topic Categorization", where we need to understand

what a piece of text is all about. However, ConvNets are not well suited for a Partof-Speech

(POS) analysis, where the goal is to understand what the logical role of

every single word is (for example, a verb, an adverb, a subject, and so on and so

forth). ConvNets are also not well suited for "Entity Extraction," where we need

to understand where relevant entities are located in sentences. Indeed, it turns out

that a position is pretty useful information for both of the last two use cases. 1D

ConvNets are very similar to 2D ConvNets. However, the former operates on a

single vector, while the latter operates on matrices.

Using a CNN for sentiment analysis

Let's have a look at the code. First of all, we load the dataset with tensorflow_

datasets. In this case we use IMDb, a collection of movie reviews:

import tensorflow as tf

from tensorflow.keras import datasets, layers, models, preprocessing

import tensorflow_datasets as tfds

max_len = 200

n_words = 10000

dim_embedding = 256

EPOCHS = 20

BATCH_SIZE =500

def load_data():

#load data

(X_train, y_train), (X_test, y_test) = datasets.imdb.load_

data(num_words=n_words)

[ 175 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!