コード例 #1
0
    np.random.seed(12345)
    samples = np.zeros((N, 2))
    samples[:N / 2] = ring(n=N / 2, center=(.4, .4), radius=(0.3, 0.4))
    samples[N / 2:] = ring(n=N / 2, center=(.6, .6), radius=(0.3, 0.4))

    print 'Neural Gas'
    np.random.seed(123)
    ng = NG((n, n, 2))
    ng.learn(samples, epochs)
    print 'Self-Organizing Map'
    np.random.seed(123)
    som = SOM((n, n, 2))
    som.learn(samples, epochs)
    print 'Dynamic Self-Organizing Map'
    np.random.seed(123)
    dsom = DSOM((n, n, 2), elasticity=1.75)
    dsom.learn(samples, epochs)

    fig = plt.figure(figsize=(21, 8))
    fig.patch.set_alpha(0.0)

    axes = plt.subplot(1, 3, 1)
    ng.plot(axes)
    axes = fig.add_subplot(1, 3, 2)
    som.plot(axes)
    axes = fig.add_subplot(1, 3, 3)
    dsom.plot(axes)

    fig.savefig('double-ring.png', dpi=150)
コード例 #2
0
ファイル: figure-density.py プロジェクト: rougier/dynamic-som
    n = 8
    epochs = 20000
    N = 10000
    np.random.seed(123)
    area_1 = np.pi*0.5**2 - np.pi*0.25**2
    area_2 = np.pi*0.25**2

    n1 = int(area_1*25000)
    n2 = int(area_2*25000)
    samples = np.zeros((n1+n2,2))
    samples[:n1] = ring(n=n1, radius=(0.25,0.50))
    samples[n1:] = ring(n=n2, radius=(0.00,0.25))
    print 'Dynamic Self-Organizing Map 1'
    np.random.seed(123)
    dsom1 = DSOM((n,n,2), elasticity=1.25, init_method='fixed')
    dsom1.learn(samples,epochs)

    n1 = int(area_1*40000)
    n2 = int(area_2*10000)
    samples = np.zeros((n1+n2,2))
    samples[:n1] = ring(n=n1, radius=(0.25,0.50))
    samples[n1:] = ring(n=n2, radius=(0.00,0.25))
    print 'Dynamic Self-Organizing Map 2'
    np.random.seed(123)
    dsom2 = DSOM((n,n,2), elasticity=1.25, init_method='fixed')
    dsom2.learn(samples,epochs)

    n1 = int(area_1*10000)
    n2 = int(area_2*40000)
    samples = np.zeros((n1+n2,2))
    samples[:n1] = ring(n=n1, radius=(0.25,0.50))
