ナンクル力学系

学んだ事を書き連ねていこう。

グラフを見て分かるNeural Networksシリーズ〜グラフ描くためのpythonのコード〜

leave a comment »

neunet0

ニューラルネットワークで用いるアクティベーション関数の入出力の非線形性はこんな感じ

import pylab
import scipy
pylab.clf()

gx0, gx1, gx2 = 0.1, 0.3, 0.95
gy0, gy1, gy2 = 0.1, 0.3, 0.95

x = scipy.arange(-5,5,0.5)

a = pylab.axes([gx1, gy1, gx2-gx1, gy2-gy1])
pylab.plot(x,scipy.tanh(x))
pylab.axis((-5,5,-1.1,1.1))
pylab.setp(a,xticks=[],yticks=[])

t = scipy.arange(0,20,0.1)
x1 = 0.5 * scipy.sin(t)
x2 = 0.5 * scipy.sin(t) + 2.0
x3 = 0.5 * scipy.sin(t) + 4.0

a = pylab.axes([gx1, gy0, gx2-gx1, gy1-gy0])
pylab.plot(x1,t)
pylab.plot(x2,t)
pylab.plot(x3,t)
pylab.axis((-5,5,None,None))
pylab.setp(a,xticks=[-5,0,5],yticks=[])
pylab.xlabel("Input")

a = pylab.axes([gx0, gy1, gx1-gx0, gy2-gy1])
pylab.plot(t,scipy.tanh(x1))
pylab.plot(t,scipy.tanh(x2))
pylab.plot(t,scipy.tanh(x3))
pylab.axis((None,None,-1.1,1.1))
pylab.setp(a,xticks=[],yticks=[-1,0,1])
pylab.ylabel("Output")

#pylab.show()
pylab.savefig("neunet0.png")
neunet1

同じ入出力に対して、weightとbiasを変化させると出力はこのように変化する

import pylab
import scipy

t = scipy.arange(0,50,0.1)
x = scipy.sin(t)

NeuNets = [
    #( w , b )
    [ (0.5,-2.0),
      (0.5,-1.0),
      (0.5,-0.5),
      (0.5,-0.0) ],
    [ (10.,-2.0),
      (3.0,-2.0),
      (2.0,-2.0),
      (0.5,-2.0) ],
    ]
col_num=len(NeuNets)+1

pylab.clf()
pylab.subplot(col_num,1,1)
pylab.plot(t,x)
pylab.axis((None,None,-1,1))
pylab.ylabel("Input")

for i,NN in enumerate(NeuNets):
    pylab.subplot(col_num,1,i+2)
    for (w,b) in NN:
        pylab.plot(t,scipy.tanh(w*x+b),
                   label="w=%.01f b=%.01f"%(w,b))
        pylab.axis((None,None,-1,1))
        pylab.ylabel("Output")
    pylab.legend()

#pylab.show()
pylab.savefig("neunet1.png")
neunet2

and回路を作ってみて、sin波のピークが合わさった時だけ反応するネットワークが出来た

import pylab
import scipy

t  = scipy.arange(0,50,0.1)
x1 = scipy.sin(t)
x2 = scipy.sin(2*t)

pylab.clf()
pylab.subplot(2,1,1)
pylab.plot(t,x1,label="x1")
pylab.plot(t,x2,label="x2")
pylab.axis((None,None,-1,1))
pylab.ylabel("Input")
pylab.legend()

pylab.subplot(2,1,2)
pylab.plot(t,scipy.tanh( 2.0*(x1 + x2 - 0.8)))
pylab.axis((None,None,-1,1))
pylab.ylabel("Output")
pylab.legend()

#pylab.show()
pylab.savefig("neunet2.png")

Written by tkf

November 29, 2008 at 1:08 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: