【问题标题】:PyBrain:How can I put specific weights in a neural network?PyBrain:如何在神经网络中放置特定的权重?
【发布时间】:2012-03-21 22:25:35
【问题描述】:

我正在尝试根据给定的事实重新创建一个神经网络。它有 3 个输入、一个隐藏层和一个输出。我的问题是权重也是给定的,所以我不需要训练。

我在想也许我可以保存类似结构神经网络的训练并相应地更改值。你认为这会奏效吗?还有其他想法。谢谢。

神经网络代码:

    net = FeedForwardNetwork()
    inp = LinearLayer(3)
    h1 = SigmoidLayer(1)
    outp = LinearLayer(1)

    # add modules
    net.addOutputModule(outp)
    net.addInputModule(inp)
    net.addModule(h1)

    # create connections
    net.addConnection(FullConnection(inp, h1))
    net.addConnection(FullConnection(h1, outp))

    # finish up
    net.sortModules()


    trainer = BackpropTrainer(net, ds)
    trainer.trainUntilConvergence()

How to save and recover PyBrain training?保存训练和加载代码

# Using NetworkWriter

from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.xml.networkwriter import NetworkWriter
from pybrain.tools.xml.networkreader import NetworkReader

net = buildNetwork(2,4,1)

NetworkWriter.writeToFile(net, 'filename.xml')
net = NetworkReader.readFrom('filename.xml') 

【问题讨论】:

  • 如果您觉得答案有帮助,请将其标记为已接受;)

标签: python neural-network pybrain


【解决方案1】:

我很好奇如何阅读已经训练好的网络(使用 xml 工具)。因为,这意味着可以以某种方式设置网络权重。所以在NetworkReader documentation我发现,可以用_setParameters()设置参数。

但是,下划线表示私有方法,可能会产生一些副作用。还要记住,带有权重的向量必须与最初构建的网络长度相同。

例子

>>> import numpy
>>> from pybrain.tools.shortcuts import buildNetwork
>>> net = buildNetwork(2,3,1)
>>> net.params

array([...some random values...])

>>> len(net.params)

13

>>> new_params = numpy.array([1.0]*13)
>>> net._setParameters(new_params)
>>> net.params

array([1.0, ..., 1.0])

其他重要的事情是将值按正确的顺序排列。比如上面是这样的:

[  1., 1., 1., 1., 1., 1.,      1., 1., 1.,        1.,       1., 1., 1.    ] 
     input->hidden0            hidden0->out     bias->out   bias->hidden0   

要确定哪些权重属于层之间的哪些连接,试试这个

# net is our neural network from previous example
for c in [connection for connections in net.connections.values() for connection in connections]:
    print("{} -> {} => {}".format(c.inmod.name, c.outmod.name, c.params))

无论如何,我仍然不知道层之间权重的确切顺序...

【讨论】:

猜你喜欢
  • 2017-05-18
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2011-08-07
  • 2014-09-07
相关资源
最近更新 更多