def run_hf_demo_alphabet(letters,
                         initialization_noise_level=0.2,
                         random_seed=None):
    """
    Simple demo

    Args:
        letters:
        initialization_noise_level:
        random_seed:

    Returns:

    """

    # fixed size 10 for the alphabet.
    pattern_size = 10
    # pick some letters we want to store in the network
    if letters is None:
        letters = ['A', 'B', 'C', 'R', 'S', 'X', 'Y', 'Z']
    reference_pattern = 0

    # instantiate a hofpfield network
    hopfield_net = network.HopfieldNetwork(pattern_size**2)
    # for the demo, use a seed to get a reproducible pattern
    np.random.seed(random_seed)
    # load the dictionary
    abc_dict = pattern_tools.load_alphabet()
    # for each key in letters, append the pattern to the list
    pattern_list = [abc_dict[key] for key in letters]
    hfplot.plot_pattern_list(pattern_list)

    hopfield_net.store_patterns(pattern_list)
    hopfield_net.set_state_from_pattern(
        pattern_tools.get_noisy_copy(abc_dict[letters[reference_pattern]],
                                     initialization_noise_level))
    states = hopfield_net.run_with_monitoring(6)
    state_patterns = pattern_tools.reshape_patterns(states,
                                                    pattern_list[0].shape)
    hfplot.plot_state_sequence_and_overlap(state_patterns, pattern_list,
                                           reference_pattern)
    plt.show()
Пример #2
0
def run_hf_demo_alphabet(letters, initialization_noise_level=0.2, random_seed=None):
    """
    Simple demo

    Args:
        letters:
        initialization_noise_level:
        random_seed:

    Returns:

    """

    # fixed size 10 for the alphabet.
    pattern_size = 10
    # pick some letters we want to store in the network
    if letters is None:
        letters = ['A', 'B', 'C', 'R', 'S', 'X', 'Y', 'Z']
    reference_pattern = 0

    # instantiate a hofpfield network
    hopfield_net = network.HopfieldNetwork(pattern_size**2)
    # for the demo, use a seed to get a reproducible pattern
    np.random.seed(random_seed)
    # load the dictionary
    abc_dict = pattern_tools.load_alphabet()
    # for each key in letters, append the pattern to the list
    pattern_list = [abc_dict[key] for key in letters]
    hfplot.plot_pattern_list(pattern_list)

    hopfield_net.store_patterns(pattern_list)
    hopfield_net.set_state_from_pattern(
        pattern_tools.get_noisy_copy(abc_dict[letters[reference_pattern]], initialization_noise_level))
    states = hopfield_net.run_with_monitoring(6)
    state_patterns = pattern_tools.reshape_patterns(states, pattern_list[0].shape)
    hfplot.plot_state_sequence_and_overlap(state_patterns, pattern_list, reference_pattern)
    plt.show()
