This notebook shows how to reuse a nlp.networks.BertEncoder from TensorFlow Model Garden to power three tasks: (1) pretraining with nlp.models.BertPretrainer (masked-LM + next-sentence), (2) span labeling with nlp.models.BertSpanLabeler (start/end logits for SQuAD-style QA), and (3) classification with nlp.models.BertClassifier ([CLS] head). You install tf-models-official (or tf-models-nightly for latest), import tensorflow_models.nlp, build small dummy examples, run each model forward pass, and compute losses (weighted sparse CE for MLM/NSP; CE for span start/end; CE for classification). Result: a clear pattern for wrapping one encoder into multiple BERT task heads with concise, production-friendly APIs.This notebook shows how to reuse a nlp.networks.BertEncoder from TensorFlow Model Garden to power three tasks: (1) pretraining with nlp.models.BertPretrainer (masked-LM + next-sentence), (2) span labeling with nlp.models.BertSpanLabeler (start/end logits for SQuAD-style QA), and (3) classification with nlp.models.BertClassifier ([CLS] head). You install tf-models-official (or tf-models-nightly for latest), import tensorflow_models.nlp, build small dummy examples, run each model forward pass, and compute losses (weighted sparse CE for MLM/NSP; CE for span start/end; CE for classification). Result: a clear pattern for wrapping one encoder into multiple BERT task heads with concise, production-friendly APIs.

TensorFlow Models NLP Library for Beginners

2025/09/08 17:40

Content Overview

  • Learning objectives

  • Install and import

  • Install the TensorFlow Model Garden pip package

  • Import TensorFlow and other libraries

  • BERT pretraining model

  • Build a BertPretrainer model wrapping BertEncoder

  • Compute loss

  • Span labelling model

  • Build a BertSpanLabeler wrapping BertEncoder

  • Compute loss

  1. Classification model
  2. Build a BertClassifier model wrapping BertEncoder
  3. Compute loss

\

Learning objectives

In this Colab notebook, you will learn how to build transformer-based models for common NLP tasks including pretraining, span labelling and classification using the building blocks from NLP modeling library.

Install and import

Install the TensorFlow Model Garden pip package

  • tf-models-official is the stable Model Garden package. Note that it may not include the latest changes in the tensorflow_models github repo. To include latest changes, you may install tf-models-nightly, which is the nightly Model Garden package created daily automatically.
  • pip will install all models and dependencies automatically.

\

pip install tf-models-official 

Import Tensorflow and other libraries

import numpy as np import tensorflow as tf  from tensorflow_models import nlp 

\

2023-10-17 12:23:04.557393: E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:9342] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2023-10-17 12:23:04.557445: E tensorflow/compiler/xla/stream_executor/cuda/cuda_fft.cc:609] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2023-10-17 12:23:04.557482: E tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:1518] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 

BERT pretraining model

BERT (Pre-training of Deep Bidirectional Transformers for Language Understanding) introduced the method of pre-training language representations on a large text corpus and then using that model for downstream NLP tasks.

In this section, we will learn how to build a model to pretrain BERT on the masked language modeling task and next sentence prediction task. For simplicity, we only show the minimum example and use dummy data.

Build a BertPretrainer model wrapping BertEncoder

The nlp.networks.BertEncoder class implements the Transformer-based encoder as described in BERT paper. It includes the embedding lookups and transformer layers (nlp.layers.TransformerEncoderBlock), but not the masked language model or classification task networks.

The nlp.models.BertPretrainer class allows a user to pass in a transformer stack, and instantiates the masked language model and classification networks that are used to create the training objectives.

\

# Build a small transformer network. vocab_size = 100 network = nlp.networks.BertEncoder(     vocab_size=vocab_size,      # The number of TransformerEncoderBlock layers     num_layers=3) 

\

2023-10-17 12:23:09.241708: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2211] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 

