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

[xdoctest] reformat example code with google style in No.6-No.10 #56146

Merged
merged 9 commits into from
Aug 30, 2023

Conversation

longranger2
Copy link
Contributor

@longranger2 longranger2 commented Aug 10, 2023

PR types

Others

PR changes

Others

Description

修改如下文件的示例代码为新的格式,并通过 xdoctest 检查:

  • python/paddle/reader/init.py
  • python/paddle/reader/decorator.py
  • python/paddle/signal.py
  • python/paddle/regularizer.py
  • python/paddle/sysconfig.py

@sunzhongkai588 @SigureMo @megemini

@paddle-bot
Copy link

paddle-bot bot commented Aug 10, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Aug 10, 2023
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Aug 10, 2023
>>> # All data is cached into memory
>>> cached_reader = paddle.io.cache(reader)

>>> # Output: 0 1 2
Copy link
Member

Choose a reason for hiding this comment

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

已经有输出了,就不需要注释了

>>> # Output: 0 1 2
>>> for i in cached_reader():
... print(i)
0 1 2
Copy link
Member

Choose a reason for hiding this comment

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

这里输出是在一行的?

>>> def reader():
... for i in range(5):
... yield i
>>> shuffled_reader = fluid.io.shuffle(reader, 3)
Copy link
Member

Choose a reason for hiding this comment

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

不要用 fluid API 了,用 paddle.reader.decorator.shuffle

@@ -142,15 +143,16 @@ def shuffle(reader, buf_size):
Examples:
.. code-block:: python

import paddle.fluid as fluid
>>> # doctest: +SKIP('outputs are 0~4 unordered arrangement')
>>> import paddle.fluid as fluid
Copy link
Member

Choose a reason for hiding this comment

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

fluid 相关 import 也要删掉

... for i in range(10):
... yield i
... return reader
>>> reader = fluid.io.compose(reader_creator_10(0), reader_creator_10(0))
Copy link
Member

Choose a reason for hiding this comment

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

这个是否也可以替换成非 fluid API(不一定需要是公开的)

>>> def reader():
... for i in range(100):
... yield i
>>> firstn_reader = fluid.io.firstn(reader, 5)
Copy link
Member

Choose a reason for hiding this comment

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

同上

>>> firstn_reader = fluid.io.firstn(reader, 5)
>>> for e in firstn_reader():
... print(e)
0 1 2 3 4
Copy link
Member

Choose a reason for hiding this comment

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

同上

# [[[9 10]], [[11 12]]]
# [13,14] will be dropped

>>> import paddle
Copy link
Member

Choose a reason for hiding this comment

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

528 和 529 中间需要空一行


np.allclose(x, x_) # True
>>> np.allclose(x, x_) # True
Copy link
Member

Choose a reason for hiding this comment

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

后面注释可以删掉了

Comment on lines 530 to 531
>>> import paddle.fluid as fluid
>>> from paddle.fluid.io import multiprocess_reader
Copy link
Contributor

Choose a reason for hiding this comment

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

参考 @SigureMo 的~ 不要用fluid的api了~

padding=0,
weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)),
bias_attr=False)
>>> # Example2: set Regularizer in parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

上面空一行~

Comment on lines 59 to 63
>>> y0 = frame(x, frame_length=4, hop_length=2, axis=-1)
[[0, 2, 4],
[1, 3, 5],
[2, 4, 6],
[3, 5, 7]]
Copy link
Contributor

Choose a reason for hiding this comment

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

如果要输出的话,最好 print ~ 另外,最好 frame -> signal.frame

>>> import paddle
>>> from paddle import signal
>>> # 1D
>>> x = paddle.arange(8)
>>> y0 = signal.frame(x, frame_length=4, hop_length=2, axis=-1)
>>> print(y0)
Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 2, 4],
 [1, 3, 5],
 [2, 4, 6],
 [3, 5, 7]])

下同 ~


paddle.seed(0)
>>> paddle.seed(0)convert-doctest --debug doctest
Copy link
Contributor

Choose a reason for hiding this comment

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

???

Copy link
Contributor

Choose a reason for hiding this comment

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

这里语法错误了,导致测试跳过~

Copy link
Member

