<jupyter_start><jupyter_text>Imports<jupyter_code>!pip install tensorflow-datasets
import numpy as np

import tensorflow_datasets as tfds
import tensorflow as tf 

tfds.disable_progress_bar()

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Graphs<jupyter_code>def plot_graphs(history, metric):
    plt.plot(history.history[metric])
    plt.plot(history.history['val_'+metric], '')
    plt.xlabel("Epochs")
    plt.ylabel(metric)
    plt.legend([metric, 'val_'+metric])<jupyter_output><empty_output><jupyter_text>Input Pipeline<jupyter_code>dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']

print(train_dataset.element_spec)

for example, label in train_dataset.take(1):
    print('text: ', example.numpy())
    print('label: ', label.numpy())
BUFFER_SIZE = 1000
BATCH_SIZE = 64
train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
test_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
for example, label in train_dataset.take(1):
    print('text: ', example.numpy()[:3])
    print()
    print('labels: ', label.numpy()[:3])<jupyter_output>text:  [b'Though this movie is cheesiness at its best, it is pulled off perfectly. This movie, without a doubt, has to be considered a modern classic. There are basically two kinds of movies I like - movies with depth (chick flicks, if you must - I blame my wife for this) and mindless comedies where I can sit back and relax. This movie is a perfect example of the latter.<br /><br />A friend of mine turned me on to this movie shortly after its release. Considering me to somewhat shallow, he said to me, "You\'ve got to see this movie. It\'s just your type of movie." Foregoing the insult, I started watching. I know they mentioned The Ramones a million times, but when you actually see them, I said, "Hey, it is The Ramones." My friend replied, "I don\'t know they were a real band." I had my moment of glory.<br /><br />This movie, though now somewhat dated, is a must see for Ramones fans - or anyone else for that matter.'
 b"Mimicking its long title the movie finds ways to come close to the [...]<jupyter_text>Create the text encoder<jupyter_code>VOCAB_SIZE = 1000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=VOCAB_SIZE)
encoder.adapt(train_dataset.map(lambda text, label: text))
vocab = np.array(encoder.get_vocabulary())
vocab[:20]
encoded_example = encoder(example)[:3].numpy()
encoded_example<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>model = tf.keras.Sequential([
    encoder,
    tf.keras.layers.Embedding(
        input_dim=len(encoder.get_vocabulary()),
        output_dim=64,
        mask_zero=True
    ),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
             optimizer=tf.keras.optimizers.Adam(1e-4),
             metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>import os
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"]="true"
TF_FORCE_GPU_ALLOW_GROWTH=1
physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices)
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
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_loss)
print('Test Accuracy: ',test_acc)
model.summary()
plt.figure(figsize=(16, 8))
plt.subplot(1, 2, 1)
plot_graphs(history, 'accuracy')
plt.ylim(None, 1)
plt.subplot(1, 2, 2)
plot_graphs(history, 'loss')
plt.ylim(0, None)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>sample_text= ('The movie was horrible. The animation and the graphics '
               'were really bad. I would not recommend this movie.')
predictions = model.predict(np.array([sample_text]))
predictions
type(sample_text)
if(predictions>0.0):
    print('positive')
else:
    print('negative')
model.save('model/')
model_json = model.to_json()
with open('model.json', 'w') as js:
    js.write(model_json)
model.save_weights('model', save_format='tf')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11969964664310954, 'fraction_numerical': 0.010159010600706713, 'mean_word_length': 5.4607703281027105, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, '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': '627106', 'license': 'MIT', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1485, 'n_words': 364, 'path': 'Streamlit-Deployment/LSTM/LSTM_model.ipynb', 'repo_name': 'Harsh188/100-Days-of-ML-Pt2', 'size': '4528'}