<jupyter_start><jupyter_text>Advanced RNN - 3- CuDNNGRU & CuDNNLSTM implementation<jupyter_code>from keras.datasets import imdb
from keras.layers import GRU, LSTM, CuDNNGRU, CuDNNLSTM, Activation
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential<jupyter_output>Using TensorFlow backend.<jupyter_text>Import dataset- IMDB dataset in Keras datasets- doc: https://keras.io/datasets/<jupyter_code>num_words = 30000
maxlen = 300
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words = num_words)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# pad the sequences with zeros 
# padding parameter is set to 'post' => 0's are appended to end of sequences
X_train = pad_sequences(X_train, maxlen = maxlen, padding = 'post')
X_test = pad_sequences(X_test, maxlen = maxlen, padding = 'post')
X_train = X_train.reshape(X_train.shape + (1,))
X_test = X_test.reshape(X_test.shape + (1,))
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)<jupyter_output>(25000, 300, 1)
(25000, 300, 1)
(25000,)
(25000,)<jupyter_text>LSTM- Naive LSTM model without CuDNN<jupyter_code>def lstm_model():
    model = Sequential()
    model.add(LSTM(50, input_shape = (300,1), return_sequences = True))
    model.add(LSTM(1, return_sequences = False))
    model.add(Activation('sigmoid'))
    
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model
model = lstm_model()
%%time
model.fit(X_train, y_train, batch_size = 100, epochs = 10, verbose = 0)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 50.84%<jupyter_text>GRU- Naive GRU model without CuDNN<jupyter_code>def gru_model():
    model = Sequential()
    model.add(GRU(50, input_shape = (300,1), return_sequences = True))
    model.add(GRU(1, return_sequences = False))
    model.add(Activation('sigmoid'))
    
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model
model = gru_model()
%%time
model.fit(X_train, y_train, batch_size = 100, epochs = 10, verbose = 0)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 52.40%<jupyter_text>CuDNN LSTM<jupyter_code>def cudnn_lstm_model():
    model = Sequential()
    model.add(CuDNNLSTM(50, input_shape = (300,1), return_sequences = True))
    model.add(CuDNNLSTM(1, return_sequences = False))
    model.add(Activation('sigmoid'))
    
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model
model = cudnn_lstm_model()
%%time
model.fit(X_train, y_train, batch_size = 100, epochs = 10, verbose = 0)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 51.70%<jupyter_text>CuDNN GRU<jupyter_code>def cudnn_gru_model():
    model = Sequential()
    model.add(CuDNNGRU(50, input_shape = (300,1), return_sequences = True))
    model.add(CuDNNGRU(1, return_sequences = False))
    model.add(Activation('sigmoid'))
    
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model
model = cudnn_gru_model()
%%time
model.fit(X_train, y_train, batch_size = 100, epochs = 10, verbose = 0)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 51.60%
{'fraction_non_alphanumeric': 0.14513829483889365, 'fraction_numerical': 0.037639007698887936, 'mean_word_length': 5.838206627680312, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 1, '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': '352737', 'license': 'MIT', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1316, 'n_words': 263, 'path': 'NLP/4-Advanced-RNN-3/4-advanced-rnn-3.ipynb', 'repo_name': 'agarwalanant/ThaparWorkshopANN', 'size': '3507'}