Пример #1
0
    np.random.seed(2)
    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)
Пример #2
0
    np.random.seed(123)
    samples = np.zeros((N, p * p))
    T = np.random.uniform(low=-np.pi / 2, high=np.pi / 2, size=N)
    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)
Пример #3
0
    n,p = 8, 8
    epochs = 10000
    N = 5000

    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)
        s2 = np.row_stack((s2,b))

    s3 = np.array([0,0])
    for x in range(0,mag):
        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)
    np.random.seed(123)
    samples = np.zeros((N,p*p))
    T = np.random.uniform(low=-np.pi/2, high=np.pi/2, size=N)
    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)
Пример #6
0
    samples_1 = uniform(n=n, center=(0.25,0.25), scale=(0.25,0.25))
    samples_2 = uniform(n=n, center=(0.75,0.75), scale=(0.25,0.25))
    samples_3 = uniform(n=n, center=(0.25,0.75), scale=(0.25,0.25))
    samples_4 = uniform(n=n, center=(0.75,0.25), scale=(0.25,0.25))


    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)