Question

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?

Was it helpful?

Solution

It seems you forgot to import that function:

from pybrain.tools.shortcuts import buildNetwork

See documentation.

Each time you want to use a special member of a module, you have to import it. Look at the documentation and search the member. For example for TanhLayer. You see that the function is in pybrain.structure.modules. So you have to import it like

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

There are other (sometimes cleaner) ways to import functions. This document from effbot explains nicely what are the differences and which you should use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top