未验证 提交 b555378e 编写于 作者: Z Zhou Wei 提交者: GitHub

fix Layer.__dir__ to show the parameters/sublayers/buffers/attr/method of the Layer (#25197)

* show the attr and functions of the Layer,test=develop

* add buffer for dir,test=develop

* fix __dir__,test=develop

* fix doc of Layer.__dir__, test=develop
上级 74468bf4
......@@ -752,6 +752,41 @@ class Layer(core.Layer):
else:
object.__delattr__(self, name)
def __dir__(self):
"""
Return a list. Get all parameters, buffers(non-parameter variables), sublayers, method and attr of Layer.
Examples:
import paddle.fluid as fluid
import numpy as np
fluid.dygraph.enable_dygraph()
class Mylayer(fluid.dygraph.Layer):
def __init__(self):
super(Mylayer, self).__init__()
self.linear1 = fluid.dygraph.Linear(10, 10)
self.linear2 = fluid.dygraph.Linear(5, 5)
self.conv2d = fluid.dygraph.Conv2D(3, 2, 3)
self.embedding = fluid.dygraph.Embedding(size=[128, 16])
self.h_0 = fluid.dygraph.to_variable(np.zeros([10, 10]).astype('float32'))
mylayer = Mylayer()
print(dir(mylayer))
# only parts are shown, because of list have too much content
# ['__call__', '__class__', ... , 'conv2d', 'embedding', 'h_0', 'linear1', 'linear2', ... , 'sublayers', 'train']
"""
method = dir(self.__class__)
attrs = list(self.__dict__.keys())
parameters = list(self._parameters.keys())
sublayers = list(self._sub_layers.keys())
buffers = list(self._buffers.keys())
keys = method + attrs + parameters + sublayers + buffers
return keys
def state_dict(self,
destination=None,
include_sublayers=True,
......
......@@ -79,6 +79,41 @@ class TestImperativeNamedParameters(unittest.TestCase):
self.assertListEqual(expected_named_parameters, named_parameters)
def test_dir_layer(self):
with fluid.dygraph.guard():
class Mymodel(fluid.dygraph.Layer):
def __init__(self):
super(Mymodel, self).__init__()
self.linear1 = fluid.dygraph.Linear(10, 10)
self.linear2 = fluid.dygraph.Linear(5, 5)
self.conv2d = fluid.dygraph.Conv2D(3, 2, 3)
self.embedding = fluid.dygraph.Embedding(size=[128, 16])
self.h_0 = fluid.dygraph.to_variable(
np.zeros([10, 10]).astype('float32'))
self.weight = self.create_parameter(
shape=[2, 3],
attr=fluid.ParamAttr(),
dtype="float32",
is_bias=False)
model = Mymodel()
expected_members = dir(model)
self.assertTrue("linear1" in expected_members,
"model should contain Layer: linear1")
self.assertTrue("linear2" in expected_members,
"model should contain Layer: linear2")
self.assertTrue("conv2d" in expected_members,
"model should contain Layer: conv2d")
self.assertTrue("embedding" in expected_members,
"model should contain Layer: embedding")
self.assertTrue("h_0" in expected_members,
"model should contain buffer: h_0")
self.assertTrue("weight" in expected_members,
"model should contain parameter: weight")
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册