From 772b476b76e0436de286922e8486fb21f928e8fc Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Mon, 20 Feb 2017 16:38:08 +0800 Subject: [PATCH] optimize code --- demo/mnist/api_train.py | 4 ++-- demo/mnist/api_train_v2.py | 4 ++-- python/paddle/v2/layer.py | 24 ++++++++++-------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/demo/mnist/api_train.py b/demo/mnist/api_train.py index 77e6fa890..ae00e9f54 100644 --- a/demo/mnist/api_train.py +++ b/demo/mnist/api_train.py @@ -71,8 +71,8 @@ def main(): assert isinstance(updater, api.ParameterUpdater) # define network - images = paddle_v2.layer.data(name='pixel', type=dp.dense_vector(784)) - label = paddle_v2.layer.data(name='label', type=dp.integer_value(10)) + images = paddle_v2.layer.data(name='pixel', data_type=dp.dense_vector(784)) + label = paddle_v2.layer.data(name='label', data_type=dp.integer_value(10)) hidden1 = paddle_v2.layer.fc(input=images, size=200) hidden2 = paddle_v2.layer.fc(input=hidden1, size=200) inference = paddle_v2.layer.fc(input=hidden2, diff --git a/demo/mnist/api_train_v2.py b/demo/mnist/api_train_v2.py index 979cc3f1b..b46cf1c87 100644 --- a/demo/mnist/api_train_v2.py +++ b/demo/mnist/api_train_v2.py @@ -16,8 +16,8 @@ def main(): paddle.init(use_gpu=False, trainer_count=1) # define network topology - images = paddle.layer.data(name='pixel', type=dense_vector(784)) - label = paddle.layer.data(name='label', type=integer_value(10)) + images = paddle.layer.data(name='pixel', data_type=dense_vector(784)) + label = paddle.layer.data(name='label', data_type=integer_value(10)) hidden1 = paddle.layer.fc(input=images, size=200) hidden2 = paddle.layer.fc(input=hidden1, size=200) inference = paddle.layer.fc(input=hidden2, diff --git a/python/paddle/v2/layer.py b/python/paddle/v2/layer.py index e006b7892..511b3e745 100644 --- a/python/paddle/v2/layer.py +++ b/python/paddle/v2/layer.py @@ -165,26 +165,22 @@ So we also need to implement some special LayerV2. class DataLayerV2(Layer): - def __init__(self, name, type, **kwargs): - self.__method_name__ = 'data_layer' - - assert isinstance(type, dp.InputType) + def __init__(self, name, data_type, **kwargs): + assert isinstance(data_type, dp.InputType) - # get data_size from type.dim - args = dict() - for key in kwargs: - args[key] = kwargs[key] - args['size'] = type.dim - self.__args__ = args + self.__method_name__ = 'data_layer' + self.__kwargs__ = kwargs + self.__data_size__ = data_type.dim super(DataLayerV2, self).__init__(name=name, parent_layers=dict()) def to_proto_impl(self, **kwargs): args = dict() + args['size'] = self.__data_size__ for each in kwargs: args[each] = kwargs[each] - for each in self.__args__: - args[each] = self.__args__[each] + for each in self.__kwargs__: + args[each] = self.__kwargs__[each] return getattr(conf_helps, self.__method_name__)(name=self.name, **args) @@ -202,8 +198,8 @@ cross_entropy_cost = __convert_to_v2__( parent_names=['input', 'label']) if __name__ == '__main__': - pixel = data(name='pixel', type=dp.dense_vector(784)) - label = data(name='label', type=dp.integer_value(10)) + pixel = data(name='pixel', data_type=dp.dense_vector(784)) + label = data(name='label', data_type=dp.integer_value(10)) hidden = fc(input=pixel, size=100, act=conf_helps.SigmoidActivation()) inference = fc(input=hidden, size=10, act=conf_helps.SoftmaxActivation()) maxid = max_id(input=inference) -- GitLab