0% found this document useful (0 votes)
2 views7 pages

DLT Experiment 2

The document outlines an experiment to design a neural network for classifying movie reviews from the IMDB dataset into positive or negative categories. It details the process of preparing the dataset, building the model using TensorFlow and Keras, and training the model while monitoring its performance. Additionally, it discusses the importance of validation and the potential for overfitting in the model's training process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

DLT Experiment 2

The document outlines an experiment to design a neural network for classifying movie reviews from the IMDB dataset into positive or negative categories. It details the process of preparing the dataset, building the model using TensorFlow and Keras, and training the model while monitoring its performance. Additionally, it discusses the importance of validation and the potential for overfitting in the model's training process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT-2

Aim: Design a Neural network for classifying movie reviews (binary


classification) using IMDB (Internet Movie Data Base)

This Expt. classifies movie reviews as positive or negative using the text of the review. This
is an example of binary—or two-class—classification, an important and widely applicable
kind of machine learning problem.
We'll use the IMDB dataset that contains the text of 50,000 movie reviews from the Internet
Movie Database. These are split into 25,000 reviews for training and 25,000 reviews for
testing. The training and testing sets are balanced, meaning they contain an equal number of
positive and negative reviews.

CODE:

Download the IMDB dataset


The IMDB dataset is available on TensorFlow datasets. The following code downloads the
IMDB dataset to your machine .

Let's take a moment to understand the format of the data. Each example is a sentence
representing the movie review and a corresponding label. The sentence is not pre-processed
in any way. The label is an integer value of either 0 or 1, where 0 is a negative review, and 1
is a positive review.

Let's print first 10 examples.


Let's also print the first 10 labels.

Build the model


The neural network is created by stacking layers—this requires three main architectural
decisions:

 How to represent the text?


 How many layers to use in the model?
 How many hidden units to use for each layer?

In this example, the input data consists of sentences. The labels to predict are either 0 or 1.

One way to represent the text is to convert sentences into embeddings vectors. We can use a
pre-trained text embedding as the first layer, which will have two advantages:

 we don't have to worry about text preprocessing,


 we can benefit from transfer learning.

For this example we will use a model from TensorFlow Hub called google/nnlm-en-dim50/2.

Let's first create a Keras layer that uses a TensorFlow Hub model to embed the sentences, and
try it out on a couple of input examples. Note that the output shape of the produced
embeddings is a expected: (num_examples, embedding_dimension).
Let's now build the full model:

The layers are stacked sequentially to build the classifier:


1. The first layer is a TensorFlow Hub layer. This layer uses a pre-trained Saved Model
to map a sentence into its embedding vector. The model that we are using
(google/nnlm-en-dim50/2) splits the sentence into tokens, embeds each token and
then combines the embedding. The resulting dimensions are: (num_examples,
embedding_dimension).

2. This fixed-length output vector is piped through a fully-connected (Dense) layer with
16 hidden units.
3. The last layer is densely connected with a single output node. This outputs logits: the
log-odds of the true class, according to the model.

Hidden units

The above model has two intermediate or "hidden" layers, between the input and output. The
number of outputs (units, nodes, or neurons) is the dimension of the representational space
for the layer. In other words, the amount of freedom the network is allowed when learning an
internal representation.

If a model has more hidden units (a higher-dimensional representation space), and/or more
layers, then the network can learn more complex representations. However, it makes the
network more computationally expensive and may lead to learning unwanted patterns—
patterns that improve performance on training data but not on the test data. This is
called overfitting, and we'll explore it later.

Loss function and optimizer

A model needs a loss function and an optimizer for training. Since this is a binary
classification problem and the model outputs a probability (a single-unit layer with a sigmoid
activation), we'll use the binary_crossentropy loss function.

This isn't the only choice for a loss function, you could, for instance,
choose mean_squared_error. But, generally, binary_crossentropy is better for dealing with
probabilities—it measures the "distance" between probability distributions, or in our case,
between the ground-truth distribution and the predictions.

Now, configure the model to use an optimizer and a loss function:

Create a validation set


When training, we want to check the accuracy of the model on data it hasn't seen before.
Create a validation set by setting apart 10,000 examples from the original training data. (Why
not use the testing set now? Our goal is to develop and tune our model using only the training
data, then use the test data just once to evaluate our accuracy).
Train the model
Train the model for 10 epochs in mini-batches of 512 samples. This is 10 iterations over all
samples in the x_train and y_train tensors. While training, monitor the model's loss and
accuracy on the 10,000 samples from the validation set:

Evaluate the model


And let's see how the model performs. Two values will be returned. Loss (a number which
represents our error, lower values are better), and accuracy.

Create a graph of accuracy and loss over time


model.fit() returns a History object that contains a dictionary with everything that happened
during training:
There are
four entries: one for each monitored metric during training and validation. We can use these
to plot the training and validation loss for comparison, as well as the training and validation
accuracy:

# plot the training and validation loss


CONCLUSIONS:

You might also like