Choose a reason for hiding this comment

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

emmm,这个问题也得记一下,最好有办法检测这个

@longranger2 longranger2 changed the title [xdoctest] reformat example code with google style in No.6-No.10 [WIP] [xdoctest] reformat example code with google style in No.6-No.10 Aug 14, 2023
Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

有个临时生成的文件 sample_file_1 不要加进来 ~

最好不要用 git add .

Comment on lines 48 to 52
>>> def reader_creator_random_image(width, height):
... def reader():
... while True:
... yield numpy.random.uniform(-1, 1, size=width*height)
>>> return reader
Copy link
Contributor

Choose a reason for hiding this comment

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

    >>> def reader_creator_random_image(width, height):
    ...     def reader():
    ...         while True:
    ...             yield numpy.random.uniform(-1, 1, size=width*height)
    >>> return reader

应该在内部返回

    >>> def reader_creator_random_image(width, height):
    ...     def reader():
    ...         while True:
    ...             yield numpy.random.uniform(-1, 1, size=width*height)
    ...     return reader

Comment on lines 58 to 62
>>> def reader_creator_random_image_and_label(width, height, label):
... def reader():
... while True:
... yield numpy.random.uniform(-1, 1, size=width*height), label
>>> return reader
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

@@ -524,59 +528,55 @@ def multiprocess_reader(readers, use_pipe=True, queue_size=1000):
Example:

.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

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

.. code-block:: python 要相对 Examples: 有缩进 ~

Copy link
Member

Choose a reason for hiding this comment

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

python/paddle/reader/decorator.py 下的 multiprocess_reader 里面的 .. code-block:: python 木有缩进 ~ 是我这边没更新?!?!

这里确实没解决呢 @longranger2

Comment on lines 537 to 541
>>> def fake_input_files():
... with open(sample_files[0], 'w') as f:
... np.savez(f, a=np.array([1, 2]), b=np.array([3, 4]), c=np.array([5, 6]), d=np.array([7, 8]))
... with open(sample_files[1], 'w') as f:
... np.savez(f, a=np.array([9, 10]), b=np.array([11, 12]), c=np.array([13, 14]))
Copy link
Contributor

Choose a reason for hiding this comment

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

np.savez 直接传文件名称的字符串,或者用 'wb' ~

Comment on lines 96 to 105
>>> from paddle.regularizer import L2Decay

>>> my_conv2d = Conv2D(
... in_channels=10,
... out_channels=10,
... kernel_size=1,
... stride=1,
... padding=0,
... weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)),
... bias_attr=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是 L1Decay 吧 ~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是 L1Decay 吧 ~

@megemini 这是为什么呢?

Copy link
Member

Choose a reason for hiding this comment

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

@megemini 标 WIP 的可以暂缓 review 吧,我看你 review 了一些 WIP 的 PR

Copy link
Contributor

Choose a reason for hiding this comment

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

@megemini 标 WIP 的可以暂缓 review 吧,我看你 review 了一些 WIP 的 PR

OK

Copy link
Contributor

Choose a reason for hiding this comment

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

这里是 L1Decay 吧 ~

@megemini 这是为什么呢?

这个是在 L1Decay 里面的示例啊 ~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是 L1Decay 吧 ~

@megemini 这是为什么呢?

这个是在 L1Decay 里面的示例啊 ~

哦哦好的,现在才发现[捂脸]

@@ -51,58 +51,52 @@ def frame(x, frame_length, hop_length, axis=-1, name=None):

.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

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

.. code-block:: python 与上面的 Examples: 要有缩进 ~

Comment on lines 57 to 83
>>> # 1D
>>> x = paddle.arange(8)
>>> y0 = signal.frame(x, frame_length=4, hop_length=2, axis=-1)
>>> print(y0)
[[0, 2, 4],
[1, 3, 5],
[2, 4, 6],
[3, 5, 7]]

>>> y1 = signal.frame(x, frame_length=4, hop_length=2, axis=0)
>>> print(y1)
[[0, 1, 2, 3],
[2, 3, 4, 5],
[4, 5, 6, 7]]

