This page lists all the custom layers used by the library, as well as the utility functions it provides for modeling.
Most of those are only useful if you are studying the code of the models in the library.
( nf nx )
1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2).
Basically works like a linear layer but the weights are transposed.
( config: PretrainedConfig )
Parameters
hidden_size of the model.
Compute SQuAD start logits from sequence hidden states.
(
hidden_states: FloatTensor
p_mask: typing.Optional[torch.FloatTensor] = None
)
→
torch.FloatTensor
Parameters
torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.
torch.FloatTensor of shape (batch_size, seq_len), optional) —
Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.
Returns
torch.FloatTensor
The start logits for SQuAD.
( config: PretrainedConfig )
Parameters
hidden_size of the model and the layer_norm_eps
to use.
Compute SQuAD end logits from sequence hidden states.
(
hidden_states: FloatTensor
start_states: typing.Optional[torch.FloatTensor] = None
start_positions: typing.Optional[torch.LongTensor] = None
p_mask: typing.Optional[torch.FloatTensor] = None
)
→
torch.FloatTensor
Parameters
torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.
torch.FloatTensor of shape (batch_size, seq_len, hidden_size), optional) —
The hidden states of the first tokens for the labeled span.
torch.LongTensor of shape (batch_size,), optional) —
The position of the first token for the labeled span.
torch.FloatTensor of shape (batch_size, seq_len), optional) —
Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.
Returns
torch.FloatTensor
The end logits for SQuAD.
One of start_states or start_positions should be not None. If both are set, start_positions overrides
start_states.
( config )
Parameters
hidden_size of the model.
Compute SQuAD 2.0 answer class from classification and start tokens hidden states.
(
hidden_states: FloatTensor
start_states: typing.Optional[torch.FloatTensor] = None
start_positions: typing.Optional[torch.LongTensor] = None
cls_index: typing.Optional[torch.LongTensor] = None
)
→
torch.FloatTensor
Parameters
torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.
torch.FloatTensor of shape (batch_size, seq_len, hidden_size), optional) —
The hidden states of the first tokens for the labeled span.
torch.LongTensor of shape (batch_size,), optional) —
The position of the first token for the labeled span.
torch.LongTensor of shape (batch_size,), optional) —
Position of the CLS token for each sentence in the batch. If None, takes the last token.
Returns
torch.FloatTensor
The SQuAD 2.0 answer class.
One of start_states or start_positions should be not None. If both are set, start_positions overrides
start_states.
( loss: typing.Optional[torch.FloatTensor] = None start_top_log_probs: typing.Optional[torch.FloatTensor] = None start_top_index: typing.Optional[torch.LongTensor] = None end_top_log_probs: typing.Optional[torch.FloatTensor] = None end_top_index: typing.Optional[torch.LongTensor] = None cls_logits: typing.Optional[torch.FloatTensor] = None )
Parameters
torch.FloatTensor of shape (1,), optional, returned if both start_positions and end_positions are provided) —
Classification loss as the sum of start token, end token (and is_impossible if provided) classification
losses.
torch.FloatTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) —
Log probabilities for the top config.start_n_top start token possibilities (beam-search).
torch.LongTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) —
Indices for the top config.start_n_top start token possibilities (beam-search).
torch.FloatTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) —
Log probabilities for the top config.start_n_top * config.end_n_top end token possibilities
(beam-search).
torch.LongTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) —
Indices for the top config.start_n_top * config.end_n_top end token possibilities (beam-search).
torch.FloatTensor of shape (batch_size,), optional, returned if start_positions or end_positions is not provided) —
Log probabilities for the is_impossible label of the answers.
Base class for outputs of question answering models using a SQuADHead.
( config )
Parameters
hidden_size of the model and the layer_norm_eps
to use.
A SQuAD head inspired by XLNet.
(
hidden_states: FloatTensor
start_positions: typing.Optional[torch.LongTensor] = None
end_positions: typing.Optional[torch.LongTensor] = None
cls_index: typing.Optional[torch.LongTensor] = None
is_impossible: typing.Optional[torch.LongTensor] = None
p_mask: typing.Optional[torch.FloatTensor] = None
return_dict: bool = False
)
→
transformers.modeling_utils.SquadHeadOutput or tuple(torch.FloatTensor)
Parameters
torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
Final hidden states of the model on the sequence tokens.
torch.LongTensor of shape (batch_size,), optional) —
Positions of the first token for the labeled span.
torch.LongTensor of shape (batch_size,), optional) —
Positions of the last token for the labeled span.
torch.LongTensor of shape (batch_size,), optional) —
Position of the CLS token for each sentence in the batch. If None, takes the last token.
torch.LongTensor of shape (batch_size,), optional) —
Whether the question has a possible answer in the paragraph or not.
torch.FloatTensor of shape (batch_size, seq_len), optional) —
Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.
bool, optional, defaults to False) —
Whether or not to return a ModelOutput instead of a plain tuple.
Returns
transformers.modeling_utils.SquadHeadOutput or tuple(torch.FloatTensor)
A transformers.modeling_utils.SquadHeadOutput or a tuple of
torch.FloatTensor (if return_dict=False is passed or when config.return_dict=False) comprising various
elements depending on the configuration (<class 'transformers.configuration_utils.PretrainedConfig'>) and inputs.
torch.FloatTensor of shape (1,), optional, returned if both start_positions and end_positions are provided) — Classification loss as the sum of start token, end token (and is_impossible if provided) classification
losses.torch.FloatTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the top config.start_n_top start token possibilities (beam-search).torch.LongTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) — Indices for the top config.start_n_top start token possibilities (beam-search).torch.FloatTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the top config.start_n_top * config.end_n_top end token possibilities
(beam-search).torch.LongTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) — Indices for the top config.start_n_top * config.end_n_top end token possibilities (beam-search).torch.FloatTensor of shape (batch_size,), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the is_impossible label of the answers.( config: PretrainedConfig )
Parameters
summary_type (str) — The method to use to make this summary. Accepted values are:
"last" — Take the last token hidden state (like XLNet)"first" — Take the first token hidden state (like Bert)"mean" — Take the mean of all tokens hidden states"cls_index" — Supply a Tensor of classification token position (GPT/GPT-2)"attn" — Not implemented now, use multi-head attentionsummary_use_proj (bool) — Add a projection after the vector extraction.
summary_proj_to_labels (bool) — If True, the projection outputs to config.num_labels classes
(otherwise to config.hidden_size).
summary_activation (Optional[str]) — Set to "tanh" to add a tanh activation to the output,
another string or None will add no activation.
summary_first_dropout (float) — Optional dropout probability before the projection and activation.
summary_last_dropout (float)— Optional dropout probability after the projection and activation.
Compute a single vector summary of a sequence hidden states.
(
hidden_states: FloatTensor
cls_index: typing.Optional[torch.LongTensor] = None
)
→
torch.FloatTensor
Parameters
torch.FloatTensor of shape [batch_size, seq_len, hidden_size]) —
The hidden states of the last layer.
torch.LongTensor of shape [batch_size] or [batch_size, ...] where … are optional leading dimensions of hidden_states, optional) —
Used if summary_type == "cls_index" and takes the last token of the sequence as classification token.
Returns
torch.FloatTensor
The summary of the sequence hidden states.
Compute a single vector summary of a sequence hidden states.
(
forward_fn: typing.Callable[..., torch.Tensor]
chunk_size: int
chunk_dim: int
*input_tensors
)
→
torch.Tensor
Parameters
Callable[..., torch.Tensor]) —
The forward function of the model.
int) —
The chunk size of a chunked tensor: num_chunks = len(input_tensors[0]) / chunk_size.
int) —
The dimension over which the input_tensors should be chunked.
Tuple[torch.Tensor]) —
The input tensors of forward_fn which will be chunked
Returns
torch.Tensor
A tensor with the same shape as the forward_fn would have given if applied`.
This function chunks the input_tensors into smaller input tensor parts of size chunk_size over the dimension
chunk_dim. It then applies a layer forward_fn to each chunk independently to save memory.
If the forward_fn is independent across the chunk_dim this function will yield the same result as directly
applying forward_fn to input_tensors.
Examples:
# rename the usual forward() fn to forward_chunk()
def forward_chunk(self, hidden_states):
hidden_states = self.decoder(hidden_states)
return hidden_states
# implement a chunked forward function
def forward(self, hidden_states):
return apply_chunking_to_forward(self.forward_chunk, self.chunk_size_lm_head, self.seq_len_dim, hidden_states)(
heads: typing.List[int]
n_heads: int
head_size: int
already_pruned_heads: typing.Set[int]
)
→
Tuple[Set[int], torch.LongTensor]
Parameters
List[int]) — List of the indices of heads to prune.
int) — The number of heads in the model.
int) — The size of each head.
Set[int]) — A set of already pruned heads.
Returns
Tuple[Set[int], torch.LongTensor]
A tuple with the indices of heads to prune taking already_pruned_heads
into account and the indices of rows/columns to keep in the layer weight.
Finds the heads and their indices taking already_pruned_heads into account.
(
layer: typing.Union[torch.nn.modules.linear.Linear, transformers.pytorch_utils.Conv1D]
index: LongTensor
dim: typing.Optional[int] = None
)
→
torch.nn.Linear or Conv1D
Parameters
Union[torch.nn.Linear, Conv1D]) — The layer to prune.
torch.LongTensor) — The indices to keep in the layer.
int, optional) — The dimension on which to keep the indices.
Returns
torch.nn.Linear or Conv1D
The pruned layer as a new layer with requires_grad=True.
Prune a Conv1D or linear layer to keep only entries in index.
Used to remove heads.
( layer: Conv1D index: LongTensor dim: int = 1 ) → Conv1D
Prune a Conv1D layer to keep only entries in index. A Conv1D work as a Linear layer (see e.g. BERT) but the weights are transposed.
Used to remove heads.
(
layer: Linear
index: LongTensor
dim: int = 0
)
→
torch.nn.Linear
Prune a linear layer to keep only entries in index.
Used to remove heads.
( *args **kwargs )
1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2).
Basically works like a linear layer but the weights are transposed.
( *args **kwargs )
Parameters
int) —
The size of the vocabulary, e.g., the number of unique tokens.
int) —
The size of the embedding vectors.
float, optional) —
The standard deviation to use when initializing the weights. If no value is provided, it will default to
{@html "1/hidden_size"}.
kwargs —
Additional keyword arguments passed along to the __init__ of tf.keras.layers.Layer.
Construct shared token embeddings.
The weights of the embedding layer is usually shared with the weights of the linear decoder when doing language modeling.
(
inputs: Tensor
mode: str = 'embedding'
)
→
tf.Tensor
Parameters
tf.Tensor) —
In embedding mode, should be an int64 tensor with shape [batch_size, length].
In linear mode, should be a float tensor with shape [batch_size, length, hidden_size].
str, defaults to "embedding") —
A valid value is either "embedding" or "linear", the first one indicates that the layer should be
used as an embedding layer, the second one that the layer should be used as a linear decoder.
In embedding mode, the output is a float32 embedding tensor, with shape [batch_size, length, embedding_size].
In linear mode, the output is a float32 with shape [batch_size, length, vocab_size].
ValueError — if mode is not valid.Get token embeddings of inputs or decode final hidden state.
Shared weights logic is adapted from here.
( *args **kwargs )
Parameters
summary_type (str) — The method to use to make this summary. Accepted values are:
"last" — Take the last token hidden state (like XLNet)"first" — Take the first token hidden state (like Bert)"mean" — Take the mean of all tokens hidden states"cls_index" — Supply a Tensor of classification token position (GPT/GPT-2)"attn" — Not implemented now, use multi-head attentionsummary_use_proj (bool) — Add a projection after the vector extraction.
summary_proj_to_labels (bool) — If True, the projection outputs to config.num_labels classes
(otherwise to config.hidden_size).
summary_activation (Optional[str]) — Set to "tanh" to add a tanh activation to the output,
another string or None will add no activation.
summary_first_dropout (float) — Optional dropout probability before the projection and activation.
summary_last_dropout (float)— Optional dropout probability after the projection and activation.
float, defaults to 0.02) — The standard deviation to use to initialize the weights.
kwargs —
Additional keyword arguments passed along to the __init__ of tf.keras.layers.Layer.
Compute a single vector summary of a sequence hidden states.
Loss function suitable for causal language modeling (CLM), that is, the task of guessing the next token.
Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.
Loss function suitable for masked language modeling (MLM), that is, the task of guessing the masked tokens.
Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.
Loss function suitable for multiple choice tasks.
Loss function suitable for question answering.
Loss function suitable for sequence classification.
Loss function suitable for token classification.
Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.
(
initializer_range: float = 0.02
)
→
tf.initializers.TruncatedNormal
Creates a tf.initializers.TruncatedNormal with the given range.
( )
Decorate a Keras Layer class to support Keras serialization.
This is done by:
transformers_config dict to the Keras config dictionary in get_config (called by Keras at
serialization time.__init__ to accept that transformers_config dict (passed by Keras at deserialization time) and
convert it to a config object for the actual layer initializer.custom_objects in the call to tf.keras.models.load_model.(
tensor: typing.Union[tensorflow.python.framework.ops.Tensor, numpy.ndarray]
)
→
List[int]
Deal with dynamic shape in tensorflow cleanly.