三木社区

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

逻辑回归

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-18 07:44:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # @author: 陈水平
  4. # @date: 2017-01-04
  5. # @description: compare the logistics regression of tensorflow with sklearn based on the exercise of deep learning course of Andrew Ng.
  6. # @ref: http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex4/ex4.html

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

  11. # Read x and y
  12. x_data = np.loadtxt("ex4x.dat").astype(np.float32)
  13. y_data = np.loadtxt("ex4y.dat").astype(np.float32)

  14. scaler = preprocessing.StandardScaler().fit(x_data)
  15. x_data_standard = scaler.transform(x_data)

  16. # We evaluate the x and y by sklearn to get a sense of the coefficients.
  17. reg = LogisticRegression(C=999999999, solver="newton-cg")  # Set C as a large positive number to minimize the regularization effect
  18. reg.fit(x_data, y_data)
  19. print "Coefficients of sklearn: K=%s, b=%f" % (reg.coef_, reg.intercept_)

  20. # Now we use tensorflow to get similar results.
  21. W = tf.Variable(tf.zeros([2, 1]))
  22. b = tf.Variable(tf.zeros([1, 1]))
  23. y = 1 / (1 + tf.exp(-tf.matmul(x_data_standard, W) + b))
  24. loss = tf.reduce_mean(- y_data.reshape(-1, 1) *  tf.log(y) - (1 - y_data.reshape(-1, 1)) * tf.log(1 - y))

  25. optimizer = tf.train.GradientDescentOptimizer(1.3)
  26. train = optimizer.minimize(loss)

  27. init = tf.initialize_all_variables()

  28. sess = tf.Session()
  29. sess.run(init)
  30. for step in range(100):
  31.     sess.run(train)
  32.     if step % 10 == 0:
  33.         print step, sess.run(W).flatten(), sess.run(b).flatten()

  34. print "Coefficients of tensorflow (input should be standardized): K=%s, b=%s" % (sess.run(W).flatten(), sess.run(b).flatten())
  35. print "Coefficients of tensorflow (raw input): K=%s, b=%s" % (sess.run(W).flatten() / scaler.scale_, sess.run(b).flatten() - np.dot(scaler.mean_ / scaler.scale_, sess.run(W)))


  36. # Problem solved and we are happy. But...
  37. # I'd like to implement the logistic regression from a multi-class viewpoint instead of binary.
  38. # In machine learning domain, it is called softmax regression
  39. # In economic and statistics domain, it is called multinomial logit (MNL) model, proposed by Daniel McFadden, who shared the 2000  Nobel Memorial Prize in Economic Sciences.

  40. print "------------------------------------------------"
  41. print "We solve this binary classification problem again from the viewpoint of multinomial classification"
  42. print "------------------------------------------------"

  43. # As a tradition, sklearn first
  44. reg = LogisticRegression(C=9999999999, solver="newton-cg", multi_class="multinomial")
  45. reg.fit(x_data, y_data)
  46. print "Coefficients of sklearn: K=%s, b=%f" % (reg.coef_, reg.intercept_)
  47. print "A little bit difference at first glance. What about multiply them with 2?"

  48. # Then try tensorflow
  49. W = tf.Variable(tf.zeros([2, 2]))  # first 2 is feature number, second 2 is class number
  50. b = tf.Variable(tf.zeros([1, 2]))
  51. V = tf.matmul(x_data_standard, W) + b
  52. y = tf.nn.softmax(V)  # tensorflow provide a utility function to calculate the probability of observer n choose alternative i, you can replace it with `y = tf.exp(V) / tf.reduce_sum(tf.exp(V), keep_dims=True, reduction_indices=[1])`

  53. # Encode the y label in one-hot manner
  54. lb = preprocessing.LabelBinarizer()
  55. lb.fit(y_data)
  56. y_data_trans = lb.transform(y_data)
  57. y_data_trans = np.concatenate((1 - y_data_trans, y_data_trans), axis=1)  # Only necessary for binary class

  58. loss = tf.reduce_mean(-tf.reduce_sum(y_data_trans * tf.log(y), reduction_indices=[1]))
  59. optimizer = tf.train.GradientDescentOptimizer(1.3)
  60. train = optimizer.minimize(loss)

  61. init = tf.initialize_all_variables()

  62. sess = tf.Session()
  63. sess.run(init)
  64. for step in range(100):
  65.     sess.run(train)
  66.     if step % 10 == 0:
  67.         print step, sess.run(W).flatten(), sess.run(b).flatten()

  68. print "Coefficients of tensorflow (input should be standardized): K=%s, b=%s" % (sess.run(W).flatten(), sess.run(b).flatten())
  69. print "Coefficients of tensorflow (raw input): K=%s, b=%s" % ((sess.run(W) / scaler.scale_).flatten(),  sess.run(b).flatten() - np.dot(scaler.mean_ / scaler.scale_, sess.run(W)))
