ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

优达学城深度学习之三(上)——卷积神经网络

2021-06-01 16:52:46  阅读:251  来源: 互联网

标签:误差 感知器 卷积 outputs 优达 test output correct 学城


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

   学习如何用神经网络来解决分类问题。

    开始都会说什么是机器学习?机器学习的应用是什么?用机器在海量数据中学习得到可以解决一类问题的办法,这就是我的理解。图像处理、文本处理、无人驾驶、等,深度学习最热门的应用就是无人驾驶。而深度学习的核心是神经网络。神经网络就是模拟人的大脑工作。所以神经网络很重要、

    神经网络是机器学习中的一个模型,可以用于两类问题的解答:

  • 分类:把数据划分成不同的类别
  • 回归:建立数据间的连续关系

1、分类问题

录取与不录取的问题:

 

如何让电脑找到这条线呢? 所以接下来我们要学习找到这条线的算法。

2、线性界限月更高维度的界限

对于二维数据是线性界限。

对于更高维度的界限。需要平面,甚至曲面。

3、Logistics(对数概率)感知器

   感知器由两个节点构成,第一个是线性求和,第二个是对线性求和的结果进行阶跃,从而得到0、1

两种格式,一般是左边。

4、为何是神经网络

感知器的结果和人体大脑神经元很相似,这个感知器和人体大脑的神经元的链接方式类似。我们会连接这些感知器来创建神经网络,与大脑神经原连接方式很相似,都是一个神经元的输出作为一个神经元的输入。

5、作为Logistic(对数概率)运算符的感知器

    我们将见到感知器的很多强大应用之一。作为逻辑运算符!你将有机会为最常见的逻辑运算符创建感知器:ANDOR 和 NOT 运算符。然后,我们将看看如何处理比较难处理的 XOR 运算符。

 

AND 感知器的权重和偏差是什么?

将权重(weight1weight2)和偏差 bias 设为正确的值,以便如上所示地计算 AND 运算。


 
  1. import pandas as pd
  2. # TODO: Set weight1, weight2, and bias
  3. weight1 = 2.0
  4. weight2 = 2.0
  5. bias = -3.0
  6. # DON'T CHANGE ANYTHING BELOW
  7. # Inputs and outputs
  8. test_inputs = [( 0, 0), ( 0, 1), ( 1, 0), ( 1, 1)]
  9. correct_outputs = [ False, False, False, True]
  10. outputs = []
  11. # Generate and check output
  12. for test_input, correct_output in zip(test_inputs, correct_outputs):
  13. linear_combination = weight1 * test_input[ 0] + weight2 * test_input[ 1] + bias
  14. output = int(linear_combination >= 0)
  15. is_correct_string = 'Yes' if output == correct_output else 'No'
  16. outputs.append([test_input[ 0], test_input[ 1], linear_combination, output, is_correct_string])
  17. # Print output
  18. num_wrong = len([output[ 4] for output in outputs if output[ 4] == 'No'])
  19. output_frame = pd.DataFrame(outputs, columns=[ 'Input 1', ' Input 2', ' Linear Combination', ' Activation Output', ' Is Correct'])
  20. if not num_wrong:
  21. print( 'Nice! You got it all correct.\n')
  22. else:
  23. print( 'You got {} wrong. Keep trying!\n'.format(num_wrong))
  24. print(output_frame.to_string(index= False))
OR 感知器

    OR 感知器和 AND 感知器很相似。在下图中,OR 感知器和 AND 感知器的直线一样,只是直线往下移动了。你可以如何处理权重和/或偏差以实现这一效果?请使用下面的 AND 感知器来创建一个 OR 感知器。

 

 

NOT 感知器

和我们刚刚研究的其他感知器不一样,NOT 运算仅关心一个输入。如果输入是 1,则运算返回 0,如果输入是 0,则返回 1。感知器的其他输入被忽略了。

