Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doc example #16448

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/transformers/utils/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None):
```python
>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
>>> num_labels = len(model.config.id2label)
>>> model = {model_class}.from_pretrained("{checkpoint}", num_labels=num_labels)
>>> model = {model_class}.from_pretrained(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I think we should revert. Why wouldn't this work?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patrickvonplaten

I had a previous conversation with @NielsRogge on Slack.

He was using celine98/canine-s-finetuned-sst2, which has "problem_type": "single_label_classification", set in the config.

Due to setting, in the following block,

if labels is not None:
if self.config.problem_type is None:
if self.num_labels == 1:
self.config.problem_type = "regression"
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
self.config.problem_type = "single_label_classification"
else:
self.config.problem_type = "multi_label_classification"

the block for the condition self.config.problem_type is None is not run, and it continue to be single_label_classification, therefore the output is not compatible with the provided labels (which is to work with multiple labels here).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Roberta, cardiffnlp/twitter-roberta-base-emotion is used, and the problem_type is not set in the config. Therefore, the model code is able to set it to multi_label_classification.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with "force-passing" single_label_classification as a flag here to overwrite default configs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean multi_label_classification? It's ok to keep this change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is in the reversed direction:

  • celine98/canine-s-finetuned-sst2 has config is set to single_label_classification)

  • but this block in PT_SEQUENCE_CLASSIFICATION_SAMPLE

    ```python
    >>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
    >>> num_labels = len(model.config.id2label)
    >>> model = {model_class}.from_pretrained(
    ... "{checkpoint}", num_labels=num_labels, problem_type="multi_label_classification"
    ... )
    >>> labels = torch.nn.functional.one_hot(torch.tensor([predicted_class_id]), num_classes=num_labels).to(
    ... torch.float
    ... )
    >>> loss = model(**inputs, labels=labels).loss
    >>> loss.backward() # doctest: +IGNORE_RESULT
    ```
    """

    is meant to be multi_label_classification. See labels = torch.nn.functional.one_hot.

  • That's why @NielsRogge needs to add problem_type="multi_label_classification" in the call to from_pretrained.

I will wait @NielsRogge joining this discussion, since he knows better the reason behind his change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, he is faster than my response ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see sorry yes you're right - ok to keep the change for me then!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nono all good this makes perfect sense, I misunderstood here.

... "{checkpoint}", num_labels=num_labels, problem_type="multi_label_classification"
... )

>>> num_labels = len(model.config.id2label)
NielsRogge marked this conversation as resolved.
Show resolved Hide resolved
>>> labels = torch.nn.functional.one_hot(torch.tensor([predicted_class_id]), num_classes=num_labels).to(
... torch.float
... )
Expand Down