Inspecting the encoder, we see it contains few embedding layers, stacked nlp.layers.TransformerEncoderBlock layers and are connected to three input layers:

input_word_idsinput_type_ids and input_mask.

\

tf.keras.utils.plot_model(network, show_shapes=True, expand_nested=True, dpi=48) 

\

# Create a BERT pretrainer with the created network. num_token_predictions = 8 bert_pretrainer = nlp.models.BertPretrainer(     network, num_classes=2, num_token_predictions=num_token_predictions, output='predictions') 

\

WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/official/nlp/modeling/models/bert_pretrainer.py:112: Classification.__init__ (from official.nlp.modeling.networks.classification) is deprecated and will be removed in a future version. Instructions for updating: Classification as a network is deprecated. Please use the layers.ClassificationHead instead. 

Inspecting the bert_pretrainer, we see it wraps the encoder with additional MaskedLM and nlp.layers.ClassificationHead heads.

\

tf.keras.utils.plot_model(bert_pretrainer, show_shapes=True, expand_nested=True, dpi=48) 

\

# We can feed some dummy data to get masked language model and sentence output. sequence_length = 16 batch_size = 2  word_id_data = np.random.randint(vocab_size, size=(batch_size, sequence_length)) mask_data = np.random.randint(2, size=(batch_size, sequence_length)) type_id_data = np.random.randint(2, size=(batch_size, sequence_length)) masked_lm_positions_data = np.random.randint(2, size=(batch_size, num_token_predictions))  outputs = bert_pretrainer(     [word_id_data, mask_data, type_id_data, masked_lm_positions_data]) lm_output = outputs["masked_lm"] sentence_output = outputs["classification"] print(f'lm_output: shape={lm_output.shape}, dtype={lm_output.dtype!r}') print(f'sentence_output: shape={sentence_output.shape}, dtype={sentence_output.dtype!r}') 

\

lm_output: shape=(2, 8, 100), dtype=tf.float32 sentence_output: shape=(2, 2), dtype=tf.float32 

Compute loss

Next, we can use lm_output and sentence_output to compute loss.

\

masked_lm_ids_data = np.random.randint(vocab_size, size=(batch_size, num_token_predictions)) masked_lm_weights_data = np.random.randint(2, size=(batch_size, num_token_predictions)) next_sentence_labels_data = np.random.randint(2, size=(batch_size))  mlm_loss = nlp.losses.weighted_sparse_categorical_crossentropy_loss(     labels=masked_lm_ids_data,     predictions=lm_output,     weights=masked_lm_weights_data) sentence_loss = nlp.losses.weighted_sparse_categorical_crossentropy_loss(     labels=next_sentence_labels_data,     predictions=sentence_output) loss = mlm_loss + sentence_loss  print(loss) 

\

tf.Tensor(5.2983174, shape=(), dtype=float32) 

With the loss, you can optimize the model. After training, we can save the weights of TransformerEncoder for the downstream fine-tuning tasks. Please see run_pretraining.py for the full example.

Span labeling model

Span labeling is the task to assign labels to a span of the text, for example, label a span of text as the answer of a given question.

In this section, we will learn how to build a span labeling model. Again, we use dummy data for simplicity.

Build a BertSpanLabeler wrapping BertEncoder

The nlp.models.BertSpanLabeler class implements a simple single-span start-end predictor (that is, a model that predicts two values: a start token index and an end token index), suitable for SQuAD-style tasks.

Note that nlp.models.BertSpanLabeler wraps a nlp.networks.BertEncoder, the weights of which can be restored from the above pretraining model.

\

network = nlp.networks.BertEncoder(         vocab_size=vocab_size, num_layers=2)  # Create a BERT trainer with the created network. bert_span_labeler = nlp.models.BertSpanLabeler(network) 

Inspecting the bert_span_labeler, we see it wraps the encoder with additional SpanLabeling that outputs start_position and end_position.

\

