Exemplo n.º 1
0
    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):
	   if event.pass_id % 10 == 0:
               with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                   trainer.save_parameter_to_tar(f)
           result = trainer.test(reader=paddle.batch(uci_housing.test(), batch_size=2), feeding=feeding)        
           print "Test %d, Cost %f" % (event.pass_id, result.cost)
Exemplo n.º 2
0
  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)
Exemplo n.º 3
0
    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):
            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)
Exemplo n.º 4
0
    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)
            if trainer_id == "0":
                with gzip.open("fit-a-line_pass_%05d.tar.gz" % event.pass_id,
                               "w") as f:
                    parameters.to_tar(f)
Exemplo n.º 5
0
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            # FIXME: for cloud data reader, pass number is managed by master
            # should print the server side pass number
            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)
Exemplo n.º 6
0
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            # FIXME: for cloud data reader, pass number is managed by master
            # should print the server side pass number
            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)
Exemplo n.º 7
0
    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):
            #保存参数
            #if event.pass_id % 10 == 0:
            with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                trainer.save_parameter_to_tar(f)
            result = trainer.test(
                reader=paddle.batch(uci_housing.test(), batch_size=2),
                feeding=feeding)
            print "Test %d, Cost %f" % (event.pass_id, result.cost)
            #print result.metrics
            #保存训练结果损失情况
            lists.append((event.pass_id, result.cost,
                          #result.metrics['classification_error_evaluator']))
                          result.metrics))
Exemplo n.º 8
0
def event_handler_plot(event):
    global step
    if isinstance(event, paddle.event.EndIteration):
        if step % 10 == 0:  # every 10 batches, record a train cost
            plot_cost.append(train_title, step, event.cost)

        if step % 100 == 0:  #every 100 batches,  record a test cost
            result = trainer.test(reader=paddle.batch(uci_housing.test(),
                                                      batch_size=2),
                                  feeding=feeding)

            plot_cost.append(test_title, step, result.cost)

        if step % 100 == 0:
            plot_cost.plot()

        step += 1

    if isinstance(event, paddle.event.EndPass):
        if event.pass_id % 10 == 0:
            with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                parameters.to_tar(f)
Exemplo n.º 9
0
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
y = paddle.layer.data(name='yy', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.square_error_cost(input=y_predict, label=y)
parameters = paddle.parameters.create(cost)
optimizer = paddle.optimizer.Momentum(momentum=0)
trainer = paddle.trainer.SGD(cost=cost,
                             parameters=parameters,
                             update_equation=optimizer)
feeding = {'xx': 0, 'yy': 1}

trainer.train(reader=paddle.batch(paddle.reader.shuffle(uci_housing.train(),
                                                        buf_size=500),
                                  batch_size=2),
              feeding=feeding,
              num_passes=30)
test_data_creator = uci_housing.test()
test_data = []
test_label = []
for item in test_data_creator():
    test_data.append((item[0], ))
    test_label.append(item[1])
    if len(test_data) == 5:
        break

probs = paddle.infer(output_layer=y_predict,
                     parameters=parameters,
                     input=test_data)

for i in xrange(len(probs)):
    print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])