提交 c57954e7 编写于 作者: Q qiaolongfei

use v2.layer in api_train_v2

上级 6dc6814f
from paddle.trainer_config_helpers import *
from paddle.trainer.PyDataProvider2 import dense_vector, integer_value
import paddle.v2 as paddle
import numpy import numpy
import paddle.v2 as paddle
from paddle.trainer.PyDataProvider2 import dense_vector, integer_value
import mnist_util import mnist_util
...@@ -12,32 +12,32 @@ def train_reader(): ...@@ -12,32 +12,32 @@ def train_reader():
yield item 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(): def main():
paddle.init(use_gpu=False, trainer_count=1) 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) parameters = paddle.parameters.create(topology)
for param_name in parameters.keys(): for param_name in parameters.keys():
array = parameters.get(param_name) array = parameters.get(param_name)
array[:] = numpy.random.uniform(low=-1.0, high=1.0, size=array.shape) array[:] = numpy.random.uniform(low=-1.0, high=1.0, size=array.shape)
parameters.set(parameter_name=param_name, value=array) parameters.set(parameter_name=param_name, value=array)
adam_optimizer = paddle.optimizer.Optimizer( adam_optimizer = paddle.optimizer.Adam(learning_rate=0.01)
learning_rate=0.01, learning_method=AdamOptimizer())
def event_handler(event): def event_handler(event):
if isinstance(event, paddle.event.EndIteration): 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" % ( print "Pass %d, Batch %d, Cost %f, Weight Mean Of Fc 2 is %f" % (
event.pass_id, event.batch_id, event.cost, para.mean()) event.pass_id, event.batch_id, event.cost, para.mean())
......
...@@ -19,8 +19,9 @@ import trainer ...@@ -19,8 +19,9 @@ import trainer
import event import event
import py_paddle.swig_paddle as api import py_paddle.swig_paddle as api
__all__ = [
__all__ = ['optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', 'event'] 'optimizer', 'layer', 'activation', 'parameters', 'init', 'trainer', 'event'
]
def init(**kwargs): def init(**kwargs):
......
...@@ -72,6 +72,11 @@ from paddle.trainer_config_helpers.config_parser_utils import \ ...@@ -72,6 +72,11 @@ from paddle.trainer_config_helpers.config_parser_utils import \
from paddle.trainer_config_helpers.default_decorators import wrap_name_default from paddle.trainer_config_helpers.default_decorators import wrap_name_default
import collections import collections
__all__ = [
'parse_network', 'data', 'fc', 'max_id', 'classification_cost',
'cross_entropy_cost'
]
def parse_network(*outputs): def parse_network(*outputs):
""" """
...@@ -165,11 +170,6 @@ cross_entropy_cost = __convert_to_v2__( ...@@ -165,11 +170,6 @@ cross_entropy_cost = __convert_to_v2__(
name_prefix='cross_entropy', name_prefix='cross_entropy',
parent_names=['input', 'label']) parent_names=['input', 'label'])
__all__ = [
'parse_network', 'data', 'fc', 'max_id', 'classification_cost',
'cross_entropy_cost'
]
if __name__ == '__main__': if __name__ == '__main__':
pixel = data(name='pixel', size=784) pixel = data(name='pixel', size=784)
label = data(name='label', size=10) label = data(name='label', size=10)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册