【发布时间】:2016-02-18 22:41:42
【问题描述】:
我遇到了 pybrain 的问题。我创建了一个简单的异或问题,并使用 pybrain 解决了它而不使用偏差,然后使用一个简单的算法来获取从一层到下一层的所有权重。这里一切正常,无需知道哪个权重属于哪个连接。
当我尝试在 VHDL 中复制神经网络时,问题就出现了。我尝试在许多组合中使用权重,但未能以正确的顺序使用它们(我最初认为问题出在 VHDL 代码上,但后来我尝试手动执行相同操作,但最终得到相同的结果)。
网络是这样的
c1/5- O c9-
O c2/6- O c10- O
O c3/7- O c11-
c4/8- O c12-
in|hidden|out
O 是神经元,cN 是连接
得到的权重如下:
在 -> hidden0 => [-1.5370131 0.20571103 -0.55526946 2.24190836 1.25758021 0.0099828 3.41607776 3.60830287]
hidden0 -> out => [ 1.18471773 -2.20053965 -2.60886924 3.70095397]
我首先尝试用两种最明显的方式来唱它们:
First combo| Second combo
c1 -> -1.537 | -1.53
c2 -> 0.206 | 1.257
c3 -> -0.555 | 0.205
c4 -> 2,242 | 0.0099
c5 -> 1.257 | -0.555
c6 -> 0.0099 | 3.416
c7 -> 3.416 | 2.242
c8 -> 3.608 | 3.608
c9 -> 1.185 | 1.185
c10 -> -2.2 | -2.2
c11 -> -2.609 | -2.609
c12 -> 3.7 | 3.7
使用这两种组合,我得到的结果大致相同 ~0.5。 这意味着我使用权重的方式或我做数学的方式绝对有问题。
我按照以下方式计算:
in -> hidden
suppose input "11"
1 * c1 + 1 * c2 = RES
output = sigmoid(RES)
1 * c3 + 1 * c4 = RES2
output2 = sigmoid(RES2)
and so on
hidden -> out
output * c9 = RES9
final = sigmoid(RES9)
output2 * c10 = RES10
final = sigmoid (RES10)
and so on
现在想象一下,我也尝试了其他组合。上面的组合是c1-c2,另一个组合是c1-c5
我在 VHDL 中实现了与我在这里所做的相同的事情,结果与我手动获得的结果相同。
我需要正确的权重顺序来验证我的 VHDL 代码。我知道这应该可行,因为我通过运行 pybrain 得到的结果是:
[1, 0] [0.95923448]
[0, 1] [0.95626049]
[0, 0] [0.03813141]
[1, 1] [0.05266151]
PS:我使用的 xor 是在这个link 中获得的。我只修改了隐藏层的神经元个数,将bias修改为False,使用hiddenclass=SigmoidLayer。并且获得权重的代码可以是以下两种之一:
第一个代码
for c in [connection for connections in net.connections.values() for connection in connections]:
print("{} -> {} => {}".format(c.inmod.name, c.outmod.name, c.params))
第二个代码
for mod in net.modules:
print("Module:", mod.name)
if mod.paramdim > 0:
print("--parameters:", mod.params)
for conn in net.connections[mod]:
print("-connection to", conn.outmod.name)
if conn.paramdim > 0:
print("- parameters", conn.params)
if hasattr(net, "recurrentConns"):
print("Recurrent connections")
for conn in net.recurrentConns:
print("-", conn.inmod.name, " to", conn.outmod.name)
if conn.paramdim > 0:
print("- parameters", conn.params)
这两个 sn-ps 都是从 stackoverflow 问题中获得的。如果您愿意,我可以查找它们并在此处发布链接。
【问题讨论】:
标签: python algorithm neural-network