-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsine-net.py
More file actions
63 lines (48 loc) · 1.66 KB
/
Copy pathsine-net.py
File metadata and controls
63 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
import matplotlib.pyplot as plt
from pybrain.datasets import SupervisedDataSet
from pybrain.utilities import percentError
from pybrain.structure import FullConnection
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#net = FeedForwardNetwork()
#inLayer = LinearLayer(1)
#hiddenLayer1 = SigmoidLayer(20, name="hidden1")
#outLayer = LinearLayer(1)
#
#net.addInputModule(inLayer)
#net.addModule(hiddenLayer1)
#net.addOutputModule(outLayer)
#
#net.addConnection(FullConnection(inLayer, hiddenLayer1))
#net.addConnection(FullConnection(hiddenLayer1, outLayer))
#net.sortModules()
net = buildNetwork(1, 20, 1, recurrent=True)
xmin = 0
xmax = 4 * np.pi
xs = np.linspace(xmin, xmax, 150)
dataset = SupervisedDataSet(1, 1)
for x in xs:
dataset.addSample(x, np.sin(x))
trainset, testset = dataset.splitWithProportion(0.75)
trainer = BackpropTrainer(net, dataset=trainset, learningrate=0.01, momentum=0.5, verbose=False)
fig = plt.figure()
ax1 = plt.axes()
ln1, = ax1.plot([], [], 'r-')
#ln2, = ax1.plot([], [], 'g-')
def animate(i):
print 'round', i+1
trainer.trainEpochs(5)
train_error = trainer.testOnData(trainset)
test_error = trainer.testOnData(testset)
print 'train error', train_error
print 'test error', test_error
ln1.set_data(i, train_error)
# ln2.set_data(i, test_error)
return ln1
ani = animation.FuncAnimation(fig, animate, interval=100, frames=200)
plt.show()