Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
9084a034
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
9084a034
编写于
8月 01, 2023
作者:
L
LoneRanger
提交者:
GitHub
8月 01, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style in No.1-No.5 (#55804)
上级
7ce0f9e1
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
469 addition
and
463 deletion
+469
-463
python/paddle/batch.py
python/paddle/batch.py
+13
-14
python/paddle/dataset/image.py
python/paddle/dataset/image.py
+18
-14
python/paddle/fft.py
python/paddle/fft.py
+220
-223
python/paddle/metric/metrics.py
python/paddle/metric/metrics.py
+178
-173
python/paddle/onnx/export.py
python/paddle/onnx/export.py
+40
-39
未找到文件。
python/paddle/batch.py
浏览文件 @
9084a034
...
...
@@ -35,21 +35,20 @@ def batch(reader, batch_size, drop_last=False):
Examples:
.. code-block:: python
import paddle
def reader():
for i in range(10):
yield i
batch_reader = paddle.batch(reader, batch_size=2)
>>>
import paddle
>>>
def reader():
...
for i in range(10):
...
yield i
>>>
batch_reader = paddle.batch(reader, batch_size=2)
for data in batch_reader():
print(data)
# Output is
# [0, 1]
# [2, 3]
# [4, 5]
# [6, 7]
# [8, 9]
>>> for data in batch_reader():
... print(data)
...
[0, 1]
[2, 3]
[4, 5]
[6, 7]
[8, 9]
"""
def
batch_reader
():
...
...
python/paddle/dataset/image.py
浏览文件 @
9084a034
...
...
@@ -123,9 +123,9 @@ def load_image_bytes(bytes, is_color=True):
.. code-block:: python
with open('cat.jpg') as f:
im = load_image_bytes(f.read())
>>>
with open('cat.jpg') as f:
...
im = load_image_bytes(f.read())
...
:param bytes: the input image bytes array.
:type bytes: str
:param is_color: If set is_color True, it will load and
...
...
@@ -148,7 +148,7 @@ def load_image(file, is_color=True):
.. code-block:: python
im = load_image('cat.jpg')
>>>
im = load_image('cat.jpg')
:param file: the input image path.
:type file: string
...
...
@@ -178,8 +178,8 @@ def resize_short(im, size):
.. code-block:: python
im = load_image('cat.jpg')
im = resize_short(im, 256)
>>>
im = load_image('cat.jpg')
>>>
im = resize_short(im, 256)
:param im: the input image with HWC layout.
:type im: ndarray
...
...
@@ -208,9 +208,9 @@ def to_chw(im, order=(2, 0, 1)):
.. code-block:: python
im = load_image('cat.jpg')
im = resize_short(im, 256)
im = to_chw(im)
>>>
im = load_image('cat.jpg')
>>>
im = resize_short(im, 256)
>>>
im = to_chw(im)
:param im: the input image with HWC layout.
:type im: ndarray
...
...
@@ -230,7 +230,8 @@ def center_crop(im, size, is_color=True):
.. code-block:: python
im = center_crop(im, 224)
>>> im = load_image('cat.jpg')
>>> im = center_crop(im, 224)
:param im: the input image with HWC layout.
:type im: ndarray
...
...
@@ -258,7 +259,8 @@ def random_crop(im, size, is_color=True):
.. code-block:: python
im = random_crop(im, 224)
>>> im = load_image('cat.jpg')
>>> im = random_crop(im, 224)
:param im: the input image with HWC layout.
:type im: ndarray
...
...
@@ -287,7 +289,8 @@ def left_right_flip(im, is_color=True):
.. code-block:: python
im = left_right_flip(im)
>>> im = load_image('cat.jpg')
>>> im = left_right_flip(im)
:param im: input image with HWC layout or HW layout for gray image
:type im: ndarray
...
...
@@ -311,7 +314,8 @@ def simple_transform(
.. code-block:: python
im = simple_transform(im, 256, 224, True)
>>> im = load_image('cat.jpg')
>>> im = simple_transform(im, 256, 224, True)
:param im: The input image with HWC layout.
:type im: ndarray
...
...
@@ -365,7 +369,7 @@ def load_and_transform(
.. code-block:: python
im = load_and_transform('cat.jpg', 256, 224, True)
>>>
im = load_and_transform('cat.jpg', 256, 224, True)
:param filename: The file name of input image.
:type filename: string
...
...
python/paddle/fft.py
浏览文件 @
9084a034
此差异已折叠。
点击以展开。
python/paddle/metric/metrics.py
浏览文件 @
9084a034
...
...
@@ -85,13 +85,13 @@ class Metric(metaclass=abc.ABCMeta):
.. 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].
...
...
@@ -100,15 +100,15 @@ class Metric(metaclass=abc.ABCMeta):
.. 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 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
):
...
...
@@ -201,44 +201,45 @@ class Accuracy(Metric):
.. code-block:: python
:name: code-standalone-example
import numpy as np
import paddle
>>>
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]]))
>>>
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
>>> m = paddle.metric.Accuracy()
>>> correct = m.compute(x, y)
>>> m.update(correct)
>>> res = m.accumulate()
>>> print(res)
0.75
.. code-block:: python
:name: code-model-api-example
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)
>>>
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)
"""
...
...
@@ -353,51 +354,52 @@ class Precision(Metric):
.. code-block:: python
:name: code-standalone-example
import numpy as np
import paddle
>>>
import numpy as np
>>>
import paddle
x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([0, 1, 1, 1])
>>>
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
>>> m = paddle.metric.Precision()
>>> m.update(x, y)
>>> res = m.accumulate()
>>> print(res)
1.0
.. code-block:: python
:name: code-model-api-example
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)
>>>
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)
"""
def
__init__
(
self
,
name
=
'precision'
,
*
args
,
**
kwargs
):
...
...
@@ -484,51 +486,52 @@ class Recall(Metric):
.. code-block:: python
:name: code-standalone-example
import numpy as np
import paddle
>>>
import numpy as np
>>>
import paddle
x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([1, 0, 1, 1])
>>>
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
>>> m = paddle.metric.Recall()
>>> m.update(x, y)
>>> res = m.accumulate()
>>> print(res)
0.6666666666666666
.. code-block:: python
:name: code-model-api-example
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)
>>>
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)
"""
def
__init__
(
self
,
name
=
'recall'
,
*
args
,
**
kwargs
):
...
...
@@ -624,56 +627,56 @@ class Auc(Metric):
.. code-block:: python
:name: code-standalone-example
import numpy as np
import paddle
>>>
import numpy as np
>>>
import paddle
m = paddle.metric.Auc()
>>>
m = paddle.metric.Auc()
n = 8
class0_preds = np.random.random(size = (n, 1))
class1_preds = 1 - class0_preds
>>>
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))
>>>
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()
>>>
m.update(preds=preds, labels=labels)
>>>
res = m.accumulate()
.. code-block:: python
:name: code-model-api-example
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)
>>>
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)
"""
def
__init__
(
...
...
@@ -789,12 +792,14 @@ def accuracy(input, label, k=1, correct=None, total=None, name=None):
Examples:
.. code-block:: python
import paddle
>>>
import paddle
predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
label = paddle.to_tensor([[2], [0]], dtype="int64")
result = paddle.metric.accuracy(input=predictions, label=label, k=1)
# 0.5
>>> predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
>>> label = paddle.to_tensor([[2], [0]], dtype="int64")
>>> result = paddle.metric.accuracy(input=predictions, label=label, k=1)
>>> print(result)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.50000000)
"""
if
label
.
dtype
==
paddle
.
int32
:
label
=
paddle
.
cast
(
label
,
paddle
.
int64
)
...
...
python/paddle/onnx/export.py
浏览文件 @
9084a034
...
...
@@ -46,45 +46,46 @@ def export(layer, path, input_spec=None, opset_version=9, **configs):
Examples:
.. code-block:: python
import paddle
class LinearNet(paddle.nn.Layer):
def __init__(self):
super().__init__()
self._linear = paddle.nn.Linear(128, 10)
def forward(self, x):
return self._linear(x)
# Export model with 'InputSpec' to support dynamic input shape.
def export_linear_net():
model = LinearNet()
x_spec = paddle.static.InputSpec(shape=[None, 128], dtype='float32')
paddle.onnx.export(model, 'linear_net', input_spec=[x_spec])
export_linear_net()
class Logic(paddle.nn.Layer):
def __init__(self):
super().__init__()
def forward(self, x, y, z):
if z:
return x
else:
return y
# Export model with 'Tensor' to support pruned model by set 'output_spec'.
def export_logic():
model = Logic()
x = paddle.to_tensor([1])
y = paddle.to_tensor([2])
# Static and run model.
paddle.jit.to_static(model)
out = model(x, y, z=True)
paddle.onnx.export(model, 'pruned', input_spec=[x], output_spec=[out])
export_logic()
>>> import paddle
>>> class LinearNet(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... self._linear = paddle.nn.Linear(128, 10)
...
... def forward(self, x):
... return self._linear(x)
...
>>> # Export model with 'InputSpec' to support dynamic input shape.
>>> def export_linear_net():
... model = LinearNet()
... x_spec = paddle.static.InputSpec(shape=[None, 128], dtype='float32')
... paddle.onnx.export(model, 'linear_net', input_spec=[x_spec])
...
>>> # doctest: +SKIP('raise ImportError')
>>> export_linear_net()
>>> class Logic(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
...
... def forward(self, x, y, z):
... if z:
... return x
... else:
... return y
...
>>> # Export model with 'Tensor' to support pruned model by set 'output_spec'.
>>> def export_logic():
... model = Logic()
... x = paddle.to_tensor([1])
... y = paddle.to_tensor([2])
... # Static and run model.
... paddle.jit.to_static(model)
... out = model(x, y, z=True)
... paddle.onnx.export(model, 'pruned', input_spec=[x], output_spec=[out])
...
>>> export_logic()
"""
p2o
=
try_import
(
'paddle2onnx'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录