Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
88ac60f7
Unverified
Commit
88ac60f7
authored
Apr 26, 2021
by
Hamel Husain
Committed by
GitHub
Apr 26, 2021
Browse files
update QuickTour docs to reflect model output object (#11462)
* update docs to reflect model output object * run make style`
parent
741d48f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
13 deletions
+12
-13
docs/source/main_classes/output.rst
docs/source/main_classes/output.rst
+2
-2
docs/source/quicktour.rst
docs/source/quicktour.rst
+10
-11
No files found.
docs/source/main_classes/output.rst
View file @
88ac60f7
...
@@ -13,8 +13,8 @@
...
@@ -13,8 +13,8 @@
Model
outputs
Model
outputs
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
PyTorch
models
have
outputs
that
are
instances
of
subclasses
of
:
class
:`~
transformers
.
file_utils
.
ModelOutput
`.
Those
All
models
have
outputs
that
are
instances
of
subclasses
of
:
class
:`~
transformers
.
file_utils
.
ModelOutput
`.
Those
are
are
data
structures
containing
all
the
information
returned
by
the
model
,
but
that
can
also
be
used
as
tuples
or
data
structures
containing
all
the
information
returned
by
the
model
,
but
that
can
also
be
used
as
tuples
or
dictionaries
.
dictionaries
.
Let
's see of this looks on an example:
Let
's see of this looks on an example:
...
...
docs/source/quicktour.rst
View file @
88ac60f7
...
@@ -238,23 +238,22 @@ keys directly to tensors, for a PyTorch model, you need to unpack the dictionary
...
@@ -238,23 +238,22 @@ keys directly to tensors, for a PyTorch model, you need to unpack the dictionary
>>> ## TENSORFLOW CODE
>>> ## TENSORFLOW CODE
>>> tf_outputs = tf_model(tf_batch)
>>> tf_outputs = tf_model(tf_batch)
In 🤗 Transformers, all outputs are
tuples (with only one element potentially). Here, we get a tuple with just the final
In 🤗 Transformers, all outputs are
objects that contain the model'
s
final
activations
along
with
other
metadata
.
These
activations of the model.
objects
are
described
in
greater
detail
:
doc
:`
here
<
main_classes
/
output
>`.
For
now
,
let
's inspect the output ourselves:
.. code-block::
.. code-block::
>>> ## PYTORCH CODE
>>> ## PYTORCH CODE
>>> print(pt_outputs)
>>> print(pt_outputs)
(
tensor([[-4.0833, 4.3364],
SequenceClassifierOutput(loss=None, logits=
tensor([[-4.0833, 4.3364],
[ 0.0818, -0.0418]], grad_fn=<AddmmBackward>),)
[ 0.0818, -0.0418]], grad_fn=<AddmmBackward>),
hidden_states=None, attentions=None
)
>>> ## TENSORFLOW CODE
>>> ## TENSORFLOW CODE
>>> print(tf_outputs)
>>> print(tf_outputs)
(
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
TFSequenceClassifierOutput(loss=None, logits=
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-4.0832963 , 4.336414
],
array([[-4.0832963 , 4.336414
3
],
[ 0.0818
1786
, -0.0417
9301
]], dtype=float32)>,)
[ 0.0818
07
, -0.0417
8282
]], dtype=float32)>,
hidden_states=None, attentions=None
)
The model can return more than just the final activations, which is why the output is a tuple. Here we only asked for
Notice how the output object has a ``logits`` attribute. You can use this to access the model'
s
final
activations
.
the final activations, so we get a tuple with one element.
..
note
::
..
note
::
...
@@ -267,10 +266,10 @@ Let's apply the SoftMax activation to get predictions.
...
@@ -267,10 +266,10 @@ Let's apply the SoftMax activation to get predictions.
>>> ## PYTORCH CODE
>>> ## PYTORCH CODE
>>> import torch.nn.functional as F
>>> import torch.nn.functional as F
>>>
pt_predictions
=
F
.
softmax
(
pt_outputs
[
0
]
,
dim
=-
1
)
>>> pt_predictions = F.softmax(pt_outputs
.logits
, dim=-1)
>>> ## TENSORFLOW CODE
>>> ## TENSORFLOW CODE
>>> import tensorflow as tf
>>> import tensorflow as tf
>>>
tf_predictions
=
tf
.
nn
.
softmax
(
tf_outputs
[
0
]
,
axis
=-
1
)
>>> tf.nn.softmax(tf_outputs
.logits
, axis=-1)
We can see we get the numbers from before:
We can see we get the numbers from before:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment