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]fix cleandoc with a first blank line #55052

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
69 changes: 32 additions & 37 deletions python/paddle/metric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,33 @@ class Metric(metaclass=abc.ABCMeta):
prediction of each sample like follows, while the correct prediction
matrix shape is [N, 5].

.. code-block:: text
.. code-block:: python
:name: code-compute-example

def compute(pred, label):
# sort prediction and slice the top-5 scores
pred = paddle.argsort(pred, descending=True)[:, :5]
# calculate whether the predictions are correct
correct = pred == label
return paddle.cast(correct, dtype='float32')
def compute(pred, label):
# sort prediction and slice the top-5 scores
pred = paddle.argsort(pred, descending=True)[:, :5]
# calculate whether the predictions are correct
correct = pred == label
return paddle.cast(correct, dtype='float32')

With the :code:`compute`, we split some calculations to OPs (which
may run on GPU devices, will be faster), and only fetch 1 tensor with
shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1].
:code:`update` can be define as follows:

.. code-block:: text

def update(self, correct):
accs = []
for i, k in enumerate(self.topk):
num_corrects = correct[:, :k].sum()
num_samples = len(correct)
accs.append(float(num_corrects) / num_samples)
self.total[i] += num_corrects
self.count[i] += num_samples
return accs
.. code-block:: python
:name: code-update-example

def update(self, correct):
accs = []
for i, k in enumerate(self.topk):
num_corrects = correct[:, :k].sum()
num_samples = len(correct)
accs.append(float(num_corrects) / num_samples)
self.total[i] += num_corrects
self.count[i] += num_samples
return accs
"""

def __init__(self):
Expand Down Expand Up @@ -195,9 +197,9 @@ class Accuracy(Metric):
name (str, optional): String name of the metric instance. Default
is `acc`.

Example by standalone:

Examples:
.. code-block:: python
:name: code-standalone-example
Copy link
Member

Choose a reason for hiding this comment

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

好了, 看看行不~

嗯嗯,没什么问题,不过这些代码块最好和上面一样统一修改成四缩进吧~


import numpy as np
import paddle
Expand All @@ -215,10 +217,8 @@ class Accuracy(Metric):
res = m.accumulate()
print(res) # 0.75


Example with Model API:

.. code-block:: python
:name: code-model-api-example

import paddle
from paddle.static import InputSpec
Expand Down Expand Up @@ -349,9 +349,9 @@ class Precision(Metric):
name (str, optional): String name of the metric instance.
Default is `precision`.

Example by standalone:

Examples:
.. code-block:: python
:name: code-standalone-example

import numpy as np
import paddle
Expand All @@ -364,10 +364,8 @@ class Precision(Metric):
res = m.accumulate()
print(res) # 1.0


Example with Model API:

.. code-block:: python
:name: code-model-api-example

import numpy as np

Expand Down Expand Up @@ -482,9 +480,9 @@ class Recall(Metric):
name (str, optional): String name of the metric instance.
Default is `recall`.

Example by standalone:

Examples:
.. code-block:: python
:name: code-standalone-example

import numpy as np
import paddle
Expand All @@ -497,10 +495,8 @@ class Recall(Metric):
res = m.accumulate()
print(res) # 2.0 / 3.0


Example with Model API:

.. code-block:: python
:name: code-model-api-example

import numpy as np

Expand Down Expand Up @@ -624,8 +620,9 @@ class Auc(Metric):

"NOTE: only implement the ROC curve type via Python now."

Example by standalone:
Examples:
.. code-block:: python
:name: code-standalone-example

import numpy as np
import paddle
Expand All @@ -642,10 +639,8 @@ class Auc(Metric):
m.update(preds=preds, labels=labels)
res = m.accumulate()


Example with Model API:

.. code-block:: python
:name: code-model-api-example

import numpy as np
import paddle
Expand Down
2 changes: 1 addition & 1 deletion tools/sampcd_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _append_code_block():
# nonlocal code_blocks, cb_cur, cb_cur_name, cb_cur_seq_id, cb_required
code_blocks.append(
{
'codes': inspect.cleandoc("\n".join(cb_info['cb_cur'])),
'codes': inspect.cleandoc("\n" + "\n".join(cb_info['cb_cur'])),
'name': cb_info['cb_cur_name'],
'id': cb_info['cb_cur_seq_id'],
'required': cb_info['cb_required'],
Expand Down