<jupyter_start><jupyter_text>Morphological Associative memories<jupyter_code># dependencies
import matplotlib.pyplot as plt
import pickle
import numpy as np
f = open('final_dataset.pickle','rb')
dataset = pickle.load(f)
sample_image = dataset['train_dataset'][0]
sample_label = dataset['train_labels'][0]
print(sample_label)
plt.figure()
plt.imshow(sample_image)
plt.show()
# lets make Wxx and Mxx for this images
# Wxx
x = np.array([0,0,0])
y = np.array([0,1,0])
final = np.subtract.outer(y,x)
print(final)
# 1. flatten the whole image into n-pixels
x_vectors = sample_image.flatten()
# dimensions must be of the form img_len,1
# for this x_vector the weights must be of the order 1,num_perceptrons
# but this gives me a sparse matrix of the order img_len,num_perceptrons
# what we do here is take sum row wise we will get like  [1,0,0,1] will become [2]
# and therefore we will have outputs as img_len,1

# so here to get the weights for x_vectors we have to multiply matrices of the order
# img_len,1 and 1,img_len 
#x_vectors = np.array([1,2,5,1])
weights = np.subtract.outer(x_vectors, x_vectors)
print(weights)
add_individual = np.add(weights, x_vectors)
# pg-6 now perform row wise max
result = [max(row) for row in add_individual]
np.testing.assert_array_almost_equal(x_vectors, result)
print('done')
# for k=1 dimesions of Mxx and Wxx are same<jupyter_output>done<jupyter_text>Now lets add some erosive noise to the image and then lets see the recall<jupyter_code>import cv2

erode_img = sample_image
# kernel is a pixel set like a cross( or any shape) which convolves and erodes
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(erode_img,kernel,iterations = 1)
plt.figure()
plt.imshow(erosion)
plt.show()
# Now lets try to do some recall
x_eroded = erosion
x_eroded_vector = x_eroded.flatten()

add_individual = np.add(weights, x_eroded_vector)
result = np.array([max(row) for row in add_individual])
# now lets reshape the result to 128 x 128
result.shape = (128, 128)
plt.figure()
plt.imshow(result)
plt.show()
# now lets see the amount of recall error
result = result.flatten()
np.testing.assert_array_almost_equal(result, x_vectors)
print('done 0%')<jupyter_output>done 0%
{'fraction_non_alphanumeric': 0.10365296803652968, 'fraction_numerical': 0.02054794520547945, 'mean_word_length': 5.720858895705521, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '555299', 'license': 'MIT', 'n_tokens_mistral': 820, 'n_tokens_neox': 770, 'n_words': 247, 'path': 'MAMs discussion.ipynb', 'repo_name': 'krantirk/ATR-FNN', 'size': '2190'}