@@ -0,0 +1,1109 @@
+import numpy as np%0A%0Aclass Hopfield(object):%0A%09def __init__(self, data, b=1.0, synchronous=False, random=True):%0A%09%09data = 2 * data - 1%0A%0A%09%09# Hebb learning.%0A%09%09self.weights = np.dot(data.T, data)%0A%09%09self.weights = self.weights / data.shape%5B0%5D%0A%09%09np.fill_diagonal(self.weights, 0)%0A%09%09%0A%09%09self.synchronous = synchronous%0A%09%09self.random = random%0A%09%09b = self.b%0A%0A%09def set_inputs(self, inps):%0A%09%09inps = 2 * inps - 1%0A%09%09self.activation = inps%0A%0A%09def update_neuron(self, activation):%0A%09%09if self.synchronous:%0A%09%09%09activation = np.tanh(self.b * np.sum(self.weights * activation, axis=1))%0A%09%09%09activation = np.where(activation %3E 0, 1, -1)%0A%09%09else:%0A%09%09%09order = range(self.activation.shape%5B0%5D)%0A%09%09%09if self.random:%0A%09%09%09%09np.random.shuffle(order)%0A%09%09%09for i in order:%0A%09%09%09%09activation%5Bi%5D = np.tanh(self.b * np.sum(self.weights%5Bi%5D * activation))%0A%09%09return activation%0A%0A%09def get_output(self, inps, max_iter):%0A%09%09self.set_inputs(inps)%0A%09%09for i in range(max_iter):%0A%09%09%09self.activation = self.update_neuron(self.activation)%0A%09%09return (self.activation + 1) / 2%0A%0A%09def compute_energy(self):%0A%09%09return -1 * np.dot(self.activation, np.dot(self.weights, self.activation))%0A

Implement hopfield network.