### Inspect Dataset Examples Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Prints a few examples from the dataset for verification. ```python # inspect at a few examples for ex in dataset.take(3): print(ex) ``` -------------------------------- ### Iterate through dataset samples Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Use this loop to inspect the first three examples from the dataset. ```python for ex in dataset.take(3): print(ex) ``` -------------------------------- ### Setup TPU/GPU Training Infrastructure Source: https://context7.com/google-research/bigbird/llms.txt Commands for provisioning GCP resources and executing training scripts on TPU or GPU hardware. ```bash # Create GCP instance with TPU gcloud compute instances create bigbird \ --zone=europe-west4-a \ --machine-type=n1-standard-16 \ --boot-disk-size=50GB \ --image-project=ml-images \ --image-family=tf-2-3-1 \ --maintenance-policy=TERMINATE \ --scopes=cloud-platform # Create TPU gcloud compute tpus create bigbird \ --zone=europe-west4-a \ --accelerator-type=v3-32 \ --version=2.3.1 # SSH into instance gcloud compute ssh --zone "europe-west4-a" "bigbird" # Install BigBird git clone https://github.com/google-research/bigbird.git cd bigbird pip3 install -e . # Training with TPU python -m bigbird.classifier.run_classifier \ --use_tpu=True \ --tpu_name=bigbird \ --tpu_zone=europe-west4-a \ --num_tpu_cores=32 \ --data_dir="gs://your-bucket/data" \ --output_dir="gs://your-bucket/output" \ --init_checkpoint="gs://bigbird-transformer/bigbr_base/model.ckpt-0" # Training with GPU (MirroredStrategy) python -m bigbird.classifier.run_classifier \ --use_tpu=False \ --data_dir="/path/to/data" \ --output_dir="/path/to/output" ``` -------------------------------- ### Install BigBird Library Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Installs the BigBird package from the official GitHub repository. ```python !pip install git+https://github.com/google-research/bigbird.git -q ``` -------------------------------- ### Generate summary for an example Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Extracts the predicted summary from the first element of the example tuple. ```python predicted_summary = summerize(ex[0])['pred_sent'][0] ``` -------------------------------- ### Big Bird Installation Source: https://github.com/google-research/bigbird/blob/master/README.md Clone the Big Bird repository and install it using pip. This also includes instructions for downloading pretrained checkpoints. ```bash git clone https://github.com/google-research/bigbird.git cd bigbird pip3 install -e . ``` ```bash mkdir -p bigbird/ckpt gsutil cp -r gs://bigbird-transformer/ bigbird/ckpt/ ``` -------------------------------- ### Initialize ROUGE scorer and aggregator Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Setup the ROUGE scorer with specific metrics and initialize a bootstrap aggregator for score collection. ```python scorer = rouge_scorer.RougeScorer(["rouge1", "rouge2", "rougeLsum"], use_stemmer=True) aggregator = scoring.BootstrapAggregator() ``` -------------------------------- ### Fine-tune BigBird for Text Classification Source: https://context7.com/google-research/bigbird/llms.txt Commands to install dependencies, download checkpoints, and train/export a classification model. ```bash # Install dependencies pip install bigbird tensorflow tensorflow-datasets tensorflow-text natsort # Download pretrained checkpoint mkdir -p bigbird/ckpt gsutil cp -r gs://bigbird-transformer/bigbr_base bigbird/ckpt/ # Train on IMDB sentiment classification python -m bigbird.classifier.run_classifier \ --data_dir="tfds://imdb_reviews/plain_text" \ --output_dir="/tmp/bigbird_imdb" \ --init_checkpoint="bigbird/ckpt/bigbr_base/model.ckpt-0" \ --max_encoder_length=4096 \ --num_labels=2 \ --train_batch_size=4 \ --learning_rate=1e-5 \ --num_train_steps=16000 \ --num_warmup_steps=1000 \ --attention_type="block_sparse" \ --do_train=True \ --do_eval=True # Export trained model python -m bigbird.classifier.run_classifier \ --output_dir="/tmp/bigbird_imdb" \ --max_encoder_length=4096 \ --do_export=True ``` -------------------------------- ### Prepare Input Data Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Builds the input function and creates a dataset for training. ```python train_input_fn = run_classifier.input_fn_builder( data_dir=FLAGS.data_dir, vocab_model_file=FLAGS.vocab_model_file, max_encoder_length=FLAGS.max_encoder_length, substitute_newline=FLAGS.substitute_newline, is_training=True) dataset = train_input_fn({'batch_size': 8}) ``` -------------------------------- ### Create GCP Instance for Big Bird Source: https://github.com/google-research/bigbird/blob/master/README.md Commands to create a Google Cloud Platform instance with specified machine type, disk size, and TensorFlow image. It also includes creating a TPU and SSHing into the instance. ```bash gcloud compute instances create \ bigbird \ --zone=europe-west4-a \ --machine-type=n1-standard-16 \ --boot-disk-size=50GB \ --image-project=ml-images \ --image-family=tf-2-3-1 \ --maintenance-policy TERMINATE \ --restart-on-failure \ --scopes=cloud-platform gcloud compute tpus create \ bigbird \ --zone=europe-west4-a \ --accelerator-type=v3-32 \ --version=2.3.1 gcloud compute ssh --zone "europe-west4-a" "bigbird" ``` -------------------------------- ### Import Dependencies and Initialize Flags Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Import necessary modules and initialize command-line flags for the BigBird environment. ```python from bigbird.core import flags from bigbird.core import modeling from bigbird.core import utils from bigbird.summarization import run_summarization import tensorflow.compat.v2 as tf import tensorflow_datasets as tfds import tensorflow_text as tft from tqdm import tqdm import sys FLAGS = flags.FLAGS if not hasattr(FLAGS, "f"): flags.DEFINE_string("f", "", "") FLAGS(sys.argv) tf.enable_v2_behavior() ``` -------------------------------- ### Initialize Tokenizer Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Creates a Sentencepiece tokenizer instance using the provided vocabulary file. ```python tokenizer = tft.SentencepieceTokenizer( model=tf.io.gfile.GFile(FLAGS.vocab_model_file, "rb").read()) ``` -------------------------------- ### Build Input Pipeline Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Construct the input function and dataset for training. ```python train_input_fn = run_summarization.input_fn_builder( data_dir=FLAGS.data_dir, vocab_model_file=FLAGS.vocab_model_file, max_encoder_length=FLAGS.max_encoder_length, max_decoder_length=FLAGS.max_decoder_length, substitute_newline=FLAGS.substitute_newline, is_training=True) dataset = train_input_fn({'batch_size': 8}) ``` -------------------------------- ### Execute Training Step Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Runs the forward and backward pass on a batch of data and prints the loss. ```python loss, log_probs, grads = fwd_bwd(ex[0], ex[1]) print('Loss: ', loss.numpy()) ``` -------------------------------- ### Initialize Eager Variable Store Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Create an eager variable store to manage model variables. ```python from tensorflow.python.ops.variable_scope import EagerVariableStore container = EagerVariableStore() ``` -------------------------------- ### Initialize Model and Classifier Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Creates the BertModel and the classifier loss layer based on the current flag configuration. ```python bert_config = flags.as_dictionary() model = modeling.BertModel(bert_config) headl = run_classifier.ClassifierLossLayer( bert_config["hidden_size"], bert_config["num_labels"], bert_config["hidden_dropout_prob"], utils.create_initializer(bert_config["initializer_range"]), name=bert_config["scope"]+"/classifier") ``` -------------------------------- ### Execute Training Step Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Run a single training step using the defined forward/backward function. ```python loss, llh, logits, pred_ids, grads = fwd_bwd(ex[0], ex[1]) print('Loss: ', loss) ``` -------------------------------- ### Configure Evaluation Input Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Sets up the input pipeline for evaluation using the classifier input builder. ```python eval_input_fn = run_classifier.input_fn_builder( data_dir=FLAGS.data_dir, vocab_model_file=FLAGS.vocab_model_file, max_encoder_length=FLAGS.max_encoder_length, substitute_newline=FLAGS.substitute_newline, is_training=False) eval_dataset = eval_input_fn({'batch_size': 8}) ``` -------------------------------- ### Run Big Bird Classification Experiment Source: https://github.com/google-research/bigbird/blob/master/README.md Set environment variables for GCP project name and experiment bucket, then execute the base size classification script. ```shell export GCP_PROJECT_NAME=bigbird-project # Replace by your project name export GCP_EXP_BUCKET=gs://bigbird-transformer-training/ # Replace sh -x bigbird/classifier/base_size.sh ``` -------------------------------- ### Pretrained Model Path Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Reference path for loading a pretrained model checkpoint. ```python # For training from scratch use # ckpt_path = 'gs://bigbird-transformer/pretrain/bigbr_base/model.ckpt-0' ``` -------------------------------- ### Print comparison of article and summaries Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Displays the original article, the model's predicted summary, and the ground truth summary for verification. ```python print('Article:\n {}\n\n Predicted summary:\n {}\n\n Ground truth summary:\n {}\n\n'.format( ex[0].numpy(), predicted_summary.numpy(), ex[1].numpy())) ``` -------------------------------- ### Import Dependencies and Initialize Flags Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Imports necessary modules and initializes TensorFlow flags for the BigBird environment. ```python from bigbird.core import flags from bigbird.core import modeling from bigbird.core import utils from bigbird.classifier import run_classifier import tensorflow.compat.v2 as tf import tensorflow_datasets as tfds from tqdm import tqdm import sys FLAGS = flags.FLAGS if not hasattr(FLAGS, "f"): flags.DEFINE_string("f", "", "") FLAGS(sys.argv) tf.enable_v2_behavior() ``` -------------------------------- ### Initialize DecoderStack Source: https://context7.com/google-research/bigbird/llms.txt Imports necessary modules for the Transformer decoder stack. ```python from bigbird.core import decoder from bigbird.core import utils import tensorflow as tf ``` -------------------------------- ### Initialize and Use EmbeddingLayer Source: https://context7.com/google-research/bigbird/llms.txt Creates an embedding layer for token and position processing and performs a forward pass. ```python from bigbird.core import utils import tensorflow as tf # Create embedding layer embedding_layer = utils.EmbeddingLayer( vocab_size=32000, emb_dim=768, initializer=utils.create_initializer(0.02), scale_emb=False, # Scale by sqrt(hidden_dim) for Pegasus use_token_type=True, # For BERT-style segment embeddings num_token_types=2, use_position_embeddings=True, max_position_embeddings=4096, dropout_prob=0.1 ) # Forward pass batch_size, seq_length = 2, 1024 input_ids = tf.random.uniform([batch_size, seq_length], 0, 32000, dtype=tf.int32) token_type_ids = tf.zeros_like(input_ids) embeddings = embedding_layer( input_ids=input_ids, seq_length=seq_length, start_pos=0, # For incremental decoding token_type_ids=token_type_ids, training=True ) # embeddings: [batch_size, seq_length, emb_dim] # Linear projection back to vocabulary (for language modeling) logits = embedding_layer.linear(embeddings) # logits: [batch_size, seq_length, vocab_size] ``` -------------------------------- ### Training Loop Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Executes the training process using an Adam optimizer and tracks loss metrics. ```python opt = tf.keras.optimizers.Adam(FLAGS.learning_rate) train_loss = tf.keras.metrics.Mean(name='train_loss') for i, ex in enumerate(tqdm(dataset.take(FLAGS.num_train_steps), position=0)): loss, llh, logits, pred_ids, grads = fwd_bwd(ex[0], ex[1]) opt.apply_gradients(zip(grads, model.trainable_weights)) train_loss(loss) if i% 10 == 0: print('Loss = {} '.format(train_loss.result().numpy())) ``` -------------------------------- ### Fine-tune BigBird for Document Summarization Source: https://context7.com/google-research/bigbird/llms.txt Commands to download Pegasus-style checkpoints and train the summarization model on PubMed data. ```bash # Download Pegasus-style pretrained checkpoint gsutil cp -r gs://bigbird-transformer/bigbp_large bigbird/ckpt/ # Train on PubMed scientific paper summarization python -m bigbird.summarization.run_summarization \ --data_dir="tfds://scientific_papers/pubmed" \ --output_dir="/tmp/bigbird_pubmed" \ --init_checkpoint="bigbird/ckpt/bigbp_large/model.ckpt-0" \ --max_encoder_length=4096 \ --max_decoder_length=256 \ --attention_type="block_sparse" \ --norm_type="prenorm" \ --vocab_model_file="pegasus" \ --train_batch_size=4 \ --learning_rate=0.32 \ --optimizer="Adafactor" \ --num_train_steps=100000 \ --num_warmup_steps=10000 \ --beam_size=5 \ --alpha=0.8 \ --label_smoothing=0.1 \ --do_train=True ``` -------------------------------- ### Execute Training Loop Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Performs model training steps using the Adam optimizer and tracks loss and accuracy metrics. ```python opt = tf.keras.optimizers.Adam(FLAGS.learning_rate) train_loss = tf.keras.metrics.Mean(name='train_loss') train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy') for i, ex in enumerate(tqdm(dataset.take(FLAGS.num_train_steps), position=0)): loss, log_probs, grads = fwd_bwd(ex[0], ex[1]) opt.apply_gradients(zip(grads, model.trainable_weights+headl.trainable_weights)) train_loss(loss) train_accuracy(tf.one_hot(ex[1], 2), log_probs) if i% 200 == 0: print('Loss = {} Accuracy = {}'.format(train_loss.result().numpy(), train_accuracy.result().numpy())) ``` -------------------------------- ### Initialize MultiHeadedAttentionLayer Source: https://context7.com/google-research/bigbird/llms.txt Configures the attention layer with specific block sparse parameters and performs a forward pass with required masks. ```python from bigbird.core import attention from bigbird.core import utils import tensorflow as tf # Configure attention layer num_heads = 12 size_per_head = 64 seq_length = 4096 block_size = 64 # Create block sparse attention layer attn_layer = attention.MultiHeadedAttentionLayer( attention_type='block_sparse', # 'original_full', 'simulated_sparse', 'block_sparse' num_attention_heads=num_heads, size_per_head=size_per_head, num_rand_blocks=3, from_seq_length=seq_length, to_seq_length=seq_length, from_block_size=block_size, to_block_size=block_size, attention_probs_dropout_prob=0.1, initializer_range=0.02, use_bias=True, seed=42 ) # Prepare inputs batch_size = 2 hidden_size = num_heads * size_per_head from_tensor = tf.random.normal([batch_size, seq_length, hidden_size]) to_tensor = from_tensor # Self-attention # Create attention masks for block sparse attention input_mask = tf.ones([batch_size, seq_length]) blocked_mask = tf.reshape(input_mask, [batch_size, seq_length // block_size, block_size]) from_mask = tf.reshape(input_mask, [batch_size, 1, seq_length, 1]) to_mask = tf.reshape(input_mask, [batch_size, 1, 1, seq_length]) band_mask = attention.create_band_mask_from_inputs(blocked_mask, blocked_mask) masks = [None, band_mask, from_mask, to_mask, blocked_mask, blocked_mask] # Forward pass context_output = attn_layer( from_tensor=from_tensor, to_tensor=to_tensor, masks=masks, training=True ) ``` -------------------------------- ### Configure and Execute BigBird Decoder Source: https://context7.com/google-research/bigbird/llms.txt Initializes the decoder stack with specific hyperparameters and performs a forward pass using random input tensors. ```python params = { 'hidden_size': 768, 'num_hidden_layers': 12, 'num_attention_heads': 12, 'intermediate_size': 3072, 'hidden_act': 'gelu', 'attention_probs_dropout_prob': 0.1, 'hidden_dropout_prob': 0.1, 'initializer_range': 0.02, 'norm_type': 'prenorm', 'use_bias': True, 'use_gradient_checkpointing': False, 'couple_encoder_decoder': False, # Separate encoder/decoder weights } # Create decoder stack decoder_stack = decoder.DecoderStack(params) # Prepare inputs batch_size = 2 encoder_length = 4096 decoder_length = 256 hidden_size = params['hidden_size'] decoder_inputs = tf.random.normal([batch_size, decoder_length, hidden_size]) encoder_outputs = tf.random.normal([batch_size, encoder_length, hidden_size]) encoder_mask = tf.ones([batch_size, encoder_length]) # Create causal self-attention mask self_attention_mask = decoder.create_self_attention_mask(decoder_length) # Forward pass decoder_output = decoder_stack( decoder_inputs=decoder_inputs, self_attention_mask=self_attention_mask, encoder_outputs=encoder_outputs, encoder_mask=encoder_mask, cache=None, # For incremental decoding decode_i=None, training=True ) # decoder_output: [batch_size, decoder_length, hidden_size] ``` -------------------------------- ### Display Results Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Prints the original article, predicted summary, and ground truth summary using the tokenizer. ```python print('Article:\n {}\n\n Predicted summary:\n {}\n\n Ground truth summary:\n {}\n\n'.format( tokenizer.detokenize(ex[0]), tokenizer.detokenize(pred_ids), tokenizer.detokenize(ex[1]))) ``` -------------------------------- ### License Header Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Standard Apache License 2.0 header for BigBird source files. ```python # Copyright 2020 The BigBird Authors. All Rights Reserved. # # 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 # # http://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 TensorFlow and Libraries Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Imports necessary TensorFlow modules, TensorFlow Datasets, and TensorFlow Text. Ensures TensorFlow v2 behavior is enabled. ```python import tensorflow.compat.v2 as tf import tensorflow_datasets as tfds import tensorflow_text as tft from tqdm import tqdm tf.enable_v2_behavior() ``` -------------------------------- ### Instantiate Transformer Model Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Create the Transformer model instance within the variable container. ```python with container.as_default(): model = modeling.TransformerModel(transformer_config) ``` -------------------------------- ### Manage BigBird Configurations Source: https://context7.com/google-research/bigbird/llms.txt Accesses default hyperparameters and defines custom configuration dictionaries for model architecture and training. ```python from bigbird.core import flags from bigbird.core import utils # Access default configuration default_config = utils.get_default_config() print(f"Default attention type: {default_config['attention_type']}") print(f"Default block size: {default_config['block_size']}") print(f"Default hidden size: {default_config['hidden_size']}") # Configuration dictionary structure: config = { # Transformer architecture 'hidden_size': 768, 'num_hidden_layers': 12, 'num_attention_heads': 12, 'intermediate_size': 3072, 'hidden_act': 'gelu', 'max_position_embeddings': 4096, # BigBird sparse attention 'attention_type': 'block_sparse', # 'original_full', 'simulated_sparse', 'block_sparse' 'block_size': 64, # Size of attention blocks 'num_rand_blocks': 3, # Random attention blocks per row # Normalization 'norm_type': 'postnorm', # 'prenorm' (Pegasus) or 'postnorm' (BERT) # Regularization 'attention_probs_dropout_prob': 0.1, 'hidden_dropout_prob': 0.1, 'initializer_range': 0.02, # Sequence lengths 'max_encoder_length': 4096, 'max_decoder_length': 256, # Decoder settings (for seq2seq) 'beam_size': 5, 'alpha': 0.8, # Length normalization 'label_smoothing': 0.1, } # Save and load configuration # flags.save('/path/to/config.json') # Save current flags # flags.load('/path/to/config.json') # Load saved flags ``` -------------------------------- ### Instantiate BigBird Encoder Layers Source: https://github.com/google-research/bigbird/blob/master/README.md Alternatively, instantiate only the layers of the BigBird encoder for more granular control. ```python from bigbird.core import encoder only_layers = encoder.EncoderStack(...) ``` -------------------------------- ### Configure and Run BigBird Encoder Stack Source: https://context7.com/google-research/bigbird/llms.txt Defines the hyperparameter dictionary and executes a forward pass through the encoder stack. ```python params = { 'hidden_size': 768, 'num_hidden_layers': 12, 'num_attention_heads': 12, 'intermediate_size': 3072, 'hidden_act': 'gelu', 'attention_probs_dropout_prob': 0.1, 'hidden_dropout_prob': 0.1, 'initializer_range': 0.02, 'max_encoder_length': 4096, 'attention_type': 'block_sparse', # BigBird sparse attention 'norm_type': 'postnorm', # BERT-style post-normalization 'block_size': 64, 'num_rand_blocks': 3, 'use_bias': True, 'use_gradient_checkpointing': False, # Memory optimization } # Create encoder stack encoder_stack = encoder.EncoderStack(params) # Prepare inputs batch_size, seq_length = 2, 4096 hidden_size = params['hidden_size'] encoder_inputs = tf.random.normal([batch_size, seq_length, hidden_size]) encoder_mask = tf.ones([batch_size, seq_length]) # 1 for valid tokens, 0 for padding # Forward pass through encoder layers encoder_output = encoder_stack( encoder_inputs=encoder_inputs, encoder_inputs_mask=encoder_mask, training=True ) ``` -------------------------------- ### Instantiate BigBird Encoder Source: https://github.com/google-research/bigbird/blob/master/README.md Use this code to directly instantiate the BigBird encoder, which can replace BERT's encoder. ```python from bigbird.core import modeling bigb_encoder = modeling.BertModel(...) ``` -------------------------------- ### Load Pretrained BigBird Model Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Initializes model weights from a specified checkpoint path using a TensorFlow checkpoint reader. ```python ckpt_path = 'gs://bigbird-transformer/pretrain/bigbr_base/model.ckpt-0' ckpt_reader = tf.compat.v1.train.NewCheckpointReader(ckpt_path) model.set_weights([ckpt_reader.get_tensor(v.name[:-2]) for v in tqdm(model.trainable_weights, position=0)]) ``` -------------------------------- ### Convert Flags to Dictionary Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Export the current flag configuration into a dictionary format. ```python transformer_config = flags.as_dictionary() ``` -------------------------------- ### Evaluation Input Pipeline Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Configures the input function and dataset for model evaluation. ```python eval_input_fn = run_summarization.input_fn_builder( data_dir=FLAGS.data_dir, vocab_model_file=FLAGS.vocab_model_file, max_encoder_length=FLAGS.max_encoder_length, max_decoder_length=FLAGS.max_decoder_length, substitute_newline=FLAGS.substitute_newline, is_training=False) eval_dataset = eval_input_fn({'batch_size': 8}) ``` -------------------------------- ### Load Model Checkpoint Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Restores model weights from a specified checkpoint path for continued training or inference. ```python ckpt_path = 'gs://bigbird-transformer/summarization/pubmed/roberta/model.ckpt-300000' ckpt_reader = tf.compat.v1.train.NewCheckpointReader(ckpt_path) loaded_weights = [] for v in tqdm(model.trainable_weights, position=0): try: val = ckpt_reader.get_tensor(v.name[:-2]) except: val = v.numpy() loaded_weights.append(val) model.set_weights(loaded_weights) ``` -------------------------------- ### Configure and Use TransformerModel for Seq2Seq Tasks Source: https://context7.com/google-research/bigbird/llms.txt This snippet demonstrates setting up and using the TransformerModel for sequence-to-sequence tasks, featuring BigBird attention in the encoder and beam search for generation. Note the 'norm_type' for Pegasus-style pre-normalization and parameters for beam search decoding. ```python from bigbird.core import modeling from bigbird.core import utils import tensorflow as tf # Configure seq2seq parameters params = utils.get_default_config() params['vocab_size'] = 32000 params['hidden_size'] = 768 params['num_hidden_layers'] = 12 params['num_attention_heads'] = 12 params['intermediate_size'] = 3072 params['max_encoder_length'] = 4096 params['max_decoder_length'] = 256 params['attention_type'] = 'block_sparse' params['norm_type'] = 'prenorm' # Pegasus-style pre-normalization params['beam_size'] = 5 params['alpha'] = 0.8 # Length penalty params['scope'] = 'pegasus' # Create encoder-decoder model model = modeling.TransformerModel(params) # Prepare inputs input_ids = tf.constant([[1, 2, 3, 4, 5] + [0] * 4091]) # Source document target_ids = tf.constant([[2, 3, 4, 1]]) # Target summary (for training) # Training forward pass (log_probs, logits, pred_ids), encoder_output = model( input_ids=input_ids, target_ids=target_ids, training=True ) # log_probs: [batch, target_length] - log probabilities of target tokens # logits: [batch, target_length, vocab_size] - raw logits # pred_ids: [batch, target_length] - predicted token ids # Inference (beam search generation) (_, _, generated_ids), _ = model( input_ids=input_ids, target_ids=None, # No targets during inference training=False ) ``` -------------------------------- ### Configure Model Parameters Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Sets the configuration flags for the BigBird model, including data directory, attention type, and training hyperparameters. ```python FLAGS.data_dir = "tfds://imdb_reviews/plain_text" FLAGS.attention_type = "block_sparse" FLAGS.max_encoder_length = 4096 # reduce for quicker demo on free colab FLAGS.learning_rate = 1e-5 FLAGS.num_train_steps = 2000 FLAGS.attention_probs_dropout_prob = 0.0 FLAGS.hidden_dropout_prob = 0.0 FLAGS.use_gradient_checkpointing = True FLAGS.vocab_model_file = "gpt2" ``` -------------------------------- ### Load Summarization Model Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Loads a pre-trained BigBird summarization model from a specified Google Cloud Storage path. This model is intended for serving. ```python path = 'gs://bigbird-transformer/summarization/pubmed/roberta/saved_model' imported_model = tf.saved_model.load(path, tags='serve') summerize = imported_model.signatures['serving_default'] ``` -------------------------------- ### Import ROUGE evaluation modules Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Required imports for calculating ROUGE scores using the rouge_score library. ```python from rouge_score import rouge_scorer from rouge_score import scoring ``` -------------------------------- ### Run Model Evaluation Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Iterates through the evaluation dataset to compute and print final loss and accuracy metrics. ```python eval_loss = tf.keras.metrics.Mean(name='eval_loss') eval_accuracy = tf.keras.metrics.CategoricalAccuracy(name='eval_accuracy') for ex in tqdm(eval_dataset, position=0): loss, log_probs = fwd_only(ex[0], ex[1]) eval_loss(loss) eval_accuracy(tf.one_hot(ex[1], 2), log_probs) print('Loss = {} Accuracy = {}'.format(eval_loss.result().numpy(), eval_accuracy.result().numpy())) ``` -------------------------------- ### Run Evaluation with ROUGE Scoring Source: https://context7.com/google-research/bigbird/llms.txt Executes the summarization evaluation script using the specified dataset and model parameters. ```bash python -m bigbird.summarization.run_summarization \ --data_dir="tfds://scientific_papers/pubmed" \ --output_dir="/tmp/bigbird_pubmed" \ --max_encoder_length=4096 \ --max_decoder_length=256 \ --do_eval=True ``` -------------------------------- ### Evaluation Loop Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Iterates through the evaluation dataset to calculate log-likelihood metrics. ```python eval_llh = tf.keras.metrics.Mean(name='eval_llh') for ex in tqdm(eval_dataset, position=0): llh, logits, pred_ids = fwd_only(ex[0], ex[1]) eval_llh(llh) print('Log Likelihood = {}'.format(eval_llh.result().numpy())) ``` -------------------------------- ### Configure Model Parameters Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Set hyperparameters for the BigBird model, including attention type and sequence lengths. ```python FLAGS.data_dir = "tfds://scientific_papers/pubmed" FLAGS.attention_type = "block_sparse" FLAGS.couple_encoder_decoder = True FLAGS.max_encoder_length = 3072 # reduce for quicker demo on free colab FLAGS.max_decoder_length = 256 FLAGS.block_size = 64 FLAGS.learning_rate = 1e-5 FLAGS.num_train_steps = 10000 FLAGS.attention_probs_dropout_prob = 0.0 FLAGS.hidden_dropout_prob = 0.0 FLAGS.use_gradient_checkpointing = True FLAGS.vocab_model_file = "gpt2" ``` -------------------------------- ### Configure and Use BertModel with BigBird Attention Source: https://context7.com/google-research/bigbird/llms.txt Use this snippet to configure and run a BERT-style encoder model with BigBird's block sparse attention for long sequences. Ensure 'block_sparse' attention type is set for sequences longer than 512 tokens. ```python from bigbird.core import modeling from bigbird.core import utils import tensorflow as tf # Configure BigBird parameters params = utils.get_default_config() params['vocab_size'] = 32000 params['hidden_size'] = 768 params['num_hidden_layers'] = 12 params['num_attention_heads'] = 12 params['intermediate_size'] = 3072 params['max_encoder_length'] = 4096 # Long sequence support params['attention_type'] = 'block_sparse' # Options: 'original_full', 'simulated_sparse', 'block_sparse' params['block_size'] = 64 params['num_rand_blocks'] = 3 params['scope'] = 'bert' # Create the model model = modeling.BertModel(params) # Prepare input (batch_size=2, seq_length=4096) input_ids = tf.constant([[101, 2054, 2003, 1996] + [0] * 4092, [101, 1045, 2293, 2017] + [0] * 4092]) token_type_ids = tf.zeros_like(input_ids, dtype=tf.int32) # Forward pass sequence_output, pooled_output = model( input_ids=input_ids, token_type_ids=token_type_ids, training=False ) # sequence_output: [batch_size, seq_length, hidden_size] - contextualized representations # pooled_output: [batch_size, hidden_size] - [CLS] token representation for classification ``` -------------------------------- ### Load PubMed Dataset Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Loads the 'scientific_papers/pubmed' dataset from TensorFlow Datasets for testing. The dataset is loaded without shuffling. ```python dataset = tfds.load('scientific_papers/pubmed', split='test', shuffle_files=False, as_supervised=True) ``` -------------------------------- ### Compute scores over a dataset Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/eval.ipynb Iterate through a dataset to generate summaries and aggregate their ROUGE scores. ```python for ex in tqdm(dataset.take(100), position=0): predicted_summary = summerize(ex[0])['pred_sent'][0] score = scorer.score(ex[1].numpy().decode('utf-8'), predicted_summary.numpy().decode('utf-8')) aggregator.add_scores(score) ``` -------------------------------- ### Perform Inference Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Runs a single forward pass to obtain predicted IDs for a given input. ```python _, _, pred_ids = fwd_only(ex[0], ex[1]) ``` -------------------------------- ### Big Bird Citation Source: https://github.com/google-research/bigbird/blob/master/README.md Cite this paper if you find the Big Bird model useful for your research. ```bibtex @article{zaheer2020bigbird, title={Big bird: Transformers for longer sequences}, author={Zaheer, Manzil and Guruganesh, Guru and Dubey, Kumar Avinava and Ainslie, Joshua and Alberti, Chris and Ontanon, Santiago and Pham, Philip and Ravula, Anirudh and Wang, Qifan and Yang, Li and others}, journal={Advances in Neural Information Processing Systems}, volume={33}, year={2020} } ``` -------------------------------- ### Define Forward and Backward Pass Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Define a TensorFlow function for the forward and backward pass, including loss calculation and gradient computation. ```python @tf.function(experimental_compile=True) def fwd_bwd(features, labels): with tf.GradientTape() as g: (llh, logits, pred_ids), _ = model(features, target_ids=labels, training=True) loss = run_summarization.padded_cross_entropy_loss( logits, labels, transformer_config["label_smoothing"], transformer_config["vocab_size"]) grads = g.gradient(loss, model.trainable_weights) return loss, llh, logits, pred_ids, grads ``` -------------------------------- ### Define Forward and Backward Pass Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb Defines a TensorFlow function to compute the loss and gradients for the model. ```python @tf.function(experimental_compile=True) def fwd_bwd(features, labels): with tf.GradientTape() as g: _, pooled_output = model(features, training=True) loss, log_probs = headl(pooled_output, labels, True) grads = g.gradient(loss, model.trainable_weights+headl.trainable_weights) return loss, log_probs, grads ``` -------------------------------- ### Define Attention Type Flag Source: https://github.com/google-research/bigbird/blob/master/README.md Defines an enum flag for selecting the attention implementation. 'block_sparse' enables BigBird's attention module. Consider 'original_full' for sequence lengths less than 1024. ```python flags.DEFINE_enum( "attention_type", "block_sparse", ["original_full", "simulated_sparse", "block_sparse"], "Selecting attention implementation. " "'original_full': full attention from original bert. " "'simulated_sparse': simulated sparse attention. " "'block_sparse': blocked implementation of sparse attention.") ``` -------------------------------- ### Define Forward-Only Pass Source: https://github.com/google-research/bigbird/blob/master/bigbird/classifier/imdb.ipynb A compiled TensorFlow function for inference or evaluation without updating model weights. ```python @tf.function(experimental_compile=True) def fwd_only(features, labels): _, pooled_output = model(features, training=False) loss, log_probs = headl(pooled_output, labels, False) return loss, log_probs ``` -------------------------------- ### Forward Pass Function Source: https://github.com/google-research/bigbird/blob/master/bigbird/summarization/pubmed.ipynb Defines a compiled forward pass for inference or evaluation without gradient calculation. ```python @tf.function(experimental_compile=True) def fwd_only(features, labels): (llh, logits, pred_ids), _ = model(features, target_ids=labels, training=False) return llh, logits, pred_ids ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.