>>> # 2D
>>> x0 = paddle.arange(16).reshape([2, 8])
>>> y0 = signal.frame(x0, frame_length=4, hop_length=2, axis=-1)
>>> print(y0)
[[[0, 2, 4],
[1, 3, 5],
[2, 4, 6],
[3, 5, 7]],
[[8 , 10, 12],
[9 , 11, 13],
[10, 12, 14],
[11, 13, 15]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的输出应该是 Tensor 吧~

@@ -169,34 +163,44 @@ def overlap_add(x, hop_length, axis=-1, name=None):

.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

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

.. code-block:: python 与上面的 Examples: 要有缩进 ~

Comment on lines 169 to 192
>>> # 2D
>>> x0 = paddle.arange(16).reshape([8, 2])
>>> print(x0)
[[0 , 1 ],
[2 , 3 ],
[4 , 5 ],
[6 , 7 ],
[8 , 9 ],
[10, 11],
[12, 13],
[14, 15]]

>>> y0 = overlap_add(x0, hop_length=2, axis=-1)
>>> print(y0)
[0 , 2 , 5 , 9 , 13, 17, 21, 25, 13, 15]

>>> x1 = paddle.arange(16).reshape([2, 8])
>>> print(x1)
[[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11, 12, 13, 14, 15]]

>>> y1 = overlap_add(x1, hop_length=2, axis=0)
>>> print(y1)
[0 , 1 , 10, 12, 14, 16, 18, 20, 14, 15]
Copy link
Contributor

Choose a reason for hiding this comment

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

Tensor


>>> x1 = paddle.arange(32).reshape([2, 8, 1, 2])
>>> y1 = overlap_add(x1, hop_length=2, axis=0)
>>> print(y1)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里应该是输出的 y1.shape

@SigureMo SigureMo changed the title [WIP] [xdoctest] reformat example code with google style in No.6-No.10 [waiting for fluid rename][xdoctest] reformat example code with google style in No.6-No.10 Aug 18, 2023
@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Aug 23, 2023

Sorry to inform you that cca196f's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@SigureMo SigureMo changed the title [waiting for fluid rename][xdoctest] reformat example code with google style in No.6-No.10 [xdoctest] reformat example code with google style in No.6-No.10 Aug 24, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

根据群里讨论,无法替代的 fluid 改动暂时不改即可,这个 PR 可以修改推动一下了

sample_file_1 Outdated
Copy link
Member

Choose a reason for hiding this comment

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

这个是?

... def reader():
... while True:
... yield numpy.random.uniform(-1, 1, size=width*height)
>>> return reader
Copy link
Contributor

Choose a reason for hiding this comment

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

这里应该用 ... 而不是 >>>

虽然从代码测试的角度,用 >>> 也能过测,但是,return reader 实际上是 def reader_creator_random_image(width, height): 这个函数的一部分,而不是单独的一句,所以,用 ... 更合适。

... def reader():
... while True:
... yield numpy.random.uniform(-1, 1, size=width*height), label
>>> return reader
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

SigureMo
SigureMo previously approved these changes Aug 29, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

ฅ LGTMeow ฅ

@megemini 确认一下?

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

python/paddle/reader/decorator.py 下的 multiprocess_reader 里面的 .. code-block:: python 木有缩进 ~ 是我这边没更新?!?!

@longranger2
Copy link
Contributor Author

python/paddle/reader/decorator.py 下的 multiprocess_reader 里面的 .. code-block:: python 木有缩进 ~ 是我这边没更新?!?!

done

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

LGTM ~ 辛苦!:)

@luotao1
Copy link
Contributor

luotao1 commented Aug 29, 2023

需要解决下 PR-CI-Codestyle-Check 的问题

@longranger2
Copy link
Contributor Author

需要解决下 PR-CI-Codestyle-Check 的问题

done

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

@luotao1 luotao1 merged commit fc1e505 into PaddlePaddle:develop Aug 30, 2023
24 of 26 checks passed
@longranger2 longranger2 deleted the xdoctest2 branch August 30, 2023 03:03
BeingGod pushed a commit to BeingGod/Paddle that referenced this pull request Sep 9, 2023
…dlePaddle#56146)

* fix sample code

* fix bug

* fix bug

* Update regularizer.py

* Update __init__.py

* Update decorator.py

* fix code-style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants