Unverified Commit b29fd697 authored by Joao Gante's avatar Joao Gante Committed by GitHub
Browse files

MBart: Fix docs and doctests (#22422)

Fix docs and doctests
parent ae5fc2db
...@@ -561,7 +561,7 @@ MBART_GENERATION_EXAMPLE = r""" ...@@ -561,7 +561,7 @@ MBART_GENERATION_EXAMPLE = r"""
>>> inputs = tokenizer(example_english_phrase, return_tensors="pt") >>> inputs = tokenizer(example_english_phrase, return_tensors="pt")
>>> # Translate >>> # Translate
>>> generated_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=5) >>> generated_ids = model.generate(**inputs, num_beams=4, max_length=5)
>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] >>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'42 este răspuns' '42 este răspuns'
``` ```
...@@ -1266,7 +1266,8 @@ class MBartModel(MBartPreTrainedModel): ...@@ -1266,7 +1266,8 @@ class MBartModel(MBartPreTrainedModel):
@add_start_docstrings( @add_start_docstrings(
"The MBART Model with a language modeling head. Can be used for summarization.", MBART_START_DOCSTRING "The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models.",
MBART_START_DOCSTRING,
) )
class MBartForConditionalGeneration(MBartPreTrainedModel): class MBartForConditionalGeneration(MBartPreTrainedModel):
base_model_prefix = "model" base_model_prefix = "model"
......
...@@ -619,37 +619,44 @@ MBART_INPUTS_DOCSTRING = r""" ...@@ -619,37 +619,44 @@ MBART_INPUTS_DOCSTRING = r"""
""" """
MBART_GENERATION_EXAMPLE = r""" MBART_GENERATION_EXAMPLE = r"""
Summarization example: Translation example:
```python ```python
>>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration, MBartConfig >>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration
>>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25") >>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-en-ro")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25") >>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-en-ro")
>>> ARTICLE_TO_SUMMARIZE = "Meine Freunde sind cool, aber sie essen zu viel Kuchen." >>> example_english_phrase = "42 is the answer"
>>> inputs = tokenizer([ARTICLE_TO_SUMMARIZE], max_length=1024, return_tensors="tf") >>> inputs = tokenizer(example_english_phrase, return_tensors="tf")
>>> # Generate Summary >>> # Translate
>>> summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=5) >>> generated_ids = model.generate(**inputs, num_beams=4, max_length=5)
>>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)) >>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'42 este răspuns'
``` ```
Mask filling example: Mask filling example:
```python ```python
>>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration >>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration
>>> import tensorflow as tf
>>> model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25") >>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25") >>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25")
>>> # de_DE is the language symbol id <LID> for German >>> # de_DE is the language symbol id <LID> for German
>>> TXT = "</s> Meine Freunde sind <mask> nett aber sie essen zu viel Kuchen. </s> de_DE" >>> TXT = "</s> Meine Freunde sind <mask> nett aber sie essen zu viel Kuchen. </s> de_DE"
>>> input_ids = tokenizer([TXT], add_special_tokens=False, return_tensors="tf")["input_ids"]
>>> input_ids = tokenizer([TXT], add_special_tokens=False, return_tensors="tf")["input_ids"]
>>> logits = model(input_ids).logits >>> logits = model(input_ids).logits
>>> probs = tf.nn.softmax(logits[0])
>>> # probs[5] is associated with the mask token >>> masked_index = tf.where(input_ids[0] == tokenizer.mask_token_id)[0, 0]
>>> probs = tf.nn.softmax(logits[0, masked_index], axis=0)
>>> values, predictions = tf.math.top_k(probs, 5)
>>> tokenizer.decode(predictions).split()
['nett', 'sehr', 'ganz', 'nicht', 'so']
``` ```
""" """
...@@ -1299,7 +1306,7 @@ class BiasLayer(tf.keras.layers.Layer): ...@@ -1299,7 +1306,7 @@ class BiasLayer(tf.keras.layers.Layer):
@add_start_docstrings( @add_start_docstrings(
"The MBART Model with a language modeling head. Can be used for summarization.", "The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models.",
MBART_START_DOCSTRING, MBART_START_DOCSTRING,
) )
class TFMBartForConditionalGeneration(TFMBartPreTrainedModel, TFCausalLanguageModelingLoss): class TFMBartForConditionalGeneration(TFMBartPreTrainedModel, TFCausalLanguageModelingLoss):
......
...@@ -120,6 +120,7 @@ src/transformers/models/maskformer/configuration_maskformer.py ...@@ -120,6 +120,7 @@ src/transformers/models/maskformer/configuration_maskformer.py
src/transformers/models/maskformer/modeling_maskformer.py src/transformers/models/maskformer/modeling_maskformer.py
src/transformers/models/mbart/configuration_mbart.py src/transformers/models/mbart/configuration_mbart.py
src/transformers/models/mbart/modeling_mbart.py src/transformers/models/mbart/modeling_mbart.py
src/transformers/models/mbart/modeling_tf_mbart.py
src/transformers/models/mctct/configuration_mctct.py src/transformers/models/mctct/configuration_mctct.py
src/transformers/models/megatron_bert/configuration_megatron_bert.py src/transformers/models/megatron_bert/configuration_megatron_bert.py
src/transformers/models/mobilebert/configuration_mobilebert.py src/transformers/models/mobilebert/configuration_mobilebert.py
......
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