Practical 7.Write a Python Program for Bidirectional Associative Memory With Two Pairs of Vectors
The document presents a Python program for Bidirectional Associative Memory using two pairs of input and output vectors. It computes a weight matrix using the outer product rule and defines a recall function for bidirectional retrieval. The program tests the recall functionality by retrieving output vectors from input vectors and vice versa.
Practical 7.Write a Python Program for Bidirectional Associative Memory With Two Pairs of Vectors
The document presents a Python program for Bidirectional Associative Memory using two pairs of input and output vectors. It computes a weight matrix using the outer product rule and defines a recall function for bidirectional retrieval. The program tests the recall functionality by retrieving output vectors from input vectors and vice versa.
# Compute the weight matrix using the outer product rule
W = np.dot(X.T, Y) # W = X^T * Y
# Function for recall (bidirectional retrieval)
def recall(input_vector, weight_matrix, is_forward=True): output = np.sign(np.dot(input_vector, weight_matrix)) output[output == 0] = 1 # To handle zero activations return output
# Test the recall in forward and backward direction
test_X = np.array([[1, -1, 1]]) # Given an input X, find Y recalled_Y = recall(test_X, W, is_forward=True) print("Recalled Y for given X:", recalled_Y)
test_Y = np.array([[1, 1, -1]]) # Given an output Y, find X
recalled_X = recall(test_Y, W.T, is_forward=False) print("Recalled X for given Y:", recalled_X)