Commit fd90a65f authored by Chen Chen's avatar Chen Chen Committed by A. Unique TensorFlower
Browse files

Internal change

PiperOrigin-RevId: 362461011
parent d87fd224
# NLP Modeling Library # NLP Modeling Library
This library provides a set of Keras primitives (Layers, Networks, and Models) This library provides a set of Keras primitives (`tf.keras.Layer` and
that can be assembled into transformer-based models. They are `tf.keras.Model`) that can be assembled into transformer-based models.
flexible, validated, interoperable, and both TF1 and TF2 compatible. They are flexible, validated, interoperable, and both TF1 and TF2 compatible.
* [`layers`](layers) are the fundamental building blocks for NLP models. * [`layers`](layers) are the fundamental building blocks for NLP models.
They can be used to assemble new layers, networks, or models. They can be used to assemble new `tf.keras` layers or models.
* [`networks`](networks) are combinations of layers (and possibly other networks). They are sub-units of models that would not be trained alone. They * [`networks`](networks) are combinations of `tf.keras` layers (and possibly
encapsulate common network structures like a classification head other networks). They are `tf.keras` models that would not be trained alone.
or a transformer encoder into an easily handled object with a It encapsulates common network structures like a transformer encoder into an
standardized configuration. easily handled object with a standardized configuration.
* [`models`](models) are combinations of layers and networks that would be trained. Pre-built canned models are provided as both convenience functions and canonical examples. * [`models`](models) are combinations of `tf.keras` layers and models that can
be trained. Several pre-built canned models are provided to train encoder
networks. These models are intended as both convenience functions and canonical
examples.
* [`losses`](losses) contains common loss computation used in NLP tasks. * [`losses`](losses) contains common loss computation used in NLP tasks.
...@@ -22,7 +25,9 @@ Please see the colab ...@@ -22,7 +25,9 @@ Please see the colab
for how to build transformer-based NLP models using above primitives. for how to build transformer-based NLP models using above primitives.
Besides the pre-defined primitives, it also provides scaffold classes to allow Besides the pre-defined primitives, it also provides scaffold classes to allow
easy experimentation with noval achitectures, e.g., you don’t need to fork a whole Transformer object to try a different kind of attention primitive, for instance. easy experimentation with noval achitectures, e.g., you don’t need to fork a
whole Transformer object to try a different kind of attention primitive,
for instance.
* [`TransformerScaffold`](layers/transformer_scaffold.py) implements the * [`TransformerScaffold`](layers/transformer_scaffold.py) implements the
Transformer from ["Attention Is All You Need"] Transformer from ["Attention Is All You Need"]
...@@ -43,4 +48,5 @@ Please see the colab ...@@ -43,4 +48,5 @@ Please see the colab
(https://colab.sandbox.google.com/github/tensorflow/models/blob/master/official/colab/nlp/customize_encoder.ipynb) (https://colab.sandbox.google.com/github/tensorflow/models/blob/master/official/colab/nlp/customize_encoder.ipynb)
for how to use scaffold classes to build noval achitectures. for how to use scaffold classes to build noval achitectures.
BERT and ALBERT models in this repo are implemented using this library. Code examples can be found in the corresponding model folder. BERT and ALBERT models in this repo are implemented using this library.
Code examples can be found in the corresponding model folder.
...@@ -12,7 +12,12 @@ ...@@ -12,7 +12,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Modeling package definition.""" """NLP Modeling Library.
This library provides a set of Keras primitives (`tf.keras.Layer` and
`tf.keras.Model`) that can be assembled into transformer-based models.
They are flexible, validated, interoperable, and both TF1 and TF2 compatible.
"""
from official.nlp.modeling import layers from official.nlp.modeling import layers
from official.nlp.modeling import losses from official.nlp.modeling import losses
from official.nlp.modeling import models from official.nlp.modeling import models
......
# Layers # Layers
Layers are the fundamental building blocks for NLP models. They can be used to Layers are the fundamental building blocks for NLP models. They can be used to
assemble new layers, networks, or models. assemble new `tf.keras` layers or models.
* [MultiHeadAttention](attention.py) implements an optionally masked attention * [MultiHeadAttention](attention.py) implements an optionally masked attention
between query, key, value tensors as described in between query, key, value tensors as described in
......
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Layers package definition.""" """Layers are the fundamental building blocks for NLP models.
They can be used to assemble new `tf.keras` layers or models.
"""
# pylint: disable=wildcard-import # pylint: disable=wildcard-import
from official.nlp.modeling.layers.attention import * from official.nlp.modeling.layers.attention import *
from official.nlp.modeling.layers.cls_head import * from official.nlp.modeling.layers.cls_head import *
......
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Activations package definition. Subject to change.""" """Losses contains common loss computation used in NLP (subject to change)."""
from official.nlp.modeling.losses.weighted_sparse_categorical_crossentropy import loss as weighted_sparse_categorical_crossentropy_loss from official.nlp.modeling.losses.weighted_sparse_categorical_crossentropy import loss as weighted_sparse_categorical_crossentropy_loss
# Models # Models
Models are combinations of layers and networks that would be trained. Models are combinations of `tf.keras` layers and models that can be trained.
Several pre-built canned models are provided to train encoder networks. These Several pre-built canned models are provided to train encoder networks.
models are intended as both convenience functions and canonical examples. These models are intended as both convenience functions and canonical examples.
* [`BertClassifier`](bert_classifier.py) implements a simple classification * [`BertClassifier`](bert_classifier.py) implements a simple classification
model containing a single classification head using the Classification network. model containing a single classification head using the Classification network.
......
...@@ -12,7 +12,11 @@ ...@@ -12,7 +12,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Models package definition.""" """Models are combinations of `tf.keras` layers and models that can be trained.
Several pre-built canned models are provided to train encoder networks.
These models are intended as both convenience functions and canonical examples.
"""
from official.nlp.modeling.models.bert_classifier import BertClassifier from official.nlp.modeling.models.bert_classifier import BertClassifier
from official.nlp.modeling.models.bert_pretrainer import * from official.nlp.modeling.models.bert_pretrainer import *
from official.nlp.modeling.models.bert_span_labeler import BertSpanLabeler from official.nlp.modeling.models.bert_span_labeler import BertSpanLabeler
......
# Networks # Networks
Networks are combinations of layers (and possibly other networks). Networks are combinations of `tf.keras` layers (and possibly other networks).
They are sub-units of models that would not be trained alone. It They are `tf.keras` models that would not be trained alone. It encapsulates
encapsulates common network structures like a classification head common network structures like a transformer encoder into an easily
or a transformer encoder into an easily handled object with a handled object with a standardized configuration.
standardized configuration.
* [`BertEncoder`](bert_encoder.py) implements a bi-directional * [`BertEncoder`](bert_encoder.py) implements a bi-directional
Transformer-based encoder as described in ["BERT: Pre-training of Deep Transformer-based encoder as described in ["BERT: Pre-training of Deep
......
...@@ -12,7 +12,12 @@ ...@@ -12,7 +12,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""Networks package definition.""" """Networks are combinations of `tf.keras` layers (and possibly other networks).
They are `tf.keras` models that would not be trained alone. It encapsulates
common network structures like a transformer encoder into an easily
handled object with a standardized configuration.
"""
from official.nlp.modeling.networks.albert_encoder import AlbertEncoder from official.nlp.modeling.networks.albert_encoder import AlbertEncoder
from official.nlp.modeling.networks.bert_encoder import BertEncoder from official.nlp.modeling.networks.bert_encoder import BertEncoder
from official.nlp.modeling.networks.classification import Classification from official.nlp.modeling.networks.classification import Classification
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment