commit files to HF hub
Browse files- config.json +4 -0
- configuration_refseg.py +111 -0
- modeling_refseg.py +82 -0
config.json
CHANGED
|
@@ -5,6 +5,10 @@
|
|
| 5 |
"XLMRobertaForReferenceSegmentation"
|
| 6 |
],
|
| 7 |
"attention_probs_dropout_prob": 0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"bos_token_id": 0,
|
| 9 |
"classifier_dropout": null,
|
| 10 |
"custom_pipelines": {
|
|
|
|
| 5 |
"XLMRobertaForReferenceSegmentation"
|
| 6 |
],
|
| 7 |
"attention_probs_dropout_prob": 0.1,
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "configuration_refseg.XLMRobertaRefSegConfig",
|
| 10 |
+
"AutoModelForTokenClassification": "modeling_refseg.XLMRobertaForReferenceSegmentation"
|
| 11 |
+
},
|
| 12 |
"bos_token_id": 0,
|
| 13 |
"classifier_dropout": null,
|
| 14 |
"custom_pipelines": {
|
configuration_refseg.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class XLMRobertaRefSegConfig(PretrainedConfig):
|
| 5 |
+
r"""
|
| 6 |
+
This is the configuration class to store the configuration of a [`XLMRobertaModel`] or a [`TFXLMRobertaModel`]. It
|
| 7 |
+
is used to instantiate a XLM-RoBERTa model according to the specified arguments, defining the model architecture.
|
| 8 |
+
Instantiating a configuration with the defaults will yield a similar configuration to that of the XLMRoBERTa
|
| 9 |
+
[xlm-roberta-base](https://huggingface.co/xlm-roberta-base) architecture.
|
| 10 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 11 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 12 |
+
Args:
|
| 13 |
+
vocab_size (`int`, *optional*, defaults to 30522):
|
| 14 |
+
Vocabulary size of the XLM-RoBERTa model. Defines the number of different tokens that can be represented by
|
| 15 |
+
the `inputs_ids` passed when calling [`XLMRobertaModel`] or [`TFXLMRobertaModel`].
|
| 16 |
+
hidden_size (`int`, *optional*, defaults to 768):
|
| 17 |
+
Dimensionality of the encoder layers and the pooler layer.
|
| 18 |
+
num_hidden_layers (`int`, *optional*, defaults to 12):
|
| 19 |
+
Number of hidden layers in the Transformer encoder.
|
| 20 |
+
num_attention_heads (`int`, *optional*, defaults to 12):
|
| 21 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 22 |
+
intermediate_size (`int`, *optional*, defaults to 3072):
|
| 23 |
+
Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
|
| 24 |
+
hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
|
| 25 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
| 26 |
+
`"relu"`, `"silu"` and `"gelu_new"` are supported.
|
| 27 |
+
hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
|
| 28 |
+
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
|
| 29 |
+
attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
|
| 30 |
+
The dropout ratio for the attention probabilities.
|
| 31 |
+
max_position_embeddings (`int`, *optional*, defaults to 512):
|
| 32 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
| 33 |
+
just in case (e.g., 512 or 1024 or 2048).
|
| 34 |
+
type_vocab_size (`int`, *optional*, defaults to 2):
|
| 35 |
+
The vocabulary size of the `token_type_ids` passed when calling [`XLMRobertaModel`] or
|
| 36 |
+
[`TFXLMRobertaModel`].
|
| 37 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 38 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 39 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-12):
|
| 40 |
+
The epsilon used by the layer normalization layers.
|
| 41 |
+
position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
|
| 42 |
+
Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
|
| 43 |
+
positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
|
| 44 |
+
[Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155).
|
| 45 |
+
For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
|
| 46 |
+
with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658).
|
| 47 |
+
is_decoder (`bool`, *optional*, defaults to `False`):
|
| 48 |
+
Whether the model is used as a decoder or not. If `False`, the model is used as an encoder.
|
| 49 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 50 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 51 |
+
relevant if `config.is_decoder=True`.
|
| 52 |
+
classifier_dropout (`float`, *optional*):
|
| 53 |
+
The dropout ratio for the classification head.
|
| 54 |
+
Examples:
|
| 55 |
+
```python
|
| 56 |
+
>>> from transformers import XLMRobertaConfig, XLMRobertaModel
|
| 57 |
+
>>> # Initializing a XLM-RoBERTa xlm-roberta-base style configuration
|
| 58 |
+
>>> configuration = XLMRobertaConfig()
|
| 59 |
+
>>> # Initializing a model (with random weights) from the xlm-roberta-base style configuration
|
| 60 |
+
>>> model = XLMRobertaModel(configuration)
|
| 61 |
+
>>> # Accessing the model configuration
|
| 62 |
+
>>> configuration = model.config
|
| 63 |
+
```"""
|
| 64 |
+
model_type = "xlm-roberta"
|
| 65 |
+
|
| 66 |
+
def __init__(
|
| 67 |
+
self,
|
| 68 |
+
vocab_size=250002,
|
| 69 |
+
hidden_size=1024,
|
| 70 |
+
num_hidden_layers=24,
|
| 71 |
+
num_attention_heads=16,
|
| 72 |
+
intermediate_size=4096,
|
| 73 |
+
hidden_act="gelu",
|
| 74 |
+
hidden_dropout_prob=0.1,
|
| 75 |
+
attention_probs_dropout_prob=0.1,
|
| 76 |
+
max_position_embeddings=514,
|
| 77 |
+
type_vocab_size=1,
|
| 78 |
+
initializer_range=0.02,
|
| 79 |
+
layer_norm_eps=1e-05,
|
| 80 |
+
pad_token_id=1,
|
| 81 |
+
bos_token_id=0,
|
| 82 |
+
eos_token_id=2,
|
| 83 |
+
position_embedding_type="absolute",
|
| 84 |
+
use_cache=True,
|
| 85 |
+
classifier_dropout=None,
|
| 86 |
+
num_labels_first=29,
|
| 87 |
+
num_labels_second=2,
|
| 88 |
+
alpha=0.5,
|
| 89 |
+
**kwargs
|
| 90 |
+
):
|
| 91 |
+
super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
| 92 |
+
|
| 93 |
+
self.vocab_size = vocab_size
|
| 94 |
+
self.hidden_size = hidden_size
|
| 95 |
+
self.num_hidden_layers = num_hidden_layers
|
| 96 |
+
self.num_attention_heads = num_attention_heads
|
| 97 |
+
self.hidden_act = hidden_act
|
| 98 |
+
self.intermediate_size = intermediate_size
|
| 99 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
| 100 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
| 101 |
+
self.max_position_embeddings = max_position_embeddings
|
| 102 |
+
self.type_vocab_size = type_vocab_size
|
| 103 |
+
self.initializer_range = initializer_range
|
| 104 |
+
self.layer_norm_eps = layer_norm_eps
|
| 105 |
+
self.position_embedding_type = position_embedding_type
|
| 106 |
+
self.use_cache = use_cache
|
| 107 |
+
self.classifier_dropout = classifier_dropout
|
| 108 |
+
self.num_labels_first = num_labels_first
|
| 109 |
+
self.num_labels_second = num_labels_second
|
| 110 |
+
self.alpha = alpha
|
| 111 |
+
super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
modeling_refseg.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.models.xlm_roberta import XLMRobertaPreTrainedModel, XLMRobertaModel
|
| 2 |
+
from transformers.modeling_outputs import TokenClassifierOutput
|
| 3 |
+
import torch
|
| 4 |
+
from torch import nn
|
| 5 |
+
from torch.nn import CrossEntropyLoss
|
| 6 |
+
from typing import Optional, Tuple, Union
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class XLMRobertaForReferenceSegmentation(XLMRobertaPreTrainedModel):
|
| 10 |
+
_keys_to_ignore_on_load_unexpected = [r"pooler"]
|
| 11 |
+
_keys_to_ignore_on_load_missing = [r"position_ids"]
|
| 12 |
+
|
| 13 |
+
def __init__(self, config):
|
| 14 |
+
super().__init__(config)
|
| 15 |
+
self.num_labels_first = config.num_labels_first
|
| 16 |
+
self.num_labels_second = config.num_labels_second
|
| 17 |
+
self.alpha = config.alpha
|
| 18 |
+
|
| 19 |
+
self.roberta = XLMRobertaModel(config, add_pooling_layer=False)
|
| 20 |
+
classifier_dropout = (
|
| 21 |
+
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
|
| 22 |
+
)
|
| 23 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
| 24 |
+
self.classifier_first = nn.Linear(config.hidden_size, self.num_labels_first)
|
| 25 |
+
self.classifier_second = nn.Linear(config.hidden_size, self.num_labels_second)
|
| 26 |
+
|
| 27 |
+
self.post_init()
|
| 28 |
+
|
| 29 |
+
def forward(
|
| 30 |
+
self,
|
| 31 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 32 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 33 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
| 34 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 35 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
| 36 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 37 |
+
labels_first: Optional[torch.LongTensor] = None,
|
| 38 |
+
labels_second: Optional[torch.LongTensor] = None,
|
| 39 |
+
output_attentions: Optional[bool] = None,
|
| 40 |
+
output_hidden_states: Optional[bool] = None,
|
| 41 |
+
return_dict: Optional[bool] = None,
|
| 42 |
+
) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
|
| 43 |
+
r"""
|
| 44 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
| 45 |
+
Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
|
| 46 |
+
"""
|
| 47 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 48 |
+
|
| 49 |
+
outputs = self.roberta(
|
| 50 |
+
input_ids,
|
| 51 |
+
attention_mask=attention_mask,
|
| 52 |
+
token_type_ids=token_type_ids,
|
| 53 |
+
position_ids=position_ids,
|
| 54 |
+
head_mask=head_mask,
|
| 55 |
+
inputs_embeds=inputs_embeds,
|
| 56 |
+
output_attentions=output_attentions,
|
| 57 |
+
output_hidden_states=output_hidden_states,
|
| 58 |
+
return_dict=return_dict,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
sequence_output = outputs[0]
|
| 62 |
+
|
| 63 |
+
sequence_output_first = self.dropout(sequence_output)
|
| 64 |
+
logits_first = self.classifier_first(sequence_output_first)
|
| 65 |
+
|
| 66 |
+
sequence_output_second = self.dropout(sequence_output)
|
| 67 |
+
logits_second = self.classifier_second(sequence_output_second)
|
| 68 |
+
|
| 69 |
+
loss = None
|
| 70 |
+
if labels_first is not None and labels_second is not None:
|
| 71 |
+
loss_fct_first = CrossEntropyLoss()
|
| 72 |
+
loss_fct_second = CrossEntropyLoss()
|
| 73 |
+
loss_first = loss_fct_first(logits_first.view(-1, self.num_labels_first), labels_first.view(-1))
|
| 74 |
+
loss_second = loss_fct_second(logits_second.view(-1, self.num_labels_second), labels_second.view(-1))
|
| 75 |
+
loss = loss_first + (self.alpha * loss_second)
|
| 76 |
+
|
| 77 |
+
return TokenClassifierOutput(
|
| 78 |
+
loss=loss,
|
| 79 |
+
logits=[logits_first, logits_second],
|
| 80 |
+
hidden_states=outputs.hidden_states,
|
| 81 |
+
attentions=outputs.attentions,
|
| 82 |
+
)
|