复制代码
输出如下:
  1. Coefficients of sklearn: K=[[ 0.14834077  0.15890845]], b=-16.378743
  2. 0 [ 0.33699557  0.34786162] [ -4.84287721e-09]
  3. 10 [ 1.15830743  1.22841871] [ 0.02142336]
  4. 20 [ 1.3378191   1.42655993] [ 0.03946959]
  5. 30 [ 1.40735555  1.50197577] [ 0.04853692]
  6. 40 [ 1.43754184  1.53418231] [ 0.05283691]
  7. 50 [ 1.45117068  1.54856908] [ 0.05484771]
  8. 60 [ 1.45742035  1.55512536] [ 0.05578374]
  9. 70 [ 1.46030474  1.55814099] [ 0.05621871]
  10. 80 [ 1.46163988  1.55953443] [ 0.05642065]
  11. 90 [ 1.46225858  1.56017959] [ 0.0565144]
  12. Coefficients of tensorflow (input should be standardized): K=[ 1.46252561  1.56045783], b=[ 0.05655487]
  13. Coefficients of tensorflow (raw input): K=[ 0.14831361  0.15888004], b=[-16.26265144]
  14. ------------------------------------------------
  15. We solve this binary classification problem again from the viewpoint of multinomial classification
  16. ------------------------------------------------
  17. Coefficients of sklearn: K=[[ 0.07417039  0.07945423]], b=-8.189372
  18. A little bit difference at first glance. What about multiply them with 2?
  19. 0 [-0.33699557  0.33699557 -0.34786162  0.34786162] [  6.05359674e-09  -6.05359674e-09]
  20. 10 [-0.68416572  0.68416572 -0.72988117  0.72988123] [ 0.02157043 -0.02157041]
  21. 20 [-0.72234094  0.72234106 -0.77087188  0.77087194] [ 0.02693938 -0.02693932]
  22. 30 [-0.72958517  0.72958535 -0.7784785   0.77847856] [ 0.02802362 -0.02802352]
  23. 40 [-0.73103166  0.73103184 -0.77998811  0.77998811] [ 0.02824244 -0.02824241]
  24. 50 [-0.73132294  0.73132324 -0.78029168  0.78029174] [ 0.02828659 -0.02828649]
  25. 60 [-0.73138171  0.73138207 -0.78035289  0.78035301] [ 0.02829553 -0.02829544]
  26. 70 [-0.73139352  0.73139393 -0.78036523  0.78036535] [ 0.02829732 -0.0282972 ]
  27. 80 [-0.73139596  0.73139632 -0.78036767  0.78036791] [ 0.02829764 -0.02829755]
  28. 90 [-0.73139644  0.73139679 -0.78036815  0.78036839] [ 0.02829781 -0.02829765]
  29. Coefficients of tensorflow (input should be standardized): K=[-0.7313965   0.73139679 -0.78036827  0.78036839], b=[ 0.02829777 -0.02829769]
  30. Coefficients of tensorflow (raw input): K=[-0.07417037  0.07446811 -0.07913655  0.07945422], b=[ 8.1893692  -8.18937111]
复制代码


回复

使用道具 举报

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

辽公网安备 21021702000620号

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

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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