# PyRNN / toy / ElmanNet / learn_l_anime.py

 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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()