From 989138378d6de5a0c4bd47dc028209565848f202 Mon Sep 17 00:00:00 2001 From: Xin Pan Date: Mon, 28 Jan 2019 17:39:10 +0800 Subject: [PATCH] add sugar for fetching parameters test=develop --- paddle/fluid/imperative/layer.cc | 2 +- python/paddle/fluid/imperative/layers.py | 53 +++++++++++++------ python/paddle/fluid/imperative/nn.py | 3 -- .../tests/unittests/test_imperative_gan.py | 7 --- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index 47488d4dea7..8f20f0c06e0 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -207,7 +207,7 @@ framework::LoDTensor& VarBase::GradValue() { std::map> OpBase::ApplyGrad() { if (grad_op_descs_.empty() && backward_id_ <= 0) { - LOG(WARNING) << "op with no grad: " << op_desc_->Type(); + VLOG(3) << "op with no grad: " << op_desc_->Type(); return {}; } diff --git a/python/paddle/fluid/imperative/layers.py b/python/paddle/fluid/imperative/layers.py index 71ff95bdea3..2641ec4cddb 100644 --- a/python/paddle/fluid/imperative/layers.py +++ b/python/paddle/fluid/imperative/layers.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import contextlib import sys import numpy as np @@ -30,25 +31,13 @@ class Layer(core.Layer): def __init__(self, dtype=core.VarDesc.VarType.FP32, name=None): self._built = False self._dtype = dtype + self._parameters = collections.OrderedDict() + self._sub_layers = collections.OrderedDict() def parameters(self): - params = [] - for key in self.__dict__.keys(): - value = self.__dict__[key] - if isinstance(value, framework.Parameter): - params.append(value) - elif isinstance(value, core.Layer): - params.extend(value.parameters()) - elif isinstance(value, collections.Container): - if len(value) == 0: - continue - if isinstance(value[0], framework.Parameter): - params.extend(value) - elif isinstance(value[0], core.Layer): - for v in value: - params.extend(v.parameters()) - - return params + """Returns an OrderedDict with parameters from current and sub-layers. + """ + return self._parameters def clear_gradients(self): for p in self.parameters(): @@ -71,6 +60,36 @@ class Layer(core.Layer): def backward(self, *inputs): raise ValueError("Layer shouldn't implement backward") + def __getattr__(self, name): + if name in self._parameters: + return self._parameters[name] + elif name in self._sub_layers: + return self._sub_layers[name] + + def __setattr__(self, name, value): + if isinstance(value, framework.Parameter): + params = self.__dict__.get('_parameters', None) + if params is None: + raise ValueError( + "super(YourLayer, self).__init__() should be called first") + params[name] = value + elif isinstance(value, core.Layer): + layers = self.__dict__.get('_sub_layers', None) + if layers is None: + raise ValueError( + "super(YourLayer, self).__init__() should be called first") + layers[name] = value + else: + object.__setattr__(self, name, value) + + def __delattr__(self, name): + if name in self._parameters: + del self._parameters[name] + elif name in self._sub_layers: + del self._sub_layers[name] + else: + object.__delattr__(self, name) + class PyLayer(core.PyLayer): """Layers composed of user-defined python codes.""" diff --git a/python/paddle/fluid/imperative/nn.py b/python/paddle/fluid/imperative/nn.py index 6c5961cc63d..1b0a60df8bc 100644 --- a/python/paddle/fluid/imperative/nn.py +++ b/python/paddle/fluid/imperative/nn.py @@ -225,9 +225,6 @@ class FC(layers.Layer): act=act, name=name) - def parameters(self): - return [self._w, self._b] - def _build_once(self, input): input_shape = input.shape param_shape = [ diff --git a/python/paddle/fluid/tests/unittests/test_imperative_gan.py b/python/paddle/fluid/tests/unittests/test_imperative_gan.py index 681661bfc63..33c196d1ab5 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_gan.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_gan.py @@ -33,9 +33,6 @@ class Discriminator(fluid.imperative.Layer): self._fc1 = FC(size=32, act='elu', name="d_fc1") self._fc2 = FC(size=1, name="d_fc2") - def parameters(self): - return self._fc1.parameters() + self._fc2.parameters() - def forward(self, inputs): x = self._fc1(inputs) return self._fc2(x) @@ -48,10 +45,6 @@ class Generator(fluid.imperative.Layer): self._fc2 = FC(size=64, act='elu', name="g_fc2") self._fc3 = FC(size=1, name="g_fc3") - def parameters(self): - return self._fc1.parameters() + self._fc2.parameters( - ) + self._fc3.parameters() - def forward(self, inputs): x = self._fc1(inputs) x = self._fc2(x) -- GitLab