在此测验中,你将设置权重(weight1weight2)和偏差 bias,以便对第二个输入进行 NOT 运算,并忽略第一个输入。


 
  1. import pandas as pd
  2. # TODO: Set weight1, weight2, and bias
  3. weight1 = 0.0
  4. weight2 = -4.0
  5. bias = 3.0
  6. # DON'T CHANGE ANYTHING BELOW
  7. # Inputs and outputs
  8. test_inputs = [( 0, 0), ( 0, 1), ( 1, 0), ( 1, 1)]
  9. correct_outputs = [ True, False, True, False]
  10. outputs = []
  11. # Generate and check output
  12. for test_input, correct_output in zip(test_inputs, correct_outputs):
  13. linear_combination = weight1 * test_input[ 0] + weight2 * test_input[ 1] + bias
  14. output = int(linear_combination >= 0)
  15. is_correct_string = 'Yes' if output == correct_output else 'No'
  16. outputs.append([test_input[ 0], test_input[ 1], linear_combination, output, is_correct_string])
  17. # Print output
  18. num_wrong = len([output[ 4] for output in outputs if output[ 4] == 'No'])
  19. output_frame = pd.DataFrame(outputs, columns=[ 'Input 1', ' Input 2', ' Linear Combination', ' Activation Output', ' Is Correct'])
  20. if not num_wrong:
  21. print( 'Nice! You got it all correct.\n')
  22. else:
  23. print( 'You got {} wrong. Keep trying!\n'.format(num_wrong))
  24. print(output_frame.to_string(index= False))
XOR 感知器

测验:构建一个 XOR 多层感知器

现在我们使用 AND、NOT 和 OR 感知器构建一个多层感知器,以便创建 XOR 逻辑!

下面的神经网络包含三个感知器:A、B 和 C。最后一个 (AND) 已经提供给你了。神经网络的输入来自第一个节点。输出来自最后一个节点。

上面的多层感知器计算出 XOR。每个感知器都是 AND、OR 和 NOT 的逻辑运算。但是,感知器 A、B、C 和 D 并不表明它们的运算。在下面的测验中,请为四个感知器设置正确的运算,以便计算 XOR。

                                                       在 XOR 神经网络中为感知器设置运算

6、感知器技巧 - 计算机如何“学习”分类? 

     在上一部分,你使用你自己的逻辑和数学知识为某些最常见的逻辑运算符创建了感知器。 但是在现实生活中,除了这些非常简单的形式,我们人类是无法靠自己构建这些感知器函数,找到用于分类的曲线的。

     计算机如何根据我们人类给出的结果,来自己进行构建感知器函数。对于这一点,有一个非常棒的技巧会帮到我们。就是希望离我们越来越近,

介绍一个让直线离点近些的诀窍。

     上面就是去改变这条直线可是怎么改变呢?就是让原本是1的点归化为0的点,使得 w、b 都减去一个学习率,然后得到新直线,看是否会越来近。同理原本是0的点归为1的点,使得w,b都加上一个学习率。

     整个数据集中的每一个点都会把分类的结果提供给感知器(分类函数),并调整感知器。——这就是计算机在神经网络算法中,找寻最优感知器的原理。

    7、感知器算法

    掌握了感知器技巧后,我们就可以编写完整的感知器运算的算法了!

    该编写代码了!在此练习中,你将实现感知器算法以分类下面的数据(位于文件 data.csv 中)。

  

