Deeplearning Algorithms tutorial

谷歌的人工智能位于全球前列,在图像识别、语音识别、无人驾驶等技术上都已经落地。而百度实质意义上扛起了国内的人工智能的大旗,覆盖无人驾驶、智能助手、图像识别等许多层面。苹果业已开始全面拥抱机器学习,新产品进军家庭智能音箱并打造工作站级别Mac。另外,腾讯的深度学习平台Mariana已支持了微信语音识别的语音输入法、语音开放平台、长按语音消息转文本等产品,在微信图像识别中开始应用。全球前十大科技公司全部发力人工智能理论研究和应用的实现,虽然入门艰难,但是一旦入门,高手也就在你的不远处! AI的开发离不开算法那我们就接下来开始学习算法吧! 回归方法是对数值型连续随机变量进行预测和建模的监督学习算法。其特点是标注的数据集具有数值型的目标变量。回归的目的是预测数值型的目标值。

常用的回归方法包括:

  • 线性回归:使用超平面拟合数据集
  • 最近邻算法:通过搜寻最相似的训练样本来预测新样本的值
  • 决策树和回归树:将数据集分割为不同分支而实现分层学习
  • 集成方法:组合多个弱学习算法构造一种强学习算法,如随机森林(RF)和梯度提升树(GBM)等
  • 深度学习:使用多层神经网络学习复杂模型

线性回归

线性回归是最简单的回归方法,它的目标是使用超平面拟合数据集,即学习一个线性模型以尽可能准确的预测实值输出标记。 线性回归是利用数理统计中回归分析,来确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法,运用十分广泛。其表达形式为y = w’x+e,e为误差服从均值为0的正态分布。 回归分析中,只包括一个自变量和一个因变量,且二者的关系可用一条直线近似表示,这种回归分析称为一元线性回归分析。如果回归分析中包括两个或两个以上的自变量,且因变量和自变量之间是线性关系,则称为多元线性回归分析。

单变量模型

模型

线性回归 - 图1

线性模型(linear model) 简单, 易于建模, 但却蕴含着机器学习的重要思想.由于w直观地表达了各属性在预测中的重要性, 所以线性模型有着很好的可解释性(comprehensibility). 目标函数(最小二乘参数估计)

线性回归 - 图2

多变量模型

多变量时可以表示为矩阵

线性回归 - 图3

目标函数为

线性回归 - 图4

当$X^TX$为满秩矩阵时,可以得到最优解

线性回归 - 图5

注意:

1.多变量时需要对输入数据作归一化,如线性回归 - 图6

2.实际计算过程中(如使用梯度下降算法)学习率的选择方法

  • 收敛慢时,增大学习率
  • 不收敛时,减小学习率
  • 学习率的选择范围一般为…, 0.001, 0.01, 0.1, 1, 10, …

代码例子

  1. #Indicate the matplotlib to show the graphics inline
  2. %matplotlib inline
  3. import matplotlib.pyplot as plt # import matplotlib
  4. import numpy as np # import numpy
  5. import tensorflow as tf
  6. import numpy as np
  7. trX = np.linspace(-1, 1, 101) #Create a linear space of 101 points between 1 and 1
  8. trY = 2 * trX + np.random.randn(*trX.shape) * 0.4 + 0.2 #Create The y function based on the x axis
  9. plt.figure() # Create a new figure
  10. plt.scatter(trX,trY) #Plot a scatter draw of the random datapoints
  11. plt.plot (trX, .2 + 2 * trX) # Draw one line with the line function

线性回归 - 图7

单变量示例

