From 02910fdad2e4c6b91ad2fd3b4f400d3a67b7c66c Mon Sep 17 00:00:00 2001 From: RedContritio Date: Mon, 26 Jun 2023 09:02:07 +0000 Subject: [PATCH 1/2] update code-block to COPY-FROM --- docs/api/paddle/metric/Accuracy__upper_cn.rst | 46 +++------------- docs/api/paddle/metric/Auc_cn.rst | 53 +------------------ docs/api/paddle/metric/Metric_cn.rst | 21 +------- docs/api/paddle/metric/Precision_cn.rst | 47 +--------------- docs/api/paddle/metric/Recall_cn.rst | 47 +--------------- 5 files changed, 14 insertions(+), 200 deletions(-) diff --git a/docs/api/paddle/metric/Accuracy__upper_cn.rst b/docs/api/paddle/metric/Accuracy__upper_cn.rst index b6a782a49e1..e8f88060ce0 100644 --- a/docs/api/paddle/metric/Accuracy__upper_cn.rst +++ b/docs/api/paddle/metric/Accuracy__upper_cn.rst @@ -16,49 +16,15 @@ Accuracy 代码示例 ::::::::: -**独立使用示例:** +独立使用示例 - .. code-block:: python +COPY-FROM paddle.metric.Accuracy:code-standalone-example - 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) ::::::::: diff --git a/docs/api/paddle/metric/Auc_cn.rst b/docs/api/paddle/metric/Auc_cn.rst index 0cacb41d77c..7f97bae6776 100644 --- a/docs/api/paddle/metric/Auc_cn.rst +++ b/docs/api/paddle/metric/Auc_cn.rst @@ -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 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Metric_cn.rst b/docs/api/paddle/metric/Metric_cn.rst index 16728d334cb..d61cff67659 100644 --- a/docs/api/paddle/metric/Metric_cn.rst +++ b/docs/api/paddle/metric/Metric_cn.rst @@ -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 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Precision_cn.rst b/docs/api/paddle/metric/Precision_cn.rst index c3fc2db1c6f..66a384b0d12 100644 --- a/docs/api/paddle/metric/Precision_cn.rst +++ b/docs/api/paddle/metric/Precision_cn.rst @@ -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 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Recall_cn.rst b/docs/api/paddle/metric/Recall_cn.rst index 13b67e92a28..ea21ebb382a 100644 --- a/docs/api/paddle/metric/Recall_cn.rst +++ b/docs/api/paddle/metric/Recall_cn.rst @@ -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 方法 :::::::::::: From 7f828d9db0bf375bdee6d9c0d9bb4fc4b04334f8 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 5 Jul 2023 14:02:10 +0000 Subject: [PATCH 2/2] fix COPY-FROM format --- docs/api/paddle/metric/Accuracy__upper_cn.rst | 4 ++-- docs/api/paddle/metric/Auc_cn.rst | 4 ++-- docs/api/paddle/metric/Metric_cn.rst | 4 ++-- docs/api/paddle/metric/Precision_cn.rst | 4 ++-- docs/api/paddle/metric/Recall_cn.rst | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/api/paddle/metric/Accuracy__upper_cn.rst b/docs/api/paddle/metric/Accuracy__upper_cn.rst index e8f88060ce0..f7079fc7a5b 100644 --- a/docs/api/paddle/metric/Accuracy__upper_cn.rst +++ b/docs/api/paddle/metric/Accuracy__upper_cn.rst @@ -18,13 +18,13 @@ Accuracy 独立使用示例 -COPY-FROM paddle.metric.Accuracy:code-standalone-example +COPY-FROM: paddle.metric.Accuracy:code-standalone-example 代码示例 2 :::::::::::: 在 Model API 中的示例 -COPY-FROM paddle.metric.Accuracy:code-model-api-example +COPY-FROM: paddle.metric.Accuracy:code-model-api-example compute(pred, label, *args) ::::::::: diff --git a/docs/api/paddle/metric/Auc_cn.rst b/docs/api/paddle/metric/Auc_cn.rst index 7f97bae6776..d5105efb824 100644 --- a/docs/api/paddle/metric/Auc_cn.rst +++ b/docs/api/paddle/metric/Auc_cn.rst @@ -26,14 +26,14 @@ Auc 独立使用示例 -COPY-FROM paddle.metric.Auc:code-standalone-example +COPY-FROM: paddle.metric.Auc:code-standalone-example 代码示例 2 :::::::::::: 在 Model API 中的示例 -COPY-FROM paddle.metric.Auc:code-model-api-example +COPY-FROM: paddle.metric.Auc:code-model-api-example 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Metric_cn.rst b/docs/api/paddle/metric/Metric_cn.rst index d61cff67659..e1648953279 100644 --- a/docs/api/paddle/metric/Metric_cn.rst +++ b/docs/api/paddle/metric/Metric_cn.rst @@ -51,14 +51,14 @@ Python/NumPy,这样可以加速计算。`update` 接口将 `compute` 的输出 可以在 `compute` 中计算每个样本的 top-5 得分,正确预测的矩阵的 shape 是[N, 5]。 -COPY-FROM paddle.metric.Metric:code-compute-example +COPY-FROM: paddle.metric.Metric:code-compute-example 代码示例 2 :::::::::::: 在 `compute` 中的计算,使用内置的算子(可以跑在 GPU 上,使得速度更快)。作为 `update` 的输入,该接口计算如下: -COPY-FROM paddle.metric.Metric:code-update-example +COPY-FROM: paddle.metric.Metric:code-update-example 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Precision_cn.rst b/docs/api/paddle/metric/Precision_cn.rst index 66a384b0d12..8a60f758afa 100644 --- a/docs/api/paddle/metric/Precision_cn.rst +++ b/docs/api/paddle/metric/Precision_cn.rst @@ -24,14 +24,14 @@ Precision 独立使用示例 -COPY-FROM paddle.metric.Precision:code-standalone-example +COPY-FROM: paddle.metric.Precision:code-standalone-example 代码示例 2 :::::::::::: 在 Model API 中的示例 -COPY-FROM paddle.metric.Precision:code-model-api-example +COPY-FROM: paddle.metric.Precision:code-model-api-example 方法 :::::::::::: diff --git a/docs/api/paddle/metric/Recall_cn.rst b/docs/api/paddle/metric/Recall_cn.rst index ea21ebb382a..1b2953825a9 100644 --- a/docs/api/paddle/metric/Recall_cn.rst +++ b/docs/api/paddle/metric/Recall_cn.rst @@ -25,13 +25,13 @@ Recall 独立使用示例 -COPY-FROM paddle.metric.Recall:code-standalone-example +COPY-FROM: paddle.metric.Recall:code-standalone-example 代码示例 2 :::::::::::: 在 Model API 中的示例 -COPY-FROM paddle.metric.Recall:code-model-api-example +COPY-FROM: paddle.metric.Recall:code-model-api-example 方法 ::::::::::::