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 1

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)

# Pad sequences with max_len.

X_train = preprocessing.sequence.pad_sequences(X_train,

maxlen=max_len)

X_test = preprocessing.sequence.pad_sequences(X_test,

maxlen=max_len)

return (X_train, y_train), (X_test, y_test)

Now let's build a model. We are going to use a few layers that will be explained

in detail in Chapter 8, Recurrent Neural Networks. For now, let's assume that the

Embedding() layer will map the sparse space of words contained in the reviews

into a denser space. This will make computation easier. In addition, we will use a

GlobalMaxPooling1D() layer, which takes the maximum value of either feature

vector from each of the n_words features. In addition, we have two Dense() layers.

The last one is made up of one single neuron with a sigmoid activation function for

making the final binary estimation:

def build_model():

model = models.Sequential()

# Input: - eEmbedding Layer.

# The model will take as input an integer matrix of size (batch,

# input_length).

# The model will output dimension (input_length, dim_embedding).

# The largest integer in the input should be no larger

# than n_words (vocabulary size).

model.add(layers.Embedding(n_words,

dim_embedding, input_length=max_len))

model.add(layers.Dropout(0.3))

# Takes the maximum value of either feature vector from each of

# the n_words features.

[ 43 ]

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

Saved successfully!

Ooh no, something went wrong!