0% found this document useful (0 votes)
29 views

Experiment 3 (D, E) (Embedding) (Plotting) PDF

PLOTTING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Experiment 3 (D, E) (Embedding) (Plotting) PDF

PLOTTING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

TRAIN THE MODEL WITH EMBEDDING AND SIMPLE RNN LAYERS

PROGRAM:

import tensorflow as tf

from tensorflow.keras.datasets import imdb

from tensorflow.keras.preprocessing.sequence import pad_sequences

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Embedding, SimpleRNN, Dense

# Parameters

vocab_size = 10000 # Only consider the top 10,000 words

maxlen = 500 # Only consider the first 500 words of each review

embedding_dim = 32 # Dimension of the embedding vector

# Load the IMDB dataset

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)

# Pad sequences to ensure uniform input size

x_train = pad_sequences(x_train, maxlen=maxlen)

x_test = pad_sequences(x_test, maxlen=maxlen)

# Build the model

model = Sequential()

model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim,
input_length=maxlen))

model.add(SimpleRNN(100)) # SimpleRNN layer with 100 units


model.add(Dense(1, activation='sigmoid'))

# Compile the model

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

# Print the model summary

model.summary()

# Train the model

model.fit(x_train, y_train, epochs=3, batch_size=64, validation_split=0.2)

# Evaluate the model

loss, accuracy = model.evaluate(x_test, y_test)

print(f"Test Loss: {loss}")

print(f"Test Accuracy: {accuracy}")


OUTPUT:

/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/embedding.py:90:
UserWarning: Argument `input_length` is deprecated. Just remove it.

warnings.warn(
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━
━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━
━━━━┩
│ embedding (Embedding) │ ? │ 0
(unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ simple_rnn (SimpleRNN) │ ? │ 0
(unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ dense (Dense) │ ? │ 0
(unbuilt) │
└──────────────────────────────────────┴─────────────────────────────┴─────────────
────┘
Total params: 0 (0.00 B)

Trainable params: 0 (0.00 B)

Non-trainable params: 0 (0.00 B)

Epoch 1/3

313/313 ━━━━━━━━━━━━━━━━━━━━ 84s 260ms/step - accuracy: 0.5576 - loss: 0.6773 -


val_accuracy: 0.8082 - val_loss: 0.4277

Epoch 2/3

313/313 ━━━━━━━━━━━━━━━━━━━━ 75s 237ms/step - accuracy: 0.7789 - loss: 0.5334 -


val_accuracy: 0.7218 - val_loss: 0.5456

Epoch 3/3

313/313 ━━━━━━━━━━━━━━━━━━━━ 82s 239ms/step - accuracy: 0.7633 - loss: 0.5021 -


val_accuracy: 0.7026 - val_loss: 0.5642

782/782 ━━━━━━━━━━━━━━━━━━━━ 37s 47ms/step - accuracy: 0.7066 - loss: 0.5609

Test Loss: 0.5561356544494629

Test Accuracy: 0.7092000246047974


PLOTTING THE RESULTS

PROGRAM:

import tensorflow as tf

from tensorflow.keras.datasets import imdb

from tensorflow.keras.preprocessing.sequence import pad_sequences

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Embedding, SimpleRNN, Dense

import matplotlib.pyplot as plt

# Parameters

vocab_size = 10000 # Only consider the top 10,000 words

maxlen = 500 # Only consider the first 500 words of each review

embedding_dim = 32 # Dimension of the embedding vector

# Load the IMDB dataset

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)

# Pad sequences to ensure uniform input size

x_train = pad_sequences(x_train, maxlen=maxlen)

x_test = pad_sequences(x_test, maxlen=maxlen)


# Build the model

model = Sequential()

model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim,
input_length=maxlen))

model.add(SimpleRNN(100)) # SimpleRNN layer with 100 units

model.add(Dense(1, activation='sigmoid'))

# Compile the model

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

# Print the model summary

model.summary()

# Train the model

history = model.fit(x_train, y_train, epochs=3, batch_size=64, validation_split=0.2)

# Evaluate the model

loss, accuracy = model.evaluate(x_test, y_test)

print(f"Test Loss: {loss}")

print(f"Test Accuracy: {accuracy}")

# Plot the training and validation loss and accuracy

plt.figure(figsize=(12, 4))

# Plot training & validation accuracy values


plt.subplot(1, 2, 1)

plt.plot(history.history['accuracy'], label='Train Accuracy')

plt.plot(history.history['val_accuracy'], label='Validation Accuracy')

plt.title('Model Accuracy')

plt.xlabel('Epoch')

plt.ylabel('Accuracy')

plt.legend(loc='upper left')

# Plot training & validation loss values

plt.subplot(1, 2, 2)

plt.plot(history.history['loss'], label='Train Loss')

plt.plot(history.history['val_loss'], label='Validation Loss')

plt.title('Model Loss')

plt.xlabel('Epoch')

plt.ylabel('Loss')

plt.legend(loc='upper left')

plt.tight_layout()

plt.show()
OUTPUT:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-


datasets/imdb.npz

17464789/17464789 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/embedding.py:90:
UserWarning: Argument `input_length` is deprecated. Just remove it warnings.warn(

Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━
━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━
━━━━┩
│ embedding (Embedding) │ ? │ 0
(unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ simple_rnn (SimpleRNN) │ ? │ 0
(unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ dense (Dense) │ ? │ 0
(unbuilt) │
└──────────────────────────────────────┴─────────────────────────────┴─────────────
────┘
Total params: 0 (0.00 B)

Trainable params: 0 (0.00 B)

Non-trainable params: 0 (0.00 B)

Epoch 1/3

313/313 ━━━━━━━━━━━━━━━━━━━━ 67s 207ms/step - accuracy: 0.5453 - loss:


0.6815 - val_accuracy: 0.7312 - val_loss: 0.5341

Epoch 2/3

313/313 ━━━━━━━━━━━━━━━━━━━━ 82s 207ms/step - accuracy: 0.7731 - loss:


0.4889 - val_accuracy: 0.6962 - val_loss: 0.5694

Epoch 3/3
313/313 ━━━━━━━━━━━━━━━━━━━━ 82s 208ms/step - accuracy: 0.7773 - loss:
0.4860 - val_accuracy: 0.8114 - val_loss: 0.4508

782/782 ━━━━━━━━━━━━━━━━━━━━ 31s 40ms/step - accuracy: 0.8051 - loss:


0.4589

Test Loss: 0.45079389214515686

Test Accuracy: 0.8087199926376343

You might also like