运行结果将绘出感知器算法给出的解决方案。它实际上会画出一组虚线,显示算法如何接近最佳解决方案(用黑色实线表示)。


 
  1. import numpy as np
  2. # Setting the random seed, feel free to change it and see different solutions.
  3. np.random.seed( 42)
  4. def stepFunction(t):
  5. if t >= 0:
  6. return 1
  7. return 0
  8. def prediction(X, W, b):
  9. return stepFunction((np.matmul(X,W)+b)[ 0])
  10. # TODO: Fill in the code below to implement the perceptron trick.
  11. # The function should receive as inputs the data X, the labels y,
  12. # the weights W (as an array), and the bias b,
  13. # update the weights and bias W, b, according to the perceptron algorithm,
  14. # and return W and b.
  15. def perceptronStep(X, y, W, b, learn_rate = 0.05):
  16. # Fill in code
  17. for i in range(len(X)):
  18. y_hat = prediction(X[i],W,b)
  19. if y[i]-y_hat == 1:
  20. W[ 0] += X[i][ 0]*learn_rate
  21. W[ 1] += X[i][ 1]*learn_rate
  22. b += learn_rate
  23. elif y[i]-y_hat == -1:
  24. W[ 0] -= X[i][ 0]*learn_rate
  25. W[ 1] -= X[i][ 1]*learn_rate
  26. b -= learn_rate
  27. return W, b
  28. # This function runs the perceptron algorithm repeatedly on the dataset,
  29. # and returns a few of the boundary lines obtained in the iterations,
  30. # for plotting purposes.
  31. # Feel free to play with the learning rate and the num_epochs,
  32. # and see your results plotted below.
  33. def trainPerceptronAlgorithm(X, y, learn_rate = 0.01, num_epochs = 25):
  34. x_min, x_max = min(X.T[ 0]), max(X.T[ 0])
  35. y_min, y_max = min(X.T[ 1]), max(X.T[ 1])
  36. W = np.array(np.random.rand( 2, 1))
  37. b = np.random.rand( 1)[ 0] + x_max
  38. # These are the solution lines that get plotted below.
  39. boundary_lines = []
  40. for i in range(num_epochs):
  41. # In each epoch, we apply the perceptron step.
  42. W, b = perceptronStep(X, y, W, b, learn_rate)
  43. boundary_lines.append((-W[ 0]/W[ 1], -b/W[ 1]))
  44. return boundary_lines

data.csv数据


 
  1. 0 .78051, -0 .063669,1
  2. 0 .28774,0 .29139,1
  3. 0 .40714,0 .17878,1
  4. 0 .2923,0 .4217,1
  5. 0 .50922,0 .35256,1
  6. 0 .27785,0 .10802,1
  7. 0 .27527,0 .33223,1
  8. 0 .43999,0 .31245,1
  9. 0 .33557,0 .42984,1
  10. 0 .23448,0 .24986,1
  11. 0 .0084492,0 .13658,1
  12. 0 .12419,0 .33595,1
  13. 0 .25644,0 .42624,1
  14. 0 .4591,0 .40426,1
  15. 0 .44547,0 .45117,1
  16. 0 .42218,0 .20118,1
  17. 0 .49563,0 .21445,1
  18. 0 .30848,0 .24306,1
  19. 0 .39707,0 .44438,1
  20. 0 .32945,0 .39217,1
  21. 0 .40739,0 .40271,1
  22. 0 .3106,0 .50702,1
  23. 0 .49638,0 .45384,1
  24. 0 .10073,0 .32053,1
  25. 0 .69907,0 .37307,1
  26. 0 .29767,0 .69648,1
  27. 0 .15099,0 .57341,1
  28. 0 .16427,0 .27759,1
  29. 0 .33259,0 .055964,1
  30. 0 .53741,0 .28637,1
  31. 0 .19503,0 .36879,1
  32. 0 .40278,0 .035148,1
  33. 0 .21296,0 .55169,1
  34. 0 .48447,0 .56991,1
  35. 0 .25476,0 .34596,1
  36. 0 .21726,0 .28641,1
  37. 0 .67078,0 .46538,1
  38. 0 .3815,0 .4622,1
  39. 0 .53838,0 .32774,1
  40. 0 .4849,0 .26071,1
  41. 0 .37095,0 .38809,1
  42. 0 .54527,0 .63911,1
  43. 0 .32149,0 .12007,1
  44. 0 .42216,0 .61666,1
  45. 0 .10194,0 .060408,1
  46. 0 .15254,0 .2168,1
  47. 0 .45558,0 .43769,1
  48. 0 .28488,0 .52142,1
  49. 0 .27633,0 .21264,1
  50. 0 .39748,0 .31902,1
  51. 0 .5533,1,0
  52. 0 .44274,0 .59205,0
  53. 0 .85176,0 .6612,0
  54. 0 .60436,0 .86605,0
  55. 0 .68243,0 .48301,0
  56. 1,0 .76815,0
  57. 0 .72989,0 .8107,0
  58. 0 .67377,0 .77975,0
  59. 0 .78761,0 .58177,0
  60. 0 .71442,0 .7668,0
  61. 0 .49379,0 .54226,0
  62. 0 .78974,0 .74233,0
  63. 0 .67905,0 .60921,0
  64. 0 .6642,0 .72519,0
  65. 0 .79396,0 .56789,0
  66. 0 .70758,0 .76022,0
  67. 0 .59421,0 .61857,0
  68. 0 .49364,0 .56224,0
  69. 0 .77707,0 .35025,0
  70. 0 .79785,0 .76921,0
  71. 0 .70876,0 .96764,0
  72. 0 .69176,0 .60865,0
  73. 0 .66408,0 .92075,0
  74. 0 .65973,0 .66666,0
  75. 0 .64574,0 .56845,0
  76. 0 .89639,0 .7085,0
  77. 0 .85476,0 .63167,0
  78. 0 .62091,0 .80424,0
  79. 0 .79057,0 .56108,0
  80. 0 .58935,0 .71582,0
  81. 0 .56846,0 .7406,0
  82. 0 .65912,0 .71548,0
  83. 0 .70938,0 .74041,0
  84. 0 .59154,0 .62927,0
  85. 0 .45829,0 .4641,0
  86. 0 .79982,0 .74847,0
  87. 0 .60974,0 .54757,0
  88. 0 .68127,0 .86985,0
  89. 0 .76694,0 .64736,0
  90. 0 .69048,0 .83058,0
  91. 0 .68122,0 .96541,0
  92. 0 .73229,0 .64245,0
  93. 0 .76145,0 .60138,0
  94. 0 .58985,0 .86955,0
  95. 0 .73145,0 .74516,0
  96. 0 .77029,0 .7014,0
  97. 0 .73156,0 .71782,0
  98. 0 .44556,0 .57991,0
  99. 0 .85275,0 .85987,0
  100. 0 .51912,0 .62359,0