Пример #3
0
def main( plot = True ):
    # set a seed to reproduce the same noise in the next run
    # numpy.random.seed(123)
    
    # access the first element and get it's size (they are all of same size)
    pattern_shape = abc_dictionary['A'].shape

    # create an instance of the class HopfieldNetwork
    hopfield_net = network.HopfieldNetwork( 
            nr_neurons = pattern_shape[0]*pattern_shape[1]
            )
    
    pattern_list = [abc_dictionary[key] for key in letter_list ]

    if plot:
        plt.figure( figsize=(12, 6) )
        for i, a in enumerate(pattern_list):
            ax = plt.subplot( nRows, maxCols, i+1)
            ax.imshow( a )
            ax.axis('off')
    
    # store the patterns
    hopfield_net.store_patterns(pattern_list)
    print( "[INFO ] Saved patterns into hopfield network." )
    
    # create a noisy version of a pattern and use that to initialize the network
    l = random.choice( letter_list )

    noise_level = 0.5
    if add_extra:
        noise_level = 0.1
        print( '[INFO] More patterns than net can handle. Catastrophic'
            ' forgetting going to happen.'
        )
    noisy_init_state = add_noise( l, noise_level= noise_level)

    hopfield_net.set_state_from_pattern(noisy_init_state)
    
    # from this initial state, let the network dynamics evolve.
    states = hopfield_net.run_with_monitoring(nr_steps=4)

    # each network state is a vector. reshape it to the same shape used to create the patterns.
    states_as_patterns = pattern_tools.reshape_patterns(states, pattern_list[0].shape)

    if plot:
        for i, pat in enumerate( states_as_patterns ):
            overlap_list = pattern_tools.compute_overlap_list(pat, pattern_list)
            ax = plt.subplot( nRows, maxCols, maxCols+i+1 )
            ax.imshow( pat )
            ax.axis('off')
            ax1 = plt.subplot( nRows, maxCols, 2*maxCols+i+1)
            ax1.bar( range(len(overlap_list)), overlap_list )
            #  ax1.set_ylim( -1, 1 )
            ax1.set_xticks( range(len(overlap_list)) )
            ax1.set_xticklabels( letter_list )

        plt.tight_layout()
        outfile = 'figures/final_%s.png' % l
        plt.savefig( outfile )
        print( '|| Saved to %s' % outfile )
        plt.show()
        plt.close()

    return pattern_list, noisy_init_state, states_as_patterns
Пример #4
0
def main(plot=True):
    # set a seed to reproduce the same noise in the next run
    # numpy.random.seed(123)

    # access the first element and get it's size (they are all of same size)
    pattern_shape = abc_dictionary['A'].shape

    # create an instance of the class HopfieldNetwork
    hopfield_net = network.HopfieldNetwork(nr_neurons=pattern_shape[0] *
                                           pattern_shape[1])

    pattern_list = [abc_dictionary[key] for key in letter_list]

    if plot:
        plt.figure(figsize=(12, 6))
        for i, k in enumerate(letter_list):
            a = abc_dictionary[k]
            ax = plt.subplot(nRows, maxCols, i + 1)
            ax.imshow(a)
            ax.axis('off')
            save_letter(a, '%s.txt' % k)

    # store the patterns
    hopfield_net.store_patterns(pattern_list)
    print("[INFO ] Saved patterns into hopfield network.")

    # create a noisy version of a pattern and use that to initialize the network
    l = random.choice(letter_list)

    noise_level = 0.5
    if add_extra:
        noise_level = 0.1
        print('[INFO] More patterns than net can handle. Catastrophic'
              ' forgetting going to happen.')
    noisy_init_state = add_noise(l, noise_level=noise_level)

    hopfield_net.set_state_from_pattern(noisy_init_state)

    # from this initial state, let the network dynamics evolve.
    states = hopfield_net.run_with_monitoring(nr_steps=4)

    # each network state is a vector. reshape it to the same shape used to create the patterns.
    states_as_patterns = pattern_tools.reshape_patterns(
        states, pattern_list[0].shape)

    if plot:
        for i, pat in enumerate(states_as_patterns):
            overlap_list = pattern_tools.compute_overlap_list(
                pat, pattern_list)
            ax = plt.subplot(nRows, maxCols, maxCols + i + 1)
            save_letter(pat, '%s%s.txt' % (l, i))
            ax.imshow(pat)
            ax.axis('off')
            ax1 = plt.subplot(nRows, maxCols, 2 * maxCols + i + 1)
            ax1.bar(range(len(overlap_list)), overlap_list)
            #  ax1.set_ylim( -1, 1 )
            ax1.set_xticks(range(len(overlap_list)))
            ax1.set_xticklabels(letter_list)

        plt.tight_layout()
        outfile = 'figures/final_%s.png' % l
        plt.savefig(outfile)
        print('|| Saved to %s' % outfile)
        plt.show()
        plt.close()

    return pattern_list, noisy_init_state, states_as_patterns