how to fit a function using PyBrain networks?
Asked Answered
F

1

5

there is a function like: y = sin(x) I want to use PyBrain networks to fit the functions, here are what i did: when you run it you will get what i get, the data obtained is far from what it should be.

from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
import pickle
import scipy as sp
import numpy as np
import pylab as pl

x = np.linspace(0, 4*np.pi, 100)
ds = SupervisedDataSet(1,1)

for i in x:
    ds.addSample(i,sin(i))
print ds

n = buildNetwork(ds.indim,3,3,3,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.01,momentum=0.5,verbose=True)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)

fileObject = open('trained_net', 'w')
pickle.dump(n, fileObject)
fileObject.close()

fileObject = open('trained_net','r')
net = pickle.load(fileObject)

y = []
for i in x:
    y.append(net.activate(i))

pl.plot(x,y)
pl.plot(x,np.sin(x))
pl.show()
Fictionalize answered 25/4, 2012 at 8:0 Comment(1)
So what is your question? Did you try other network architectures than this 5-layer-deep example?Endres
A
8

I suppose your problem is that this network does not fit the function well. The total number of network nodes is too low to properly fit this sin(x) function: the function is too complex. Also, for fitting any function, no more than one hidden layer is required in principle.

For instance, try to remove two hidden layers, and increase the number of hidden nodes (to, say, 20). Your code fits the function just fine then

Ammoniacal answered 27/9, 2012 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.