Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainavo committed Jul 13, 2023
1 parent 252486f commit e5de258
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 130 deletions.
13 changes: 1 addition & 12 deletions docs/api/paddle/LazyGuard_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,4 @@ LazyGuard 是一个用于设置模型(继承自 ``paddle.nn.Layer`` ) 中参
代码示例
::::::::::::

.. code-block:: python
from paddle import LazyGuard
from paddle.nn import Linear
with LazyGuard():
# w and b are initialized lazily and have no memory.
net = Linear(10, 10)
for param in net.parameters():
# Initialize param and allocate memory explicitly.
param.initialize()
COPY-FROM: paddle.LazyGuard
25 changes: 3 additions & 22 deletions docs/api/paddle/Tensor_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ shape
查看一个 Tensor 的 shape,shape 是 Tensor 的一个重要的概念,其描述了 tensor 在每个维度上的元素数量。

**代码示例**

.. code-block:: python
import paddle
print("Tensor's shape: ", paddle.to_tensor([[1, 2], [3, 4]]).shape)
# Tensor's shape: [2, 2]
COPY-FROM: paddle.shape

stop_gradient
:::::::::
Expand Down Expand Up @@ -625,14 +620,7 @@ cosh(name=None)
请参考 :ref:`cn_api_fluid_layers_cosh`

**代码示例**
.. code-block:: python
import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.cosh(x)
print(out)
# [1.08107237 1.02006674 1.00500417 1.04533851]
COPY-FROM: paddle.cosh

count_nonzero(axis=None, keepdim=False, name=None)
:::::::::
Expand Down Expand Up @@ -2034,14 +2022,7 @@ sinh(name=None)
对该 Tensor 中逐个元素求双曲正弦。

**代码示例**
.. code-block:: python
import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = x.sinh()
print(out)
# [-0.41075233 -0.201336 0.10016675 0.30452029]
COPY-FROM: paddle.sinh

size()
:::::::::
Expand Down
76 changes: 2 additions & 74 deletions docs/api/paddle/grad_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,8 @@ tuple(Tensor),其长度等于 `inputs` 中的变量个数,且第 i 个返回

代码示例 1
:::::::::
.. code-block:: python
import paddle
def test_dygraph_grad(create_graph):
x = paddle.ones(shape=[1], dtype='float32')
x.stop_gradient = False
y = x * x
# Since y = x * x, dx = 2 * x
dx = paddle.grad(
outputs=[y],
inputs=[x],
create_graph=create_graph,
retain_graph=True)[0]
z = y + dx
# If create_graph = False, the gradient of dx
# would not be backpropagated. Therefore,
# z = x * x + dx, and x.gradient() = 2 * x = 2.0
# If create_graph = True, the gradient of dx
# would be backpropagated. Therefore,
# z = x * x + dx = x * x + 2 * x, and
# x.gradient() = 2 * x + 2 = 4.0
z.backward()
return x.gradient()
print(test_dygraph_grad(create_graph=False)) # [2.]
print(test_dygraph_grad(create_graph=True)) # [4.]
COPY-FROM: paddle.grad:code-example-1

代码示例 2
:::::::::
.. code-block:: python
import paddle
def test_dygraph_grad(grad_outputs=None):
x = paddle.to_tensor(2.0)
x.stop_gradient = False
y1 = x * x
y2 = x * 3
# If grad_outputs=None, dy1 = [1], dy2 = [1].
# If grad_outputs=[g1, g2], then:
# - dy1 = [1] if g1 is None else g1
# - dy2 = [1] if g2 is None else g2
# Since y1 = x * x, dx = 2 * x * dy1.
# Since y2 = x * 3, dx = 3 * dy2.
# Therefore, the final result would be:
# dx = 2 * x * dy1 + 3 * dy2 = 4 * dy1 + 3 * dy2.
dx = paddle.grad(
outputs=[y1, y2],
inputs=[x],
grad_outputs=grad_outputs)[0]
return dx.numpy()
grad_value = paddle.to_tensor(4.0)
# dy1 = [1], dy2 = [1]
print(test_dygraph_grad(None)) # [7.]
# dy1 = [1], dy2 = [4]
print(test_dygraph_grad([None, grad_value])) # [16.]
# dy1 = [4], dy2 = [1]
print(test_dygraph_grad([grad_value, None])) # [19.]
# dy1 = [3], dy2 = [4]
grad_y1 = paddle.to_tensor(3.0)
print(test_dygraph_grad([grad_y1, grad_value])) # [24.]
COPY-FROM: paddle.grad:code-example-2
23 changes: 1 addition & 22 deletions docs/api/paddle/scatter_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,7 @@ scatter
通过基于 ``updates`` 来更新选定索引 ``index`` 上的输入来获得输出。具体行为如下:

.. code-block:: python
import paddle
#input:
x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
# shape of updates should be the same as x
# shape of updates with dim > 1 should be the same as input
updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')
overwrite = False
# calculation:
if not overwrite:
for i in range(len(index)):
x[index[i]] = paddle.zeros([2])
for i in range(len(index)):
if (overwrite):
x[index[i]] = updates[i]
else:
x[index[i]] += updates[i]
# output:
out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]])
out.shape # [3, 2]
COPY-FROM: paddle.scatter:code-example1

**Notice:**
因为 ``updates`` 的应用顺序是不确定的,因此,如果索引 ``index`` 包含重复项,则输出将具有不确定性。
Expand Down

0 comments on commit e5de258

Please sign in to comment.