Skip to content

Commit eb2ead6

Browse files
authored
修改 COPY-FROM No.6 metric (#5971)
* update code-block to COPY-FROM * fix COPY-FROM format
1 parent a60b04a commit eb2ead6

File tree

5 files changed

+14
-200
lines changed

5 files changed

+14
-200
lines changed

docs/api/paddle/metric/Accuracy__upper_cn.rst

+6-40
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,15 @@ Accuracy
1616
代码示例
1717
:::::::::
1818

19-
**独立使用示例:**
19+
独立使用示例
2020

21-
.. code-block:: python
21+
COPY-FROM: paddle.metric.Accuracy:code-standalone-example
2222

23-
import numpy as np
24-
import paddle
25-
x = paddle.to_tensor(np.array([
26-
[0.1, 0.2, 0.3, 0.4],
27-
[0.1, 0.4, 0.3, 0.2],
28-
[0.1, 0.2, 0.4, 0.3],
29-
[0.1, 0.2, 0.3, 0.4]]))
30-
y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
31-
m = paddle.metric.Accuracy()
32-
correct = m.compute(x, y)
33-
m.update(correct)
34-
res = m.accumulate()
35-
print(res) # 0.75
36-
37-
38-
**在 Model API 中的示例**
39-
40-
.. code-block:: python
41-
42-
import paddle
43-
from paddle.static import InputSpec
44-
import paddle.vision.transforms as T
45-
from paddle.vision.datasets import MNIST
46-
47-
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
48-
label = InputSpec([None, 1], 'int64', 'label')
49-
transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
50-
train_dataset = MNIST(mode='train', transform=transform)
51-
52-
model = paddle.Model(paddle.vision.models.LeNet(), input, label)
53-
optim = paddle.optimizer.Adam(
54-
learning_rate=0.001, parameters=model.parameters())
55-
model.prepare(
56-
optim,
57-
loss=paddle.nn.CrossEntropyLoss(),
58-
metrics=paddle.metric.Accuracy())
59-
60-
model.fit(train_dataset, batch_size=64)
23+
代码示例 2
24+
::::::::::::
25+
在 Model API 中的示例
6126

27+
COPY-FROM: paddle.metric.Accuracy:code-model-api-example
6228

6329
compute(pred, label, *args)
6430
:::::::::

docs/api/paddle/metric/Auc_cn.rst

+2-51
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,14 @@ Auc
2626

2727
独立使用示例
2828

29-
.. code-block:: python
30-
31-
import numpy as np
32-
import paddle
33-
34-
m = paddle.metric.Auc()
35-
36-
n = 8
37-
class0_preds = np.random.random(size = (n, 1))
38-
class1_preds = 1 - class0_preds
39-
40-
preds = np.concatenate((class0_preds, class1_preds), axis=1)
41-
labels = np.random.randint(2, size = (n, 1))
42-
43-
m.update(preds=preds, labels=labels)
44-
res = m.accumulate()
29+
COPY-FROM: paddle.metric.Auc:code-standalone-example
4530

4631
代码示例 2
4732
::::::::::::
4833

4934
在 Model API 中的示例
5035

51-
.. code-block:: python
52-
53-
import numpy as np
54-
import paddle
55-
import paddle.nn as nn
56-
57-
class Data(paddle.io.Dataset):
58-
def __init__(self):
59-
super().__init__()
60-
self.n = 1024
61-
self.x = np.random.randn(self.n, 10).astype('float32')
62-
self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')
63-
64-
def __getitem__(self, idx):
65-
return self.x[idx], self.y[idx]
66-
67-
def __len__(self):
68-
return self.n
69-
70-
model = paddle.Model(nn.Sequential(
71-
nn.Linear(10, 2), nn.Softmax())
72-
)
73-
optim = paddle.optimizer.Adam(
74-
learning_rate=0.001, parameters=model.parameters())
75-
76-
def loss(x, y):
77-
return nn.functional.nll_loss(paddle.log(x), y)
78-
79-
model.prepare(
80-
optim,
81-
loss=loss,
82-
metrics=paddle.metric.Auc())
83-
data = Data()
84-
model.fit(data, batch_size=16)
85-
36+
COPY-FROM: paddle.metric.Auc:code-model-api-example
8637

8738
方法
8839
::::::::::::

docs/api/paddle/metric/Metric_cn.rst

+2-19
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,14 @@ Python/NumPy,这样可以加速计算。`update` 接口将 `compute` 的输出
5151
可以在 `compute` 中计算每个样本的 top-5 得分,正确预测的矩阵的 shape 是[N, 5]。
5252

5353

54-
.. code-block:: python
55-
56-
def compute(pred, label):
57-
# sort prediction and slice the top-5 scores
58-
pred = paddle.argsort(pred, descending=True)[:, :5]
59-
# calculate whether the predictions are correct
60-
correct = pred == label
61-
return paddle.cast(correct, dtype='float32')
54+
COPY-FROM: paddle.metric.Metric:code-compute-example
6255

6356
代码示例 2
6457
::::::::::::
6558

6659
在 `compute` 中的计算,使用内置的算子(可以跑在 GPU 上,使得速度更快)。作为 `update` 的输入,该接口计算如下:
6760

68-
.. code-block:: python
69-
70-
def update(self, correct):
71-
accs = []
72-
for i, k in enumerate(self.topk):
73-
num_corrects = correct[:, :k].sum()
74-
num_samples = len(correct)
75-
accs.append(float(num_corrects) / num_samples)
76-
self.total[i] += num_corrects
77-
self.count[i] += num_samples
78-
return accs
61+
COPY-FROM: paddle.metric.Metric:code-update-example
7962

8063
方法
8164
::::::::::::

docs/api/paddle/metric/Precision_cn.rst

+2-45
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,14 @@ Precision
2424

2525
独立使用示例
2626

27-
.. code-block:: python
28-
29-
import numpy as np
30-
import paddle
31-
32-
x = np.array([0.1, 0.5, 0.6, 0.7])
33-
y = np.array([0, 1, 1, 1])
34-
35-
m = paddle.metric.Precision()
36-
m.update(x, y)
37-
res = m.accumulate()
38-
print(res) # 1.0
27+
COPY-FROM: paddle.metric.Precision:code-standalone-example
3928

4029
代码示例 2
4130
::::::::::::
4231

4332
在 Model API 中的示例
4433

45-
.. code-block:: python
46-
47-
import numpy as np
48-
49-
import paddle
50-
import paddle.nn as nn
51-
52-
class Data(paddle.io.Dataset):
53-
def __init__(self):
54-
super().__init__()
55-
self.n = 1024
56-
self.x = np.random.randn(self.n, 10).astype('float32')
57-
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
58-
59-
def __getitem__(self, idx):
60-
return self.x[idx], self.y[idx]
61-
62-
def __len__(self):
63-
return self.n
64-
65-
model = paddle.Model(nn.Sequential(
66-
nn.Linear(10, 1),
67-
nn.Sigmoid()
68-
))
69-
optim = paddle.optimizer.Adam(
70-
learning_rate=0.001, parameters=model.parameters())
71-
model.prepare(
72-
optim,
73-
loss=nn.BCELoss(),
74-
metrics=paddle.metric.Precision())
75-
76-
data = Data()
77-
model.fit(data, batch_size=16)
34+
COPY-FROM: paddle.metric.Precision:code-model-api-example
7835

7936
方法
8037
::::::::::::

docs/api/paddle/metric/Recall_cn.rst

+2-45
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,13 @@ Recall
2525

2626
独立使用示例
2727

28-
.. code-block:: python
29-
30-
import numpy as np
31-
import paddle
32-
33-
x = np.array([0.1, 0.5, 0.6, 0.7])
34-
y = np.array([1, 0, 1, 1])
35-
36-
m = paddle.metric.Recall()
37-
m.update(x, y)
38-
res = m.accumulate()
39-
print(res) # 2.0 / 3.0
28+
COPY-FROM: paddle.metric.Recall:code-standalone-example
4029

4130
代码示例 2
4231
::::::::::::
4332
在 Model API 中的示例
4433

45-
.. code-block:: python
46-
47-
import numpy as np
48-
49-
import paddle
50-
import paddle.nn as nn
51-
52-
class Data(paddle.io.Dataset):
53-
def __init__(self):
54-
super().__init__()
55-
self.n = 1024
56-
self.x = np.random.randn(self.n, 10).astype('float32')
57-
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
58-
59-
def __getitem__(self, idx):
60-
return self.x[idx], self.y[idx]
61-
62-
def __len__(self):
63-
return self.n
64-
65-
model = paddle.Model(nn.Sequential(
66-
nn.Linear(10, 1),
67-
nn.Sigmoid()
68-
))
69-
optim = paddle.optimizer.Adam(
70-
learning_rate=0.001, parameters=model.parameters())
71-
model.prepare(
72-
optim,
73-
loss=nn.BCELoss(),
74-
metrics=[paddle.metric.Precision(), paddle.metric.Recall()])
75-
76-
data = Data()
77-
model.fit(data, batch_size=16)
34+
COPY-FROM: paddle.metric.Recall:code-model-api-example
7835

7936
方法
8037
::::::::::::

0 commit comments

Comments
 (0)