mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-04 05:10:06 +06:00
Result of new doc style with fixes (#17015)
* Result of new doc style with fixes * Add last two files * Bump hf-doc-builder
This commit is contained in:
parent
18df440709
commit
7152ed2bae
@ -49,7 +49,7 @@ Usage:
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "This is a long article to summarize", add_special_tokens=False, return_tensors="pt"
|
... "This is a long article to summarize", add_special_tokens=False, return_tensors="pt"
|
||||||
>>> ).input_ids
|
... ).input_ids
|
||||||
>>> labels = tokenizer("This is a short summary", return_tensors="pt").input_ids
|
>>> labels = tokenizer("This is a short summary", return_tensors="pt").input_ids
|
||||||
|
|
||||||
>>> # train...
|
>>> # train...
|
||||||
@ -67,7 +67,7 @@ Usage:
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "This is the first sentence. This is the second sentence.", add_special_tokens=False, return_tensors="pt"
|
... "This is the first sentence. This is the second sentence.", add_special_tokens=False, return_tensors="pt"
|
||||||
>>> ).input_ids
|
... ).input_ids
|
||||||
|
|
||||||
>>> outputs = sentence_fuser.generate(input_ids)
|
>>> outputs = sentence_fuser.generate(input_ids)
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ Example:
|
|||||||
>>> entities = [
|
>>> entities = [
|
||||||
... "Beyoncé",
|
... "Beyoncé",
|
||||||
... "Los Angeles",
|
... "Los Angeles",
|
||||||
>>> ] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
... ] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
||||||
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||||
>>> inputs = tokenizer(text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
>>> inputs = tokenizer(text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
||||||
>>> outputs = model(**inputs)
|
>>> outputs = model(**inputs)
|
||||||
|
2
setup.py
2
setup.py
@ -111,7 +111,7 @@ _deps = [
|
|||||||
"ftfy",
|
"ftfy",
|
||||||
"fugashi>=1.0",
|
"fugashi>=1.0",
|
||||||
"GitPython<3.1.19",
|
"GitPython<3.1.19",
|
||||||
"hf-doc-builder>=0.2.0",
|
"hf-doc-builder>=0.3.0",
|
||||||
"huggingface-hub>=0.1.0,<1.0",
|
"huggingface-hub>=0.1.0,<1.0",
|
||||||
"importlib_metadata",
|
"importlib_metadata",
|
||||||
"ipadic>=1.0.0,<2.0",
|
"ipadic>=1.0.0,<2.0",
|
||||||
|
@ -18,7 +18,7 @@ deps = {
|
|||||||
"ftfy": "ftfy",
|
"ftfy": "ftfy",
|
||||||
"fugashi": "fugashi>=1.0",
|
"fugashi": "fugashi>=1.0",
|
||||||
"GitPython": "GitPython<3.1.19",
|
"GitPython": "GitPython<3.1.19",
|
||||||
"hf-doc-builder": "hf-doc-builder>=0.2.0",
|
"hf-doc-builder": "hf-doc-builder>=0.3.0",
|
||||||
"huggingface-hub": "huggingface-hub>=0.1.0,<1.0",
|
"huggingface-hub": "huggingface-hub>=0.1.0,<1.0",
|
||||||
"importlib_metadata": "importlib_metadata",
|
"importlib_metadata": "importlib_metadata",
|
||||||
"ipadic": "ipadic>=1.0.0,<2.0",
|
"ipadic": "ipadic>=1.0.0,<2.0",
|
||||||
|
@ -457,7 +457,7 @@ class EncoderDecoderModel(PreTrainedModel):
|
|||||||
>>> tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
>>> tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
||||||
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
|
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
|
||||||
... "bert-base-uncased", "bert-base-uncased"
|
... "bert-base-uncased", "bert-base-uncased"
|
||||||
>>> ) # initialize Bert2Bert from pre-trained checkpoints
|
... ) # initialize Bert2Bert from pre-trained checkpoints
|
||||||
|
|
||||||
>>> # training
|
>>> # training
|
||||||
>>> model.config.decoder_start_token_id = tokenizer.cls_token_id
|
>>> model.config.decoder_start_token_id = tokenizer.cls_token_id
|
||||||
|
@ -528,7 +528,7 @@ class TFEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLoss):
|
|||||||
>>> # forward
|
>>> # forward
|
||||||
>>> input_ids = tokenizer.encode(
|
>>> input_ids = tokenizer.encode(
|
||||||
... "Hello, my dog is cute", add_special_tokens=True, return_tensors="tf"
|
... "Hello, my dog is cute", add_special_tokens=True, return_tensors="tf"
|
||||||
>>> ) # Batch size 1
|
... ) # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=input_ids)
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=input_ids)
|
||||||
|
|
||||||
>>> # training
|
>>> # training
|
||||||
|
@ -1061,7 +1061,7 @@ class TFGPT2DoubleHeadsModel(TFGPT2PreTrainedModel):
|
|||||||
|
|
||||||
>>> embedding_layer = model.resize_token_embeddings(
|
>>> embedding_layer = model.resize_token_embeddings(
|
||||||
... len(tokenizer)
|
... len(tokenizer)
|
||||||
>>> ) # Update the model embeddings with the new vocabulary size
|
... ) # Update the model embeddings with the new vocabulary size
|
||||||
|
|
||||||
>>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
|
>>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
|
||||||
>>> encoded_choices = [tokenizer.encode(s) for s in choices]
|
>>> encoded_choices = [tokenizer.encode(s) for s in choices]
|
||||||
|
@ -1000,7 +1000,7 @@ class ImageGPTForCausalImageModeling(ImageGPTPreTrainedModel):
|
|||||||
>>> samples = output[:, 1:].cpu().detach().numpy()
|
>>> samples = output[:, 1:].cpu().detach().numpy()
|
||||||
>>> samples_img = [
|
>>> samples_img = [
|
||||||
... np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples
|
... np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples
|
||||||
>>> ] # convert color cluster tokens back to pixels
|
... ] # convert color cluster tokens back to pixels
|
||||||
>>> f, axes = plt.subplots(1, batch_size, dpi=300)
|
>>> f, axes = plt.subplots(1, batch_size, dpi=300)
|
||||||
|
|
||||||
>>> for img, ax in zip(samples_img, axes):
|
>>> for img, ax in zip(samples_img, axes):
|
||||||
|
@ -1634,10 +1634,10 @@ class LongformerModel(LongformerPreTrainedModel):
|
|||||||
|
|
||||||
>>> attention_mask = torch.ones(
|
>>> attention_mask = torch.ones(
|
||||||
... input_ids.shape, dtype=torch.long, device=input_ids.device
|
... input_ids.shape, dtype=torch.long, device=input_ids.device
|
||||||
>>> ) # initialize to local attention
|
... ) # initialize to local attention
|
||||||
>>> global_attention_mask = torch.zeros(
|
>>> global_attention_mask = torch.zeros(
|
||||||
... input_ids.shape, dtype=torch.long, device=input_ids.device
|
... input_ids.shape, dtype=torch.long, device=input_ids.device
|
||||||
>>> ) # initialize to global attention to be deactivated for all tokens
|
... ) # initialize to global attention to be deactivated for all tokens
|
||||||
>>> global_attention_mask[
|
>>> global_attention_mask[
|
||||||
... :,
|
... :,
|
||||||
... [
|
... [
|
||||||
@ -1645,7 +1645,7 @@ class LongformerModel(LongformerPreTrainedModel):
|
|||||||
... 4,
|
... 4,
|
||||||
... 21,
|
... 21,
|
||||||
... ],
|
... ],
|
||||||
>>> ] = 1 # Set global attention to random tokens for the sake of this example
|
... ] = 1 # Set global attention to random tokens for the sake of this example
|
||||||
>>> # Usually, set global attention based on the task. For example,
|
>>> # Usually, set global attention based on the task. For example,
|
||||||
>>> # classification: the <s> token
|
>>> # classification: the <s> token
|
||||||
>>> # QA: question tokens
|
>>> # QA: question tokens
|
||||||
@ -2025,7 +2025,7 @@ class LongformerForQuestionAnswering(LongformerPreTrainedModel):
|
|||||||
>>> answer_tokens = all_tokens[torch.argmax(start_logits) : torch.argmax(end_logits) + 1]
|
>>> answer_tokens = all_tokens[torch.argmax(start_logits) : torch.argmax(end_logits) + 1]
|
||||||
>>> answer = tokenizer.decode(
|
>>> answer = tokenizer.decode(
|
||||||
... tokenizer.convert_tokens_to_ids(answer_tokens)
|
... tokenizer.convert_tokens_to_ids(answer_tokens)
|
||||||
>>> ) # remove space prepending space token
|
... ) # remove space prepending space token
|
||||||
```"""
|
```"""
|
||||||
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
||||||
|
|
||||||
|
@ -953,11 +953,11 @@ class LukeModel(LukePreTrainedModel):
|
|||||||
>>> entities = [
|
>>> entities = [
|
||||||
... "Beyoncé",
|
... "Beyoncé",
|
||||||
... "Los Angeles",
|
... "Los Angeles",
|
||||||
>>> ] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
... ] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
||||||
>>> entity_spans = [
|
>>> entity_spans = [
|
||||||
... (0, 7),
|
... (0, 7),
|
||||||
... (17, 28),
|
... (17, 28),
|
||||||
>>> ] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
... ] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||||
|
|
||||||
>>> encoding = tokenizer(
|
>>> encoding = tokenizer(
|
||||||
... text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt"
|
... text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt"
|
||||||
@ -1435,7 +1435,7 @@ class LukeForEntityPairClassification(LukePreTrainedModel):
|
|||||||
>>> entity_spans = [
|
>>> entity_spans = [
|
||||||
... (0, 7),
|
... (0, 7),
|
||||||
... (17, 28),
|
... (17, 28),
|
||||||
>>> ] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
... ] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||||
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
|
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
|
||||||
>>> outputs = model(**inputs)
|
>>> outputs = model(**inputs)
|
||||||
>>> logits = outputs.logits
|
>>> logits = outputs.logits
|
||||||
|
@ -674,7 +674,7 @@ class OpenAIGPTDoubleHeadsModel(OpenAIGPTPreTrainedModel):
|
|||||||
>>> model = OpenAIGPTDoubleHeadsModel.from_pretrained("openai-gpt")
|
>>> model = OpenAIGPTDoubleHeadsModel.from_pretrained("openai-gpt")
|
||||||
>>> tokenizer.add_special_tokens(
|
>>> tokenizer.add_special_tokens(
|
||||||
... {"cls_token": "[CLS]"}
|
... {"cls_token": "[CLS]"}
|
||||||
>>> ) # Add a [CLS] to the vocabulary (we should train it also!)
|
... ) # Add a [CLS] to the vocabulary (we should train it also!)
|
||||||
>>> model.resize_token_embeddings(len(tokenizer))
|
>>> model.resize_token_embeddings(len(tokenizer))
|
||||||
|
|
||||||
>>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
|
>>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
|
||||||
|
@ -693,9 +693,9 @@ class TFOpenAIGPTDoubleHeadsModel(TFOpenAIGPTPreTrainedModel):
|
|||||||
>>> inputs = {k: tf.expand_dims(v, 0) for k, v in encoding.items()}
|
>>> inputs = {k: tf.expand_dims(v, 0) for k, v in encoding.items()}
|
||||||
>>> inputs["mc_token_ids"] = tf.constant(
|
>>> inputs["mc_token_ids"] = tf.constant(
|
||||||
... [inputs["input_ids"].shape[-1] - 1, inputs["input_ids"].shape[-1] - 1]
|
... [inputs["input_ids"].shape[-1] - 1, inputs["input_ids"].shape[-1] - 1]
|
||||||
>>> )[
|
... )[
|
||||||
... None, :
|
... None, :
|
||||||
>>> ] # Batch size 1
|
... ] # Batch size 1
|
||||||
>>> outputs = model(inputs)
|
>>> outputs = model(inputs)
|
||||||
>>> lm_prediction_scores, mc_prediction_scores = outputs[:2]
|
>>> lm_prediction_scores, mc_prediction_scores = outputs[:2]
|
||||||
```"""
|
```"""
|
||||||
|
@ -1813,7 +1813,7 @@ class ProphetNetModel(ProphetNetPreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
||||||
|
|
||||||
@ -1935,7 +1935,7 @@ class ProphetNetForConditionalGeneration(ProphetNetPreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
||||||
|
|
||||||
@ -2202,7 +2202,7 @@ class ProphetNetForCausalLM(ProphetNetPreTrainedModel):
|
|||||||
>>> input_ids = tokenizer_enc(ARTICLE, return_tensors="pt").input_ids
|
>>> input_ids = tokenizer_enc(ARTICLE, return_tensors="pt").input_ids
|
||||||
>>> labels = tokenizer_dec(
|
>>> labels = tokenizer_dec(
|
||||||
... "us rejects charges against its ambassador in bolivia", return_tensors="pt"
|
... "us rejects charges against its ambassador in bolivia", return_tensors="pt"
|
||||||
>>> ).input_ids
|
... ).input_ids
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=labels[:, :-1], labels=labels[:, 1:])
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=labels[:, :-1], labels=labels[:, 1:])
|
||||||
|
|
||||||
>>> loss = outputs.loss
|
>>> loss = outputs.loss
|
||||||
|
@ -826,7 +826,7 @@ class RagSequenceForGeneration(RagPreTrainedModel):
|
|||||||
>>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
|
>>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
|
||||||
>>> doc_scores = torch.bmm(
|
>>> doc_scores = torch.bmm(
|
||||||
... question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
|
... question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
|
||||||
>>> ).squeeze(1)
|
... ).squeeze(1)
|
||||||
>>> # 3. Forward to generator
|
>>> # 3. Forward to generator
|
||||||
>>> outputs = model(
|
>>> outputs = model(
|
||||||
... context_input_ids=docs_dict["context_input_ids"],
|
... context_input_ids=docs_dict["context_input_ids"],
|
||||||
@ -1293,7 +1293,7 @@ class RagTokenForGeneration(RagPreTrainedModel):
|
|||||||
>>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
|
>>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
|
||||||
>>> doc_scores = torch.bmm(
|
>>> doc_scores = torch.bmm(
|
||||||
... question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
|
... question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
|
||||||
>>> ).squeeze(1)
|
... ).squeeze(1)
|
||||||
>>> # 3. Forward to generator
|
>>> # 3. Forward to generator
|
||||||
>>> outputs = model(
|
>>> outputs = model(
|
||||||
... context_input_ids=docs_dict["context_input_ids"],
|
... context_input_ids=docs_dict["context_input_ids"],
|
||||||
|
@ -354,7 +354,7 @@ class RagRetriever:
|
|||||||
|
|
||||||
>>> dataset = (
|
>>> dataset = (
|
||||||
... ...
|
... ...
|
||||||
>>> ) # dataset must be a datasets.Datasets object with columns "title", "text" and "embeddings", and it must have a faiss index
|
... ) # dataset must be a datasets.Datasets object with columns "title", "text" and "embeddings", and it must have a faiss index
|
||||||
>>> retriever = RagRetriever.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base", indexed_dataset=dataset)
|
>>> retriever = RagRetriever.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base", indexed_dataset=dataset)
|
||||||
|
|
||||||
>>> # To load your own indexed dataset built with the datasets library that was saved on disk. More info in examples/rag/use_own_knowledge_dataset.py
|
>>> # To load your own indexed dataset built with the datasets library that was saved on disk. More info in examples/rag/use_own_knowledge_dataset.py
|
||||||
|
@ -1782,7 +1782,7 @@ class RealmForOpenQA(RealmPreTrainedModel):
|
|||||||
... add_special_tokens=False,
|
... add_special_tokens=False,
|
||||||
... return_token_type_ids=False,
|
... return_token_type_ids=False,
|
||||||
... return_attention_mask=False,
|
... return_attention_mask=False,
|
||||||
>>> ).input_ids
|
... ).input_ids
|
||||||
|
|
||||||
>>> reader_output, predicted_answer_ids = model(**question_ids, answer_ids=answer_ids, return_dict=False)
|
>>> reader_output, predicted_answer_ids = model(**question_ids, answer_ids=answer_ids, return_dict=False)
|
||||||
>>> predicted_answer = tokenizer.decode(predicted_answer_ids)
|
>>> predicted_answer = tokenizer.decode(predicted_answer_ids)
|
||||||
|
@ -1387,7 +1387,7 @@ class TFSpeech2TextForConditionalGeneration(TFSpeech2TextPreTrainedModel, TFCaus
|
|||||||
|
|
||||||
>>> input_features = processor(
|
>>> input_features = processor(
|
||||||
... ds["speech"][0], sampling_rate=16000, return_tensors="tf"
|
... ds["speech"][0], sampling_rate=16000, return_tensors="tf"
|
||||||
>>> ).input_features # Batch size 1
|
... ).input_features # Batch size 1
|
||||||
>>> generated_ids = model.generate(input_features)
|
>>> generated_ids = model.generate(input_features)
|
||||||
|
|
||||||
>>> transcription = processor.batch_decode(generated_ids)
|
>>> transcription = processor.batch_decode(generated_ids)
|
||||||
|
@ -1344,7 +1344,7 @@ FLAX_T5_MODEL_DOCSTRING = """
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="np"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="np"
|
||||||
>>> ).input_ids
|
... ).input_ids
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="np").input_ids
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="np").input_ids
|
||||||
|
|
||||||
>>> # forward pass
|
>>> # forward pass
|
||||||
|
@ -1375,7 +1375,7 @@ class T5Model(T5PreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
||||||
|
|
||||||
>>> # forward pass
|
>>> # forward pass
|
||||||
@ -1583,7 +1583,7 @@ class T5ForConditionalGeneration(T5PreTrainedModel):
|
|||||||
>>> # inference
|
>>> # inference
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "summarize: studies have shown that owning a dog is good for you", return_tensors="pt"
|
... "summarize: studies have shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> outputs = model.generate(input_ids)
|
>>> outputs = model.generate(input_ids)
|
||||||
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||||
>>> # studies have shown that owning a dog is good for you.
|
>>> # studies have shown that owning a dog is good for you.
|
||||||
@ -1831,7 +1831,7 @@ class T5EncoderModel(T5PreTrainedModel):
|
|||||||
>>> model = T5EncoderModel.from_pretrained("t5-small")
|
>>> model = T5EncoderModel.from_pretrained("t5-small")
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids)
|
>>> outputs = model(input_ids=input_ids)
|
||||||
>>> last_hidden_states = outputs.last_hidden_state
|
>>> last_hidden_states = outputs.last_hidden_state
|
||||||
```"""
|
```"""
|
||||||
|
@ -1165,7 +1165,7 @@ class TFT5Model(TFT5PreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="tf"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="tf"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="tf").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="tf").input_ids # Batch size 1
|
||||||
|
|
||||||
>>> # forward pass
|
>>> # forward pass
|
||||||
@ -1353,7 +1353,7 @@ class TFT5ForConditionalGeneration(TFT5PreTrainedModel, TFCausalLanguageModeling
|
|||||||
>>> # inference
|
>>> # inference
|
||||||
>>> inputs = tokenizer(
|
>>> inputs = tokenizer(
|
||||||
... "summarize: studies have shown that owning a dog is good for you", return_tensors="tf"
|
... "summarize: studies have shown that owning a dog is good for you", return_tensors="tf"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> outputs = model.generate(inputs)
|
>>> outputs = model.generate(inputs)
|
||||||
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||||
>>> # studies have shown that owning a dog is good for you
|
>>> # studies have shown that owning a dog is good for you
|
||||||
@ -1642,7 +1642,7 @@ class TFT5EncoderModel(TFT5PreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="tf"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="tf"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids)
|
>>> outputs = model(input_ids)
|
||||||
```"""
|
```"""
|
||||||
|
|
||||||
|
@ -1068,7 +1068,7 @@ class TapasForMaskedLM(TapasPreTrainedModel):
|
|||||||
... )
|
... )
|
||||||
>>> labels = tokenizer(
|
>>> labels = tokenizer(
|
||||||
... table=table, queries="How many movies has George Clooney played in?", return_tensors="pt"
|
... table=table, queries="How many movies has George Clooney played in?", return_tensors="pt"
|
||||||
>>> )["input_ids"]
|
... )["input_ids"]
|
||||||
|
|
||||||
>>> outputs = model(**inputs, labels=labels)
|
>>> outputs = model(**inputs, labels=labels)
|
||||||
>>> logits = outputs.logits
|
>>> logits = outputs.logits
|
||||||
|
@ -1095,7 +1095,7 @@ class TFTapasForMaskedLM(TFTapasPreTrainedModel, TFMaskedLanguageModelingLoss):
|
|||||||
... )
|
... )
|
||||||
>>> labels = tokenizer(
|
>>> labels = tokenizer(
|
||||||
... table=table, queries="How many movies has George Clooney played in?", return_tensors="tf"
|
... table=table, queries="How many movies has George Clooney played in?", return_tensors="tf"
|
||||||
>>> )["input_ids"]
|
... )["input_ids"]
|
||||||
|
|
||||||
>>> outputs = model(**inputs, labels=labels)
|
>>> outputs = model(**inputs, labels=labels)
|
||||||
>>> logits = outputs.logits
|
>>> logits = outputs.logits
|
||||||
|
@ -326,7 +326,7 @@ class TFVisionEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLos
|
|||||||
|
|
||||||
>>> output_ids = model.generate(
|
>>> output_ids = model.generate(
|
||||||
... pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True
|
... pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True
|
||||||
>>> ).sequences
|
... ).sequences
|
||||||
|
|
||||||
>>> preds = decoder_tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
>>> preds = decoder_tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
||||||
>>> preds = [pred.strip() for pred in preds]
|
>>> preds = [pred.strip() for pred in preds]
|
||||||
|
@ -1081,7 +1081,7 @@ FLAX_WAV2VEC2_MODEL_DOCSTRING = """
|
|||||||
|
|
||||||
>>> input_values = processor(
|
>>> input_values = processor(
|
||||||
... ds["speech"][0], sampling_rate=16_000, return_tensors="np"
|
... ds["speech"][0], sampling_rate=16_000, return_tensors="np"
|
||||||
>>> ).input_values # Batch size 1
|
... ).input_values # Batch size 1
|
||||||
>>> hidden_states = model(input_values).last_hidden_state
|
>>> hidden_states = model(input_values).last_hidden_state
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
@ -1200,7 +1200,7 @@ FLAX_WAV2VEC2_FOR_CTC_DOCSTRING = """
|
|||||||
|
|
||||||
>>> input_values = processor(
|
>>> input_values = processor(
|
||||||
... ds["speech"][0], sampling_rate=16_000, return_tensors="np"
|
... ds["speech"][0], sampling_rate=16_000, return_tensors="np"
|
||||||
>>> ).input_values # Batch size 1
|
... ).input_values # Batch size 1
|
||||||
>>> logits = model(input_values).logits
|
>>> logits = model(input_values).logits
|
||||||
>>> predicted_ids = jnp.argmax(logits, axis=-1)
|
>>> predicted_ids = jnp.argmax(logits, axis=-1)
|
||||||
|
|
||||||
|
@ -1039,7 +1039,7 @@ class XLMForQuestionAnswering(XLMPreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(
|
>>> input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(
|
||||||
... 0
|
... 0
|
||||||
>>> ) # Batch size 1
|
... ) # Batch size 1
|
||||||
>>> start_positions = torch.tensor([1])
|
>>> start_positions = torch.tensor([1])
|
||||||
>>> end_positions = torch.tensor([3])
|
>>> end_positions = torch.tensor([3])
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ class XLMProphetNetModel(ProphetNetModel):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ class XLMProphetNetForConditionalGeneration(ProphetNetForConditionalGeneration):
|
|||||||
|
|
||||||
>>> input_ids = tokenizer(
|
>>> input_ids = tokenizer(
|
||||||
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
|
||||||
>>> ).input_ids # Batch size 1
|
... ).input_ids # Batch size 1
|
||||||
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
|
||||||
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
||||||
|
|
||||||
|
@ -1281,17 +1281,17 @@ class TFXLNetLMHeadModel(TFXLNetPreTrainedModel, TFCausalLanguageModelingLoss):
|
|||||||
>>> # We show how to setup inputs to predict a next token using a bi-directional context.
|
>>> # We show how to setup inputs to predict a next token using a bi-directional context.
|
||||||
>>> input_ids = tf.constant(tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=True))[
|
>>> input_ids = tf.constant(tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=True))[
|
||||||
... None, :
|
... None, :
|
||||||
>>> ] # We will predict the masked token
|
... ] # We will predict the masked token
|
||||||
|
|
||||||
>>> perm_mask = np.zeros((1, input_ids.shape[1], input_ids.shape[1]))
|
>>> perm_mask = np.zeros((1, input_ids.shape[1], input_ids.shape[1]))
|
||||||
>>> perm_mask[:, :, -1] = 1.0 # Previous tokens don't see last token
|
>>> perm_mask[:, :, -1] = 1.0 # Previous tokens don't see last token
|
||||||
|
|
||||||
>>> target_mapping = np.zeros(
|
>>> target_mapping = np.zeros(
|
||||||
... (1, 1, input_ids.shape[1])
|
... (1, 1, input_ids.shape[1])
|
||||||
>>> ) # Shape [1, 1, seq_length] => let's predict one token
|
... ) # Shape [1, 1, seq_length] => let's predict one token
|
||||||
>>> target_mapping[
|
>>> target_mapping[
|
||||||
... 0, 0, -1
|
... 0, 0, -1
|
||||||
>>> ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
... ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
||||||
|
|
||||||
>>> outputs = model(
|
>>> outputs = model(
|
||||||
... input_ids,
|
... input_ids,
|
||||||
@ -1301,7 +1301,7 @@ class TFXLNetLMHeadModel(TFXLNetPreTrainedModel, TFCausalLanguageModelingLoss):
|
|||||||
|
|
||||||
>>> next_token_logits = outputs[
|
>>> next_token_logits = outputs[
|
||||||
... 0
|
... 0
|
||||||
>>> ] # Output has shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
... ] # Output has shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
||||||
```"""
|
```"""
|
||||||
transformer_outputs = self.transformer(
|
transformer_outputs = self.transformer(
|
||||||
input_ids=input_ids,
|
input_ids=input_ids,
|
||||||
|
@ -1400,47 +1400,47 @@ class XLNetLMHeadModel(XLNetPreTrainedModel):
|
|||||||
>>> # We show how to setup inputs to predict a next token using a bi-directional context.
|
>>> # We show how to setup inputs to predict a next token using a bi-directional context.
|
||||||
>>> input_ids = torch.tensor(
|
>>> input_ids = torch.tensor(
|
||||||
... tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=False)
|
... tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=False)
|
||||||
>>> ).unsqueeze(
|
... ).unsqueeze(
|
||||||
... 0
|
... 0
|
||||||
>>> ) # We will predict the masked token
|
... ) # We will predict the masked token
|
||||||
>>> perm_mask = torch.zeros((1, input_ids.shape[1], input_ids.shape[1]), dtype=torch.float)
|
>>> perm_mask = torch.zeros((1, input_ids.shape[1], input_ids.shape[1]), dtype=torch.float)
|
||||||
>>> perm_mask[:, :, -1] = 1.0 # Previous tokens don't see last token
|
>>> perm_mask[:, :, -1] = 1.0 # Previous tokens don't see last token
|
||||||
>>> target_mapping = torch.zeros(
|
>>> target_mapping = torch.zeros(
|
||||||
... (1, 1, input_ids.shape[1]), dtype=torch.float
|
... (1, 1, input_ids.shape[1]), dtype=torch.float
|
||||||
>>> ) # Shape [1, 1, seq_length] => let's predict one token
|
... ) # Shape [1, 1, seq_length] => let's predict one token
|
||||||
>>> target_mapping[
|
>>> target_mapping[
|
||||||
... 0, 0, -1
|
... 0, 0, -1
|
||||||
>>> ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
... ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
||||||
|
|
||||||
>>> outputs = model(input_ids, perm_mask=perm_mask, target_mapping=target_mapping)
|
>>> outputs = model(input_ids, perm_mask=perm_mask, target_mapping=target_mapping)
|
||||||
>>> next_token_logits = outputs[
|
>>> next_token_logits = outputs[
|
||||||
... 0
|
... 0
|
||||||
>>> ] # Output has shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
... ] # Output has shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
||||||
|
|
||||||
>>> # The same way can the XLNetLMHeadModel be used to be trained by standard auto-regressive language modeling.
|
>>> # The same way can the XLNetLMHeadModel be used to be trained by standard auto-regressive language modeling.
|
||||||
>>> input_ids = torch.tensor(
|
>>> input_ids = torch.tensor(
|
||||||
... tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=False)
|
... tokenizer.encode("Hello, my dog is very <mask>", add_special_tokens=False)
|
||||||
>>> ).unsqueeze(
|
... ).unsqueeze(
|
||||||
... 0
|
... 0
|
||||||
>>> ) # We will predict the masked token
|
... ) # We will predict the masked token
|
||||||
>>> labels = torch.tensor(tokenizer.encode("cute", add_special_tokens=False)).unsqueeze(0)
|
>>> labels = torch.tensor(tokenizer.encode("cute", add_special_tokens=False)).unsqueeze(0)
|
||||||
>>> assert labels.shape[0] == 1, "only one word will be predicted"
|
>>> assert labels.shape[0] == 1, "only one word will be predicted"
|
||||||
>>> perm_mask = torch.zeros((1, input_ids.shape[1], input_ids.shape[1]), dtype=torch.float)
|
>>> perm_mask = torch.zeros((1, input_ids.shape[1], input_ids.shape[1]), dtype=torch.float)
|
||||||
>>> perm_mask[
|
>>> perm_mask[
|
||||||
... :, :, -1
|
... :, :, -1
|
||||||
>>> ] = 1.0 # Previous tokens don't see last token as is done in standard auto-regressive lm training
|
... ] = 1.0 # Previous tokens don't see last token as is done in standard auto-regressive lm training
|
||||||
>>> target_mapping = torch.zeros(
|
>>> target_mapping = torch.zeros(
|
||||||
... (1, 1, input_ids.shape[1]), dtype=torch.float
|
... (1, 1, input_ids.shape[1]), dtype=torch.float
|
||||||
>>> ) # Shape [1, 1, seq_length] => let's predict one token
|
... ) # Shape [1, 1, seq_length] => let's predict one token
|
||||||
>>> target_mapping[
|
>>> target_mapping[
|
||||||
... 0, 0, -1
|
... 0, 0, -1
|
||||||
>>> ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
... ] = 1.0 # Our first (and only) prediction will be the last token of the sequence (the masked token)
|
||||||
|
|
||||||
>>> outputs = model(input_ids, perm_mask=perm_mask, target_mapping=target_mapping, labels=labels)
|
>>> outputs = model(input_ids, perm_mask=perm_mask, target_mapping=target_mapping, labels=labels)
|
||||||
>>> loss = outputs.loss
|
>>> loss = outputs.loss
|
||||||
>>> next_token_logits = (
|
>>> next_token_logits = (
|
||||||
... outputs.logits
|
... outputs.logits
|
||||||
>>> ) # Logits have shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
... ) # Logits have shape [target_mapping.size(0), target_mapping.size(1), config.vocab_size]
|
||||||
```"""
|
```"""
|
||||||
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
||||||
|
|
||||||
@ -1980,7 +1980,7 @@ class XLNetForQuestionAnswering(XLNetPreTrainedModel):
|
|||||||
|
|
||||||
>>> input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(
|
>>> input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(
|
||||||
... 0
|
... 0
|
||||||
>>> ) # Batch size 1
|
... ) # Batch size 1
|
||||||
>>> start_positions = torch.tensor([1])
|
>>> start_positions = torch.tensor([1])
|
||||||
>>> end_positions = torch.tensor([3])
|
>>> end_positions = torch.tensor([3])
|
||||||
>>> outputs = model(input_ids, start_positions=start_positions, end_positions=end_positions)
|
>>> outputs = model(input_ids, start_positions=start_positions, end_positions=end_positions)
|
||||||
|
Loading…
Reference in New Issue
Block a user