-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Feature/python.paddle.v2 #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/python.paddle.v2 #1108
Changes from 2 commits
07fe0ee
1935b34
da97042
823eb1f
3bc8f99
26e7ca9
258fc55
2b988b4
5e1d187
a2cf635
7826fdf
d896ff4
e7da4ae
6e4086c
286372b
9360a1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,42 +6,33 @@ | |
|
|
||
| The user api could be simpler and carefully designed. | ||
| """ | ||
| import py_paddle.swig_paddle as api | ||
| from py_paddle import DataProviderConverter | ||
| import paddle.trainer.PyDataProvider2 as dp | ||
| import numpy as np | ||
| import random | ||
|
|
||
| import paddle.v2 as paddle | ||
| import py_paddle.swig_paddle as api | ||
|
|
||
| from mnist_util import read_from_mnist | ||
| from paddle.trainer_config_helpers import * | ||
|
|
||
|
|
||
| def optimizer_config(): | ||
| settings( | ||
| paddle.config.settings( | ||
| learning_rate=1e-4, | ||
| learning_method=AdamOptimizer(), | ||
| learning_method=paddle.config.AdamOptimizer(), | ||
| batch_size=1000, | ||
| model_average=ModelAverage(average_window=0.5), | ||
| regularization=L2Regularization(rate=0.5)) | ||
| model_average=paddle.config.ModelAverage(average_window=0.5), | ||
| regularization=paddle.config.L2Regularization(rate=0.5)) | ||
|
|
||
|
|
||
| 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( | ||
| imgs = paddle.config.data_layer(name='pixel', size=784) | ||
|
||
| hidden1 = paddle.config.fc_layer(input=imgs, size=200) | ||
| hidden2 = paddle.config.fc_layer(input=hidden1, size=200) | ||
| inference = paddle.config.fc_layer( | ||
| input=hidden2, size=10, act=paddle.config.SoftmaxActivation()) | ||
| cost = paddle.config.classification_cost( | ||
| input=inference, label=paddle.config.data_layer( | ||
| name='label', size=10)) | ||
| outputs(cost) | ||
|
|
||
|
|
||
| def init_parameter(network): | ||
| assert isinstance(network, api.GradientMachine) | ||
| for each_param in network.getParameters(): | ||
| assert isinstance(each_param, api.Parameter) | ||
| array_size = len(each_param) | ||
| array = np.random.uniform(-1.0, 1.0, array_size).astype('float32') | ||
| each_param.getBuf(api.PARAMETER_VALUE).copyFromNumpyArray(array) | ||
| paddle.config.outputs(cost) | ||
|
|
||
|
|
||
| def generator_to_batch(generator, batch_size): | ||
|
|
@@ -73,42 +64,44 @@ def input_order_converter(generator): | |
|
|
||
|
|
||
| def main(): | ||
| api.initPaddle("-use_gpu=false", "-trainer_count=4") # use 4 cpu cores | ||
| paddle.raw.initPaddle("-use_gpu=false", | ||
| "-trainer_count=4") # use 4 cpu cores | ||
|
|
||
| # get enable_types for each optimizer. | ||
| # enable_types = [value, gradient, momentum, etc] | ||
| # For each optimizer(SGD, Adam), GradientMachine should enable different | ||
| # buffers. | ||
| opt_config_proto = parse_optimizer_config(optimizer_config) | ||
| opt_config = api.OptimizationConfig.createFromProto(opt_config_proto) | ||
| _temp_optimizer_ = api.ParameterOptimizer.create(opt_config) | ||
| opt_config_proto = paddle.config.parse_optimizer(optimizer_config) | ||
| opt_config = paddle.raw.OptimizationConfig.createFromProto(opt_config_proto) | ||
| _temp_optimizer_ = paddle.raw.ParameterOptimizer.create(opt_config) | ||
|
||
| enable_types = _temp_optimizer_.getParameterTypes() | ||
|
||
|
|
||
| # Create Simple Gradient Machine. | ||
| model_config = parse_network_config(network_config) | ||
| m = api.GradientMachine.createFromConfigProto( | ||
| model_config = paddle.config.parse_network(network_config) | ||
| m = paddle.raw.GradientMachine.createFromConfigProto( | ||
| model_config, api.CREATE_MODE_NORMAL, enable_types) | ||
|
|
||
| # This type check is not useful. Only enable type hint in IDE. | ||
| # Such as PyCharm | ||
| assert isinstance(m, api.GradientMachine) | ||
| assert isinstance(m, paddle.raw.GradientMachine) | ||
|
||
|
|
||
| # Initialize Parameter by numpy. | ||
| init_parameter(network=m) | ||
| m.randParameters() | ||
|
||
|
|
||
| # Create Local Updater. Local means not run in cluster. | ||
| # For a cluster training, here we can change to createRemoteUpdater | ||
| # in future. | ||
| updater = api.ParameterUpdater.createLocalUpdater(opt_config) | ||
| assert isinstance(updater, api.ParameterUpdater) | ||
| updater = paddle.raw.ParameterUpdater.createLocalUpdater(opt_config) | ||
|
||
| assert isinstance(updater, paddle.raw.ParameterUpdater) | ||
|
||
|
|
||
| # Initialize ParameterUpdater. | ||
| updater.init(m) | ||
|
|
||
| # 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=[dp.dense_vector(784), dp.integer_value(10)]) | ||
| converter = paddle.data.DataProviderConverter(input_types=[ | ||
|
||
| paddle.data.dense_vector(784), paddle.data.integer_value(10) | ||
| ]) | ||
|
|
||
| train_file = './data/raw_data/train' | ||
| test_file = './data/raw_data/t10k' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| This is an experimental package for Paddle new API. | ||
|
|
||
| Currently, we use should always use | ||
|
|
||
| .. code-block: python | ||
|
|
||
| import paddle.v2 as paddle | ||
|
|
||
| as our import statement. The API is in flux, never use this package in | ||
| production. | ||
| """ | ||
|
|
||
| import py_paddle.swig_paddle as raw | ||
| import config | ||
| import data | ||
|
|
||
| __all__ = ['config', 'data', 'raw'] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from paddle.trainer_config_helpers import * | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里 import *,让我们丧失了对 paddle.v2 package 里的内容的掌握——只要有人修改了 paddle.trianer_config_helpers 里的内容,这里的symbols就发生变化了吧? 我建议,我们趁此机会,先把 mnist 这个 demo 需要的内容 copy-n-paste 过来。然后依据把 mnist demo 写的让读者能“顾名思义”的原则,修改 copy 过来的库。 随后我们一个一个demo的过,重复上述过程,得到的 paddle.v2 应该就是我们想要的了吧。
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不过其实paddle.trainer_config_helpers里面暴露的符号是严格控制的。里面使用了__all__来控制符号暴露。 copy and paste这个包,会少暴露非常多东西。比如,我们教程里面的MNIST可能使用全连接做的。用户可能想改成卷积之类的操作。但是,如果只是copy and paste demo需要的接口的话,卷积很可能就没复制过去。这用户就缺乏了这部分灵活性了。 同时,和之前的一个comment类似,如果我们真的需要使用『返回值』而不是『函数』来去定义网络结构的话,那其实所有的配置解析都要重写一下。copy and paste反而不好,不如直接重写一个解析过程。
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我建议copy-n-paste,就是为了“重写”,而不只是为现有symbols在v2 package下面建立一个link。 |
||
| from paddle.trainer.config_parser import parse_config as parse | ||
| from paddle.trainer_config_helpers.config_parser_utils import \ | ||
| parse_network_config as parse_network | ||
| from paddle.trainer_config_helpers.config_parser_utils import \ | ||
| parse_optimizer_config as parse_optimizer | ||
|
|
||
| import paddle.trainer_config_helpers as tmp | ||
|
|
||
| __all__ = ['parse', 'parse_network', 'parse_optimizer'] | ||
|
|
||
| __all__.extend(filter(lambda x: x[:2] != '__', dir(tmp))) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from paddle.trainer.PyDataProvider2 import * | ||
| from py_paddle.dataprovider_converter import DataProviderConverter | ||
|
|
||
| __all__ = [ | ||
| 'dense_vector', 'dense_vector_sequence', 'dense_vector_sub_sequence', | ||
| 'integer_value', 'integer_sequence', 'integer_value_sub_sequence', | ||
| 'sparse_binary_vector', 'sparse_binary_vector_sequence', | ||
| 'sparse_binary_vector_sub_sequence', 'sparse_vector', | ||
| 'sparse_vector_sequence', 'sparse_vector_sub_sequence', 'provider', | ||
| 'CacheType', 'DataProviderConverter' | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,8 @@ packages=['paddle', | |
| 'paddle.proto', | ||
| 'paddle.trainer', | ||
| 'paddle.trainer_config_helpers', | ||
| 'paddle.utils'] | ||
| 'paddle.utils', | ||
| 'paddle.v2'] | ||
|
||
|
|
||
| setup(name='paddle', | ||
| version='${PADDLE_VERSION}', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我总觉得这个swig_paddle需要稍微封装一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看到下边已经改了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.