paddlepaddle是百度提出来的深度学习的框架,个人感觉其实和tensorflow差不多(语法上面),因为本人也是初学者,也不是很懂tensorflow,所以,这些都是个人观点。

百度的paddlepaddle提出貌似有一段时间了,我是最近才知道的,好奇去看了看,而且最近在看tensorflow,所以想看看paddlepaddle是不是友好一点,说实话,tensorflow还是比较难懂的(对于个人来说)。感觉paddlepaddle比tensorflow好的地方在于,paddlepaddle有百度的工程师给出对应视频和代码进行讲解,对于入门深度学习比较好。

以下就是paddlepaddle的第一讲,利用波士顿房价讲解线性回归。

模型训练:

  1. #-*- 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
  2.  
  3. with_gpu = os.getenv('WITH_GPU', '0') != '0'
  4.  
  5. def main():
    # 初始化PaddlePaddle
    paddle.init(use_gpu=with_gpu, trainer_count=1)
  6.  
  7. # 模型配置
    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)#损失函数
  8.  
  9. # 保存网络拓扑
    inference_topology = paddle.topology.Topology(layers=y_predict)
    with open("inference_topology.pkl", 'wb') as f:
    inference_topology.serialize_for_inference(f)
  10.  
  11. # 创建参数
    parameters = paddle.parameters.create(cost)
  12.  
  13. # 创建trainer
    optimizer = paddle.optimizer.Momentum(momentum=0)#learning_rate=0.0001 学习率
  14.  
  15. trainer = paddle.trainer.SGD(
    cost=cost, parameters=parameters, update_equation=optimizer)#随机梯度下降算法
  16.  
  17. feeding = {'x': 0, 'y': 1}
  18.  
  19. # 读取数据且打印训练的中间信息
    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)
  20.  
  21. 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)
  22.  
  23. # 开始训练
    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=30)
  24.  
  25. # 生成测试数据
    test_data_creator = paddle.dataset.uci_housing.test()
    test_data = []
    test_label = []
  26.  
  27. #取出测试集中5条数据用于最后的预测
    for item in test_data_creator():
    test_data.append((item[0], ))
    test_label.append(item[1])
    if len(test_data) == 5:
    break
  28.  
  29. #推测inference
    probs = paddle.infer(
    output_layer=y_predict, parameters=parameters, input=test_data)
  30.  
  31. for i in xrange(len(probs)):
    print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])
  32.  
  33. if __name__ == '__main__':
    main()

