Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions keras_nlp/benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,28 @@ the following results were obtained:
To change the configuration, say, for example, number of layers in the transformer
model used for inference, the user can modify the config dictionaries given at
the top of the script.


## Sentiment Analysis


For benchmarking classification models, the following command can be run
from the root of the repository:

```sh
python3 .keras_nlp/benchmarks/sentiment_analysis.py \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the extra ".": .keras_nlp/ => keras_nlp/

--model="bert" \
--preset="bert_small_en_uncased" \
--learning_rate=5e-5 \
--num_epochs=5 \
--batch_size=32
```

flag `--model` specifies the model name, and `--preset` specifies the preset under testing. `--preset` could be None,
while `--model` is required. Other flags are common training flags.

This script outputs:

- validation accuracy for each epoch.
- testing accuracy after training is done.
- total elapsed time (in seconds).
136 changes: 136 additions & 0 deletions keras_nlp/benchmarks/sentiment_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2022 The KerasNLP Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use 2023 now, time flashes!

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import time

import tensorflow as tf
import tensorflow_datasets as tfds
from absl import app
from absl import flags
from tensorflow import keras

import keras_nlp

# Use mixed precision for optimal performance
keras.mixed_precision.set_global_policy("mixed_float16")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually given that this is a benchmarking script, better to leave this as a flag probably. Totally valid to benchmark a model under full precision or mixed.

Can we just make this a string flag?

flags.DEFINE_string(
    "mixed_precision_policy",
    "mixed_float16",
    "The global mixed precision policy to use. E.g. 'mixed_float16' or 'float32'.",
)


FLAGS = flags.FLAGS
flags.DEFINE_string(
"model", None, "The name of the classifier such as BertClassifier."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a training comma so that it's formatted to multilines.

)
flags.DEFINE_string(
"preset",
None,
"The name of a preset, e.g. bert_base_multi.",
)

flags.DEFINE_float("learning_rate", 5e-5, "The learning rate.")
flags.DEFINE_integer("num_epochs", 1, "The number of epochs.")
flags.DEFINE_integer("batch_size", 16, "The batch size.")

tfds.disable_progress_bar()

BUFFER_SIZE = 10000


def check_flags():
if not FLAGS.model:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just remembered there is actually a way to do this with absl directly. flags.mark_flag_as_required("flag").

https://github.com/keras-team/keras-nlp/blob/master/examples/bert_pretraining/bert_pretrain.py#L454

raise ValueError("Please specify a model name.")


def create_imdb_dataset():
dataset = tfds.load("imdb_reviews", as_supervised=True)
train_dataset, test_dataset = dataset["train"], dataset["test"]

train_dataset = (
train_dataset.shuffle(BUFFER_SIZE)
.batch(FLAGS.batch_size)
.prefetch(tf.data.AUTOTUNE)
)
val_dataset = (
test_dataset.take(10000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather than a hardcoded split like this, you could run tfds.load with_info. Use that to get the size of the test set, and use a fractional split here. E.g. int(test_dataset_cardinality / 2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do:

test_ds_size = test_dataset.cardinality()
val_dataset = test_dataset.take(test_ds_size // 2)
...

.batch(FLAGS.batch_size)
.prefetch(tf.data.AUTOTUNE)
)
test_dataset = (
test_dataset.skip(10000)
.batch(FLAGS.batch_size)
.prefetch(tf.data.AUTOTUNE)
)

return train_dataset, val_dataset, test_dataset


def create_model():
for name, symbol in keras_nlp.models.__dict__.items():
if inspect.isclass(symbol) and issubclass(symbol, keras.Model):
if FLAGS.model and name != f"{FLAGS.model.capitalize()}Classifier":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than all this, I would just take in the symbol name directly e.g. --model=BertClassifier. This will be a little more obvious in usage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, this is my bad honestly, I put "bert" in the problem description.

continue
if not hasattr(symbol, "from_preset"):
continue
for preset in symbol.presets:
if FLAGS.preset and preset != FLAGS.preset:
continue
model = symbol.from_preset(preset)
print(f"Using model {name} with preset {preset}")
return model

raise ValueError(f"Model {FLAGS.model} or preset {FLAGS.preset} not found.")


def train_model(
model: keras.Model,
train_dataset: tf.data.Dataset,
validation_dataset: tf.data.Dataset,
):
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.Adam(5e-5),
metrics=keras.metrics.SparseCategoricalAccuracy(),
jit_compile=True,
)

model.fit(
train_dataset,
epochs=FLAGS.num_epochs,
validation_data=validation_dataset,
verbose=2,
)

return model


def evaluate_model(model: keras.Model, test_dataset: tf.data.Dataset):
loss, accuracy = model.evaluate(test_dataset)
print(f"Test loss: {loss}")
print(f"Test accuracy: {accuracy}")


def main(_):
# Start time
start_time = time.time()

check_flags()
train_dataset, validation_dataset, test_dataset = create_imdb_dataset()
model = create_model()
model = train_model(model, train_dataset, validation_dataset)
evaluate_model(model, test_dataset)

# End time
end_time = time.time()
print(f"Total time: {end_time - start_time}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Wall time", so it's clear we are just measuring elapsed time?



if __name__ == "__main__":
app.run(main)