线性回归 - 图8

  1. #!/usr/bin/env python
  2. # h(X)= b + wX
  3. %matplotlib inline
  4. import tensorflow as tf
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. def model(X, w, b):
  8. return tf.mul(X, w) + b
  9. trX = np.linspace(-1, 1, 101).astype(np.float32)
  10. # create a y value which is approximately linear but with some random noise
  11. trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 + 10
  12. # create a shared variable (like theano.shared) for the weight matrix
  13. w = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  14. b = tf.Variable(tf.zeros([1]))
  15. cost = tf.reduce_mean(tf.square(trY-model(trX, w, b)))
  16. # construct an optimizer to minimize cost and fit line to my data
  17. train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
  18. # Launch the graph in a session
  19. with tf.Session() as sess:
  20. # you need to initialize variables (in this case just variable W)
  21. tf.initialize_all_variables().run()
  22. for i in range(1000):
  23. sess.run(train_op)
  24. print "w should be something around [2]: ", sess.run(w)
  25. print "b should be something around [10]:", sess.run(b)
  26. plt.plot(trX, trY, "ro", label="Orinal data")
  27. plt.plot(trX, w.eval()*trX + b.eval(), label="Fitted line")
  28. plt.legend()
  29. plt.show()
  30. # Plot with pandas
  31. #import pandas as pd
  32. #fig, axes = plt.subplots(nrows=1, ncols=1)
  33. #pd.DataFrame({'x':trX,'y':trY}).plot.scatter(x='x', y='y', ax=axes, color='red')
  34. #pd.DataFrame({'x':trX,'y':w.eval()*trX + b.eval()}).plot.scatter(x='x', y='y', ax=axes, color='blue')
  1. w should be something around [2]: [ 2.00981951]
  2. b should be something around [10]: [ 9.98865509]

线性回归 - 图9

多变量示例

多变量其实就是输入变成了矩阵:

  1. #!/usr/bin/env python
  2. # h(X)= B + WX
  3. %matplotlib inline
  4. import tensorflow as tf
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. def model(X, w, b):
  8. return tf.mul(w, X) + b
  9. trX = np.mgrid[-1:1:0.01, -10:10:0.1].reshape(2, -1).T
  10. trW = np.array([3, 5])
  11. trY = trW*trX + np.random.randn(*trX.shape) + [20, 100]
  12. w = tf.Variable(np.array([1., 1.]).astype(np.float32))
  13. b = tf.Variable(np.array([[1., 1.]]).astype(np.float32))
  14. cost = tf.reduce_mean(tf.square(trY-model(trX, w, b)))
  15. # construct an optimizer to minimize cost and fit line to my data
  16. train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
  17. # Launch the graph in a session
  18. with tf.Session() as sess:
  19. # you need to initialize variables (in this case just variable W)
  20. tf.initialize_all_variables().run()
  21. for i in range(1000):
  22. if i % 99 == 0:
  23. print "Cost at step", i, "is:", cost.eval()
  24. sess.run(train_op)
  25. print "w should be something around [3, 5]: ", sess.run(w)
  26. print "b should be something around [20,100]:", sess.run(b)
  27. Cost at step 0 is: 5329.87
  28. Cost at step 99 is: 1.22204
  29. Cost at step 198 is: 0.998043
  30. Cost at step 297 is: 0.997083
  31. Cost at step 396 is: 0.997049
  32. Cost at step 495 is: 0.997049
  33. Cost at step 594 is: 0.997049
  34. Cost at step 693 is: 0.997049
  35. Cost at step 792 is: 0.997049
  36. Cost at step 891 is: 0.997049
  37. Cost at step 990 is: 0.997049
  38. w should be something around [3, 5]: [ 3.00108743 5.00054932]
  39. b should be something around [20,100]: [[ 20.00317383 100.00382233]]
  40. tensorflow示例
  41. import tensorflow as tf
  42. # initialize variables/model parameters
  43. W = tf.Variable(tf.zeros([2, 1]), name="weights")
  44. b = tf.Variable(0., name="bias")
  45. def inference(X):
  46. # compute inference model over data X and return the result
  47. return tf.matmul(X, W) + b
  48. def loss(X, Y):
  49. # compute loss over training data X and expected outputs Y
  50. Y_predicted = inference(X)
  51. return tf.reduce_sum(tf.squared_difference(Y, Y_predicted))
  52. def inputs():
  53. # read/generate input training data X and expected outputs Y
  54. weight_age = [[84, 46], [73, 20], [65, 52], [70, 30], [76, 57], [69, 25], [63, 28], [72, 36], [79, 57], [75, 44], [27, 24], [89, 31], [65, 52], [57, 23], [59, 60], [69, 48], [60, 34], [79, 51], [75, 50], [82, 34], [59, 46], [67, 23], [85, 37], [55, 40], [63, 30]]
  55. blood_fat_content = [354, 190, 405, 263, 451, 302, 288, 385, 402, 365, 209, 290, 346, 254, 395, 434, 220, 374, 308, 220, 311, 181, 274, 303, 244]
  56. return tf.to_float(weight_age), tf.to_float(blood_fat_content)
  57. def train(total_loss):
  58. # train / adjust model parameters according to computed total loss
  59. learning_rate = 0.000001
  60. return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)
  61. def evaluate(sess, X, Y):
  62. # evaluate the resulting trained model
  63. print sess.run(inference([[80., 25.]])) # ~ 303
  64. print sess.run(inference([[65., 25.]])) # ~ 256
  65. # Create a saver.
  66. # saver = tf.train.Saver()
  67. # Launch the graph in a session, setup boilerplate
  68. with tf.Session() as sess:
  69. tf.initialize_all_variables().run()
  70. X, Y = inputs()
  71. total_loss = loss(X, Y)
  72. train_op = train(total_loss)
  73. coord = tf.train.Coordinator()
  74. threads = tf.train.start_queue_runners(sess=sess, coord=coord)
  75. # actual training loop
  76. training_steps = 1000
  77. for step in range(training_steps):
  78. sess.run([train_op])
  79. # for debugging and learning purposes, see how the loss gets decremented
  80. # through training steps
  81. if step % 10 == 0:
  82. print "loss at step ", step, ":", sess.run([total_loss])
  83. # save training checkpoints in case loosing them
  84. # if step % 1000 == 0:
  85. # saver.save(sess, 'my-model', global_step=step)
  86. evaluate(sess, X, Y)
  87. coord.request_stop()
  88. coord.join(threads)
  89. # saver.save(sess, 'my-model', global_step=training_steps)

