<jupyter_start><jupyter_text>Let's memorize some digits!<jupyter_code>from sklearn import datasets
import matplotlib.pyplot as plt
#Load the digits dataset
digits = datasets.load_digits()
# Binarize the dataset
for digit_im, digit_data in zip(digits['images'], digits['data']):
    digit_im[digit_im>0] = 1
    digit_data[digit_data>0] = 1
    digit_data[digit_data==0] = -1
#Display the first digit
plt.figure(1, figsize=(3, 3))
plt.imshow(digits.images[0], cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
hop = hopfieldNetwork(np.matrix(digits.data[0]))
print(digits.data[0])
print([i.output for i in hop.neuronList])
output = np.array([i.output for i in hop.neuronList])
image_out = output.reshape((8,8))
#Display the first digit
plt.figure(1, figsize=(3, 3))
plt.imshow(image_out, cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
output[3:6] = -1
image_out = output.reshape((8,8))
#Display the first digit
plt.figure(1, figsize=(3, 3))
plt.imshow(image_out, cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
hop.mapInputToNeurons(np.matrix(output))
hop.compute()
output = np.array([i.output for i in hop.neuronList])
image_out = output.reshape((8,8))
#Display the first digit
plt.figure(1, figsize=(3, 3))
plt.imshow(image_out, cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
a * -1<jupyter_output><empty_output><jupyter_text>Let's Visualize the state changes<jupyter_code>start_image = np.matrix(digits.data[0])
perturb_image = copy.deepcopy(digits.data[0])
perturb_image[0:32] = -1


hop = hopfieldNetwork(start_image)
states = hop.runHopfieldNetwork( np.matrix(perturb_image))
pprint(states)

image_out = states[0].reshape(8,8)
from IPython import display
plt.ion()

f, ax = plt.subplots(1,1)
im = ax.imshow(states[0].reshape(8,8), cmap=plt.cm.gray_r, interpolation = 'nearest')

f.colorbar(im)
f.canvas.draw()

for i in range(len(states)):  
    try:
        im.set_data(states[i].reshape(8,8)) 
        f.canvas.draw()
        display.display(f)
        display.clear_output(wait=True) 
        plt.pause(0.1)

    except KeyboardInterrupt:
        break
plt.ioff()
plt.close()
plt.ion()

f = plt.figure(1, figsize=(3, 3))
im = plt.imshow(start_image.reshape((8,8)), cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup