最后我设法从一个文件训练一个网络:)现在我想打印节点和权重,特别是权重,因为我想用pybrain训练网络,然后在其他地方实现NN将使用它
我需要一种方法来打印节点之间的层,节点和权重,以便我可以轻松地复制它。到目前为止,我看到我可以使用n ['in']来访问图层,然后我可以这样做:
DIR(N [ '在']) ['strong>类',' delattr ',' dict ',' doc ','格式',' getattribute ','哈希',' init ','模块', ' new ',' reduce ',' reduce_ex ',' repr ',' setattr < / strong>',' sizeof ',' str ','子类别','弱点',' _backwardImplementation','_ forwardImplementation','_ generateName','_ getName','_ _ growBuffers','_ name','_ nameIds','_ readBuffers','_ setName','activate','activateOnDataset','argdict','backActivate' ,'后退','缓冲列表','暗淡','前进','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])
很难说什么是什么,至少在重量连接哪些节点时。这就是我所需要的一切。
答案 0 :(得分:21)
有许多方法可以访问网络的内部,即通过其“模块”列表或其“连接”字典。参数存储在这些连接或模块中。例如,以下内容应打印任意网络的所有信息:
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)
如果你想要更细粒度的东西(在神经元级而不是层级),你将不得不进一步分解这些参数向量 - 或者,从单神经元层构建你的网络。
答案 1 :(得分: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'>]