From c57954e71cee4cc3e45f7dff21c770c4f636f319 Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Fri, 17 Feb 2017 09:57:37 +0800 Subject: [PATCH] use v2.layer in api_train_v2 --- demo/mnist/api_train_v2.py | 36 ++++++++++++++++++------------------ python/paddle/v2/__init__.py | 5 +++-- python/paddle/v2/layer.py | 10 +++++----- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/demo/mnist/api_train_v2.py b/demo/mnist/api_train_v2.py index 5e46d510ad..47584f3380 100644 --- a/demo/mnist/api_train_v2.py +++ b/demo/mnist/api_train_v2.py @@ -1,7 +1,7 @@ -from paddle.trainer_config_helpers import * -from paddle.trainer.PyDataProvider2 import dense_vector, integer_value -import paddle.v2 as paddle import numpy +import paddle.v2 as paddle +from paddle.trainer.PyDataProvider2 import dense_vector, integer_value + import mnist_util @@ -12,32 +12,32 @@ def train_reader(): yield item -def network_config(): - imgs = data_layer(name='pixel', size=784) - hidden1 = fc_layer(input=imgs, size=200) - hidden2 = fc_layer(input=hidden1, size=200) - inference = fc_layer(input=hidden2, size=10, act=SoftmaxActivation()) - cost = classification_cost( - input=inference, label=data_layer( - name='label', size=10)) - outputs(cost) - - def main(): paddle.init(use_gpu=False, trainer_count=1) - topology = parse_network_config(network_config) + + # define network topology + images = paddle.layer.data(name='pixel', size=784) + label = paddle.layer.data(name='label', size=10) + hidden1 = paddle.layer.fc(input=images, size=200) + hidden2 = paddle.layer.fc(input=hidden1, size=200) + inference = paddle.layer.fc(input=hidden2, + size=10, + act=paddle.activation.Softmax()) + cost = paddle.layer.classification_cost(input=inference, label=label) + + topology = paddle.layer.parse_network(cost) + print topology parameters = paddle.parameters.create(topology) for param_name in parameters.keys(): array = parameters.get(param_name) array[:] = numpy.random.uniform(low=-1.0, high=1.0, size=array.shape) parameters.set(parameter_name=param_name, value=array) - adam_optimizer = paddle.optimizer.Optimizer( - learning_rate=0.01, learning_method=AdamOptimizer()) + adam_optimizer = paddle.optimizer.Adam(learning_rate=0.01) def event_handler(event): if isinstance(event, paddle.event.EndIteration): - para = parameters.get('___fc_layer_2__.w0') + para = parameters.get('___fc_2__.w0') print "Pass %d, Batch %d, Cost %f, Weight Mean Of Fc 2 is %f" % ( event.pass_id, event.batch_id, event.cost, para.mean()) diff --git a/python/paddle/v2/__init__.py b/python/paddle/v2/__init__.py index 577e073ee5..bc064a21ae 100644 --- a/python/paddle/v2/__init__.py +++ b/python/paddle/v2/__init__.py @@ -19,8 +19,9 @@ import trainer import event import py_paddle.swig_paddle as api - -__all__ = ['optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', 'event'] +__all__ = [ + 'optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', 'event' +] def init(**kwargs): diff --git a/python/paddle/v2/layer.py b/python/paddle/v2/layer.py index 4acbd5ef28..0ce4ecd569 100644 --- a/python/paddle/v2/layer.py +++ b/python/paddle/v2/layer.py @@ -72,6 +72,11 @@ from paddle.trainer_config_helpers.config_parser_utils import \ from paddle.trainer_config_helpers.default_decorators import wrap_name_default import collections +__all__ = [ + 'parse_network', 'data', 'fc', 'max_id', 'classification_cost', + 'cross_entropy_cost' +] + def parse_network(*outputs): """ @@ -165,11 +170,6 @@ cross_entropy_cost = __convert_to_v2__( name_prefix='cross_entropy', parent_names=['input', 'label']) -__all__ = [ - 'parse_network', 'data', 'fc', 'max_id', 'classification_cost', - 'cross_entropy_cost' -] - if __name__ == '__main__': pixel = data(name='pixel', size=784) label = data(name='label', size=10) -- GitLab