输出:

  1. loss at step 0 : [54929292.0]
  2. loss at step 10 : [14629748.0]
  3. loss at step 20 : [7090800.0]
  4. loss at step 30 : [5680201.0]
  5. loss at step 40 : [5416011.0]
  6. loss at step 50 : [5366280.5]
  7. loss at step 60 : [5356678.0]
  8. loss at step 70 : [5354588.0]
  9. loss at step 80 : [5353913.0]
  10. loss at step 90 : [5353510.0]
  11. loss at step 100 : [5353166.0]
  12. loss at step 110 : [5352839.5]
  13. loss at step 120 : [5352525.0]
  14. loss at step 130 : [5352218.5]
  15. loss at step 140 : [5351921.0]
  16. loss at step 150 : [5351631.5]
  17. loss at step 160 : [5351349.0]
  18. loss at step 170 : [5351075.0]
  19. loss at step 180 : [5350808.0]
  20. loss at step 190 : [5350549.5]
  21. loss at step 200 : [5350297.0]
  22. loss at step 210 : [5350050.5]
  23. loss at step 220 : [5349814.0]
  24. loss at step 230 : [5349580.5]
  25. loss at step 240 : [5349356.0]
  26. loss at step 250 : [5349134.0]
  27. loss at step 260 : [5348922.0]
  28. loss at step 270 : [5348712.5]
  29. loss at step 280 : [5348511.5]
  30. loss at step 290 : [5348313.5]
  31. loss at step 300 : [5348123.5]
  32. loss at step 310 : [5347935.0]
  33. loss at step 320 : [5347753.5]
  34. loss at step 330 : [5347577.5]
  35. loss at step 340 : [5347405.0]
  36. loss at step 350 : [5347237.0]
  37. loss at step 360 : [5347073.0]
  38. loss at step 370 : [5346915.0]
  39. loss at step 380 : [5346761.0]
  40. loss at step 390 : [5346611.0]
  41. loss at step 400 : [5346464.5]
  42. loss at step 410 : [5346320.5]
  43. loss at step 420 : [5346182.5]
  44. loss at step 430 : [5346047.5]
  45. loss at step 440 : [5345914.0]
  46. loss at step 450 : [5345786.0]
  47. loss at step 460 : [5345662.0]
  48. loss at step 470 : [5345539.5]
  49. loss at step 480 : [5345420.5]
  50. loss at step 490 : [5345305.5]
  51. loss at step 500 : [5345193.0]
  52. loss at step 510 : [5345082.5]
  53. loss at step 520 : [5344976.5]
  54. loss at step 530 : [5344871.0]
  55. loss at step 540 : [5344771.0]
  56. loss at step 550 : [5344670.5]
  57. loss at step 560 : [5344574.5]
  58. loss at step 570 : [5344480.5]
  59. loss at step 580 : [5344388.0]
  60. loss at step 590 : [5344298.0]
  61. loss at step 600 : [5344212.0]
  62. loss at step 610 : [5344127.0]
  63. loss at step 620 : [5344042.5]
  64. loss at step 630 : [5343962.0]
  65. loss at step 640 : [5343882.0]
  66. loss at step 650 : [5343805.5]
  67. loss at step 660 : [5343729.5]
  68. loss at step 670 : [5343657.0]
  69. loss at step 680 : [5343584.0]
  70. loss at step 690 : [5343514.5]
  71. loss at step 700 : [5343446.5]
  72. loss at step 710 : [5343380.0]
  73. loss at step 720 : [5343314.5]
  74. loss at step 730 : [5343250.0]
  75. loss at step 740 : [5343187.5]
  76. loss at step 750 : [5343128.0]
  77. loss at step 760 : [5343067.5]
  78. loss at step 770 : [5343010.5]
  79. loss at step 780 : [5342952.5]
  80. loss at step 790 : [5342897.5]
  81. loss at step 800 : [5342843.0]
  82. loss at step 810 : [5342791.5]
  83. loss at step 820 : [5342738.5]
  84. loss at step 830 : [5342688.5]
  85. loss at step 840 : [5342638.5]
  86. loss at step 850 : [5342589.5]
  87. loss at step 860 : [5342543.0]
  88. loss at step 870 : [5342496.5]
  89. loss at step 880 : [5342449.5]
  90. loss at step 890 : [5342406.0]
  91. loss at step 900 : [5342363.0]
  92. loss at step 910 : [5342319.5]
  93. loss at step 920 : [5342277.5]
  94. loss at step 930 : [5342236.0]
  95. loss at step 940 : [5342197.5]
  96. loss at step 950 : [5342157.0]
  97. loss at step 960 : [5342118.5]
  98. loss at step 970 : [5342080.5]
  99. loss at step 980 : [5342043.0]
  100. loss at step 990 : [5342007.5]
  101. [[ 318.77984619]]
  102. [[ 266.52853394]]

sklearn示例

  1. import tensorflow.contrib.learn.python.learn as learn
  2. from sklearn import datasets, metrics, preprocessing
  3. boston = datasets.load_boston()
  4. x = preprocessing.StandardScaler().fit_transform(boston.data)
  5. feature_columns = learn.infer_real_valued_columns_from_input(x)
  6. regressor = learn.LinearRegressor(feature_columns=feature_columns)
  7. regressor.fit(x, boston.target, steps=200, batch_size=32)
  8. boston_predictions = list(regressor.predict(x, as_iterable=True))
  9. score = metrics.mean_squared_error(boston_predictions, boston.target)
  10. print ("MSE: %f" % score)