Ejemplo n.º 1
0
    epochs = 20000
    N = 10000

    np.random.seed(123)
    samples = uniform(n=N) 

    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('uniform.png',dpi=150)

Ejemplo n.º 2
0
    samples = np.zeros((N, 2))
    #samples[:,0] = np.array([0.1, 0.1, 0.5, 0.9])
    #samples[:,1] = np.array([0.1, 0.9, 0.5, 0.9])
    samples[:, 0] = np.array([0.1, 0.1, 0.5, 0.9, 0.9])
    samples[:, 1] = np.array([0.1, 0.9, 0.5, 0.1, 0.9])

    print 'Neural Gas'
    np.random.seed(2)
    ng = NG((n, n, 2))
    ng.learn(samples, epochs)
    print 'Self-Organizing Map'
    np.random.seed(2)
    som = SOM((n, n, 2))
    som.learn(samples, epochs)
    print 'Dynamic Self-Organizing Map'
    np.random.seed(2)
    dsom = DSOM((n, n, 2), elasticity=1.0, lrate=0.1)
    #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('false.png', dpi=150)
Ejemplo n.º 3
0
    np.random.seed(123)
    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)
Ejemplo n.º 4
0
    for i in range(N):
        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()
Ejemplo n.º 5
0
    #samples[:,1] = np.array([0.1, 0.9, 0.5, 0.9])
    samples[:,0] = np.array([0.1, 0.1, 0.5, 0.9, 0.9])
    samples[:,1] = np.array([0.1, 0.9, 0.5, 0.1, 0.9])


    print 'Neural Gas'
    np.random.seed(2)
    ng = NG((n,n,2))
    ng.learn(samples,epochs)
    print 'Self-Organizing Map'
    np.random.seed(2)
    som = SOM((n,n,2))
    som.learn(samples,epochs)
    print 'Dynamic Self-Organizing Map'
    np.random.seed(2)
    dsom = DSOM((n,n,2), elasticity=1.0, lrate=0.1)
    #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('false.png',dpi=150)
    
        v = x/float(mag)
        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)
Ejemplo n.º 7
0
    rho   = 0.5
    u = np.random.uniform(low=0, high=1, size=N//2)
    v = np.random.uniform(low=0, high=1, size=N//2)
    theta = 2*np.pi*u
    phi = np.arccos(2*v-1)
    spheres[:N//2,0] = rho*np.cos(theta)*np.sin(phi)+.5
    spheres[:N//2,1] = rho*np.sin(theta)*np.sin(phi)+.0
    spheres[:N//2,2] = rho*np.cos(phi)+.5
    spheres[N//2:,0] = rho*np.cos(theta)*np.sin(phi)+.5
    spheres[N//2:,1] = rho*np.sin(theta)*np.sin(phi)+1.0
    spheres[N//2:,2] = rho*np.cos(phi)+.5 


    samples = sphere
    np.random.seed(123)
    net = DSOM((n,n,3), elasticity=1.0, init_method='fixed')
    I = np.random.randint(0,samples.shape[0], epochs)
    bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=epochs).start()
    plotfile = '/tmp/plot.txt'
    datafile = '/tmp/data.txt'
    rot_x, rot_z = 65,225


    for i in range(epochs):
        if i == (epochs//2):
            samples = spheres
            I = np.random.randint(0,samples.shape[0], epochs)

        if i%5 == 0:
            rot_x = 20+(1+np.cos(i/float(epochs)*4*np.pi))*45
            rot_z = (rot_z+1) % 360
Ejemplo n.º 8
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()
    
Ejemplo n.º 9
0
    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()

Ejemplo n.º 10
0
    np.random.seed(123)
    samples = np.zeros((N, 3))
    samples[:, 0] = np.random.uniform(low=0, high=1, size=N)
    samples[:, 1] = np.random.uniform(low=0, high=1, size=N)
    samples[:, 2] = np.random.uniform(low=0, high=1, size=N)
    for i in range(N):
        index = int(np.random.random() * 3)
        value = int(np.random.random() * 2)
        samples[i, index] = value
    x = samples[:, 0]
    y = samples[:, 1]
    z = samples[:, 2]

    # Learn
    np.random.seed(123)
    dsom = DSOM((n, n, 3), elasticity=1.0, init_method='fixed')
    dsom.learn(samples, epochs)

    fig = plt.figure(figsize=(10, 10))
    axes = Axes3D(fig)
    axes.scatter(x, y, z)
    C = dsom.codebook
    Cx, Cy, Cz = C[..., 0], C[..., 1], C[..., 2]
    for i in range(C.shape[0]):
        axes.plot(Cx[i, :], Cy[i, :], Cz[i, :], 'k', alpha=0.85, lw=1.5)
    for i in range(C.shape[1]):
        axes.plot(Cx[:, i], Cy[:, i], Cz[:, i], 'k', alpha=0.85, lw=1.5)
    axes.scatter(Cx.flatten(),
                 Cy.flatten(),
                 Cz.flatten(),
                 s=50,
Ejemplo n.º 11
0
    for i in range(N):
        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()

Ejemplo n.º 12
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))
Ejemplo n.º 13
0
    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=(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$",
Ejemplo n.º 14
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))
Ejemplo n.º 15
0
    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=(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)
Ejemplo n.º 16
0
    # Pick N points on a cube uniformly
    np.random.seed(123)
    samples = np.zeros((N, 3))
    samples[:, 0] = np.random.uniform(low=0, high=1, size=N)
    samples[:, 1] = np.random.uniform(low=0, high=1, size=N)
    samples[:, 2] = np.random.uniform(low=0, high=1, size=N)
    for i in range(N):
        index = int(np.random.random() * 3)
        value = int(np.random.random() * 2)
        samples[i, index] = value
    x = samples[:, 0]
    y = samples[:, 1]
    z = samples[:, 2]

    np.random.seed(123)
    net = DSOM((n, n, 3), elasticity=1.0, init_method='fixed')
    I = np.random.randint(0, samples.shape[0], epochs)
    bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=epochs).start()
    plotfile = '/tmp/plot'
    datafile = '/tmp/data.txt'
    rot_x, rot_z = 65, 225
    for i in range(epochs):
        if i % 5 == 0:
            rot_x = 20 + (1 + np.cos(i / float(epochs) * 4 * np.pi)) * 45
            rot_z = (rot_z + 1) % 360
            filename = '/tmp/image-%05d' % i
            file = open(plotfile, 'w')
            file.write('''set parametric\n''')
            file.write('''set hidden3d\n''')
            file.write('''unset key\n''')
            file.write('''unset border\n''')
Ejemplo n.º 17
0
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from network import NG, SOM, DSOM
    from distribution import uniform, normal, ring

    n = 8
    epochs = 20000
    N = 10000

    np.random.seed(12345)
    samples = normal(n=N)

    print 'Dynamic Self-Organizing Map, elasticity = 1.0'
    np.random.seed(12345)
    dsom1 = DSOM((n, n, 2), elasticity=1.0, init_method='regular')
    dsom1.learn(samples, epochs)

    print 'Dynamic Self-Organizing Map, elasticity = 2.0'
    np.random.seed(12345)
    dsom2 = DSOM((n, n, 2), elasticity=2.0, init_method='regular')
    dsom2.learn(samples, epochs)

    print 'Dynamic Self-Organizing Map, elasticity = 3.0'
    np.random.seed(12345)
    dsom3 = DSOM((n, n, 2), elasticity=3.0, init_method='regular')
    dsom3.learn(samples, epochs)

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