コード例 #3
0
        b = np.array([v+.2, np.sin(7*(v+.2))/5 + np.random.random_sample()/3+ 0.3])
        s3 = np.row_stack((s3,b))

    samps = [s1,s2,s3]
    n = len(samps)
    epcs = [epochs//n for x in range(n)]

    print 'Self-Organizing Map'
    np.random.seed(12345)
    som = SOM((size,size,2))
    som.learn(samps,epcs)

    print 'Dynamic Self-Organizing Map'
    np.random.seed(12345)
    dsom = DSOM((size,size,2), elasticity=2)
    dsom.learn(samps,epcs)


    fig = plt.figure(figsize=(16,7))
    fig.patch.set_alpha(1.0)

    axes = plt.subplot(1,2,1)
    som.plot(axes)
    axes = plt.subplot(1,2,2)
    dsom.plot(axes)
    fig.savefig('dynamicA.png',dpi=150)
    fig.clf()

    axes = plt.subplot(1,2,1)
    som.plot_dist(axes)
    axes = plt.subplot(1,2,2)
コード例 #4
0
ファイル: figure-lena.py プロジェクト: rougier/dynamic-som
    samples = image(filename='lena.png', shape=(p,p), n=N) 

    print 'Neural Gas'
    np.random.seed(123)
    ng = NG((n,n,p*p), init_method='fixed')
    ng.learn(samples,epochs)

    print 'Self-Organizing Map'
    np.random.seed(123)
    som = SOM((n,n,p*p), init_method='fixed')
    som.learn(samples,epochs)

    print 'Dynamic Self-Organizing Map'
    np.random.seed(123)
    dsom = DSOM((n,n,p*p), elasticity=0.5, init_method='fixed')
    dsom.learn(samples,epochs)

    # fig = plt.figure(figsize=(10,10))
    # axes = plt.subplot(111, frameon=False)
    # plot(dsom,n,p)

    fig = plt.figure(figsize=(21,8))
    fig.patch.set_alpha(0.0)
    axes = plt.subplot(131, frameon=False)
    plot(ng,n,p)
    axes = plt.subplot(132, frameon=False)
    plot(som,n,p)
    axes = plt.subplot(133, frameon=False)
    plot(dsom,n,p)
    fig.savefig('image.png',dpi=150)
コード例 #5
0
    #np.random.seed(123)
    samples = uniform(n=N)
    samples[:, 0] = [0.0, 1.0]
    samples[:, 1] = [0.5, 0.5]

    fig = plt.figure(figsize=(15, 6))
    p = 120
    X, Y = np.zeros((p, )), np.zeros((p, ))
    lrate = 0.1
    for s in [0.5, 0.8, 0.85, 0.9, 0.95]:
        for i in range(p):
            elasticity = 1.0 + i * (3.0 / p)
            np.random.seed(123)
            dsom = DSOM((n, 1, 2), elasticity=elasticity, lrate=lrate)
            #dsom.codebook[...] = [0.5,0.5]
            dsom.codebook[0] = [s, 0.5]
            dsom.codebook[1] = [1 - s, 0.5]
            dsom.learn(samples, epochs, show_progress=False)
            x1, x2 = dsom.codebook[0, 0][0], dsom.codebook[-1, 0][0]
            X[i], Y[i] = elasticity, 1 - np.sqrt((x2 - x1)**2)
            print s, elasticity, Y[i]
        plt.plot(X, Y, lw=2)
    plt.legend((r'$x_0 = 1-y_0 = 0.50$', r'$x_0 = 1-y_0 = 0.20$',
                r'$x_0 = 1-y_0 = 0.15$', r'$x_0 = 1-y_0 = 0.10$',
                r'$x_0 = 1-y_0 = 0.05$'), 'upper right')
    plt.xlabel('Elasticity', fontsize=16)
    plt.ylabel('Error', fontsize=16)
    plt.title('Error as a function of initial conditions and elasticity',
              fontsize=20)
    plt.show()
コード例 #6
0
ファイル: figure-test.py プロジェクト: rougier/dynamic-som
    epochs = 1000
    N = 2

    #np.random.seed(123)
    samples = uniform(n=N) 
    samples[:,0] = [0.0,1.0]
    samples[:,1] = [0.5,0.5]

    fig = plt.figure(figsize=(12,9))


    p = 200
    X,Y = np.zeros((p,)), np.zeros((p,))
    for n in [2,4,6,8,10]:
        for i in range(p):
            elasticity = 0.5 +i*2.0/p
            np.random.seed(123)
            dsom = DSOM((n,1,2), elasticity=elasticity)
            dsom.codebook[...] = [0.5,0.5]
            dsom.learn(samples,epochs,show_progress=False)
            x1,x2 = dsom.codebook[0,0][0], dsom.codebook[-1,0][0]
            X[i],Y[i] = elasticity, 1-np.sqrt((x2-x1)**2)
            print n, elasticity, Y[i]
        plt.plot(X,Y,lw=2)

    plt.legend(('n=2', 'n=4', 'n=6', 'n=8', 'n=10'), 'upper left')
    plt.xlabel('Elasticity', fontsize=20)
    plt.ylabel('Distortion', fontsize=20)
    plt.show()

コード例 #7
0
        samples[i] = gaussian(shape=(p,p), sigma=(.5,2), theta=T[i]).flatten()

    print 'Neural Gas'
    np.random.seed(123)
    ng = NG((n,n,p*p), init_method='fixed')
    ng.learn(samples,epochs, noise=0.1)

    print 'Self-Organizing Map'
    np.random.seed(123)
    som = SOM((n,n,p*p), init_method='fixed')
    som.learn(samples,epochs, noise=0.1)

    print 'Dynamic Self-Organizing Map'
    np.random.seed(123)
    dsom = DSOM((n,n,p*p), elasticity=1.5, init_method='fixed')
    dsom.learn(samples,epochs, noise=0.1)

    fig = plt.figure(figsize=(21,8))
    fig.patch.set_alpha(0.0)

    axes = plt.subplot(131, frameon=False)
    plot(ng,n,p)
    axes = plt.subplot(132, frameon=False)
    plot(som,n,p)
    axes = plt.subplot(133, frameon=False)
    plot(dsom,n,p)
    fig.savefig('gaussian-filters.png',dpi=150)
    #plt.show()


コード例 #8
0
    n = 8
    epochs = 20000
    N = 10000
    np.random.seed(123)
    area_1 = np.pi * 0.5**2 - np.pi * 0.25**2
    area_2 = np.pi * 0.25**2

    n1 = int(area_1 * 25000)
    n2 = int(area_2 * 25000)
    samples = np.zeros((n1 + n2, 2))
    samples[:n1] = ring(n=n1, radius=(0.25, 0.50))
    samples[n1:] = ring(n=n2, radius=(0.00, 0.25))
    print 'Dynamic Self-Organizing Map 1'
    np.random.seed(123)
    dsom1 = DSOM((n, n, 2), elasticity=1.25, init_method='fixed')
    dsom1.learn(samples, epochs)

    n1 = int(area_1 * 40000)
    n2 = int(area_2 * 10000)
    samples = np.zeros((n1 + n2, 2))
    samples[:n1] = ring(n=n1, radius=(0.25, 0.50))
    samples[n1:] = ring(n=n2, radius=(0.00, 0.25))
    print 'Dynamic Self-Organizing Map 2'
    np.random.seed(123)
    dsom2 = DSOM((n, n, 2), elasticity=1.25, init_method='fixed')
    dsom2.learn(samples, epochs)

    n1 = int(area_1 * 10000)
    n2 = int(area_2 * 40000)
    samples = np.zeros((n1 + n2, 2))
    samples[:n1] = ring(n=n1, radius=(0.25, 0.50))
コード例 #9
0
    print 'Neural gas'
    np.random.seed(12345)
    ng = NG((size,size,2))
    ng.learn([samples_1,   samples_2,   samples_3,   samples_4],
             [2*epochs//8, 2*epochs//8, 2*epochs//8, 2*epochs//8])

    print 'Self-Organizing Map'
    np.random.seed(12345)
    som = SOM((size,size,2))
    som.learn([samples_1,   samples_2,   samples_3,   samples_4],
              [2*epochs//8, 2*epochs//8, 2*epochs//8, 2*epochs//8])

    print 'Dynamic Self-Organizing Map'
    np.random.seed(12345)
    dsom = DSOM((size,size,2), elasticity=2.5)
    dsom.learn([samples_1,   samples_2,   samples_3,   samples_4],
               [2*epochs//8, 2*epochs//8, 2*epochs//8, 2*epochs//8])

    fig = plt.figure(figsize=(21,8))
    fig.patch.set_alpha(0.0)

    axes = plt.subplot(1,3,1)
    ng.plot(axes)
    axes = plt.subplot(1,3,2)
    som.plot(axes)
    axes = plt.subplot(1,3,3)
    dsom.plot(axes)
    fig.savefig('dynamic.png',dpi=150)
    #plt.show()