import paddle.v2 as paddle
import pcloud.dataset.uci_housing as uci_housing

def main():
    # init
    paddle.init()

    # network config
    x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
    y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
    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)

    # create optimizer
    optimizer = paddle.optimizer.Momentum(momentum=0)

    trainer = paddle.trainer.SGD(
        cost=cost, parameters=parameters, update_equation=optimizer, is_local=False)

    feeding = {'x': 0, 'y': 1}

    # event_handler to print training and testing info
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            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):
            result = trainer.test(
                reader=paddle.batch(uci_housing.test(), batch_size=2),
                feeding=feeding)
            print "Test %d, Cost %f" % (event.pass_id, result.cost)

    # training
    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(uci_housing.train(), buf_size=500),
            batch_size=2),
        feeding=feeding,
        event_handler=event_handler,
        num_passes=30)


if __name__ == '__main__':
    main()

@@ -68,16 +68,87 @@
 _housing
+%0Aimport os%0Aimport gzip%0Atrianer_id = os.getenv(%22PADDLE_INIT_TRAINER_ID%22)
 %0A%0Adef ma
@@ -1357,24 +1357,218 @@
 esult.cost)%0A
+            if trainer_id == %220%22:%0A                with gzip.open(%22fit-a-line_pass_%2505d.tar.gz%22 %25 event.pass_id,%0A                               %22w%22) as f:%0A                    parameters.to_tar(f)
 %0A    # train

save parameter