<jupyter_start><jupyter_text>Solving Natural Language Processing Problems using RNN<jupyter_code># import modules
import tflearn
import numpy as np
import tensorflow as tf

tf.reset_default_graph()
# load dataset
(train_x, train_y), (test_x, test_y), _ = tflearn.datasets.imdb.load_data(n_words=5000, path='/imdb/imdb.pkl')
# how our dataset looks like
train_x[0], train_y[0]
# data preprocesing
# sequence padding
train_x = tflearn.data_utils.pad_sequences(train_x, maxlen=500, value=0.)
test_x = tflearn.data_utils.pad_sequences(test_x, maxlen=500)

# One hot encode labels
train_y = tflearn.data_utils.to_categorical(train_y, nb_classes=2)
test_y = tflearn.data_utils.to_categorical(test_y, nb_classes=2)
# define model
model = tflearn.input_data([None, 500])
model = tflearn.embedding(model, input_dim=5000, output_dim=128)
model = tflearn.simple_rnn(model, 128, dropout=0.5)
model = tflearn.fully_connected(model, 2, activation='softmax')
model = tflearn.regression(model, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy')
# build model
model = tflearn.DNN(model)
# train model
model.fit(train_x, train_y, n_epoch=1, batch_size=1000)
# get scores
scores = model.evaluate(test_x, test_y)
scores
# # check predictions
# model.predict(train_x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11651234567901235, 'fraction_numerical': 0.03009259259259259, 'mean_word_length': 7.589403973509934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '470185', 'license': 'Apache-2.0', 'n_tokens_mistral': 507, 'n_tokens_neox': 474, 'n_words': 97, 'path': '5_Recurrent_Neural_networks_with_Tensorflow.ipynb', 'repo_name': 'AjeetGitHub2016/Neural-Networks-with-TensorFlow', 'size': '1296'}