三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 386|回复: 0
打印 上一主题 下一主题

线性回归

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-18 07:42:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # @author: 陈水平
  4. # @date: 2016-12-30
  5. # @description: compare scikit-learn and tensorflow, using linear regression data from deep learning course by Andrew Ng.
  6. # @ref: http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html

  7. import tensorflow as tf
  8. import numpy as np
  9. from sklearn import linear_model

  10. # Read x and y
  11. x_data = np.loadtxt("ex2x.dat")
  12. y_data = np.loadtxt("ex2y.dat")


  13. # We use scikit-learn first to get a sense of the coefficients
  14. reg = linear_model.LinearRegression()
  15. reg.fit(x_data.reshape(-1, 1), y_data)

  16. print "Coefficient of scikit-learn linear regression: k=%f, b=%f" % (reg.coef_, reg.intercept_)


  17. # Then we apply tensorflow to achieve the similar results
  18. # The structure of tensorflow code can be divided into two parts:

  19. # First part: set up computation graph
  20. W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  21. b = tf.Variable(tf.zeros([1]))
  22. y = W * x_data + b

  23. loss = tf.reduce_mean(tf.square(y - y_data)) / 2
  24. optimizer = tf.train.GradientDescentOptimizer(0.07)  # Try 0.1 and you will see unconvergency
  25. train = optimizer.minimize(loss)

  26. init = tf.initialize_all_variables()

  27. # Second part: launch the graph
  28. sess = tf.Session()
  29. sess.run(init)

  30. for step in range(1500):
  31.     sess.run(train)
  32.     if step % 100 == 0:
  33.         print step, sess.run(W), sess.run(b)
  34. print "Coeeficient of tensorflow linear regression: k=%f, b=%f" % (sess.run(W), sess.run(b))
复制代码
输出如下:
  1. Coefficient of scikit-learn linear regression: k=0.063881, b=0.750163
  2. 0 [ 0.45234478] [ 0.10217379]
  3. 100 [ 0.13166969] [ 0.4169243]
  4. 200 [ 0.09332827] [ 0.58935112]
  5. 300 [ 0.07795752] [ 0.67282093]
  6. 400 [ 0.07064758] [ 0.71297228]
  7. 500 [ 0.06713474] [ 0.73227954]
  8. 600 [ 0.06544565] [ 0.74156356]
  9. 700 [ 0.06463348] [ 0.74602771]
  10. 800 [ 0.06424291] [ 0.74817437]
  11. 900 [ 0.06405514] [ 0.74920654]
  12. 1000 [ 0.06396478] [ 0.74970293]
  13. 1100 [ 0.06392141] [ 0.74994141]
  14. 1200 [ 0.06390052] [ 0.75005609]
  15. 1300 [ 0.06389045] [ 0.7501114]
  16. 1400 [ 0.0638856] [ 0.75013816]
  17. Coeeficient of tensorflow linear regression: k=0.063883, b=0.750151
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-5-10 00:12 , Processed in 0.026837 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表