8、非线性界限

     就是这样,如果有个学生是给的条件是9,1,就是平时成绩是1,考试成绩为9,但是也被录取了,显然就不合适了,所以要把线性变成曲线,圆,或者别的函数关系,只要更加好就行,我们需要开发更为复杂的算法来解决这个问题。我们需要重新定义感知器算法,使其能泛化到除直线以外的其他类型曲线。

 

9、Logistic(对数概率)误差函数-初探

      刚刚的感知器算法实现告诉我们,获取正确分类的方式,就是通过每一个错误分类的点,评估错误点位置与我们期望位置之间的差异,来慢慢的修正我们分类函数。

     因为误差暗示了如何进行正确的分类,因此误差的定义就变得尤为重要,这也被称为误差函数

10、Logistic(对数概率)误差函数与梯度下降-初探

     误差函数提供给我们的预测值与实际值之间的差异,但是这个差异如何指导我们权重的更新呢?我们的目标是找到最小的误差函数值来找到与实际值误差最小的预测值。

     在简单的线性方程中,我们可以通过判断“预测值与实测值相比是大了还是小了”来决定权重是增加还是减少。但是在更为复杂的非线性环境中呢?复杂的数学问题,我们就直接来看看学者们的解决策略。

     假设一维问题是一条直线,那么二维问题就是一个平面,而三维问题就是一个曲面。曲面可以理解为有山峰也有低谷的地面,误差最小的地方就是低谷处,我们希望计算机找到的就是这个低谷的值。为了找到这个低谷,学者们发明了梯度下降

      如果仅仅是距离的话,那么正巧都是2 那么如何选择,那么就无法选择了,所以科学家们想到了梯度,变化最快的那个方向,因为梯度所以不能时离散函数,必须是连续的哦。

      我们的目需要找到用什么误差函数?

      当点分类错误时,惩罚值等于点离直线的距离。将所有点的误差相加,得到总误差,两个错误的点使得误差很大,需要移动直线,降低误差,现在可以实现误差函数连续变化了,我们队直线参数进行小改动,使得误差函数产生一些小的变化,移动直线时,虽然有些误差变大了,但是总得误差减少了,而且还都分开了,能够构建具有这一属性的误差函数后,借可以使用梯度下降来解决我们的问题了。

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

   学习如何用神经网络来解决分类问题。

标签:误差,感知器,卷积,outputs,优达,test,output,correct,学城
来源: https://blog.51cto.com/u_15242250/2842425

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有