pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 7$ python run_classifier.py \--task_name=COLA|MRPC \--do_train=true \--do_eval=true \--do_predict=true \--data_dir=${CLASSIFIER_DATA} \--vocab_file=${BERT_BASE_DIR}/vocab.txt \--bert_config_file=${BERT_BASE_DIR}/bert_config_file.json \--init_checkpoint=${BERT_BASE_DIR}/bert_model.ckpt \--max_seq_length=128 \--train_batch_size=8 \--learning_rate=2e-5 \--num_train_epochs=2.0 \--output_dir=${TRAINED_CLASSIFIER}To predict only using a trained network, turn the --do_train and --do_eval flagsto false.Using BERT as part of your own networkCurrently BERT is available on TensorFlow Hub as an estimator, but at the momentit is not fully compliant with TensorFlow 2.x, in the sense that it is not yet callableas a tf.hub.KerasLayer. Meanwhile, Zweig demonstrates how to include BERTin your Keras/TensorFlow 1.x-based network in his blog post [35].The more popular way to use BERT in your own TensorFlow 2.x code is via theHuggingFace Transformers library. This library provides convenience classes forvarious popular Transformer architectures such as BERT, as well as convenienceclasses for fine-tuning on several downstream tasks. It was originally writtenfor PyTorch, but has since been extended with convenience classes callable fromTensorFlow as well. However, in order to use this library, you must have PyTorchinstalled as well.The Transformers library provides the following classes:1. A set of Transformer classes for 10 (at the time of writing) differentTransformer architectures that can be instantiated from PyTorch client code.The naming convention is to append "Model" to the name of the architecture,for example, BertModel, XLNetModel, and so on. There is also a correspondingset of classes that can be instantiated from TensorFlow 2.x code; these areprefixed by "TF", for example, TFBertModel, TFXLNetModel, and so on.[ 271 ]

Word Embeddings2. Each Transformer model has a corresponding tokenizer class, which knowhow to tokenize text input for these classes. So the tokenizer correspondingto the BERT model is named BertTokenizer.3. Each Transformer class has a set of convenience classes to allow finetuningthe Transformer model to a set of downstream tasks. For example,the convenience classes corresponding to BERT are BertForPreTraining,BertForMaskedLM, BertForNextSentencePrediction,BertForSequenceClassification, BertForMultipleChoice,BertForTokenClassification, and BertForQuestionAnswering.In order to install PyTorch, head over to the PyTorch site (http://pytorch.org) and find the section titled Quick Start Locally. Underit is a form where you have to specify some information about yourplatform, and the site will generate an installation command thatwill download and install PyTorch on your environment. Copy thecommand to your terminal and run it to install PyTorch in yourenvironment.Once you have installed PyTorch, install the Transformers libraryusing the following pip command. Refer to https://github.com/huggingface/transformers for additional documentationon how to use the library:$ pip install transformersIn order to run the example, you will also need to install thetensorflow_datasets package. You can do so using the pipcommand as follows:$ pip install tensorflow-datasetsThe following code instantiates a BERT cased model and fine-tunes it with datafrom the MRPC dataset. The MRPC task tries to predict if a pair of sentences areparaphrases of one another. The dataset is available from the tensorflow-datasetspackage. As usual, we first import the necessary libraries:import osimport tensorflow as tfimport tensorflow_datasetsfrom transformers import BertTokenizer, \TFBertForSequenceClassification, BertForSequenceClassification,\ glue_convert_examples_to_featuresWe declare a few constants that we will use later in the code:BATCH_SIZE = 32FINE_TUNED_MODEL_DIR = "./data/"[ 272 ]

Chapter 7

$ python run_classifier.py \

--task_name=COLA|MRPC \

--do_train=true \

--do_eval=true \

--do_predict=true \

--data_dir=${CLASSIFIER_DATA} \

--vocab_file=${BERT_BASE_DIR}/vocab.txt \

--bert_config_file=${BERT_BASE_DIR}/bert_config_file.json \

--init_checkpoint=${BERT_BASE_DIR}/bert_model.ckpt \

--max_seq_length=128 \

--train_batch_size=8 \

--learning_rate=2e-5 \

--num_train_epochs=2.0 \

--output_dir=${TRAINED_CLASSIFIER}

To predict only using a trained network, turn the --do_train and --do_eval flags

to false.

Using BERT as part of your own network

Currently BERT is available on TensorFlow Hub as an estimator, but at the moment

it is not fully compliant with TensorFlow 2.x, in the sense that it is not yet callable

as a tf.hub.KerasLayer. Meanwhile, Zweig demonstrates how to include BERT

in your Keras/TensorFlow 1.x-based network in his blog post [35].

The more popular way to use BERT in your own TensorFlow 2.x code is via the

HuggingFace Transformers library. This library provides convenience classes for

various popular Transformer architectures such as BERT, as well as convenience

classes for fine-tuning on several downstream tasks. It was originally written

for PyTorch, but has since been extended with convenience classes callable from

TensorFlow as well. However, in order to use this library, you must have PyTorch

installed as well.

The Transformers library provides the following classes:

1. A set of Transformer classes for 10 (at the time of writing) different

Transformer architectures that can be instantiated from PyTorch client code.

The naming convention is to append "Model" to the name of the architecture,

for example, BertModel, XLNetModel, and so on. There is also a corresponding

set of classes that can be instantiated from TensorFlow 2.x code; these are

prefixed by "TF", for example, TFBertModel, TFXLNetModel, and so on.

[ 271 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!