提交 6d09f70a 编写于 作者: L liaogang

Add event_handler test and comment

上级 ec8e2108
...@@ -12,27 +12,41 @@ ...@@ -12,27 +12,41 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License # limitations under the License
import sys
import paddle.v2 as paddle
from api_v2_vgg import vgg_bn_drop from api_v2_vgg import vgg_bn_drop
from api_v2_resnet import resnet_cifar10 from api_v2_resnet import resnet_cifar10
import paddle.v2 as paddle
# End batch and end pass event handler
def event_handler(event): def event_handler(event):
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0: if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % (event.pass_id, event.batch_id, print "\nPass %d, Batch %d, Cost %f, %s" % (
event.cost) event.pass_id, event.batch_id, event.cost, event.metrics)
else:
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
result = trainer.test(
reader=paddle.reader.batched(
paddle.dataset.cifar.test10(), batch_size=128),
reader_dict={'image': 0,
'label': 1})
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
def main(): def main():
datadim = 3 * 32 * 32 datadim = 3 * 32 * 32
classdim = 10 classdim = 10
# PaddlePaddle init
paddle.init(use_gpu=True, trainer_count=1) paddle.init(use_gpu=True, trainer_count=1)
image = paddle.layer.data( image = paddle.layer.data(
name="image", type=paddle.data_type.dense_vector(datadim)) name="image", type=paddle.data_type.dense_vector(datadim))
# Add neural network config
# option 1. resnet # option 1. resnet
net = resnet_cifar10(image, depth=32) net = resnet_cifar10(image, depth=32)
# option 2. vgg # option 2. vgg
...@@ -46,8 +60,10 @@ def main(): ...@@ -46,8 +60,10 @@ def main():
name="label", type=paddle.data_type.integer_value(classdim)) name="label", type=paddle.data_type.integer_value(classdim))
cost = paddle.layer.classification_cost(input=out, label=lbl) cost = paddle.layer.classification_cost(input=out, label=lbl)
# Create parameters
parameters = paddle.parameters.create(cost) parameters = paddle.parameters.create(cost)
# Create optimizer
momentum_optimizer = paddle.optimizer.Momentum( momentum_optimizer = paddle.optimizer.Momentum(
momentum=0.9, momentum=0.9,
regularization=paddle.optimizer.L2Regularization(rate=0.0002 * 128), regularization=paddle.optimizer.L2Regularization(rate=0.0002 * 128),
...@@ -57,6 +73,7 @@ def main(): ...@@ -57,6 +73,7 @@ def main():
learning_rate_schedule='discexp', learning_rate_schedule='discexp',
batch_size=128) batch_size=128)
# Create trainer
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters, parameters=parameters,
update_equation=momentum_optimizer) update_equation=momentum_optimizer)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册