- paddlepaddle-v1.0.0编译
g++-5
cmake-3.5
- 基本例子
#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
import paddle.v2 as paddle
import paddle.v2.dataset.uci_housing as uci_housing
with_gpu = os.getenv('WITH_GPU', '0') != '0'
def main():
# 初始化PaddlePaddle
paddle.init(use_gpu=with_gpu, trainer_count=1)
# 模型配置
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))#利用前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.square_error_cost(input=y_predict, label=y)#损失函数
# 保存网络拓扑
inference_topology = paddle.topology.Topology(layers=y_predict)
with open("inference_topology.pkl", 'wb') as f:
inference_topology.serialize_for_inference(f)
# 创建参数
parameters = paddle.parameters.create(cost)
# 创建trainer
optimizer = paddle.optimizer.Momentum(momentum=0)#learning_rate=0.0001 学习率
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=optimizer)#随机梯度下降算法
feeding = {'x': 0, 'y': 1}
# 读取数据且打印训练的中间信息
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)#读取房价数据,将数据打乱,每次取出2条
print "Test %d, Cost %f" % (event.pass_id, result.cost)
# 开始训练
trainer.train(
reader=paddle.batch(
paddle.reader.shuffle(uci_housing.train(), buf_size=500),
batch_size=2),
feeding=feeding,
event_handler=event_handler,#提供一个 event_handler,来打印训练的进度:
num_passes=3000)
# 生成测试数据
test_data_creator = paddle.dataset.uci_housing.test()
test_data = []
test_label = []
#取出测试集中5条数据用于最后的预测
for item in test_data_creator():
test_data.append((item[0], ))
test_label.append(item[1])
if len(test_data) == 5:
break
#推测inference
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])
if __name__ == '__main__':
main()
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import paddle.v2 as paddle
import multiprocessing
# Initialize PaddlePaddle.
paddle.init(use_gpu=False, trainer_count=1)
# Configure the neural network.
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())
# Infer using provided test data.
probs = paddle.infer(
output_layer=y_predict,
parameters=paddle.dataset.uci_housing.model(),
#parameters="params_pass_20.tar",
input=[item for item in paddle.dataset.uci_housing.test()()])
for i in xrange(len(probs)):
print 'Predicted price: ${:,.2f}'.format(probs[i][0] * 1000)
print("-------------------------------")
if __name__ == '__main__':
mp = multiprocessing.Pool(2)