运行结果:

  1. Pass 0, Batch 0, Cost 886.077026
  2. Pass 0, Batch 100, Cost 236.768433
  3. Pass 0, Batch 200, Cost 555.669922
  4. Test 0, Cost 56.372781
  5. Pass 1, Batch 0, Cost 558.157104
  6. Pass 1, Batch 100, Cost 17.486526
  7. Pass 1, Batch 200, Cost 49.110359
  8. Test 1, Cost 22.666769
  9. Pass 2, Batch 0, Cost 2.017142
  10. Pass 2, Batch 100, Cost 5.376208
  11. Pass 2, Batch 200, Cost 1.576212
  12. Test 2, Cost 18.296844
  13. Pass 3, Batch 0, Cost 103.864586
  14. Pass 3, Batch 100, Cost 84.158134
  15. Pass 3, Batch 200, Cost 5.564497
  16. Test 3, Cost 17.668033
  17. Pass 4, Batch 0, Cost 2.316584
  18. Pass 4, Batch 100, Cost 9.555552
  19. Pass 4, Batch 200, Cost 74.418373
  20. Test 4, Cost 17.311696
  21. Pass 5, Batch 0, Cost 9.540855
  22. Pass 5, Batch 100, Cost 22.676167
  23. Pass 5, Batch 200, Cost 123.998085
  24. Test 5, Cost 16.799527
  25. Pass 6, Batch 0, Cost 56.558044
  26. Pass 6, Batch 100, Cost 33.035114
  27. Pass 6, Batch 200, Cost 58.189980
  28. Test 6, Cost 16.333503
  29. Pass 7, Batch 0, Cost 7.590010
  30. Pass 7, Batch 100, Cost 34.771137
  31. Pass 7, Batch 200, Cost 44.883244
  32. Test 7, Cost 16.017060
  33. Pass 8, Batch 0, Cost 42.311310
  34. Pass 8, Batch 100, Cost 24.567163
  35. Pass 8, Batch 200, Cost 33.340485
  36. Test 8, Cost 15.520346
  37. Pass 9, Batch 0, Cost 178.452744
  38. Pass 9, Batch 100, Cost 10.791793
  39. Pass 9, Batch 200, Cost 0.137641
  40. Test 9, Cost 15.214742
  41. Pass 10, Batch 0, Cost 10.072014
  42. Pass 10, Batch 100, Cost 11.594021
  43. Pass 10, Batch 200, Cost 24.404564
  44. Test 10, Cost 14.916112
  45. Pass 11, Batch 0, Cost 5.649694
  46. Pass 11, Batch 100, Cost 31.902603
  47. Pass 11, Batch 200, Cost 11.218608
  48. Test 11, Cost 14.600422
  49. Pass 12, Batch 0, Cost 87.761772
  50. Pass 12, Batch 100, Cost 53.684475
  51. Pass 12, Batch 200, Cost 37.861378
  52. Test 12, Cost 14.326864
  53. Pass 13, Batch 0, Cost 5.141076
  54. Pass 13, Batch 100, Cost 0.324465
  55. Pass 13, Batch 200, Cost 2.333709
  56. Test 13, Cost 14.124264
  57. Pass 14, Batch 0, Cost 9.482045
  58. Pass 14, Batch 100, Cost 22.704296
  59. Pass 14, Batch 200, Cost 12.826228
  60. Test 14, Cost 13.945640
  61. Pass 15, Batch 0, Cost 41.819580
  62. Pass 15, Batch 100, Cost 10.353182
  63. Pass 15, Batch 200, Cost 13.374403
  64. Test 15, Cost 13.767083
  65. Pass 16, Batch 0, Cost 83.044785
  66. Pass 16, Batch 100, Cost 27.363625
  67. Pass 16, Batch 200, Cost 5.347357
  68. Test 16, Cost 13.665516
  69. Pass 17, Batch 0, Cost 0.994224
  70. Pass 17, Batch 100, Cost 0.298174
  71. Pass 17, Batch 200, Cost 140.061615
  72. Test 17, Cost 13.568394
  73. Pass 18, Batch 0, Cost 11.832894
  74. Pass 18, Batch 100, Cost 8.340067
  75. Pass 18, Batch 200, Cost 30.967430
  76. Test 18, Cost 13.465723
  77. Pass 19, Batch 0, Cost 15.379287
  78. Pass 19, Batch 100, Cost 123.313614
  79. Pass 19, Batch 200, Cost 36.328705
  80. Test 19, Cost 13.377999
  81. Pass 20, Batch 0, Cost 12.842525
  82. Pass 20, Batch 100, Cost 54.218903
  83. Pass 20, Batch 200, Cost 18.377592
  84. Test 20, Cost 13.266518
  85. Pass 21, Batch 0, Cost 49.386784
  86. Pass 21, Batch 100, Cost 215.253906
  87. Pass 21, Batch 200, Cost 0.260682
  88. Test 21, Cost 13.237288
  89. Pass 22, Batch 0, Cost 469.974213
  90. Pass 22, Batch 100, Cost 8.073731
  91. Pass 22, Batch 200, Cost 0.810365
  92. Test 22, Cost 13.192008
  93. Pass 23, Batch 0, Cost 145.341141
  94. Pass 23, Batch 100, Cost 15.787022
  95. Pass 23, Batch 200, Cost 4.965213
  96. Test 23, Cost 13.133022
  97. Pass 24, Batch 0, Cost 10.377566
  98. Pass 24, Batch 100, Cost 3.863908
  99. Pass 24, Batch 200, Cost 15.857657
  100. Test 24, Cost 13.113067
  101. Pass 25, Batch 0, Cost 6.239013
  102. Pass 25, Batch 100, Cost 15.914387
  103. Pass 25, Batch 200, Cost 48.752701
  104. Test 25, Cost 13.137239
  105. Pass 26, Batch 0, Cost 57.843086
  106. Pass 26, Batch 100, Cost 0.732344
  107. Pass 26, Batch 200, Cost 48.501846
  108. Test 26, Cost 13.141359
  109. Pass 27, Batch 0, Cost 443.271545
  110. Pass 27, Batch 100, Cost 227.696655
  111. Pass 27, Batch 200, Cost 1.482114
  112. Test 27, Cost 13.094058
  113. Pass 28, Batch 0, Cost 11.784382
  114. Pass 28, Batch 100, Cost 1.334578
  115. Pass 28, Batch 200, Cost 16.487831
  116. Test 28, Cost 13.122105
  117. Pass 29, Batch 0, Cost 10.043719
  118. Pass 29, Batch 100, Cost 26.890572
  119. Pass 29, Batch 200, Cost 11.034937
  120. Test 29, Cost 13.203439
  121. label=8.5, predict=11.7476
  122. label=5.0, predict=13.6822
  123. label=11.9, predict=10.7325
  124. label=27.9, predict=18.0696
  125. label=17.2, predict=13.0193

