# -*- coding: utf-8 -*-
"""ANN-B4.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1ubEMaX7iGpwKWBs4BcN7uE3UmeAEGebs

### Library imports
"""

import numpy as np

"""### Initializing input vectors"""

p1 = [1, -1, 1,-1, 1, -1]
p2 = [-1, 1, -1, 1, -1, 1]
p3 = [1, 1, -1, -1, 1, 1]
p4 = [-1, -1, 1, 1, -1, -1]
p = np.array([p1,p2,p3,p4])
noisy_data = np.array([1, -1, 1, -1, -1, -1])

"""### Initializing weights to 0 and then assigning them to correlation matrix"""

w = np.zeros((6,6))
for i in range(4):
  w += np.outer(p[i],p[i])
w

"""### Assigning the diagonal values to 0"""

for i in range(6):
  w[i,i] = 0
w

"""### Sign function: converts the positive values to 1 and negative to -1, keeps 0 as it is."""

def sign(x):
    return np.where(x >= 0, 1, -1)

"""### Checking of the previous state and the noisy data and fixing it iteratively"""

prev_state = np.copy(noisy_data)
for _ in range(10):
    new_state = sign(np.dot(w, prev_state))

    if np.array_equal(new_state, prev_state):
        break

    prev_state = new_state
print("Recovered pattern:", prev_state)