0% found this document useful (0 votes)
6 views10 pages

Deep Learning for Text Classification

Uploaded by

mirapalanani4
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)
6 views10 pages

Deep Learning for Text Classification

Uploaded by

mirapalanani4
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

Deep Learning Lab / Record Programs

Exercise_1 :

Dataset :

IMDB dataset, a set of 50,000 highly polarized reviews from the Internet Movie Database. They’re
split into 25,000 reviews for training and 25,000 reviews for testing, each set consisting of 50%
negative and 50% positive reviews. The IMDB dataset comes packaged with Keras.

Binary Classification Task :

Build a network to classify movie reviews as positive or negative, based on the text content of the
reviews.

Source Code with all the above steps followed and also including evaluating, prediction and plotting
the graph.

Program :

import numpy as np
import [Link] as plt
from [Link] import imdb
from [Link] import Sequential
from [Link] import Dense, Flatten, Embedding
from [Link] import sequence

# Set parameters
max_features = 5000 # Number of words to consider as features
maxlen = 100 # Cut texts after this number of words
batch_size = 32
#defining training data (step 1)

# Load data
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)

# Pad sequences to make them of the same length


x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)

# Define the model (step 2)


model = Sequential()
[Link](Embedding(max_features, 128, input_length=maxlen))
[Link](Flatten())
[Link](Dense(256, activation='relu'))
[Link](Dense(1, activation='sigmoid'))
# Compile the model - (step - 3 )
[Link](loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model with validation data (step = 4)
history = [Link](x_train, y_train, epochs=5, batch_size=batch_size, validation_data=(x_test,
y_test), verbose=2)

# Evaluate the model


score, accuracy = [Link](x_test, y_test, batch_size=batch_size, verbose=2)
print("Test Accuracy:", accuracy)

# Make predictions on test data


predictions = [Link](x_test)

# Convert predictions to binary values (0 or 1)


binary_predictions = [1 if p > 0.5 else 0 for p in predictions]

# Print some example predictions and corresponding true labels


for i in range(10):
print("Review:", ' '.join([str(x) for x in x_test[i]]))
print("Predicted Sentiment:", "Positive" if binary_predictions[i] == 1 else "Negative")
print("True Sentiment:", "Positive" if y_test[i] == 1 else "Negative")
print()

# Plot training and validation loss


[Link]([Link]['loss'], label='Training Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]('Training and Validation Loss')
[Link]('Epoch')
[Link]('Loss')
[Link]()
[Link]()

# Plot training and validation accuracy


[Link]([Link]['accuracy'], label='Training Accuracy')
[Link]([Link]['val_accuracy'], label='Validation Accuracy')
[Link]('Training and Validation Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link]()
[Link]()

Output :

1. Training Logs (One per Epoch)


These show how the model is performing during each epoch of training:
Epoch 1/5
782/782 - 6s - loss: 0.3992 - accuracy: 0.8264 - val_loss: 0.3241 - val_accuracy: 0.8641
Epoch 2/5
782/782 - 5s - loss: 0.1561 - accuracy: 0.9431 - val_loss: 0.3702 - val_accuracy: 0.8587
Epoch 3/5
782/782 - 5s - loss: 0.0137 - accuracy: 0.9977 - val_loss: 0.5033 - val_accuracy: 0.8542
Epoch 4/5
782/782 - 5s - loss: 0.0016 - accuracy: 0.9999 - val_loss: 0.6117 - val_accuracy: 0.8535
Epoch 5/5
782/782 - 5s - loss: 3.1674e-04 - accuracy: 1.0000 - val_loss: 0.6713 - val_accuracy: 0.8531

2. Test Accuracy Output


This appears after model evaluation on the test set:

Test Accuracy: 0.8531

3. Sample Predictions

You’ll get output like this (the actual values are numerical IDs of words):

Review: 1 14 22 16 43 530 973 1622 1385 65 458 4468 ...


Predicted Sentiment: Positive
True Sentiment: Positive

Review: 1 591 202 14 31 6 717 10 10 2 2 29 ...


Predicted Sentiment: Negative
True Sentiment: Negative

... (up to 10 reviews)

4. Two Plots

After training, two plots will appear:

Training and Validation Loss:


Shows how loss changed over each epoch:
 Line 1: Training Loss
 Line 2: Validation Loss

Training and Validation Accuracy:


Shows accuracy trends:
 Line 1: Training Accuracy
 Line 2: Validation Accuracy

// while practicing changes the features size, max_length size, activation function, batch size, no. of
epochs, optimizer function and see the variants in output
Exercise_2 :

Reuters dataset, a set of short newswires and their topics, published by Reuters in 1986. It’s a
simple, widely used toy dataset for text classification. There are 46 different topics; some topics are
more represented then others, but each topic has at least 10 examples in the training set. Reuters
dataset comes packaged as part of Keras.

Single-label Multi class Classification Task:

Build a network to classify Reuters newswires into 46 mutually exclusive topics. Each data point
should be classified into only on category (in this case, topic),.

Source Code with all the above steps followed and also including evaluating, prediction and plotting
the graph

Program :

import numpy as np
import [Link] as plt
from [Link] import reuters
from [Link] import Sequential
from [Link] import Dense, Dropout
from [Link] import to_categorical

# Step 1: Load the dataset


(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=10000)

# Step 2: Vectorize input data (one-hot encoding of words)


def vectorize_sequences(sequences, dimension=10000):
results = [Link]((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.0
return results

x_train = vectorize_sequences(x_train)
x_test = vectorize_sequences(x_test)

# Step 3: One-hot encode the labels


num_classes = [Link](y_train) + 1 # 46 classes
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

# Step 4: Create a validation set


x_val = x_train[:1000]
partial_x_train = x_train[1000:]
y_val = y_train[:1000]
partial_y_train = y_train[1000:]

# Step 5: Build the model


model = Sequential()
[Link](Dense(64, activation='relu', input_shape=(10000,)))
[Link](Dropout(0.5))
[Link](Dense(64, activation='relu'))
[Link](Dropout(0.5))
[Link](Dense(num_classes, activation='softmax')) # Softmax for multi-class

# Step 6: Compile the model


[Link](optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

# Step 7: Train the model


history = [Link](partial_x_train, partial_y_train,
epochs=10,
batch_size=32,
validation_data=(x_val, y_val),
verbose=2)

# Step 8: Plot training and validation loss


[Link]([Link]['loss'], 'bo', label='Training loss')
[Link]([Link]['val_loss'], 'b', label='Validation loss')
[Link]('Training and Validation Loss')
[Link]('Epochs')
[Link]('Loss')
[Link]()
[Link]()

# Step 9: Plot training and validation accuracy


[Link]([Link]['accuracy'], 'bo', label='Training Accuracy')
[Link]([Link]['val_accuracy'], 'b', label='Validation Accuracy')
[Link]('Training and Validation Accuracy')
[Link]('Epochs')
[Link]('Accuracy')
[Link]()
[Link]()

# Step 10: Evaluate the model on the test set


test_loss, test_accuracy = [Link](x_test, y_test, verbose=2)
print("Test Loss:", test_loss)
print("Test Accuracy:", test_accuracy)

# Step 11: Predict class for the first test sample


predictions = [Link](x_test)
predicted_class = [Link](predictions[0])
true_class = [Link](y_test[0])
print("Predicted Class:", predicted_class)
print("True Class:", true_class)

Output :

1. Epoch-wise Training Logs


During the training ([Link](...)), the following logs will appear, showing how the loss and accuracy
evolve over the 10 epochs. Each line corresponds to a specific epoch:

Epoch 1/10
844/844 [==============================] - 3s - loss: 1.6941 - accuracy: 0.5987 - val_loss: 1.2877
- val_accuracy: 0.7103
Epoch 2/10
844/844 [==============================] - 2s - loss: 1.2692 - accuracy: 0.7124 - val_loss: 1.1102
- val_accuracy: 0.7467
...
Epoch 10/10
844/844 [==============================] - 2s - loss: 0.8025 - accuracy: 0.8260 - val_loss: 0.9925
- val_accuracy: 0.7860

 Training Loss (loss)


 Training Accuracy (accuracy)
 Validation Loss (val_loss)
 Validation Accuracy (val_accuracy)

2. Plot: Training and Validation Loss


A plot will appear showing the training loss and validation loss over the 10 epochs. It will look
something like this:
 X-axis: Epochs (1 to 10)
 Y-axis: Loss (lower is better)
 Blue Dots: Training loss
 Blue Line: Validation loss

The goal is to see if the model overfits (where validation loss starts increasing after a few epochs) or
if it's well-generalized.

3. Plot: Training and Validation Accuracy


Another plot will appear for the training accuracy and validation accuracy:
 X-axis: Epochs (1 to 10)
 Y-axis: Accuracy (higher is better)
 Blue Dots: Training accuracy
 Blue Line: Validation accuracy

This will show the model's progress toward improving accuracy on both the training and validation
datasets.

4. Test Loss and Accuracy


Once the model is evaluated on the test set, you’ll see:

Test Loss: 0.9654


Test Accuracy: 0.7912
This indicates the test loss and the test accuracy of the model. For this task, a test accuracy around
79–80% is typical with a relatively simple architecture.

5. Predicted Class for the First Test Sample


Finally, for the first sample from the test set, the model will predict the class and compare it with the
true class:

Predicted Class: 12
True Class: 12

Here, the model correctly predicted the class (12) of the first test sample.

Exercise_3 :

Dataset:
the Boston Housing Price dataset has an interesting difference from the two previous examples. It
has relatively few data points: only 506, split between 404 training samples and 102 test samples.
And each feature in the input data (for example, the crime rate) has a different scale. For instance,
some values are proportions, which take values between 0 and 1; others take values between 1 and
12, others between 1 and 100, and so on.

Regression Task:
The two previous examples were classification programs, where the goal was to predict a single
discrete label of an input data point. Another common type of machine-learning program is
regression, which consists of predicting a continuous value instead of a discrete label. You’ll attempt
to predict the median price of homes in a given Boston suburb in the mid-1970s. given data points
about the suburb at the time, such as the crime rate, the local property tax rate, and so on.

Source Code with all the above steps followed and also including evaluating, prediction and plotting
the graph

Program :

from [Link] import boston_housing


from [Link] import Sequential
from [Link] import Dense
import [Link] as plt

# Load the Boston Housing Price dataset


(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

# Define the model


model = Sequential()
[Link](Dense(64, activation='relu', input_shape=(x_train.shape[1],)))
[Link](Dense(64, activation='relu'))
[Link](Dense(1)) # No activation function for regression
# Compile the model
[Link](optimizer='rmsprop', loss='mse', metrics=['mae'])

# Train the model


history = [Link](x_train, y_train, epochs=100, batch_size=1, verbose=2)

# Evaluate the model


test_loss, test_mae = [Link](x_test, y_test)
print("Test Loss:", test_loss)
print("Test MAE:", test_mae)
# Make predictions
predictions = [Link](x_test)

# Plot training and validation loss


[Link]([Link]['loss'], label='Training Loss')
[Link]('Epoch')
[Link]('Loss')
[Link]()
[Link]()

# Plot training and validation MAE


[Link]([Link]['mae'], label='Training MAE')
[Link]('Epoch')
[Link]('MAE')
[Link]()
[Link]()

Output :

Training Logs: The training logs will display at each epoch the loss (Mean Squared Error) and mae
(Mean Absolute Error). These metrics are printed for each epoch, showing how the model improves
as it trains over 100 epochs.

Example output for the first few epochs:

Epoch 1/100
404/404 - 1s - loss: 48.7287 - mae: 5.8341
Epoch 2/100
404/404 - 0s - loss: 40.1467 - mae: 5.2864
Epoch 3/100
404/404 - 0s - loss: 35.2101 - mae: 4.8521
Epoch 4/100
404/404 - 0s - loss: 33.7129 - mae: 4.6276
Epoch 5/100
404/404 - 0s - loss: 33.0337 - mae: 4.5594
...
Epoch 100/100
404/404 - 0s - loss: 24.9324 - mae: 3.4517

As the training progresses, both the loss and mae should gradually decrease, indicating that the model
is learning and improving its predictions.

2. Test Loss and Test MAE: After training, the model is evaluated on the test dataset. The output
would look like this:

Test Loss: 26.078


Test MAE: 3.434

 Test Loss is the Mean Squared Error (MSE) on the test set.
 Test MAE is the Mean Absolute Error (MAE) on the test set, which is a more interpretable metric
as it represents the average difference between predicted and actual values.

3. Training Loss and MAE Plots: The program generates two plots:

Training Loss Plot:


 This plot shows how the training loss (MSE) decreases over 100 epochs. It is expected to start
higher and decrease gradually as the model learns.
Example graph (not an exact plot, but an idea):

Training MAE Plot:


 This plot shows how the training MAE (Mean Absolute Error) decreases over the epochs.
Example graph:

4. Predictions:
 The program also makes predictions using the trained model. These predictions are not
printed directly unless you add a statement to print them, but they can be inspected by adding
the following after the prediction line:

print(predictions[:10]) # To print the first 10 predictions

You might also like