房价预测:

  1. #-*- coding:utf-8 -*-
  2. import sys
  3. reload(sys)
  4. sys.setdefaultencoding("utf-8")
  5. import paddle.v2 as paddle
  6.  
  7. # Initialize PaddlePaddle.
  8. paddle.init(use_gpu=False, trainer_count=1)
  9.  
  10. # Configure the neural network.
  11. x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
  12. y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
  13.  
  14. # Infer using provided test data.
  15. probs = paddle.infer(
  16. output_layer=y_predict,
  17. parameters=paddle.dataset.uci_housing.model(),
  18. input=[item for item in paddle.dataset.uci_housing.test()()])
  19.  
  20. for i in xrange(len(probs)):
  21. print 'Predicted price: ${:,.2f}'.format(probs[i][0] * 1000)

运行结果

  1. Predicted price: $12,316.63
  2. Predicted price: $13,830.34
  3. Predicted price: $11,499.34
  4. Predicted price: $17,395.05
  5. Predicted price: $13,317.67
  6. Predicted price: $16,834.08
  7. Predicted price: $16,632.04
  8. Predicted price: $15,384.20
  9. Predicted price: $7,697.38
  10. Predicted price: $13,657.83
  11. Predicted price: $6,329.62
  12. Predicted price: $12,153.18
  13. Predicted price: $13,890.60
  14. Predicted price: $11,367.41
  15. Predicted price: $13,269.13
  16. Predicted price: $14,979.35
  17. Predicted price: $17,539.03
  18. Predicted price: $16,686.41
  19. Predicted price: $16,810.74
  20. Predicted price: $13,620.53
  21. Predicted price: $14,720.09
  22. Predicted price: $12,533.42
  23. Predicted price: $15,835.49
  24. Predicted price: $16,064.76
  25. Predicted price: $14,566.97
  26. Predicted price: $13,783.11
  27. Predicted price: $16,211.73
  28. Predicted price: $16,362.79
  29. Predicted price: $18,183.92
  30. Predicted price: $16,298.03
  31. Predicted price: $16,084.58
  32. Predicted price: $14,406.07
  33. Predicted price: $15,309.62
  34. Predicted price: $12,104.60
  35. Predicted price: $9,865.44
  36. Predicted price: $14,116.36
  37. Predicted price: $14,552.37
  38. Predicted price: $16,381.32
  39. Predicted price: $16,992.90
  40. Predicted price: $16,722.93
  41. Predicted price: $13,468.48
  42. Predicted price: $13,622.97
  43. Predicted price: $16,512.31
  44. Predicted price: $17,004.60
  45. Predicted price: $16,492.97
  46. Predicted price: $16,179.70
  47. Predicted price: $15,989.17
  48. Predicted price: $17,289.17
  49. Predicted price: $16,975.07
  50. Predicted price: $18,950.22
  51. Predicted price: $15,513.54
  52. Predicted price: $15,652.08
  53. Predicted price: $14,162.51
  54. Predicted price: $14,665.31
  55. Predicted price: $16,724.47
  56. Predicted price: $17,369.51
  57. Predicted price: $17,330.55
  58. Predicted price: $17,923.71
  59. Predicted price: $18,018.71
  60. Predicted price: $19,392.96
  61. Predicted price: $18,379.00
  62. Predicted price: $17,187.61
  63. Predicted price: $14,920.71
  64. Predicted price: $15,435.08
  65. Predicted price: $16,458.07
  66. Predicted price: $17,390.93
  67. Predicted price: $17,520.05
  68. Predicted price: $18,763.72
  69. Predicted price: $18,698.70
  70. Predicted price: $20,425.67
  71. Predicted price: $15,431.77
  72. Predicted price: $14,803.56
  73. Predicted price: $17,336.69
  74. Predicted price: $13,052.34
  75. Predicted price: $16,874.23
  76. Predicted price: $18,547.62
  77. Predicted price: $19,574.30
  78. Predicted price: $21,303.89
  79. Predicted price: $22,053.60
  80. Predicted price: $18,862.40
  81. Predicted price: $17,969.15
  82. Predicted price: $19,496.96
  83. Predicted price: $17,676.56
  84. Predicted price: $18,699.87
  85. Predicted price: $14,520.48
  86. Predicted price: $12,410.05
  87. Predicted price: $9,987.12
  88. Predicted price: $15,381.11
  89. Predicted price: $16,906.17
  90. Predicted price: $21,538.57
  91. Predicted price: $21,566.74
  92. Predicted price: $19,905.33
  93. Predicted price: $17,938.98
  94. Predicted price: $20,776.08
  95. Predicted price: $21,715.28
  96. Predicted price: $20,169.60
  97. Predicted price: $21,148.05
  98. Predicted price: $22,589.09
  99. Predicted price: $21,913.31
  100. Predicted price: $24,388.41
  101. Predicted price: $23,748.72
  102. Predicted price: $22,013.94

