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

修改 COPY-FROM No.6 metric #5971

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 6 additions & 40 deletions docs/api/paddle/metric/Accuracy__upper_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,15 @@ Accuracy
代码示例
:::::::::

**独立使用示例:**
独立使用示例

.. code-block:: python
COPY-FROM paddle.metric.Accuracy:code-standalone-example
RedContritio marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
import paddle
x = paddle.to_tensor(np.array([
[0.1, 0.2, 0.3, 0.4],
[0.1, 0.4, 0.3, 0.2],
[0.1, 0.2, 0.4, 0.3],
[0.1, 0.2, 0.3, 0.4]]))
y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
m = paddle.metric.Accuracy()
correct = m.compute(x, y)
m.update(correct)
res = m.accumulate()
print(res) # 0.75


**在 Model API 中的示例**

.. code-block:: python

import paddle
from paddle.static import InputSpec
import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST

input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
train_dataset = MNIST(mode='train', transform=transform)

model = paddle.Model(paddle.vision.models.LeNet(), input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(
optim,
loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy())

model.fit(train_dataset, batch_size=64)
代码示例 2
::::::::::::
在 Model API 中的示例

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

compute(pred, label, *args)
:::::::::
Expand Down
53 changes: 2 additions & 51 deletions docs/api/paddle/metric/Auc_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,14 @@ Auc

独立使用示例

.. code-block:: python

import numpy as np
import paddle

m = paddle.metric.Auc()

n = 8
class0_preds = np.random.random(size = (n, 1))
class1_preds = 1 - class0_preds

preds = np.concatenate((class0_preds, class1_preds), axis=1)
labels = np.random.randint(2, size = (n, 1))

m.update(preds=preds, labels=labels)
res = m.accumulate()
COPY-FROM paddle.metric.Auc:code-standalone-example

代码示例 2
::::::::::::

在 Model API 中的示例

.. code-block:: python

import numpy as np
import paddle
import paddle.nn as nn

class Data(paddle.io.Dataset):
def __init__(self):
super().__init__()
self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')

def __getitem__(self, idx):
return self.x[idx], self.y[idx]

def __len__(self):
return self.n

model = paddle.Model(nn.Sequential(
nn.Linear(10, 2), nn.Softmax())
)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())

def loss(x, y):
return nn.functional.nll_loss(paddle.log(x), y)

model.prepare(
optim,
loss=loss,
metrics=paddle.metric.Auc())
data = Data()
model.fit(data, batch_size=16)

COPY-FROM paddle.metric.Auc:code-model-api-example

方法
::::::::::::
Expand Down
21 changes: 2 additions & 19 deletions docs/api/paddle/metric/Metric_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,14 @@ Python/NumPy,这样可以加速计算。`update` 接口将 `compute` 的输出
可以在 `compute` 中计算每个样本的 top-5 得分,正确预测的矩阵的 shape 是[N, 5]。


.. code-block:: python

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')
COPY-FROM paddle.metric.Metric:code-compute-example

代码示例 2
::::::::::::

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

.. code-block:: python

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
COPY-FROM paddle.metric.Metric:code-update-example

方法
::::::::::::
Expand Down
47 changes: 2 additions & 45 deletions docs/api/paddle/metric/Precision_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,14 @@ Precision

独立使用示例

.. code-block:: python

import numpy as np
import paddle

x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([0, 1, 1, 1])

m = paddle.metric.Precision()
m.update(x, y)
res = m.accumulate()
print(res) # 1.0
COPY-FROM paddle.metric.Precision:code-standalone-example

代码示例 2
::::::::::::

在 Model API 中的示例

.. code-block:: python

import numpy as np

import paddle
import paddle.nn as nn

class Data(paddle.io.Dataset):
def __init__(self):
super().__init__()
self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')

def __getitem__(self, idx):
return self.x[idx], self.y[idx]

def __len__(self):
return self.n

model = paddle.Model(nn.Sequential(
nn.Linear(10, 1),
nn.Sigmoid()
))
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(
optim,
loss=nn.BCELoss(),
metrics=paddle.metric.Precision())

data = Data()
model.fit(data, batch_size=16)
COPY-FROM paddle.metric.Precision:code-model-api-example

方法
::::::::::::
Expand Down
47 changes: 2 additions & 45 deletions docs/api/paddle/metric/Recall_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,13 @@ Recall

独立使用示例

.. code-block:: python

import numpy as np
import paddle

x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([1, 0, 1, 1])

m = paddle.metric.Recall()
m.update(x, y)
res = m.accumulate()
print(res) # 2.0 / 3.0
COPY-FROM paddle.metric.Recall:code-standalone-example

代码示例 2
::::::::::::
在 Model API 中的示例

.. code-block:: python

import numpy as np

import paddle
import paddle.nn as nn

class Data(paddle.io.Dataset):
def __init__(self):
super().__init__()
self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')

def __getitem__(self, idx):
return self.x[idx], self.y[idx]

def __len__(self):
return self.n

model = paddle.Model(nn.Sequential(
nn.Linear(10, 1),
nn.Sigmoid()
))
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(
optim,
loss=nn.BCELoss(),
metrics=[paddle.metric.Precision(), paddle.metric.Recall()])

data = Data()
model.fit(data, batch_size=16)
COPY-FROM paddle.metric.Recall:code-model-api-example

方法
::::::::::::
Expand Down