【问题标题】:pybrain: how to print a network (nodes and weights)pybrain:如何打印网络(节点和权重)
【发布时间】:2011-12-30 08:22:25
【问题描述】:

最后我设法从一个文件中训练了一个网络 :) 现在我想打印节点和权重,尤其是权重,因为我想用 pybrain 训练网络,然后在其他地方实现一个 NN 来使用它.

我需要一种方法来打印层、节点和节点之间的权重,以便我可以轻松地复制它。到目前为止,我看到我可以使用例如 n['in'] 访问图层,然后例如我可以这样做:

dir(n['in']) ['class', 'delattr', 'dict', 'doc', '格式', 'getattribute', 'hash', 'init', 'module', 'new'、'reduce'、'reduce_ex'、'repr'、'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', ' _backwardImplementation','_forwardImplementation','_generateName','_getName','_growBuffers','_name','_nameIds','_resetBuffers','_setName','activate','activateOnDataset','argdict','backActivate' , 'backward', 'bufferlist', 'dim', 'forward', 'getName', 'indim', 'inputbuffer', 'inputerror', 'name', 'offset', 'outdim', 'outputbuffer', ' outputerror', 'paramdim', 'reset', 'sequential', 'setArgs', 'setName', 'shift', 'whichNeuron']

但我看不到如何在此处访问权重。还有 params 属性,例如我的网络是 2 4 1 有偏差,它说:

n.params 数组([-0.8167133,1.00077451,-0.7591257,-1.1150532,-1.58789386, 0.11625991、0.98547457、-0.99397871、-1.8324281、-2.42200963、 1.90617387、1.93741167、-2.88433965、0.27449852、-1.52606976、 2.39446258, 3.01359547])

很难说什么是什么,至少用权重连接了哪些节点。这就是我所需要的。

【问题讨论】:

    标签: python neural-network pybrain


    【解决方案1】:

    有许多方法可以访问网络的内部,即通过其“模块”列表或“连接”字典。参数存储在这些连接或模块中。例如,下面应该打印任意网络的所有这些信息:

    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)
    

    如果您想要更细粒度的东西(在神经元级别而不是层级别),您将必须进一步分解这些参数向量 - 或者,从单神经元层构建您的网络。

    【讨论】:

    • 我可以按顺序考虑结果吗?所以 3 个连续的数字是来自同一个神经元的权重?
    • 我使用了上面的代码。你能解释一下它是如何工作的吗?我还有一个问题。我的网络中的 in_to_hiddens 的数量是 5200。使用上面的代码是这样写的:[ 1.55300577 -0.62533809 -0.08147982 ..., 1.29706926 0.50138988 ] 但我需要所有这些而不是一些点。我该怎么办??谢谢
    • 请您回答我的问题。我快疯了。看来我只是无法完成我的论文。
    【解决方案2】:

    试试这个,它对我有用:

    def pesos_conexiones(n):
        for mod in n.modules:
            for conn in n.connections[mod]:
                print conn
                for cc in range(len(conn.params)):
                    print conn.whichBuffers(cc), conn.params[cc]
    

    结果应该是这样的:

    <FullConnection 'co1': 'hidden1' -> 'out'>
    (0, 0) -0.926912942354
    (1, 0) -0.964135087592
    <FullConnection 'ci1': 'in' -> 'hidden1'>
    (0, 0) -1.22895643048
    (1, 0) 2.97080368887
    (2, 0) -0.0182867906276
    (3, 0) 0.4292544603
    (4, 0) 0.817440427069
    (0, 1) 1.90099230604
    (1, 1) 1.83477578625
    (2, 1) -0.285569867513
    (3, 1) 0.592193396226
    (4, 1) 1.13092061631
    

    【讨论】:

      【解决方案3】:

      也许这有帮助(PyBrain for Python 3.2)?

      C:\tmp\pybrain_examples>\Python32\python.exe
      Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from pybrain.tools.shortcuts import buildNetwork
      >>> from pybrain.structure.modules.tanhlayer import TanhLayer
      >>> from pybrain.structure.modules.softmax import SoftmaxLayer
      >>>
      >>> net = buildNetwork(4, 3, 1,bias=True,hiddenclass = TanhLayer, outclass =   SoftmaxLayer)
      >>> print(net)
      FeedForwardNetwork-8
      Modules:
      [<BiasUnit 'bias'>, <LinearLayer 'in'>, <TanhLayer 'hidden0'>, <SoftmaxLayer 'out'>]
      Connections:
      [<FullConnection 'FullConnection-4': 'hidden0' -> 'out'>, <FullConnection   'FullConnection-5': 'bias' -> 'out'>, <FullConnection
      'FullConnection-6': 'bias' -> 'hidden0'>, <FullConnection 'FullConnection-7': 'in' -> 'hidden0'>]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 2017-05-18
        • 1970-01-01
        • 2015-12-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多