Unverified Commit 83424ade authored by Sylvain Gugger's avatar Sylvain Gugger Committed by GitHub
Browse files

[Doctest] Setup, quicktour and task_summary (#13078)

* Fix doctests for quicktour

* Adapt causal LM exemple

* Remove space

* Fix until summarization

* End of task summary

* Style

* With last changes in quicktour
parent bfc88509
...@@ -65,7 +65,7 @@ make them readable. For instance: ...@@ -65,7 +65,7 @@ make them readable. For instance:
.. code-block:: .. code-block::
>>> classifier('We are very happy to show you the 🤗 Transformers library.') >>> classifier('We are very happy to show you the 🤗 Transformers library.')
[{'label': 'POSITIVE', 'score': 0.9997795224189758}] [{'label': 'POSITIVE', 'score': 0.99978}]
That's encouraging! You can use it on a list of sentences, which will be preprocessed then fed to the model as a That's encouraging! You can use it on a list of sentences, which will be preprocessed then fed to the model as a
`batch`, returning a list of dictionaries like this one: `batch`, returning a list of dictionaries like this one:
...@@ -195,7 +195,8 @@ sequence: ...@@ -195,7 +195,8 @@ sequence:
.. code-block:: .. code-block::
>>> print(inputs) >>> print(inputs)
{'input_ids': [101, 2057, 2024, 2200, 3407, 2000, 2265, 2017, 1996, 100, 19081, 3075, 1012, 102], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} {'input_ids': [101, 2057, 2024, 2200, 3407, 2000, 2265, 2017, 1996, 100, 19081, 3075, 1012, 102],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
You can pass a list of sentences directly to your tokenizer. If your goal is to send them through your model as a You can pass a list of sentences directly to your tokenizer. If your goal is to send them through your model as a
batch, you probably want to pad them all to the same length, truncate them to the maximum length the model can accept batch, you probably want to pad them all to the same length, truncate them to the maximum length the model can accept
...@@ -264,8 +265,8 @@ objects are described in greater detail :doc:`here <main_classes/output>`. For n ...@@ -264,8 +265,8 @@ objects are described in greater detail :doc:`here <main_classes/output>`. For n
>>> ## TENSORFLOW CODE >>> ## TENSORFLOW CODE
>>> print(tf_outputs) >>> print(tf_outputs)
TFSequenceClassifierOutput(loss=None, logits=<tf.Tensor: shape=(2, 2), dtype=float32, numpy= TFSequenceClassifierOutput(loss=None, logits=<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-4.0832963 , 4.3364143 ], array([[-4.0833 , 4.3364 ],
[ 0.081807 , -0.04178282]], dtype=float32)>, hidden_states=None, attentions=None) [ 0.0818, -0.0418]], dtype=float32)>, hidden_states=None, attentions=None)
Notice how the output object has a ``logits`` attribute. You can use this to access the model's final activations. Notice how the output object has a ``logits`` attribute. You can use this to access the model's final activations.
...@@ -283,7 +284,7 @@ Let's apply the SoftMax activation to get predictions. ...@@ -283,7 +284,7 @@ Let's apply the SoftMax activation to get predictions.
>>> pt_predictions = nn.functional.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_predictions = 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:
...@@ -292,8 +293,8 @@ We can see we get the numbers from before: ...@@ -292,8 +293,8 @@ We can see we get the numbers from before:
>>> ## TENSORFLOW CODE >>> ## TENSORFLOW CODE
>>> print(tf_predictions) >>> print(tf_predictions)
tf.Tensor( tf.Tensor(
[[2.2042994e-04 9.9977952e-01] [[2.2043e-04 9.9978e-01]
[5.3086340e-01 4.6913657e-01]], shape=(2, 2), dtype=float32) [5.3086e-01 4.6914e-01]], shape=(2, 2), dtype=float32)
>>> ## PYTORCH CODE >>> ## PYTORCH CODE
>>> print(pt_predictions) >>> print(pt_predictions)
tensor([[2.2043e-04, 9.9978e-01], tensor([[2.2043e-04, 9.9978e-01],
...@@ -314,9 +315,9 @@ attribute: ...@@ -314,9 +315,9 @@ attribute:
>>> import tensorflow as tf >>> import tensorflow as tf
>>> tf_outputs = tf_model(tf_batch, labels = tf.constant([1, 0])) >>> tf_outputs = tf_model(tf_batch, labels = tf.constant([1, 0]))
>>> print(tf_outputs) >>> print(tf_outputs)
TFSequenceClassifierOutput(loss=<tf.Tensor: shape=(2,), dtype=float32, numpy=array([2.2051287e-04, 6.3326043e-01], dtype=float32)>, logits=<tf.Tensor: shape=(2, 2), dtype=float32, numpy= TFSequenceClassifierOutput(loss=<tf.Tensor: shape=(2,), dtype=float32, numpy=array([2.2051e-04, 6.3326e-01], dtype=float32)>, logits=<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-4.0832963 , 4.3364143 ], array([[-4.0833 , 4.3364 ],
[ 0.081807 , -0.04178282]], dtype=float32)>, hidden_states=None, attentions=None) [ 0.0818, -0.0418]], dtype=float32)>, hidden_states=None, attentions=None)
Models are standard `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__ or `tf.keras.Model Models are standard `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__ or `tf.keras.Model
<https://www.tensorflow.org/api_docs/python/tf/keras/Model>`__ so you can use them in your usual training loop. 🤗 <https://www.tensorflow.org/api_docs/python/tf/keras/Model>`__ so you can use them in your usual training loop. 🤗
......
This diff is collapsed.
...@@ -49,3 +49,6 @@ use_parentheses = True ...@@ -49,3 +49,6 @@ use_parentheses = True
[flake8] [flake8]
ignore = E203, E501, E741, W503, W605 ignore = E203, E501, E741, W503, W605
max-line-length = 119 max-line-length = 119
[tool:pytest]
doctest_optionflags=NUMBER NORMALIZE_WHITESPACE ELLIPSIS
\ No newline at end of file
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