-
Notifications
You must be signed in to change notification settings - Fork 163
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
Conversation
Thanks for your contribution! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
辛苦大佬提交PR
ppsci/arch/base.py
Outdated
>>> 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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 感觉改成2D的Tensor能更好,因为2D更符合实际场景,用paddle.randn([8, 1])这种形状就行
- 输出改成这样:
print(out.dtype, out.shape)
,这样能避免cpu设备运行的问题,也重点展示了数据类型和数据形状。
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
谢谢大佬的建议,我研究一下,如果好用的话我就尽快换上
ppsci/arch/base.py
Outdated
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])} |
There was a problem hiding this comment.
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}")
ppsci/arch/base.py
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个example讲清楚了如何注册,但没有很好地展示transform_fn是如何作用的,所以:
- 可以使用MLP这个类作为model,并且接受"x"和"2x"这两个key作为输入关键字,输出y
- 然后register一个transform_in,它接受原始输入
input = {"x": paddle.randn([4, 1])}
,然后根据原始输入"x",生成"2x"(=2*x), - 运行model(input),然后打印输出的y的类型和形状。
ppsci/arch/base.py
Outdated
>>> 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 函数名改成transform_out
- 可以通过transform_out往输出dict中加入额外的内容,然后将结果打印出来,这样更容易让人理解
Examples: | ||
>>> import ppsci | ||
>>> model = ppsci.arch.Arch() | ||
>>> # freeze all parameters and make model `eval` | ||
>>> model.freeze() |
There was a problem hiding this comment.
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
ppsci/arch/base.py
Outdated
"""Unfreeze all parameters. | ||
|
||
Examples: | ||
>>> import ppsci | ||
>>> model = ppsci.arch.Arch() | ||
>>> # unfreeze all parameters and make model `train` | ||
>>> model.unfreeze() | ||
|
||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
与freeze类似加上验证代码
|
这个有点绕 ~~~ 🤣🤣🤣 帮忙看看现在对不对 ~ 谢谢! |
ppsci/arch/base.py
Outdated
>>> import ppsci | ||
>>> model = ppsci.arch.Arch() | ||
>>> # fetch one tensor | ||
>>> out = model.concat_to_tensor({'x':paddle.to_tensor(123)}, ('x',)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这里的输入建议使用2D+float32 Tensor,比如[8,1]这种,0 DTensor不太有代表性
- 构造输入用
paddle.randn(shape)
更好
ppsci/arch/base.py
Outdated
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]])}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
ppsci/arch/base.py
Outdated
>>> 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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上,类型和形状可以改成二维+flaot32
已修改输入数据为 paddle.rand ~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* [Add] arch examples * [Change] examples * [Change] examples * [Change] register_input_transform * [Change] data with rand --------- Co-authored-by: HydrogenSulfate <[email protected]>
PR types
Others
PR changes
Docs
Describe
补充 ppsci.arch.Arch 中的示例代码
关联 PR #686
@HydrogenSulfate
请评审 ~