0

I'm using Python 2.7.5 and PyBrain 0.3 (installed via pip). I can't reproduce the "quickstart" code there is in PyBrain documentation pages because the function buildNetwork() seems to not be defined and it triggers a NameError. Here is the code:

from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

ds = SupervisedDataSet(2, 1)
ds.addSample((0, 0), (0,))
ds.addSample((0, 1), (1,))
ds.addSample((1, 0), (1,))
ds.addSample((1, 1), (0,))

# here is the problem \/\/\/\/\/\/\/\/
net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)

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

net.activate([0, 0])
net.activate([0, 1])
net.activate([1, 0])
net.activate([1, 1])

And this is the error message I receive when trying to run this script:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-d45aee0605fb> in <module>()
----> 1 net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)

NameError: name 'buildNetwork' is not defined

It's strange because all previous lines don't trigger any errors, the problem is occurring with buildNetwork() function. Could someone please help me?

4

1 回答 1

3

您似乎忘记导入该功能:

from pybrain.tools.shortcuts import buildNetwork

请参阅文档

每次要使用模块的特殊成员时,都必须导入它。查看文档并搜索成员。例如对于 TanhLayer。您会看到该函数位于pybrain.structure.modules. 所以你必须像这样导入它

from pybrain.structure.modules import TanhLayer
# or
from pybrain.structure.modules import *

还有其他(有时更简洁)的方法来导入函数。来自 effbot 的这份文档很好地解释了它们之间的区别以及您应该使用哪些。

于 2014-02-12T16:51:08.517 回答