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

【PPSCI Doc No.12、13、14、15、16、17】ppsci.arch.Arch #752

Merged
merged 6 commits into from
Jan 16, 2024

Conversation

megemini
Copy link
Contributor

PR types

Others

PR changes

Docs

Describe

补充 ppsci.arch.Arch 中的示例代码

关联 PR #686

@HydrogenSulfate

请评审 ~

Copy link

paddle-bot bot commented Jan 15, 2024

Thanks for your contribution!

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

辛苦大佬提交PR

Comment on lines 77 to 92
>>> import paddle
>>> import ppsci
>>> model = ppsci.arch.Arch()
>>> # fetch one tensor
>>> out = model.concat_to_tensor({'x':paddle.to_tensor(123)}, ('x',))
>>> print(out)
Tensor(shape=[], dtype=int64, place=Place(gpu:0), stop_gradient=True,
123)
>>> # fetch more tensors
>>> out = model.concat_to_tensor(
... {'x1':paddle.to_tensor([123]), 'x2':paddle.to_tensor([234])},
... ('x1', 'x2'),
... axis=0)
>>> print(out)
Tensor(shape=[2], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[123, 234])
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 感觉改成2D的Tensor能更好,因为2D更符合实际场景,用paddle.randn([8, 1])这种形状就行
  2. 输出改成这样:print(out.dtype, out.shape),这样能避免cpu设备运行的问题,也重点展示了数据类型和数据形状。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个 pydoctest 不太好用 ... ... 可以试试 xdoctest,或者用 paddle 前段时间的 doctester 来验证示例代码会比较好 ~

单纯的提个建议 ~~~ 😅

https://github.com/PaddlePaddle/Paddle/blob/develop/tools/sampcd_processor.py

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个 pydoctest 不太好用 ... ... 可以试试 xdoctest,或者用 paddle 前段时间的 doctester 来验证示例代码会比较好 ~

单纯的提个建议 ~~~ 😅

https://github.com/PaddlePaddle/Paddle/blob/develop/tools/sampcd_processor.py

谢谢大佬的建议,我研究一下,如果好用的话我就尽快换上

Comment on lines 113 to 127
Examples:
>>> import paddle
>>> import ppsci
>>> model = ppsci.arch.Arch()
>>> # split one tensor
>>> out = model.split_to_dict(paddle.to_tensor(123), ('x',))
>>> print(out)
{'x': Tensor(shape=[], dtype=int64, place=Place(gpu:0), stop_gradient=True,
123)}
>>> # split more tensors
>>> out = model.split_to_dict(paddle.to_tensor([123, 234]), ('x1', 'x2'), axis=0)
>>> print(out)
{'x1': Tensor(shape=[1], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[123]), 'x2': Tensor(shape=[1], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[234])}
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上,并且输出代码可以改成

for k, v in out.items():
    print(f"{k} {v.dtype} {v.shape}")

Comment on lines 145 to 153
Examples:
>>> import ppsci
>>> def transform_fn(in_):
... x = in_["x"]
... x = 2.0 * x
... input_trans = {"x": x}
... return input_trans
>>> model = ppsci.arch.Arch()
>>> model.register_input_transform(transform_fn)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个example讲清楚了如何注册,但没有很好地展示transform_fn是如何作用的,所以:

  1. 可以使用MLP这个类作为model,并且接受"x"和"2x"这两个key作为输入关键字,输出y
  2. 然后register一个transform_in,它接受原始输入input = {"x": paddle.randn([4, 1])},然后根据原始输入"x",生成"2x"(=2*x),
  3. 运行model(input),然后打印输出的y的类型和形状。

Comment on lines 173 to 181
>>> import ppsci
>>> def transform_fn(in_, out):
... x = in_["x"]
... y = out["y"]
... u = 2.0 * x * y
... output_trans = {"u": u}
... return output_trans
>>> model = ppsci.arch.Arch()
>>> model.register_output_transform(transform_fn)
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 函数名改成transform_out
  2. 可以通过transform_out往输出dict中加入额外的内容,然后将结果打印出来,这样更容易让人理解

Comment on lines +189 to +193
Examples:
>>> import ppsci
>>> model = ppsci.arch.Arch()
>>> # freeze all parameters and make model `eval`
>>> model.freeze()
Copy link
Collaborator

Choose a reason for hiding this comment

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

末尾加上如下验证代码:

assert p.training is False

for p in model.parameters():
    assert p.stop_gradient is True

Comment on lines 202 to 210
"""Unfreeze all parameters.

Examples:
>>> import ppsci
>>> model = ppsci.arch.Arch()
>>> # unfreeze all parameters and make model `train`
>>> model.unfreeze()

"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

与freeze类似加上验证代码

@megemini
Copy link
Contributor Author

@HydrogenSulfate

  • 已修改输出为 out.dtype, out.shape 样式
  • 已修改 register_input_transform/register_output_transform,不过 register_input_transform 从输出貌似看不出啥不一样 ~ 😅

@HydrogenSulfate
Copy link
Collaborator

@HydrogenSulfate

  • 已修改输出为 out.dtype, out.shape 样式
  • 已修改 register_input_transform/register_output_transform,不过 register_input_transform 从输出貌似看不出啥不一样 ~ 😅

image

x这个字典会经过transform_in进行处理,所以通过以下方式体现transform_in具备“对x进行修改”这一能力
image

@megemini
Copy link
Contributor Author

x这个字典会经过transform_in进行处理,所以通过以下方式体现transform_in具备“对x进行修改”这一能力 !

这个有点绕 ~~~ 🤣🤣🤣

帮忙看看现在对不对 ~

谢谢!

@HydrogenSulfate

>>> import ppsci
>>> model = ppsci.arch.Arch()
>>> # fetch one tensor
>>> out = model.concat_to_tensor({'x':paddle.to_tensor(123)}, ('x',))
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 这里的输入建议使用2D+float32 Tensor,比如[8,1]这种,0 DTensor不太有代表性
  2. 构造输入用paddle.randn(shape)更好

paddle.int64 []
>>> # fetch more tensors
>>> out = model.concat_to_tensor(
... {'x1':paddle.to_tensor([[1, 2], [2, 3]]), 'x2':paddle.to_tensor([[3, 4], [4, 5]])},
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上

Comment on lines 116 to 125
>>> out = model.split_to_dict(paddle.to_tensor(123), ('x',))
>>> for k, v in out.items():
... print(f"{k} {v.dtype} {v.shape}")
x paddle.int64 []
>>> # split more tensors
>>> out = model.split_to_dict(paddle.to_tensor([[1, 2], [2, 3]]), ('x1', 'x2'), axis=0)
>>> for k, v in out.items():
... print(f"{k} {v.dtype} {v.shape}")
x1 paddle.int64 [1, 2]
x2 paddle.int64 [1, 2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上,类型和形状可以改成二维+flaot32

@megemini
Copy link
Contributor Author

已修改输入数据为 paddle.rand ~

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate 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 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Jan 16, 2024
@HydrogenSulfate HydrogenSulfate merged commit 4f191f7 into PaddlePaddle:develop Jan 16, 2024
3 checks passed
huohuohuohuohuo123 pushed a commit to huohuohuohuohuo123/PaddleScience that referenced this pull request Aug 12, 2024
* [Add] arch examples

* [Change] examples

* [Change] examples

* [Change] register_input_transform

* [Change] data with rand

---------

Co-authored-by: HydrogenSulfate <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants