Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
Created January 17, 2022 20:40
Show Gist options
  • Save eileen-code4fun/9923488ac5945e7f0e150749f4d529e3 to your computer and use it in GitHub Desktop.
Save eileen-code4fun/9923488ac5945e7f0e150749f4d529e3 to your computer and use it in GitHub Desktop.
NLP Classification
import tensorflow_datasets as tfds
import tensorflow as tf
dataset, info = tfds.load('imdb_reviews', with_info=True,
as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
def experiment(train, test):
VOCAB_SIZE = 1000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=VOCAB_SIZE, standardize=None)
encoder.adapt(train_dataset.map(lambda text, label: text))
model = tf.keras.Sequential([
encoder,
tf.keras.layers.Embedding(
input_dim=len(encoder.get_vocabulary()),
output_dim=64,
# Use masking to handle the variable sequence lengths
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'])
history = model.fit(train, epochs=20,
validation_data=test,
validation_steps=30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment