consistent nn. and nn.functional: part 5 docs (#12161)
This commit is contained in:
@@ -518,7 +518,7 @@ PyTorch, called ``SimpleModel`` as follows:
|
|||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
import torch.nn as nn
|
from torch import nn
|
||||||
|
|
||||||
class SimpleModel(nn.Module):
|
class SimpleModel(nn.Module):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ classification:
|
|||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import torch
|
from torch import nn
|
||||||
from transformers import Trainer
|
from transformers import Trainer
|
||||||
|
|
||||||
class MultilabelTrainer(Trainer):
|
class MultilabelTrainer(Trainer):
|
||||||
@@ -67,7 +67,7 @@ classification:
|
|||||||
labels = inputs.pop("labels")
|
labels = inputs.pop("labels")
|
||||||
outputs = model(**inputs)
|
outputs = model(**inputs)
|
||||||
logits = outputs.logits
|
logits = outputs.logits
|
||||||
loss_fct = torch.nn.BCEWithLogitsLoss()
|
loss_fct = nn.BCEWithLogitsLoss()
|
||||||
loss = loss_fct(logits.view(-1, self.model.config.num_labels),
|
loss = loss_fct(logits.view(-1, self.model.config.num_labels),
|
||||||
labels.float().view(-1, self.model.config.num_labels))
|
labels.float().view(-1, self.model.config.num_labels))
|
||||||
return (loss, outputs) if return_outputs else loss
|
return (loss, outputs) if return_outputs else loss
|
||||||
|
|||||||
@@ -265,8 +265,8 @@ Let's apply the SoftMax activation to get predictions.
|
|||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
>>> ## PYTORCH CODE
|
>>> ## PYTORCH CODE
|
||||||
>>> import torch.nn.functional as F
|
>>> from torch import nn
|
||||||
>>> pt_predictions = F.softmax(pt_outputs.logits, dim=-1)
|
>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1)
|
||||||
>>> ## TENSORFLOW CODE
|
>>> ## TENSORFLOW CODE
|
||||||
>>> import tensorflow as tf
|
>>> import tensorflow as tf
|
||||||
>>> tf.nn.softmax(tf_outputs.logits, axis=-1)
|
>>> tf.nn.softmax(tf_outputs.logits, axis=-1)
|
||||||
|
|||||||
@@ -451,7 +451,7 @@ of tokens.
|
|||||||
>>> ## PYTORCH CODE
|
>>> ## PYTORCH CODE
|
||||||
>>> from transformers import AutoModelWithLMHead, AutoTokenizer, top_k_top_p_filtering
|
>>> from transformers import AutoModelWithLMHead, AutoTokenizer, top_k_top_p_filtering
|
||||||
>>> import torch
|
>>> import torch
|
||||||
>>> from torch.nn import functional as F
|
>>> from torch import nn
|
||||||
|
|
||||||
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
||||||
>>> model = AutoModelWithLMHead.from_pretrained("gpt2")
|
>>> model = AutoModelWithLMHead.from_pretrained("gpt2")
|
||||||
@@ -467,7 +467,7 @@ of tokens.
|
|||||||
>>> filtered_next_token_logits = top_k_top_p_filtering(next_token_logits, top_k=50, top_p=1.0)
|
>>> filtered_next_token_logits = top_k_top_p_filtering(next_token_logits, top_k=50, top_p=1.0)
|
||||||
|
|
||||||
>>> # sample
|
>>> # sample
|
||||||
>>> probs = F.softmax(filtered_next_token_logits, dim=-1)
|
>>> probs = nn.functional.softmax(filtered_next_token_logits, dim=-1)
|
||||||
>>> next_token = torch.multinomial(probs, num_samples=1)
|
>>> next_token = torch.multinomial(probs, num_samples=1)
|
||||||
|
|
||||||
>>> generated = torch.cat([input_ids, next_token], dim=-1)
|
>>> generated = torch.cat([input_ids, next_token], dim=-1)
|
||||||
|
|||||||
Reference in New Issue
Block a user