|
|
|
@ -71,7 +71,11 @@ import collections
|
|
|
|
|
import paddle.trainer_config_helpers as conf_helps
|
|
|
|
|
from paddle.trainer_config_helpers.config_parser_utils import \
|
|
|
|
|
parse_network_config as __parse__
|
|
|
|
|
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_name_default
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_act_default
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_bias_attr_default
|
|
|
|
|
from paddle.trainer_config_helpers.layers import layer_support
|
|
|
|
|
|
|
|
|
|
import data_type
|
|
|
|
|
import activation
|
|
|
|
@ -84,6 +88,13 @@ __all__ = [
|
|
|
|
|
'sum_cost', 'huber_cost'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
__projection_names__ = filter(lambda x: x.endswith('_projection'),
|
|
|
|
|
dir(conf_helps))
|
|
|
|
|
__all__ += __projection_names__
|
|
|
|
|
|
|
|
|
|
__operator_names__ = filter(lambda x: x.endswith('_operator'), dir(conf_helps))
|
|
|
|
|
__all__ += __operator_names__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_network(*outputs):
|
|
|
|
|
"""
|
|
|
|
@ -101,9 +112,8 @@ def parse_network(*outputs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Layer(object):
|
|
|
|
|
def __init__(self, name, parent_layers):
|
|
|
|
|
def __init__(self, name=None, parent_layers=None):
|
|
|
|
|
assert isinstance(parent_layers, dict)
|
|
|
|
|
assert isinstance(name, basestring)
|
|
|
|
|
self.name = name
|
|
|
|
|
self.__parent_layers__ = parent_layers
|
|
|
|
|
|
|
|
|
@ -122,22 +132,25 @@ class Layer(object):
|
|
|
|
|
self.__parent_layers__[layer_name])
|
|
|
|
|
kwargs[layer_name] = v1_layer
|
|
|
|
|
|
|
|
|
|
if self.name not in context:
|
|
|
|
|
if self.name is None:
|
|
|
|
|
return self.to_proto_impl(**kwargs)
|
|
|
|
|
elif self.name not in context:
|
|
|
|
|
context[self.name] = self.to_proto_impl(**kwargs)
|
|
|
|
|
|
|
|
|
|
return context[self.name]
|
|
|
|
|
|
|
|
|
|
def to_proto_impl(self, **kwargs):
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __convert_to_v2__(method_name, name_prefix, parent_names):
|
|
|
|
|
def __convert_to_v2__(method_name, name_prefix=None, parent_names=None):
|
|
|
|
|
if name_prefix is not None:
|
|
|
|
|
wrapper = wrap_name_default(name_prefix=name_prefix)
|
|
|
|
|
else:
|
|
|
|
|
wrapper = None
|
|
|
|
|
|
|
|
|
|
class V2LayerImpl(Layer):
|
|
|
|
|
def __init__(self, name=None, **kwargs):
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
parent_layers = dict()
|
|
|
|
|
other_kwargs = dict()
|
|
|
|
|
for pname in parent_names:
|
|
|
|
@ -148,6 +161,7 @@ def __convert_to_v2__(method_name, name_prefix, parent_names):
|
|
|
|
|
if key not in parent_names:
|
|
|
|
|
other_kwargs[key] = kwargs[key]
|
|
|
|
|
|
|
|
|
|
name = kwargs.get('name', None)
|
|
|
|
|
super(V2LayerImpl, self).__init__(name, parent_layers)
|
|
|
|
|
self.__other_kwargs__ = other_kwargs
|
|
|
|
|
|
|
|
|
@ -160,7 +174,7 @@ def __convert_to_v2__(method_name, name_prefix, parent_names):
|
|
|
|
|
args[each] = kwargs[each]
|
|
|
|
|
for each in self.__other_kwargs__:
|
|
|
|
|
args[each] = self.__other_kwargs__[each]
|
|
|
|
|
return getattr(conf_helps, method_name)(name=self.name, **args)
|
|
|
|
|
return getattr(conf_helps, method_name)(**args)
|
|
|
|
|
|
|
|
|
|
return V2LayerImpl
|
|
|
|
|
|
|
|
|
@ -191,6 +205,78 @@ class DataLayerV2(Layer):
|
|
|
|
|
return getattr(conf_helps, self.__method_name__)(name=self.name, **args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MixedLayerV2(Layer):
|
|
|
|
|
"""
|
|
|
|
|
This class is use to support `with` grammar. If not, the following code
|
|
|
|
|
could convert mixed_layer simply.
|
|
|
|
|
|
|
|
|
|
mixed = __convert_to_v2__(
|
|
|
|
|
'mixed_layer', name_prefix='mixed', parent_names=['input'])
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class AddToSealedMixedLayerExceptionV2(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
|
size=0,
|
|
|
|
|
input=None,
|
|
|
|
|
name=None,
|
|
|
|
|
act=None,
|
|
|
|
|
bias_attr=None,
|
|
|
|
|
layer_attr=None):
|
|
|
|
|
self.__method_name__ = 'mixed_layer'
|
|
|
|
|
self.finalized = False
|
|
|
|
|
self.__inputs__ = []
|
|
|
|
|
if input is not None:
|
|
|
|
|
self.__inputs__ = input
|
|
|
|
|
|
|
|
|
|
other_kwargs = dict()
|
|
|
|
|
other_kwargs['name'] = name
|
|
|
|
|
other_kwargs['size'] = size
|
|
|
|
|
other_kwargs['act'] = act
|
|
|
|
|
other_kwargs['bias_attr'] = bias_attr
|
|
|
|
|
other_kwargs['layer_attr'] = layer_attr
|
|
|
|
|
|
|
|
|
|
parent_layers = {"input": self.__inputs__}
|
|
|
|
|
super(MixedLayerV2, self).__init__(name, parent_layers)
|
|
|
|
|
self.__other_kwargs__ = other_kwargs
|
|
|
|
|
|
|
|
|
|
def __iadd__(self, other):
|
|
|
|
|
if not self.finalized:
|
|
|
|
|
self.__inputs__.append(other)
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
raise MixedLayerTypeV2.AddToSealedMixedLayerExceptionV2()
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
assert len(self.__inputs__) == 0
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *args, **kwargs):
|
|
|
|
|
self.finalized = True
|
|
|
|
|
|
|
|
|
|
def to_proto_impl(self, **kwargs):
|
|
|
|
|
args = dict()
|
|
|
|
|
for each in kwargs:
|
|
|
|
|
args[each] = kwargs[each]
|
|
|
|
|
for each in self.__other_kwargs__:
|
|
|
|
|
args[each] = self.__other_kwargs__[each]
|
|
|
|
|
return getattr(conf_helps, self.__method_name__)(**args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wrap_name_default("mixed")
|
|
|
|
|
@wrap_act_default(act=activation.Linear())
|
|
|
|
|
@wrap_bias_attr_default(has_bias=False)
|
|
|
|
|
@layer_support(conf_helps.layers.ERROR_CLIPPING, conf_helps.layers.DROPOUT)
|
|
|
|
|
def mixed(size=0,
|
|
|
|
|
name=None,
|
|
|
|
|
input=None,
|
|
|
|
|
act=None,
|
|
|
|
|
bias_attr=False,
|
|
|
|
|
layer_attr=None):
|
|
|
|
|
return MixedLayerV2(size, input, name, act, bias_attr, layer_attr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = DataLayerV2
|
|
|
|
|
fc = __convert_to_v2__('fc_layer', name_prefix='fc', parent_names=['input'])
|
|
|
|
|
max_id = __convert_to_v2__(
|
|
|
|
@ -226,32 +312,15 @@ sum_cost = __convert_to_v2__(
|
|
|
|
|
huber_cost = __convert_to_v2__(
|
|
|
|
|
'huber_cost', name_prefix='huber_cost', parent_names=['input', 'label'])
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
pixel = data(name='pixel', type=data_type.dense_vector(784))
|
|
|
|
|
label = data(name='label', type=data_type.integer_value(10))
|
|
|
|
|
weight = data(name='weight', type=data_type.dense_vector(10))
|
|
|
|
|
score = data(name='score', type=data_type.dense_vector(1))
|
|
|
|
|
|
|
|
|
|
hidden = fc(input=pixel,
|
|
|
|
|
size=100,
|
|
|
|
|
act=activation.Sigmoid(),
|
|
|
|
|
param_attr=attr.Param(name='hidden'))
|
|
|
|
|
inference = fc(input=hidden, size=10, act=activation.Softmax())
|
|
|
|
|
maxid = max_id(input=inference)
|
|
|
|
|
cost1 = classification_cost(input=inference, label=label)
|
|
|
|
|
cost2 = classification_cost(input=inference, label=label, weight=weight)
|
|
|
|
|
cost3 = cross_entropy_cost(input=inference, label=label)
|
|
|
|
|
cost4 = cross_entropy_with_selfnorm_cost(input=inference, label=label)
|
|
|
|
|
cost5 = regression_cost(input=inference, label=label)
|
|
|
|
|
cost6 = regression_cost(input=inference, label=label, weight=weight)
|
|
|
|
|
cost7 = multi_binary_label_cross_entropy_cost(input=inference, label=label)
|
|
|
|
|
cost8 = rank_cost(left=score, right=score, label=score)
|
|
|
|
|
cost9 = lambda_cost(input=inference, score=score)
|
|
|
|
|
cost10 = sum_cost(input=inference)
|
|
|
|
|
cost11 = huber_cost(input=score, label=label)
|
|
|
|
|
|
|
|
|
|
print parse_network(cost1, cost2)
|
|
|
|
|
print parse_network(cost3, cost4)
|
|
|
|
|
print parse_network(cost5, cost6)
|
|
|
|
|
print parse_network(cost7, cost8, cost9, cost10, cost11)
|
|
|
|
|
print parse_network(inference, maxid)
|
|
|
|
|
# convert projection
|
|
|
|
|
for prj in __projection_names__:
|
|
|
|
|
globals()[prj] = __convert_to_v2__(prj, parent_names=['input'])
|
|
|
|
|
|
|
|
|
|
# convert operator
|
|
|
|
|
operator_list = [
|
|
|
|
|
# [V1_method_name, parent_names],
|
|
|
|
|
['dotmul_operator', ['a', 'b']],
|
|
|
|
|
['conv_operator', ['img', 'filter']]
|
|
|
|
|
]
|
|
|
|
|
for op in operator_list:
|
|
|
|
|
globals()[op[0]] = __convert_to_v2__(op[0], parent_names=op[1])
|
|
|
|
|