<jupyter_start><jupyter_text><jupyter_code># install tensroflow if you haven't
# pip install tensorflow
from keras.preprocessing import sequence, text
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Embedding, LSTM, Conv1D, Flatten, SimpleRNN, Bidirectional
from keras.datasets import imdb
import numpy as np<jupyter_output>Using TensorFlow backend.<jupyter_text>Load Data<jupyter_code># set parameters:
vocab_size = 2500
maxlen = 100
batch_size = 32
embedding_dims = 128
filters = 250
kernel_size = 3
hidden_dims = 100
epochs = 10

(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words = vocab_size)

# Here the data is already tokenized
# Go ahead to pad it to ensure all input is the same size

X_train = sequence.pad_sequences(X_train, maxlen = maxlen)
X_test = sequence.pad_sequences(X_test, maxlen = maxlen)
import matplotlib.pyplot as plt

def plot_graphs(history, string):
  plt.plot(history.history[string])
  plt.plot(history.history['val_'+string])
  plt.xlabel("Epochs")
  plt.ylabel(string)
  plt.legend([string, 'val_'+string])
  plt.show()<jupyter_output><empty_output><jupyter_text>Single Layer Simple RNN<jupyter_code>rnn_model = Sequential()
rnn_model.add(Embedding(vocab_size, embedding_dims, input_length = maxlen))
rnn_model.add(SimpleRNN(units = 64, dropout=0.2, recurrent_dropout=0.2))
rnn_model.add(Dense(1, activation='sigmoid'))

rnn_model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history_rnn = rnn_model.fit(X_train, y_train,
          batch_size = batch_size,
          epochs = epochs,
          validation_data=(X_test, y_test))
plot_graphs(history_rnn, 'accuracy')
plot_graphs(history_rnn, 'loss')<jupyter_output><empty_output><jupyter_text>Single Layer LSTM<jupyter_code>model_lstm1 = Sequential()

model_lstm1.add(Embedding(vocab_size,
                    embedding_dims,
                    input_length = maxlen))

model_lstm1.add(LSTM(hidden_dims, activation="tanh"))
model_lstm1.add(Dense(1, activation='sigmoid'))

model_lstm1.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history_lstm1 = model_lstm1.fit(X_train, y_train,
          batch_size = batch_size,
          epochs = epochs,
          validation_data=(X_test, y_test))
plot_graphs(history_lstm1, 'accuracy')
plot_graphs(history_lstm1, 'loss')<jupyter_output><empty_output><jupyter_text>Single Layer Bidirectional LSTM<jupyter_code>model_bilstm1 = Sequential()

model_bilstm1.add(Embedding(vocab_size,
                    embedding_dims,
                    input_length = maxlen))

model_bilstm1.add(Bidirectional(LSTM(hidden_dims, activation="tanh")))
model_bilstm1.add(Dense(1, activation='sigmoid'))

model_bilstm1.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history_bilstm1 = model_bilstm1.fit(X_train, y_train,
          batch_size = batch_size,
          epochs = 20,
          validation_data=(X_test, y_test))
plot_graphs(history_bilstm1, 'accuracy')
plot_graphs(history_bilstm1, 'loss')<jupyter_output><empty_output><jupyter_text>Multi-layer LSTM<jupyter_code>hidden_dims1 = 64
hidden_dims2 = 32

model_lstm2 = Sequential()

model_lstm2.add(Embedding(vocab_size,
                    embedding_dims,
                    input_length = maxlen))

model_lstm2.add(LSTM(hidden_dims1, activation="tanh", dropout = 0.2, recurrent_dropout = 0.2, return_sequences=True))
model_lstm2.add(LSTM(32, activation="tanh"))
model_lstm2.add(Dense(64, activation='relu'))
model_lstm2.add(Dense(1, activation='sigmoid'))

model_lstm2.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history_lstm2 = model_lstm2.fit(X_train, y_train,
          batch_size = batch_size,
          epochs = epochs,
          validation_data=(X_test, y_test))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1114199090449722, 'fraction_numerical': 0.019201616978271854, 'mean_word_length': 4.6964028776978415, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '301630', 'license': 'CC0-1.0', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1417, 'n_words': 226, 'path': 'Python/RNN.ipynb', 'repo_name': 'happyrabbit/IntroDataScience', 'size': '3958'}