2020年人工神经网络第二次作业-参考答案第三题
如下是 2020年人工神经网络第二次作业 中第三题的参考答案。
➤01 第三题参考答案
1.题目分析
本题实际上是课件中例题的重现问题。
对于7个字符(三种字体),21个训练样本,它们分别属于7大类。所以用于对该数据集合的竞争学习,竞争神经元的个数应该至少大于7。题目要求竞争层的神经元的个数等于25。
根据课件上的描述,以及题目中的要求,构造自组织特征映射(SOFM)的主要参数为:
- 竞争层的三种结构:要求分别从无拓扑结构(WTA)、一维拓扑结构、二维拓扑结构来进行对比;
- 训练过程中,学习速率按照线性从0.6逐步线性减小至0.01;
- 对于一维拓扑结构、二维拓扑结构,训练半径始终保持为1;
- 对于二维结构,一个神经元只有上下左右四个相邻的神经元样本;
根据以上简化假设,可以编写相应的程序,完成网络训练。
2.求解过程
(1) 求解程序
求解过程中相关程序参见后面附录中 作业中的程序。
- 无拓扑结构训练算法
def compete0(x, w, eta):
for xx in x:
id = WTA2(xx, w)
w[id] = w[id] + eta * (xx - w[id])
return w
- 1
- 2
- 3
- 4
- 5
- 6
- 一维拓扑结构训练算法
def compete1(x, w, eta):
for xx in x:
id = WTA2(xx, w)
w[id] = w[id] + eta * (xx - w[id])
if id > 0: w[id-1] = w[id-1] + eta*(xx-w[id-1])
if id+1 < w.shape[0]: w[id+1] = w[id+1] + eta*(xx-w[id+1])
return w
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 二维拓扑结构训练算法
def neighborid2(id, row, col):
rown = id // col
coln = id % col
iddim = [id]
if coln > 0: iddim.append(id-1)
if coln < col-1: iddim.append(id+1)
if rown > 0: iddim.append(id-col)
if rown < row-1: iddim.append(id+col)
return iddim
def compete2(x, w, eta):
for xx in x:
id = WTA2(xx, w)
iddim = neighborid2(id, 5, 5)
for iidd in iddim:
w[iidd] = w[iidd] + eta * (xx - w[iidd])
return w
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
(2) 竞争层无结构
也就是采用胜者为王的竞争算法:
- 随机初始化后,竞争层神经元对应的响应:
1. 2. 3. 4.JJEJ 5.
6. 7. 8. 9. 10.
11. 12.A 13.AB 14. 15.BEE
16. 17.KK 18. 19. 20.
21. 22. 23. 24.CDBCKDCD25.A
- 1
- 2
- 3
- 4
- 5
上面显示随机初始化之后,只有6个神经元(编号:4,12,13,15,17,24)对训练样本有响应。其中四个神经元(编号:4,13,15,24)响应样本有混淆,即多于一个字符。
- 经过训练之后竞争层层的神经元响应:
1. 2. 3. 4.JJJ 5.BED
6. 7. 8. 9.K 10.AA
11. 12.KEKE 13.A 14. 15.
16. 17.BDBD 18. 19. 20.
21. 22. 23. 24.CCC 25.
- 1
- 2
- 3
- 4
- 5
经过竞争训练之后,有8个神经元(编号:4,5,9,10,12,13,17,24)有响应了。其中三个神经元(编号:5,12,17)存在字符混淆。
- 重新初始化之后再一次训练之后的神经元响应:
1.CCC 2.BED 3. 4. 5.
6. 7.KEKE 8. 9. 10.
11. 12.AA 13. 14.BDBD 15.
16. 17. 18. 19. 20.
21.K 22.A 23. 24. 25.JJJ
- 1
- 2
- 3
- 4
- 5
重新训练,仍然有8个神经元(编号:1,2,7,12,14,21,22,25)有响应,有3个神经元(编号:2,7,14)存在字符混淆。
(3) 竞争层采用一维拓扑结构
- 第一次训练结果
1.KK 2. 3.EE 4. 5.BB
6. 7.DD 8. 9.CCC 10.
11.JJJ 12. 13.A 14. 15.A
16. 17.A 18. 19.B 20.
21.D 22. 23.E 24. 25.K
- 1
- 2
- 3
- 4
- 5
使用一维拓扑结构训练之后,总共有13个神经元有响应,而且不存在有混淆字符的神经元。这说明对于字符分类已经能够达到100%正确了。
- 第二次训练结果
1.KK 2. 3.E 4. 5.BE
6. 7.B 8. 9.DD 10.
11.K 12. 13.BE 14. 15.D
17. 17.CCC 18. 19. 20.A
18. 22.AA 23. 24.JJJ 25
- 1
- 2
- 3
- 4
- 5
再训练一次,存在14个神经元响应。其中存在2个神经元(编号:5,13)存在着字符混淆。
(4) 竞争层采用二维拓扑结构
- 第一次训练结果
1. 2.AA 3. 4.C 5.
6.A 7. 8.DD 9. 10.CC
21. 12.K 13. 14.JJJ 15.
16.ED 17. 18.KK 19. 20.EE
22. 22.B 23. 24.BB 25.
- 1
- 2
- 3
- 4
- 5
- 第二次训练结果
1. 2.K 3. 4. 5.BB
6.D 7. 8. 9.EE 10.
11. 12. 13.KK 14. 15.DD
16. 17.BE 18. 19.A 20.
21.CCC 22. 23.JJJ 24. 25.AA
- 1
- 2
- 3
- 4
- 5
使用二维拓扑结构,无论是在神经元响应的数量上,还是存在欢笑神经元方面,都没有比一维拓扑结构有明显改善。
3.结果讨论
- 通过对比三种竞争层的拓扑结构(无拓扑结构(0维拓扑)、一维拓扑、二维拓扑)可以看到,对于分类问题,一维拓扑结构比起无拓扑结构(0维结构)效果要好。体现在能够更多的激发出神经元来对应样本相应,类别混淆的神经元个数进一步降低;
- 分类问题上,二维拓扑结构比一维拓扑结构没有显著的效果改进。
➤※ 作业中的程序
1.作业竞争算法主程序
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HW23.PY -- by Dr. ZhuoQing 2020-11-24
#
# Note:
#============================================================
from headm import *
import hw19data
#------------------------------------------------------------
x_data = hw19data.chardata.astype('float32')
target = hw19data.targetdata.T
CharStr = 'ABCDEJK'
#------------------------------------------------------------
def target2c(t):
id = list(where(t==1))[0][0]
# printff(t, id)
return CharStr[id]
#------------------------------------------------------------
W = random.rand(25, x_data.shape[1])
#------------------------------------------------------------
def WTA2(x, w):
""" Win-Take-All
In: x-sample(x1,x2)
w-net argument
Ret: id-Win ID of w
"""
dist = array([(x-ww).dot(x-ww) for ww in w])
return list(where(dist==amin(dist)))[0][0]
#------------------------------------------------------------
SHOW_SEGMENT_LEN = 10
def shownet0(w): # Show net result: 0-dimension
strdim = [''] * 25
for id,x in enumerate(x_data):
iidd = WTA2(x, w)
c = target2c(target[id])
strdim[iidd] += c
for i in range(5):
showstr = ''
for j in range(5):
strid = i * 5 + j
outstr = '%2d.%s'%(strid+1, strdim[strid])
if len(outstr) < SHOW_SEGMENT_LEN:
outstr += " " * (SHOW_SEGMENT_LEN - len(outstr))
showstr += outstr
printf(showstr)
shownet0(W)
#------------------------------------------------------------
def compete0(x, w, eta):
for xx in x:
id = WTA2(xx, w)
w[id] = w[id] + eta * (xx - w[id])
return w
#------------------------------------------------------------
def compete1(x, w, eta):
for xx in x:
id = WTA2(xx, w)
w[id] = w[id] + eta * (xx - w[id])
if id > 0: w[id-1] = w[id-1] + eta*(xx-w[id-1])
if id+1 < w.shape[0]: w[id+1] = w[id+1] + eta*(xx-w[id+1])
return w
#------------------------------------------------------------
def neighborid2(id, row, col):
rown = id // col
coln = id % col
iddim = [id]
if coln > 0: iddim.append(id-1)
if coln < col-1: iddim.append(id+1)
if rown > 0: iddim.append(id-col)
if rown < row-1: iddim.append(id+col)
return iddim
def compete2(x, w, eta):
for xx in x:
id = WTA2(xx, w)
iddim = neighborid2(id, 5, 5)
for iidd in iddim:
w[iidd] = w[iidd] + eta * (xx - w[iidd])
return w
#------------------------------------------------------------
STEPS = 1000
for i in range(STEPS):
eta = 0.6 - (0.59 * i/STEPS)
x = x_data.copy()
random.shuffle(x)
W = compete2(x, W, eta)
shownet0(W)
#------------------------------------------------------------
# END OF FILE : HW23.PY
#============================================================
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
2.训练数据整理子程序模块
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HW19DATA.PY -- by Dr. ZhuoQing 2020-11-24
#
# Note:
#============================================================
from headm import *
sdata = ('[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]',\
'[1, 0, 0, 0, 0, 0, 0]',\
'[1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0]',\
'[0, 1, 0, 0, 0, 0, 0]',\
'[0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0]',\
'[0, 0, 1, 0, 0, 0, 0]',\
'[1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 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 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1]',\
'[0, 0, 0, 0, 0, 0, 1]',\
'[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0]',\
'[0, 0, 0, 0, 0, 1, 0]',\
'[1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]',\
'[0, 0, 0, 0, 1, 0, 0]',\
'[1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0]',\
'[0, 0, 0, 1, 0, 0, 0]',)
#------------------------------------------------------------
chardata = []
targetdata = []
for s in sdata:
data = eval(s)
if len(data) > 7: chardata.append(data)
else: targetdata.append(data)
#printf(chardata, targetdata)
chardata = array(chardata)
targetdata = array(targetdata).T
if __name__ == "__main__":
printff(chardata, targetdata)
#------------------------------------------------------------
# END OF FILE : HW19DATA.PY
#============================================================
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/110085886
- 点赞
- 收藏
- 关注作者
评论(0)