来源:paddlepaddle官网、以上代码对应的视频讲解地址

paddlepaddle使用(一)的更多相关文章

  1. Install PaddlePaddle (Parallel Distributed Deep Learning)

    Step 1: Install docker on your linux system (My linux is fedora) https://docs.docker.com/engine/inst ...

  2. 【深度学习系列】PaddlePaddle之手写数字识别

    上周在搜索关于深度学习分布式运行方式的资料时,无意间搜到了paddlepaddle,发现这个框架的分布式训练方案做的还挺不错的,想跟大家分享一下.不过呢,这块内容太复杂了,所以就简单的介绍一下padd ...

  3. 【深度学习系列】PaddlePaddle之数据预处理

    上篇文章讲了卷积神经网络的基本知识,本来这篇文章准备继续深入讲CNN的相关知识和手写CNN,但是有很多同学跟我发邮件或私信问我关于PaddlePaddle如何读取数据.做数据预处理相关的内容.网上看的 ...

  4. 【深度学习系列】用PaddlePaddle和Tensorflow进行图像分类

    上个月发布了四篇文章,主要讲了深度学习中的"hello world"----mnist图像识别,以及卷积神经网络的原理详解,包括基本原理.自己手写CNN和paddlepaddle的 ...

  5. 【深度学习系列】用PaddlePaddle和Tensorflow实现AlexNet

    上周我们用PaddlePaddle和Tensorflow实现了图像分类,分别用自己手写的一个简单的CNN网络simple_cnn和LeNet-5的CNN网络识别cifar-10数据集.在上周的实验表现 ...

  6. 【深度学习系列】用PaddlePaddle和Tensorflow实现经典CNN网络Vgg

    上周我们讲了经典CNN网络AlexNet对图像分类的效果,2014年,在AlexNet出来的两年后,牛津大学提出了Vgg网络,并在ILSVRC 2014中的classification项目的比赛中取得 ...

  7. 【深度学习系列】用PaddlePaddle和Tensorflow实现经典CNN网络GoogLeNet

    前面讲了LeNet.AlexNet和Vgg,这周来讲讲GoogLeNet.GoogLeNet是由google的Christian Szegedy等人在2014年的论文<Going Deeper ...

  8. 【深度学习系列】用PaddlePaddle和Tensorflow实现GoogLeNet InceptionV2/V3/V4

    上一篇文章我们引出了GoogLeNet InceptionV1的网络结构,这篇文章中我们会详细讲到Inception V2/V3/V4的发展历程以及它们的网络结构和亮点. GoogLeNet Ince ...

  9. 【深度学习系列】一起来参加百度 PaddlePaddle AI 大赛吧!

    写这个系列写了两个月了,对paddlepaddle的使用和越来越熟悉,不过一直没找到合适的应用场景.最近百度搞了个AI大赛,据说有四个赛题,现在是第一个----综艺节目精彩片段预测 ,大家可以去检测一 ...

  10. 【深度学习系列】关于PaddlePaddle的一些避“坑”技巧

    最近除了工作以外,业余在参加Paddle的AI比赛,在用Paddle训练的过程中遇到了一些问题,并找到了解决方法,跟大家分享一下: PaddlePaddle的Anaconda的兼容问题 之前我是在服务 ...

