-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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][task 335] reformat example code with google style in python/paddle/base/dygraph/tensor_patch_methods.py #57144
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
>>> import paddle | ||
>>> x = paddle.to_tensor(5., stop_gradient=False) | ||
>>> for i in range(5): | ||
... y = paddle.pow(x, 4.0) | ||
... y.backward() | ||
... print("{}: {}".format(i, x.grad)) | ||
0: [500.] | ||
1: [1000.] | ||
2: [1500.] | ||
3: [2000.] | ||
4: [2500.] | ||
|
||
>>> x.clear_grad() | ||
>>> print("{}".format(x.grad)) | ||
0. | ||
|
||
>>> grad_tensor=paddle.to_tensor(2.) | ||
>>> for i in range(5): | ||
... y = paddle.pow(x, 4.0) | ||
... y.backward(grad_tensor) | ||
... print("{}: {}".format(i, x.grad)) | ||
0: [1000.] | ||
1: [2000.] | ||
2: [3000.] | ||
3: [4000.] | ||
4: [5000.] |
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.
输出是 Tensor
>>> y = paddle.pow(x, 4.0) | ||
>>> y.backward() | ||
>>> print("grad of x: {}".format(x.gradient())) | ||
[500.] |
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.
输出不对~
>>> print("w.grad:", w.grad) | ||
w.grad: [1. 2. 3. 4.] | ||
>>> print("x.grad:", x.grad) | ||
x.grad: [ 4. 8. 12. 16.] | ||
>>> print("y.grad:", y.grad) | ||
y.grad: [2. 4. 6. 8.] |
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.
检查一下输出
>>> import paddle | ||
>>> x = paddle.rand([2, 5]) | ||
>>> print(x) | ||
|
||
# Tensor(shape=[2, 5], dtype=float32, place=CPUPlace, | ||
# [[0.30574632, 0.55739117, 0.30902600, 0.39413780, 0.44830436], | ||
# [0.79010487, 0.53972793, 0.09495186, 0.44267157, 0.72112119]]) | ||
Tensor(shape=[2, 5], dtype=float32, place=CPUPlace, | ||
[[0.30574632, 0.55739117, 0.30902600, 0.39413780, 0.44830436], | ||
[0.79010487, 0.53972793, 0.09495186, 0.44267157, 0.72112119]]) |
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.
加 seed ~ 用 cpu place ~
>>> print(x) | ||
Tensor(shape=[], dtype=float32, place=CPUPlace, stop_gradient=True, | ||
2.) | ||
|
||
>>> print(y) | ||
Tensor(shape=[], dtype=float32, place=CPUPlace, stop_gradient=True, | ||
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.
place=Place(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.
LGTMeow 🐾
PR types
Others
PR changes
Others
Description
修改如下文件的示例代码,使其通过
xdoctest
检查:python/paddle/base/dygraph/tensor_patch_methods.py
预览:
Related links
@sunzhongkai588 @SigureMo @megemini