test_train.py 3.3 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
import paddle.v2 as paddle
import paddle.v2.dataset.uci_housing as uci_housing
17 18 19
import paddle.v2.master as master
import os
import cPickle as pickle
Y
Yancey 已提交
20
from paddle.v2.reader.creator import cloud_reader
21 22

etcd_ip = os.getenv("MASTER_IP", "127.0.0.1")
Y
Yancey 已提交
23 24
etcd_endpoints = "http://" + etcd_ip + ":2379"
print "etcd endpoints: ", etcd_endpoints
25 26 27 28


def main():
    # init
Q
qiaolongfei 已提交
29
    paddle.init(use_gpu=False, trainer_count=1)
30 31 32 33

    # network config
    x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
    y_predict = paddle.layer.fc(input=x,
34
                                param_attr=paddle.attr.Param(name='w'),
35 36
                                size=1,
                                act=paddle.activation.Linear(),
37
                                bias_attr=paddle.attr.Param(name='b'))
38 39 40 41 42 43
    y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
    cost = paddle.layer.mse_cost(input=y_predict, label=y)

    # create parameters
    parameters = paddle.parameters.create(cost)

W
wuyi05 已提交
44
    # create optimizer of new remote updater to pserver
武毅 已提交
45
    optimizer = paddle.optimizer.Momentum(momentum=0, learning_rate=1e-3)
46 47 48 49 50

    trainer = paddle.trainer.SGD(cost=cost,
                                 parameters=parameters,
                                 update_equation=optimizer,
                                 is_local=False,
Y
Yancey 已提交
51
                                 pserver_spec=etcd_endpoints,
52
                                 use_etcd=True)
53 54 55 56

    # event_handler to print training and testing info
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
57 58
            # FIXME: for cloud data reader, pass number is managed by master
            # should print the server side pass number
59 60 61 62 63 64 65 66 67 68 69 70 71 72
            if event.batch_id % 100 == 0:
                print "Pass %d, Batch %d, Cost %f" % (
                    event.pass_id, event.batch_id, event.cost)

        if isinstance(event, paddle.event.EndPass):
            if (event.pass_id + 1) % 10 == 0:
                result = trainer.test(
                    reader=paddle.batch(
                        uci_housing.test(), batch_size=2),
                    feeding={'x': 0,
                             'y': 1})
                print "Test %d, %.2f" % (event.pass_id, result.cost)

    # training
73
    # NOTE: use uci_housing.train() as reader for non-paddlecloud training
74 75 76
    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(
Y
Yancey 已提交
77 78 79 80 81
                cloud_reader(
                    ["/pfs/dlnel/public/dataset/uci_housing/uci_housing*"],
                    etcd_endpoints),
                buf_size=500),
            batch_size=2),
82 83 84 85 86 87 88 89
        feeding={'x': 0,
                 'y': 1},
        event_handler=event_handler,
        num_passes=30)


if __name__ == '__main__':
    main()