Pybrain - 哪个权重属于哪个连接?

Pybrain - which weight belongs to which connection?

我在使用 pybrain 时遇到问题。我创建了一个简单的 xor 问题并使用 pybrain 解决它而不使用偏差,然后使用一个简单的算法从一层到下一层获取所有权重。到这里一切都ok了,不用知道哪个weight属于哪个connection了。

当我尝试在 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 是连接

得到的权重如下:

in -> hidden0 => [-1.53​​70131 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 : 我用的异或就是这个link中得到的。我只修改了隐藏层的神经元数量,修改了偏置为False,并使用了hiddenclass=SigmoidLayer。而获取权重的代码可以是以下2中的一种:

第一个密码

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)

这两个片段都是从 Whosebug 问题中获得的。如果您需要,我可以在此处查找它们和 post 链接。

好的,我找到了我要找的答案。

权重并排 (c1-c2)。 这意味着正确的权重是:

c1 -> -1.537

c2 -> 0.206

c3 -> -0.555

c4 -> 2,242

c5 -> 1.257

c6 -> 0.0099

c7 -> 3.416

c8 -> 3.608

c9 -> 1.185

c10 -> -2.2

c11 -> -2.609

c12 -> 3.7

要获得正确的权重顺序,请执行以下操作:

for c in [connection for connections in net.connections.values() for connection in connections]:
        print("{} -> {} => {}".format(c.inmod.name, c.outmod.name, c.params))
        print("org {}", reshape(c.params, (c.outdim, c.indim)))

获取数据的代码为: 重塑(c.params, (c.outdim, c.indim)) 其中 "c" 是与神经元的连接。

权重给出如下:

[[-1.537   0.206],
 [-0.555   2.242],
 [1.257    0.0099],
 [3.416    3.608]]

 [[1.185],
  [-2.2],
  [-2.609], 
  [3.7]]

每一行都是与神经元的连接。

例如:

Suppose input = "10"
1 * -1.537 + 0 * 0.206 
and then goes to the sigmoid function
and so on

我已经试过了,但是没有用,这是有道理的,因为我做错了。当你创建一个新的网络时,有一些参数可以传递给它,hiddenclass 和 outclass 就是其中的一部分。我将 hiddenclass 设置为 SigmoidLayer,但我没有初始化 outclass,它使用默认值 LinearLayer 进行了初始化。这意味着当我在 VHDL 中实现代码并通过手动计算对其进行测试时,我正在执行一个额外的 sigmoid 函数。

如果你有超过 2 个输入连接到隐藏层,假设有 4 个,它看起来像这样:

[[c1 c2 c3],
 [c4 c5 c6],
 [c7 c8 c9]]

等等。

祝以后有需要的人好运