随机推荐

  1. 1) Spring_HelloWorld

    1. Spring Tool Suite™ 方式一:下载对应eclipse版本的文件,离线安装 4.4.2 springsource-tool-suite-3.6.4.RELEASE-e4.4.2-u ...

  2. SPATIALINDEX_LIBRARY Cmake

    https://libspatialindex.org/ QGIS:https://github.com/qgis/QGIS/blob/master/cmake/FindSpatialindex.cm ...

  3. 几个CSS-content的小例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Codeforces777D Cloud of Hashtags 2017-05-04 18:06 67人阅读 评论(0) 收藏

    D. Cloud of Hashtags time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces758A Holiday Of Equality 2017-01-20 10:08 48人阅读 评论(0) 收藏

    A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. 几个经典的数学库之一学习---VCGlib(3)

    Camera and shot abstraction for managing views 视图的定义,以及mesh的操作说明. Shot(镜头) and camera(相机) shot摄像结构以及 ...

  7. centos7 磁盘管理—— lvm的使用

    Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不 ...

  8. Android开发基于百度地图的乘车助手

    写在前面: 出去玩免不了挤公交.等地铁,不知道乘车方案当然不行,用官方APP吧,缺点一大堆,手机浏览器在线查的话既慢又麻烦...为了解决这些问题,我们来做一个简版的出行助手,嘛嘛再也不用担心我会迷路了 ...

  9. 转(C# 实现生产者消费者队列)

    class Program { // 任务队列 static Queue<string> _tasks = new Queue<string>(); // 为保证线程安全,使用 ...

  10. Win(Phone)10开发第(4)弹,HTTP 实时流播放 m3u8

    其实这篇只有一句话,win10原生支持HLS啦 1 2 3 AdaptiveMediaSourceCreationResult amsResult = await AdaptiveMediaSourc ...