In [4]:
import neurolab as nl
import numpy as np
In [3]:
# Create train samples
x = np.linspace(-7, 7, 20)
y = np.sin(x) * 0.5
In [3]:
size = len(x)
inp = x.reshape(size,1)
tar = y.reshape(size,1)
# Create network with 2 layers and random initialized
net = nl.net.newff([[-7, 7]],[5, 1])
# Train network
# default train method is
# BroydenFletcherGoldfarbShanno (BFGS) method Using scipy.optimize.fmin_bfgs
error = net.train(inp, tar, epochs=500, show=100, goal=0.02)
# Simulate network
out = net.sim(inp)
# Plot result
import pylab as pl
pl.subplot(211)
pl.plot(error)
pl.xlabel('Epoch number')
pl.ylabel('error (default SSE)')
x2 = np.linspace(-6.0,6.0,150)
y2 = net.sim(x2.reshape(x2.size,1)).reshape(x2.size)
y3 = out.reshape(size)
pl.subplot(212)
pl.plot(x2, y2, '-',x , y, '.', x, y3, 'p')
pl.legend(['train target', 'net output'])
pl.show()
In [ ]: