1. Takafumi Arakaki
  2. PyRNN

Source

PyRNN / toy / ElmanNet / learn_l_anime.py

import numpy
import pylab
import os
from pyrnn.elmannet import ElmanNet
from pyrnn.learn import Learn, tms_plot_tixo

save_dir = 'lanime'
num_o = 2
num_c = 10
num_i = 2
ec_scale = 0.2
iter_max = 10000
cb_step =  1
learn_rate = 1.0
momentum = 0.9

def make_l():
    a = numpy.array([
        [-0.71,  0.37],
        [-0.6 ,  0.4 ],
        [-0.6 ,  0.4 ],
        [-0.4 ,  0.5 ],
        [-0.3 ,  0.6 ],
        [-0.2 ,  0.8 ],
        [-0.4 ,  0.5 ],
        [-0.42,  0.3 ],
        [-0.46, -0.1 ],
        [-0.5 , -0.5 ],
        [-0.63, -0.65],
        [-0.66, -0.58],
        [-0.65, -0.48],
        [-0.5 , -0.5 ],
        [-0.1 , -0.55],
        [ 0.3 , -0.58],
        [ 0.47, -0.53],
        [ 0.46, -0.47],
        [ 0.3 , -0.58],
        [ 0.21, -0.63],
        ])
    return numpy.concatenate(( a,a[::-1],a,a[::-1],a,a[::-1] ))

def phase_plot_xo(ln, xomin=-1, xomax=1, xcmin=-1, xcmax=1):
    nn = ln.nn
    xo = nn.ns.xo
    to = nn.ns.to
    xc = nn.ns.xc
    pylab.clf()
    pylab.subplot(221)
    pylab.loglog(ln.rec[:,0],ln.rec[:,1])
    pylab.subplot(222)
    for i in xrange(3,ln.rec.shape[1]):
        pylab.loglog(ln.rec[:,0],ln.rec[:,i])
    pylab.subplot(223)
    for i in range(1,xo.shape[1],2):
        pylab.plot(xo[1:,i-1],xo[1:,i])
    if 1 == xo.shape[1] % 2:
        pylab.plot(xo[1:,0],xo[1:,-1])
    pylab.xlim(xomin,xomax)
    pylab.ylim(xomin,xomax)
    pylab.subplot(224)
    for i in range(1,xc.shape[1],2):
        pylab.plot(xc[:,i-1],xc[:,i])
    if 1 == xc.shape[1] % 2:
        pylab.plot(xc[:,0],xc[:,-1])
    pylab.xlim(xcmin,xcmax)
    pylab.ylim(xcmin,xcmax)

def get_cb_phase_plot(ln):
    def gene_cb_phase_plot():
        i = 0
        while 1:
            issave = False
            if   i < 100               : issave = True
            elif i < 1000 and i%10 == 0: issave = True
            elif             i%100 == 0: issave = True
            if issave:
                phase_plot_xo(ln)
                pylab.savefig('%s/phase%010d.png'%(save_dir,i))
                tms_plot_tixo(ln)
                pylab.savefig('%s/tms%010d.png'%(save_dir,i))
            i += 1
            yield
    cb_phase_plot = gene_cb_phase_plot()
    return cb_phase_plot.next

a = make_l()
steps = a.shape[0]

nn = ElmanNet(num_o,num_c,num_i,steps)
ln = Learn(nn,iter_max)
cb_func = get_cb_phase_plot(ln)
ln.set_callback(cb_func,cb_step)

nn.set_learn_rate(learn_rate)
nn.momentum = momentum
nn.ns.to = a
nn.ns.ti[1:] = nn.ns.to[:-1]
nn.set_input()
nn.randomize_param()
nn.ns.ec *= ec_scale

os.mkdir(save_dir)
ln.learn()