Search code examples
pythonneural-networktraining-data

How to fix hopfield library errors in python


I have some code about hopfield network. I am trying to train some letters with hebbian training and then add noise and denoise them. However, I have problem with libraries. I did a search but I cant find the right answer to my problem. Hopfieldnet library is not working and if I try to replace it I take other errors. Can u help me?

 from random import randint
import numpy as np
import HopfieldNetwork
from hopfieldnet.trainers import hebbian_training
from matplotlib import pyplot as plt


# Create the training patterns
j_pattern = np.array([[1, 1, 1, 1, 1],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0],
                      [1, 0, 1, 0, 0],
                      [1, 1, 1, 0, 0]])

a_pattern = np.array([[0, 0, 1, 0, 0],
                      [0, 1, 0, 1, 0],
                      [1, 0, 0, 0, 1],
                      [1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1]])

m_pattern = np.array([[1, 0, 0, 0, 1],
                      [1, 1, 0, 1, 1],
                      [1, 0, 1, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1],
                      [1, 0, 0, 0, 1]])

e_pattern = np.array([[1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [1, 0, 0, 0, 0],
                      [1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [1, 0, 0, 0, 0],
                      [1, 1, 1, 1, 1]])

s_pattern = np.array([[1, 1, 1, 1, 1],
                      [1, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0],
                      [0, 0, 1, 0, 0],
                      [0, 0, 0, 1, 0],
                      [0, 0, 0, 0, 1],
                      [1, 1, 1, 1, 1]])

j_pattern *= 2
j_pattern -= 1

a_pattern *= 2
a_pattern -= 1

m_pattern *= 2
m_pattern -= 1

e_pattern *= 2
e_pattern -= 1

s_pattern *= 2
s_pattern -= 1

input_patterns = np.array([j_pattern.flatten(), a_pattern.flatten(), m_pattern.flatten(), e_pattern.flatten(), s_pattern.flatten()])

# Create the neural network and train it using the training patterns
network = HopfieldNetwork(35)

hebbian_training(network, input_patterns)

# Create the test patterns by using the training patterns and adding some noise to them
# and use the neural network to denoise them
j_test = j_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    j_test[p] *= -1

j_result = network.run(j_test)

j_result.shape = (7, 5)
j_test.shape = (7, 5)

a_test = a_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    a_test[p] *= -1

a_result = network.run(a_test)

a_result.shape = (7, 5)
a_test.shape = (7, 5)

m_test = m_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    m_test[p] *= -1

m_result = network.run(m_test)

m_result.shape = (7, 5)
m_test.shape = (7, 5)

e_test = e_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    e_test[p] *= -1

e_result = network.run(e_test)

e_result.shape = (7, 5)
e_test.shape = (7, 5)

s_test = s_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    s_test[p] *= -1

s_result = network.run(s_test)

s_result.shape = (7, 5)
s_test.shape = (7, 5)

# Show the results
plt.subplot(3, 2, 1)
plt.imshow(j_test, interpolation="nearest")
plt.subplot(3, 2, 2)
plt.imshow(j_result, interpolation="nearest")

plt.subplot(3, 2, 3)
plt.imshow(a_test, interpolation="nearest")
plt.subplot(3, 2, 4)
plt.imshow(a_result, interpolation="nearest")

plt.subplot(3, 2, 5)
plt.imshow(m_test, interpolation="nearest")
plt.subplot(3, 2, 6)
plt.imshow(m_result, interpolation="nearest")

plt.subplot(3, 2, 7)
plt.imshow(e_test, interpolation="nearest")
plt.subplot(3, 2, 8)
plt.imshow(e_result, interpolation="nearest")

plt.subplot(3, 2, 9)
plt.imshow(s_test, interpolation="nearest")
plt.subplot(3, 2, 10)
plt.imshow(s_result, interpolation="nearest")

plt.show()

The error:

"C:/Users/chriss/PycharmProjects/untitled3/hopfield.py", line 3, in <module> import HopfieldNetwork ModuleNotFoundError: No module named 'HopfieldNetwork'

Solution

  • Not sure if it is actual solution to the problem. I cannot reproduce the error. Code works for me after 3 slight modifications.

    (1) Imports

    from random import randint
    import numpy as np
    from hopfieldnet.net import HopfieldNetwork  # modified import
    from hopfieldnet.trainers import hebbian_training
    from matplotlib import pyplot as plt
    

    (2) Drawing

    plt.subplot(3, 4, ...)  # 3 * 4 = 12 places for 10 plots
    

    (3) hopfieldnet module -> net.py -> class HopfieldNetwork -> method run

    update_list = list(range(self._num_inputs))  # list(range(...)) instead of just range(...)