def plot_state_sequence_and_overlap(state_sequence, pattern_list, reference_idx, color_map="brg", suptitle=None):
    """
    For each time point t ( = index of state_sequence), plots the sequence of states and the overlap (barplot)
    between state(t) and each pattern.

    Args:
        state_sequence: (list(numpy.ndarray))
        pattern_list: (list(numpy.ndarray))
        reference_idx: (int) identifies the pattern in pattern_list for which wrong pixels are colored.
    """
    if reference_idx is None:
        reference_idx = 0
    reference = pattern_list[reference_idx]
    f, ax = plt.subplots(2, len(state_sequence))
    if len(state_sequence) == 1:
        ax = [ax]
    _plot_list(ax[0, :], state_sequence, reference, "S{0}", color_map)
    for i in range(len(state_sequence)):
        overlap_list = pattern_tools.compute_overlap_list(state_sequence[i], pattern_list)
        ax[1, i].bar(range(len(overlap_list)), overlap_list)
        ax[1, i].set_title("m = {1}".format(i, round(overlap_list[reference_idx], 2)))
        ax[1, i].set_ylim([-1, 1])
        ax[1, i].get_xaxis().set_major_locator(plt.MaxNLocator(integer=True))
        if i > 0:  # show lables only for the first subplot
            ax[1, i].set_xticklabels([])
            ax[1, i].set_yticklabels([])
    if suptitle is not None:
        f.suptitle(suptitle)
    plt.show()
Example #2
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
Example #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, 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