提交 b18dbc67 编写于 作者: W Wang Huan

modify doc for Layer, test=develop

上级 09f19532
...@@ -62,10 +62,6 @@ class HookRemoveHelper(object): ...@@ -62,10 +62,6 @@ class HookRemoveHelper(object):
class Layer(core.Layer): class Layer(core.Layer):
""" """
:alias_main: paddle.nn.Layer
:alias: paddle.nn.Layer
:old_api: paddle.fluid.dygraph.layers.Layer
Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on. Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on.
Parameters: Parameters:
...@@ -150,8 +146,6 @@ class Layer(core.Layer): ...@@ -150,8 +146,6 @@ class Layer(core.Layer):
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
paddle.disable_static()
net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
def init_weights(layer): def init_weights(layer):
...@@ -197,7 +191,7 @@ class Layer(core.Layer): ...@@ -197,7 +191,7 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
# the forward_post_hook change the output of the layer: output = output * 2 # the forward_post_hook change the output of the layer: output = output * 2
...@@ -207,14 +201,13 @@ class Layer(core.Layer): ...@@ -207,14 +201,13 @@ class Layer(core.Layer):
# change the output # change the output
return output * 2 return output * 2
with fluid.dygraph.guard(): linear = paddle.nn.Linear(13, 5)
linear = fluid.Linear(13, 5, dtype="float32")
# register the hook # register the hook
forward_post_hook_handle = linear.register_forward_post_hook(forward_post_hook) forward_post_hook_handle = linear.register_forward_post_hook(forward_post_hook)
value1 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(26).reshape(2, 13).astype("float32")
in1 = fluid.dygraph.to_variable(value1) in1 = paddle.to_tensor(value1)
out0 = linear(in1) out0 = linear(in1)
...@@ -249,7 +242,7 @@ class Layer(core.Layer): ...@@ -249,7 +242,7 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
# the forward_post_hook change the input of the layer: input = input * 2 # the forward_post_hook change the input of the layer: input = input * 2
...@@ -260,21 +253,20 @@ class Layer(core.Layer): ...@@ -260,21 +253,20 @@ class Layer(core.Layer):
input_return = (input[0] * 2) input_return = (input[0] * 2)
return input_return return input_return
with fluid.dygraph.guard(): linear = paddle.nn.Linear(13, 5)
linear = fluid.Linear(13, 5, dtype="float32")
# register the hook # register the hook
forward_pre_hook_handle = linear.register_forward_pre_hook(forward_pre_hook) forward_pre_hook_handle = linear.register_forward_pre_hook(forward_pre_hook)
value0 = np.arange(26).reshape(2, 13).astype("float32") value0 = np.arange(26).reshape(2, 13).astype("float32")
in0 = fluid.dygraph.to_variable(value0) in0 = paddle.to_tensor(value0)
out0 = linear(in0) out0 = linear(in0)
# remove the hook # remove the hook
forward_pre_hook_handle.remove() forward_pre_hook_handle.remove()
value1 = value0 * 2 value1 = value0 * 2
in1 = fluid.dygraph.to_variable(value1) in1 = paddle.to_tensor(value1)
out1 = linear(in1) out1 = linear(in1)
# hook change the linear's input to input * 2, so out0 is equal to out1. # hook change the linear's input to input * 2, so out0 is equal to out1.
...@@ -366,12 +358,11 @@ class Layer(core.Layer): ...@@ -366,12 +358,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
layer_list = list(model.children()) layer_list = list(model.children())
...@@ -391,12 +382,11 @@ class Layer(core.Layer): ...@@ -391,12 +382,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
for prefix, layer in model.named_children(): for prefix, layer in model.named_children():
print(prefix, layer) print(prefix, layer)
...@@ -438,12 +428,11 @@ class Layer(core.Layer): ...@@ -438,12 +428,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
for name, param in model.named_parameters(): for name, param in model.named_parameters():
print(name, param) print(name, param)
...@@ -483,12 +472,11 @@ class Layer(core.Layer): ...@@ -483,12 +472,11 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
fc2 = fluid.Linear(3, 10, bias_attr=False) model = paddle.nn.Sequential(fc1, fc2)
model = fluid.dygraph.Sequential(fc1, fc2)
for prefix, layer in model.named_sublayers(): for prefix, layer in model.named_sublayers():
print(prefix, layer) print(prefix, layer)
...@@ -536,12 +524,11 @@ class Layer(core.Layer): ...@@ -536,12 +524,11 @@ class Layer(core.Layer):
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): linear = paddle.nn.Linear(10, 3)
linear = fluid.Linear(10, 3)
value = np.array([0]).astype("float32") value = np.array([0]).astype("float32")
buffer = fluid.dygraph.to_variable(value) buffer = paddle.to_tensor(value)
linear.register_buffer("buf_name", buffer, persistable=True) linear.register_buffer("buf_name", buffer, persistable=True)
# get the buffer by attribute. # get the buffer by attribute.
...@@ -609,21 +596,20 @@ class Layer(core.Layer): ...@@ -609,21 +596,20 @@ class Layer(core.Layer):
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3)
fc1 = fluid.Linear(10, 3) buffer1 = paddle.to_tensor(np.array([0]).astype("float32"))
buffer1 = fluid.dygraph.to_variable(np.array([0]).astype("float32"))
# register a variable as buffer by specific `persistable` # register a variable as buffer by specific `persistable`
fc1.register_buffer("buf_name_1", buffer1, persistable=True) fc1.register_buffer("buf_name_1", buffer1, persistable=True)
fc2 = fluid.Linear(3, 10) fc2 = paddle.nn.Linear(3, 10)
buffer2 = fluid.dygraph.to_variable(np.array([1]).astype("float32")) buffer2 = paddle.to_tensor(np.array([1]).astype("float32"))
# register a buffer by assigning an attribute with Variable. # register a buffer by assigning an attribute with Variable.
# The `persistable` can only be False by this way. # The `persistable` can only be False by this way.
fc2.buf_name_2 = buffer2 fc2.buf_name_2 = buffer2
model = fluid.dygraph.Sequential(fc1, fc2) model = paddle.nn.Sequential(fc1, fc2)
# get all named buffers # get all named buffers
for name, buffer in model.named_buffers(): for name, buffer in model.named_buffers():
...@@ -654,15 +640,14 @@ class Layer(core.Layer): ...@@ -654,15 +640,14 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import numpy as np
with fluid.dygraph.guard():
value = np.arange(26).reshape(2, 13).astype("float32") value = np.arange(26).reshape(2, 13).astype("float32")
a = fluid.dygraph.to_variable(value) a = paddle.to_tensor(value)
linear = fluid.Linear(13, 5, dtype="float32") linear = paddle.nn.Linear(13, 5)
adam = fluid.optimizer.Adam(learning_rate=0.01, adam = paddle.optimizer.Adam(learning_rate=0.01,
parameter_list=linear.parameters()) parameters=linear.parameters())
out = linear(a) out = linear(a)
out.backward() out.backward()
adam.minimize(out) adam.minimize(out)
...@@ -918,12 +903,12 @@ class Layer(core.Layer): ...@@ -918,12 +903,12 @@ class Layer(core.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding([10, 10]) emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict() state_dict = emb.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy") paddle.save( state_dict, "paddle_dy")
''' '''
...@@ -968,15 +953,11 @@ class Layer(core.Layer): ...@@ -968,15 +953,11 @@ class Layer(core.Layer):
import paddle import paddle
paddle.disable_static()
emb = paddle.nn.Embedding(10, 10) emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict() state_dict = emb.state_dict()
paddle.save(state_dict, "paddle_dy.pdparams") paddle.save(state_dict, "paddle_dy.pdparams")
para_state_dict = paddle.load("paddle_dy.pdparams") para_state_dict = paddle.load("paddle_dy.pdparams")
emb.set_state_dict(para_state_dict) emb.set_state_dict(para_state_dict)
''' '''
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册