tf.keras.utils.plot_model(bert_span_labeler, show_shapes=True, expand_nested=True, dpi=48) 

\

# Create a set of 2-dimensional data tensors to feed into the model. word_id_data = np.random.randint(vocab_size, size=(batch_size, sequence_length)) mask_data = np.random.randint(2, size=(batch_size, sequence_length)) type_id_data = np.random.randint(2, size=(batch_size, sequence_length))  # Feed the data to the model. start_logits, end_logits = bert_span_labeler([word_id_data, mask_data, type_id_data])  print(f'start_logits: shape={start_logits.shape}, dtype={start_logits.dtype!r}') print(f'end_logits: shape={end_logits.shape}, dtype={end_logits.dtype!r}') 

\

start_logits: shape=(2, 16), dtype=tf.float32 end_logits: shape=(2, 16), dtype=tf.float32 

Compute loss

With start_logits and end_logits, we can compute loss:

\

start_positions = np.random.randint(sequence_length, size=(batch_size)) end_positions = np.random.randint(sequence_length, size=(batch_size))  start_loss = tf.keras.losses.sparse_categorical_crossentropy(     start_positions, start_logits, from_logits=True) end_loss = tf.keras.losses.sparse_categorical_crossentropy(     end_positions, end_logits, from_logits=True)  total_loss = (tf.reduce_mean(start_loss) + tf.reduce_mean(end_loss)) / 2 print(total_loss) 

\

tf.Tensor(5.3621416, shape=(), dtype=float32) 

With the loss, you can optimize the model. Please see run_squad.py for the full example.

Classification model

In the last section, we show how to build a text classification model.

Build a BertClassifier model wrapping BertEncoder

nlp.models.BertClassifier implements a [CLS] token classification model containing a single classification head.

\

network = nlp.networks.BertEncoder(         vocab_size=vocab_size, num_layers=2)  # Create a BERT trainer with the created network. num_classes = 2 bert_classifier = nlp.models.BertClassifier(     network, num_classes=num_classes) 

Inspecting the bert_classifier, we see it wraps the encoder with additional Classification head.

\

tf.keras.utils.plot_model(bert_classifier, show_shapes=True, expand_nested=True, dpi=48) 

\

# Create a set of 2-dimensional data tensors to feed into the model. word_id_data = np.random.randint(vocab_size, size=(batch_size, sequence_length)) mask_data = np.random.randint(2, size=(batch_size, sequence_length)) type_id_data = np.random.randint(2, size=(batch_size, sequence_length))  # Feed the data to the model. logits = bert_classifier([word_id_data, mask_data, type_id_data]) print(f'logits: shape={logits.shape}, dtype={logits.dtype!r}') 

\

logits: shape=(2, 2), dtype=tf.float32 

Compute loss

With logits, we can compute loss:

\

labels = np.random.randint(num_classes, size=(batch_size))  loss = tf.keras.losses.sparse_categorical_crossentropy(     labels, logits, from_logits=True) print(loss) 

\

tf.Tensor([0.7332015 1.3447659], shape=(2,), dtype=float32) 

With the loss, you can optimize the model. Please see the Fine tune_bert notebook or the model training documentation for the full example.

\ \

:::info Originally published on the TensorFlow website, this article appears here under a new headline and is licensed under CC BY 4.0. Code samples shared under the Apache 2.0 License.

:::

\

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.
Share Insights

You May Also Like

Ethereum's "double crisis": core talent continues to leave, and technical debt quietly accumulates

Ethereum's "double crisis": core talent continues to leave, and technical debt quietly accumulates

