运行入口程序Create_AI_Framework_In5Classes(Class1)的Neuron_Network_Entry.py代码,运行结果如下:在hidden_layers= [4,2]时, 运行结果的第一行中的第一个“+1”表示输入层的Bias节点名字,V1表示输入层的第一个输入节点,V2表示输入层的第2个输入节点;第二行“Hiddenlayer creation”的1是第1层隐藏层Bias名字,之后是第一个隐藏层的4个神经元节点的名字。第三行“Hidden layer creation”的2是第2层隐藏层Bias名字,之后是第2个隐藏层的2个神经元节点的名字。第四行打印输出层Output layer的节点名字Output;第4行至第15行的记录打印了节点是否为Bias的节点;第16行至48行的记录打印了层次之间节点的权重信息。
+1 V1 V2
Hidden layer creation: 1 N[1][1] N[1][2] N[1][3] N[1][4] Hidden layer creation: 2 N[2][1] N[2][2]
Output layer: Output
This is a bias:0 True
This is a bias:1 False
This is a bias:2 False
This is a bias:3 True
This is a bias:4 False
This is a bias:5 False
This is a bias:6 False
This is a bias:7 False
This is a bias:8 True
This is a bias:9 False
This is a bias:10 False
This is a bias:11 False
The weight from 0 to 3 : -0.8432927944499292
The weight from 0 to 4 : 0.31040626938373794
The weight from 0 to 5 : 0.9805869742938771
The weight from 0 to 6 : 0.5640002305153979
The weight from 0 to 7 : 0.18013609435209132
The weight from 1 to 3 : -0.3730098485963417
The weight from 1 to 4 : 0.7799945591751547
The weight from 1 to 5 : -0.9594934440293328
The weight from 1 to 6 : -0.3861544423563733
The weight from 1 to 7 : -0.9230632804294159
The weight from 2 to 3 : -0.3417936962300032
The weight from 2 to 4 : -0.8808083450903371
The weight from 2 to 5 : -0.9828144841662153
The weight from 2 to 6 : 0.40570514965497284
The weight from 2 to 7 : 0.2062009051883149
The weight from 3 to 8 : -0.08571059670290881
The weight from 3 to 9 : 0.5887786989813657
The weight from 3 to 10 : -0.044944262158458814
The weight from 4 to 8 : -0.22056670893200558
The weight from 4 to 9 : 0.9022426398160805
The weight from 4 to 10 : 0.9589479648411074
The weight from 5 to 8 : 0.7624314210742158
The weight from 5 to 9 : 0.6188878269523919
The weight from 5 to 10 : 0.144186123214594
The weight from 6 to 8 : 0.9296242746949004
The weight from 6 to 9 : -0.7883625105175263
The weight from 6 to 10 : -0.8826609847341614
The weight from 7 to 8 : 0.3292160910341271
The weight from 7 to 9 : 0.28721651434474116
The weight from 7 to 10 : -0.8050407637944632
The weight from 8 to 11 : -0.7737203895168975
The weight from 9 to 11 : 0.2926894644394433
The weight from 10 to 11 : -0.8860746255530616
以上第16行至48行的记录打印的层次之间的权重连接关系如图所示,其中第0个节点、第3个节点、第8个节点都是Bias节点,也进行了Weight的计算,但参考TensorFlow的可视化图,Weight的连接不应包括Bias的节点,因此,接下来的工作需将Bias从Weight连接关系中去掉。
图 1- 23 weight图(Weight中需去除Bias)
NetworkConnection.py代码中构成Weight的前提条件是有前后相邻的Layer关系。 是不是就可以创建Weight了?不是的,因为我们创建的节点中还有Bias,这里要不要Bias?Weight是发生在神经元之间的,跟Bias没关系,比较的节点Node之间不能是Bias成员,所以要去掉Bias的节点。在NetworkConnection.py代码判断一下节点nodes[k]的get_is_bias_unit()如果等于True,去掉Bias节点,节点nodes[k]的get_is_bias_unit()如果等于False,说明是神经元节点;同样的,判断一下节点nodes[j]的get_is_bias_unit()如果等于True,去掉Bias节点,节点nodes[j]的get_is_bias_unit()如果等于False,说明是神经元节点。下一步才是创建Weight节点,从nodes[j]出发到nodes[k]之间创建Weight,相当于TensorFlow的可视化图中,从输入层的x1节点到第一个隐藏层的第一个节点的Weight。
因此,在Create_AI_Framework_In5Classes(Class1)版本的NetworkConnection.py 的基础上进行微调:
在NetworkConnection.py 的33行代码之后新增代码,判断nodes[k]、nodes[j]节点是否为Bias节点。
……
if nodes[k].get_is_bias_unit() == False:
if nodes[j].get_is_bias_unit() == False:
……
再次运行入口程序Create_AI_Framework_In5Classes(Class1)的Neuron_Network_Entry.py代码,运行结果如下:这里是hidden_layers = [4,2] 为两个Hidden layer,第一个Hidden Layer有4个Neuron,第二个Hidden Layer有2个Neuron。整个神经元网络有12个节点,其中ID为0、3、8的Node为Bias节点,ID为1,2的是输入层,分别关联到 4、5、6、7节点,然后4、5、6、7节点分别关联到下一个隐藏层的节点9,10节点;9,10节点最后输出到输出层节点ID 11 ,打印出各权重的结果。此时的第16行到第33行记录中权重连接已经不包括Bias节点的信息:
+1 V1 V2
Hidden layer creation: 1 N[1][1] N[1][2] N[1][3] N[1][4] Hidden layer creation: 2 N[2][1] N[2][2]
Output layer: Output
This is a bias:0 True
This is a bias:1 False
This is a bias:2 False
This is a bias:3 True
This is a bias:4 False
This is a bias:5 False
This is a bias:6 False
This is a bias:7 False
This is a bias:8 True
This is a bias:9 False
This is a bias:10 False
This is a bias:11 False
The weight from 1 to 4 : -0.5966709106901581
The weight from 1 to 5 : 0.07947677556332922
The weight from 1 to 6 : 0.07152849138429773
The weight from 1 to 7 : -0.6151153437634211
The weight from 2 to 4 : 0.4404589389379745
The weight from 2 to 5 : -0.37302058580259034
The weight from 2 to 6 : 0.6301744080838145
The weight from 2 to 7 : 0.8399836297367551
The weight from 4 to 9 : -0.8540440064378192
The weight from 4 to 10 : 0.11234629072264402
The weight from 5 to 9 : -0.7001168324752038
The weight from 5 to 10 : 0.3559754422762831
The weight from 6 to 9 : 0.1871381838161481
The weight from 6 to 10 : -0.24520160115212353
The weight from 7 to 9 : -0.8298084340883155
The weight from 7 to 10 : 0.3495018265054681
The weight from 9 to 11 : 0.5642582213695844
The weight from 10 to 11 : -0.5101870920556825
代码调整以后,第16行到第33行记录的层次之间的权重连接关系如图所示,此时,我们自研的神经网络Weight关系图和TensorFlow的可视化图是类似的。
欢迎关注微信公众号:“从零起步学习人工智能”。
喜欢我们发布的信息,就在右下角点一下“在看”吧!欢迎转发分享!