tkf / PyRNN

Recurrent Neural Networks by pure python

Clone this repository (size: 28.5 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/tkf/pyrnn/
commit 13: e324183e99d6
parent 12: 37c65b5a5106
branch: default
for demo
Takafumi Arakaki
4 months ago

Changed (Δ1.3 KB):

raw changeset »

toy/ElmanNet/learn_liss_anime.py (58 lines added, 0 lines removed)

Up to file-list toy/ElmanNet/learn_liss_anime.py:

1
import numpy
2
import pylab
3
import os
4
from pyrnn.elmannet import ElmanNet
5
from pyrnn.learn import Learn, phase_plot_tixo
6
7
num_o = 2
8
num_c = 5
9
num_i = 2
10
ec_scale = 1
11
steps = 30
12
tch_T1 = 8
13
tch_T2 = 12
14
tch_dT = 0
15
iter_max = 10000
16
cb_step =  1
17
learn_rate = 1.0
18
momentum = 0.9
19
20
def make_lissajous(T1, T2, dT, steps):
21
    t = numpy.arange(steps)
22
    a = numpy.zeros((steps, 2), dtype=numpy.float)
23
    pi = numpy.pi
24
    a[:,0] = numpy.cos(2*pi*t/T1)
25
    a[:,1] = numpy.sin(2*pi*(t+dT)/T2)
26
    return a
27
28
def get_cb_phase_plot(ln):
29
    def gene_cb_phase_plot():
30
        i = 0
31
        while 1:
32
            issave = False
33
            if   i < 100               : issave = True
34
            elif i < 1000 and i%10 == 0: issave = True
35
            elif             i%100 == 0: issave = True
36
            if issave:
37
                phase_plot_tixo(ln)
38
                pylab.savefig('anime/nn%010d.png'%i)
39
            i += 1
40
            yield
41
    cb_phase_plot = gene_cb_phase_plot()
42
    return cb_phase_plot.next
43
44
nn = ElmanNet(num_o,num_c,num_i,steps)
45
ln = Learn(nn,iter_max)
46
cb_func = get_cb_phase_plot(ln)
47
ln.set_callback(cb_func,cb_step)
48
49
nn.set_learn_rate(learn_rate)
50
nn.momentum = momentum
51
nn.ns.to = make_lissajous(tch_T1,tch_T2,tch_dT,steps) * 0.8
52
nn.ns.ti[1:] = nn.ns.to[:-1]
53
nn.set_input()
54
nn.randomize_param()
55
nn.ns.ec *= ec_scale
56
57
os.mkdir('anime')
58
ln.learn()