<jupyter_start><jupyter_text>Classifier to perform sentiment analysis on movie reviews<jupyter_code>from keras import layers
from keras.datasets import imdb
from keras import optimizers
from keras import losses

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
(train_data, train_label), (test_data, test_label) = imdb.load_data(num_words=12000)
train_data.shape
test_data.shape
for review in train_data[:2]:
    print('Reviews:', review)
train_label[:2]
print(type(train_data))
word_index = imdb.get_word_index()
print(type(word_index))
reverse_word_index = dict([ (value, key) for (key, value) in word_index.items() ])
first_review = ' '.join(reverse_word_index.get(i-3, ',') for i in train_data[0])
print(first_review)
def one_hot_encoding(data, num_words=12000):
    vectorized_sequence = np.zeros((len(data), num_words))
    
    for row, col in enumerate(data):
        vectorized_sequence[row, col] = 1 # assign 1 whenever there is an integer
    return vectorized_sequence
trainX = one_hot_encoding(train_data)
print(trainX.shape)
testX = one_hot_encoding(test_data)
print(testX.shape)
trainY = train_label.astype('float32')
testY = test_label.astype('float32')
testY[:5]
from sklearn.model_selection import train_test_split
from keras import metrics
train_X, val_X = trainX[:20000], trainX[20000:]
train_Y, val_Y = trainY[:20000], trainY[20000:]
from keras.models import Model, Input
from keras import regularizers
ip = Input(batch_shape=(None, 12000), name='input')
dense1 = layers.Dense(32, activation='relu',
                      kernel_regularizer = regularizers.l1_l2(l1=0.001, l2=0.001),
                      name='dense1')(ip)
dropout1 = layers.Dropout(0.3)(dense1)
dense2 = layers.Dense(32, activation='relu',
                      kernel_regularizer = regularizers.l1_l2(l1=0.001, l2=0.001),
                      name='dense2')(dropout1)
dropout2 = layers.Dropout(0.3)(dense2)
output = layers.Dense(1, activation='sigmoid', name='output')(dropout2)
model = Model(inputs=ip, outputs=output)
model.summary()
loss = losses.binary_crossentropy
opt = optimizers.adam(lr=0.001)
accuracy = metrics.binary_accuracy
model.compile(loss=loss, optimizer=opt, metrics=[accuracy])
history = model.fit(train_X, train_Y,
                   epochs = 20,
                   batch_size = 512,
                   verbose = 2,
                   validation_data = (val_X, val_Y)
                   )
print(type(history))
print(type(history.history))
print(history.history.keys())
plt.rcParams['figure.figsize'] = 15, 8
plt.plot(history.history['loss'], label = 'training_loss')
plt.plot(history.history['val_loss'], label = 'validation_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and Validation loss')
plt.legend()
plt.show()
plt.plot(history.history['binary_accuracy'], label = 'training_accuracy')
plt.plot(history.history['val_binary_accuracy'], label = 'validation_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.show()
test_result = model.evaluate(testX, testY)
print(type(test_result))
print(test_result)<jupyter_output>[0.5713596053504943, 0.87092]
{'fraction_non_alphanumeric': 0.1302017654476671, 'fraction_numerical': 0.03940731399747793, 'mean_word_length': 4.930841121495327, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '569994', 'license': 'MIT', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1066, 'n_words': 219, 'path': '23.imdb_review_analysis.ipynb', 'repo_name': 'mkumar73/neural-nets', 'size': '3172'}