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

modify doc for paddle.nn.Layer #27624

Merged
merged 10 commits into from
Oct 8, 2020
Merged

modify doc for paddle.nn.Layer #27624

merged 10 commits into from
Oct 8, 2020

Conversation

wanghuancoder
Copy link
Contributor

@wanghuancoder wanghuancoder commented Sep 27, 2020

PR types

Others

PR changes

Docs

Describe

根据2.0API要求,修改paddle.nn.Layer代码示例
为没有代码示例的函数添加代码示例。forward、backward无实际意义,没有添加代码示例
将构造函数中dtype的默认值改成”float32“

预览图:
image

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@@ -62,10 +62,6 @@ def remove(self):

class Layer(core.Layer):
"""
:alias_main: paddle.nn.Layer
Copy link
Collaborator

Choose a reason for hiding this comment

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

为啥要移除这个alias

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个是2.0API文档中要求的。说会有自动生成工具。这里让删掉。

print(layer_list)
layer_list = list(model.children())

print(layer_list)
Copy link
Collaborator

Choose a reason for hiding this comment

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

最好有一个注释的说明

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

model = fluid.dygraph.Sequential(fc1, fc2)

layer_list = list(model.children())
fc1 = paddle.nn.Linear(10, 3)
Copy link
Collaborator

Choose a reason for hiding this comment

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

变量名称最好叫 linear1 毕竟后面的类名是Linear

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

'alpha': 1,
}
tmp = self.create_variable(name = "linear_tmp_0", dtype=self._dtype)
paddle.fluid.default_main_program().current_block().append_op(
Copy link
Collaborator

Choose a reason for hiding this comment

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

示例中不要使用append op,对于普通用户来说,他们不需要了解这种用法

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已整体修改示例。

self.weight = self.create_parameter(
shape=[in_features, out_features],
is_bias=False)
self.bias = self.create_parameter(
Copy link
Collaborator

Choose a reason for hiding this comment

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

forward中没有使用 bias,bias 可以不写

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已整体修改示例。


Example::
.. code-block:: python

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个train 可以和下面的eval用同样的MyLayer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

inputs = {'X': [input], 'Y': [self.weight]}
attrs = {
'transpose_X': False,
'transpose_Y': False,
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个forward里面可以直接写 paddle.matmul( input, self.self.weight), 不用append op

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已整体修改示例。


import paddle

linear = paddle.nn.Linear(1, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个 add_parameter 建议卸载一个class里面,不建议直接放在class 外面用

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

state_dict = emb.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy")
state_dict = emb.state_dict()
paddle.save( state_dict, "paddle_dy")
Copy link
Collaborator

Choose a reason for hiding this comment

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

"paddle_dy" 建议改成 "paddle_dy.pdparams"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

def forward(self, input):
return self._linear(input)

x = paddle.randn([10, 1], 'float32')
Copy link
Collaborator

Choose a reason for hiding this comment

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

这行好像没有用

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

XiaoguangHu01
XiaoguangHu01 previously approved these changes Sep 29, 2020
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

lanxianghit
lanxianghit previously approved these changes Sep 30, 2020
@@ -77,13 +73,13 @@ class Layer(core.Layer):
dtype(str or core.VarDesc.VarType, optional): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16".
Default: ``core.VarDesc.VarType.FP32``
Default: "float32"
Copy link
Contributor

Choose a reason for hiding this comment

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

73 行,不需要暴露core.VarDesc.Vartype给用户。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!


paddle.disable_static()


Copy link
Contributor

Choose a reason for hiding this comment

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

201行
paddle.full

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -305,6 +361,26 @@ def create_parameter(self,

Returns:
:ref:`api_guide_Variable_en` : created parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

  • 353行::ref:api_fluid_ParamAttr 现在是:ref:api_paddle_ParamAttr
  • 354行: core.VarDesc.VarType 不需要暴露, str写了两次
  • 359行::ref:api_fluid_initializer_XavierInitializer and :ref:api_fluid_initializer_ConstantInitializer 更新成paddle.nn.initializer下的API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -326,11 +402,32 @@ def create_variable(self,
dtype(str or core.VarDesc.VarType, optional): data type of this parameter.
Copy link
Contributor

Choose a reason for hiding this comment

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

  • core.VarDesc.VarType不暴露给用户

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -326,11 +402,32 @@ def create_variable(self,
dtype(str or core.VarDesc.VarType, optional): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16".
If set None, it will be ``core.VarDesc.VarType.FP32``. Default: None
If set None, it will be "float32". Default: None
type(core.VarDesc.VarType, optional): type of the variable. No need to set this parameter. Default: ``core.VarDesc.VarType.LOD_TENSOR``
Copy link
Contributor

Choose a reason for hiding this comment

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

这个参数可以直接从参数列表中去掉吧?
2.0里暂时没有LODTensor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.和红雨商议后,将接口部分的改参数做了删除

@@ -349,6 +446,15 @@ def parameters(self, include_sublayers=True):

Returns:
list of :ref:`api_guide_Variable_en` : a list of Parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

list of :ref:api_guide_Variable_en :
这个建议先删掉。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -536,16 +660,15 @@ def register_buffer(self, name, variable, persistable=True):
.. code-block:: python

import numpy as np
import paddle.fluid as fluid
import paddle
Copy link
Contributor

Choose a reason for hiding this comment

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

641行,应该是non-trainable parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done。同时将所有Variable改成Tensor

@@ -585,6 +708,20 @@ def buffers(self, include_sublayers=True):

Returns:
list of :ref:`api_guide_Variable_en` : a list of buffers.
Copy link
Contributor

Choose a reason for hiding this comment

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

list of :ref:api_guide_Variable_en : 建议去掉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -609,25 +746,24 @@ def named_buffers(self, prefix='', include_sublayers=True):
.. code-block:: python

import numpy as np
import paddle.fluid as fluid
import paddle
Copy link
Contributor

Choose a reason for hiding this comment

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

named_buffer这个API文档里的variable,应该称为tensor 还是 buffer?variable有些不合适。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done。已将所有Variable改成Tensor

parameters=linear.parameters())
out = linear(a)
out.backward()
adam.minimize(out)
Copy link
Contributor

Choose a reason for hiding this comment

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

现在推荐用adam.step()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done,thx!

@@ -198,7 +198,7 @@ def apply(self, fn):
def init_weights(layer):
if type(layer) == nn.Linear:
print('before init weight:', layer.weight.numpy())
new_weight = paddle.fill_constant(layer.weight.shape, layer.weight.dtype, value=0.9)
new_weight = paddle.fill(layer.weight.shape, layer.weight.dtype, value=0.9)
Copy link
Contributor

Choose a reason for hiding this comment

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

paddle里只有paddle.full没有paddle.fill

Copy link
Contributor Author

Choose a reason for hiding this comment

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

抱歉,已修改

If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16". Default: "float32".
is_bias(bool, optional): if this is a bias parameter. Default: False.
default_initializer(Initializer, optional): the default initializer for this parameter.
If set None, default initializer will be set to :ref:`api_fluid_initializer_XavierInitializer` and :ref:`api_fluid_initializer_ConstantInitializer`
If set None, default initializer will be set to :ref:`_api_paddle_fluid_initializer_Xavier` and :ref:`_api_paddle_fluid_initializer_Constant`
Copy link
Contributor

Choose a reason for hiding this comment

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

没改对啊。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

经沟通,暂时使用
paddle.nn.initializer.Constant
paddle.nn.initializer.Xavier

Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@phlrain phlrain merged commit 7698e19 into PaddlePaddle:develop Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants