Search code examples
pythonnumpymachine-learningneural-networkpybrain

Getting output of pybrain prediction as array


I am making use of pybrain to build a network that has 6 input dimensions and one real valued output dimension. The code I use is shown below:

network = buildNetwork(train.indim, 4, train.outdim)

trainer = BackpropTrainer( network, train)
trainer.trainOnDataset(train, 8000)

print 'MSE train', trainer.testOnData(train, verbose = True)

here train is of type Dataset I want to get the predictions made in trainer.testOnData() as a numpy array. I am able to view the predicted result along with the error but I want it as an array. Is there anyway that this can be done?


Solution

  • Use the activate function of your network:

    numpy.array([network.activate(x) for x, _ in train])
    

    Complete example:

    from datasets import XORDataSet 
    from pybrain.tools.shortcuts import buildNetwork
    from pybrain.supervised import BackpropTrainer
    import numpy
    d = XORDataSet()
    n = buildNetwork(d.indim, 4, d.outdim, bias=True)
    t = BackpropTrainer(n, learningrate=0.01, momentum=0.99, verbose=True)
    t.trainOnDataset(d, 1000)
    t.testOnData(verbose=True)
    print numpy.array([n.activate(x) for x, _ in d])
    

    (Only works in the directory pybrain/examples/supervised/backprop of pybrain because the XORDataSet is required.)