import tensorflow as tf tf.test.is_gpu_available(
cuda_only=False, min_cuda_compute_capability=None
)
!pip install keras import numpy as np import pandas as pd import keras
from keras.datasets import imdb

num_classification_words = 20000
words_limit = 100
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_classification_words) word_to_id = keras.datasets.imdb.get_word_index()
INDEX_FROM = 3
word_to_id = {k: (v + INDEX_FROM) for k, v in word_to_id.items()} word_to_id["<PAD>"] = 0 # Padding
word_to_id["<START>"] = 1 # Start Index word_to_id["<UNK>"] = 2 # Unknown Words
id_to_word = {value: key for key, value in word_to_id.items()}

for i in range(5):
print("REVIEW", str(i + 1), "\t", ' '.join(id_to_word.get(id, '<UNK>') for id in x_train[i])) import numpy as np
import random import json
from six.moves import range import six
def pad_sequences(sequences, maxlen=None):

dtype='int32';padding='pre';truncating='pre';value=0. num_samples = len(sequences)

lengths = [] sample_shape = () flag = True

# take the sample shape from the first non empty sequence # checking for consistency in the main loop below.

for x in sequences: try:
lengths.append(len(x)) if flag and len(x):
sample_shape = np.asarray(x).shape[1:] flag = False
except TypeError:
raise ValueError('`sequences` must be a list of iterables. ' 'Found non-iterable: ' + str(x))

if maxlen is None:
maxlen = np.max(lengths)

is_dtype_str = np.issubdtype(dtype, np.str_) or np.issubdtype(dtype, np.str_)

x = np.full((num_samples, maxlen) + sample_shape, value, dtype=dtype) for idx, s in enumerate(sequences):
if not len(s):
continue # empty list/array was found if truncating == 'pre':
trunc = s[-maxlen:] elif truncating == 'post':
trunc = s[:maxlen]
trunc = np.asarray(trunc, dtype=dtype)

if padding == 'post':
x[idx, :len(trunc)] = trunc elif padding == 'pre':
x[idx, -len(trunc):] = trunc return x

x_train_seq = pad_sequences(x_train, maxlen=words_limit) x_test_seq = pad_sequences(x_test, maxlen=words_limit)


print('train shape:', x_train_seq.shape) print('test shape:', x_test_seq.shape) x_train_seq
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense, Dropout, Activation,
Input, LSTM, GRU
rnn_input = Input(shape=(100,))
embedding = Embedding(num_classification_words, 128, input_length=words_limit)(rnn_input)
simple_rnn = SimpleRNN(128)(embedding) dropout = Dropout(0.4)(simple_rnn)
dense = Dense(1)(dropout)
activation = Activation('sigmoid')(dense) model = Model(rnn_input, activation)

model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) history=model.fit(x_train_seq, y_train, batch_size=32, epochs=3, validation_data=(x_test_seq, y_test))
print("Accuracy: ",acc) import matplotlib.pyplot as plt

# Get the training and validation metrics from the history object accuracy = history.history['accuracy']
loss = history.history['loss']
val_accuracy = history.history['val_accuracy'] val_loss = history.history['val_loss']

# Print the metrics print("Accuracy:", accuracy) print("Loss:", loss)
print("Validation Accuracy:", val_accuracy) print("Validation Loss:", val_loss)

avg_accuracy = np.mean(accuracy) avg_loss = np.mean(loss) avg_val_accuracy = np.mean(val_accuracy) avg_val_loss = np.mean(val_loss)

print(f"Average Accuracy: {avg_accuracy:.4f}") print(f"Average Loss: {avg_loss:.4f}")
print(f"Average Validation Accuracy: {avg_val_accuracy:.4f}") print(f"Average Validation Loss: {avg_val_loss:.4f}")

# Plot the metrics
epochs = range(1, len(accuracy) + 1) plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(epochs, accuracy, 'b', label='Training Accuracy') plt.plot(epochs, val_accuracy, 'r', label='Validation Accuracy') plt.title('Training and Validation Accuracy') plt.xlabel('Epochs')
plt.ylabel('Accuracy') plt.legend()

plt.subplot(1, 2, 2)
plt.plot(epochs, loss, 'b', label='Training Loss') plt.plot(epochs, val_loss, 'r', label='Validation Loss') plt.title('Training and Validation Loss') plt.xlabel('Epochs')
plt.ylabel('Loss') plt.legend()