@@ -1782,23 +1782,31 @@ def forward(
1782
1782
1783
1783
Returns:
1784
1784
1785
- Examples :
1785
+ Mask filling example :
1786
1786
1787
1787
```python
1788
- >>> import torch
1789
- >>> from transformers import LongformerForMaskedLM, LongformerTokenizer
1788
+ >>> from transformers import LongformerTokenizer, LongformerForMaskedLM
1790
1789
1791
- >>> model = LongformerForMaskedLM.from_pretrained("allenai/longformer-base-4096")
1792
1790
>>> tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096")
1791
+ >>> model = LongformerForMaskedLM.from_pretrained("allenai/longformer-base-4096")
1792
+ ```
1793
1793
1794
- >>> SAMPLE_TEXT = " ".join(["Hello world! "] * 1000) # long input document
1795
- >>> input_ids = torch.tensor(tokenizer.encode(SAMPLE_TEXT)).unsqueeze(0) # batch of size 1
1794
+ Let's try a very long input.
1796
1795
1797
- >>> attention_mask = None # default is local attention everywhere, which is a good choice for MaskedLM
1798
- >>> # check `LongformerModel.forward` for more details how to set *attention_mask*
1799
- >>> outputs = model(input_ids, attention_mask=attention_mask, labels=input_ids)
1800
- >>> loss = outputs.loss
1801
- >>> prediction_logits = outputs.logits
1796
+ ```python
1797
+ >>> TXT = (
1798
+ ... "My friends are <mask> but they eat too many carbs."
1799
+ ... + " That's why I decide not to eat with them." * 300
1800
+ ... )
1801
+ >>> input_ids = tokenizer([TXT], return_tensors="pt")["input_ids"]
1802
+ >>> logits = model(input_ids).logits
1803
+
1804
+ >>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item()
1805
+ >>> probs = logits[0, masked_index].softmax(dim=0)
1806
+ >>> values, predictions = probs.topk(5)
1807
+
1808
+ >>> tokenizer.decode(predictions).split()
1809
+ ['healthy', 'skinny', 'thin', 'good', 'vegetarian']
1802
1810
```"""
1803
1811
return_dict = return_dict if return_dict is not None else self .config .use_return_dict
1804
1812
@@ -1860,9 +1868,11 @@ def __init__(self, config):
1860
1868
@add_start_docstrings_to_model_forward (LONGFORMER_INPUTS_DOCSTRING .format ("batch_size, sequence_length" ))
1861
1869
@add_code_sample_docstrings (
1862
1870
processor_class = _TOKENIZER_FOR_DOC ,
1863
- checkpoint = _CHECKPOINT_FOR_DOC ,
1871
+ checkpoint = "jpelhaw/longformer-base-plagiarism-detection" ,
1864
1872
output_type = LongformerSequenceClassifierOutput ,
1865
1873
config_class = _CONFIG_FOR_DOC ,
1874
+ expected_output = "'ORIGINAL'" ,
1875
+ expected_loss = 5.44 ,
1866
1876
)
1867
1877
def forward (
1868
1878
self ,
@@ -2127,9 +2137,11 @@ def __init__(self, config):
2127
2137
@add_start_docstrings_to_model_forward (LONGFORMER_INPUTS_DOCSTRING .format ("batch_size, sequence_length" ))
2128
2138
@add_code_sample_docstrings (
2129
2139
processor_class = _TOKENIZER_FOR_DOC ,
2130
- checkpoint = _CHECKPOINT_FOR_DOC ,
2140
+ checkpoint = "brad1141/Longformer-finetuned-norm" ,
2131
2141
output_type = LongformerTokenClassifierOutput ,
2132
2142
config_class = _CONFIG_FOR_DOC ,
2143
+ expected_output = "['Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence', 'Evidence']" ,
2144
+ expected_loss = 0.63 ,
2133
2145
)
2134
2146
def forward (
2135
2147
self ,
0 commit comments