<jupyter_start><jupyter_text>Text Classification with an RNN<jupyter_code>import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Setup<jupyter_code>from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
import tensorflow_datasets as tfds
tf.enable_eager_execution()
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>Setup input pipeline<jupyter_code>dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
encoder = info.features['text'].encoder
print('Vocabulary size: {}'.format(encoder.vocab_size))
sample_string = 'Hello Tensorflow'
encoded_string = encoder.encode(sample_string)
print('Encoded string is {}'.format(encoded_string))

original_string = encoder.decode(encoded_string)
print('The original string is {}'.format(original_string))
assert original_string == sample_string
for index in encoded_string:
    print('{} --> {}'.format(index, encoder.decode([index])))<jupyter_output>4025 --> Hell
222 --> o 
6307 --> Ten
2327 --> sor
2934 --> flow<jupyter_text>Prepare data for training<jupyter_code>BUFFER_SIZE = 10000
BATCH_SIZE = 64
train_dataset = train_dataset.shuffle(BUFFER_SIZE)
train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes)

test_dataset = test_dataset.padded_batch(BATCH_SIZE, test_dataset.output_shapes)<jupyter_output>WARNING:tensorflow:From <ipython-input-12-766600657fa2>:2: DatasetV1.output_shapes (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.data.get_output_shapes(dataset)`.<jupyter_text>Create the model<jupyter_code>model = tf.keras.Sequential([
    tf.keras.layers.Embedding(encoder.vocab_size, 64),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
tf.keras.utils.plot_model(model)
history = model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(1e-3),
                        metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code># model.fit(train_dataset, epochs=10, validation_data=test_dataset, validation_steps=30)
test_loss, test_acc = model.evaluate(test_dataset)
print('test loss: {}'.format(test_loss))
print('test accuracy: {}'.format(test_acc))
def pad_to_size(vec, size):
    zeros = [0] * (size - len(vec))
    vec.extend(zeros)
    return vec
def sample_predict(sample_string, pad):
    encoded_sample_pred_text = encoder.encode(sample_string)

    if pad:
        encoded_sample_pred_text = pad_to_size(encoded_sample_pred_text, 64)
        
    encoded_sample_pred_text = tf.cast(encoded_sample_pred_text, tf.float32)
    predictions = model.predict(tf.expand_dims(encoded_sample_pred_text, 0))
    return predictions
# predict on a sample text without padding.

sample_pred_text = ('The movie was cool. The animation and the graphics '
                    'were out of this world. I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad=False)
print (predictions)
# predict on a sample text with padding

sample_pred_text = ('The movie was cool. The animation and the graphics '
                    'were out of this world. I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad=True)
print (predictions)<jupyter_output>[[0.4900237]]<jupyter_text>Stack two more layers<jupyter_code>model = tf.keras.Sequential([
    tf.keras.layers.Embedding(encoder.vocab_size, 64),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(1, activation='sigmoid'),
])
model.summary()
tf.keras.utils.plot_model(model)
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(1e-3),
             metrics=['accuracy'])
history = model.fit(train_dataset, epochs=10, validation_data=test_dataset, validation_steps=30)
test_loss, test_acc = model.evaluate(test_dataset)
print('test loss: {}, test acc: {}'.format(test_loss, test_acc))
# predict on a sample text without padding.

sample_pred_text = ('The movie was not good. The animation and the graphics '
                    'were terrible. I would not recommend this movie.')
predictions = sample_predict(sample_pred_text, pad=False)
print (predictions)<jupyter_output>[[0.49952647]]
{'fraction_non_alphanumeric': 0.12236976506639428, 'fraction_numerical': 0.01940755873340143, 'mean_word_length': 5.905500705218618, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 32, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '345109', 'license': 'MIT', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1616, 'n_words': 306, 'path': 'notebooks/rnn_text_classification.ipynb', 'repo_name': 'hotbaby/sentiment-analysis', 'size': '4895'}