From 984d54d0180729236f660d3af787fff862fdbc74 Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Mon, 20 Feb 2017 21:51:57 +0800 Subject: [PATCH] change data_type to type --- demo/mnist/api_train.py | 10 +++++----- demo/mnist/api_train_v2.py | 9 ++++----- python/paddle/v2/__init__.py | 4 +++- python/paddle/v2/data.py | 22 ++++++++++++++++++++++ python/paddle/v2/layer.py | 14 +++++++------- 5 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 python/paddle/v2/data.py diff --git a/demo/mnist/api_train.py b/demo/mnist/api_train.py index c6993eda38b..25c25c1fcdd 100644 --- a/demo/mnist/api_train.py +++ b/demo/mnist/api_train.py @@ -9,7 +9,6 @@ The user api could be simpler and carefully designed. import random import numpy as np -import paddle.trainer.PyDataProvider2 as dp import paddle.v2 as paddle_v2 import py_paddle.swig_paddle as api from paddle.trainer_config_helpers import * @@ -71,8 +70,10 @@ def main(): assert isinstance(updater, api.ParameterUpdater) # define network - 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)) + images = paddle_v2.layer.data( + name='pixel', type=paddle_v2.data.dense_vector(784)) + label = paddle_v2.layer.data( + name='label', type=paddle_v2.data.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, @@ -98,8 +99,7 @@ def main(): # DataProvider Converter is a utility convert Python Object to Paddle C++ # Input. The input format is as same as Paddle's DataProvider. - converter = DataProviderConverter( - input_types=[images.data_type, label.data_type]) + converter = DataProviderConverter(input_types=[images.type, label.type]) train_file = './data/raw_data/train' test_file = './data/raw_data/t10k' diff --git a/demo/mnist/api_train_v2.py b/demo/mnist/api_train_v2.py index 6bbc3a4ab35..c50a46c4d82 100644 --- a/demo/mnist/api_train_v2.py +++ b/demo/mnist/api_train_v2.py @@ -1,6 +1,5 @@ import numpy import paddle.v2 as paddle -from paddle.trainer.PyDataProvider2 import dense_vector, integer_value import mnist_util @@ -16,8 +15,8 @@ def main(): paddle.init(use_gpu=False, trainer_count=1) # define network topology - images = paddle.layer.data(name='pixel', data_type=dense_vector(784)) - label = paddle.layer.data(name='label', data_type=integer_value(10)) + images = paddle.layer.data(name='pixel', type=paddle.data.dense_vector(784)) + label = paddle.layer.data(name='label', type=paddle.data.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, @@ -51,8 +50,8 @@ def main(): batch_size=32, # batch size should be refactor in Data reader data_types={ # data_types will be removed, It should be in # network topology - 'pixel': images.data_type, - 'label': label.data_type + 'pixel': images.type, + 'label': label.type }) diff --git a/python/paddle/v2/__init__.py b/python/paddle/v2/__init__.py index bc064a21ae1..cd6dfce0e66 100644 --- a/python/paddle/v2/__init__.py +++ b/python/paddle/v2/__init__.py @@ -17,10 +17,12 @@ import activation import parameters import trainer import event +import data import py_paddle.swig_paddle as api __all__ = [ - 'optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', 'event' + 'optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', + 'event', 'data' ] diff --git a/python/paddle/v2/data.py b/python/paddle/v2/data.py new file mode 100644 index 00000000000..5b01ba4cd48 --- /dev/null +++ b/python/paddle/v2/data.py @@ -0,0 +1,22 @@ +# 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. + +from paddle.trainer.PyDataProvider2 import \ + InputType, dense_vector, sparse_binary_vector,\ + sparse_vector, integer_value + +__all__ = [ + 'InputType', 'dense_vector', 'sparse_binary_vector', 'sparse_vector', + 'integer_value' +] diff --git a/python/paddle/v2/layer.py b/python/paddle/v2/layer.py index f2bc47946b6..f5a411198e4 100644 --- a/python/paddle/v2/layer.py +++ b/python/paddle/v2/layer.py @@ -67,7 +67,7 @@ paddle.v2.parameters.create, no longer exposed to users. """ import paddle.trainer_config_helpers as conf_helps -import paddle.trainer.PyDataProvider2 as dp +from . import data as v2_data 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 @@ -165,10 +165,10 @@ So we also need to implement some special LayerV2. class DataLayerV2(Layer): - def __init__(self, name, data_type, **kwargs): - assert isinstance(data_type, dp.InputType) + def __init__(self, name, type, **kwargs): + assert isinstance(type, v2_data.InputType) - self.data_type = data_type + self.type = type self.__method_name__ = 'data_layer' self.__kwargs__ = kwargs @@ -176,7 +176,7 @@ class DataLayerV2(Layer): def to_proto_impl(self, **kwargs): args = dict() - args['size'] = self.data_type.dim + args['size'] = self.type.dim for each in kwargs: args[each] = kwargs[each] for each in self.__kwargs__: @@ -198,8 +198,8 @@ cross_entropy_cost = __convert_to_v2__( parent_names=['input', 'label']) if __name__ == '__main__': - pixel = data(name='pixel', data_type=dp.dense_vector(784)) - label = data(name='label', data_type=dp.integer_value(10)) + pixel = data(name='pixel', type=v2_data.dense_vector(784)) + label = data(name='label', type=v2_data.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