By Eric, Foresight News On the evening of the 19th Beijing time, Bankless co-founder David Hoffman posted a message on X to "mourn" Dankrad Feist, the longest-serving researcher at the Ethereum Foundation, who chose to leave Ethereum and join the stablecoin L1 Tempo. David Hoffman believes the issue of for-profit companies co-opting the talent cultivated by the Ethereum open-source community is significant, and argues that these companies do not, as they claim, bring greater benefits to Ethereum. He bluntly stated, "In my view, Tempo's purpose is to intercept the trillions of dollars in stablecoins expected to flow in over the next decade and place them on their private blockchain. While this will certainly expand the market, Tempo still intends to grab as much of the pie as possible." He believes Tempo will inevitably be constrained by compliance issues, which even issuing tokens cannot address. While both Tempo and Ethereum will bring change to the world, Ethereum is uniquely suited to serve as a trusted, neutral global settlement layer, without shareholders and unconstrained by law. The feeling of disappointment with Ethereum began to surface when its price began to lag behind Bitcoin's in this cycle. However, over time, people began to realize that the exodus of talented individuals from the Ethereum community seemed irreversible. When dreams conflicted with self-interest, many ultimately chose the latter, a fact that many in the industry have long worried about. Dankrad Feist is not the first and will not be the last Dankrad Feist announced his joining Tempo at X on the 17th of this month and stated that he would continue to serve as a research advisor for the Ethereum Foundation's Protocol Cluster's three strategic initiatives: scaling Layer 1, scaling Blobs, and improving user experience. He stated, "Ethereum has strong values and technology choices that make it unique. Tempo will be a great complement, building on similar technology and values while pushing boundaries in scale and speed. I believe this will be a significant benefit to Ethereum. Tempo's open-source technology can be easily integrated back into Ethereum, benefiting the entire ecosystem." According to LinkedIn, Dankrad Feist officially joined Ethereum as a researcher in 2019, focusing on sharding technology, which can scale the Ethereum mainnet. Danksharding, one of the core components of Ethereum's current scaling roadmap, is named after him. Danksharding is a key technical path for Ethereum to achieve high-throughput and low-cost transactions, and is widely considered by the community to be the most important upgrade direction after Ethereum 2.0. Dankrad Feist promoted Proto-Danksharding (EIP-4844), a predecessor of Danksharding. This EIP introduced the blob transaction type, providing a cheaper and more efficient data availability layer for Rollup, significantly reducing the data publishing cost of Rollup. In addition, he had a public debate with Geth development lead Péter Szilágyi on the MEV issue, which eventually prompted Vitalik to step in to coordinate and promote the community's attention to MEV mitigation mechanisms (such as PBS, Proposer-Builder Separation). Tempo researcher Mallesh Pai introduced the members joining Tempo in September, and Liam Horne, former CEO of OP Labs and co-founder of ETHGlobal, also appeared on the list. Before Dankrad Feist, the person who surprised the industry was Danny Ryan, who co-founded Etherealize, a $40 million funding round. A former core member of the Ethereum Foundation and known as the "Chief Engineer of Ethereum 2.0," Ryan joined Etherealize just six months after announcing his indefinite departure in September 2024. However, given that Etherealize shares similarities with ConsenSys, founded by Ethereum co-founder Joseph Lubin 11 years prior amidst controversy over commercialization, Ryan's departure has been widely understood. What really worries David Hoffman are companies like Tempo and Paradigm. Well-known Ethereum developer Federico Carrone expressed a similar sentiment, retweeting David Hoffman's tweet about Dankrad Feist joining Tempo and stating that he has been saying for the past two years that Paradigm's influence within Ethereum could become a tail risk for the entire ecosystem. Federico Carrone wrote that the sole goal of a venture capital fund is to maximize returns for its limited partners. Ethereum shouldn't become deeply dependent on the technology of a venture capital firm that is playing its cards with extreme strategic skill. Following the FTX debacle, Paradigm removed nearly all cryptocurrency-related branding and made a high-profile shift to AI. Carrone believes this is proof enough of his point. After Trump returned to the White House, Paradigm re-entered the Web3 space, aggressively recruiting top researchers from the community, funding key Ethereum open-source libraries, and supporting Stripe's launch of Tempo. Carrone believes that while Paradigm claims its work is beneficial to Ethereum—more funding, more tools, more testing grounds, and the potential for new ideas to feed back into Ethereum—are all potential benefits, but when corporations have excessive visibility and influence over open-source projects, priorities shift from the community's long-term vision to corporate profits. Ethereum’s technical debt is accumulating The simple loss of talent in the Ethereum open source community may not cause widespread concern, but if the loss of talent is accompanied by the accumulation of technical debt, it is worthy of high vigilance. A week ago, a community user posted a screenshot on X, revealing that Solidity's top contributors have all but ceased development. Only Cameel continues to raise new issues and advance the technology, but appears to be in maintenance mode. He believes the community needs to invest more resources in supporting the programming language. Some users in the comments questioned why efforts were being expended on continuously improving and upgrading Solidity rather than simply maintaining it to ensure stability and security. The user who tweeted explained that even changing the Solidity compiler wouldn't change any deployed contracts, but could improve security, enhance the development experience, or support the use of new contracts. As can be seen in the chart above, development activity began to decline sharply at the beginning of the previous bull market. Federico Carrone also expressed his concern, stating that his biggest concern is that the numerous core tools and libraries built around Solidity may not receive long-term maintenance. Even the latest Solidity compiler is currently supported by only a handful of developers. Furthermore, companies involved in L2 and ZK technologies are downsizing, leaving the final iteration of cutting-edge technologies to a handful of companies. With increasing gas limits, many execution clients have not seen substantial performance improvements, and judging by the libraries, the development teams of these clients appear to be lagging behind. Federico Carrone said, “Ethereum’s technical debt continues to accumulate, not only because the protocol itself must continue to evolve, but also because many of its dependencies and surrounding repositories have become stagnant. The entire ecosystem continues to expand, protecting tens of billions of dollars in assets, while part of its foundation is quietly eroding.” Open source communities cannot simply "generate power with love" For an open-source community like Ethereum, which carries a vast amount of value that can be measured in real money, balancing "generate power with love" and economic incentives is a problem without any real precedent. This should be a matter of great concern to the Ethereum Foundation, but it seems to have been overlooked. Péter Szilágyi, who joined the Ethereum Foundation in 2015 and is responsible for the development and maintenance of Geth, clearly pointed out the three most disappointing problems in a letter to the leadership of the Ethereum Foundation a year and a half ago: being portrayed as a leader externally but marginalized internally; the serious disproportion between income and the growth of Ethereum's market value; and Vitalik and a small group of people around him having too much say in the Ethereum ecosystem. In late 2024, Péter Szilágyi discovered that the Ethereum Foundation was secretly incubating an independent fork of Geth. He was subsequently fired due to a dispute with the Ethereum Foundation and repeatedly declined rehire. The Ethereum Foundation even offered Szilágyi $5 million to separate Geth from the Foundation, but was rejected. Currently, Szilágyi maintains the Geth codebase as an independent contributor. Rumors of corruption within the Ethereum Foundation have been circulating, but this is a problem that should have been anticipated from the moment the Ethereum Foundation was founded. As the saying goes, "where there are people, there are gangs." We can't eliminate human greed, but we also can't allow Ethereum to gradually lose its core value due to commercialization. Ethereum's market capitalization of hundreds of billions of dollars, having handled trillions of dollars in on-chain value transfers for years, is built on infrastructure built by a professional technical team, centered on a permissionless, open-source ethos, and commercialized by a large number of businesses. However, simply maintaining such a massive system requires a significant workforce, and as we've discussed, these individuals are leaving due to disappointment or opting for other projects driven by financial gain. The Ethereum Foundation underwent drastic reforms this year, but so far, they haven't produced any significant results. Ethereum can still be called the world's computer, and its potential for commercial applications is still being explored by talented teams. However, as the foundation of all this, Ethereum cannot continue to disappoint those who still hold on to its ideals.
Share
2025/10/23 09:01
Share