How to fit cubic equation on pybrain

71 Views Asked by At

I am trying to fit a Neural Network on a cubic Equation, but after many tries changing the number of the neurons on the hidden layer and increasing the number of epochs I could only get this: enter image description here

Could you guys help me on this?

from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.supervised.trainers import BackpropTrainer
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Preparing Data
dataset = pd.read_csv("C:\Users\gugub\Documents\projects\Cyrus\graph.data", header = None)
darray = np.array(dataset)
x = []
y = []
ds = SupervisedDataSet(1,1)
#Preparing True inputs and True Outputs
for i in range(43):
    e1 = darray[i,0]
    s1 = darray[i,1]
    x.append(e1)
    y.append(s1)
print(x)
print(y)
ds = SupervisedDataSet(1,1)

i = 0
for i in range(43):
    ds.addSample(x[i],y[i])
print(ds)
net = buildNetwork(ds.indim,30,ds.outdim,recurrent=True)
trainer = BackpropTrainer(net,learningrate=0.01,verbose=True)

trainer.trainOnDataset(ds,20000)
trainer.testOnData(verbose=True)

y1 = []

i = 0
for i in x:
    y1.append(net.activate(i))

plt.plot(x,y,'r')
plt.plot(x,y1,'b')
plt.show()

while True:
    e2 = int(raw_input(">"))
    s2 = [e2]
    print(net.activate(s2))

P.S The red line on the graph is what it should be and the blue one is the function generated by my network

0

There are 0 best solutions below