config_base.py 2.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Y
Yu Yang 已提交
15
import collections
Y
Yu Yang 已提交
16
import re
Y
Yu Yang 已提交
17
import paddle.trainer_config_helpers as conf_helps
X
xuwei06 已提交
18 19 20 21 22 23

__layer_map__ = {}


def __map_docstr__(doc, name):
    if doc is None:
Y
Yu Yang 已提交
24 25
        return doc

X
xuwei06 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    assert isinstance(doc, basestring)

    # replace LayerOutput to paddle.v2.config_base.Layer
    doc = doc.replace("LayerOutput", "paddle.v2.config_base.Layer")

    doc = doc.replace('ParameterAttribute', 'paddle.v2.attr.ParameterAttribute')

    doc = re.sub(r'ExtraLayerAttribute[^\s]?', 'paddle.v2.attr.ExtraAttribute',
                 doc)

    # xxx_layer to xxx
    doc = re.sub(r"(?P<name>[a-z]+)_layer", r"\g<name>", doc)

    # XxxxActivation to paddle.v2.Activation.Xxxx
    doc = re.sub(r"(?P<name>[A-Z][a-zA-Z]+)Activation",
                 r"paddle.v2.Activation.\g<name>", doc)

    # xxx_evaluator to paddle.v2.evaluator.xxx
    doc = re.sub(r"(?P<name>[a-z]+)_evaluator", r"evaluator.\g<name>", doc)

    # TODO(yuyang18): Add more rules if needed.
    return doc


def __convert_to_v2__(f, name, module):
    def wrapped(*args, **xargs):
        out = f(*args, **xargs)
        outs = out
        if not isinstance(out, collections.Sequence):
            outs = [out]
        for l in outs:
            if isinstance(l, conf_helps.LayerOutput):
                __layer_map__[l.full_name] = l
        return out

    wrapped.__doc__ = __map_docstr__(f.__doc__, name)
    wrapped.__name__ = name
    wrapped.__module__ = module

    return wrapped

Y
Yu Yang 已提交
67

X
xuwei06 已提交
68
Layer = conf_helps.LayerOutput