Modules

hopfield.HopfieldNetwork class

class hopfield.HopfieldNetwork[source]

Bases: object

Implementation of a Hopfield Network using Hebb rule and synchronous pattern recovery.

compute_energy(s)[source]

Compute energy of given state

Parameters:

s (np.ndarray) – state for which energy needs to be computed

Returns:

energy of state s

Return type:

float

plot_weight_matrix(figsize=(5, 5))[source]

Plot weights of trained network

Parameters:

figsize (tuple of ints) – figsize for matplotlib figure, defaults to (5,5)

Returns:

None

predict(data, num_iter=20, threshold=0)[source]

Recover stored patterns from noisy images.

Parameters:
  • data (list of np.ndarray) – list of corrupted samples to be reconstructed

  • num_iter (int, defaults to 20) – number of iterations to run

  • threshold (float, defaults to 0) – activation threshold for neurons

Returns:

list of predictions

Return type:

list of np.ndarray

sync_update(init_s)[source]

Synchronous update

Parameters:

init_s (np.ndarray) – initial state; the corrupted image

Returns:

predicted state

Return type:

np.ndarray

train(train_data)[source]

Train the network using Hebbian learning rule.

Parameters:

train_data (list of np.ndarray) – list of preprocessed training images

Returns:

None

utils module

utils.get_corrupted_input(input, corruption_level)[source]

Get image with noise added

Parameters:
  • input (np.ndarray) – image array to be corrupted

  • corruption_level (np.float) – corruption level, range [0.0-1.0]

Returns:

corrupted image

Return type:

np.ndarray

utils.plot(data, test, predicted, figsize=(5, 5), savefig=False)[source]

Plot training images, corrupted inputs and predictions side by side

Parameters:
  • data (list of np.ndarray with len >=3) – training images; atleast 3

  • test (list of np.ndarray with len >=3) – corrupted (test) versions of images; atleast 3

  • predicted (list of np.ndarray with len >=3) – predictions for images in test; atleast 3

  • figsize (tuple) – size for matplotlib figure; defaults to (5, 5)

  • savefig (bool) – boolean value to save matplotlib figure; defaults to False

Returns:

None

utils.preprocessing(img)[source]

Perform thresholding to convert grayscale image to binary & flatten image

Parameters:

img (np.ndarray of shape (n, n)) – grayscale image array

Returns:

flattened binary image array

Return type:

np.ndarray of shape (n^2,)

utils.reshape(data)[source]

Reshape flattened 1D array to 2D image

Parameters:

data (np.ndarray of shape (n,)) – flattened image array

Returns:

2D image array

Return type:

np.ndarray of shape (sqrt(n), sqrt(n))